This commit is contained in:
2025-09-24 10:53:28 +08:00
commit f8e4df77fb
856 changed files with 140098 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
#pragma once
#include <unitree/common/json/jsonize.hpp>
#include <variant>
namespace unitree {
namespace robot {
namespace g1 {
/*service name*/
const std::string ARM_ACTION_SERVICE_NAME = "arm";
/*api version*/
const std::string ARM_ACTION_API_VERSION = "1.0.0.14";
/*api id*/
const int32_t ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION = 7106;
const int32_t ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST = 7107;
class JsonizeArmActionCommand : public common::Jsonize {
public:
JsonizeArmActionCommand() {}
~JsonizeArmActionCommand() {}
void fromJson(common::JsonMap &json) {
common::FromJson(json["data"], action_id);
}
void toJson(common::JsonMap &json) const {
common::ToJson(action_id, json["data"]);
}
int32_t action_id;
};
} // namespace g1
} // namespace robot
} // namespace unitree

View File

@@ -0,0 +1,69 @@
#pragma once
#include <unitree/robot/client/client.hpp>
#include <unitree/robot/go2/public/jsonize_type.hpp>
#include "g1_arm_action_api.hpp"
namespace unitree {
namespace robot {
namespace g1 {
/**
* @brief Arm action client
*
* The arm action server provides some upper body actions.
* The controller is based on the `rt/arm_sdk` interface.
*/
class G1ArmActionClient : public Client {
public:
G1ArmActionClient() : Client(ARM_ACTION_SERVICE_NAME, false) {}
~G1ArmActionClient() {}
/*Init*/
void Init() {
SetApiVersion(ARM_ACTION_API_VERSION);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST);
}
/*API Call*/
int32_t ExecuteAction(int32_t action_id) {
std::string parameter, data;
JsonizeArmActionCommand json;
json.action_id = action_id;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION, parameter, data);
}
int32_t GetActionList(std::string &data) {
std::string parameter;
return Call(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST, parameter, data);
}
/*Action List*/
std::map<std::string, int32_t> action_map = {
{"release arm", 99},
{"two-hand kiss", 11},
{"left kiss", 12},
{"right kiss", 12},
{"hands up", 15},
{"clap", 17},
{"high five", 18},
{"hug", 19},
{"heart", 20},
{"right heart", 21},
{"reject", 22},
{"right hand up", 23},
{"x-ray", 24},
{"face wave", 25},
{"high wave", 26},
{"shake hand", 27},
};
};
} // namespace g1
} // namespace robot
} // namespace unitree

View File

@@ -0,0 +1,20 @@
#pragma once
#include <unitree/common/decl.hpp>
namespace unitree {
namespace robot {
namespace g1 {
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_ARMSDK, 7400, "The topic rt/armsdk is occupied.")
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_HOLDING, 7401, "The arm is holding. Expecting release action(99) or the same last action id.")
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_INVALID_ACTION_ID, 7402, "Invalid action id.")
// The actions are only supported in fsm id {500, 501, 801};
// You can subscribe the topic rt/sportmodestate to check the fsm id.
// And in the state 801, the actions are only supported in the fsm mode {0, 3}.
// See https://support.unitree.com/home/en/G1_developer/sport_services_interface#Expert%20interface
UT_DECL_ERR(UT_ROBOT_ARM_ACTION_ERR_INVALID_FSM_ID, 7404, "Invalid fsm id.")
} // namespace g1
} // namespace robot
} // namespace unitree

View File

