03. 发放无线VerseGrip
与教程 02 采用相同的模式,但适用于无线 VerseGrip —— 在流媒体播放状态下增加了按钮(A/B/C)和电池电量显示。
您将学到:
- 读取无线特有的状态字段:
buttons.{a,b,c},battery_level,hall - 使用
probe_orientationals 作为独立观察者的保持活动机制 - 与教程 02 中的握手模式相同,其中只有第一条消息包含会话配置信息
流程
- 通过 WebSocket 建立连接至
ws://localhost:10001然后等待第一条状态消息。 - 请选择
device_idArray推出的首款无线VerseGripwireless_verse_grip. - 请使用以下方式提交请求: 会议简介 以及通过
probe_orientation. - 提交请求后,请取消勾选该复选框
session— 这是一次独一无二的握手。 - 在处理每个后续帧时,将四元数转换为欧拉角,并输出经过限流处理的遥测数据,包括按键状态和电池电量。每时钟周期重新发送一次保持活动信号。
参数
| 名称 | 标准 | Zweck |
|---|---|---|
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. 请勿使用 glm::eulerAngles — 它遵循另一种约定,因此在此处返回了错误的值。这三种语言版本都实现了相同的数学运算;您可以在源代码中找到该公式。
probe_orientation 实际上确实需要probe_orientation 只有当您的会话 keinen 向Inverse3 发送命令。一旦您向Inverse3 发送指令(力、位置、扭矩等),该服务便会在每个状态帧中自动流式传输已配对的 VerseGrip 的姿态数据——此时无需保持活动连接。请使用 probe_orientation 仅适用于像本教程中这样的独立Grip监控工具。
已读取的状态字段
Aus 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 在 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-除了常规配置字段外,还应屏蔽 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++ 的某些版本不提供这些标志。
此外,教程 03 会随 SDK 一起本地安装——可在以下位置找到: tutorials/03-haply-inverse-print-wireless-verse-grip/ 位于该服务的安装目录中。
相关主题: 教程 02(有线 VG) · 类型(四元数) · 控制命令 (probe_orientation) · WebSocket 协议