07. Basis & Mount Playground
Interactively drives the device's mount transform and demonstrates how configure.basis, configure.preset, and configure.mount work together on the wire. Keeps a fixed horizontal floor so the interactive focus stays on the configure commands.
What you'll learn:
- Setting a basis permutation (
"XZY"→ Y-up application frame) - Choosing a preset (
arm_front) and understanding what it configures - Overriding the mount at runtime with a rotation quaternion (keyboard-driven)
- The mutual exclusion rule:
mountandpresetcannot coexist in the same device configure block - One-shot
configuresemantics: each key press sends exactly one configure message - (C++ Glaze) modelling mutually-exclusive fields with
std::optional
Workflow
- On first message: send the session profile,
configure.basis: "XZY", andconfigure.preset: arm_front. Start sendingset_cursor_forcefor the fixed floor. - Each tick: read cursor Y, compute
force_y = max(0, (floor_pos - y) * stiffness), send it. - When the user presses a mount-rotation key, flag
pending_configure = true. - On the next tick: build a
configure.mountblock with a transform whoserotationis a unit quaternion (Z-then-X composition of the current pitch / yaw). Omitpreset— the two are mutually exclusive on the wire. - Reset key (
R) clears the override; next configure falls back topresetagain.
Parameters
| Name | Default | Purpose |
|---|---|---|
BASIS | "XZY" | Axis permutation — Y-up application frame |
DEVICE_PRESET / DEVICE_CONFIG_PRESET | "arm_front" | Named preset — origin at device base |
FLOOR_POS_Y | 0.0 m | Fixed floor plane (application-Y) |
STIFFNESS | 1000 N/m | Floor spring constant |
MOUNT_STEP_DEG | 10° | Rotation per key press |
PRINT_EVERY_MS | 200 | Telemetry throttle |
Controls
| Key | Action |
|---|---|
W / S | Rotate mount ±10° around device +X (pitch) |
A / D | Rotate mount ±10° around device +Z (yaw) |
R | Reset mount — revert to preset |
H | Show controls |
Q | Quit |
mount and preset are mutually exclusiveThe service rejects a device configure block containing both. Once the user overrides the mount, the tutorial omits preset from every subsequent configure. Pressing R re-enables preset on the next configure and drops mount.
C++ variants read line-based input on a background stdin thread (press ENTER after each letter). Python uses the keyboard package for real-time key polling in the main async loop — no ENTER needed. Same keys, same commands.
State fields read
From data.inverse3[i].state:
cursor_position.y—vec3, used to compute floor penetrationcurrent_cursor_force— reported for telemetry
Send / receive
The payload shape is the same across variants; the interesting differences are how each builds the mutually-exclusive mount / preset branching and how the input thread signals the WebSocket thread.
- Python
- C++ (nlohmann)
- C++ (Glaze)
Single async loop with real-time key polling via the keyboard package. pending_configure is a global flag set by the key handlers and cleared each time a configure block is sent.
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["inverse3"][0]["device_id"]
# Handshake: profile + basis + preset
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": device_id,
"configure": build_configure_block(first_handshake=True),
# -> {"basis": {"permutation": "XZY"},
# "preset": {"preset": "arm_front"}}
}],
}
else:
handle_key_inputs() # may set pending_configure = True (classic, not shown)
y = data["inverse3"][0]["state"]["cursor_position"]["y"]
force_y = 0.0 if y > FLOOR_POS_Y else (FLOOR_POS_Y - y) * STIFFNESS
entry = {
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": force_y, "z": 0.0}}},
}
if pending_configure:
entry["configure"] = build_configure_block(first_handshake=False)
# -> {"mount": {...}} OR {"preset": {...}} (never both)
pending_configure = False
request_msg = {"inverse3": [entry]}
await websocket.send(json.dumps(request_msg))
Two-thread model: a background stdin thread reads lines and flips pending_configure (an std::atomic<bool>); the libhv I/O thread checks it each tick and emits configure when set.
std::atomic<bool> pending_configure{false};
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (!data.contains("inverse3") || data["inverse3"].empty()) return;
const bool do_handshake = first_message;
if (first_message) first_message = false;
const bool do_configure = do_handshake || pending_configure.exchange(false);
json request = {};
if (do_handshake) {
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:basis-and-mount"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
json dev_cmd = {{"device_id", el.value()["device_id"]}};
if (do_configure) {
json cfg = {};
if (do_handshake) cfg["basis"] = {{"permutation", BASIS}};
if (mount_overridden) {
cfg["mount"] = {{"transform", {
{"position", {{"x", 0.0}, {"y", 0.0}, {"z", 0.0}}},
{"rotation", quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)},
{"scale", {{"x", 1.0}, {"y", 1.0}, {"z", 1.0}}},
}}};
} else {
cfg["preset"] = {{"preset", DEVICE_CONFIG_PRESET}};
}
dev_cmd["configure"] = cfg;
}
const float y = el.value()["state"]["cursor_position"]["y"].get<float>();
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dev_cmd["commands"] = {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", force_y}, {"z", 0.0}}}}}};
request["inverse3"].push_back(dev_cmd);
}
ws.send(request.dump());
};
std::thread input_thr(input_thread_func); // stdin reader — flips pending_configure
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
The mutual-exclusion rule maps naturally onto std::optional<preset_cfg> and std::optional<mount_cfg>: only the populated one appears in the serialized JSON. The preset name is modelled as an enum class with a glz::meta specialization that maps it to the string the service expects.
// The preset set modelled as an enum
enum class device_preset {
defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom,
};
// Glaze meta — serialize the enum as the JSON string the service expects
template <> struct glz::meta<device_preset> {
using enum device_preset;
static constexpr auto value =
enumerate(defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom);
};
// Transform + the configure block
struct vec3 { float x{}, y{}, z{}; };
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct transform_t { vec3 position{}; quat rotation{}; vec3 scale{1,1,1}; };
struct preset_cfg { device_preset preset; };
struct basis_cfg { std::string permutation; };
struct mount_cfg { transform_t transform; };
struct device_configure {
std::optional<preset_cfg> preset; // mutually exclusive with mount
std::optional<basis_cfg> basis;
std::optional<mount_cfg> mount; // mutually exclusive with preset
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (data.inverse3.empty()) return;
const bool do_handshake = first_message.exchange(false);
const bool do_configure = do_handshake || pending_configure.exchange(false);
commands_message out_cmds{};
if (do_handshake) {
out_cmds.session = session_cmd{ /* profile = basis-and-mount */ };
}
for (const auto &dev : data.inverse3) {
device_commands dc{ .device_id = dev.device_id };
if (do_configure) {
device_configure cfg{};
if (do_handshake) cfg.basis = basis_cfg{BASIS};
if (mount_overridden) {
cfg.mount = mount_cfg{ .transform = transform_t{
.rotation = quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)}};
} else {
cfg.preset = preset_cfg{DEVICE_CONFIG_PRESET};
}
dc.configure = std::move(cfg);
}
const float y = dev.state.cursor_position.y;
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, force_y, 0.0f}};
out_cmds.inverse3.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
std::thread input_thr(input_thread_func);
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
Source: Python · C++ · C++ Glaze
Related: Basis Permutation · Mount & Workspace · Device Configuration · Control Commands (set_cursor_force) · Types (transform) · Tutorial 04 (Hello Floor)