03. ワイヤレスVerseGrip出力
チュートリアル02と同じパターンですが、ワイヤレスVerseGrip用です。ストリーミング中の状態において、ボタン(A/B/C)とバッテリー残量を追加します。
学習内容:
- 無線専用ステータスフィールドの読み取り:
buttons.{a,b,c},battery_level,hall - 独立したオブザーバー向けのキープアライブとして
probe_orientation使い方 - チュートリアル02と同様に、最初のメッセージでのみ実行されるハンドシェイクパターン
ワークフロー
ws://localhost:10001WebSocket接続を開き、最初のステータスフレームを待ちます。wireless_verse_grip配列の最初のワイヤレスVerseGripのdevice_idを取得します。- セッションプロファイルおよびデバイス別
probe_orientationkeepaliveを含むリクエストを設定します。 - リクエストを送信した後
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ここでは、固有の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を有効にしてください。
送信 / 受信
チュートリアル 02…のような構造であり、デバイスの配置だけが wireless_verse_gripに変わります。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
型が指定された構造体を使用します。参考までに wvg_stateの buttons フィールドを隠さないように button_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— 一行の要約の代わりに、生のJSONペイロードを見やすく表示します。--query-config— 送信ティックごとにsession.force_render_full_stateを再度注入することで、すべてのフレームが状態の変化分のみを含むのではなく、configブロックを含めるようにします。
--query-configは config ブロックは、一般的な構成フィールドに加え、WVG専用 sub_type フィールド(stylus / prototype / ruko / kingfisher(3.5で追加された機能)が含まれているため、ワイヤレスVerseGripでの使用に特に便利です。 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(有線 VG) ・ 型(クォータニオン) ・ 制御コマンド (probe_orientation) ・ WebSocketプロトコル