ROS/MAVROS Offboard Example (Python)
This tutorial shows the basics of OFFBOARD control with MAVROS Python, using an Iris quadcopter simulated in Gazebo Classic. It provides step-by-step instructions demonstrating how to start developing programs to control a vehicle and running the code in simulation.
At the end of the tutorial, you should see the same behaviour as in the video below, i.e. a slow takeoff to an altitude of 2 meters.
:::warning OFFBOARD control is dangerous. If you are operating on a real vehicle be sure to have a way of gaining back manual control in case something goes wrong. :::
:::tip This example uses Python. Other examples in Python can be found here: integrationtests/python_src/px4_it/mavros. :::
Creating the ROS Package
Open the terminal and go to
~/catkin_ws/src
directoryIn the
~/catkin_ws/src
directory create a new package namedoffboard_py
(in this case) with therospy
dependency:Build the new package in the
~/catkin_ws/
directory:You should now be able to cd into the package by using:
To store your Python files, create a new folder called
/scripts
on the package:
Code
After creating the ROS package and scripts folder you are ready to start your Python script. Inside the scripts folder create the offb_node.py
file and give it executable permissions:
After that, open offb_node.py
file and paste the following code:
Code explanation
The mavros_msgs
package contains all of the custom messages required to operate services and topics provided by the MAVROS package. All services and topics as well as their corresponding message types are documented in the mavros wiki.
We create a simple callback which will save the current state of the autopilot. This will allow us to check connection, arming and OFFBOARD flags.:
We instantiate a publisher to publish the commanded local position and the appropriate clients to request arming and mode change. Note that for your own system, the "mavros" prefix might be different as it will depend on the name given to the node in it's launch file.
PX4 has a timeout of 500ms between two OFFBOARD commands. If this timeout is exceeded, the commander will fall back to the last mode the vehicle was in before entering OFFBOARD mode. This is why the publishing rate must be faster than 2 Hz to also account for possible latencies. This is also the same reason why it is recommended to enter OFFBOARD mode from Position mode, this way if the vehicle drops out of OFFBOARD mode it will stop in its tracks and hover.
Here we set the publishing rate appropriately:
Before publishing anything, we wait for the connection to be established between MAVROS and the autopilot. This loop should exit as soon as a heartbeat message is received.
Even though PX4 operates in the aerospace NED coordinate frame, MAVROS translates these coordinates to the standard ENU frame and vice-versa. This is why we set z
to positive 2:
Before entering OFFBOARD mode, you must have already started streaming setpoints. Otherwise the mode switch will be rejected. Below, 100
was chosen as an arbitrary amount.
We prepare the message request used to set the custom mode to OFFBOARD
. A list of supported modes is available for reference.
The rest of the code is largely self explanatory. We attempt to switch to Offboard mode, after which we arm the quad to allow it to fly. We space out the service calls by 5 seconds so to not flood the autopilot with the requests. In the same loop, we continue sending the requested pose at the rate previously defined.
:::tip This code has been simplified to the bare minimum for illustration purposes. In larger systems, it is often useful to create a new thread which will be in charge of periodically publishing the setpoints. :::
Creating the ROS launch file
In your offboard_py
package, create another folder inside the ~/catkin_ws/src/offboard_py/src
directory named launch
. This is where your launch files for the package will be stored. After that, create your first launch file, in this case we will call it start_offb.launch
.
For the start_offb.launch
copy the following code:
As you can see, the mavros_posix_sitl.launch
file is included. This file is responsible for launching MAVROS, the PX4 SITL, the Gazebo Classic Environment and for spawning a vehicle in a given world (for further information see the file here).
:::tip The mavros_posix_sitl.launch
file takes several arguments that can be set according to your preferences such as the vehicle to spawn or the Gazebo Classic world (refer to here) for a complete list).
You can override the default value of these arguments defined in mavros_posix_sitl.launch
by declaring them inside the include tags. As an example, if you wanted to spawn the vehicle in the warehouse.world
, you would write the following:
:::
Launching your script
If everything is done, you should now be able to launch and test your script.
In the terminal write:
You should now see the PX4 firmware initiating and the Gazebo Classic application running. After the OFFBOARD mode is set and the vehicle is armed, the behavior shown in the video should be observed.
:::warning It is possible that when running the script an error appears saying:
Resource not found: px4 ROS path [0] = ... ...
This means that PX4 SITL was not included in the path. To solve this add these lines at the end of the .bashrc
file:
Now in the terminal, go to the home directory and run the following command to apply the changes above to the current terminal:
After this step, every time you open a new terminal window you should not have to worry about this error anymore. If it appears again, a simple source .bashrc
should fix it. This solution was obtained from this issue thread, where you can get more information about the problem. :::
Last updated