07. 基座与安装:试验场地
以交互方式控制 剪辑转换 设备的,并展示了如何 configure.basis, configure.preset et configure.mount 在协议层面上协同工作。保持水平基面固定,以确保交互式焦点始终位于配置控件上。
您将学到:
- 定义一个 基本排列 (
"XZY"→ 应用基准点(Y轴向上)) - 选择一个 预设 (
arm_front) 并理解其配置内容 - 将运行时的拼接替换为旋转四元数(通过键盘控制)
- 互斥规则:
mountetpreset不能在同一个区块中并存 设备配置 - 《……的点状语义学》
configure: 每次按键都会发送恰好一条配置消息 - (C++ Glaze) 使用……对互斥字段进行建模
std::optional
工作流程
- 收到第一条消息时:发送该消息 会话配置文件,
configure.basis: "XZY"etconfigure.preset: arm_front. 开始发送set_cursor_force适用于固定地面。 - 每个循环中:读取光标的Y值,计算
force_y = max(0, (floor_pos - y) * stiffness)然后发送出去。 - 当用户按下蒙太奇旋转键时,请设置
pending_configure = true. - 在下一个循环中:构建一个块
configure.mount通过一种变换,其中rotation是一个单位四元数(由当前俯仰角和偏航角依次与Z、X相乘得到)。省略preset— 在协议层面,这两者是相互排斥的。 - 重置按钮(
R) 会清除替换操作;以下配置相当于preset.
参数
| Nom | 缺陷 | 角色 |
|---|---|---|
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 | 遥测数据速率的限制 |
命令
| 说得对 | 行动 |
|---|---|
W / S | 将安装件围绕设备+X轴旋转±10°(俯仰) |
A / D | 将安装件绕设备+Z轴旋转±10°(偏航) |
R | 重置剪辑 — 恢复为预设 |
H | 显示命令 |
Q | 逃兵 |
mount et preset 彼此互斥该服务拒绝了一个 设备配置 其中包含这两者。一旦用户替换了该组件,教程就省略了 preset 以下每种配置。按下 R 反应性 preset 在进行以下配置时,请移除 mount.
C++ 版本会通过后台的 stdin 线程逐行读取用户输入(输入每个字母后请按回车键)。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))
双线程执行模型:后台的一个 stdin 线程读取行并切换 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> et 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);
教程 07 也随 SDK 一起本地安装了——请参阅 tutorials/07-haply-inverse-basis-and-mount/ 在服务安装目录中。
另请参阅: 基本排列 · 剪辑与工作区 · 设备配置 · 控制命令(set_cursor_force) · 类型(转换) · 教程 04(Hello Floor)