04. 哈洛·博登
其首个触觉效果:一个虚拟的水平地面,当光标进入其中时,地面会产生反作用力。该力由一个简单的惩罚弹簧模型(stiffness × penetration_depth),沿Z轴方向通过 set_cursor_force 被应用。
您将学到:
- 使用
set_cursor_force,以应用惩罚弹簧力 - 从……中读取
cursor_position以及实时计算力 - (Python) 设置一个 工作区预设 (
arm_front_centered),从而使原点位于工作区域的中心 - (Python) 通过该包使用键盘对底盘高度和刚度进行交互式微调
keyboard
流程
- 建立与的 WebSocket 连接
ws://localhost:10001然后等待第一个状态帧。 - 在第一个帧中注册 会话概况. Python 版本还会额外发送
configure.preset: arm_front_centered,从而使原点位于工作区域的中心;C++ 版本则使用设备上已启用的配置。 - 在每个帧中,您会看到
cursor_position.zaus, 计算force_z = max(0, (floor_pos - z) * stiffness)并将结果作为命令发送set_cursor_force返回。 - 在接下来的几个时钟周期中,仅发送力量指令——会话配置文件仅进行一次握手。
- (Python) 此外,在每个时钟周期都会检测键盘上的方向键,并且
floor_pos/stiffness将实时更新。
参数
| 名称 | 标准 | Zweck |
|---|---|---|
floor_pos | 0.10 m | 虚拟地面平面的Z坐标 |
stiffness | 1000 不适用 | 弹簧常数(1 毫米压入深度 → 1 牛顿) |
PRINT_EVERY_MS | 100–200 | 遥测数据限流 |
| 会话配置文件名称 | co.haply.inverse.tutorials:hello-floor | 在Haply Hub中标记此模拟 |
Python 版本使用该包 keyboard (在 Linux 系统上需要提升权限):
↑/↓— 升降底板←/→— 降低/提高刚度R— 恢复为默认值
在“服务 tick”中,力是可累加的——它们会从所有来源汇总后,再发送至设备。像这样的教程可以与其他力源并存,且彼此不会相互干扰。
已读取的状态字段
Aus data.inverse3[i].state:
cursor_position.z—vec3, 用于计算穿透深度current_cursor_force— 将作为遥测数据上报
发送 / 接收
每次时钟脉冲:读取光标的Z值, force_z = max(0, (floor_pos - z) * stiffness) 计算并执行一条命令 set_cursor_force 发送。第一条发出的消息还包含会话配置文件(所有变体),而在 Python 中还额外包含 configure.preset: arm_front_centered.
- Python
- C++ (nlohmann)
- C++ (Glaze)
一个异步循环。第一个帧中的握手过程包含配置文件和 configure.preset,因此 floor_pos = 0.1 与工作区中心的坐标一致。
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 + preset (one-shot)
request_msg = {
"session": {"configure": {"profile": {"name": "co.haply.inverse.tutorials:hello-floor"}}},
"inverse3": [{
"device_id": device_id,
"configure": {"preset": {"preset": "arm_front_centered"}}
}]
}
else:
# Per tick: compute force along Z, send set_cursor_force
z = data["inverse3"][0]["state"]["cursor_position"]["z"]
force_z = 0.0 if z > floor_pos else (floor_pos - z) * stiffness
request_msg = {
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": 0.0, "z": force_z}}}
}]
}
await websocket.send(json.dumps(request_msg))
libhv-回调模型——onmessage 在 WebSocket 的 I/O 线程上运行;主线程将处于阻塞状态,直到按下 ENTER 键。C++ 版本采用最小握手(仅使用会话配置文件,不使用预设)。
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
// If no Inverse3 yet, ask the service to re-send the full state
if (!data.contains("inverse3") || data["inverse3"].empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
json request = {};
if (first_message) {
first_message = false;
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:hello-floor"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
const float z = el.value()["state"]["cursor_position"]["z"].get<float>();
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
request["inverse3"].push_back({
{"device_id", el.value()["device_id"]},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", 0.0}, {"z", force_z}}}}}}},
});
}
ws.send(request.dump());
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
相同的 libhv 回调模型——只是函数主体发生了变化。状态和命令都使用了类型化的结构体。 std::optional<session_cmd> 保留该唯一标识符——如果未设置,Glaze 在序列化 JSON 时会将其省略。
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state { vec3 cursor_position{}, current_cursor_force{}; };
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message { std::vector<inverse_device> inverse3; };
struct set_cursor_force_cmd { vec3 vector; };
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;
if (data.inverse3.empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
commands_message out_cmds{};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = hello-floor */ };
}
for (const auto &dev : data.inverse3) {
const float z = dev.state.cursor_position.z;
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
device_commands dc{ .device_id = dev.device_id };
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, 0.0f, force_z}};
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
此外,教程 04 会随 SDK 一起本地安装——可在以下位置找到: tutorials/04-haply-inverse-hello-floor/ 位于该服务的安装目录中。
相关主题: 控制命令 (set_cursor_force) · 剪辑与工作区(预设) · 类型 (vec3) · 会议 · 教程 07(基础与蒙太奇)