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 ステートフレームから両デバイスの 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) ・ 型(クォータニオン、vec3) ・ マウントとワークスペース(プリセット) ・ チュートリアル 03(ワイヤレス VG) ・ チュートリアル 05(位置制御)