03. 显示 Wireless VerseGrip 的数据
原理与教程02相同,但适用于无线VerseGrip——在实时流媒体状态下添加按钮(A/B/C)和电量显示。
您将学到:
- 读取无线版本特有的状态字段:
buttons.{a,b,c},battery_level,hall - 使用
probe_orientation作为自主观察器的保持活动消息 - 与教程02中一样,握手(handshake)机制仅限于第一条消息
流程
- 建立与……的 WebSocket 连接
ws://localhost:10001并等待第一个状态帧。 - 取回该
device_id从图表中选取首款 Wireless VerseGripwireless_verse_grip. - 使用 会话配置文件 并设置一个保持活动连接
probe_orientation按设备计算。 - 发送请求,然后删除该字段
session— 这只是一次性的握手。 - 在每个后续帧中,将四元数转换为欧拉角,并显示限频遥测数据,包括按钮状态和电池电量。在每次迭代中返回保持活动信号。
参数
| Nom | 默认值 | 角色 |
|---|---|---|
URI | ws://localhost:10001 | WebSocket 模拟通道的 URL |
PRINT_EVERY_MS | 100 | 控制控制台的刷新频率 |
| 会话配置文件名称 | co.haply.inverse.tutorials:print-wireless-verse-grip | 在Haply Hub中识别此模拟 |
在映射坐标系中,该转换是内在的 Z-X-Y 转换(横滚 → 俯仰 → 滚转) +X right, +Y forward, +Z up. 请勿 utiliser 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; 第一条出站消息包含会话配置文件,而随后的每帧数据仅包含保持活动消息。
- Python
- C++ (nlohmann)
- C++ (Glaze)
仅有一个异步循环。
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
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["wireless_verse_grip"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-wireless-verse-grip"}}}}}}},
{"wireless_verse_grip", json::array({
{{"device_id", device_id},
{"commands", {{"probe_orientation", json::object()}}}},
})},
};
}
ws.send(request_msg.dump());
request_msg.erase("session"); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
类型化结构。请注意, button_state 被明确指定(而非 buttons) 以避免遮挡该字段 buttons de wvg_state — Glaze 根据字段名称将字段进行关联,因此结构类型的名称并不重要。
// Struct models
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct button_state { bool a{}, b{}, c{}; };
struct wvg_state {
quat orientation{};
uint8_t hall{};
button_state buttons{};
float battery_level{};
};
struct wvg_device { std::string device_id; wvg_state state; };
struct devices_message { std::vector<wvg_device> wireless_verse_grip; };
struct probe_orientation_cmd {}; // empty object on the wire
struct commands_message {
std::optional<session_cmd> session; // one-shot — omitted when unset
std::vector<device_commands> wireless_verse_grip;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (first_message) {
first_message = false;
request_msg.session = session_cmd{ /* profile = print-wireless-verse-grip */ };
device_commands dc{ .device_id = data.wireless_verse_grip[0].device_id };
dc.commands.probe_orientation = probe_orientation_cmd{};
request_msg.wireless_verse_grip.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(request_msg, out_json);
ws.send(out_json);
request_msg.session.reset(); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
命令行选项(Python)
Python 版本支持与教程 01相同的两个选项:
--full— 显示格式化的 JSON 总有效载荷,而不是单行摘要。--query-config— 重新注入session.force_render_full_state在每个输出时钟周期内,确保每个帧都包含该块config而不是仅使用状态差分。
--query-config 对于无线VerseGrip而言,这尤其有用,因为该模块 config 包含该字段 sub_type 属于《水资源法》(WVG)的(stylus / prototype / ruko / kingfisher(在 3.5 版中新增),此外还有常规的配置字段。执行 python 03-haply-inverse-print-wireless-verse-grip.py --full --query-config 可以查看 config.sub_type 与其他配置值同步实时更新。
C++ 的变体不提供这些选项。
教程 03 也随 SDK 一起本地安装了——请参阅 tutorials/03-haply-inverse-print-wireless-verse-grip/ 在服务安装目录中。
相关链接: 教程 02(有线 VG) · 类型(四元数) · 控制命令(probe_orientation) · WebSocket 协议