腰部关节

1.waist_front - 腰部前倾

方法: ctrl.waist_front(spd, acc, dec)

C++ 签名: bool waist_front(std::int32_t spd, std::uint32_t acc, std::uint32_t dec);

参数:

  • spd:速度值,int32_t 类型,建议约 300
  • acc:加速度值,uint32_t 类型,建议约 50
  • dec:减速度值,uint32_t 类型,建议约 50

返回值:

  • bool:操作结果
    • true:指令发送成功
    • false:指令发送失败

使用示例:

C++
#include "fd_api/control.h"
#include <cstdio>

int main() {
    fd_robot::Control ctrl;
    fd_robot::Control::Options opts;
    opts.node_name = "fd_api_control";
    if (!ctrl.Init(opts)) {
        return 1;
    }

    // 腰部前倾
    bool ok = ctrl.waist_front(300, 50, 50);
    if (ok) {
        printf("腰部前倾指令发送成功\n");
    } else {
        printf("腰部前倾指令发送失败\n");
    }
    return 0;
}

2.waist_back - 腰部后倾

方法: ctrl.waist_back(spd, acc, dec)

C++ 签名: bool waist_back(std::int32_t spd, std::uint32_t acc, std::uint32_t dec);

参数:

  • spd:速度值,int32_t 类型,建议约 300
  • acc:加速度值,uint32_t 类型,建议约 50
  • dec:减速度值,uint32_t 类型,建议约 50

返回值:

  • bool:操作结果
    • true:指令发送成功
    • false:指令发送失败

使用示例:

C++
#include "fd_api/control.h"
#include <cstdio>

int main() {
    fd_robot::Control ctrl;
    fd_robot::Control::Options opts;
    opts.node_name = "fd_api_control";
    if (!ctrl.Init(opts)) {
        return 1;
    }

    // 腰部后倾
    bool ok = ctrl.waist_back(300, 50, 50);
    if (ok) {
        printf("腰部后倾指令发送成功\n");
    } else {
        printf("腰部后倾指令发送失败\n");
    }
    return 0;
}

3.waist_set_pos - 腰部位置控制

方法: ctrl.waist_set_pos(spd, acc, dec, pos_deg)

C++ 签名: bool waist_set_pos(std::uint32_t spd, std::uint32_t acc, std::uint32_t dec, float pos_deg);

参数:

  • spd:速度值,uint32_t 类型,建议约 300
  • acc:加速度值,uint32_t 类型,建议约 50
  • dec:减速度值,uint32_t 类型,建议约 50
  • pos_deg:目标角度,float 类型,范围 0° ~ 35°

返回值:

  • bool:操作结果
    • true:指令发送成功
    • false:指令发送失败

使用示例:

C++
#include "fd_api/control.h"
#include <cstdio>

int main() {
    fd_robot::Control ctrl;
    fd_robot::Control::Options opts;
    opts.node_name = "fd_api_control";
    if (!ctrl.Init(opts)) {
        return 1;
    }

    // 腰部转到 20° 位置
    bool ok = ctrl.waist_set_pos(300, 50, 50, 20.0f);
    if (ok) {
        printf("腰部位置控制指令发送成功\n");
    } else {
        printf("腰部位置控制指令发送失败\n");
    }
    return 0;
}

4.waist_stop - 腰部停止

方法: ctrl.waist_stop()

C++ 签名: bool waist_stop();

参数:

  • 无

返回值:

  • bool:操作结果
    • true:指令发送成功
    • false:指令发送失败

使用示例:

C++
#include "fd_api/control.h"
#include <cstdio>

int main() {
    fd_robot::Control ctrl;
    fd_robot::Control::Options opts;
    opts.node_name = "fd_api_control";
    if (!ctrl.Init(opts)) {
        return 1;
    }

    // 停止腰部运动
    ctrl.waist_stop();
    printf("腰部已停止\n");
    return 0;
}

5.waist_get_current_pos - 获取腰部当前位置(同步读取)

方法: state.waist_get_current_pos(angle_deg)

C++ 签名: bool waist_get_current_pos(float &angle_deg) const;

参数:

  • angle_deg:出参,float & 类型,返回当前腰部角度,单位:度 (°),范围约 0° ~ 35°

返回值:

  • bool:表示是否已有有效缓存数据
    • true:已有数据,angle_deg 有效
    • false:尚无数据,angle_deg 不可信(对应 Python 版可能返回 0)

使用示例:

C++
#include "fd_api/state.h"
#include <cstdio>

int main() {
    fd_robot::State state;
    if (!state.Init()) {
        return 1;
    }

    // 读取当前腰部角度
    float angle_deg = 0.0f;
    if (state.waist_get_current_pos(angle_deg)) {
        printf("当前腰部角度:%.1f°\n", angle_deg);
    } else {
        printf("尚未收到腰部位置数据\n");
    }
    return 0;
}

6.waist_sub_current_pos - 订阅腰部位置(异步回调)

方法: state.waist_sub_current_pos(cb)

C++ 签名: SubscriptionId waist_sub_current_pos(WaistDegCallback cb);  // cb: void(float deg)

参数:

  • cb:回调函数,std::function<void(float)> 类型,deg 为 float,单位:度 (°)。回调在线程池中执行。

返回值:

  • SubscriptionId:订阅ID,类型为 uint64_t。可用于后续取消订阅;空回调返回 0(失败)

使用示例:

C++
#include "fd_api/state.h"
#include <cstdio>

int main() {
    fd_robot::State state;
    if (!state.Init()) {
        return 1;
    }

    // 订阅腰部位置(回调在线程池中执行)
    fd_robot::SubscriptionId sid = state.waist_sub_current_pos(
        [](float deg) {
            printf("腰部角度更新:%.1f°\n", deg);
        });
    if (sid != 0) {
        printf("订阅成功,订阅ID:%llu\n", sid);
    }

    // 无需接收时取消订阅
    state.unsubscribe(sid);
    printf("已取消腰部位置订阅\n");
    return 0;
}

建议:

  • 订阅的回调在线程池中执行,注意线程安全
  • 无需接收位置更新时及时取消订阅,避免不必要的回调
  • 可与 waist_get_current_pos 同步读取配合使用