跳至主要内容

模拟通道

概述​

模拟通道是一个高频、双向的 WebSocket 通道,用于交换设备状态,并发送会话或设备命令。

默认网址: ws://localhost:10001

端口可在配置中进行修改。

核心约定(重要):

  • 当客户端连接时,服务器会发送一条初始清单消息,其中包含完整的设备列表。
  • 随后,对于客户端发送的每一条消息,服务器都会恰好返回**一条****状态更新(State Update)**消息。
  • 每条状态更新都包含所有设备的状态(及运行状态)。
模块

本页介绍了核心的Simulation Channel 协议及命令。

其他模块/功能可以注册并扩展系统,添加各自的命令和/或状态字段。这些内容在“模块”部分中单独记录。

协议规则​

每条客户端消息对应一次响应​

对于客户端发送的每一条消息,服务器都会发送一条状态更新消息,其中包含所有设备的状态。

这意味着:

  • 如果需要获取最新的设备状态快照,必须发送某些内容(会话命令、探测命令或设备命令)。
  • 该通道的行为类似于由客户端消息驱动的“tick”循环。

不施加力的轮询(探测命令)​

如果希望在不施加力或修改仿真参数的情况下观察状态变化,请使用探测命令:

  • inverse3 使用 probe_position
  • Verse 抓手的使用 probe_orientation

探测命令不包含命令数据,仅强制执行设备信息查询,从而确保下一条状态更新中的位置/姿态数据是最新的。

你可能不需要使用探测命令

正在发送控制命令(set_cursor_force、set_cursor_position 等)的仿真会话无需再次发送探测命令,因为响应帧中始终包含设备状态。探测命令适用于仅监控的会话(例如Haply Hub),此类会话需要在不驱动设备的情况下轮询设备状态。

配置与状态​

  • 初始清单消息包含设备的 config、state 和 status。
  • 常规状态更新消息包含设备的 state 和 status。

如果需要再次获取包含配置的完整快照,请使用 session.force_render_full_state。

configure 与 commands​

设备消息支持两种独立的映射:configure 和 commands。它们的用途不同:

映射用途发送频率
configure一次性持久配置:预设、基准坐标系、安装方式、滤波器、模块设置发送一次或在发生变更时发送
commands每个时钟周期生效、非持久化:力、位置、扭矩必须在每个 tick 时发送

commands 中的条目(set_cursor_force、set_cursor_position、set_angular_torques、set_angular_position)按每个 tick 应用一次,且不会保存 —— 如果停止发送,效果将立即消失,设备将恢复到空闲状态。对于需要在多个 tick 周期内持续生效且无需重复发送的内容,请使用 configure。

备注
set_transform 属于特例

set_transform 位于 commands 下,但它是持久的——服务器会保留你最后发送的转换,直到你发送新的转换为止。你并不需要每个 tick 都会发送该数据。尽管如此,由于其用途是相机/场景导航,因此在持续运动导航中,以 tick 频率持续推送变换(例如,当用户平移场景时,每帧发送一次变换)是预期用法。

坐标系​

Haply 默认使用右手坐标系,Z 轴朝上。

