06. Combiné(Inverse3 + 无线 VerseGrip)
双设备教程:将手柄指向某个方向并按住按钮,即可将光标Inverse3 沿该方向移动。光标移动范围限定在球形工作空间内。
您将学到:
- 在同一状态帧中读取两种类型的设备(
inverse3etwireless_verse_grip) - 根据其声音确定手柄的指向方向 四元数 (axe local
+Y) - 使用
set_cursor_position将光标移动到计算出的目标位置 - 将目标限制在安全的工作空间范围内——Minverse 使用的半径小于Inverse3
- 定义一个 工作区预设 (
arm_front_centered) 以便原点位于五线谱的中央
流程
- 检测这两台设备:
- 变体 C++ interrogent简体中文(大陆)
GET /devices启动时通过 HTTP 连接,随后显示校准提示,并等待用户按下回车键。 - Python从 WebSocket 的第一个状态帧中读取了这两台设备的标识符。
- 变体 C++ interrogent简体中文(大陆)
- 保存 会话配置文件 并设置
configure.preset: arm_front_centered在第一条消息中(仅此一次的联系)。 - 每个周期,请阅读
orientation把手及状态buttons.{a, b}. - 如果按下了移动按钮,请计算手柄在世界空间中的方向(
R(q) · ĵ— 旋转后的+Y单位轴),将其乘以SPEED并将它添加到目标位置。 - 将目标限制在工作空间球体范围内,并通过
set_cursor_position. - (仅限 Python)根据……调整球体的半径
config.type设备 —minverse= 0.04 米,其余部分均为 0.10 米。
参数
| Nom | 默认值 | 角色 |
|---|---|---|
SPEED | 0.01 摩托车 | 按下按钮期间不移动 |
RADIUS_INVERSE3 | 0.10 m | Inverse3 /Inverse3x 工作空间的约束半径 |
RADIUS_MINVERSE | 0.04 m | Minverse 的工作区约束半径(仅限 Python — C++ 已修复) 0.10 (en dur) |
PRINT_EVERY_MS | 200 | 遥测数据速率的限制 |
| 会话配置文件名称 | co.haply.inverse.tutorials:combined | 在Haply Hub中识别此模拟 |
执行前请进行校准
- 让Inverse3 自动校准(或将笔杆放在墨水瓶上,等待LED指示灯变为常亮状态)。
- 将墨水瓶的把手取下。
- 按住 A 或 B 键不放,同时旋转手柄——光标将朝手柄指向的方向移动。
阅读过的州冠军
基于每个周期的状态帧:
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— 如果该值不正确,则提示用户进行校准
发送与接收
基于四元数的航向计算(旋转) +Y par R(q)) 以及球面约束属于经典线性代数范畴——请参阅源文件。就 Inverse API 而言,这涉及初始接触以及 set_cursor_position 每个周期。
- Python
- C++ (nlohmann)
- C++ (Glaze)
唯一的异步循环。Python 从第一个状态帧中读取两台设备的标识符;建立连接时,会附加配置文件并 configure.preset: arm_front_centered 第一场 set_cursor_position.
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 启动时(HTTP),然后打开 WebSocket。 onmessage 在 libhv 的 I/O 线程上运行;主线程则处于阻塞状态,等待输入。
// 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 相同的调用回调模型。类型化结构同时对状态帧和输出命令进行了建模——这两者 std::vector<> dans devices_message 使单个 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(位置控制)