Files
Go2_navigation/unitree_sdk2/example/b2/b2_sport_client.cpp

185 lines
4.4 KiB
C++
Raw Normal View History

#include <iostream>
#include <string>
#include <chrono>
#include <mutex>
#include <thread>
#include <pthread.h>
#include <stdexcept>
#include <unitree/robot/b2/sport/sport_client.hpp>
using namespace std;
struct TestOption
{
std::string name;
int id;
};
const vector<TestOption> option_list =
{
{"damp", 0},
{"balance_stand", 1},
{"stop_move", 2},
{"stand_down", 3},
{"recovery_stand", 4},
{"move", 5},
{"switch_gait", 6},
{"speed_level", 7},
{"hand_stand", 8},
{"auto_recovery_set", 9},
{"free_walk", 11},
{"classic_walk", 12},
{"fast_walk", 13},
{"euler", 14},
};
int ConvertToInt(const std::string &str)
{
try
{
std::stoi(str);
return std::stoi(str);
}
catch (const std::invalid_argument &)
{
return -1;
}
catch (const std::out_of_range &)
{
return -1;
}
}
class UserInterface
{
public:
UserInterface() {};
~UserInterface() {};
void terminalHandle()
{
std::string input;
std::getline(std::cin, input);
if (input.compare("list") == 0)
{
for (TestOption option : option_list)
{
std::cout << option.name << ", id: " << option.id << std::endl;
}
}
for (TestOption option : option_list)
{
if (input.compare(option.name) == 0 || ConvertToInt(input) == option.id)
{
test_option_->id = option.id;
test_option_->name = option.name;
std::cout << "Test: " << test_option_->name << ", test_id: " << test_option_->id << std::endl;
}
}
};
TestOption *test_option_;
};
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " networkInterface" << std::endl;
exit(-1);
}
unitree::robot::ChannelFactory::Instance()->Init(0, argv[1]);
TestOption test_option;
test_option.id = 1;
unitree::robot::b2::SportClient sport_client;
sport_client.SetTimeout(25.0f);
sport_client.Init();
UserInterface user_interface;
user_interface.test_option_ = &test_option;
std::cout << "Input \"list \" to list all test option ..." << std::endl;
long res_count = 0;
while (1)
{
auto time_start_trick = std::chrono::high_resolution_clock::now();
static const constexpr auto dt = std::chrono::microseconds(20000); // 50Hz
user_interface.terminalHandle();
int res = 1;
if (test_option.id == 0)
{
res = sport_client.Damp();
}
else if (test_option.id == 1)
{
res = sport_client.BalanceStand();
}
else if (test_option.id == 2)
{
res = sport_client.StopMove();
}
else if (test_option.id == 3)
{
res = sport_client.StandDown();
}
else if (test_option.id == 4)
{
res = sport_client.RecoveryStand();
}
else if (test_option.id == 5)
{
res = sport_client.Move(0.0, 0.0, 0.5);
}
else if (test_option.id == 6)
{
res = sport_client.SwitchGait(0);
}
else if (test_option.id == 7)
{
res = sport_client.SpeedLevel(1);
}
else if (test_option.id == 8)
{
res = sport_client.HandStand(true);
}
else if (test_option.id == 9)
{
res = sport_client.AutoRecoverySet(true);
}
else if (test_option.id == 11)
{
res = sport_client.FreeWalk();
}
else if (test_option.id == 12)
{
res = sport_client.ClassicWalk(true);
}
else if (test_option.id == 13)
{
res = sport_client.FastWalk(true);
}
else if (test_option.id == 14)
{
res = sport_client.Euler(0.0, 0.2, 0.0);
}
if (res < 0)
{
res_count += 1;
std::cout << "Request error for: " << option_list[test_option.id].name << ", code: " << res << ", count: " << res_count << std::endl;
}
else
{
res_count = 0;
std::cout << "Request successed: " << option_list[test_option.id].name << ", code: " << res << std::endl;
}
std::this_thread::sleep_until(time_start_trick + dt);
}
return 0;
}