跳转到内容

插件之间怎么对接

需要: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 发飞行指令。

连线:

  1. Drone 的 Drone,连到 Apply Body Rate Command 的 Target
  2. Make Vector 的 Return Value,连到 Make UBot Body Rate Command 的 Body Rate Radians Per Second
  3. Make UBot Body Rate Command 的 UBot Body Rate Command,连到 Apply Body Rate Command 的 Command
给实现了姿态指令接口的对象(变量 Drone)发一条横滚角速度指令,推力 55%

烟、雾这类介质由球形单元组成,每个单元有位置和半径(单位 cm)、密度和边缘衰减(0 到 1)。实现了 UBot Perception Medium 的 Actor 在 Get Medium Snapshot 里交出当前的单元,传感器采样时收集世界里所有处于激活状态的介质,再各自处理:

  • 激光雷达:射线进入有效密度不低于阈值的单元时,在入口处被截断,按命中处理。
  • 深度相机:把深度换成射线进入介质的距离。
  • RGB 相机:像素向介质的颜色混合。

介质 Actor 不需要知道有哪些传感器,传感器也不需要知道介质是谁做的。实现一个介质见感知介质。

下面以 UBot Velocity Commandable 为例,给一个 Actor 蓝图 BP_Rover 加上接口。

  1. 添加接口

    打开 BP_Rover,点工具栏的「Class Settings」,在「Details」面板的「Interfaces」区点「Add」,选择 UBot Velocity Commandable。

  2. 打开函数

    在「My Blueprint」面板的「Interfaces」下,双击 Apply Velocity Command,打开它的函数图。没有输出的函数,比如 Stop Motion,作为事件实现:在事件图里右键,搜索 Event Stop Motion。

  3. 照图实现

    Command 是收到的速度指令。实现完成后,用返回值告诉调用方有没有接受这条指令。

连线:

  1. Apply Velocity Command 的 执行,连到 Print String 的 执行
  2. Apply Velocity Command 的 Command,连到 Break UBot Velocity Command 的 UBot Velocity Command
  3. Break UBot Velocity Command 的 Linear Velocity Meters Per Second,连到 Vector Length 的 A
  4. Vector Length 的 Return Value,连到 To String (Float) 的 In Double
  5. To String (Float) 的 Return Value,连到 Print String 的 In String
  6. Print String 的 执行,连到 Return Node 的 执行
在 BP_Rover 里实现 Apply Velocity Command:打印线速度的大小,并接受指令
  1. 接口函数的入口,Command 是调用方传进来的速度指令。
  2. 返回 false 表示拒绝这条指令。