|
| 1 | +/** |
| 2 | + * @file g1_arm_action_example.cpp |
| 3 | + * @brief This example demonstrates how to use the G1 Arm Action Client to execute predefined arm actions. |
| 4 | + */ |
| 5 | +#include "unitree/robot/g1/arm/g1_arm_action_error.hpp" |
| 6 | +#include "unitree/robot/g1/arm/g1_arm_action_client.hpp" |
| 7 | + |
| 8 | +using namespace unitree::robot::g1; |
| 9 | + |
| 10 | +int main(int argc, const char** argv) |
| 11 | +{ |
| 12 | + std::cout << " --- Unitree Robotics --- \n"; |
| 13 | + std::cout << " G1 Arm Action Example \n\n"; |
| 14 | + |
| 15 | + // Unitree DDS Initialization; arg[1] is the network interface |
| 16 | + unitree::robot::ChannelFactory::Instance()->Init(0, argc > 1 ? argv[1] : ""); |
| 17 | + |
| 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. |
| 21 | + |
| 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"; |
| 30 | + |
| 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; |
| 41 | + } |
| 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 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | +}; |
0 commit comments