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 ステータスフレームから 2 台のデバイスの ID を読み取ります。
- 起動時には、C++ の各バリエーションをHTTPクエリを通じて
- 最初のメッセージで登録する 会話プロファイル,そして設定する
configure.preset: arm_front_centered(一度きりの握手)。 - 各サイクル:グリップの
orientationおよびbuttons.{a, b}状態。 - 運動ボタンを長押しすると、ワールド座標系におけるグリップの方向が計算されます(
R(q) · ĵ,すなわち回転後の単位の+Y軸)を指定し、SPEED拡大・縮小した後、目的の位置に重ね合わせます。 - ターゲットをワークスペースの球体内に制限し、以下の方法で
set_cursor_position送信。 - (Pythonのみ)デバイスの
config.type球体の半径を調整する――minverse0.04 m、その他のすべてのタイプは 0.10 m である。
パラメータ
| 名称 | デフォルト値 | 用途 |
|---|---|---|
SPEED | 0.01 m/周期 | ボタンを押し続けた場合の1ステップあたりの移動距離 |
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、第1フレームのみ)
data.inverse3[0].config.type——Inverse3 またはMinverse の半径を選択するためのもの - (Python、第1フレームのみ)
data.inverse3[0].status.calibrated—— false の場合、ユーザーにメッセージを表示する
送信 / 受信
四元数から方向への変換(使用して R(q) 回転 +Y)および球体制限はいずれも古典的な線形代数の演算であり、詳細はソースファイルを参照のこと。Inverse API の部分は、ハンドシェイク手順および各周期の set_cursor_position 呼び出しの構成。
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループ。Python は最初のステートフレームから 2 台のデバイスの 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++ は起動時に GET /devices 2台のデバイスの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 のコールバックモデルと同じです。強型構造体を使用して、ステートフレームと送信されるコマンドをモデル化しています――devices_message そのうちの2つ std::vector<> 1回の glz::read これにより、2種類のデバイスを同時に解析できます。
// 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(位置制御)