Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

温度读取

概述

通过 hand.temperature 读取 L6 灵巧手 6 个关节电机的温度数据(单位:°C)。

温度属性

属性说明
thumb_flex拇指屈曲关节电机
thumb_abd拇指侧摆关节电机
index食指关节电机
middle中指关节电机
ring无名指关节电机
pinky小指关节电机

读取温度

阻塞读取

data = hand.temperature.get_blocking(timeout_ms=500)
print(f"拇指温度:{data.temperatures.thumb_flex}°C")

参数

  • timeout_ms: 超时时间(毫秒),默认 100

返回值

  • TemperatureData: 包含 temperaturestimestamp

异常

  • TimeoutError: 超时未响应

缓存读取

非阻塞读取最近一次缓存的温度数据。

data = hand.temperature.get_snapshot()
if data:
    print(f"温度:{data.temperatures.to_list()}")

返回值

  • TemperatureDataNone(无缓存数据时)

流式读取

通过顶层 hand.stream() 统一接收所有传感器事件:

from linkerbot.hand.l6 import SensorSource, TemperatureEvent

hand.start_polling({SensorSource.TEMPERATURE: 0.1})

for event in hand.stream():
    match event:
        case TemperatureEvent(data=data):
            print(f"温度:{data.temperatures.to_list()}")

hand.stop_polling()
hand.stop_stream()

示例

读取所有关节电机温度

from linkerbot import L6

with L6(side="left", interface_name="can0") as hand:
    data = hand.temperature.get_blocking(timeout_ms=500)

    # 按属性访问
    print(f"拇指屈曲:{data.temperatures.thumb_flex}°C")
    print(f"食指:{data.temperatures.index}°C")

    # 转换为列表
    temps = data.temperatures.to_list()
    print(f"全部温度:{temps}")

    # 索引访问
    print(f"第一个关节电机:{data.temperatures[0]}°C")

温度监控

from linkerbot import L6
from linkerbot.hand.l6 import SensorSource, TemperatureEvent

with L6(side="left", interface_name="can0") as hand:
    hand.start_polling({SensorSource.TEMPERATURE: 0.1})

    try:
        for event in hand.stream():
            match event:
                case TemperatureEvent(data=data):
                    for i, temp in enumerate(data.temperatures.to_list()):
                        if temp > 60.0:
                            print(f"警告:关节电机 {i} 过热 ({temp}°C)")
    except KeyboardInterrupt:
        pass
    finally:
        hand.stop_polling()
        hand.stop_stream()