06. 統合サンプル(Inverse3 +ワイヤレスVerseGrip)
2つのデバイスを使用するチュートリアルです。グリップを希望する方向に向けた状態でボタンを押し続けると、Inverse3 のカーソルがその方向に移動します。カーソルの移動範囲は球形のワークスペース内に制限されます。
学習内容:
- 同じ状態フレーム内で2種類のデバイスを読み取る (
inverse3およびwireless_verse_grip) - クォータニオングリップが指す方向を抽出する(ローカル
+Yおめでとう) set_cursor_positionを使用して計算された目標地点にカーソルを移動させる- 目標地点を安全な作業領域内に限定する — MinverseはInverse3よりも小さい半径を使用します
- 原点が到達範囲の中央に位置するように ワークスペースのプリセット (
arm_front_centered) の設定
ワークフロー
- 2つのデバイスを検索:
- 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/ティック | ボタンを押している間の移動間隔 |
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 ボタンを押したままグリップを回転させてください。カーソルは、グリップが指し示す方向に沿って移動します。
読み取り状態フィールド
各ティックごとに送信されるステートフレームから、以下の値を読み取ります:
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は最初のステートフレームから2つのデバイス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(HTTP)を通じて2つのデバイスのIDを取得した後、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のコールバックモデルは同じです。型指定された構造体(typed struct)を用いて、状態フレームと送信コマンドの両方をモデル化しています。 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(位置制御)