06. 組み合わせ(Inverse3 Wireless VerseGrip)
2つのデバイスを使ったチュートリアル:グリップを向け、ボタンを押し続けると、Inverse3 がその方向に移動します。カーソルは球形の作業領域内に固定されています。
学習内容:
- 同じステートフレーム内で2種類のデバイスを読み取る(
inverse3そしてwireless_verse_grip) - グリップの向きをその 四元数 (ローカル
+Y(軸) - 使用して
set_cursor_positionカーソルを計算された目標地点へと移動させる - ターゲットを安全な作業領域内に固定する —Minverse Inverse3よりも半径を小さくMinverse
- 設定 ワークスペースのプリセット (
arm_front_centered)そのため、原点がリーチの中央に位置するように
ワークフロー
- 両方のデバイスをご覧ください:
- C++ バリアントのクエリ
GET /devices起動時にHTTP経由で接続し、キャリブレーションのプロンプトを表示して、ENTERキーの入力待ちを行う。 - Pythonは、最初のWebSocketステートフレームから両方のデバイスIDを読み取ります。
- C++ バリアントのクエリ
- を登録する セッションプロファイル そして設定する
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 のワークスペースクランプ半径Minverse Python のみ — C++ ではハードコーディングMinverse 0.10) |
PRINT_EVERY_MS | 200 | テレメトリースロットル |
| セッションプロファイル名 | co.haply.inverse.tutorials:combined | Haply 上でこのシミュレーションを特定します |
実行する前にキャリブレーションを行ってください
- Inverse3 を行わせます(またはグリップをインクウェルに置き、LEDが点灯したままになるのを待ちます)。
- インク壺からグリップを取り外してください。
- AまたはBボタンを押したままグリップを回すと、カーソルがグリップの向いている方向へ移動します。
状態フィールドを読み取る
ティックごとの状態フレームから:
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の場合、ユーザーに確認を求める
送信/受信
四元数から方向への変換式(回転 +Y によって R(q)) および sphere clamp は古典的な線形代数の手法です — ソースファイルを参照してください。Inverse-API 側はハンドシェイクとティックごとの処理です set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループ。Pythonは最初のステートフレームから両方のデバイスIDを読み取り、ハンドシェイクでプロファイルと configure.preset: arm_front_centered 最初の 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++は、以下の方法を通じて両方のデバイスIDを検出します GET /devices 起動時(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 コールバックモデルです。型付き構造体モデルは、ステートフレームと送信コマンドの両方を扱います — 2つ std::vector<> で devices_message 1つだけ 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
関連: 制御コマンド (set_cursor_position) ・ 型 (quaternion, vec3) ・ マウントとワークスペース(プリセット) ・ チュートリアル 03(ワイヤレス VG) ・ チュートリアル 05(位置制御)