机械臂

一、概述

机械臂是 fd_api 中接口数量最多、语义最复杂的一个外设模块。相比升降/头部/腰部这类单自由度或双轴机构,机械臂具备多关节(默认 7 DOF)、左右双臂、上/下电管理,以及关节空间与笛卡尔空间两套运动方式。

本模块由两大组件协同完成:fd_robot::Control 负责下发运动命令(14 个接口),fd_robot::State 负责读取/订阅关节与末端状态(5 个接口)。

语言差异(重要):机械臂的状态读/订阅接口(arm_get_* / arm_sub_*)仅 C++ 提供,Python 暂未导出;控制侧 arm_set_six_axis_force_enable 也仅 C++。因此本说明为纯 C++ 版,完整覆盖机械臂全部能力。

核心特性

双臂 / 单臂
arm_mode 区分 dual / single
多关节
DOF 由机型决定,缺省 7
关节 + 笛卡尔
两套运动空间并存
上/下电管理
单臂/双臂电源控制
六维力采集
启停力传感器发布
关节/末端订阅
角度 + 位姿双通道
含时间戳采样
ArmJointSample 带纳秒源戳
预置位置运动
按位置名直达
上层应用
#include "fd_api/control.h" / "fd_api/state.h"
▼
fd_robot::Control / fd_robot::State
关节运动 · 笛卡尔运动 · 电源 · 状态订阅
▼
ROS 2 通信层
/tl_driver/joint_trajectory · /tl_driver/cmd · /tl_driver/current_joint_states
▼
机械臂硬件
左臂 · 右臂 · 六维力传感器 · arm_api_base HTTP 服务

二、机构参数与标识

2.1 枚举类型

类型 枚举 值 说明
ArmSide kLeft 0 左臂
ArmSide kRight 1 右臂
PeripheralId kArm 30 机械臂(健康监控用外设 ID)
PeripheralId kForceSensor 18 力传感器(六维力采集关联外设)

2.2 状态数据结构(仅 C++ 导出)

结构体 字段 类型 说明
ArmEndPose position[3] float × 3 末端位置 x、y、z,单位 m
orientation[4] float × 4 姿态四元数 x、y、z、w
ArmJointSample position_rad float[] 关节角 rad,长度 = 该侧 DOF
source_stamp_ns int64 源时间戳(纳秒),来自关节状态消息
说明:ArmEndPose 与 ArmJointSample 两个类型均未从 Python fd_api 导出,仅供 C++ 使用。

2.3 关键配置项(Control::Options)

字段 类型 默认 说明
arm_mode string dual dual 双臂 / single 单臂
arm_dof_left int 0 左/单臂 DOF;0=从 robot_profile 解析
arm_dof_right int 0 右臂 DOF;0=从 robot_profile 解析
arm_api_base string http://127.0.0.1:8000 笛卡尔运动 HTTP 服务根地址
arm_trajectory_topic string /tl_driver/joint_trajectory 机械臂关节轨迹话题
arm_cmd_topic string /tl_driver/cmd 机械臂电源/命令话题
arm_joint_states_topic string /tl_driver/current_joint_states 机械臂关节状态话题
arm_speed_left_topic string /tl_driver/armleft/set_speed 左臂速度百分比话题
arm_speed_right_topic string /tl_driver/armright/set_speed 右臂速度百分比话题
arm_speed_single_topic string /tl_driver/set_speed 单臂速度百分比话题

2.4 自由度(DOF)

关节数量 N 由机型 robot_profile 决定,通过 dof1 / dof2 字段解析;缺省为 7,机型 hh_y2_bdt 为 6(hh_w2_bdt 仅为该机型 alias)。当 arm_dof_left / arm_dof_right 显式设为非 0 值时,以配置值覆盖解析结果。

长度必须匹配:所有关节入参(joints_rad / joints_deg / traj 的每个点)长度必须等于该侧 DOF,否则命令可能失败。

三、接口总览

机械臂共 14 个控制接口 + 5 个状态接口,均为 C++ 提供。控制接口按职责可分六类:电源管理、速度设置、关节空间运动、单关节操作、笛卡尔空间运动、六维力采集。

