Skip to content

Instantly share code, notes, and snippets.

@julianoes
Last active April 16, 2024 21:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save julianoes/adbf76408663829cd9aed8d14c88fa29 to your computer and use it in GitHub Desktop.
Save julianoes/adbf76408663829cd9aed8d14c88fa29 to your computer and use it in GitHub Desktop.
PX4 ROS2 offboard tutorial

PX4 ROS2 offboard tutorial

The instructions are assuming Ubuntu 22.04 and use ROS2 Humble and PX4 from the main branch (going to be v1.14).

Steps

ROS2

Install ROS2 Humble according to offical instructions. I ended up install the full desktop, so that I could try the Talker-listener example.

PX4 SITL with Gazebo Classic

Install the toolchain as explained in the PX4 docs, namely:

git clone https://github.com/PX4/PX4-Autopilot.git --recursive
PX4-Autopilot/Tools/setup/ubuntu.sh --no-nuttx

Then I built and ran it:

make px4_sitl gazebo_iris

(Note that make px4_sitl gazebo no longer works).

Leave PX4 running, and create a new terminal for the next step.

micro-ROS

Install micro-ROS and run it.

Source the ROS2 environment:

source /opt/ros/humble/setup.bash

Create a workspace:

mkdir microros_ws
cd microros_ws
mkdir src

Clone repo:

git clone -b humble https://github.com/micro-ROS/micro_ros_setup.git src/micro_ros_setup

And build it:

colcon build
source install/local_setup.bash
ros2 run micro_ros_setup create_agent_ws.sh
ros2 run micro_ros_setup build_agent.sh

For the first time it complained that I needed to run:

sudo rosdep init
rosdep update

Now the micro-ROS agent can be run:

source install/local_setup.bash
ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888

It should report that several subscribers and topics have been created.

Leave it running, and open a new terminal for the next step.

Check PX4 topics

Again, source the ROS2 environment:

source /opt/ros/humble/setup.bash

Then, try to check if you can see the topics published by PX4:

ros2 topic list

The output should be something like this:

/fmu/in/obstacle_distance
/fmu/in/offboard_control_mode
/fmu/in/onboard_computer_status
/fmu/in/sensor_optical_flow
/fmu/in/telemetry_status
/fmu/in/trajectory_setpoint
/fmu/in/vehicle_attitude_setpoint
/fmu/in/vehicle_command
/fmu/in/vehicle_mocap_odometry
/fmu/in/vehicle_rates_setpoint
/fmu/in/vehicle_trajectory_bezier
/fmu/in/vehicle_trajectory_waypoint
/fmu/in/vehicle_visual_odometry
/fmu/out/failsafe_flags
/fmu/out/sensor_combined
/fmu/out/timesync_status
/fmu/out/vehicle_attitude
/fmu/out/vehicle_control_mode
/fmu/out/vehicle_global_position
/fmu/out/vehicle_gps_position
/fmu/out/vehicle_local_position
/fmu/out/vehicle_odometry
/fmu/out/vehicle_status
/parameter_events
/rosout

If so, you should be ready for the example:

Offboard example and px4_msgs

Again, source the ROS2 environment:

source /opt/ros/humble/setup.bash

Create a second ROS workspace for the offboard example

mkdir example_ws
cd example_ws
mkdir src
git clone https://github.com/PX4/px4_msgs.git
git clone https://github.com/PX4/px4_ros_com.git src/px4_ros_com
git clone https://github.com/Jaeyoung-Lim/px4-offboard.git src/px4-offboard

Then build it:

colcon build

Check if you can see the PX4 messages being published now, e.g.

ros2 topic echo /fmu/out/vehicle_status

For me, it looks something like:

timestamp: 1673219083786731
armed_time: 0
takeoff_time: 0
arming_state: 1
...
---

Now, you can try to run the example:

ros2 launch px4_offboard offboard_position_control.launch.py

