05. 位置控制
通过以下方式将Inverse3 移动到目标位置 set_cursor_position. 交互模型因语言而异 — C++ 使用一次性随机目标,Python 使用基于键盘的连续移动。
您将学到:
- 使用
set_cursor_position用于位置模式控制 - 同一底层命令的两种不同交互模型
- 将目标固定到工作空间球体上——Minverse 半径比Inverse3更小
- 设置工作区预设,使原点位于工作区中心
工作流程
C++(随机目标模型)
- 启动一个后台输入线程,用于读取行缓冲的按键输入(
n,+,-,q) 从标准输入读取。 - 打开 WebSocket。在第一个状态帧中,注册 会话配置文件 并设置
configure.preset: arm_front_centered. 在一个球体内部生成第一个随机目标(拒绝采样,半径 0.08 米)。 - 每次 tick,发送一个
set_cursor_position命令发送到当前目标。光标会平滑地跟随它——该服务会进行速率限制和插值。 - 当用户输入
n+ 按下 ENTER 键,输入线程将生成一个新的随机目标。+/-调整速度;q退出。
Python(按住移动模式)
- 打开 WebSocket。在第一个状态帧中,检查
status.calibrated— 如果设备尚未校准,则提示用户。 - 阅读
config.type选择工作区半径(minverse= 0.04 米,其余情况 = 0.10 米)。 - 注册 会话配置文件 并设置
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 / tick | 每次交互的步骤 |
PRINT_EVERY_MS | — | 100 | 遥测节流阀(Python) |
| 会话配置文件 | co.haply.inverse.tutorials:position-control | 同上 | 在Haply 中识别 |
校准检查(Python)
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 键,输入线程会生成一个新的随机目标;在q,这两个帖子都被关闭了。 - Python 这是单线程异步操作——WebSocket 循环会在每个 tick 周期轮询键盘状态并进行更新
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))
双线程模型:一个后台线程读取标准输入并写入 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);
同样的双线程模型。命令采用类型化结构体—— 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(综合)