06. 組み合わせ(Inverse3 +Wireless VerseGrip)
2台のデバイスを使ったチュートリアル:ハンドルを合わせ、ボタンを長押しして、Inverse3 のカーソルをその方向に移動させます。カーソルは球形の作業領域内に制限されます。
学習内容:
- 同一の状態フレームから2種類のデバイスを読み出す(
inverse3undwireless_verse_grip) - ハンドルの向きを、その クォータニオン (ローカル
+Y-軸) - 「Verwenden von」の使用
set_cursor_position、カーソルを計算された目標位置に移動させるために - 目標点を安全な作業領域の球体に限定する――Minverse では、より小さな半径が使用されている。Inverse3
- の指定 作業スペースのプリセット (
arm_front_centered), これにより、原点が射程範囲の中央に位置するようにする
手順
- 両方のデバイスが認識します:
- C++-起動時にHTTP経由でバリエーションを問い合わせる
GET /devicesab、その後、キャリブレーションの要求を表示し、ENTERキーの入力待ちとなります。 - Pythonは、WebSocketの最初のステータスフレームから両方のデバイスIDを読み取ります。
- C++-起動時にHTTP経由でバリエーションを問い合わせる
- これを登録してください 会議概要 そして、最初のメッセージに
configure.preset: arm_front_centered(1回限りの握手)。 - 各サイクルごとに:以下の内容をお読みください
orientationグリフと、その状態についてbuttons.{a, b}. - 移動キーが押されたままの場合、ワールド座標系におけるグリップの方向を計算してください(
R(q) · ĵ– 回転した基準軸(+Y)を求め、これを、以下の倍率でスケーリングして加算してください。SPEED, 目標位置へ。 - ワークスペース球体内の目標点を指定し、それを
set_cursor_position. - (Pythonのみ) 以下の値に基づいて球の半径を調整してください
config.typeデバイスの――minverse= 0.04 m、その他すべて = 0.10 m。
パラメータ
| 名称 | 標準値 | Zweck |
|---|---|---|
SPEED | 0.01 m/tick | キーが押されている間のステップ幅 |
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 に自動キャリブレーションを行わせてください(または、グリップをInkwellに置いて、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}— Booleans- (Python、最初のブロック内のみ)
data.inverse3[0].config.type— 「Inverse3 」の半径を選択するか、Minverse - (Python、最初のブロック内のみ)
data.inverse3[0].status.calibrated— 値が false の場合、ユーザーに次のように促します
送信/受信
クォータニオンからの方向の計算(回転の +Y mit R(q)) および球面への制限は、古典的な線形代数の範囲です――ソースファイルを参照してください。逆APIの側面は、ハンドシェイクと、1サイクルごとに送信される set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
たった1つの非同期ループです。Pythonは最初のステータスフレームから両方のデバイス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++は起動時に、以下の方法を用いて両方のデバイスIDを特定します。 GET /devices (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 と同じコールバックモデルです。型付けされた構造体が、状態フレームと送信されるコマンドの両方を表現しています――これら2つは std::vector<> で devices_message 1つだけで glz::read 両方の機種で読み取ることができます。
// 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) ・ 型 (クォータニオン、vec3) ・ モンタージュ&ワークスペース(プリセット) ・ チュートリアル 03(ワイヤレス VG) ・ チュートリアル 05(位置制御)