有两个配置项会影响坐标的解释和返回方式:

  • session.configure.basis:更改Haply 坐标与应用程序坐标系之间的映射关系(参见下方的 基础)。
  • inverse3[*].configure.preset:选择一个命名的出厂配置,用于设定工作空间原点的位置(例如 arm_front_centered 将 (0, 0, 0) (置于工作区中心)。请参阅各设备的“配置”部分。

消息格式​

本节介绍高层的消息封装与消息类型。完整的示例请参见本文档后文。

设备分组​

初始清单(服务器端 → 客户端)​

消息在顶层按设备类型分组(例如 inverse3、verse_grip、wireless_verse_grip)。每个设备类型键对应一个由单个设备对象组成的数组。

在 WebSocket 连接建立后立即发送一次。

每个条目包含:

  • device_id
  • config
  • state
  • status

参见:示例:初始清单加载

状态更新(服务器端 → 客户端)​

每条客户端消息发送一次。

每个条目包含:

  • device_id
  • state
  • status

参见:示例:状态更新负载

会话命令封装(客户端 → 服务器端)​

会话命令是针对当前连接/会话的操作,不针对特定设备。

{
"session": {
"<command_name>": {
"...": "..."
}
}
}

设备命令封套(客户端 → 服务器)​

设备命令以设备类型键的形式发送,其值为数组,因此可以在一条消息中同时向一个或多个设备发送命令。每个设备条目均支持 configure 映射和 commands 映射。

{
"<device_type>": [
{
"device_id": "<id>",
"configure": {
"<config_key>": { "...": "..." }
},
"commands": {
"<command_name>": { "...": "..." }
}
}
]
}

您可以在一条消息中添加多个条目,以便同时控制同一类型的多台设备。

请注意,commands 这是一个字典,可以为某个设备包含多条命令,但每种命令类型只能有一条。

execute 字段​

每个 command 或 configure 条目都接受一个可选的布尔值 "execute"(默认值为 true)。设置 "execute": false 可使该条目仅被解析而不被应用。这在基于反射的序列化器(例如 Unity JsonUtility)中非常有用,因为这类序列化器总会输出每个字段。

"set_cursor_force": { "execute": false, "vector": { "x": 0.0, "y": 0.0, "z": 0.0 } }

命令参考​

会话​

强制渲染完整状态​

请求获取所有设备状态和配置的快照。

{
"session": {
"force_render_full_state": {}
}
}

会话配置​

会话级配置持久化通过 session.configure 发送。这与设备级 configure 映射模式一致。

个人简介​

用于设置会话的配置文件名称,Haply Hub 借此识别模拟环境并保存每个应用的设备个性化设置。推荐格式:<app id>:<X.Y>[::<sub>],参见 个人资料名称格式。

{
"session": {
"configure": {
"profile": {
"name": "co.mycompany.myapp:1.4::main-scene"
}
}
}
}
基础(会话层)​

一种快捷方式,可在消息到达时将相同的基础写入会话中的所有设备,包括Inverse3 和Verse Grips。基础本身是设备级设置:有关替换格式,请参阅“基础(设备级)”。

{
"session": {
"configure": {
"basis": { "permutation": "X-YZ" }
}
}
}
快捷方式,并非对后续设备生效的设置

session.configure.basis 不会保存在会话中。在您建立连接后才加入的设备(连接后开启或唤醒的 Verse Grip、会话进行中插入的设备)不会应用该设置,而是以恒等基 XYZ 启动:当该设备出现时,请通过 device-connected 事件,单独发送 configure.basis。此快捷方式还会覆盖之前为每台设备发送的基本设置。

所有设备命令​

检测(所有设备)​

使用探测命令可以请求最新的位置/姿态信息,而不会施加力或进行其他仿真更改。

  • inverse3:probe_position
  • Verse 握持器(verse_grip、wireless_verse_grip):probe_orientation
{
"inverse3": [
{
"device_id": "049D",
"commands": {
"probe_position": {}
}
}
],
"verse_grip": [
{
"device_id": "049D",
"commands": {
"probe_orientation": {}
}
}
],
"wireless_verse_grip": [
{
"device_id": "049D",
"commands": {
"probe_orientation": {}
}
}
]
}

设置转换(所有设备)​

为设备设置工作空间变换(从设备空间到应用空间)。

与其他 commands 条目不同,set_transform 是持久性的——服务器会一直保留你最后发送的值,直到你发送新值为止。你不需要每个周期都发送该值,但由于其用途是相机/场景导航,因此在连续运动时(例如用户平移场景时,每帧发送一次变换),按周期速率持续发送是预期中的使用方式。

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_transform": {
"transform": {
"position": { "x": 0.0, "y": 0.0, "z": 0.0 },
"rotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"scale": { "x": 1.0, "y": 1.0, "z": 1.0 }
}
}
}
}
]
}
Verse grip / 无线 Verse grip

