06. 组合款(Inverse3 Wireless VerseGrip)
双设备教程:指向握把并按住按钮,即可将Inverse3 朝该方向移动。光标被限制在球形工作空间内。
您将学到:
- 在同一个状态帧中读取两种设备类型(
inverse3和wireless_verse_grip) - 从握把中提取其指向方向 四元数 (本地
+Y轴) - 使用
set_cursor_position将光标移动到计算出的目标位置 - 将目标固定在安全工作空间球体内——Minverse 半径比Inverse3更小
- 设置一个 工作区预设 (
arm_front_centered) 因此原点位于伸展范围的中间
工作流程
- 了解这两款设备:
- C++ 变体查询
GET /devices启动时通过 HTTP 连接,然后显示校准提示并等待按 ENTER 键。 - Python会从第一个 WebSocket 状态帧中读取这两个设备 ID。
- C++ 变体查询
- 注册 会话配置文件 并设置
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/tick | 按住按钮时的移动步骤 |
RADIUS_INVERSE3 | 0.10 m | Inverse3 Inverse3x 的工作区夹紧半径 |
RADIUS_MINVERSE | 0.04 m | Minverse 的工作区夹持半径Minverse 仅限 Python — C++ 版本为硬编码Minverse 0.10) |
PRINT_EVERY_MS | 200 | 遥测节流阀 |
| 会话配置文件名称 | co.haply.inverse.tutorials:combined | 在Haply 中标识此模拟 |
运行前请进行校准
- 让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,则提示用户
发送/接收
四元数到方向的数学运算(旋转 +Y 由 R(q)) 和球面夹具属于经典的线性代数——请参阅源文件。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++ 通过以下方式识别这两个设备 ID: GET /devices 在启动时(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 回调模型。类型化结构体同时用于表示状态帧和发出的命令——两者 std::vector<> 在 devices_message 让一首 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
相关: 控制命令 (set_cursor_position) · 类型 (四元数, vec3) · 安装与工作区(预设) · 教程 03(无线 VG) · 教程 05(位置控制)