Haply Inverse SDK
Haply Service SDK是一项在后台运行的服务,用于Haply 设备和计算机。语言无关的 SDK 为Haply 硬件,包括Inverse3 、 Inverse3X 、 Minverse和Versegrip 。
其功能包括:
- 设备发现和管理:列出并配置连接的Haply 通过HTTP REST API 自动连接设备。
- 实时状态流:以高频率提供设备状态更新,实现精确控制和WebSockets通信。
- 命令处理:以高保真度执行力或位置命令,增强触觉反馈。
- 后台运行:在后台运行,无需用户干预即可保持设备就绪状态。
安装
从develop.haply.co下载最新的反向安装程序,运行并按照安装说明进行操作。
一旦安装,逆向服务应该自动在后台运行。
温馨提示
使用Haply 设备管理器可轻松管理服务并查看连接的设备。
使用示例
以下 Python 代码片段演示了如何通过WebSockets接口与Inverse Service交互以从Inverse3和Wireless Verse Grip设备读取数据。
代码读取设备的位置、速度、按钮和方向,并向Inverse3设备发送零力命令。
import asyncio # To run async loops
import websockets # Required for device communication
import orjson # JSON reader for fast processing
# Main asynchronous loop
async def main():
uri = 'ws://localhost:10001' # WebSocket port for Inverse Service 3.1 json format
first_message = True
inverse3_device_id = None
force = {"x": 0, "y": 0, "z": 0} # Forces to send to the Inverse3 device.
# Haptic loop
async with websockets.connect(uri) as ws:
while True:
# Receive data from the device
response = await ws.recv()
data = orjson.loads(response)
# Get devices list from the data
inverse3_devices = data.get("inverse3", [])
verse_grip_devices = data.get("wireless_verse_grip", [])
# Get the first device from the list
inverse3_data = inverse3_devices[0] if inverse3_devices else {}
verse_grip_data = verse_grip_devices[0] if verse_grip_devices else {}
# Handle the first message to get device IDs and extra information
if first_message:
first_message = False
if not inverse3_data:
print("No Inverse3 device found.")
break
if not verse_grip_data:
print("No Wireless Verse Grip device found.")
# Store device ID for sending forces
inverse3_device_id = inverse3_data.get("device_id")
# Get handedness from Inverse3 device config data (only available in the first message)
handedness = inverse3_devices[0].get("config", {}).get("handedness")
print(f"Inverse3 device ID: {inverse3_device_id}, Handedness: {handedness}")
if verse_grip_data:
print(f"Wireless Verse Grip device ID: {verse_grip_data.get("device_id")}")
# Extract position, velocity from Inverse3 device state
position = inverse3_data["state"].get("cursor_position", {})
velocity = inverse3_data["state"].get("cursor_velocity", {})
# Extract buttons and orientation from Wireless Verse Grip device state (or default if not found)
buttons = verse_grip_data.get("state", {}).get("buttons", {})
orientation = verse_grip_data.get("state", {}).get("orientation", {})
print(f"Position: {position} Velocity: {velocity} Orientation: {orientation} Buttons: {buttons}")
# Prepare the force command message to send
# Must send forces to receive state updates (even if forces are 0)
request_msg = {
"inverse3": [
{
"device_id": inverse3_device_id,
"commands": {
"set_cursor_force": {
"values": force
}
}
}
]
}
# Send the force command message to the server
await ws.send(orjson.dumps(request_msg))
# Run the asynchronous main function
if __name__ == "__main__":
asyncio.run(main())
警告
更改力值以谨慎地对Inverse3设备施加力。突然增加的力值可能会损坏设备或导致意外行为。
信息
有关 json 格式的更多信息请参阅Inverse Service websocket
其他示例
基本的 C++ 示例 位于安装目录中(%programfiles%\Haply\Inverse
).
资料来源:
tutorial
文件夹中,编译后的示例可在bin
文件夹。