Haply Inverse SDK
Haply Inverse SDK 是一个与语言无关的WebSocket + HTTP接口,用于连接Haply触觉设备,包括 Inverse3、Inverse3x、Minverse、VerseGrip 和 Wireless VerseGrip。它以本地服务的形式运行,负责设备发现、串口通信、安全监控和状态流传输,因此您的应用只需通过套接字与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 库之一——进行编译期反射。在触觉刷新速率(数千赫兹)下,解析 JSON 所花的每一微秒都会挤占控制回路的时间,因此这一点很重要。请只声明你需要读写的最小结构,其余部分会被忽略。
#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(())
}
请谨慎调整力值。突然施加的高力值可能会损坏设备,或导致意外行为。
有关消息封装、端口和内容类型(content-type)规则,请参阅 JSON 约定 页面。
更多示例
如需查看功能完整的 Python、C++(nlohmann)和 C++(Glaze)教程(涵盖力反馈、位置控制、多设备配置、mount/basis 配置以及事件流),请参阅教程页面。