03. Wireless VerseGrip の出力
チュートリアル 02と同じパターンですが、Wireless VerseGrip 向けです。ストリーミング中の状態として、ボタン(A/B/C)とバッテリー残量を追加します。
このチュートリアルで学べる内容:
- Wireless 固有の状態フィールドの読み取り:
buttons.{a,b,c}、battery_level、hall - スタンドアロンのオブザーバー向けのキープアライブとして
probe_orientationを使用する - チュートリアル 02と同様に、最初のメッセージのみでハンドシェイクを行うパターン
ワークフロー
ws://localhost:10001WebSocket 接続を開き、最初のステートフレームを待ちます。wireless_verse_grip配列の中から、最初の Wireless VerseGrip のdevice_idを取得します。- セッションプロファイル と、デバイスごとに
probe_orientation「KeepAlive」を含むリクエストを作成します。 - リクエストを送信した後、
sessionフィールドを削除します。これは1回限りのハンドシェイクです。 - 以降の各フレームごとに、クォータニオンをオイラー角に変換し、ボタンの状態やバッテリー残量を含むテレメトリデータを間引いて出力します。キープアライブは毎ティック再送信します。
パラメータ
| 名前 | デフォルト | 用途 |
|---|---|---|
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 プロトコル