@@ -0,0 +1,94 @@
#ifndef __UT_ROBOT_G1_AUDIO_API_HPP__
#define __UT_ROBOT_G1_AUDIO_API_HPP__
#include <unitree/common/json/jsonize.hpp>
// #include <variant>
namespace unitree {
namespace robot {
namespace g1 {
/*service name*/
const std::string AUDIO_SERVICE_NAME = "voice";
/*api version*/
const std::string AUDIO_API_VERSION = "1.0.0.0";
/*api id*/
const int32_t ROBOT_API_ID_AUDIO_TTS = 1001;
const int32_t ROBOT_API_ID_AUDIO_ASR = 1002;
const int32_t ROBOT_API_ID_AUDIO_START_PLAY = 1003;
const int32_t ROBOT_API_ID_AUDIO_STOP_PLAY = 1004;
const int32_t ROBOT_API_ID_AUDIO_GET_VOLUME = 1005;
const int32_t ROBOT_API_ID_AUDIO_SET_VOLUME = 1006;
const int32_t ROBOT_API_ID_AUDIO_SET_RGB_LED = 1010;
class TtsMakerParameter : public common::Jsonize {
public:
TtsMakerParameter() {}
~TtsMakerParameter() {}
void fromJson(common::JsonMap &json) {}
void toJson(common::JsonMap &json) const {
common::ToJson(index, json["index"]);
common::ToJson(speaker_id, json["speaker_id"]);
common::ToJson(text, json["text"]);
}
int32_t index = 0;
uint16_t speaker_id = 0;
std::string text;
};
class PlayStreamParameter : public common::Jsonize {
public:
PlayStreamParameter() {}
~PlayStreamParameter() {}
void fromJson(common::JsonMap &json) {}
void toJson(common::JsonMap &json) const {
common::ToJson(app_name, json["app_name"]);
common::ToJson(stream_id, json["stream_id"]);
}
std::string app_name;
std::string stream_id;
};
class PlayStopParameter : public common::Jsonize {
public:
PlayStopParameter() {}
~PlayStopParameter() {}
void fromJson(common::JsonMap &json) {}
void toJson(common::JsonMap &json) const {
common::ToJson(app_name, json["app_name"]);
}
std::string app_name;
};
class LedControlParameter : public common::Jsonize {
public:
LedControlParameter() {}
~LedControlParameter() {}
void fromJson(common::JsonMap &json) {}
void toJson(common::JsonMap &json) const {
common::ToJson(R, json["R"]);
common::ToJson(G, json["G"]);
common::ToJson(B, json["B"]);
}
uint8_t R;
uint8_t G;
uint8_t B;
};
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_AUDIO_API_HPP__

View File

@@ -0,0 +1,110 @@
#ifndef __UT_ROBOT_G1_AUDIO_CLIENT_HPP__
#define __UT_ROBOT_G1_AUDIO_CLIENT_HPP__
#include <limits>
#include <unitree/robot/client/client.hpp>
#include <unitree/robot/go2/public/jsonize_type.hpp>
#include "g1_audio_api.hpp"
namespace unitree {
namespace robot {
namespace g1 {
class AudioClient : public Client {
public:
AudioClient() : Client(AUDIO_SERVICE_NAME, false) {}
~AudioClient() {}
/*Init*/
void Init() {
SetApiVersion(AUDIO_API_VERSION);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_TTS);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_ASR);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_START_PLAY);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_STOP_PLAY);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_GET_VOLUME);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_SET_VOLUME);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_AUDIO_SET_RGB_LED);
};
/*API Call*/
int32_t TtsMaker(const std::string& text, int32_t speaker_id) {
std::string parameter, data;
TtsMakerParameter json;
json.index = tts_index++;
json.text = text;
json.speaker_id = speaker_id;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_AUDIO_TTS, parameter, data);
}
int32_t GetVolume(uint8_t& volume) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_AUDIO_GET_VOLUME, parameter, data);
if (ret == 0) {
unitree::robot::go2::JsonizeCommObjInt json;
json.name = "volume";
common::FromJsonString(data, json);
volume = json.value;
}
return ret;
}
int32_t SetVolume(uint8_t volume) {
std::string parameter, data;
unitree::robot::go2::JsonizeCommObjInt json;
json.value = volume;
json.name = "volume";
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_AUDIO_SET_VOLUME, parameter, data);
}
int32_t PlayStream(std::string app_name, std::string stream_id,
std::vector<uint8_t> pcm_data) {
std::string parameter;
PlayStreamParameter json;
json.app_name = app_name;
json.stream_id = stream_id;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_AUDIO_START_PLAY, parameter, pcm_data);
}
int32_t PlayStop(std::string app_name) {
std::string parameter, data;
PlayStopParameter json;
json.app_name = app_name;
parameter = common::ToJsonString(json);
Call(ROBOT_API_ID_AUDIO_STOP_PLAY, parameter, data);
return 0;
}
int32_t LedControl(uint8_t R, uint8_t G, uint8_t B) {
std::string parameter, data;
LedControlParameter json;
json.R = R;
json.G = G;
json.B = B;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_AUDIO_SET_RGB_LED, parameter, data);
}
private:
uint32_t tts_index = 0;
};
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_AUDIO_CLIENT_HPP__

View File

