插件之间怎么对接
需要:uBotCore 0.1.x
uBot 的插件通过接口对接,而不是互相依赖对方的类,这些接口在蓝图里也可以实现。
| 接口 | 描述的是 |
|---|---|
UBot Sensor |
能产生 uBot 样本的对象 |
UBot Actuator |
一个执行器(驱动、机械臂、夹爪等),与怎样给它下指令无关 |
UBot Velocity Commandable |
接受机体系(body frame)速度指令的对象 |
UBot Attitude Commandable |
接受姿态指令和角速度指令的飞行器(0.1.2 起) |
UBot Motor Commandable |
接受单个电机指令的飞行器(0.1.2 起) |
UBot Perception Medium |
影响感知的介质,比如烟、雾 |
每个接口有几个函数,在蓝图里是调用这些节点:
UBot Sensor:Get Sensor Id、Get Sensor Frame Id、Get Sensor Type、Is Sensor Running、Get Latest Sensor Header、Get Sensor Frame Component。UBot Actuator:Get Actuator Id、Get Actuator Type、Is Actuator Ready。UBot Velocity Commandable:Apply Velocity Command、Stop Motion。UBot Attitude Commandable:Apply Attitude Command、Apply Body Rate Command、Release Attitude Command。UBot Motor Commandable:Apply Motor Command、Get Motor Count、Release Motor Command。UBot Perception Medium:Get Medium Snapshot。
UBot Sensor、UBot Actuator 和三个指令接口可以由 Actor 或组件实现;UBot Perception Medium 只能由 Actor 实现,传感器只收集实现了它的 Actor。谁实现、谁使用:
| 接口 | 谁实现 | 谁使用 |
|---|---|---|
UBot Sensor |
uBotSensor 的全部传感器组件,也可以是你自己的 | uBotROS 的 uBot ROS Sensor Bridge 用它找出 Actor 上的传感器并发布 |
UBot Actuator |
uBotAerial 的 uBot Multirotor Movement(uBot Simple Flight 和 uBot Multirotor Dynamics 都是它的子类),也可以是你自己的 |
已发布的插件还没有用到它 |
UBot Velocity Commandable |
你自己的车辆、无人机等,Actor 或组件均可;uBotAerial 的 uBot Simple Flight 和 uBot Multirotor Dynamics |
uBotROS 的 uBot ROS Velocity Command 把 cmd_vel 交给它;uBotAerial 的 uBot Multirotor Manual Input |
UBot Attitude Commandable |
uBotAerial 的 uBot Multirotor Dynamics |
uBotROS 的 uBot ROS Attitude Command;uBotAerial 的 uBot Multirotor Manual Input(Angle、Rate 模式) |
UBot Motor Commandable |
uBotAerial 的 uBot Multirotor Dynamics |
uBotROS 的 uBot ROS Motor Command |
UBot Perception Medium |
你自己的烟雾、雾等 Actor | uBotSensor 的激光雷达和相机 |
三个指令接口都是独立的能力接口,实现它们不要求同时实现 UBot Actuator。
例子:ROS 的速度指令驱动一个对象
Section titled “例子:ROS 的速度指令驱动一个对象”uBotROS 只负责把 ROS 的 cmd_vel 翻译成 uBot 的速度指令(UBot Velocity Command),怎样让车辆动起来,由实现了 UBot Velocity Commandable 的对象决定。
| 步骤 | 发生了什么 |
|---|---|
| 1 | uBot ROS Velocity Command 收到 geometry_msgs/msg/Twist,换算成机体系的 UE 坐标轴,Source 填 ROS |
| 2 | 限速后调用目标的 Apply Velocity Command;目标返回 false 表示拒绝 |
| 3 | 超过 Command Timeout Seconds(默认 0.5 s)没有新指令,调用一次目标的 Stop Motion |
目标由 Target Component 指定;留空时,Actor 实现了接口就用 Actor 本身,否则用 Actor 上第一个实现了接口的组件。
收到的指令已经是 UE 的约定:机体系,X 前、Y 右、Z 上,线速度 m/s,角速度 rad/s,+Z 是向右偏航。会倾斜的接收者(无人机)按航向坐标系解释速度:X、Y 沿当前航向,Z 朝上。ROS 里的符号差异由 uBotROS 换算好,见坐标系与单位。
例子:飞行器的姿态、角速度和电机指令
Section titled “例子:飞行器的姿态、角速度和电机指令”研究飞控时,除了速度,还要给飞行器发更底层的指令。uBotCore 0.1.2 起为此提供两个接口,以及三种指令结构体:
| 指令 | 接口函数 | 内容 |
|---|---|---|
UBot Attitude Command |
Apply Attitude Command |
目标姿态(机体到世界的四元数),可选按偏航角速度转航向 |
UBot Body Rate Command |
Apply Body Rate Command |
机体系目标角速度(rad/s) |
UBot Motor Command |
Apply Motor Command |
每个电机一个值,Normalized(0 到 1)或 AngularVelocity(rad/s) |
姿态和角速度指令的推力有两种给法:Use Thrust 为 true 时用 Normalized Thrust(最大总推力的比例,0 到 1),否则由接收者按 Vertical Velocity Meters Per Second 控制升降,0 表示保持高度。
发送方不再发送时,调用 Release Attitude Command 或 Release Motor Command,由飞行器自己兜底,比如悬停。Apply … 返回 false 表示拒绝,例如电机值的个数与 Get Motor Count 不符。
在蓝图里用同名节点调用;C++ 里用 UBot::Actuation::FindAttitudeCommandable 和 FindMotorCommandable 在 Actor 上找到实现者。用 uBotAerial 的无人机时,实现者是 uBot Multirotor Dynamics,见 物理无人机的指令与控制模式;从 ROS 2 下发这些指令见 从 ROS 2 发飞行指令。
连线:
- Drone 的 Drone,连到 Apply Body Rate Command 的 Target
- Make Vector 的 Return Value,连到 Make UBot Body Rate Command 的 Body Rate Radians Per Second
- Make UBot Body Rate Command 的 UBot Body Rate Command,连到 Apply Body Rate Command 的 Command
Drone
组件或变量引用。在 Components 面板里把 Drone 拖进事件图即可得到它。
输出
- Drone
UBot Attitude Commandable对象引用
Make Vector
UE 引擎 · 中文界面:创建向量
Makes a vector {X, Y, Z}
输入
- X
Float (double) - Y
Float (double) - Z
Float (double)
输出
- Return Value
Vector
查看参考UKismetMathLibrary.MakeVector
Make UBot Body Rate Command
UBotCore
用机体角速度、推力、时间戳和来源等字段构造一个 UBot Body Rate Command。
输入
- Body Rate Radians Per Second
Vector - Use Thrust
Boolean - Normalized Thrust
Float (double) - Vertical Velocity Meters Per Second
Float (double) - Timestamp Seconds
Float (double) - Source
Name
输出
- UBot Body Rate Command
UBot Body Rate Command结构体
查看参考Make:FUBotBodyRateCommand
Apply Body Rate Command
Target is UBot Attitude Commandable · UBotCore
应用一条机体角速度指令。指令被拒绝时(没有就绪、飞行器不接受、数值无效等)返回 false。
输入
- 执行
- Target
UBot Attitude Commandable接口 - Command
UBot Body Rate Command结构体
输出
- 执行
- Return Value
Boolean
查看参考UUBotAttitudeCommandable.ApplyBodyRateCommand
例子:感知介质影响传感器
Section titled “例子:感知介质影响传感器”烟、雾这类介质由球形单元组成,每个单元有位置和半径(单位 cm)、密度和边缘衰减(0 到 1)。实现了 UBot Perception Medium 的 Actor 在 Get Medium Snapshot 里交出当前的单元,传感器采样时收集世界里所有处于激活状态的介质,再各自处理:
- 激光雷达:射线进入有效密度不低于阈值的单元时,在入口处被截断,按命中处理。
- 深度相机:把深度换成射线进入介质的距离。
- RGB 相机:像素向介质的颜色混合。
介质 Actor 不需要知道有哪些传感器,传感器也不需要知道介质是谁做的。实现一个介质见感知介质。
在蓝图里实现接口
Section titled “在蓝图里实现接口”下面以 UBot Velocity Commandable 为例,给一个 Actor 蓝图 BP_Rover 加上接口。
-
添加接口
打开
BP_Rover,点工具栏的「Class Settings」,在「Details」面板的「Interfaces」区点「Add」,选择UBot Velocity Commandable。 -
打开函数
在「My Blueprint」面板的「Interfaces」下,双击
Apply Velocity Command,打开它的函数图。没有输出的函数,比如Stop Motion,作为事件实现:在事件图里右键,搜索Event Stop Motion。 -
照图实现
Command是收到的速度指令。实现完成后,用返回值告诉调用方有没有接受这条指令。
连线:
- Apply Velocity Command 的 执行,连到 Print String 的 执行
- Apply Velocity Command 的 Command,连到 Break UBot Velocity Command 的 UBot Velocity Command
- Break UBot Velocity Command 的 Linear Velocity Meters Per Second,连到 Vector Length 的 A
- Vector Length 的 Return Value,连到 To String (Float) 的 In Double
- To String (Float) 的 Return Value,连到 Print String 的 In String
- Print String 的 执行,连到 Return Node 的 执行
- 接口函数的入口,
Command是调用方传进来的速度指令。 - 返回 false 表示拒绝这条指令。
Apply Velocity Command
这个节点没有登记在 API 数据里(inline 节点),引脚未经校验。
输出
- 执行
- Command
UBot Velocity Command结构体
inline 节点:未在 data/api 中登记
Break UBot Velocity Command
UBotCore
把 UBot Velocity Command 拆成线速度、角速度、时间戳和来源。
输入
- UBot Velocity Command
UBot Velocity Command结构体
输出
- Linear Velocity Meters Per Second
Vector - Angular Velocity Radians Per Second
Vector - Timestamp Seconds
Float (double) - Source
Name
查看参考Break:FUBotVelocityCommand
Vector Length
UE 引擎 · 中文界面:向量长度
Returns the length of the vector
输入
- A
Vector
输出
- Return Value
Float (double)
查看参考UKismetMathLibrary.VSize
To String (Float)
UE 引擎 · 中文界面:转换为字符串(浮点)
Converts a double value to a string
输入
- In Double
Float (double)
输出
- Return Value
String
查看参考UKismetStringLibrary.Conv_DoubleToString
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
Return Node
这个节点没有登记在 API 数据里(inline 节点),引脚未经校验。
输入
- 执行
- Return Value
Boolean
输出
无
inline 节点:未在 data/api 中登记
- 接收 cmd_vel:
uBot ROS Velocity Command组件的属性、超时和限速。 - 从 ROS 2 发飞行指令:姿态、角速度和电机指令组件。
- 物理无人机的指令与控制模式:实现这些接口的无人机。
- 感知介质:在蓝图里实现一个介质。
- 在 C++ 中使用传感器与接口:查找实现了接口的对象,调用和实现接口。