07. ベース&マウント・プレイグラウンド
デバイスの機能をインタラクティブに制御します マウント変換 そして、その方法を示しています configure.basis, configure.presetそして configure.mount ワイヤ上で共同作業を行います。水平方向の画面レイアウトが固定されるため、操作の焦点は「configure」コマンドに集中したままになります。
学習内容:
- 設定 基底置換 (
"XZY"→ Y-upアプリケーションフレーム) - 選ぶ プリセット (
arm_front) およびそれが何を設定しているかを理解する - 実行時に回転クォータニオンを使用してマウントを上書きする(キーボード操作)
- 排他性ルール:
mountそしてpreset同じ場所には共存できない デバイスの設定 ブロック - 単発
configureセマンティクス:キーが押されるたびに、正確に1つの設定メッセージが送信される - (C++ Glaze) 排他的なフィールドのモデル化
std::optional
ワークフロー
- 最初のメッセージで: セッションプロファイル,
configure.basis: "XZY"そしてconfigure.preset: arm_front送信を開始set_cursor_force固定床用。 - 各ティックごとに:カーソルのY座標を読み取り、計算する
force_y = max(0, (floor_pos - y) * stiffness)、送信してください。 - ユーザーがマウント回転キーを押すと、フラグ
pending_configure = true. - 次のティックで:
configure.mount変換が適用されたブロックでrotationは単位クォータニオン(現在のピッチ/ヨーをZ→X順に合成したもの)です。省略preset— これら2つは、回線上で互いに排他的である。 - リセットキー (
R) オーバーライドを解除します。次に configure を実行すると、デフォルト設定に戻ります。presetまた。
パラメータ
| 名称 | デフォルト | 目的 |
|---|---|---|
BASIS | "XZY" | 軸の回転— Y軸上向きの適用フレーム |
DEVICE_PRESET / DEVICE_CONFIG_PRESET | "arm_front" | 名前付きプリセット— 原点はデバイスの基部 |
FLOOR_POS_Y | 0.0 m | 固定床面(アプリケーションY) |
STIFFNESS | 1000 単位なし | 床のばね定数 |
MOUNT_STEP_DEG | 10° | キーを押すごとに回転 |
PRINT_EVERY_MS | 200 | テレメトリースロットル |
操作方法
| キー | アクション |
|---|---|
W / S | デバイス軸 +X を中心にマウントを±10°回転させる(ピッチ) |
A / D | デバイス軸 +Z を中心にマウントを±10°回転させる(ヨー) |
R | マウントをリセット — プリセットに戻す |
H | コントロールを表示 |
Q | 終了 |
mount そして preset 互いに排他的であるこのサービスは デバイスの設定 両方を含むブロック。ユーザーがマウントを上書きすると、チュートリアルでは preset 以降のすべての configure 実行において。を押すと R 再度有効にする preset 次回の設定と削除について mount.
C++のバリアントは、バックグラウンドのstdinスレッドで1行ごとの入力を読み込みます(各文字の入力後にEnterキーを押してください)。Pythonは keyboard メインの非同期ループ内でリアルタイムにキー入力をポーリングするパッケージ — ENTERキーを押す必要はありません。対応するキーとコマンドは従来と同じです。
状態フィールドを読み取る
差出人 data.inverse3[i].state:
cursor_position.y—vec3、床への貫通を計算するために使用されるcurrent_cursor_force— テレメトリ用に報告された
送信/受信
ペイロードの形状は各バリエーションで共通ですが、興味深い違いは、それぞれが互いに排他的な mount / preset 分岐処理と、入力スレッドがWebSocketスレッドにシグナルを送信する方法。
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループと、 keyboard パッケージ。 pending_configure は、キーハンドラによって設定され、 configure ブロックが送信されます。
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["inverse3"][0]["device_id"]
# Handshake: profile + basis + preset
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": device_id,
"configure": build_configure_block(first_handshake=True),
# -> {"basis": {"permutation": "XZY"},
# "preset": {"preset": "arm_front"}}
}],
}
else:
handle_key_inputs() # may set pending_configure = True (classic, not shown)
y = data["inverse3"][0]["state"]["cursor_position"]["y"]
force_y = 0.0 if y > FLOOR_POS_Y else (FLOOR_POS_Y - y) * STIFFNESS
entry = {
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": force_y, "z": 0.0}}},
}
if pending_configure:
entry["configure"] = build_configure_block(first_handshake=False)
# -> {"mount": {...}} OR {"preset": {...}} (never both)
pending_configure = False
request_msg = {"inverse3": [entry]}
await websocket.send(json.dumps(request_msg))
2スレッドモデル:バックグラウンドのstdinスレッドが行を読み込み、反転させる pending_configure (ある std::atomic<bool>); libhvのI/Oスレッドは毎ティックこれをチェックし、 configure 設定された場合。
std::atomic<bool> pending_configure{false};
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (!data.contains("inverse3") || data["inverse3"].empty()) return;
const bool do_handshake = first_message;
if (first_message) first_message = false;
const bool do_configure = do_handshake || pending_configure.exchange(false);
json request = {};
if (do_handshake) {
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:basis-and-mount"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
json dev_cmd = {{"device_id", el.value()["device_id"]}};
if (do_configure) {
json cfg = {};
if (do_handshake) cfg["basis"] = {{"permutation", BASIS}};
if (mount_overridden) {
cfg["mount"] = {{"transform", {
{"position", {{"x", 0.0}, {"y", 0.0}, {"z", 0.0}}},
{"rotation", quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)},
{"scale", {{"x", 1.0}, {"y", 1.0}, {"z", 1.0}}},
}}};
} else {
cfg["preset"] = {{"preset", DEVICE_CONFIG_PRESET}};
}
dev_cmd["configure"] = cfg;
}
const float y = el.value()["state"]["cursor_position"]["y"].get<float>();
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dev_cmd["commands"] = {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", force_y}, {"z", 0.0}}}}}};
request["inverse3"].push_back(dev_cmd);
}
ws.send(request.dump());
};
std::thread input_thr(input_thread_func); // stdin reader — flips pending_configure
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
相互排他性の法則は、自然に次のように対応する std::optional<preset_cfg> そして std::optional<mount_cfg>: 値が設定されているものだけがシリアライズされたJSONに表示されます。プリセット名は enum class ~で glz::meta サービスが期待する文字列に変換する変換ルール。
// The preset set modelled as an enum
enum class device_preset {
defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom,
};
// Glaze meta — serialize the enum as the JSON string the service expects
template <> struct glz::meta<device_preset> {
using enum device_preset;
static constexpr auto value =
enumerate(defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom);
};
// Transform + the configure block
struct vec3 { float x{}, y{}, z{}; };
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct transform_t { vec3 position{}; quat rotation{}; vec3 scale{1,1,1}; };
struct preset_cfg { device_preset preset; };
struct basis_cfg { std::string permutation; };
struct mount_cfg { transform_t transform; };
struct device_configure {
std::optional<preset_cfg> preset; // mutually exclusive with mount
std::optional<basis_cfg> basis;
std::optional<mount_cfg> mount; // mutually exclusive with preset
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (data.inverse3.empty()) return;
const bool do_handshake = first_message.exchange(false);
const bool do_configure = do_handshake || pending_configure.exchange(false);
commands_message out_cmds{};
if (do_handshake) {
out_cmds.session = session_cmd{ /* profile = basis-and-mount */ };
}
for (const auto &dev : data.inverse3) {
device_commands dc{ .device_id = dev.device_id };
if (do_configure) {
device_configure cfg{};
if (do_handshake) cfg.basis = basis_cfg{BASIS};
if (mount_overridden) {
cfg.mount = mount_cfg{ .transform = transform_t{
.rotation = quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)}};
} else {
cfg.preset = preset_cfg{DEVICE_CONFIG_PRESET};
}
dc.configure = std::move(cfg);
}
const float y = dev.state.cursor_position.y;
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, force_y, 0.0f}};
out_cmds.inverse3.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
std::thread input_thr(input_thread_func);
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
関連: 基底の置換 ・ マウントとワークスペース ・ デバイスの設定 ・ 制御コマンド (set_cursor_force) ・ 型(変換) ・ チュートリアル 04(Hello Floor)