03. Wireless VerseGrip の出力
チュートリアル 02 と同じパターンですが、Wireless VerseGrip 向けです。ストリーミングされる状態にボタン(A/B/C)とバッテリーレベルを追加します。
このチュートリアルで学べること:
- Wireless 固有の状態フィールドの読み取り:
buttons.{a,b,c}、battery_level、hall - スタンドアロンのオブザーバー向けキープアライブとして
probe_orientationを使用する - チュートリアル 02 と同じく、最初のメッセージのみでハンドシェイクを行うパターン
ワークフロー
ws://localhost:10001への WebSocket を開き、最初の状態フレームを待ちます。wireless_verse_grip配列から、最初の Wireless VerseGrip のdevice_idを取得します。- セッションプロファイル と、デバイスごとの
probe_orientationキープアライブを含むリクエストを作成します。 - リクエストを送信した後、
sessionフィールドを削除します。これは一回限りのハンドシェイクです。 - 以降のフレームごとに、クォータニオンをオイラー角に変換し、ボタンの状態とバッテリーレベルを含むテレメトリを間引いて出力します。キープアライブは毎ティック再送します。
パラメータ
| 名前 | デフォルト | 用途 |
|---|---|---|
URI | ws://localhost:10001 | シミュレーションチャネル WebSocket の URL |
PRINT_EVERY_MS | 100 | コンソール出力の間引き間隔 |
| セッションプロファイル名 | co.haply.inverse.tutorials:print-wireless-verse-grip | Haply Hub でこのシミュレーションを識別します |
変換は、アプリケーションフレーム +X right, +Y forward, +Z up における内因的(intrinsic)な Z-X-Y(ヨー → ピッチ → ロール)です。glm::eulerAngles は使用しないでください。この関数は別の規約に従うため、ここでは誤った値になります。3 つの言語版はいずれも同じ計算を実装しています。計算式はソースコードを参照してください。
probe_orientation が実際に必要になる場合probe_orientation が役立つのは、セッションから Inverse3 へコマンドを一切送信しない場合のみです。Inverse3(力、位置、トルクなど)にコマンドを送信するとすぐに、サービスはすべての状態フレームでペアになった VerseGrip の姿勢を自動的にストリーミングします。そのためプローブは不要です。probe_orientation は、このチュートリアルのようなスタンドアロンのグリップ監視ツールでのみ使用してください。
読み取る状態フィールド
data.wireless_verse_grip[0].state から読み取ります:
orientation—quaternion(w, x, y, z)hall— 整数値のホールセンサーの読み取り値buttons.a、buttons.b、buttons.c— 真偽値battery_level— 浮動小数点数(0.0 〜 1.0)transform.rotation— ワークスペースの回転(quaternion)。姿勢のみのデバイスでは位置とスケールは該当せず、出力されることはありません
transform.rotation は、単位クォータニオン {w:1,x:0,y:0,z:0} と等しい場合は省略されます。読み取り時にはデフォルト値を設定してください。常に受信するには serialization/explicit_fields を有効にしてください。
送信 / 受信
wireless_verse_grip デバイス配列を使う点を除けば、チュートリアル 02 と同じ形式です。WebSocket のループは状態フレームを受信し、ハンドシェイクと probe_orientation のキープアライブを返信します。最初の送信メッセージにはセッションプロファイルが含まれ、以降のフレームにはキープアライブのみが含まれます。
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループです。
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
device_id = data["wireless_verse_grip"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-wireless-verse-grip"}}},
"wireless_verse_grip": [{
"device_id": device_id,
"commands": {"probe_orientation": {}} # empty — keepalive
}]
}
await websocket.send(json.dumps(request_msg))
request_msg.pop("session", None) # one-shot handshake
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["wireless_verse_grip"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-wireless-verse-grip"}}}}}}},
{"wireless_verse_grip", json::array({
{{"device_id", device_id},
{"commands", {{"probe_orientation", json::object()}}}},
})},
};
}
ws.send(request_msg.dump());
request_msg.erase("session"); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
型付きの構造体です。button_state は(buttons ではなく)明示的に名付けしています。これは、wvg_state の buttons フィールドをシャドウイングしないようにするためです。Glaze はフィールド名でマッピングするため、構造体の型名は自由に決められます。
// 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{};
float battery_level{};
};
struct wvg_device { std::string device_id; wvg_state state; };
struct devices_message { std::vector<wvg_device> wireless_verse_grip; };
struct probe_orientation_cmd {}; // empty object on the wire
struct commands_message {
std::optional<session_cmd> session; // one-shot — omitted when unset
std::vector<device_commands> wireless_verse_grip;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (first_message) {
first_message = false;
request_msg.session = session_cmd{ /* profile = print-wireless-verse-grip */ };
device_commands dc{ .device_id = data.wireless_verse_grip[0].device_id };
dc.commands.probe_orientation = probe_orientation_cmd{};
request_msg.wireless_verse_grip.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(request_msg, out_json);
ws.send(out_json);
request_msg.session.reset(); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
コマンドラインフラグ(Python)
Python 版は、チュートリアル 01 と同じ 2 つのフラグを受け付けます:
--full— 1 行の要約の代わりに、生の JSON ペイロードを整形して出力します。--query-config— 送信するティックごとにsession.force_render_full_stateを再注入し、各フレームが状態の差分だけでなくconfigブロックを含むようにします。
--query-config は、Wireless VerseGrip で特に役立ちます。config ブロックには、通常の設定フィールドに加えて、WVG 固有の sub_type フィールド(stylus / prototype / ruko / kingfisher、3.5 で追加)が含まれるためです。python 03-haply-inverse-print-wireless-verse-grip.py --full --query-config を実行すると、他の設定値と並べて config.sub_type がリアルタイムに更新される様子を確認できます。
C++ 版では、これらのフラグは公開されていません。
チュートリアル 03 は SDK とともにローカルにもインストールされます。サービスのインストールディレクトリ配下の tutorials/03-haply-inverse-print-wireless-verse-grip/ を参照してください。
関連: チュートリアル 02(Wired VG) · 型(クォータニオン) · 制御コマンド(probe_orientation) · WebSocket プロトコル