03. Wireless VerseGripのデータを表示する
チュートリアル02と同じ仕組みですが、Wireless VerseGrip向けです。ストリーミング中の画面にボタン(A/B/C)とバッテリー残量を表示します。
このコースで学べる内容:
- ワイヤレス版固有のステータスフィールドを読み取る:
buttons.{a,b,c},battery_level,hall - 利用
probe_orientation自律型オブザーバー向けのキープアライブメッセージとして - チュートリアル02と同様、最初のメッセージに限定された握手(handshake)の仕組み
進行
- WebSocket接続を以下に向けて確立してください
ws://localhost:10001そして、最初のステータスフレームを待ちます。 - [を]取り戻す
device_id表にある最初のWireless VerseGripからwireless_verse_grip. - 以下のを使用してクエリを作成してください セッションプロファイル そしてキープアライブ
probe_orientation1台あたり。 - リクエストを送信してから、そのフィールドを削除してください
session— これはその場限りの握手だ。 - 次のフレームごとに、クォータニオンをオイラー角に変換し、ボタンの状態やバッテリー残量を含むテレメトリデータを、サンプリングレート制限付きで表示してください。各反復ごとにキープアライブを送信してください。
パラメータ
| ノム | デフォルト値 | 役割 |
|---|---|---|
URI | ws://localhost:10001 | WebSocket シミュレーションチャネルの URL |
PRINT_EVERY_MS | 100 | コンソールの表示頻度の制限 |
| セッションプロファイル名 | co.haply.inverse.tutorials:print-wireless-verse-grip | Haply Hub でこのシミュレーションを特定してください |
変換は、適用座標系において本質的にZ-X-Y(横揺れ→ピッチ→ロール)の変換である。 +X right, +Y forward, +Z up. Ne pas utiliser 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)
非同期ループは1つだけ。
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) フィールドが隠れてしまうのを防ぐために buttons de wvg_state — 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— réinjectesession.force_render_full_state各出力ティックごとに、各フレームにそのブロックが含まれるようにconfig単なる状態遷移ではなく。
--query-config 特にワイヤレスのVerseGripには非常に便利で、そのブロックが config このフィールドを含みます sub_type WVGに固有の(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(有線VG) ・ 型(クォータニオン) ・ 制御コマンド (probe_orientation) ・ WebSocketプロトコル