05. 位置控制
通过以下方式控制Inverse3 的光标: set_cursor_position 到目标位置。 交互模型因编程语言的不同而有所差异 — C++ 使用一次性随机目标位置,而 Python 采用连续的、由键盘控制的移动方式。
您将学到:
- 使用
set_cursor_position用于位置模式下的控制 - 针对同一底层命令的两种不同交互模型
- 将目标限制在工作空间球体内——Minverse 使用的半径比Inverse3
- 设置工作空间预设,使原点位于工作空间中心
流程
C++(随机目标模型)
- 启动一个后台输入线程,用于处理按行缓冲的键盘输入(
n,+,-,q) 从标准输入(stdin)读取。 - 打开 WebSocket。在收到第一个状态帧时,注册 会议简介 并设置
configure.preset: arm_front_centered. 在一个球体内部生成第一个随机目标(拒绝采样,半径 0.08 米)。 - 每次 tick 时发送一个
set_cursor_position-向当前目标发送指令。光标会平稳地跟随它——该服务会限制速率并进行插值。 - 如果用户
n+ 按下 ENTER 键时,输入线程会生成一个新的随机目标。通过+/-请调整速度;q结束程序。
Python(长按即可移动的模型)
- 打开 WebSocket。在第一个状态帧时进行检查
status.calibrated— 如果设备尚未校准,请提示用户进行校准。 - 请阅读
config.typeaus,用于确定工作空间的半径(minverse= 0.04 米,其余所有数值 = 0.10 米)。 - 请注册该 会议简介 并设置
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 / 勾选 | 每次交互的步长 |
PRINT_EVERY_MS | — | 100 | 遥测限速(Python) |
| 会议简介 | co.haply.inverse.tutorials:position-control | gleich | 在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>-Ziele 负责写入数据;WebSocket 线程在每次 tick 时读取这些数据。在n+ 按下 ENTER 键后,输入线程会生成一个新的随机目标;当q这两个线程都将被终止。 - Python 它是单线程且异步的——WebSocket 的循环会在每次 tick 时查询键盘状态并进行更新
positiondirekt.
Inverse-API 的有效数据完全相同:第一个数据点包含会话配置文件 + configure.preset,以下时间戳仅包含 set_cursor_position.
- Python
- C++ (nlohmann)
- C++ (Glaze)
单个异步循环。键盘检测(handle_keys) 在每个 tick 时都会内联执行——无需线程。 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))
双线程模型:一个后台线程读取标准输入并写入 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> 包含每台设备的唯一预设;在后续的Tick中,该预设将从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(综合)