04. Hello Floor
你的第一个触觉效果:一个虚拟的水平地面,当光标压入其中时会产生反推力。该力是一个简单的惩罚弹簧模型——stiffness × penetration_depth——通过 set_cursor_force 沿 Z 轴施加。
你将学到:
- 使用
set_cursor_force施加惩罚弹簧力 - 读取
cursor_position并实时计算力 - (Python)设置 工作空间预设(
arm_front_centered),使原点位于工作空间中心 - (Python)通过
keyboard包以交互方式调节地面高度和刚度
工作流程
- 打开到
ws://localhost:10001的 WebSocket,并等待第一个状态帧。 - 在第一帧时:注册 会话配置文件。Python 版本还会发送
configure.preset: arm_front_centered,使原点位于工作空间的中部;C++ 版本则使用设备上当前已激活的配置。 - 每一帧:读取
cursor_position.z,计算force_z = max(0, (floor_pos - z) * stiffness),并通过set_cursor_force指令将其回传。 - 之后的每个周期仅发送力指令——会话配置文件只是一次性握手。
- (Python)每个周期还会轮询键盘方向键,并实时更新
floor_pos/stiffness。
参数
| 名称 | 默认值 | 用途 |
|---|---|---|
floor_pos | 0.10 m | 虚拟地面平面的 Z 坐标 |
stiffness | 1000 N/m | 弹簧常数(穿透 1 mm → 1 N) |
PRINT_EVERY_MS | 100–200 | 遥测节流 |
| 会话配置文件名称 | co.haply.inverse.tutorials:hello-floor | 在 Haply Hub 中标识此仿真 |
Python 版本使用 keyboard 包(在 Linux 上需要提升权限):
↑/↓——抬高 / 降低地面平面←/→——减小 / 增大刚度R——恢复默认值
在服务周期中,力是可叠加的——在发送到设备之前,会先汇总各来源的力。像本教程这样的示例可以与其他力生成源共存,彼此互不阻塞。
读取的状态字段
来自 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(基座与挂载)