传感器通用用法
需要:uBotSensor 0.1.x(含依赖 uBotCore)
所有 uBot 传感器的操作都一样:添加传感器,启动、停止和手动采样,用事件或主动读取取得数据。
下面的例子都用位姿传感器(uBot Pose Sensor),其他传感器的用法相同。
启动、停止与手动采样
Section titled “启动、停止与手动采样”关闭 Auto Start 后,由蓝图决定什么时候开始和停止:
连线:
- Event BeginPlay 的 执行,连到 Start Sensor 的 执行
- Pose 的 Pose,连到 Start Sensor 的 Target
- Start Sensor 的 执行,连到 Delay 的 执行
- Delay 的 Completed,连到 Stop Sensor 的 执行
- Pose 的 Pose,连到 Stop Sensor 的 Target
Event BeginPlay
UE 引擎 · 中文界面:事件开始运行
Event when play begins for this actor.
输出
- 执行
查看参考AActor.ReceiveBeginPlay
Start Sensor
Target is UBot Sensor Component · UBotSensor
启动传感器:立即采样一次,之后按 Update Rate Hz 定时采样。Sensor Enabled 为 false 时返回 false。
输入
- 执行
- Target
UBot Sensor Component对象引用
输出
- 执行
- Return Value
Boolean
查看参考UUBotSensorComponent.StartSensor
Delay
UE 引擎 · 中文界面:延迟
Perform a latent action with a delay (specified in seconds). Calling again while it is counting down will be ignored.
输入
- 执行
- Duration
Float
输出
- Completed
查看参考UKismetSystemLibrary.Delay
Stop Sensor
Target is UBot Sensor Component · UBotSensor
停止定时采样。手动调用 Sample Sensor 仍然可以采样。
输入
- 执行
- Target
UBot Sensor Component对象引用
输出
- 执行
查看参考UUBotSensorComponent.StopSensor
Pose
组件或变量引用。在 Components 面板里把 Pose 拖进事件图即可得到它。
输出
- Pose
uBot Pose Sensor对象引用
想自己决定每次采样的时机,就关掉 Auto Start,也不要调用 Start Sensor,只调用 Sample Sensor。传感器运行时会按 Update Rate Hz 自己采样,再手动采一次,同一仿真时刻就有两个样本,IMU 和里程计的第二个样本是 Invalid Delta Time:
连线:
- Event Tick 的 执行,连到 Sample Sensor 的 执行
- Pose 的 Pose,连到 Sample Sensor 的 Target
- Sample Sensor 的 执行,连到 Branch 的 执行
- Sample Sensor 的 Return Value,连到 Branch 的 Condition
- Branch 的 True,连到 Print String 的 执行
Event Tick
UE 引擎 · 中文界面:事件Tick
Event called every frame, if ticking is enabled
输出
- 执行
- Delta Seconds
Float
查看参考AActor.ReceiveTick
Pose
组件或变量引用。在 Components 面板里把 Pose 拖进事件图即可得到它。
输出
- Pose
uBot Pose Sensor对象引用
Sample Sensor
Target is UBot Sensor Component · UBotSensor
立即采样一次,不要求传感器已经启动。本次样本为 Valid 时返回 true。
输入
- 执行
- Target
UBot Sensor Component对象引用
输出
- 执行
- Return Value
Boolean
查看参考UUBotSensorComponent.SampleSensor
Branch
UE 引擎 · 中文界面:分支
Branch Statement If Condition is true, execution goes to True, otherwise it goes to False
输入
- 执行
- Condition
Boolean
输出
- True
- False
查看参考K2Node_IfThenElse
Print String
UE 引擎 · 中文界面:打印字符串
Prints a string to the log, and optionally, to the screen If Print To Log is true, it will be visible in the Output Log window. Otherwise it will be logged only as 'Verbose', so it generally won't show up.
输入
- 执行
- In String
String - Print to Screen
Boolean - Print to Log
Boolean - Text Color
Linear Color结构体 - Duration
Float - Key
Name
输出
- 执行
查看参考UKismetSystemLibrary.PrintString
Start Sensor:先触发On Sensor Started,取一个立即样本,之后每 1 /Update Rate Hz秒取一个。已经在运行时直接返回 true。Sensor Enabled为 false 时Start Sensor返回 false。带Capture Group Id的相机也返回 false,见相机采集协调器。Stop Sensor:停止定时采样并触发On Sensor Stopped,最近一次的数据保留。Sample Sensor:不要求传感器在运行,随时取一个样本。样本为Valid时返回 true。
每次采样后,传感器触发 On Sensor Sampled,Header 是这一次的帧头:
连线:
- On Sensor Sampled (Pose) 的 Header,连到 Break UBot Frame Header 的 UBot Frame Header
- On Sensor Sampled (Pose) 的 执行,连到 Branch 的 执行
- Break UBot Frame Header 的 Valid,连到 Branch 的 Condition
- Break UBot Frame Header 的 Sequence,连到 To String (Integer64) 的 In Int
- To String (Integer64) 的 Return Value,连到 Print String 的 In String
- Branch 的 True,连到 Print String 的 执行
On Sensor Sampled (Pose)
UBot Sensor Component · UBotSensor
每次采样后触发,Header 是本次的帧头。每种传感器都有这个事件。
输出
- 执行
- Header
UBot Frame Header结构体
查看参考UUBotSensorComponent.OnSensorSampled
Break UBot Frame Header
UBotCore
把帧头拆成 Sensor Id、Frame Id、Sequence、Timestamp Seconds、Valid 和 Status。
输入
- UBot Frame Header
UBot Frame Header结构体
输出
- Sensor Id
String - Frame Id
String - Sequence
Integer64 - Timestamp Seconds
Float (double) - Valid
Boolean - Status
EUBot Sample Status枚举
查看参考Break:FUBotFrameHeader
Branch
UE 引擎 · 中文界面:分支
Branch Statement If Condition is true, execution goes to True, otherwise it goes to False
输入
- 执行
- Condition
Boolean
输出
- True
- False
查看参考K2Node_IfThenElse
To String (Integer64)
UE 引擎 · 中文界面:转换为字符串(Integer64)
Converts an 64-bit integer value to a string
输入
- In Int
Integer64
输出
- Return Value
String
查看参考UKismetStringLibrary.Conv_Int64ToString
Print String
UE 引擎 · 中文界面:打印字符串
Prints a string to the log, and optionally, to the screen If Print To Log is true, it will be visible in the Output Log window. Otherwise it will be logged only as 'Verbose', so it generally won't show up.
输入
- 执行
- In String
String - Print to Screen
Boolean - Print to Log
Boolean - Text Color
Linear Color结构体 - Duration
Float - Key
Name
输出
- 执行
查看参考UKismetSystemLibrary.PrintString
每种传感器还有自己的数据事件,比如 On Pose Data Updated,直接带本次的完整数据。
不想用事件时,在 Event Tick 或计时器里读最近一次的结果。Get Latest Frame Header 只读缓存,不会触发采样:
连线:
- Pose 的 Pose,连到 Get Latest Frame Header 的 Target
- Get Latest Frame Header 的 Return Value,连到 Break UBot Frame Header 的 UBot Frame Header
- Break UBot Frame Header 的 Sequence,连到 To String (Integer64) 的 In Int
- To String (Integer64) 的 Return Value,连到 Print String 的 In String
- Event Tick 的 执行,连到 Print String 的 执行
Event Tick
UE 引擎 · 中文界面:事件Tick
Event called every frame, if ticking is enabled
输出
- 执行
- Delta Seconds
Float
查看参考AActor.ReceiveTick
Pose
组件或变量引用。在 Components 面板里把 Pose 拖进事件图即可得到它。
输出
- Pose
uBot Pose Sensor对象引用
Get Latest Frame Header
Target is UBot Sensor Component · UBotSensor
返回最近一次采样的帧头。
输入
- Target
UBot Sensor Component对象引用
输出
- Return Value
UBot Frame Header结构体
查看参考UUBotSensorComponent.GetLatestFrameHeader
Break UBot Frame Header
UBotCore
把帧头拆成 Sensor Id、Frame Id、Sequence、Timestamp Seconds、Valid 和 Status。
输入
- UBot Frame Header
UBot Frame Header结构体
输出
- Sensor Id
String - Frame Id
String - Sequence
Integer64 - Timestamp Seconds
Float (double) - Valid
Boolean - Status
EUBot Sample Status枚举
查看参考Break:FUBotFrameHeader
To String (Integer64)
UE 引擎 · 中文界面:转换为字符串(Integer64)
Converts an 64-bit integer value to a string
输入
- In Int
Integer64
输出
- Return Value
String
查看参考UKismetStringLibrary.Conv_Int64ToString
Print String
UE 引擎 · 中文界面:打印字符串
Prints a string to the log, and optionally, to the screen If Print To Log is true, it will be visible in the Output Log window. Otherwise it will be logged only as 'Verbose', so it generally won't show up.
输入
- 执行
- In String
String - Print to Screen
Boolean - Print to Log
Boolean - Text Color
Linear Color结构体 - Duration
Float - Key
Name
输出
- 执行
查看参考UKismetSystemLibrary.PrintString
- 每帧都读时可能连续读到同一个样本。比较
Sequence可以判断是不是新数据。 - 每种传感器有自己的读取节点,比如
Get Latest Pose Data,见传感器概述。
-
添加传感器
在「Components」面板点「Add」,在「uBot」分类下选一个传感器,比如
uBot Pose Sensor。 -
摆放
组件本身就是传感器坐标系,它的位置和朝向就是传感器的位置和朝向。把它挂到要测量的部件下,再调整相对位置和朝向。
-
设置标识和频率
在「Details」面板的「U Bot Sensor」类别里设置
Sensor Id、Frame Id和Update Rate Hz。
-
决定怎么启动
Auto Start默认打开,运行开始时自动启动。想自己控制就关掉,再调用Start Sensor。 -
取数
选中组件,在「Details」面板底部「Events」区点事件旁的「+」,生成事件节点;或者用上面的主动读取。
所有传感器共有的属性在「Details」面板的「U Bot Sensor」类别(及其「Debug」、「Model」、「Noise」子类别)下:
| 属性 | 默认值 | 作用 |
|---|---|---|
Sensor Id |
各传感器自己的值,如 pose_sensor |
写入帧头的传感器标识;为空时用组件名 |
Frame Id |
各传感器自己的值 | 写入帧头的坐标系名;为空时用 Sensor Id |
Update Rate Hz |
基类 10,各传感器见概述 | 采样频率,范围 0.1 到 240 Hz |
Auto Start |
true | 运行开始时自动启动,运行结束时自动停止 |
Sensor Enabled |
true | 总开关;false 时不能启动,手动采样只得到 Disabled 帧头 |
Noise Enabled |
false | 噪声开关,只对 IMU、里程计和两种激光雷达有效 |
Random Seed |
1337 | 每次 Start Sensor 用它重置随机序列 |
Debug Visualization |
false | 运行时画调试射线,只有激光雷达使用 |
Hide Sensor Model in Game |
true | 游戏中隐藏传感器模型 |
这些属性在蓝图里只读。运行中能改的只有采样频率(Set Update Rate Hz,限制在 0.1 到 240 Hz,不会重启传感器)和模型显示。各传感器自己的属性见各自的页面。
传感器模型的显示
Section titled “传感器模型的显示”每个传感器组件带一个小模型,方便在视口里看到它装在哪里。模型没有碰撞,不投射阴影。
| 做法 | 说明 |
|---|---|
Hide Sensor Model in Game |
默认打开,游戏中隐藏模型;在「Details」面板里取消勾选即可显示 |
Set Sensor Model Visible in Game |
运行中切换,参数 Model Visible 为 true 时显示 |
Is Sensor Model Visible in Game |
读取当前是否显示 |
| 事件 | 引脚 | 什么时候触发 |
|---|---|---|
On Sensor Started |
无 | Start Sensor 启动成功,在第一个样本之前 |
On Sensor Stopped |
无 | 传感器从运行变为停止,包括运行结束时的自动停止 |
On Sensor Sampled |
Header |
每次采样之后,包括无效样本和 Disabled 样本 |
On Sensor Error |
Error Message |
采样出错时,比如位姿传感器的参考组件无效 |
每个样本的帧头(Header)带 Sensor Id、Frame Id、Sequence、Timestamp Seconds、Valid 和 Status。用数据之前先看 Valid,字段和状态码见帧头与采样状态。
Get Latest Frame Header:最近一次采样的帧头。传感器还没采过样时,它是默认值:Sequence为 0,Status为Error。Get Dropped Sample Count:因为一帧跨过多个采样时刻而跳过的采样次数,Start Sensor时清零。- 不关心具体类型时,可以通过
UBot Sensor接口读取:Get Sensor Id、Get Sensor Type、Is Sensor Running、Get Latest Sensor Header。见插件之间怎么对接。
- 组件的
BeginPlay先于 Actor 的Event BeginPlay。Auto Start在那时已经触发了On Sensor Started和第一个样本,所以在Event BeginPlay里再绑定事件会漏掉它们。用「Details」面板「Events」区的「+」生成事件节点就不会漏。 - 传感器自己的数据事件(比如
On Pose Data Updated)在On Sensor Sampled之前触发。在数据事件里调用Get Latest Frame Header得到的是上一次的帧头,请改用数据自带的Header。 - 每个 world tick 最多采一个样本。帧率低于
Update Rate Hz时,实际采样频率等于帧率,跳过的采样时刻计入Get Dropped Sample Count。见仿真时钟与采样调度。 - 暂停时不自动采样;手动调用
Sample Sensor仍然可以,但仿真时间不前进,时间戳不变。 Random Seed只在Start Sensor时生效。只用Sample Sensor、从不启动的传感器,噪声序列不受它控制。- 数据里的坐标轴保持 UE 约定(X 前、Y 右、Z 上),单位是米、秒、弧度,没有做 ROS 转换。见坐标系与单位。
- uBot Sensor Component:所有传感器共有的节点和属性。
- UBot Frame Header:帧头的字段。
- EUBot Sample Status:状态码。
- 标识与命名:
Sensor Id和Frame Id的取值规则。