State(小脑)
fd_api State 说明
天链机器人 · 位置/状态读取 · 变化订阅 · 空闲与健康查询(纯 C++ 版)
State 模块 · 19 接口 · 机械臂状态仅 C++一、概述
State 模块(命名空间 fd_robot::State,头文件 #include "fd_api/state.h")是天链机器人 fd_api 的「状态读取」半边,与 Control(控制)模块相对。它提供三类能力:
- 位置/状态读取:同步读取各外设(升降/头部/腰部/手部/机械臂)当前值。
- 变化订阅:异步订阅外设状态变化,更新时在回调线程中触发回调。
- 空闲与健康查询:整机空闲状态、外设健康、系统资源(CPU/内存/存储)查询与订阅。
上层应用程序
#include "fd_api/state.h"
▼
#include "fd_api/state.h"
fd_api State(状态)
get_* 同步读 · sub_* 订阅 · runtime/health 查询
回调线程池 · 订阅管理 unsubscribe
▼
get_* 同步读 · sub_* 订阅 · runtime/health 查询
回调线程池 · 订阅管理 unsubscribe
ROS 2 通信层
/diagnostics · /fd_robot/robot_idle_status · 外设状态话题
▼
/diagnostics · /fd_robot/robot_idle_status · 外设状态话题
机器人硬件外设
升降 · 头部 · 腰部 · 手部 · 机械臂 · 系统
升降 · 头部 · 腰部 · 手部 · 机械臂 · 系统
与 Control 的分工边界:Control 负责「发命令」、State 负责「读状态」。两者各自
Init()、各自独立 ROS 节点(默认节点名分别为 fd_api_control 与 fd_api_state),需分别初始化。二、类型与结构体
2.1 健康等级 HealthLevel
| 枚举 | 值 | 说明 |
|---|---|---|
| kUnknown | 0 | 未知 |
| kOk | 1 | 正常 |
| kWarn | 2 | 警告 |
| kError | 3 | 错误 |
| kStale | 4 | 过时 |
2.2 故障类型 ApiFaultKind
| 枚举 | 值 | 说明 |
|---|---|---|
| kNone | 0 | 无故障 |
| kDegraded | 1 | 降级 |
| kHardwareFault | 2 | 硬件故障 |
| kCommunicationLost | 3 | 通信丢失 |
2.3 外设标识 PeripheralId
用于 health_get_peripheral 指定外设,共 43 个(0~42,kCount=43 为上界)。常见值:
| 枚举 | 值 | 说明 | 枚举 | 值 | 说明 |
|---|---|---|---|---|---|
| kDriverMotor | 0 | 驱动电机 | kMotorLifting | 3 | 升降电机 |
| kBattery | 9 | 电池 | kCamera | 13 | 相机 |
| kOdom | 21 | 里程计 | kImu | 25 | IMU |
| kNavigation | 27 | 导航 | kLaser | 29 | 激光雷达 |
| kArm | 30 | 机械臂 | kCpuDe | 34 | 小脑 CPU 状态 |
| kMemoryDe | 35 | 小脑内存 | kStorageDe | 36 | 小脑存储 |
| kCpuA | 37 | 大脑 CPU 状态 | kMemoryA | 38 | 大脑内存 |
| kStorageA | 39 | 大脑存储 | kPowerBoard | 41 | 电源板状态 |
2.4 外设健康 PeripheralHealth
| 字段 | 类型 | 说明 |
|---|---|---|
| level | HealthLevel | 健康等级 |
| fault | ApiFaultKind | 故障类型 |
| detail | string | 详情文字 |
2.5 系统资源 SystemResources
各数据块均带 has_* 有效性标志(bool),字段如下:
| 字段 | 类型 | 说明 |
|---|---|---|
| has_cpu_de / cpu_de_user / cpu_de_sys / cpu_de_idle | bool / float×3 | 小脑 CPU 用户/系统/空闲占比 |
| has_cpu_a / cpu_a_user / cpu_a_sys / cpu_a_idle | bool / float×3 | 大脑 CPU 用户/系统/空闲占比 |
| has_mem_de / mem_de_total_kib / used / free | bool / int64×3 | 小脑内存 KiB |
| has_mem_a / mem_a_total_kib / used / free | bool / int64×3 | 大脑内存 KiB |
| has_storage_de / storage_de_home_* / storage_de_etc_* | bool / int64×n | 小脑 home/etc 存储,单位 M |
| has_storage_a / storage_a_home_* / storage_a_etc_* | bool / int64×n | 大脑 home/etc 存储,单位 M |
2.6 健康快照 RobotHealthSnapshot
| 字段 | 类型 | 说明 |
|---|---|---|
| peripherals | list[(PeripheralId, PeripheralHealth)] | 外设健康列表 |
| resources | SystemResources | 系统资源 |
| has_resources | bool | 系统资源数据是否有效 |
2.7 机械臂专属结构体(仅 C++ 导出)
| 结构体 | 字段 | 说明 |
|---|---|---|
| ArmEndPose | position[3] / orientation[4] | 末端位置 x,y,z(m) + 姿态四元数 x,y,z,w |
| ArmJointSample | position_rad / source_stamp_ns | 关节角 rad(长度=DOF)+ 源时间戳(纳秒) |
三、接口总览
State 模块共 19 个接口,按用途分四类。所有订阅接口均返回 SubscriptionId(uint64_t),0 表示失败。
| 分类 | 接口 | C++ 签名 |
|---|---|---|
| 位置/状态读取 | lift_get_current_pos | bool lift_get_current_pos(float &pos_mm) const |
| head_get_current_pos | bool head_get_current_pos(HeadType, float &angle_deg) const | |
| waist_get_current_pos | bool waist_get_current_pos(float &angle_deg) const | |
| hand_get_current_pos | bool hand_get_current_pos(HandType, std::vector<float> &value) const | |
| arm_get_joint_angles | bool arm_get_joint_angles(ArmSide, std::vector<float> &rad_out) const | |
| arm_get_end_pose | bool arm_get_end_pose(ArmSide, ArmEndPose &out) const | |
| 变化订阅 | lift_sub_current_pos | SubscriptionId lift_sub_current_pos(LiftPosCallback) |
| head_sub_current_pos | SubscriptionId head_sub_current_pos(HeadPosCallback) | |
| waist_sub_current_pos | SubscriptionId waist_sub_current_pos(WaistDegCallback) | |
| hand_sub_current_pos | SubscriptionId hand_sub_current_pos(HandType, HandPosCallback) | |
| arm_sub_joint_angles | SubscriptionId arm_sub_joint_angles(ArmSide, ArmJointRadCallback) | |
| arm_sub_joint_samples | SubscriptionId arm_sub_joint_samples(ArmSide, ArmJointSampleCallback) | |
| arm_sub_end_pose | SubscriptionId arm_sub_end_pose(ArmSide, ArmEndPoseCallback) | |
| runtime_sub_idle_state | SubscriptionId runtime_sub_idle_state(IdleStateCallback) | |
| health_sub_snapshot | SubscriptionId health_sub_snapshot(HealthSnapshotCallback) | |
| 空闲与健康 | runtime_get_idle_state | bool runtime_get_idle_state(bool &idle_state) const |
| health_get_peripheral | bool health_get_peripheral(PeripheralId, PeripheralHealth &out) const | |
| health_get_system_resources | bool health_get_system_resources(SystemResources &out) const | |
| 订阅管理 | unsubscribe | bool unsubscribe(SubscriptionId id) |
机械臂状态接口(
arm_get_* / arm_sub_*)全部仅 C++ 导出,Python State 模块未提供——这是本说明采用纯 C++ 版的原因。四、位置/状态读取(同步读)
同步读取「最近一次」缓存值,返回 bool 表示是否有有效数据,结果通过出参写回;返回 false 时出参不可信。
4.1 lift_get_current_pos — 升降高度
bool lift_get_current_pos(float &pos_mm) const; // 出参 pos_mm:位置 mm
4.2 head_get_current_pos — 头部角度
bool head_get_current_pos(HeadType type, float &angle_deg) const;
// type:kLower(下头/yaw)/ kUpper(上头/pitch);出参 angle_deg:角度 deg
4.3 waist_get_current_pos — 腰部角度
bool waist_get_current_pos(float &angle_deg) const; // 约 0°~35°
4.4 hand_get_current_pos — 手部位置/开度
bool hand_get_current_pos(HandType type, std::vector<float> &value) const;
// type:kLeft / kRight(一般不用 kBoth);出参 value:关节或开度数组
4.5 arm_get_joint_angles — 机械臂关节角(仅 C++)
bool arm_get_joint_angles(ArmSide side, std::vector<float> &rad_out) const;
// 关节角 rad,长度 = 该侧 DOF
4.6 arm_get_end_pose — 机械臂末端位姿(仅 C++)
bool arm_get_end_pose(ArmSide side, ArmEndPose &out) const;
// 出参:末端位置 + 四元数姿态
五、变化订阅(异步订阅)
订阅外设状态变化,数据更新时在回调线程中触发回调。所有订阅均返回 SubscriptionId,0 表示失败,用 unsubscribe(id) 取消。
5.1 外设位置订阅(升降/头部/腰部/手部)
SubscriptionId lift_sub_current_pos(LiftPosCallback cb); // cb: void(float pos_mm)
SubscriptionId head_sub_current_pos(HeadPosCallback cb); // cb: void(HeadType, float deg)
SubscriptionId waist_sub_current_pos(WaistDegCallback cb); // cb: void(float deg)
SubscriptionId hand_sub_current_pos(HandType type, HandPosCallback cb);
// cb: void(HandType, const std::vector<float>&)
回调参数差异:升降/腰部回调只带数值;头部回调额外带
HeadType 轴标识;手部回调带 HandType 手型标识,且订阅时需先指定 type 订阅哪只手。5.2 机械臂状态订阅(仅 C++)
SubscriptionId arm_sub_joint_angles(ArmSide side, ArmJointRadCallback cb);
// cb: void(const std::vector<float>& rad)
SubscriptionId arm_sub_joint_samples(ArmSide side, ArmJointSampleCallback cb);
// cb: void(const ArmJointSample&) —— 含纳秒源时间戳
SubscriptionId arm_sub_end_pose(ArmSide side, ArmEndPoseCallback cb);
// cb: void(const ArmEndPose&)
arm_sub_joint_angles vs arm_sub_joint_samples:前者回调只给关节角 rad;后者额外带 source_stamp_ns 源时间戳(纳秒),用于需要时序同步的场景。5.3 空闲与健康订阅
SubscriptionId runtime_sub_idle_state(IdleStateCallback cb); // cb: void(bool idle)
SubscriptionId health_sub_snapshot(HealthSnapshotCallback cb); // cb: void(const RobotHealthSnapshot&)
六、空闲与健康查询
6.1 runtime_get_idle_state — 空闲状态
bool runtime_get_idle_state(bool &idle_state) const; // idle_state:true 表示空闲
6.2 health_get_peripheral — 单外设健康
bool health_get_peripheral(PeripheralId id, PeripheralHealth &out) const;
6.3 health_get_system_resources — 系统资源
bool health_get_system_resources(SystemResources &out) const; // CPU/内存/存储
6.4 unsubscribe — 取消订阅
bool unsubscribe(SubscriptionId id); // true 已取消,false 表示 id 无效
七、使用示例
#include "fd_api/state.h"
#include "fd_api/control.h"
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
fd_robot::State st;
if (!st.Init()) { // C++ Init 可能返回 false
return 1;
}
// —— 1. 同步读取 ——
float lift_mm = 0.0f;
if (st.lift_get_current_pos(lift_mm)) {
// 使用 lift_mm
}
float head_deg = 0.0f;
st.head_get_current_pos(fd_robot::HeadType::kLower, head_deg);
// —— 2. 变化订阅 ——
auto sub_lift = st.lift_sub_current_pos([](float mm) {
// 升降位置更新
});
auto sub_head = st.head_sub_current_pos([](fd_robot::HeadType t, float deg) {
// 头部角度更新,t 区分下头/上头
});
// 机械臂关节角订阅(仅 C++)
auto sub_arm = st.arm_sub_joint_angles(fd_robot::ArmSide::kLeft,
[](const std::vector<float>& rad) { /* 左臂关节角 */ });
// —— 3. 空闲与健康 ——
bool idle = false;
st.runtime_get_idle_state(idle);
fd_robot::PeripheralHealth ph;
if (st.health_get_peripheral(fd_robot::PeripheralId::kBattery, ph)) {
// 检查 ph.level / ph.fault / ph.detail
}
fd_robot::SystemResources res;
st.health_get_system_resources(res);
// 健康快照订阅
auto sub_health = st.health_sub_snapshot(
[](const fd_robot::RobotHealthSnapshot& snap) { /* ... */ });
// —— 4. 取消订阅 ——
st.unsubscribe(sub_lift);
st.unsubscribe(sub_arm);
rclcpp::shutdown();
return 0;
}
八、注意事项
同步读须检查 bool 返回值:
get_* 系列返回 bool 表示是否有有效缓存,false 时出参不可信(可能刚 Init 尚未收到数据)。务必先判断再使用出参。回调线程安全:订阅回调在独立回调线程中执行,线程数由
State::Options::callback_thread_count 配置(默认 2)。请确保回调内无竞态条件,跨线程共享数据需加锁。订阅 ID 与取消:所有
sub_* 返回 SubscriptionId(uint64_t),0 表示失败;用 unsubscribe(id) 取消,false 表示 id 无效。机械臂状态仅 C++:
arm_get_joint_angles / arm_sub_joint_angles / arm_sub_joint_samples / arm_get_end_pose / arm_sub_end_pose 均未在 Python State 导出。State 与 Control 独立初始化:两者各自
Init()、默认节点名不同(fd_api_state vs fd_api_control),多实例时需保证 node_name 不冲突。init 返回语义:C++
State::Init() 可能返回 false(Python 恒 True);C++ 由析构释放资源,无需手动 close。