05. Position Control
Drives the Inverse3 cursor toward a target position via set_cursor_position. The interaction model differs by language — C++ uses one-shot random targets, Python uses continuous keyboard-driven motion.
What you'll learn:
- Using
set_cursor_positionfor position-mode control - Two different interaction models for the same underlying command
- Clamping the target to a workspace sphere — Minverse uses a smaller radius than Inverse3
- Setting a workspace preset so the origin sits at workspace centre
Workflow
C++ (random-target model)
- Start a background input thread that reads line-buffered keystrokes (
n,+,-,q) from stdin. - Open the WebSocket. On the first state frame, register the session profile and set
configure.preset: arm_front_centered. Generate the first random target inside a sphere (rejection sampling, radius 0.08 m). - Each tick, send a
set_cursor_positioncommand to the current target. The cursor smoothly tracks it — the service rate-limits and interpolates. - When the user types
n+ ENTER, the input thread generates a new random target.+/-adjust speed;qquits.
Python (hold-to-move model)
- Open the WebSocket. On the first state frame, check
status.calibrated— prompt the user if the device isn't calibrated yet. - Read
config.typeto choose the workspace radius (minverse= 0.04 m, anything else = 0.10 m). - Register the session profile and set
configure.preset: arm_front_centered. - Each tick: poll keyboard state (
W/A/S/D/Q/E), update the target position bySPEEDalong each pressed axis, clamp to the workspace sphere, and sendset_cursor_position.Rresets the target to the origin.
Parameters
| Name | Default (C++) | Default (Python) | Purpose |
|---|---|---|---|
workspace_radius / RADIUS_INVERSE3 | 0.08 m | 0.10 m (Inverse3) / 0.04 m (Minverse) | Target sphere radius |
speed_step / SPEED | 0.01 / press | 0.00005 m / tick | Step per interaction |
PRINT_EVERY_MS | — | 100 | Telemetry throttle (Python) |
| Session profile | co.haply.inverse.tutorials:position-control | same | Identifies in Haply Hub |
The Python variant checks status.calibrated from the first state frame and prompts the user if the device isn't calibrated. The C++ variant assumes calibration is already complete.
State fields read
data.inverse3[0].device_id— for building the commanddata.inverse3[0].state.cursor_position— telemetry- (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
Communication workflow
- C++ runs a background stdin thread that writes
std::atomic<float>targets; the WebSocket thread reads them each tick. Onn+ ENTER, the input thread generates a new random target; onq, both threads shut down. - Python is single-threaded async — the WebSocket loop polls keyboard state each tick and updates
positiondirectly.
The Inverse-API payloads are the same: first tick carries the session profile + configure.preset, subsequent ticks carry only set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
Single async loop. Keyboard polling (handle_keys) runs inline every tick — no threads. config.type and status.calibrated are read once from the first state frame.
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"]
radius = get_workspace_radius(data["inverse3"][0].get("config", {}))
# Handshake: profile + preset (one-shot)
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": device_id,
"configure": {"preset": {"preset": "arm_front_centered"}},
}],
}
else:
# Per tick: update position from keyboard (classic polling, not shown), send command
position = handle_keys(position, radius)
request_msg = {
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_position": {"position": position}},
}],
}
await websocket.send(json.dumps(request_msg))
Two-thread model: a background thread reads stdin and writes std::atomic<float> targets; the libhv I/O thread runs ws.onmessage each tick and reads the atomics.
// Shared state written by the stdin thread, read by the ws thread
static std::atomic<float> target_x{0.0f}, target_y{0.0f}, target_z{0.0f};
ws.onmessage = [&](const std::string &message) {
const json data = json::parse(message);
if (data["inverse3"].empty()) return;
json request = {};
const bool do_handshake = first_message.exchange(false);
if (do_handshake) {
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:position-control"}}}}}};
generate_random_target();
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
json dev = {
{"device_id", el.value()["device_id"]},
{"commands", {{"set_cursor_position",
{{"position", {{"x", target_x.load()},
{"y", target_y.load()},
{"z", target_z.load()}}}}}}},
};
if (do_handshake)
dev["configure"] = {{"preset", {{"preset", "arm_front_centered"}}}};
request["inverse3"].push_back(dev);
}
ws.send(request.dump());
};
ws.open("ws://localhost:10001");
std::thread input_thr(input_thread_func); // stdin reader — writes target atomics
while (running) std::this_thread::sleep_for(100ms);
Same two-thread model. Typed structs for the command — std::optional<device_configure> carries the one-shot preset per-device; omitted from JSON on subsequent ticks.
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct set_cursor_position_cmd { vec3 position; };
struct preset_cfg { std::string preset; };
struct device_configure { std::optional<preset_cfg> preset; };
struct device_commands {
std::string device_id;
std::optional<device_configure> configure; // one-shot
struct commands_t {
std::optional<set_cursor_position_cmd> set_cursor_position;
} commands;
};
struct commands_message {
std::optional<session_cmd> session; // one-shot
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
commands_message request;
const bool do_handshake = first_message.exchange(false);
if (do_handshake) {
request.session = session_cmd{ /* profile = position-control */ };
}
for (const auto &dev : data.inverse3) {
device_commands dc{ .device_id = dev.device_id };
dc.commands.set_cursor_position = set_cursor_position_cmd{
.position = {target_x.load(), target_y.load(), target_z.load()}};
if (do_handshake)
dc.configure = device_configure{ .preset = preset_cfg{"arm_front_centered"} };
request.inverse3.push_back(std::move(dc));
}
std::string out;
(void)glz::write_json(request, out);
ws.send(out);
};
ws.open("ws://localhost:10001");
std::thread input_thr(input_thread_func);
while (running) std::this_thread::sleep_for(100ms);
Source: Python · C++ · C++ Glaze
Related: Control Commands (set_cursor_position) · Mount & Workspace (presets) · Types (vec3) · Tutorial 06 (Combined)