05. 位置控制
set_cursor_position 通过该方法,将Inverse3 的游标移动到目标位置。交互模型因语言而异 — C++ 使用一次性随机目标,而 Python 则使用通过连续键盘操作实现的移动。
学习内容:
- 用于位置模式控制的
set_cursor_position的使用方法 - 针对同一基础命令的两种不同交互模型
- 将夹具夹紧在目标位置的工作空间球体上——Minverse 使用的半径小于Inverse3
- 设置工作区预设,使原点位于工作区中心
工作流
C++(随机目标模型)
- 来自标准输入的按行缓冲的键盘输入(
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进行设置。 - 在每个 tick 中,键盘的状态(
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++ 是,
std::atomic<float>运行一个后台标准输入线程,用于写入目标值。WebSocket 线程会在每个时间步长读取该值。n按下 ENTER 键后,输入线程将生成一个新的随机目标,q这样,两个线程都将结束。 - Python 这是一种单线程的异步处理。WebSocket 循环会在每个时间步长轮询键盘状态,
position直接更新。
Inverse-API 的有效载荷是相同的。在第一个时标中,会包含会话配置文件和 configure.preset 发送后,在随后的 tick 中 set_cursor_position 仅发送该内容。
- Python
- C++ (nlohmann)
- C++ (Glaze)
这是一个单一的异步循环。键盘轮询(handle_keys)在每个 tick 时以内联方式执行,不使用线程。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);
2 线程模型是相同的。命令中使用了带类型的结构体。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(综合)