06. 组合(Inverse3 + 无线 VerseGrip)
双设备教程:将握把指向某个方向并按住按钮,即可驱动Inverse3 光标沿该方向移动。光标被限制在球形工作空间内。
你将学到:
- 在同一状态帧中读取两种设备类型(
inverse3和wireless_verse_grip) - 从握把的 四元数(局部
+Y从(轴)中提取指向方向 - 使用
set_cursor_position将光标移动到计算得出的目标位置 - 将目标限制在安全的球形工作空间内——Minverse 所使用的半径小于Inverse3
- 设置 工作区预设(
arm_front_centered),使原点位于可达范围的中心
工作流程
- 发现两台设备:
- 启动时,C++ 的各个变体通过 HTTP 查询
GET /devices,随后打印校准提示,并等待按下 ENTER 键。 - Python从第一个 WebSocket 状态帧中读取两台设备的 ID。
- 启动时,C++ 的各个变体通过 HTTP 查询
- 在第一条消息中注册 会话配置文件,并设置
configure.preset: arm_front_centered(一次性握手)。 - 每个周期:读取握把的
orientation以及buttons.{a, b}状态。 - 如果按住运动按钮,则计算手柄在世界坐标系中的方向(
R(q) · ĵ,即旋转后的+Y轴方向),并按SPEED缩放后叠加到目标位置上。 - 将目标限制在工作空间球体内,并通过
set_cursor_position发送。 - (仅限 Python)根据设备的
config.type调整球体半径——minverse为 0.04 米,其他所有类型均为 0.10 米。
参数
| 名称 | 默认值 | 用途 |
|---|---|---|
SPEED | 0.01 m/周期 | 按住按钮时每步的移动距离 |
RADIUS_INVERSE3 | 0.10 m | Inverse3 / Inverse3x 的工作空间限制半径 |
RADIUS_MINVERSE | 0.04 m | Minverse 工作空间限制半径(仅适用于 Python —— 在 C++ 中硬编码为 0.10) |
PRINT_EVERY_MS | 200 | 遥测输出节流间隔 |
| 会话配置文件名称 | co.haply.inverse.tutorials:combined | 在Haply Hub中标识此模拟 |
运行前先进行校准
- 让Inverse3 自行校准(或将笔握放在墨盒上,等待LED常亮)。
- 将笔握从墨水盒上取下。
- 按住 A 或 B 键并转动握把——光标将沿握把指向的方向移动。
读取的状态字段
从每个周期的状态帧中读取:
data.inverse3[0].state.cursor_position—vec3data.wireless_verse_grip[0].state.orientation—quaterniondata.wireless_verse_grip[0].state.buttons.{a, b, c}— 布尔值- (Python,仅第一帧)
data.inverse3[0].config.type—— 用于选择“Inverse3 ”或“Minverse ”的半径 - (Python,仅第一帧)
data.inverse3[0].status.calibrated—— 若为 false,则提示用户
发送 / 接收
四元数到方向的计算(使用 R(q) 旋转 +Y)以及球体限制都是经典的线性代数运算,详见源文件。Inverse API 部分由握手过程以及每个周期的 set_cursor_position 调用结构。
- Python
- C++ (nlohmann)
- C++ (Glaze)
单一异步循环。Python 从第一个状态帧中读取两台设备的 ID;握手过程将配置文件 + configure.preset: arm_front_centered 附加到第一次 set_cursor_position 正在调用。
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
inverse3_id = data["inverse3"][0]["device_id"]
grip_id = data["wireless_verse_grip"][0]["device_id"]
radius = get_workspace_radius(data["inverse3"][0].get("config", {}))
# Handshake: profile + preset + first position command
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": inverse3_id,
"configure": {"preset": {"preset": "arm_front_centered"}},
"commands": {"set_cursor_position": {"position": position}},
}],
}
else:
# Per tick: update position from grip pointing direction (classic math, not shown), send
request_msg = {
"inverse3": [{
"device_id": inverse3_id,
"commands": {"set_cursor_position": {"position": position}},
}],
}
await websocket.send(json.dumps(request_msg))
C++ 在启动时通过 GET /devices 获取两台设备的 ID(HTTP),然后建立 WebSocket 连接。onmessage 在 libhv 的 I/O 线程上运行;主线程则处于阻塞状态,等待按下 ENTER 键。
// Startup (synchronous):
const std::string inv3_device_id = get_first_device_id("inverse3");
const std::string grip_device_id = get_first_device_id("wireless_verse_grip");
// Per tick:
ws.onmessage = [&](const std::string &msg) {
json data = json::parse(msg);
// ... classic math (not shown): update local Inverse3State + WirelessVerseGripState,
// compute new position from grip orientation, clamp to sphere ...
json command;
command["inverse3"] = json::array();
command["inverse3"].push_back({
{"device_id", inv3_device_id},
{"commands", {{"set_cursor_position",
{{"position", {{"x", pos.x}, {"y", pos.y}, {"z", pos.z}}}}}}}
});
if (first_message) {
first_message = false;
command["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:combined"}}}}}};
command["inverse3"][0]["configure"] = {
{"preset", {{"preset", "arm_front_centered"}}}};
}
ws.send(command.dump());
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
与 libhv 的回调模型相同。使用强类型结构体对状态帧和发出的命令进行建模——devices_message 其中的两个 std::vector<> 使得单次 glz::read 即可同时解析这两种设备类型。
// 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{}; };
struct wvg_device { std::string device_id; wvg_state state; };
struct inverse_state { vec3 cursor_position{}, cursor_velocity{}; };
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message {
std::vector<inverse_device> inverse3;
std::vector<wvg_device> wireless_verse_grip;
};
struct set_cursor_position_cmd { vec3 position; };
struct commands_message {
std::optional<session_cmd> session;
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
const auto &wvg = data.wireless_verse_grip[0].state;
cursor_pos = data.inverse3[0].state.cursor_position;
// ... classic math (not shown): if (wvg.buttons.a || wvg.buttons.b)
// move in pointing dir; clamp to sphere ...
commands_message out_cmds{};
device_commands dc{ .device_id = inv3_device_id };
dc.commands.set_cursor_position = set_cursor_position_cmd{cursor_pos};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = combined */ };
dc.configure = device_configure{ .preset = preset_cfg{"arm_front_centered"} };
}
out_cmds.inverse3.push_back(std::move(dc));
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
随 SDK 安装程序一同提供
教程 06 也会随 SDK 一起安装在本地——请在服务安装目录下的 tutorials/06-haply-inverse-combined/ 中查找。
相关内容: 控制命令(set_cursor_position) · 类型(四元数、vec3) · 安装与工作区(默认设置) · 教程 03(无线 VG) · 教程 05(位置控制)