07. Basis & Mount 游乐场
以交互方式驱动设备的 挂载转换 并演示了如何 configure.basis, configure.preset和 configure.mount 在终端中协同工作。保持固定的水平界面,以便交互重点始终集中在配置命令上。
您将学到:
- 设置一个 基底置换 (
"XZY"→ Y-up 应用框架) - 选择一个 预设 (
arm_front) 并了解其配置内容 - 在运行时使用旋转四元数覆盖坐骑(键盘控制)
- 互斥规则:
mount和preset无法共存于同一 设备配置 块 - 单篇
configure语义:每次按键都会发送且仅发送一条配置消息 - (C++ Glaze) 使用
std::optional
工作流程
- 在第一条消息中:发送 会话配置文件,
configure.basis: "XZY"和configure.preset: arm_front. 开始发送set_cursor_force用于固定地板。 - 每次 tick:读取光标 Y 坐标,计算
force_y = max(0, (floor_pos - y) * stiffness),发送。 - 当用户按下某个旋转键时,标记
pending_configure = true. - 在下一个时钟周期:构建一个
configure.mount具有以下变换的块rotation是一个单位四元数(当前俯仰角与偏航角的Z-X组合)。省略preset— 这两者在网络上是互斥的。 - 重置键 (
R) 清除覆盖设置;下次配置将回退到preset再次。
参数
| 名称 | 默认 | 目的 |
|---|---|---|
BASIS | "XZY" | 轴置换— Y-up 应用框架 |
DEVICE_PRESET / DEVICE_CONFIG_PRESET | "arm_front" | 命名预设— 原点位于设备底座 |
FLOOR_POS_Y | 0.0 m | 固定地板平面(应用-Y) |
STIFFNESS | 1000 不适用 | 地板弹簧刚度 |
MOUNT_STEP_DEG | 10° | 每次按键的旋转角度 |
PRINT_EVERY_MS | 200 | 遥测节流阀 |
控制
| 关键 | 行动 |
|---|---|
W / S | 沿设备+X轴旋转支架±10°(俯仰角) |
A / D | 将支架沿设备+Z轴旋转±10°(偏航) |
R | 重置挂载 — 恢复为预设 |
H | 显示控件 |
Q | 退出 |
mount 和 preset 互斥该服务拒绝了一个 设备配置 包含两者的代码块。一旦用户覆盖了挂载操作,本教程便省略了 preset 从后续的每次配置开始。按下 R 重新启用 preset 在接下来的配置和删除操作中 mount.
C++ 变体在后台的 stdin 线程上读取按行输入(每个字母输入后按 ENTER 键)。Python 使用 keyboard 用于在主异步循环中进行实时按键轮询的包——无需按下 ENTER 键。支持相同的按键和命令。
读取状态字段
来自 data.inverse3[i].state:
cursor_position.y—vec3,用于计算楼板穿透深度current_cursor_force— 用于遥测报告
发送/接收
各变体的有效载荷形状相同;有趣的区别在于它们各自如何构建互斥的 mount / preset 分支机制以及输入线程如何向 WebSocket 线程发送信号。
- Python
- C++ (nlohmann)
- C++ (Glaze)
通过...实现的单线程异步循环,并进行实时键位轮询 keyboard 包。 pending_configure 这是一个由键处理程序设置的全局标志,并在每次 configure 数据块已发送。
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"]
# Handshake: profile + basis + preset
request_msg = {
"session": {"configure": {"profile": {"name": SLUG}}},
"inverse3": [{
"device_id": device_id,
"configure": build_configure_block(first_handshake=True),
# -> {"basis": {"permutation": "XZY"},
# "preset": {"preset": "arm_front"}}
}],
}
else:
handle_key_inputs() # may set pending_configure = True (classic, not shown)
y = data["inverse3"][0]["state"]["cursor_position"]["y"]
force_y = 0.0 if y > FLOOR_POS_Y else (FLOOR_POS_Y - y) * STIFFNESS
entry = {
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": force_y, "z": 0.0}}},
}
if pending_configure:
entry["configure"] = build_configure_block(first_handshake=False)
# -> {"mount": {...}} OR {"preset": {...}} (never both)
pending_configure = False
request_msg = {"inverse3": [entry]}
await websocket.send(json.dumps(request_msg))
双线程模型:一个后台标准输入线程读取行并翻转 pending_configure (一个 std::atomic<bool>); libhv 的 I/O 线程会在每个时钟周期检查该值并发出 configure 设置后。
std::atomic<bool> pending_configure{false};
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (!data.contains("inverse3") || data["inverse3"].empty()) return;
const bool do_handshake = first_message;
if (first_message) first_message = false;
const bool do_configure = do_handshake || pending_configure.exchange(false);
json request = {};
if (do_handshake) {
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:basis-and-mount"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
json dev_cmd = {{"device_id", el.value()["device_id"]}};
if (do_configure) {
json cfg = {};
if (do_handshake) cfg["basis"] = {{"permutation", BASIS}};
if (mount_overridden) {
cfg["mount"] = {{"transform", {
{"position", {{"x", 0.0}, {"y", 0.0}, {"z", 0.0}}},
{"rotation", quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)},
{"scale", {{"x", 1.0}, {"y", 1.0}, {"z", 1.0}}},
}}};
} else {
cfg["preset"] = {{"preset", DEVICE_CONFIG_PRESET}};
}
dev_cmd["configure"] = cfg;
}
const float y = el.value()["state"]["cursor_position"]["y"].get<float>();
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dev_cmd["commands"] = {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", force_y}, {"z", 0.0}}}}}};
request["inverse3"].push_back(dev_cmd);
}
ws.send(request.dump());
};
std::thread input_thr(input_thread_func); // stdin reader — flips pending_configure
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
互斥规则自然对应于 std::optional<preset_cfg> 和 std::optional<mount_cfg>:只有已填充的那个会出现在序列化的 JSON 中。预设名称被建模为一个 enum class 用一个 glz::meta 一种转换机制,用于将其映射为服务所期望的字符串。
// The preset set modelled as an enum
enum class device_preset {
defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom,
};
// Glaze meta — serialize the enum as the JSON string the service expects
template <> struct glz::meta<device_preset> {
using enum device_preset;
static constexpr auto value =
enumerate(defaults, arm_front, arm_front_centered,
led_front, led_front_centered, custom);
};
// Transform + the configure block
struct vec3 { float x{}, y{}, z{}; };
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct transform_t { vec3 position{}; quat rotation{}; vec3 scale{1,1,1}; };
struct preset_cfg { device_preset preset; };
struct basis_cfg { std::string permutation; };
struct mount_cfg { transform_t transform; };
struct device_configure {
std::optional<preset_cfg> preset; // mutually exclusive with mount
std::optional<basis_cfg> basis;
std::optional<mount_cfg> mount; // mutually exclusive with preset
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (data.inverse3.empty()) return;
const bool do_handshake = first_message.exchange(false);
const bool do_configure = do_handshake || pending_configure.exchange(false);
commands_message out_cmds{};
if (do_handshake) {
out_cmds.session = session_cmd{ /* profile = basis-and-mount */ };
}
for (const auto &dev : data.inverse3) {
device_commands dc{ .device_id = dev.device_id };
if (do_configure) {
device_configure cfg{};
if (do_handshake) cfg.basis = basis_cfg{BASIS};
if (mount_overridden) {
cfg.mount = mount_cfg{ .transform = transform_t{
.rotation = quat_from_xz_deg(mount_angle_x_deg, mount_angle_z_deg)}};
} else {
cfg.preset = preset_cfg{DEVICE_CONFIG_PRESET};
}
dc.configure = std::move(cfg);
}
const float y = dev.state.cursor_position.y;
const float force_y = y > FLOOR_POS_Y ? 0.0f : (FLOOR_POS_Y - y) * STIFFNESS;
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, force_y, 0.0f}};
out_cmds.inverse3.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
std::thread input_thr(input_thread_func);
ws.open("ws://localhost:10001");
while (running.load()) std::this_thread::sleep_for(50ms);
相关: 基底置换 · 安装与工作区 · 设备配置 · 控制命令 (set_cursor_force) · 类型(转换) · 教程 04(Hello Floor)