Nothing happens just yet, we need to do a few steps in QGroundControl.

Configure and setup in QGroundControl

Open QGroundControl which should connect automatically.

To allow arming, we need to tell PX4 that it's fine to do offboard control without RC connected.

Go to Vehicle Setup, Parameters, and set COM_RCL_EXCEPT to 4 which means Offboard is ignored.

Then go back and click on the mode and switch it to Offboard. Then, click on Ready to fly and click Arm.

It should now take off and start flying a circle, as commanded by the offboard Python script.

Screenshot of PX4 SITL with micro-ROS and the offboard example running

Sources

@Nunzio03
Copy link

Nunzio03 commented Feb 3, 2023

Is Fast DDS installation required for this setup? Or it is already inside ROS2 humble installation as default middleware?

@ViniciusAbrao
Copy link

Hi @Nunzio03 ,

according to tho the Humble's documentation:

"In Humble, the default DDS vendor is eProsima’s Fast DDS."

You can find more info here:

https://docs.ros.org/en/humble/Installation/DDS-Implementations.html

I am not sure about fast-rtps-gen. The names has become a little confused to me, since we are moving to use XRCE-DDS Bridge instead of Fast RTPS, but some DDS packages are still named with Fast RTPS.

In case of need to install it, you can find more info here:

https://docs.px4.io/main/en/dev_setup/fast-dds-installation.html#fast-rtps-gen

@Nunzio03
Copy link

Nunzio03 commented Feb 8, 2023

Hi @Nunzio03 ,

according to tho the Humble's documentation:

"In Humble, the default DDS vendor is eProsima’s Fast DDS."

You can find more info here:

https://docs.ros.org/en/humble/Installation/DDS-Implementations.html

I am not sure about fast-rtps-gen. The names has become a little confused to me, since we are moving to use XRCE-DDS Bridge instead of Fast RTPS, but some DDS packages are still named with Fast RTPS.

In case of need to install it, you can find more info here:

https://docs.px4.io/main/en/dev_setup/fast-dds-installation.html#fast-rtps-gen

Thanks :)

@iftahnaf
Copy link

Thanks for this tutorial, we used it and manage to run the PX4 in offboard mode with ROS2, we even connect it to Foxglove Studio and viewed the topics from there:

Screencast.from.03-15-2023.03.50.08.PM.webm

Is there a way to enter offboard and arm the drone directly from the code (without QGroundControl)?

Thanks again!

@ViniciusAbrao
Copy link

Thanks for this tutorial, we used it and manage to run the PX4 in offboard mode with ROS2, we even connect it to Foxglove Studio and viewed the topics from there:

Screencast.from.03-15-2023.03.50.08.PM.webm
Is there a way to enter offboard and arm the drone directly from the code (without QGroundControl)?

Thanks again!

Yes... you can find it here:

https://github.com/ViniciusAbrao/px4_ros2_xrcedds/blob/master/px4-offboard/px4_offboard/offboard_control.py

@iftahnaf
Copy link

Thanks for this tutorial, we used it and manage to run the PX4 in offboard mode with ROS2, we even connect it to Foxglove Studio and viewed the topics from there:
Screencast.from.03-15-2023.03.50.08.PM.webm
Is there a way to enter offboard and arm the drone directly from the code (without QGroundControl)?
Thanks again!

Yes... you can find it here:

https://github.com/ViniciusAbrao/px4_ros2_xrcedds/blob/master/px4-offboard/px4_offboard/offboard_control.py

@ViniciusAbrao Thanks!

@Kihoon-Shin
Copy link

Hello, Thanks for this tutorials, I want to test this on hardware pixhawk4-mini, How should I edit the micro-ros part to connect through UART?

@julianoes
Copy link
Author

By the way, the PX4 docs should now be up to date and this gist no longer required:

https://docs.px4.io/main/en/middleware/xrce_dds.html#xrce-dds-px4-fastdds-bridge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment