05. 位置制御
Inverse3 ターゲット位置に向かって移動させる set_cursor_position. 言語によって相互作用モデルは異なる — C++では単発のランダムなターゲットを使用するのに対し、Pythonではキーボード操作による連続的な移動を使用します。
学習内容:
- 使用して
set_cursor_position位置制御用 - 同じ基本コマンドに対する2つの異なるインタラクションモデル
- ターゲットを作業領域の球体に固定する —Minverse Inverse3よりも半径を小さくMinverse
- ワークスペースのプリセットを設定し、原点をワークスペースの中心に配置する
ワークフロー
C++(ランダムターゲットモデル)
- 行単位でバッファリングされたキー入力を読み込むバックグラウンド入力スレッドを開始する(
n,+,-,q) を標準入力から読み込む。 - WebSocketを開きます。最初のステートフレームで、 セッションプロファイル そして設定する
configure.preset: arm_front_centered. 球体(半径 0.08 m)の内部で最初のランダムなターゲットを生成する(リジェクションサンプリング)。 - 1ティックごとに、
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) | 目的 |
|---|---|---|---|
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 | 同じ | Haply での特定 |
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++ バックグラウンドで標準入力スレッドを実行し、そこに書き込みを行う
std::atomic<float>ターゲット;WebSocketスレッドは、各ティックごとにそれらを読み取ります。n+ ENTER キーを押すと、入力スレッドが新しいランダムなターゲットを生成します;onq、両方のスレッドが終了しました。 - Python これはシングルスレッドの非同期処理であり、WebSocketループは各ティックでキーボードの状態をポーリングし、更新を行います
position直接。
Inverse-APIのペイロードは同じです。最初のティックにはセッションプロファイルが含まれ、 configure.preset、それ以降のティックには set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループ。キーボードのポーリング(handle_keys) は、スレッドを使用せずに、各ティックごとにインラインで実行されます。 config.type そして 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スレッドモデル:バックグラウンドスレッドが標準入力から読み込み、書き込みを行う 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> デバイスごとにワンショットプリセットを保持します。以降のティックでは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);
関連: 制御コマンド (set_cursor_position) ・ マウントとワークスペース(プリセット) ・ 型 (vec3) ・ チュートリアル 06(統合版)