跳至主要内容
版本:3.5.x

03. 打印无线VerseGrip

教程 02的模式相同,但适用于无线 VerseGrip —— 在实时状态中增加了按钮(A/B/C)和电量显示。

您将学到:

  • 读取无线特有的状态字段: buttons.{a,b,c}, battery_level, hall
  • 使用 probe_orientation 作为独立观察者保持活动
  • 教程02相同的“仅首条消息”握手模式

工作流程

  1. 打开一个 WebSocket 连接至 ws://localhost:10001 并等待第一个状态帧。
  2. 抢先入手首款 Wireless VerseGrip device_id 来自 wireless_verse_grip 数组。
  3. 使用 会话配置文件 以及按设备计费 probe_orientation keepalive。
  4. 发送请求,然后移除 session 字段——这是一种一次性握手。
  5. 在随后的每一帧中,将四元数转换为欧拉角,并输出经过限流处理的遥测数据(包括按钮状态和电量)。在每个时间步长中重新发送保持连接信号。

参数

名称默认目的
URIws://localhost:10001模拟通道 WebSocket URL
PRINT_EVERY_MS100控制台输出节流阀
会话配置文件名称co.haply.inverse.tutorials:print-wireless-verse-grip在Haply 中标识此模拟
欧拉约定

该转换是在应用坐标系中进行的内变换 Z-X-Y(偏航 → 俯仰 → 滚转) +X right, +Y forward, +Z up. 不要 使用 glm::eulerAngles — 它遵循不同的约定,因此在此处显示不正确。这三种语言变体都实现了相同的数学运算;请参阅源代码以查看公式。

probe_orientation 实际上是必要的

probe_orientation 仅在您的会话 向Inverse3 发送任意指令。一旦您向Inverse3 发出指令Inverse3 力、位置、扭矩等),该服务便会在每个状态帧中自动流式传输已配对的 VerseGrip 的姿态——无需探针。使用 probe_orientation 仅适用于本教程中介绍的此类独立式握力监测工具。

读取状态字段

来自 data.wireless_verse_grip[0].state:

  • orientationquaternion (w, x, y, z)
  • hall — 整数形式的霍尔传感器读数
  • buttons.a, buttons.b, buttons.c — 布尔值
  • battery_level — 浮点数 (0.0 – 1.0)

发送/接收

形状与……相同 教程 02,只是用 wireless_verse_grip 设备数组。WebSocket 循环接收一个状态帧,并发送回握手信息 + probe_orientation 保持活动;第一个发送的帧携带会话配置,此后每个帧仅携带保持活动信息。

单个异步循环。

async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)

if first_message:
first_message = False
device_id = data["wireless_verse_grip"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-wireless-verse-grip"}}},
"wireless_verse_grip": [{
"device_id": device_id,
"commands": {"probe_orientation": {}} # empty — keepalive
}]
}

await websocket.send(json.dumps(request_msg))
request_msg.pop("session", None) # one-shot handshake

来源: Python·C++·C++ Glaze

相关: 教程 02(有线版) · 类型(四元数) · 控制命令 (probe_orientation) · WebSocket 协议