1.hand_move - 手部运动控制
方法: ctrl.hand_move(hand_type, value)
参数:
hand_type:手部类型,HandType 枚举
HandType.kLeft:左手
HandType.kRight:右手
HandType.kBoth:双手
value:控制值列表,list[float] 类型
- RoHand 后端:传入 6 个值,对应各关节
- Inspire 后端:取 首值 作为开合控制
返回值:
使用示例:
Python
from fd_api import Control, HandType
ctrl = Control()
ctrl.init({"node_name": "fd_api_control"})
# Inspire 灵巧手:控制左手开合
ctrl.hand_move(HandType.kLeft, [500])
# Inspire 灵巧手:控制右手开合
ctrl.hand_move(HandType.kRight, [500])
# Inspire 灵巧手:控制双手开合
ctrl.hand_move(HandType.kBoth, [500])
# RoHand 灵巧手:传入6个关节值
ctrl.hand_move(HandType.kLeft, [0.0, 0.5, 0.3, 0.0, 0.0, 0.0])
2.hand_stop - 手部停止
方法: ctrl.hand_stop(hand_type)
参数:
hand_type:手部类型,HandType 枚举
HandType.kLeft:停止左手
HandType.kRight:停止右手
HandType.kBoth:停止双手
返回值:
使用示例:
Python
from fd_api import Control, HandType
ctrl = Control()
ctrl.init({"node_name": "fd_api_control"})
# 停止左手运动
ctrl.hand_stop(HandType.kLeft)
# 停止右手运动
ctrl.hand_stop(HandType.kRight)
# 停止双手运动
ctrl.hand_stop(HandType.kBoth)
3.hand_get_current_pos - 获取手部当前位置(同步读取)
方法: state.hand_get_current_pos(hand_type)
参数:
hand_type:手部类型,HandType 枚举
HandType.kLeft:读取左手位置
HandType.kRight:读取右手位置
返回值:
list[float]:当前手部各关节位置值列表。初始值为 [0.0, ...],收到订阅数据后更新为缓存值
使用示例:
Python
from fd_api import State, HandType
state = State()
state.init({"node_name": "fd_api_state"})
# 读取左手当前位置
left_pos = state.hand_get_current_pos(HandType.kLeft)
print(f"左手位置:{left_pos}")
# 读取右手当前位置
right_pos = state.hand_get_current_pos(HandType.kRight)
print(f"右手位置:{right_pos}")
4.hand_sub_current_pos - 订阅手部位置(异步回调)
方法: state.hand_sub_current_pos(hand_type, callback)
参数:
hand_type:手部类型,HandType 枚举
HandType.kLeft:订阅左手位置
HandType.kRight:订阅右手位置
callback:回调函数,签名 callback(hand_type, value)
hand_type:HandType 类型,标识左手还是右手
value:list[float] 类型,各关节位置值列表
返回值:
SubscriptionId:订阅ID,类型为 int。可用于后续取消订阅;空回调返回 0
使用示例:
Python
from fd_api import State, HandType
state = State()
state.init({"node_name": "fd_api_state"})
# 定义回调函数
def on_hand_pos(hand_type, value):
name = "左手" if hand_type == HandType.kLeft else "右手"
print(f"{name}位置更新:{value}")
# 订阅左手位置
sub_left = state.hand_sub_current_pos(HandType.kLeft, on_hand_pos)
# 订阅右手位置
sub_right = state.hand_sub_current_pos(HandType.kRight, on_hand_pos)
# 取消左手订阅
state.unsubscribe(sub_left)
# 取消右手订阅
state.unsubscribe(sub_right)
建议:
- 回调在线程池中执行,注意线程安全
- 无需接收位置更新时及时取消订阅,避免不必要的回调
- 可与
hand_get_current_pos 同步读取配合使用