07. ベース&マウント・プレイグラウンド
インタラクティブに制御します マウント変換 デバイスの…を示し、その方法を説明しています。 configure.basis, configure.preset und configure.mount プロトコルレベルで連携する。configure コマンドへのインタラクティブな焦点を維持するため、固定された水平ベースが維持される。
学習内容:
- 設定 基底の順列 (
"XZY"→ Y軸が上向きの適用範囲) - の選択 プリセット (
arm_front) および、それが何を設定しているのかを理解する - 実行時に回転クォータニオンを用いてマウントを上書きする(キーボード操作による)
- 相互排他性の法則:
mountundpreset同じブロックの デバイスの設定 発生 - 一回限りの
configure-セマンティクス:キーが押されるたびに、正確に1つのconfigureメッセージが送信される - (C++ Glaze) 相互に排他的なフィールドのモデリング
std::optional
手順
- 最初のメッセージでは、以下を送信してください: セッション概要,
configure.basis: "XZY"undconfigure.preset: arm_front. 固い地面のために始めましょうset_cursor_force送信する。 - 各ティックごとに:Cursor-Yを読み取り、
force_y = max(0, (floor_pos - y) * stiffness)計算して送信する。 - ユーザーがマウントローテーションボタンを押した場合は、
pending_configure = true. - 次のティックで、次のものを作成してください。
configure.mount-変換を含むブロックで、そのrotationこれは単位クォータニオンです(現在のピッチ値とヨー値のZ→X順の合成)。では、presetweg – これら2つはプロトコルレベルにおいて互いに排他的である。 - リセットボタン(
R) 上書きを解除します。次の configure 命令では、再びpreset戻る。
パラメータ
| 名称 | 標準 | Zweck |
|---|---|---|
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 und preset 互いに排他的である同サービスは、以下のブロックを拒否しています。 デバイスの設定 ab(両方を含む)。ユーザーがマウントを上書きすると、チュートリアルは preset 以降のすべての configure ステートメントから削除されます。を押すと R wird preset 次の configure コマンドで再び有効化され、 mount 却下された。
C++ バージョンでは、バックグラウンドの stdin スレッドで行単位の入力を受け付けます(各文字の入力後に Enter キーを押してください)。Python では、パッケージ keyboard 非同期のメインループにおけるリアルタイムのキー入力検出用――ENTERキーは不要です。同じキー、同じコマンドです。
読み込まれた状態フィールド
Aus 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 (ein std::atomic<bool>) を true に設定する。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> und std::optional<mount_cfg> abbilden:JSONのシリアライズされた出力には、これら2つの値のうち、その時点で設定されているもののみが表示されます。プリセット名は 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);
チュートリアル 07 も SDK とともにローカルにインストールされています。以下の場所にあります。 tutorials/07-haply-inverse-basis-and-mount/ サービスのインストールディレクトリ内。
関連項目: 基底の順列 ・ マウントとワークスペース ・ デバイスの設定 ・ ステューアコマンド (set_cursor_force) ・ タイプ(変換) ・ チュートリアル 04(Hello Floor)