05. 位置控制
通过 set_cursor_position 将 Inverse3 光标驱动至目标位置。不同语言的交互模型有所不同——C++ 使用一次性随机目标,Python 使用连续的键盘驱动运动。
学习内容:
- 使用
set_cursor_position实现位置模式控制 - 同一底层命令对应的两种不同交互模型
- 将目标限制在工作空间球体内——Minverse 使用的半径比 Inverse3 更小
- 设置 工作空间预设,使原点位于工作空间中心
工作流程
C++(随机目标模型)
- 启动一个后台输入线程,从标准输入(stdin)读取按行缓冲的按键(
n、+、-、q)。 - 打开 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) | 用途 |
|---|---|---|---|
workspace_radius / RADIUS_INVERSE3 | 0.08 m | 0.10 m(Inverse3)/ 0.04 m(Minverse) | 目标球体半径 |
speed_step / SPEED | 0.01 / 每次按键 | 0.00005 m / 每个周期 | 每次交互的步长 |
PRINT_EVERY_MS | — | 100 | 遥测节流(Python) |
| 会话配置文件 | co.haply.inverse.tutorials:position-control | 相同 | 用于在 Haply Hub 中标识 |
校准检查(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++ 运行一个后台 stdin 线程,写入
std::atomic<float>类型的目标值;WebSocket 线程在每个周期读取这些值。输入n并按 ENTER 时,输入线程生成新的随机目标;输入q时,两个线程都会退出。 - 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))
双线程模型:后台线程读取 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);
相同的双线程模型。命令使用强类型结构体——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);
随 SDK 安装程序提供
教程 05 同样随 SDK 一起安装在本地——请在服务安装目录下的 tutorials/05-haply-inverse-position-control/ 中查找。
相关内容: 控制命令(set_cursor_position) · 挂载与工作空间(预设) · 类型(vec3) · 教程 06(组合)