05. 位置制御
Inverse3 からカーソルを移動させます set_cursor_position 目的の位置へ。 インタラクションモデルは、プログラミング言語によって異なります。 — C++ では一度きりのランダムなターゲット位置が使用されるのに対し、Python ではキーボード操作による連続的な移動が使用されます。
学習内容:
- の使用
set_cursor_position位置制御モードでの制御用 - 同じ基本コマンドに対する2つの異なるインタラクションモデル
- 目標を作業空間内の球体に限定すること — 『Minverse 』では、これよりも小さい半径が使用されている。Inverse3
- 作業空間のプリセットを設定し、原点を作業空間の中心に位置させる
手順
C++(ランダム目標モデル)
- 行単位でバッファリングされたキー入力を処理するバックグラウンド入力スレッドを起動します(
n,+,-,q) stdin から読み込む。 - WebSocket を開きます。最初のステータスフレームで、 会議概要 そして、次のように設定してください
configure.preset: arm_front_centered. 球体内から最初のランダムな目標点を生成します(リジェクション・サンプリング、半径 0.08 m)。 - ティックが発生するたびに、
set_cursor_position-現在のターゲットへのコマンド。カーソルはそれに均一に追従します。このサービスはレートを制限し、補間を行います。 - ユーザーが
n+ ENTER を入力すると、入力スレッドは新しいランダムなターゲットを生成します。+/-速度を調整してください;qプログラムを終了します。
Python(ボタンを押し続けると動くモデル)
- WebSocket を開きます。最初のステータスフレームで以下を確認してください。
status.calibrated— デバイスがまだ校正されていない場合は、ユーザーに校正を行うよう指示してください。 - こちらをご覧ください
config.type作業領域の半径を決定するために(minverse= 0.04 m、その他の値はすべて = 0.10 m)。 - これを登録してください 会議概要 そして、次のように設定してください
configure.preset: arm_front_centered. - 各ティックごとに:キーボードの状態を取得する (
W/A/S/D/Q/E), 目標位置をSPEED押されている各軸に沿って更新し、作業領域の球体に制限して、set_cursor_position送信。R目標を起点に戻します。
パラメータ
| 名称 | 標準値 (C++) | 標準値(Python) | Zweck |
|---|---|---|---|
workspace_radius / RADIUS_INVERSE3 | 0.08 m | 0.10 m (Inverse3) / 0.04 m (Minverse) | 標的球の半径 |
speed_step / SPEED | 0.01 / キーを押す | 0.00005 m / ティック | 1回の操作あたりのステップ数 |
PRINT_EVERY_MS | — | 100 | テレメトリの制限(Python) |
| 会議概要 | co.haply.inverse.tutorials:position-control | gleich | Haply Hubで特定されました |
Pythonのバリエーションでは、以下を確認します status.calibrated 最初のステートフレームで、デバイスがキャリブレーションされていない場合はユーザーにキャリブレーションを行うよう促します。C++版では、キャリブレーションがすでに完了していることを前提としています。
読み取り済みステータスフィールド
data.inverse3[0].device_id— コマンドの作成についてdata.inverse3[0].state.cursor_position— テレメトリー- (Python、最初のフレームのみ)
data.inverse3[0].config.type— 「Inverse3 」と「Minverse 」の半径のいずれかを選択します - (Python、最初のフレームのみ)
data.inverse3[0].status.calibrated— 値が false の場合、ユーザーに次のように促します
送信/受信
コミュニケーションの流れ
- C++ バックグラウンドのstdinスレッドを実行し、そのスレッドは
std::atomic<float>-Zieleが書き込みを行い、WebSocketスレッドは各ティックごとにこれを読み取ります。n+ ENTER を入力すると、入力スレッドが新しいランダムなターゲットを生成します。q両方のスレッドが終了します。 - Python これはシングルスレッドかつ非同期です。WebSocketのループは、各ティックごとにキーボードの状態を照会し、更新を行います。
positiondirekt。
Inverse-APIのペイロードデータは同一です。最初のティックにはセッションプロファイルが含まれています + configure.preset, 以下のティックには以下の内容のみが含まれています set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループ。キーボードのポーリング(handle_keys) は、各ティックごとにインラインで実行されます — スレッドを使用せずに。 config.type und status.calibrated は、最初の状態フレームから一度だけ読み込まれます。
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"]
radius = get_workspace_radius(data["inverse3"][0].get("config", {}))
# Handshake: profile + preset (one-shot)
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": device_id,
"configure": {"preset": {"preset": "arm_front_centered"}},
}],
}
else:
# Per tick: update position from keyboard (classic polling, not shown), send command
position = handle_keys(position, radius)
request_msg = {
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_position": {"position": position}},
}],
}
await websocket.send(json.dumps(request_msg))
2スレッドモデル:バックグラウンドスレッドがstdinから読み込み、書き込みを行う std::atomic<float>- 目的:libhvのI/Oスレッドは ws.onmessage 各ティックごとに実行され、アトミック値を読み取ります。
// Shared state written by the stdin thread, read by the ws thread
static std::atomic<float> target_x{0.0f}, target_y{0.0f}, target_z{0.0f};
ws.onmessage = [&](const std::string &message) {
const json data = json::parse(message);
if (data["inverse3"].empty()) return;
json request = {};
const bool do_handshake = first_message.exchange(false);
if (do_handshake) {
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:position-control"}}}}}};
generate_random_target();
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
json dev = {
{"device_id", el.value()["device_id"]},
{"commands", {{"set_cursor_position",
{{"position", {{"x", target_x.load()},
{"y", target_y.load()},
{"z", target_z.load()}}}}}}},
};
if (do_handshake)
dev["configure"] = {{"preset", {{"preset", "arm_front_centered"}}}};
request["inverse3"].push_back(dev);
}
ws.send(request.dump());
};
ws.open("ws://localhost:10001");
std::thread input_thr(input_thread_func); // stdin reader — writes target atomics
while (running) std::this_thread::sleep_for(100ms);
同じ2スレッドモデル。コマンド用の型付き構造体 — std::optional<device_configure> 各デバイスごとに1回限りの初期設定が含まれています。以降のティックでは、JSONからこの設定は省略されます。
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct set_cursor_position_cmd { vec3 position; };
struct preset_cfg { std::string preset; };
struct device_configure { std::optional<preset_cfg> preset; };
struct device_commands {
std::string device_id;
std::optional<device_configure> configure; // one-shot
struct commands_t {
std::optional<set_cursor_position_cmd> set_cursor_position;
} commands;
};
struct commands_message {
std::optional<session_cmd> session; // one-shot
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
commands_message request;
const bool do_handshake = first_message.exchange(false);
if (do_handshake) {
request.session = session_cmd{ /* profile = position-control */ };
}
for (const auto &dev : data.inverse3) {
device_commands dc{ .device_id = dev.device_id };
dc.commands.set_cursor_position = set_cursor_position_cmd{
.position = {target_x.load(), target_y.load(), target_z.load()}};
if (do_handshake)
dc.configure = device_configure{ .preset = preset_cfg{"arm_front_centered"} };
request.inverse3.push_back(std::move(dc));
}
std::string out;
(void)glz::write_json(request, out);
ws.send(out);
};
ws.open("ws://localhost:10001");
std::thread input_thr(input_thread_func);
while (running) std::this_thread::sleep_for(100ms);
チュートリアル 05 は、SDK とともにローカルにもインストールされます。以下の場所に保存されています。 tutorials/05-haply-inverse-position-control/ サービスのインストールディレクトリ内。
関連項目: ステューアコマンド (set_cursor_position) ・ モンタージュと作業スペース(デフォルト設定) ・ 型 (vec3) ・ チュートリアル 06(複合)