07. 基礎と設置:試験場
インタラクティブに操作して 編集の変換 本機の機能について説明し、その方法を示しています configure.basis, configure.preset et configure.mount プロトコルレベルで連携して動作します。インタラクティブな注目点が設定コマンド上に留まるよう、水平方向の背景を固定します。
学習内容:
- 定義する 基本順列 (
"XZY"→ Y軸が上向きになるように配置された目印) - 選ぶ プリセット (
arm_front) そして、それがどのような設定を行うのかを理解する - 実行時のモンタージュを、(キーボード操作による)回転クォータニオンに置き換える
- 相互排他性の法則:
mountetpresetは、同じブロック内で共存することはできません。 デバイスの設定 - 『』の点在的意味論
configure: キーを押すたびに、設定メッセージが正確に1つ送信されます - (C++ Glaze) 相互排他的なフィールドのモデル化
std::optional
ワークフロー
- 最初のメッセージが届いたら、それを送信してください セッションプロファイル,
configure.basis: "XZY"etconfigure.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) は置換を元に戻します。以下の設定は、次と同じ効果になります。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 et preset 互いに排他的であるこのサービスは、あるブロックを拒否します。 デバイスの設定 その両方が含まれています。ユーザーがマウントを置き換えると、チュートリアルではその部分が省略されます preset 以下の各設定について。を押してください R 反応性 preset 以下の設定を行い、削除します mount.
C++ 版は、バックグラウンドの stdin スレッドから入力を行単位で読み込みます(各文字を入力するたびに 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 (un 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> et 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);
チュートリアル 07 も SDK とともにローカルにインストールされています。詳しくは tutorials/07-haply-inverse-basis-and-mount/ サービスのインストールディレクトリ内。
関連項目: 基本順列 ・ 組立と作業スペース ・ デバイスの設定 ・ 制御コマンド (set_cursor_force) ・ 型(変換) ・ チュートリアル 04(Hello Floor)