对于 Verse grip 和无线 Verse grip 设备,转换过程中仅 rotation 权重生效——该握把仅上报姿态,因此 position 和 scale 没有可缩放或平移的内容。position 和 scale 虽然这些字段被接受(并会在快照中回传),但这纯粹是为了与Inverse3 保持结构一致;请将它们保留为单位值(position = {0,0,0}、scale = {1,1,1})是标准做法。

Inverse3 设备配置​

配置项通过 configure 映射发送。这些配置是持久生效的——只需发送一次,或在需要更改时再发送。

预设​

{
"inverse3": [
{
"device_id": "049D",
"configure": {
"preset": { "preset": "arm_front_centered" }
}
}
]
}

可用值:device_defaults、arm_front、arm_front_centered、led_front、led_front_centered、custom。

预设仅在设备当前所在位置应用一次。此后,当旋转设备时,工作空间会随之旋转;并且自应用居中预设以来,当设备旋转 1° 或以上时,config.preset 将变为 custom。若要重新居中,请再次发送预设。参见 预设仅应用一次。

基准(设备级)​

设置此设备的基准,即Haply 坐标如何映射到应用程序的坐标系。设置完成后,设备的状态将以此基准返回,您发送至设备的数值也将按此基准进行解释。请将此基准发送至您使用的每一台设备(包括Verse Grips),以及后续添加的设备(device-connected)。若要同时为当前所有设备写入相同的基准值,请使用会话级快捷方式。

字符串的排列是相对于Haply 的右手系/Z轴向上坐标系表示的,由 X、Y、Z 由一个排列组成,前面可选地加上 + 或 -。示例:

  • XYZ、ZYX、+Y-Z+X、X-ZY
  • YZX 请表达您的 X 对应Haply 的 Y(前方),您的 Y 对应Haply 的 Z(上方),您的 Z 对应Haply 的 X(右侧)。
  • X-YZ:左手系 Z 轴向上(Unreal)。
{
"inverse3": [
{
"device_id": "049D",
"configure": {
"basis": { "permutation": "ZXY" }
}
}
]
}
注意
从 session.set_basis 迁移

旧版 session.set_basis 对置换字符串的解读是反向的:新字符串是旧字符串的逆(X-ZY 变为 XZ-Y,ZXY 变为 YZX)。请参阅 转换表。

安装​

设置设备安装转换。注意 mount 内部的 transform 包装层。

{
"inverse3": [
{
"device_id": "049D",
"configure": {
"mount": {
"transform": {
"position": { "x": 0.0, "y": 0.0, "z": 0.0 },
"rotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"scale": { "x": 1.0, "y": 1.0, "z": 1.0 }
}
}
}
}
]
}
附着结构的不对称性

命令端(客户端到服务器端)会将转换包裹一层:"mount": { "transform": { ... } }。 快照端(从服务器端到客户端)则采用扁平化结构:"mount": { "position": {...}, "rotation": {...}, "scale": {...} }。 这是有意为之:命令采用统一的封装结构,而快照则直接序列化转换。请注意,不要将快照的结构复制到命令负载中。

阻尼​

用于控制均匀阻尼和/或方向性阻尼。至少需包含一个字段。

{
"inverse3": [
{
"device_id": "049D",
"configure": {
"damping": { "scalar": 0.5 }
}
}
]
}

您还可以设置方向性阻尼,或者同时设置这两者:

"damping": { "scalar": 0.5, "vector": { "x": 0.0, "y": 1.0, "z": 0.0 } }

力门控​

设置力门控衰减增益(0.0 至 1.0)。

{
"inverse3": [
{
"device_id": "049D",
"configure": {
"force_gate": { "gain": 0.3 }
}
}
]
}

Inverse3 命令​

要向 inverse3 设备发送命令,请在 inverse3 在键下方添加一个 device_id 与之匹配的条目。该命令按 tick 生效,必须在每个 tick 重新发送才能保持有效。