分类 接口 C++ 签名 返回值
电源管理 arm_power_on bool arm_power_on(ArmSide side) 命令已下发
arm_power_on_both bool arm_power_on_both() 命令已下发
arm_power_off_both bool arm_power_off_both() 命令已下发
arm_stop bool arm_stop(ArmSide side) 命令已下发
速度设置 arm_set_speed bool arm_set_speed(ArmSide side, std::int32_t speed_pct) 命令已下发
关节空间运动 arm_move_joints bool arm_move_joints(ArmSide side, const std::vector<float>& joints_rad) 命令已下发
arm_move_joints_deg bool arm_move_joints_deg(ArmSide side, const std::vector<float>& joints_deg) 命令已下发
arm_move_joints_traj bool arm_move_joints_traj(ArmSide side, const std::vector<std::vector<float>>& traj) 命令已下发
单关节操作 arm_joint_jog bool arm_joint_jog(ArmSide side, int joint_1_to_n, int direction, std::int32_t speed_pct) 命令已下发
arm_joint_step_deg bool arm_joint_step_deg(ArmSide side, int joint_1_to_n, int direction, float step_deg) 命令已下发
笛卡尔空间运动 arm_move_axis_delta bool arm_move_axis_delta(ArmSide side, char axis, float distance_m) 请求成功
arm_move_to_target bool arm_move_to_target(ArmSide side, float x, float y, float z) 请求成功
arm_move_to_location bool arm_move_to_location(ArmSide side, const std::string& location_name) 请求成功
六维力采集 arm_set_six_axis_force_enable bool arm_set_six_axis_force_enable(bool enable) 命令已下发

状态接口(5 个,仅 C++)

接口 C++ 签名 返回值
arm_get_joint_angles bool arm_get_joint_angles(ArmSide side, std::vector<float>& rad_out) const 是否有有效数据
arm_sub_joint_angles SubscriptionId arm_sub_joint_angles(ArmSide side, ArmJointRadCallback cb) 订阅 ID;0 失败
arm_sub_joint_samples SubscriptionId arm_sub_joint_samples(ArmSide side, ArmJointSampleCallback cb) 订阅 ID;0 失败
arm_get_end_pose bool arm_get_end_pose(ArmSide side, ArmEndPose& out) const 是否有有效数据
arm_sub_end_pose SubscriptionId arm_sub_end_pose(ArmSide side, ArmEndPoseCallback cb) 订阅 ID;0 失败

四、控制接口

命名空间 fd_robot::Control。所有控制接口均返回 bool:关节/电源/速度类返回 true 表示命令已下发,笛卡尔类返回 true 表示请求成功。

4.1 电源管理

bool arm_power_on(ArmSide side);

发送单臂上电命令。single 模式下忽略侧别。

参数 类型 说明
side ArmSide 左/右臂
bool arm_power_on_both();

双臂上电:dual 模式下同时上电双臂;single 模式下上电单臂。

bool arm_power_off_both();

双臂下电:dual 模式下同时下电双臂;single 模式下下电单臂。

bool arm_stop(ArmSide side);

对指定侧下电/停止。

参数 类型 说明
side ArmSide 左/右臂
语义区分:arm_power_off_both() 同时作用于双臂;arm_stop(side) 可单独停止某一侧。二者均对应下电/停止。

4.2 速度设置

bool arm_set_speed(ArmSide side, std::int32_t speed_pct);

设置指定侧机械臂运动速度百分比。

参数 类型 默认 说明
side ArmSide - kLeft / kRight;single 模式可能忽略侧别
speed_pct int32 - 速度百分比,约 1~100

4.3 关节空间运动

bool arm_move_joints(ArmSide side, const std::vector<float>& joints_rad);

将指定侧关节运动到目标角(弧度)。入参长度须等于该侧 DOF。

参数 类型 说明
side ArmSide 左/右臂
joints_rad float[N] 关节角 rad,长度 = 该侧 DOF
bool arm_move_joints_deg(ArmSide side, const std::vector<float>& joints_deg);

同 arm_move_joints,入参为角度(度)。

参数 类型 说明
side ArmSide 左/右臂
joints_deg float[N] 关节角 deg,长度 = 该侧 DOF
bool arm_move_joints_traj(ArmSide side, const std::vector<std::vector<float>>& traj);

按多点关节轨迹运动。

参数 类型 说明
side ArmSide 左/右臂
traj float[][] 轨迹点序列,每点长度 = DOF

4.4 单关节操作

bool arm_joint_jog(ArmSide side, int joint_1_to_n, int direction, std::int32_t speed_pct);

对指定关节按方向点动(连续运动)。

