Haply Inverse SDK
Haply Inverse SDK 是Haply的触觉设备(Inverse3、Inverse3x、Minverse、VerseGrip以及Wireless VerseGrip)提供的一种语言无关的 WebSocket + HTTP 接口。它作为本地服务运行,负责处理设备检测、串行通信、安全性监控和状态流传输,因此应用程序只需通过套接字使用 JSON 进行通信即可。
主要功能如下:
- 设备检测与管理— 通过 HTTP REST API,自动列出并配置已连接的 Haply 设备。
- 实时状态流— 通过 WebSocket 以触觉控制速率(数 kHz)传输设备状态。
- 命令处理— 为了实现精确的触觉反馈,以高保真度执行力和位置命令。
- 后台运行— 作为本地服务运行,无需用户操作即可保持设备处于就绪状态。
Haply 在 Hub 上的安装
最简单的入门方法是,Haply 使用 Hub 。这是一款用于安装、运行、配置、测试和监控 Haply 设备的桌面应用程序。它能帮助您保持固件处于最新状态,内置 Inverse Service,并提供演示程序,让您在编写任何代码之前就能验证硬件。

Haply Hub
Haply 下载 Hub 的最新版本
请下载并安装 Hub,然后连接设备。Hub 将引导您进行固件更新。安装完成后,当 Hub 处于打开状态时,Inverse Service会自动在后台运行。
您也可以不使用 Hub,直接将 Inverse Service 的特定版本安装为系统服务(Windows)或守护进程(Linux/macOS)。有关安装程序的链接和操作步骤,请参阅“运行服务”。
快速示例
连接到服务,读取第一个 Inverse3 的光标位置,并发送一个强度为零的保持活动信号,以确保服务继续流式传输状态帧:
- Python
- JavaScript(Node)
- C++ (nlohmann)
- C++(优化)
- Rust
import asyncio, json, websockets
async def main():
# Connect to the Haply Inverse service WebSocket
async with websockets.connect("ws://localhost:10001") as ws:
# Read the first state frame to discover the device id
first_state = json.loads(await ws.recv())
device_id = first_state["inverse3"][0]["device_id"]
# Build a zero-force keepalive command targeting that device
keepalive = {"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force": {"vector": {"x": 0, "y": 0, "z": 0}}}
}]}
# Realtime loop: one send per tick, then read the resulting state
while True:
await ws.send(json.dumps(keepalive))
state = json.loads(await ws.recv())
pos = state["inverse3"][0]["state"]["cursor_position"]
print(f"pos: {pos}")
asyncio.run(main())
import WebSocket from 'ws'
// Connect to the Haply Inverse service WebSocket
const ws = new WebSocket('ws://localhost:10001')
let keepalive
ws.on('message', (msg) => {
const state = JSON.parse(msg)
if (!keepalive) {
// Read the first state frame to discover the device id
const deviceId = state.inverse3[0].device_id
// Build a zero-force keepalive command targeting that device
keepalive = JSON.stringify({
inverse3: [
{
device_id: deviceId,
commands: { set_cursor_force: { vector: { x: 0, y: 0, z: 0 } } },
},
],
})
ws.send(keepalive)
return
}
// Realtime loop: one send per tick, then read the resulting state
ws.send(keepalive)
console.log('pos:', state.inverse3[0].state.cursor_position)
})
#include <external/libhv.h>
#include <nlohmann/json.hpp>
int main() {
hv::WebSocketClient ws;
nlohmann::json keepalive;
ws.onmessage = [&](const std::string& msg) {
auto state = nlohmann::json::parse(msg);
if (keepalive.is_null()) {
// Read the first state frame to discover the device id
auto device_id = state["inverse3"][0]["device_id"];
// Build a zero-force keepalive command targeting that device
keepalive = {{"inverse3", nlohmann::json::array({{
{"device_id", device_id},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0}, {"y", 0}, {"z", 0}}}}}}}
}})}};
ws.send(keepalive.dump());
return;
}
// Realtime loop: one send per tick, then read the resulting state
ws.send(keepalive.dump());
auto pos = state["inverse3"][0]["state"]["cursor_position"];
std::cout << "pos: " << pos << "\n";
};
// Connect to the Haply Inverse service WebSocket
ws.open("ws://localhost:10001");
std::cin.get();
}
编译时反射使用Glaze。这是全球最快的 JSON 库之一。在触觉速率(数 kHz)下,解析 JSON 所耗费的 1 微秒,就意味着从控制循环中直接扣除 1 微秒,因此这一点至关重要。请声明仅需读写的最小形状。其余部分将被忽略。
#include <external/libhv.h>
#include <glaze/glaze.hpp>
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state { vec3 cursor_position; };
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 device_cmd { std::string device_id;
struct { std::optional<set_cursor_force_cmd> set_cursor_force; } commands; };
struct commands_message { std::vector<device_cmd> inverse3; };
int main() {
hv::WebSocketClient ws;
commands_message keepalive;
ws.onmessage = [&](const std::string& msg) {
devices_message state{};
if (glz::read_json(state, msg)) return;
if (keepalive.inverse3.empty()) {
// Read the first state frame to discover the device id
auto device_id = state.inverse3[0].device_id;
// Build a zero-force keepalive command targeting that device
keepalive.inverse3.push_back({device_id});
keepalive.inverse3[0].commands.set_cursor_force = set_cursor_force_cmd{};
std::string out; (void)glz::write_json(keepalive, out);
ws.send(out);
return;
}
// Realtime loop: one send per tick, then read the resulting state
std::string out; (void)glz::write_json(keepalive, out);
ws.send(out);
printf("pos: %f %f %f\n", state.inverse3[0].state.cursor_position.x,
state.inverse3[0].state.cursor_position.y,
state.inverse3[0].state.cursor_position.z);
};
// Connect to the Haply Inverse service WebSocket
ws.open("ws://localhost:10001");
std::cin.get();
}
tokio-tungstenite + serde_json 这是一个使用该功能的示例说明。
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use tokio_tungstenite::{connect_async, tungstenite::Message};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Connect to the Haply Inverse service WebSocket
let (mut ws, _) = connect_async("ws://localhost:10001").await?;
// Read the first state frame to discover the device id
let first = match ws.next().await {
Some(Ok(Message::Text(m))) => m,
_ => anyhow::bail!("no initial state frame"),
};
let first_state: serde_json::Value = serde_json::from_str(&first)?;
let device_id = first_state["inverse3"][0]["device_id"].as_str().unwrap();
// Build a zero-force keepalive command targeting that device
let keepalive = json!({
"inverse3": [{
"device_id": device_id,
"commands": { "set_cursor_force": { "vector": { "x": 0, "y": 0, "z": 0 } } }
}]
}).to_string();
// Realtime loop: one send per tick, then read the resulting state
loop {
ws.send(Message::Text(keepalive.clone())).await?;
let msg = match ws.next().await {
Some(Ok(Message::Text(m))) => m,
_ => break,
};
let state: serde_json::Value = serde_json::from_str(&msg)?;
println!("pos: {}", state["inverse3"][0]["state"]["cursor_position"]);
}
Ok(())
}
更改力值时,请务必小心。如果突然设置过大的力值,可能会导致设备损坏或引发意外操作。
有关消息的信封、端口和内容类型的规则,请参阅JSON 规范页面。
其他示例
有关使用 Python、C++(nlohmann)和 C++(Glaze)编写、涵盖力反馈、位置控制、多设备配置、mount/basis 设置以及事件流处理的高功能教程,请参阅教程页面。