向单个设备发送命令:

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_cursor_force": {
"vector": {
"x": 1.0,
"y": 2.0,
"z": 3.0
}
}
}
}
]
}

多设备批量发送指令:

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_cursor_force": {
"vector": {
"x": 1.0,
"y": 2.0,
"z": 3.0
}
}
}
},
{
"device_id": "049E",
"commands": {
"set_cursor_force": {
"vector": {
"x": 1.0,
"y": 2.0,
"z": 3.0
}
}
}
}
]
}

设置光标位置​

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_cursor_position": {
"position": {
"x": 1.0,
"y": 2.0,
"z": 3.0
}
}
}
}
]
}

设置光标力​

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_cursor_force": {
"vector": {
"x": 1.0,
"y": 2.0,
"z": 3.0
}
}
}
}
]
}

设置角度位置​

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_angular_position": {
"angles": {
"a0": 1.0,
"a1": 2.0,
"a2": 3.0
}
}
}
}
]
}

设置角扭矩​

{
"inverse3": [
{
"device_id": "049D",
"commands": {
"set_angular_torques": {
"torques": {
"a0": 1.0,
"a1": 2.0,
"a2": 3.0
}
}
}
}
]
}

Verse Grip 配置​

Verse Grip 和无线 Verse Grip 设备支持与Inverse3 相同的 configure 键,即 preset、basis 和 mount。

扩展 Verse Grip​

set_extension_data 该命令是 Verse Grip 扩展协议的一部分,用于实现板级扩展通信协议的 grip 版本。

设置 Grip 扩展数据​

支持的数据长度:

  • 上行(客户端 → 设备)最多 20 字节。
  • 下行(设备 → 客户端)最多 12 字节,通过 State Update 消息中的 state.extension_data 返回。

数据规范:

  • 数组长度:20 字节
  • 取值范围:每个值在0–255 之间
{
"wireless_verse_grip": [
{
"device_id": "049D",
"commands": {
"set_extension_data": {
"extension_data": [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
}
}
}
]
}

示例​

示例:初始库存加载​

当 WebSocket 建立连接时,服务会发送一条包含完整设备列表的消息。初始消息的格式如下: JSON:

{
"inverse3": [
{
"device_id": "04BA",
"config": {
"type": "inverse3",
"port": "COM13",
"device_info": {
"major_version": 7,
"minor_version": 1,
"id": "04BA",
"device_type": 4,
"uuid": "2D35F80DD9005F599B68F49944CB04BA"
},
"extended_device_id": "2D35F80DD9005F599B68F49944CB04BA",
"extended_firmware_version": "8C20FDC8010AA1E15AA133CDA2534874",
"gravity_compensation": {
"enabled": true,
"scaling_factor": 1
},
"handedness": "right",
"streaming_mode": "USB",
"torque_scaling": {
"enabled": true
},
"home_return": {
"enabled": false
},
"filters": {
"force_gate": { "gain": 0.3 },
"damping": { "scalar": 0 }
},
"preset": "defaults",
"basis": { "permutation": "XYZ" },
"mount": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
}
},
"state": {
"cursor_position": {
"x": 0.07842738,
"y": -0.14836666,
"z": 0.14297646
},
"cursor_velocity": {
"x": -0.011969013,
"y": 0.0012009288,
"z": -0.043197
},
"angular_position": {
"x": -69.31704,
"y": 137.62952,
"z": 19.832787
},
"angular_velocity": {
"x": 0,
"y": 0,
"z": 0
},
"body_orientation": {
"x": -0.01940918,
"y": 0.7026367,
"z": 0.00048828125,
"w": 0.7113037
},
"current_cursor_force": { "x": 0, "y": 0, "z": 0 },
"current_cursor_position": { "x": 0, "y": 0, "z": 0 },
"current_angular_torques": { "x": 0, "y": 0, "z": 0 },
"current_angular_position": { "x": 0, "y": 0, "z": 0 },
"control_domain": "cartesian",
"control_mode": "force",
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"calibrated": false,
"in_use": false,
"power_supply": true,
"ready": true,
"started": true
}
}
],
"verse_grip": [
{
"device_id": "61548",
"config": {
"type": "verse_grip",
"port": "COM3",
"preset": "device_defaults",
"basis": { "permutation": "XYZ" },
"mount": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
}
},
"state": {
"button": false,
"hall": 0,
"orientation": {
"x": -0.5019531,
"y": 0.8632202,
"z": -0.048095703,
"w": -0.022338867
},
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"error": 0,
"ready": true
}
}
],
"wireless_verse_grip": [
{
"device_id": "0",
"config": {
"type": "wireless_verse_grip",
"port": "COM6",
"major_version": 1,
"minor_version": 4,
"hardware_version": 1,
"streaming_mode": "Radio",
"preset": "device_defaults",
"basis": { "permutation": "XYZ" },
"mount": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
}
},
"state": {
"battery_level": 0.816,
"battery_voltage": 3.77,
"buttons": {
"a": false,
"b": false,
"c": false
},
"hall": 16,
"orientation": {
"x": -0.019866943,
"y": -0.017486572,
"z": 0.05508423,
"w": -0.9963989
},
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"connected": true,
"awake": true,
"ready": true
}
}
]
}

