03. 无线 VerseGrip 的输出
这与教程 02的模式相同,但适用于 Wireless VerseGrip。将按钮(A/B/C)和电量状态添加到流媒体传输中。
本教程将教你:
- 读取无线设备的固有状态字段:
buttons.{a,b,c}、battery_level、hall - 作为独立观察者的保持活动机制
probe_orientation使用 - 与教程 02一样,仅通过第一条消息进行握手的一种模式
工作流
ws://localhost:10001打开与该服务器的 WebSocket 连接,并等待第一个状态帧。wireless_verse_grip从列表中,第一个 Wireless 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 这是其中的内在(intrinsic)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 请启用该功能。
发送 / 接收
wireless_verse_grip 除了使用设备数组这一点之外,教程 02 与……格式相同。WebSocket 循环会接收状态帧,并进行握手和 probe_orientation 将回复保持活动(Keep-Alive)消息。首条发送的消息包含会话配置文件,后续帧仅包含保持活动消息。
- 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 (而不是)明确地命名了。这是因为,wvg_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— 每次发送一个 tick 时session.force_render_full_state将其重新注入,使每个帧不仅包含状态的差异,而且config确保包含该块。
--query-config 在Wireless 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(Wired VG) · 型(四元数) · 控制命令(probe_orientation) · WebSocket 协议