04. 你好,地板
这是第一个触觉效果。当光标被按下时,会生成一个会产生反弹力的虚拟水平地面。该力由一个简单的惩罚弹簧(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)在每个 tick 中也轮询键盘的箭头键,
floor_pos和stiffness将实时更新。
参数
| 姓名 | 默认 | 用途 |
|---|---|---|
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— 恢复默认设置
在服务端,力会进行累加。在将力发送至设备之前,来自多个来源的力会被相加。此类教程可以与其他力生成源共存,且互不干扰。
读取状态字段
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(基座与安装)