Skip to content

Commit 1931aa2

Browse files
committed
Add g1 arm action example
1 parent 40c02be commit 1931aa2

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

example/g1/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ target_link_libraries(g1_arm5_sdk_dds_example unitree_sdk2)
88
add_executable(g1_arm7_sdk_dds_example high_level/g1_arm7_sdk_dds_example.cpp)
99
target_link_libraries(g1_arm7_sdk_dds_example unitree_sdk2)
1010

11+
add_executable(g1_arm_action_example high_level/g1_arm_action_example.cpp)
12+
target_link_libraries(g1_arm_action_example unitree_sdk2)
13+
1114
add_executable(g1_ankle_swing_example low_level/g1_ankle_swing_example.cpp)
1215
target_link_libraries(g1_ankle_swing_example unitree_sdk2)
1316

@@ -17,6 +20,7 @@ target_link_libraries(g1_audio_client_example unitree_sdk2)
1720
add_executable(g1_dex3_example dex3/g1_dex3_example.cpp)
1821
target_link_libraries(g1_dex3_example unitree_sdk2)
1922

23+
2024
find_package(yaml-cpp QUIET)
2125
if(yaml-cpp_FOUND)
2226
if (${yaml-cpp_VERSION} VERSION_GREATER_EQUAL "0.6")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
};

include/unitree/robot/g1/arm/g1_arm_action_api.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const std::string ARM_ACTION_API_VERSION = "1.0.0.14";
1414

1515
/*api id*/
1616
const int32_t ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION = 7106;
17+
const int32_t ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST = 7107;
1718

1819
class JsonizeArmActionCommand : public common::Jsonize {
1920
public:

include/unitree/robot/g1/arm/g1_arm_action_client.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class G1ArmActionClient : public Client {
2323
void Init() {
2424
SetApiVersion(ARM_ACTION_API_VERSION);
2525
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION);
26+
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST);
2627
}
2728

2829
/*API Call*/
@@ -36,6 +37,11 @@ class G1ArmActionClient : public Client {
3637
return Call(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION, parameter, data);
3738
}
3839

40+
int32_t GetActionList(std::string &data) {
41+
std::string parameter;
42+
return Call(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST, parameter, data);
43+
}
44+
3945
/*Action List*/
4046
std::map<std::string, int32_t> action_map = {
4147
{"release arm", 99},

include/unitree/robot/g1/arm/g1_arm_action_error.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ namespace unitree {
66
namespace robot {
77
namespace g1 {
88

9-
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_ARMSDK, 7400, "armsdk is occupied.")
10-
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_HOLDING, 7401, "The arm is holding. Only release is allowed.")
9+
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_ARMSDK, 7400, "The topic rt/armsdk is occupied.")
10+
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_HOLDING, 7401, "The arm is holding. Expecting release action(99) or the same last action id.")
1111
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID, 7402, "Invalid action id.")
12+
// The actions are only supported in fsm id {500, 501, 801};
13+
// You can subscribe the topic rt/sportmodestate to check the fsm id.
14+
// And in the state 801, the actions are only supported in the fsm mode {0, 3}.
15+
// See https://support.unitree.com/home/en/G1_developer/sport_services_interface#Expert%20interface
1216
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_INVALID_FSM_ID, 7404, "Invalid fsm id.")
1317

1418
} // namespace g1

0 commit comments

Comments
 (0)