|
4 | 4 | */ |
5 | 5 | #include "unitree/robot/g1/arm/g1_arm_action_error.hpp" |
6 | 6 | #include "unitree/robot/g1/arm/g1_arm_action_client.hpp" |
| 7 | +#include <boost/program_options.hpp> |
7 | 8 |
|
| 9 | +namespace po = boost::program_options; |
| 10 | +using namespace unitree::robot; |
8 | 11 | using namespace unitree::robot::g1; |
9 | 12 |
|
10 | 13 | int main(int argc, const char** argv) |
11 | 14 | { |
12 | 15 | std::cout << " --- Unitree Robotics --- \n"; |
13 | 16 | std::cout << " G1 Arm Action Example \n\n"; |
14 | 17 |
|
15 | | - // Unitree DDS Initialization; arg[1] is the network interface |
16 | | - unitree::robot::ChannelFactory::Instance()->Init(0, argc > 1 ? argv[1] : ""); |
| 18 | + // Parse command line arguments |
| 19 | + po::options_description desc("Unitree G1 Arm Action Example."); |
| 20 | + desc.add_options() |
| 21 | + ("help,h", "show help message") |
| 22 | + ("network,n", po::value<std::string>()->default_value(""), "dds network interface") |
| 23 | + ("list,l", "list all supported actions") |
| 24 | + ("id,i", po::value<int>(), "action id to execute, 0 to list all supported actions") |
| 25 | + ("name", po::value<std::string>(), "custom action name to execute") |
| 26 | + ("stop", "stop the current custom action") |
| 27 | + ; |
| 28 | + po::variables_map vm; |
| 29 | + po::store(po::parse_command_line(argc, argv, desc), vm); |
| 30 | + po::notify(vm); |
| 31 | + if(argc < 2 || vm.count("help")) { |
| 32 | + std::cout << desc << std::endl; |
| 33 | + return 0; |
| 34 | + } |
17 | 35 |
|
18 | | - auto client = std::make_shared<unitree::robot::g1::G1ArmActionClient>(); |
19 | | - client->Init(); |
20 | | - client->SetTimeout(10.f); // All actions will last less than 10 seconds. |
| 36 | + // DDS Init |
| 37 | + ChannelFactory::Instance()->Init(0, vm["network"].as<std::string>()); |
21 | 38 |
|
22 | | - std::cout << "Usage: \n"; |
23 | | - std::cout << " - 0: print supported actions.\n"; |
24 | | - std::cout << " - an id: execute an action.\n"; |
25 | | - std::cout << "Attention: \n"; |
26 | | - std::cout << " Some actions will not be displayed on the APP, \n"; |
27 | | - std::cout << " but can be executed by the program.\n"; |
28 | | - std::cout << " These actions may cause the robot to fall,\n"; |
29 | | - std::cout << " so please execute them with caution.\n"; |
| 39 | + auto client = std::make_shared<G1ArmActionClient>(); |
| 40 | + client->Init(); |
| 41 | + // Attention: unitree actions are all less than 10s, |
| 42 | + // but the custom actions may be longer. |
| 43 | + client->SetTimeout(10.f); |
30 | 44 |
|
31 | | - int32_t action_id = 0; |
32 | | - std::string line; |
33 | | - while (true) { |
34 | | - std::cout << "\nEnter action ID: .\n"; |
35 | | - std::getline(std::cin, line); |
36 | | - try { |
37 | | - action_id = std::stoi(line); |
38 | | - } catch (const std::exception&) { |
39 | | - std::cout << "Invalid input. Please enter an integer.\n"; |
40 | | - continue; |
| 45 | + if(vm.count("list") || (vm.count("id") && vm["id"].as<int>() == 0)){ |
| 46 | + std::string action_list_data; |
| 47 | + int32_t ret = client->GetActionList(action_list_data); |
| 48 | + if (ret != 0) { |
| 49 | + std::cerr << "Failed to get action list, error code: " << ret << "\n"; |
41 | 50 | } |
42 | | - |
43 | | - if (action_id == 0) { |
44 | | - std::string action_list_data; |
45 | | - int32_t ret = client->GetActionList(action_list_data); |
46 | | - if (ret != 0) { |
47 | | - std::cerr << "Failed to get action list, error code: " << ret << "\n"; |
48 | | - continue; |
49 | | - } |
50 | | - std::cout << "Available actions:\n" << action_list_data << std::endl; |
51 | | - } else { |
52 | | - int32_t ret = client->ExecuteAction(action_id); |
53 | | - if(ret != 0) { |
54 | | - switch (ret) |
55 | | - { |
56 | | - case UT_ROBOT_ARM_ACTION_ERR_ARMSDK: |
57 | | - std::cout << UT_ROBOT_ARM_ACTION_ERR_ARMSDK_DESC << std::endl; |
58 | | - break; |
59 | | - case UT_ROBOT_ARM_ACTION_ERR_HOLDING: |
60 | | - std::cout << UT_ROBOT_ARM_ACTION_ERR_HOLDING_DESC << std::endl; |
61 | | - break; |
62 | | - case UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID: |
63 | | - std::cout << UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID_DESC << std::endl; |
64 | | - break; |
65 | | - case UT_ROBOT_ARM_ACTION_ERR_INVALID_FSM_ID: |
66 | | - std::cout << "The actions are only supported in fsm id {500, 501, 801}" << std::endl; |
67 | | - std::cout << "You can subscribe the topic rt/sportmodestate to check the fsm id." << std::endl; |
68 | | - std::cout << "And in the state 801, the actions are only supported in the fsm mode {0, 3}." << std::endl; |
69 | | - std::cout << "If an error is still returned at this point, ignore this action."; |
70 | | - break; |
71 | | - default: |
72 | | - std::cerr << "Execute action failed, error code: " << ret << std::endl; |
73 | | - break; |
74 | | - } |
| 51 | + std::cout << "Available actions:\n" << action_list_data << std::endl; |
| 52 | + } else if (vm.count("id")) { |
| 53 | + int32_t ret = client->ExecuteAction(vm["id"].as<int>()); |
| 54 | + if(ret != 0) { |
| 55 | + switch (ret) |
| 56 | + { |
| 57 | + case UT_ROBOT_ARM_ACTION_ERR_ARMSDK: |
| 58 | + std::cout << UT_ROBOT_ARM_ACTION_ERR_ARMSDK_DESC << std::endl; |
| 59 | + break; |
| 60 | + case UT_ROBOT_ARM_ACTION_ERR_HOLDING: |
| 61 | + std::cout << UT_ROBOT_ARM_ACTION_ERR_HOLDING_DESC << std::endl; |
| 62 | + break; |
| 63 | + case UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID: |
| 64 | + std::cout << UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID_DESC << std::endl; |
| 65 | + break; |
| 66 | + case UT_ROBOT_ARM_ACTION_ERR_INVALID_FSM_ID: |
| 67 | + std::cout << "The actions are only supported in fsm id {500, 501, 801}" << std::endl; |
| 68 | + std::cout << "You can subscribe the topic rt/sportmodestate to check the fsm id." << std::endl; |
| 69 | + std::cout << "And in the state 801, the actions are only supported in the fsm mode {0, 3}." << std::endl; |
| 70 | + std::cout << "If an error is still returned at this point, ignore this action."; |
| 71 | + break; |
| 72 | + default: |
| 73 | + std::cerr << "Execute action failed, error code: " << ret << std::endl; |
| 74 | + break; |
75 | 75 | } |
76 | 76 | } |
| 77 | + } else if (vm.count("name")) { |
| 78 | + int32_t ret = client->ExecuteAction(vm["name"].as<std::string>()); |
| 79 | + if(ret != 0) std::cout << "Execute custom action failed, error code: " << ret << std::endl; |
| 80 | + } else if (vm.count("stop")) { |
| 81 | + int32_t ret = client->StopCustomAction(); |
77 | 82 | } |
78 | 83 |
|
79 | 84 | return 0; |
|
0 commit comments