参数 类型 说明
side ArmSide 左/右臂
joint_1_to_n int 关节序号 1~N(N = 该侧 DOF)
direction int 点动方向(实现约定正负)
speed_pct int32 速度百分比
bool arm_joint_step_deg(ArmSide side, int joint_1_to_n, int direction, float step_deg);

对指定关节按步长增减角度(单次步进)。

参数 类型 说明
side ArmSide 左/右臂
joint_1_to_n int 关节序号 1~N
direction int -1 减,+1 加
step_deg float 步长 deg,约 0.5~5.0

4.5 笛卡尔空间运动

依赖前提:以下三个接口均依赖 arm_api_base 指向的 fd_arm_http 服务(默认 http://127.0.0.1:8000),服务未启动时调用将失败。
bool arm_move_axis_delta(ArmSide side, char axis, float distance_m);

沿轴平移(笛卡尔相对运动)。

参数 类型 说明
side ArmSide 左/右臂
axis char 轴,如 'x' / 'y' / 'z'
distance_m float 平移距离 m
bool arm_move_to_target(ArmSide side, float x, float y, float z);

运动到笛卡尔坐标点。

参数 类型 说明
side ArmSide 左/右臂
x / y / z float 目标位置 m
bool arm_move_to_location(ArmSide side, const std::string& location_name);

运动到已配置的预置位置名称。

参数 类型 说明
side ArmSide 左/右臂
location_name string 预置位置名

4.6 六维力采集

bool arm_set_six_axis_force_enable(bool enable);

向 /tl_driver/cmd 发送 six_axis_force_on / six_axis_force_off,开启或关闭六维力传感器发布。

参数 类型 说明
enable bool true 开启,false 关闭
仅 C++:本接口 Python Control 暂未封装。

五、状态接口

命名空间 fd_robot::State。机械臂的全部状态接口仅 C++ 提供。同步读接口返回 bool 表示「是否有有效数据」,订阅接口返回 SubscriptionId(uint64_t),0 表示失败,用 unsubscribe(id) 取消。

5.1 关节角读取 / 订阅

bool arm_get_joint_angles(ArmSide side, std::vector<float>& rad_out) const;

同步读取关节角 rad,长度等于该侧 DOF。

参数 类型 说明
side ArmSide 左/右臂
rad_out float[]& 关节角 rad(出参)
SubscriptionId arm_sub_joint_angles(ArmSide side, ArmJointRadCallback cb);
// cb: void(const std::vector<float>& rad)

订阅关节角,单位 rad。

参数 类型 说明
side ArmSide 左/右臂
cb ArmJointRadCallback 关节角回调,单位 rad
SubscriptionId arm_sub_joint_samples(ArmSide side, ArmJointSampleCallback cb);
// cb: void(const ArmJointSample&)

关节状态更新时回调 ArmJointSample(关节角 rad + 源时间戳纳秒)。

参数 类型 说明
side ArmSide 左/右臂
cb ArmJointSampleCallback 关节采样回调

5.2 末端位姿读取 / 订阅

bool arm_get_end_pose(ArmSide side, ArmEndPose& out) const;

同步读取末端位置与四元数姿态。

参数 类型 说明
side ArmSide 左/右臂
out ArmEndPose& 末端位姿(出参)
SubscriptionId arm_sub_end_pose(ArmSide side, ArmEndPoseCallback cb);
// cb: void(const ArmEndPose&)

订阅末端位姿。

参数 类型 说明
side ArmSide 左/右臂
cb ArmEndPoseCallback 末端位姿回调

六、运动空间与模式对比

6.1 关节空间 vs 笛卡尔空间

维度 关节空间 笛卡尔空间
接口 arm_move_joints(_deg) / arm_move_joints_traj / arm_joint_jog / arm_joint_step_deg arm_move_axis_delta / arm_move_to_target / arm_move_to_location
目标描述 各关节角度(rad/deg) 末端位置(x,y,z)/ 轴平移 / 预置位置名
底层通道 ROS 2 话题 /tl_driver/joint_trajectory HTTP 服务 arm_api_base
依赖前提 机械臂驱动节点已启动 额外依赖 fd_arm_http 服务
典型用途 关节级精确控制、步进调试 末端点位直连、预置动作

6.2 点动 vs 步进(单关节)

维度 arm_joint_jog arm_joint_step_deg
运动方式 连续点动(持续运动) 单次步进(固定步长)
步长参数 无(以 speed_pct 控制速度) step_deg(约 0.5~5.0)
方向参数 direction(实现约定正负) direction(-1 减 / +1 加)
适用场景 需要持续运动、速度可调 精确微调、定量步进

6.3 dual / single 模式

维度 dual(双臂) single(单臂)
侧别语义 kLeft / kRight 严格区分 多数接口忽略侧别,作用于单臂
上/下电 arm_power_on_both 同时上电双臂 arm_power_on_both 仅上电单臂
关节运动前提 通常需先收到关节状态 相对宽松

七、使用示例

完整 C++ 示例:初始化 → 上电 → 关节运动 → 单关节步进 → 笛卡尔运动 → 状态订阅 → 停止。

#include "fd_api/control.h"
#include "fd_api/state.h"
#include <iostream>
#include <vector>

int main() {
    // 1. 初始化(机械臂控制)
    fd_robot::Control ctrl;
    if (!ctrl.Init()) {
        std::cerr << "Control Init failed" << std::endl;
        return 1;
    }
    fd_robot::State state;
    if (!state.Init()) {
        std::cerr << "State Init failed" << std::endl;
        return 1;
    }

    // 2. 上电 + 设置速度(左臂)
    ctrl.arm_power_on(fd_robot::ArmSide::kLeft);
    ctrl.arm_set_speed(fd_robot::ArmSide::kLeft, 30);

    // 3. 关节空间运动(弧度,长度须 = 该侧 DOF,缺省 7)
    std::vector<float> joints = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
    ctrl.arm_move_joints(fd_robot::ArmSide::kLeft, joints);

    // 4. 单关节步进(关节 3,正方向,步长 2.0°)
    ctrl.arm_joint_step_deg(fd_robot::ArmSide::kLeft, 3, 1, 2.0f);

    // 5. 笛卡尔空间运动(依赖 arm_api_base HTTP 服务)
    ctrl.arm_move_to_target(fd_robot::ArmSide::kLeft, 0.4f, 0.1f, 0.3f);

    // 6. 订阅关节角
    auto sid = state.arm_sub_joint_angles(
        fd_robot::ArmSide::kLeft,
        [](const std::vector<float>& rad) {
            // 处理关节角(rad)
        });

    // 7. 同步读末端位姿
    fd_robot::ArmEndPose pose;
    if (state.arm_get_end_pose(fd_robot::ArmSide::kLeft, pose)) {
        std::cout << "x=" << pose.position[0] << " y=" << pose.position[1]
                  << " z=" << pose.position[2] << std::endl;
    }

    // 8. 取消订阅 + 下电
    state.unsubscribe(sid);
    ctrl.arm_stop(fd_robot::ArmSide::kLeft);

    return 0;
}

八、注意事项

入参长度必须匹配 DOF:arm_move_joints / arm_move_joints_deg / arm_move_joints_traj 的每个关节向量长度必须等于该侧 DOF(缺省 7,hh_y2_bdt 为 6),否则命令可能失败。
dual 模式需先有关节状态:双臂模式下,关节运动通常要求已收到关节状态消息,否则运动命令可能无法正确执行。
笛卡尔运动依赖 HTTP 服务:arm_move_axis_delta / arm_move_to_target / arm_move_to_location 依赖 arm_api_base(默认 http://127.0.0.1:8000)指向的 fd_arm_http 服务,服务未启动时调用失败。
仅 C++ 的接口:机械臂状态读/订阅(arm_get_* / arm_sub_*)全部仅 C++;控制侧 arm_set_six_axis_force_enable 也仅 C++。Python 使用方需自行绕过或另建封装。
DOF 解析优先级:arm_dof_left / arm_dof_right 设为 0(默认)时从 robot_profile 的 dof1 / dof2 解析;显式非 0 值则覆盖解析结果。
返回值语义差异:关节/电源/速度类返回 true 仅表示命令已下发,不代表运动已完成;同步读接口返回 true 才表示有有效数据。
六维力采集:arm_set_six_axis_force_enable 只控制传感器是否发布,不改变机械臂运动状态。
末端位姿单位:ArmEndPose 的位置单位为 m,姿态为四元数(x,y,z,w);关节角统一为 rad(除 arm_move_joints_deg / arm_joint_step_deg 明确为 deg)。
fd_api 机械臂说明 — 天链机器人 © 2026