06. Combined (Inverse3 + Wireless VerseGrip)
Two-device tutorial: point the grip and hold a button to drive the Inverse3 cursor in that direction. The cursor is clamped inside a spherical workspace.
What you'll learn:
- Reading two device types in the same state frame (
inverse3andwireless_verse_grip) - Extracting the grip's pointing direction from its quaternion (local
+Yaxis) - Using
set_cursor_positionto drive the cursor toward a computed target - Clamping the target to a safe workspace sphere — Minverse uses a smaller radius than Inverse3
- Setting a workspace preset (
arm_front_centered) so the origin sits at the middle of reach
Workflow
- Discover both devices:
- C++ variants query
GET /devicesover HTTP at startup, then print a calibration prompt and wait for ENTER. - Python reads both device IDs from the first WebSocket state frame.
- C++ variants query
- Register the session profile and set
configure.preset: arm_front_centeredon the first message (one-shot handshake). - Each tick: read the grip's
orientationandbuttons.{a, b}state. - If a motion button is held, compute the grip's world-space direction (
R(q) · ĵ— the rotated unit +Y axis) and accumulate it into the target position scaled bySPEED. - Clamp the target inside the workspace sphere and send it via
set_cursor_position. - (Python only) Adapt the sphere radius from the device's
config.type—minverse= 0.04 m, everything else = 0.10 m.
Parameters
| Name | Default | Purpose |
|---|---|---|
SPEED | 0.01 m/tick | Motion step while a button is held |
RADIUS_INVERSE3 | 0.10 m | Workspace clamp radius for Inverse3 / Inverse3x |
RADIUS_MINVERSE | 0.04 m | Workspace clamp radius for Minverse (Python only — C++ hardcodes 0.10) |
PRINT_EVERY_MS | 200 | Telemetry throttle |
| Session profile name | co.haply.inverse.tutorials:combined | Identifies this simulation in Haply Hub |
- Let the Inverse3 self-calibrate (or place the grip on the inkwell and wait for the LED to turn solid).
- Detach the grip from the inkwell.
- Hold A or B and rotate the grip — the cursor moves along the direction the grip is pointing.
State fields read
From the per-tick state frame:
data.inverse3[0].state.cursor_position—vec3data.wireless_verse_grip[0].state.orientation—quaterniondata.wireless_verse_grip[0].state.buttons.{a, b, c}— booleans- (Python, first frame only)
data.inverse3[0].config.type— selects Inverse3 vs Minverse radius - (Python, first frame only)
data.inverse3[0].status.calibrated— prompts the user if false
Send / receive
The quaternion-to-direction math (rotate +Y by R(q)) and sphere clamp are classic linear algebra — see the source files. The Inverse-API side is the handshake + per-tick set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
Single async loop. Python reads both device IDs from the first state frame; the handshake attaches profile + configure.preset: arm_front_centered to the first 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++ discovers both device IDs via GET /devices at startup (HTTP), then opens the WebSocket. onmessage runs on libhv's I/O thread; the main thread blocks on 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
Same libhv callback model. Typed structs model both state frames and the outgoing command — two std::vector<> in devices_message let a single glz::read yield both device types.
// 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
Source: Python · C++ · C++ Glaze
Related: Control Commands (set_cursor_position) · Types (quaternion, vec3) · Mount & Workspace (presets) · Tutorial 03 (Wireless VG) · Tutorial 05 (Position Control)