07. 基座与安装——游乐场
交互式控制 蒙特变换 该设备的功能,并演示如何 configure.basis, configure.preset und configure.mount 在协议层面上协同工作。将保持一个固定的水平底层,以便交互重点仍集中在 configure 命令上。
您将学到:
- 设置一个 基底置换 (
"XZY"→ 应用框架(Y轴朝上)) - 选择一个 预设 (
arm_front) 并理解其配置内容 - 在运行时使用旋转四元数覆盖挂载(通过键盘控制)
- 互斥性法则:
mountundpreset不能共同位于同一块的 设备配置 vorkommen - 一次性
configure-语义:每次按键都会发送且仅发送一条 configure 消息 - (C++ Glaze) 使用……对相互排斥的字段进行建模
std::optional
流程
- 对于第一条消息:请发送该 会话概况,
configure.basis: "XZY"undconfigure.preset: arm_front. 开始为坚实的地基做准备set_cursor_force发送。 - 每次时钟脉冲:读取光标Y值,
force_y = max(0, (floor_pos - y) * stiffness)计算并发送。 - 当用户按下挂载轮换按钮时,请设置
pending_configure = true. - 在下一次 tick 时,请创建一个
configure.mount-使用一个变换的块,其rotation是一个单位四元数(由当前俯仰角和偏航角按 Z-X 顺序组合而成)。请presetweg——这两者在协议层面上是相互排斥的。 - 重置按钮(
R) 取消覆盖;下一个 configure 指令将再次使用preset返回。
参数
| 名称 | 标准 | Zweck |
|---|---|---|
BASIS | "XZY" | 轴排列——Y轴朝上的应用框架 |
DEVICE_PRESET / DEVICE_CONFIG_PRESET | "arm_front" | 命名预设——源自设备底座 |
FLOOR_POS_Y | 0.0 m | 固定地面平面(应用-Y) |
STIFFNESS | 1000 不适用 | 土壤的弹性常数 |
MOUNT_STEP_DEG | 10° | 按键旋转 |
PRINT_EVERY_MS | 200 | 遥测数据限流 |
控制
| 口味 | Aktion |
|---|---|
W / S | 沿设备+X轴旋转±10微米(俯仰角) |
A / D | 沿设备的+Z轴旋转±10°(偏航) |
R | 重置音色——返回预设 |
H | 显示控制面板 |
Q | 结束 |
mount und preset 相互排斥该服务拒绝封锁 设备配置 ab,其中包含这两者。一旦用户覆盖了挂载点,本教程就会 preset 在后续的每个 configure 语句中都会被省略。按下 R 将 preset 在接下来的 configure 指令中重新启用,并 mount 被驳回。
C++ 版本会在后台的 stdin 线程中读取基于行的输入(输入每个字母后按 ENTER 键)。Python 则使用该包 keyboard 用于在异步主循环中进行实时按键检测——无需按 ENTER 键。相同的按键,相同的命令。
已读取的状态字段
Aus 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 (ein std::atomic<bool>) 设为 true;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> und std::optional<mount_cfg> abbilden:在 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);
教程 07 也已随 SDK 一起安装在本地——可在以下位置找到: tutorials/07-haply-inverse-basis-and-mount/ 位于该服务的安装目录中。
相关: 基底置换 · 安装与工作区 · 设备配置 · 控制命令 (set_cursor_force) · 类型(转换) · 教程 04(Hello Floor)