@@ -0,0 +1,14 @@
#ifndef __UT_ROBOT_G1_AUDIO_ERROR_HPP__
#define __UT_ROBOT_G1_AUDIO_ERROR_HPP__
#include <unitree/common/decl.hpp>
namespace unitree {
namespace robot {
namespace g1 {
UT_DECL_ERR(UT_ROBOT_AUDIO_ERR_COMM, 100, "Invalid parameter ")
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_AUDIO_ERROR_HPP__

View File

@@ -0,0 +1,72 @@
#ifndef __UT_ROBOT_G1_LOCO_API_HPP__
#define __UT_ROBOT_G1_LOCO_API_HPP__
#include <unitree/common/json/jsonize.hpp>
#include <variant>
namespace unitree {
namespace robot {
namespace g1 {
/*service name*/
const std::string LOCO_SERVICE_NAME = "sport";
/*api version*/
const std::string LOCO_API_VERSION = "1.0.0.0";
/*api id*/
const int32_t ROBOT_API_ID_LOCO_GET_FSM_ID = 7001;
const int32_t ROBOT_API_ID_LOCO_GET_FSM_MODE = 7002;
const int32_t ROBOT_API_ID_LOCO_GET_BALANCE_MODE = 7003;
const int32_t ROBOT_API_ID_LOCO_GET_SWING_HEIGHT = 7004;
const int32_t ROBOT_API_ID_LOCO_GET_STAND_HEIGHT = 7005;
const int32_t ROBOT_API_ID_LOCO_GET_PHASE = 7006; // deprecated
const int32_t ROBOT_API_ID_LOCO_SET_FSM_ID = 7101;
const int32_t ROBOT_API_ID_LOCO_SET_BALANCE_MODE = 7102;
const int32_t ROBOT_API_ID_LOCO_SET_SWING_HEIGHT = 7103;
const int32_t ROBOT_API_ID_LOCO_SET_STAND_HEIGHT = 7104;
const int32_t ROBOT_API_ID_LOCO_SET_VELOCITY = 7105;
const int32_t ROBOT_API_ID_LOCO_SET_ARM_TASK = 7106;
const int32_t ROBOT_API_ID_LOCO_SET_SPEED_MODE = 7107;
using LocoCmd =
std::map<std::string, std::variant<int, float, std::vector<float>>>;
class JsonizeDataVecFloat : public common::Jsonize {
public:
JsonizeDataVecFloat() {}
~JsonizeDataVecFloat() {}
void fromJson(common::JsonMap &json) { common::FromJson(json["data"], data); }
void toJson(common::JsonMap &json) const {
common::ToJson(data, json["data"]);
}
std::vector<float> data;
};
class JsonizeVelocityCommand : public common::Jsonize {
public:
JsonizeVelocityCommand() {}
~JsonizeVelocityCommand() {}
void fromJson(common::JsonMap &json) {
common::FromJson(json["velocity"], velocity);
common::FromJson(json["duration"], duration);
}
void toJson(common::JsonMap &json) const {
common::ToJson(velocity, json["velocity"]);
common::ToJson(duration, json["duration"]);
}
std::vector<float> velocity;
float duration;
};
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_LOCO_API_HPP__

View File

@@ -0,0 +1,253 @@
#ifndef __UT_ROBOT_G1_LOCO_CLIENT_HPP__
#define __UT_ROBOT_G1_LOCO_CLIENT_HPP__
#include "g1_loco_api.hpp"
#include <limits>
#include <unitree/robot/client/client.hpp>
#include <unitree/robot/go2/public/jsonize_type.hpp>
namespace unitree {
namespace robot {
namespace g1 {
class LocoClient : public Client {
public:
LocoClient() : Client(LOCO_SERVICE_NAME, false) {}
~LocoClient() {}
/*Init*/
void Init() {
SetApiVersion(LOCO_API_VERSION);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_FSM_ID);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_FSM_MODE);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_BALANCE_MODE);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_SWING_HEIGHT);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_STAND_HEIGHT);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_GET_PHASE); // deprecated
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_FSM_ID);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_BALANCE_MODE);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_SWING_HEIGHT);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_STAND_HEIGHT);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_VELOCITY);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_ARM_TASK);
UT_ROBOT_CLIENT_REG_API_NO_PROI(ROBOT_API_ID_LOCO_SET_SPEED_MODE);
};
/*Low Level API Call*/
int32_t GetFsmId(int& fsm_id) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_FSM_ID, parameter, data);
if (ret == 0) {
go2::JsonizeDataInt json;
common::FromJsonString(data, json);
fsm_id = json.data;
}
return ret;
}
int32_t GetFsmMode(int& fsm_mode) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_FSM_MODE, parameter, data);
if (ret == 0) {
go2::JsonizeDataInt json;
common::FromJsonString(data, json);
fsm_mode = json.data;
}
return ret;
}
int32_t GetBalanceMode(int& balance_mode) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_BALANCE_MODE, parameter, data);
if (ret == 0) {
go2::JsonizeDataInt json;
common::FromJsonString(data, json);
balance_mode = json.data;
}
return ret;
}
int32_t GetSwingHeight(float& swing_height) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_SWING_HEIGHT, parameter, data);
if (ret == 0) {
go2::JsonizeDataFloat json;
common::FromJsonString(data, json);
swing_height = json.data;
}
return ret;
}
int32_t GetStandHeight(float& stand_height) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_STAND_HEIGHT, parameter, data);
if (ret == 0) {
go2::JsonizeDataFloat json;
common::FromJsonString(data, json);
stand_height = json.data;
}
return ret;
}
int32_t GetPhase(std::vector<float>& phase) {
std::string parameter, data;
int32_t ret = Call(ROBOT_API_ID_LOCO_GET_PHASE, parameter, data);
if (ret == 0) {
JsonizeDataVecFloat json;
common::FromJsonString(data, json);
phase = json.data;
}
return ret;
}
int32_t SetFsmId(int fsm_id) {
std::string parameter, data;
go2::JsonizeDataInt json;
json.data = fsm_id;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_FSM_ID, parameter, data);
}
int32_t SetBalanceMode(int balance_mode) {
std::string parameter, data;
go2::JsonizeDataInt json;
json.data = balance_mode;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_BALANCE_MODE, parameter, data);
}
int32_t SetSwingHeight(float swing_height) {
std::string parameter, data;
go2::JsonizeDataFloat json;
json.data = swing_height;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_SWING_HEIGHT, parameter, data);
}
int32_t SetStandHeight(float stand_height) {
std::string parameter, data;
go2::JsonizeDataFloat json;
json.data = stand_height;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_STAND_HEIGHT, parameter, data);
}
int32_t SetVelocity(float vx, float vy, float omega, float duration = 1.f) {
std::string parameter, data;
JsonizeVelocityCommand json;
std::vector<float> velocity = {vx, vy, omega};
json.velocity = velocity;
json.duration = duration;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_VELOCITY, parameter, data);
}
int32_t SetTaskId(int task_id) {
std::string parameter, data;
go2::JsonizeDataInt json;
json.data = task_id;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_ARM_TASK, parameter, data);
}
/*High Level API Call*/
int32_t Damp() { return SetFsmId(1); }
int32_t Start() { return SetFsmId(500); }
int32_t Squat() { return SetFsmId(2); }
int32_t Sit() { return SetFsmId(3); }
int32_t StandUp() { return SetFsmId(4); }
int32_t ZeroTorque() { return SetFsmId(0); }
int32_t StopMove() { return SetVelocity(0.f, 0.f, 0.f); }
int32_t HighStand() { return SetStandHeight(std::numeric_limits<uint32_t>::max()); }
int32_t LowStand() { return SetStandHeight(std::numeric_limits<uint32_t>::min()); }
int32_t Move(float vx, float vy, float vyaw, bool continous_move) {
return SetVelocity(vx, vy, vyaw, continous_move ? 864000.f : 1.f);
}
int32_t Move(float vx, float vy, float vyaw) { return Move(vx, vy, vyaw, continous_move_); }
int32_t BalanceStand() { return SetBalanceMode(0); }
int32_t ContinuousGait(bool flag) { return SetBalanceMode(flag ? 1 : 0); }
int32_t SwitchMoveMode(bool flag) {
continous_move_ = flag;
return 0;
}
int32_t WaveHand(bool turn_flag = false) { return SetTaskId(turn_flag ? 1 : 0); }
int32_t ShakeHand(int stage = -1) {
switch (stage) {
case 0:
first_shake_hand_stage_ = false;
return SetTaskId(2);
case 1:
first_shake_hand_stage_ = true;
return SetTaskId(3);
default:
first_shake_hand_stage_ = !first_shake_hand_stage_;
return SetTaskId(first_shake_hand_stage_ ? 3 : 2);
}
}
int32_t SetSpeedMode(int speed_mode) {
std::string parameter, data;
go2::JsonizeDataInt json;
json.data = speed_mode;
parameter = common::ToJsonString(json);
return Call(ROBOT_API_ID_LOCO_SET_SPEED_MODE, parameter, data);
}
private:
bool continous_move_ = false;
bool first_shake_hand_stage_ = true;
};
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_LOCO_CLIENT_HPP__

View File

@@ -0,0 +1,17 @@
#ifndef __UT_ROBOT_G1_LOCO_ERROR_HPP__
#define __UT_ROBOT_G1_LOCO_ERROR_HPP__
#include <unitree/common/decl.hpp>
namespace unitree {
namespace robot {
namespace g1 {
UT_DECL_ERR(UT_ROBOT_LOCO_ERR_LOCOSTATE_NOT_AVAILABLE, 7301,
"LocoState not available.")
UT_DECL_ERR(UT_ROBOT_LOCO_ERR_INVALID_FSM_ID, 7302, "Invalid fsm id.")
UT_DECL_ERR(UT_ROBOT_LOCO_ERR_INVALID_TASK_ID, 7303, "Invalid task id.")
} // namespace g1
} // namespace robot
} // namespace unitree
#endif // __UT_ROBOT_G1_LOCO_ERROR_HPP__