06. 整合(Inverse3 + 无线VerseGrip)
2 这是设备教程。将手柄朝向某个方向并长按按钮,Inverse3 中的光标就会朝该方向移动。光标移动范围仅限于球形工作区内。
学习内容:
- 同一 state 帧内的两种设备(
inverse3和wireless_verse_grip)进行读取 - 四元数 从握把的方向(本地
+Y取出(轴) set_cursor_position使用……,将光标移至计算出的目标位置- 将目标限制在安全工作区球体内。Minverse 应使用小于Inverse3 的半径
- 工作区预设(
arm_front_centered)并设置为使原点位于到达范围的中心
工作流
- 检测两台设备:
- C++ 该版本在启动时通过 HTTP
GET /devices查询,显示校准提示,并等待按下 ENTER 键。 - Python会从第一个 WebSocket 状态帧中读取两台设备的 ID。
- C++ 该版本在启动时通过 HTTP
- 在第一条消息中 会话配置文件 进行注册,
configure.preset: arm_front_centered进行设置(单次握手)。 - 在每个刻度上,握把的
orientation和buttons.{a, b}读取其状态。 - 如果按下了移动按钮,则在握把的世界坐标系中的方向(
R(q) · ĵ、计算旋转后的单位(+Y轴),SPEED将按比例缩放后的值加到目标位置上。 - 将目标限制在工作区球体内,
set_cursor_position将通过该方式发送。 - (仅限 Python)设备的
config.type根据情况调整球的半径。minverse该情况为 0.04 米,其余情况为 0.10 米。
参数
| 姓名 | 默认 | 用途 |
|---|---|---|
SPEED | 0.01 m/tick | 按住按钮时的移动步数 |
RADIUS_INVERSE3 | 0.10 m | Inverse3 / Inverse3x 的工作区限制半径 |
RADIUS_MINVERSE | 0.04 m | Minverse 工作区限制半径(仅限 Python — C++ 则为 0.10 (固定) |
PRINT_EVERY_MS | 200 | 遥测数据发送频率限制 |
| 会话配置文件名称 | co.haply.inverse.tutorials:combined | Haply 在 Hub 中识别此模拟 |
在执行前进行校准
- Inverse3 进行自动校准(或将笔握置于墨水瓶上,等待 LED 灯亮起)。
- 将笔握从墨水瓶上取下。
- 按住 A 或 B 键的同时旋转手柄。光标将朝手柄所指的方向移动。
读取 state 字段
从每个 tick 的 state 帧中读取以下内容:
data.inverse3[0].state.cursor_position—vec3data.wireless_verse_grip[0].state.orientation—quaterniondata.wireless_verse_grip[0].state.buttons.{a, b, c}— 真值与假值- (Python,仅限第一个帧)
data.inverse3[0].config.type— 选择使用Inverse3 还是Minverse 中的半径 - (Python,仅限第一个帧)
data.inverse3[0].status.calibrated— 若为 false,则向用户显示提示信息
发送 / 接收
根据四元数求取方向的计算(R(q) で +Y (旋转)和球体夹紧是线性代数中的经典计算。详情请参阅源文件。在 Inverse API 方面,握手以及每个时钟周期中的 set_cursor_position 调用该函数。
- Python
- C++ (nlohmann)
- C++ (Glaze)
这是一个单个的异步循环。Python 会从第一个 state 帧中读取两个设备的 ID。在握手过程中,第一个 set_cursor_position 个人资料和 configure.preset: arm_front_centered 将授予……
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
inverse3_id = data["inverse3"][0]["device_id"]
grip_id = data["wireless_verse_grip"][0]["device_id"]
radius = get_workspace_radius(data["inverse3"][0].get("config", {}))
# Handshake: profile + preset + first position command
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": inverse3_id,
"configure": {"preset": {"preset": "arm_front_centered"}},
"commands": {"set_cursor_position": {"position": position}},
}],
}
else:
# Per tick: update position from grip pointing direction (classic math, not shown), send
request_msg = {
"inverse3": [{
"device_id": inverse3_id,
"commands": {"set_cursor_position": {"position": position}},
}],
}
await websocket.send(json.dumps(request_msg))
C++ 在启动时 GET /devices 通过此方式获取两台设备的 ID(通过 HTTP),随后建立 WebSocket 连接。onmessage 它在 libhv 的 I/O 线程上运行,主线程则因等待 ENTER 输入而被阻塞。
// Startup (synchronous):
const std::string inv3_device_id = get_first_device_id("inverse3");
const std::string grip_device_id = get_first_device_id("wireless_verse_grip");
// Per tick:
ws.onmessage = [&](const std::string &msg) {
json data = json::parse(msg);
// ... classic math (not shown): update local Inverse3State + WirelessVerseGripState,
// compute new position from grip orientation, clamp to sphere ...
json command;
command["inverse3"] = json::array();
command["inverse3"].push_back({
{"device_id", inv3_device_id},
{"commands", {{"set_cursor_position",
{{"position", {{"x", pos.x}, {"y", pos.y}, {"z", pos.z}}}}}}}
});
if (first_message) {
first_message = false;
command["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:combined"}}}}}};
command["inverse3"][0]["configure"] = {
{"preset", {{"preset", "arm_front_centered"}}}};
}
ws.send(command.dump());
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
libhv 的回调模型也是如此。它是一个带类型的结构体,同时表示 state 帧和待发送的命令。devices_message 其中的两个 std::vector<> 因此,单一的 glz::read 这样就可以获取这两种设备类型。
// Struct models
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct button_state { bool a{}, b{}, c{}; };
struct wvg_state { quat orientation{}; uint8_t hall{}; button_state buttons{}; };
struct wvg_device { std::string device_id; wvg_state state; };
struct inverse_state { vec3 cursor_position{}, cursor_velocity{}; };
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message {
std::vector<inverse_device> inverse3;
std::vector<wvg_device> wireless_verse_grip;
};
struct set_cursor_position_cmd { vec3 position; };
struct commands_message {
std::optional<session_cmd> session;
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
const auto &wvg = data.wireless_verse_grip[0].state;
cursor_pos = data.inverse3[0].state.cursor_position;
// ... classic math (not shown): if (wvg.buttons.a || wvg.buttons.b)
// move in pointing dir; clamp to sphere ...
commands_message out_cmds{};
device_commands dc{ .device_id = inv3_device_id };
dc.commands.set_cursor_position = set_cursor_position_cmd{cursor_pos};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = combined */ };
dc.configure = device_configure{ .preset = preset_cfg{"arm_front_centered"} };
}
out_cmds.inverse3.push_back(std::move(dc));
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
包含在 SDK 安装程序中
教程 06 也已随 SDK 一起安装在本地。位于服务安装目录下的 tutorials/06-haply-inverse-combined/ 请参阅。
相关: 控制命令(set_cursor_position) · 类型(四元数, vec3) · 卡口与工作区(预设) · 教程 03(无线 VG) · 教程 05(位置控制)