示例:状态更新负载​

该服务会针对收到的每条消息发送一条状态更新消息,其中包含所有设备的状态。

如果您想获取设备的状态,必须先向其发送一条消息(例如探测命令或力值,即使数值为零)。当将设备用作输入源(例如跟踪位置)且未施加力时,这一点尤为重要。

状态更新消息采用以下 JSON 格式:

{
"inverse3": [
{
"device_id": "04BA",
"state": {
"cursor_position": {
"x": 0.07842738,
"y": -0.14836666,
"z": 0.14297646
},
"cursor_velocity": {
"x": -0.011969013,
"y": 0.0012009288,
"z": -0.043197
},
"angular_position": {
"x": -69.31704,
"y": 137.62952,
"z": 19.832787
},
"angular_velocity": {
"x": 0,
"y": 0,
"z": 0
},
"body_orientation": {
"x": -0.01940918,
"y": 0.7026367,
"z": 0.00048828125,
"w": 0.7113037
},
"current_cursor_force": { "x": 0, "y": 0, "z": 0 },
"current_cursor_position": { "x": 0, "y": 0, "z": 0 },
"current_angular_torques": { "x": 0, "y": 0, "z": 0 },
"current_angular_position": { "x": 0, "y": 0, "z": 0 },
"control_domain": "cartesian",
"control_mode": "force",
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"calibrated": false,
"in_use": false,
"power_supply": true,
"ready": true,
"started": true
}
}
],
"verse_grip": [
{
"device_id": "61548",
"state": {
"button": false,
"hall": 0,
"orientation": {
"x": -0.5019531,
"y": 0.8632202,
"z": -0.048095703,
"w": -0.022338867
},
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"error": 0,
"ready": true
}
}
],
"wireless_verse_grip": [
{
"device_id": "0",
"state": {
"battery_level": 0.816,
"battery_voltage": 3.77,
"buttons": {
"a": false,
"b": false,
"c": false
},
"hall": 16,
"orientation": {
"x": -0.019866943,
"y": -0.017486572,
"z": 0.05508423,
"w": -0.9963989
},
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"connected": true,
"awake": true,
"ready": true
}
}
],
"custom_verse_grip": [
{
"device_id": "0",
"state": {
"battery_level": 0.816,
"battery_voltage": 3.77,
"buttons": {
"a": false,
"b": false,
"c": false
},
"hall": 16,
"orientation": {
"x": -0.019866943,
"y": -0.017486572,
"z": 0.05508423,
"w": -0.9963989
},
"extension_data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"transform": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 1, "y": 1, "z": 1 }
},
"transform_velocity": {
"position": { "x": 0, "y": 0, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
"scale": { "x": 0, "y": 0, "z": 0 }
}
},
"status": {
"ready": true
}
}
]
}