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 读取 L20Lite 灵巧手 10 个关节电机的温度数据(单位:°C)。

温度属性

属性说明
thumb_flex拇指弯曲关节电机
thumb_abd拇指侧摆关节电机
index_flex食指弯曲关节电机
middle_flex中指弯曲关节电机
ring_flex无名指弯曲关节电机
pinky_flex小指弯曲关节电机
index_abd食指侧摆关节电机
ring_abd无名指侧摆关节电机
pinky_abd小指侧摆关节电机
thumb_yaw拇指旋转关节电机

读取温度

阻塞读取

from linkerbot.exceptions import TimeoutError

try:
    data = hand.temperature.get_blocking(timeout_ms=500)
    print(f"拇指弯曲温度:{data.temperatures.thumb_flex}°C")
except TimeoutError:
    print("读取超时")

参数

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

返回值

  • TemperatureData: 包含 temperatures(L20liteTemperature)和 timestamp

异常

  • TimeoutError: 超时未响应

缓存读取

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

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

返回值

  • TemperatureDataNone(无缓存数据时)

流式读取

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

from linkerbot.hand.l20lite import SensorSource, TemperatureEvent

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

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

示例

读取所有关节电机温度

from linkerbot import L20lite

with L20lite(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_flex}°C")

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

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

温度监控

from linkerbot import L20lite
from linkerbot.hand.l20lite import SensorSource, TemperatureEvent

with L20lite(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()