跳至主要内容

03. 打印无线 VerseGrip

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

你将学到:

  • 读取无线设备特有的状态字段:buttons.{a,b,c}、battery_level、hall
  • 将 probe_orientation 作为独立观察者的存活机制
  • 与教程 02 相同的“仅首条消息握手”模式

工作流程​

  1. 跳转至 ws://localhost:10001 的 WebSocket,并等待第一个状态帧。
  2. 从 wireless_verse_grip 从数组中获取第一个无线 VerseGrip 的 device_id。
  3. 使用 会话配置文件 构建请求,并为每个设备附加 probe_orientation 保活指令。
  4. 发送请求,然后取消 session 字段——它只是一次性的握手。
  5. 在随后的每一帧中,将四元数转换为欧拉角,并打印经过限流处理的遥测数据,包括按钮状态和电池电量。每个周期重新发送心跳指令。

参数​

名称默认值用途
URIws://localhost:10001WebSocket 仿真通道的 URL
PRINT_EVERY_MS100控制台输出限流
会话配置文件名称co.haply.inverse.tutorials:print-wireless-verse-grip在Haply Hub中标识此仿真
欧拉角约定

转换采用内在 Z-X-Y 顺序(偏航 → 俯仰 → 横滚),应用坐标系为 +X right, +Y forward, +Z up。请勿使用 glm::eulerAngles——它遵循不同的约定,因此会得到错误的结果。这三种语言版本实现的是相同的数学计算;具体公式请参阅源代码。

何时才真正需要 probe_orientation

probe_orientation 仅在你的会话中没有仅在向Inverse3 发送任何指令时才有效。一旦你向Inverse3 下达指令(力、位置、扭矩等),该服务就会在每个状态帧中自动推送配对的 VerseGrip 方向数据——无需探测。仅适用于本教程中此类独立抓取监控工具 probe_orientation。

读取的状态字段​

来源: data.wireless_verse_grip[0].state:

  • orientation — quaternion(w, x, y, z)
  • hall — 整数型霍尔传感器读数
  • buttons.a、buttons.b、buttons.c — 布尔值
  • battery_level — 浮点数(0.0 – 1.0)
  • transform.rotation — 工作空间旋转(quaternion);位置和缩放不适用于仅支持方向的设备,因此从未输出
部分语义更新

当 transform.rotation 等于单位四元数 {w:1,x:0,y:0,z:0} 时,该字段将被省略。读取时请提供默认值。启用 serialization/explicit_fields 可以始终接收该字段。

发送 / 接收​

与教程 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)​

Python版本接受与教程 01 相同的两个参数:

  • --full ——以格式化方式输出原始 JSON 负载,而非单行摘要。
  • --query-config ——在每个出站周期重新注入 session.force_render_full_state,使每一帧都携带 config 区块,而不仅仅是状态增量。

--query-config 这对无线版 VerseGrip 尤其有用,因为 config 除常规配置字段外,该块还包含 WVG 专用的 sub_type 字段(stylus / prototype / ruko / kingfisher(3.5 版本新增)。运行 python 03-haply-inverse-print-wireless-verse-grip.py --full --query-config 可以实时观察 config.sub_type 与其他配置值一起更新。

C++ 版本不提供这些参数。

随 SDK 安装程序一并提供

教程 03 也随 SDK 一起安装在本地——请在服务安装目录下的 tutorials/03-haply-inverse-print-wireless-verse-grip/ 中查找。

源代码: Python·C++·C++ Glaze

相关内容: 教程 02(有线 VG) · 类型(四元数) · 控制指令(probe_orientation) · WebSocket 协议