跳转到内容

机器人与 DDS 接口

RobotInterface 是所有机器人接入的抽象基类,负责:

  • 创建 ROS2 node(rl_deploy)
  • 提供机器人状态读取接口
  • 提供关节命令下发接口
  • 提供电池、IMU、温度等扩展读取接口
RobotInterface(const std::string& robot_name, int dof_num)
// robot_name: 机器人名称标识 | dof_num: 自由度数量

virtual VecXf GetJointPosition() = 0; // 读取所有关节当前位置(rad)
virtual VecXf GetJointVelocity() = 0; // 读取所有关节速度(rad/s)
virtual VecXf GetJointTorque() = 0; // 读取所有关节力矩(N·m)
virtual Vec3f GetImuRpy() = 0; // 读取 IMU 欧拉角 Roll/Pitch/Yaw(rad)
virtual Vec3f GetImuAcc() = 0; // 读取 IMU 线加速度(m/s²)
virtual Vec3f GetImuOmega() = 0; // 读取 IMU 角速度,机体坐标系(rad/s)
virtual VecXf GetContactForce() { return VecXf::Zero(4); }
// 读取 4 个足端接触力估计值(N),默认返回零向量
virtual VecXf GetMotorTemperture() { return 30.*VecXf::Ones(dof_num_); }
// 读取各电机温度(°C),默认返回 30°C
virtual VecXf GetDriverTemperture() { return VecXf::Zero(dof_num_); }
// 读取各驱动器温度(°C),默认返回 0
virtual std::vector<uint16_t> GetBatteryData() { return std::vector<uint16_t>(8, 0); }
// 读取电池数据,返回 8 个 uint16_t:
// [0] 电量百分比 [1] 电压(mV) [2] 电流(mA)
// [3] 循环次数 [4] 温度×4(°C) [5-7] 保留
virtual double GetImuTimestamp() { return 0.; }
// 读取 IMU 数据时间戳(秒)
virtual std::vector<uint16_t> GetDriverStatusWord() { return std::vector<uint16_t>(dof_num_, 1); }
// 读取各驱动器状态字,1 = 正常
virtual std::vector<uint16_t> GetJointDataID() { return std::vector<uint16_t>(dof_num_, 0); }
// 读取各关节数据帧 ID,用于判断数据是否更新
virtual void RefreshRobotData() {} // 手动刷新机器人数据(仿真场景使用)

virtual void SetJointCommand(Eigen::Matrix<float, Eigen::Dynamic, 5> input) = 0;
// `input` 为 `dof_num × 5` 矩阵,每行 5 列依次为 `kp`(位置刚度)、`position`(目标位置)、`kd`(速度阻尼)、`velocity`(目标速度)、`torque`(前馈力矩),底层执行公式:`torque = kp × (posDes - pos) + kd × (velDes - vel) + torque_ff`。
virtual MatXf GetJointCommand() { return joint_cmd_; }
// 读取当前已下发的关节命令矩阵

DdsInterface 继承 RobotInterface,是实际的 DDS 连接实现,通过 ROS2 话题订阅机器人状态反馈并发布关节指令。

// 发布关节命令
joint_cmd_pub_ = node_->create_publisher<drdds::msg::JointsDataCmd>("/JOINTS_CMD", 10);
// 订阅关节反馈
joint_data_sub_ = node_->create_subscription<drdds::msg::JointsData>("/JOINTS_DATA", 10, Handler);
// 订阅 IMU 数据
imu_data_sub_ = node_->create_subscription<drdds::msg::ImuData>("/IMU_DATA", 10, HandlerIMU);
// 订阅电池数据
health_data_sub_ = node_->create_subscription<drdds::msg::BatteryData>("/BATTERY_DATA", 10, HandlerHealth);

SetJointCommand 将输入矩阵映射到 DDS 消息,自动应用偏置和方向修正:

virtual void SetJointCommand(Eigen::Matrix<float, Eigen::Dynamic, 5> input) {
...
}
virtual void ResetJointError();
// 依次发送 Disable(1) → ErrorReset(17) → Enable(2) → GetStatusWord(23) 四个控制字,用于清除电机驱动器的故障状态

void Handler(const drdds::msg::JointsData::SharedPtr msg);
// 关节数据回调:解析位置/速度/力矩/温度/驱动器状态,自动应用方向反转和偏置补偿
void HandlerIMU(const drdds::msg::ImuData::SharedPtr msg);
// IMU 数据回调:解析欧拉角(°→rad)、加速度、角速度
void HandlerHealth(const drdds::msg::BatteryData::SharedPtr msg);
// 电池数据回调:解析电量百分比

ResetPositionOffset() 在启动时自动检测各关节实际位置,动态修正偏置值,避免跨越关节奇点。

void ResetPositionOffset() {
...
// 髋前摆(index % 4 == 1):超出 ±140° 时补偿
for (int i = 1; i < dof_num_; i += 4) { ... }
// 膝关节(index % 4 == 2):超出 ±164° 时补偿
for (int i = 2; i < dof_num_; i += 4) { ... }
}

M20Interface 继承 DdsInterface,负责 M20 特有的关节偏置、方向和真机适配。

M20Interface(const std::string &robot_name) : DdsInterface(robot_name, 16)

在构造时配置偏置和方向:

float init_pos_offset[16] = {
-25, -131, 160, 0, // 左前腿:髋侧摆、髋前摆、膝、轮
25, -131, 160, 0, // 右前腿
-25, 131, -160, 0, // 左后腿
25, 131, -160, 0 // 右后腿
};
float joint_dir[16] = {
1, 1, -1, 1, // 左前腿方向
1, -1, 1, -1, // 右前腿方向
-1, 1, -1, 1, // 左后腿方向
-1, -1, 1, -1 // 右后腿方向
};

SetJointCommand 对轮关节强制置 kp = 0,使轮子以速度/力矩模式运行:

virtual void SetJointCommand(Eigen::Matrix<float, Eigen::Dynamic, 5> input) {
for (int i = 0; i < dof_num_; ++i) {
// ... 同 DdsInterface 的映射逻辑 ...
if (i % 4 == 3) {
msg.data.joints_data[i].kp = 0; // 轮关节禁用位置环,使用速度/力矩模式
}
}
}

SafeController 以 1ms 周期运行安全检查,检测到异常时自动切换安全模式:

检查项检测逻辑触发动作
驱动器状态status_word != 1safe_control_mode = 2(坐下)
关节数据异常位置/速度/力矩含 NaNsafe_control_mode = 3(关节阻尼)
电机过温温度 > 100°Csafe_control_mode = 2(坐下)
IMU 数据异常RPY/Acc/Omega 含 NaNsafe_control_mode = 2(坐下)
IMU 超时时间戳不再更新safe_control_mode = 2(坐下)

错误状态通过 RobotErrorState 位域记录,共 11 个错误位:

位含义
bit 0IMU 数值异常
bit 1IMU 更新超时
bit 2命令心跳超时
bit 3驱动器故障
bit 4电机过温警告
bit 5关节数值异常
bit 6关节更新异常
bit 7电池低电量警告
bit 8电池过温警告
bit 9CPU 过温警告
bit 10CPU 降频警告