Files
Main/99 Work/0 OneSec/OneSecNotes/10 Projects/PX4/Custom uORB Message.md
2024-12-02 15:11:30 +01:00

3.2 KiB

uORB message are the equivalent of ROS2 messages in the PX4 messaging system. uORB does the same job as the ROS2 middleware system by sharing messages in a public and subscribe system across different modules. A good overview on how uORB works and how to customize it look at the dev-series on this blog.

In order to add a custom uORB message that can be used to share information between modules and allow to log data we need to do the following steps:

  • add the message definition to PX4-Autopilot/msg/custom_msg.msg
    • add the timestamp field in the first line, since it is mandatory: uint64 timestamp
  • add the message file path to PX4-Autopilot/msg/CMakeLists.txt in order to compile the message.

If the message should be used in several independent topics we can add the following line to the message definition: # TOPICS topic_name_1 topic_name_2 topic_name_3

ROS2 Compatibility

Starting from PX4 v1.14 ROS2 compatibility is native (see this release article). In order to use the new uORB message with the ROS2 system on the companion computer you need to enable it on the bridge. In order to do this there are 4 steps required as explained in this thread:

  1. create the uORB .msg file under PX4-Autopilot/msg/
  2. Generate the ROS .msg using the uorb_to_ros_msgs.py script and copy it to px4_msgs under you colcon/ament workspace
  3. Do the changes on both yaml files in PX4-Autopilot/msg/tools and px4_ros_com/templates as you did above
  4. Build your colcon/ament workspace with both updated px4_ros_com and px4_msgs

Bridge Message Yaml File

The .yaml file is located in two locations: under PX4-Autopilot/msg/tools/ and in px4_ros_com/templates/ and it needs to be the correct version in the two locations in order for the bridge to work seamlessly. Below you can find a part of such a file (px4_ros_com version):

- msg: TelemetryStatus #9
receive: true
- msg: Timesync #10
receive: true
send: true
- msg: TrajectoryWaypoint #11
send: true
- msg: VehicleCommand #12
receive: true
- msg: VehicleControlMode #13
send: true
- msg: VehicleLocalPositionSetpoint #14
receive: true
- base: VehicleLocalPositionSetpoint #15
msg: TrajectorySetpoint
receive: true

The option receive: true means that the bridge shares the messages from the companion computer to the PX4-flight controller, whereas the option send: true means that the PX4-flight controller sends the messages to the companion computer. If a two-way communication is desired you can add both options, just like in the example above (Timesync). The PX4-Autopilot version uses the uORB message names (lowercase and underscore), whereas the px4_ros_com version uses the default ROS2 standard for the message definition (CamelCase). It is extremely important that the yaml file definition of those messages matches, else the bridge won't work properly. Specifically, the order of the messages as well. Because in the background, the messages are shared over their id (indicated as a comment in the excerpt above).