02. 打印 VerseGrip
从第一个有线VerseGrip读取方向(四元数+ Z-X-Y欧拉角)、霍尔传感器电平以及按钮状态。
您将学到:
- 阅读
quaternion相对于州坐标系的定向 - 将四元数转换为以度为单位的Z-X-Y欧拉角(+X向右,+Y向前,+Z向上)
- 使用
probe_orientation作为独立观察者保持活动 - 仅首报文握手模式(与教程 01 相同)
工作流程
- 打开一个 WebSocket 连接至
ws://localhost:10001并等待第一个状态帧。 - 挑选第一款有线版VerseGrip
device_id来自verse_grip数组。 - 使用 会话配置文件 以及按设备计费
probe_orientationkeepalive(一个空对象命令,用于在状态帧中保持抓握方向的流动)。 - 发送请求,然后移除
session字段——这是一种一次性握手。 - 在随后的每一帧中,将四元数转换为欧拉角,并输出限速后的遥测数据。在每个时间步长中重新发送保持活动信号。
参数
| 名称 | 默认 | 目的 |
|---|---|---|
URI | ws://localhost:10001 | 模拟通道 WebSocket URL |
PRINT_EVERY_MS | 100 | 控制台输出节流阀 |
| 会话配置文件名称 | co.haply.inverse.tutorials:print-verse-grip | 在Haply 中标识此模拟 |
该转换是在应用坐标系中进行的内变换 Z-X-Y(偏航 → 俯仰 → 滚转) +X right, +Y forward, +Z up. 不要 使用 glm::eulerAngles — 它遵循不同的约定,因此在此处显示不正确。这三种语言变体都实现了相同的数学运算;请参阅源代码以查看公式。
probe_orientation 实际上是必要的probe_orientation 仅在您的会话 不 向Inverse3 发送任意指令。一旦您向Inverse3 发出指令Inverse3 力、位置、扭矩等),该服务便会在每个状态帧中自动流式传输已配对的 VerseGrip 的姿态——无需探针。使用 probe_orientation 仅适用于本教程中介绍的此类独立式握力监测工具。
读取状态字段
来自 data.verse_grip[0].state:
orientation—quaternion(w, x, y, z)hall— 整数形式的霍尔传感器读数button— 布尔型
发送/接收
WebSocket 循环:接收状态帧,构建并发送握手帧 + probe_orientation 保持活动。第一个发送的帧携带会话配置;此后每个帧仅携带保持活动信息。
- Python
- C++ (nlohmann)
- C++ (Glaze)
单个异步循环 — recv() → 构建命令 → send() → 重复。
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["verse_grip"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-verse-grip"}}},
"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
libhv 在其 I/O 线程上驱动 WebSocket —— 每帧处理机制位于 ws.onmessage. 主线程在按下 ENTER 键时被阻塞。
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["verse_grip"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-versegrip"}}}}}}},
{"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
采用相同的 libhv 回调模型。使用类型化结构体替代 nlohmann::json — probe_orientation_cmd 是一个空结构体(Glaze 将其写为 {}). std::optional<session_cmd> 负责处理单次握手: .reset() 首次发送后,后续的 JSON 输出中将不再包含该内容。
// Struct models
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct grip_state { quat orientation{}; bool button{}; uint8_t hall{}; };
struct grip_device { std::string device_id; grip_state state; };
struct devices_message { std::vector<grip_device> 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> 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;
out_cmds.session = session_cmd{ /* profile = print-versegrip */ };
device_commands dc{ .device_id = data.verse_grip[0].device_id };
dc.commands.probe_orientation = probe_orientation_cmd{};
out_cmds.verse_grip.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
out_cmds.session.reset(); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
相关: 类型(四元数) · 控制命令 (probe_orientation) · WebSocket 协议 · 教程 03(无线 VG)