模拟通道
概述
“模拟通道”是一种高频双向 WebSocket 通道,用于交换设备状态以及发送会话或设备命令。
默认 URL: ws://localhost:10001
端口可在设置中进行更改。
基本合同(重要):
- 当客户端连接时,服务会发送一条包含所有设备完整列表的初始设备清单消息。
- 随后,服务器会针对从客户端接收到的每条消息,发送恰好一条 State Update消息。
- “各状态更新”包含所有设备的状态(及状态信息)。
本页面将介绍 Simulation Channel的核心协议和命令。
通过注册自定义命令和状态字段,可以利用附加模块或功能来扩展系统。关于这些内容,将在“modules”章节中分别进行说明。
议事规则
每个客户端消息对应一个响应
该服务会针对从客户端接收到的每条消息,发布一条包含所有设备状态的“状态更新”消息。
这意味着:
- 如果需要获取设备状态的最新快照,则必须发送某条消息(会话命令、探查命令或设备命令)。
- 该通道的工作方式类似于由客户端消息驱动的“滴答”循环。
不施加力的轮询(探测命令)
如果希望在不施加力或更改仿真参数的情况下监控状态变化,请使用探测命令:
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 vs commands
设备消息中包含:configure 和 commands 有两个独立的地图。它们各自的目的不同:
| 地图 | 目的 | 频率 |
|---|---|---|
configure | 只需设置一次的持久化配置:预设、basis、挂载、滤波器、模块设置 | 发送或修改时 |
commands | 每帧的非持久性指令:力、位置、扭矩 | 每 tick 必须发送一次 |
commands 内的条目(set_cursor_force、set_cursor_position、set_angular_torques、set_angular_position)是每帧仅应用一次,且不会被保存 -- 停止发送后,效果将立即消失,设备将恢复至空闲状态。对于那些应保持状态而不应重新发送、且需跨越一个时钟周期的情况, configure 请使用。
set_transform 这是一种特殊情况set_transform 是 commands 虽然包含在内,但,持久的。该服务会保留上次发送的变换数据,直到发送新的变换为止。无需在每个时间步长都发送数据。不过,由于其用途是相机/场景导航,因此在连续移动的导航场景中(例如:用户每次平移场景时,每帧发送一个变换),按时间步长进行流式传输是预期用法。
坐标系
Haply 默认使用Z-Up的右手坐标系。
有两个配置条目会影响坐标的解释和返回方式:
session.configure.basis: 更改Haply 的坐标与应用程序坐标系之间的对应关系(如下所述的 基础 (参见)。inverse3[*].configure.preset: 选择一个命名好的出厂配置,用于放置工作区的起点(例如:arm_front_centered是(0, 0, 0)将其放置在工作区中央)。请参阅各设备的“配置”部分。
消息格式
本节将介绍高级封装和消息类型。完整的示例见本文档后半部分。
设备组
在顶级层,消息会按设备类型进行分组(例如:inverse3、verse_grip、wireless_verse_grip)。各设备类型的键对应于每个设备的对象数组。
初始库存(服务器 → 客户端)
WebSocket 连接建立后会立即发送一次。
每个条目包含以下内容:
device_idconfigstatestatus
参考:例如:初始库存的有效载荷
状态更新(服务器 → 客户端)
每收到一条来自客户端的消息,就会发送一次。
每个条目包含以下内容:
device_idstatestatus
参考:例如:状态更新的有效载荷
会话命令的封装(客户端 → 服务器)
会话命令是针对当前连接/会话执行的操作,与特定设备无关。
{
"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"
}
}
}
}
基础(按会话计)
这是一个快捷方式,可在收到消息时,将相同的基数(basis)写入会话中的所有设备(包括Inverse3 和Verse Grips)。基数本身是每个设备的设置。关于置换格式,请参阅“基数(按设备)”。
{
"session": {
"configure": {
"basis": { "permutation": "X-YZ" }
}
}
}
session.configure.basis 不会保存在会话中。对于连接后启动或恢复的 Verse Grip,或者在会话过程中连接的设备,此设置将不适用,且将采用单位基底 XYZ 在该状态下开始运行。一旦出现此类设备,device-connected 在活动中,该设备专用的 configure.basis 请发送此消息。此快捷方式还将覆盖此前为每个设备发送的所有基础设置。
所有设备通用命令
探针测试(所有器件)
探针命令用于在无需施加力或进行其他仿真更改的情况下,获取最新的位置和姿态信息。
- inverse3:
probe_position - Verse Grip(
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 是持久的。该服务会保留上次发送的值,直到接收到新值为止。虽然无需在每个时间步长(tick)都发送数据,但鉴于其用途是用于相机/场景导航,因此在连续运动时(例如:当用户在场景中平移时,每帧发送一个变换),按时间步长进行流式传输是其预期用法。
{
"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 和 Wireless 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)请也将其发送至该设备。若要一次性为所有已连接的设备设置相同的basis,请:会话快捷键 请使用。
排序字符串以Haply 中的右手系(Z轴向上)坐标系为基准进行表示。X、Y、Z 根据需要对……进行排序 + 或者 - 是在其前添加了该词。例如:
XYZ,ZYX,+Y-Z+X,X-ZYYZX在这种情况下,您使用的X是Haply 的Y(前方)、您使用的Y是Haply 的Z(上方)、您使用的Z是Haply 的X(右侧)对应。X-YZ: 左手系·Z轴向上(虚幻引擎)。
{
"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 } }
ForceGate
设置ForceGate的衰减增益(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 和 Wireless Verse Grip 设备,preset、basis、mount 关于Inverse3 与之相同 configure 支持该键。
扩展版 Verse Grip
set_extension_data 该命令是 Verse 握把扩展协议的一部分,适用于实现了板载扩展通信协议的握把版本。
握把扩展数据的设置
支持的数据长度:
- 上游(客户端 → 设备):最多 20 字节。
- 下游(设备 → 客户端):最多 12 字节。状态更新消息的
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
}
}
]
}