03. 无线 VerseGrip 输出
虽然与教程 02采用相同的模式,但这是针对无线 VerseGrip 的——在流媒体播放状态下,增加了按钮(A/B/C)和电池电量显示。
学习内容:
- 读取无线专用状态字段:
buttons.{a,b,c},battery_level,hall - 作为独立观察者的保持活动机制
probe_orientation使用方法 - 与教程 02类似,仅在首次消息中执行的握手模式
工作流程
ws://localhost:10001通过 WebSocket 建立连接,并等待第一个状态帧。wireless_verse_grip在该系列中,首款无线VerseGrip的device_id获取。- 会话配置文件按设备分类
probe_orientation配置包含 keepalive 的请求。 - 发送请求后
session删除该字段——因为这是一次性的握手。 - 此后,在每个帧中将四元数转换为欧拉角,并以限定周期输出包含按钮状态和电池电量在内的遥测数据。每个时钟周期都会重新发送保持活动信号。
参数
| 姓名 | 默认值 | 用途 |
|---|---|---|
URI | ws://localhost:10001 | 仿真通道 WebSocket URL |
PRINT_EVERY_MS | 100 | 控制台输出间隔限制 |
| 会话配置文件名称 | co.haply.inverse.tutorials:print-wireless-verse-grip | Haply 在 Hub 中识别此模拟 |
变换是在应用程序坐标系中进行的 +X right, +Y forward, +Z up遵循内在的 Z-X-Y 顺序(偏航 → 俯仰 → 滚转)。 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 重新发送 keepalive。第一个发送的消息包含会话配置文件,此后所有帧仅包含 keepalive。
- 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
使用指定类型的结构体。顺便提一下, wvg_state的 buttons 不要遮挡球场 button_state明确使用了“”这个名称(buttons (否则)。由于 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是 config 该模块除常规配置字段外,还专用于 WVG sub_type 字段(stylus / prototype / ruko / kingfisher(在 3.5 版中新增)因此对无线 VerseGrip 特别有用。 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 协议