06. 統合(Inverse3 + ワイヤレス VerseGrip)
2 デバイスのチュートリアルです。グリップを向けてボタンを押し続けると、その方向へ Inverse3 のカーソルが動きます。カーソルは球状のワークスペース内に制限されます。
学習内容:
- 同じ state フレーム内の 2 種類のデバイス(
inverse3とwireless_verse_grip)を読み取る - クォータニオン からグリップの向き(ローカルの
+Y軸)を取り出す set_cursor_positionを使い、計算したターゲットへカーソルを動かす- ターゲットを安全なワークスペース球内に制限する。Minverse は Inverse3 より小さい半径を使用する
- ワークスペースのプリセット(
arm_front_centered)を設定し、原点が到達範囲の中央に来るようにする
ワークフロー
- 両方のデバイスを検出します:
- C++ 版は起動時に HTTP 経由で
GET /devicesを照会し、キャリブレーションのプロンプトを表示して ENTER の入力を待ちます。 - Python は最初の WebSocket state フレームから両デバイスの 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 m、それ以外は 0.10 m です。
パラメータ
| 名前 | デフォルト | 用途 |
|---|---|---|
SPEED | 0.01 m/tick | ボタン押下中の移動ステップ |
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 を押したままグリップを回転させます。カーソルはグリップが向いている方向に動きます。
読み取る state フィールド
各ティックの state フレームから以下を読み取ります:
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 は最初の state フレームから両デバイスの ID を読み取ります。ハンドシェイクでは、最初の set_cursor_position にプロファイルと configure.preset: arm_front_centered を付与します。
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 のコールバックモデルは同じです。型付き構造体で、state フレームと送信するコマンドの両方を表現しています。devices_message 内の 2 つの 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
チュートリアル 06 も SDK と一緒にローカルにインストールされています。サービスのインストールディレクトリ配下の tutorials/06-haply-inverse-combined/ を参照してください。
関連: 制御コマンド(set_cursor_position) · 型(quaternion, vec3) · マウントとワークスペース(プリセット) · チュートリアル 03(ワイヤレス VG) · チュートリアル 05(位置制御)