Robot Operating System for Flight Monitoring and Control – Getting Started.

If you have heard of Robot Operating System and want to use it to monitor and control UAV flight, this post will get you started…

More specifically, this post details how to set up a Pixhawk flight controller running PX4 firmware, with a Raspberry Pi3 companion computer running Robot Operating System.  This combination will give flexible control over the flight control unit and the ability to integrate a very wide range of features such as depth-sensing cameras and machine learning networks.

The system represents a fairly major change in direction as I move towards a new phase in what is becoming a quest to build a multi-role, resilient UAV platform. In previous posts I have used Arducopter firmware on the Pixhawk and the capable Dronekit API on the Raspberry Pi.  So why switch out Arducopter in favour of PX4 firmware, and why replace Dronekit for ROS?

Arducopter vs PX4

I have used Arducopter for development for the last 4 years and it and remains a fine flight control package. The decision to move to PX4 has been based purely on a narrow set of requirements around my particular use of a companion computer. One reason is that the PX4 stack seems to allow more flexibility around the rate at which Mavlink messages are published to the companion computer. A second reason concerns potential delays in response in communications between the flight controller and companion computer. I have experienced this myself and whilst it has not previously been critical, it most certainly will be for future plans.

So whilst in some respects some might argue that the PX4 stack seems less flexible, it has particular strengths in some important areas for my purposes. At least I hope so!

DroneKit vs MAVROS

Dronekit has proved invaluable for developing skills to control a UAV from a companion computer. My educational activities are based on it and will continue to be so, including the UAV coding Summer School I am running this July at the University of the West of England.  The move to Robot Operating System is simply because it offers a more open development environment for robotics and a broader technical community.

Setting up the Pixhawk

  1. Download and install QGroundcontrol on a PC from here.
  2. Connect the Pixhawk to the PC. It should connect automatically over the USB link.
  3. Install the latest stable release of PX4 and configure for your airframe here.
  4. Now change the following parameter as on the PX4 guide  here.
    • Set Telem 2 for use by a companion computer: SYS_COMPANION to 921600 8N1, then reboot.


Setting up the Raspberry Pi3

The RPi3 will need Linux, ROS and MAVROS. To do anything useful with images it will also need OpenCV.

Rather than load it all up manually, I am starting with the Raspberry Pi image published by those wonderful people at Ubiquity Robotics.  It uses Ubuntu Mate and has ROS and OpenCV preinstalled. It’s hugely more convenient than doing it yourself, at least to start with.

  1. Download the Ubuntu image from Ubiquity Robotics here.


  1. Use Etcher or similar to create the boot SD card for the Pi.
  2. Boot up the Pi and allow the card to expand to fill the available space.
  3. Modify the config file to allow the Pi to access the serial port.
    • The file is at /boot/config.txt
    • You will probably need to use VIM to edit the file from the command line.
    • add “enable_uart = 1”
    • modify “dtoverlay=pi3-disable-bt”
    • Screenshot from 2018-05-13 10-30-55
  4. Follow the instructions on the UR download page to remove specific UR configuration and allow more general use.
    • sudo systemctl disable magni-base
  5. Remove or comment out the line invoking the startup script in the bashrc file.Screenshot from 2018-05-13 10-11-13
  6. Package installation – make sure you have a valid internet connection for the next sequence.
  7. Install the MAVROS package as below (it’s from here ) to allow communication between the Pixhawk and RPi. Download and run the geographiclib script.  Nb: The script will need to be made executable before it can be run ( right-click, properties…).
    $ sudo apt-get install ros-kinetic-mavros ros-kinetic-mavros-extras
    $ wget
    $ sudo ./

Connect, Configure and Test

  1. Connect the Telem2 port of the Pixhawk to the UART of the Raspberry Pi using a cable detailed in a previous post here.
  2. Modify the launch file of the MAVROS package to connect with the correct baud rate.
    • The launch file is at: /opt/ros/kinetic/share/mavros/launch/px4.launch
    • You will probably need to use VIM to edit the file from the command line.
    • Set the fcu_url to serial0 at 921600 baudScreenshot from 2018-05-13 10-16-19
  3. Power up the Pixhawk, maybe by plugging it into QGroundcontrol on a PC.
  4. Power up the Raspberry Pi.
  5. Open a terminal on the Pi and start the ROS Core node:
    • $ roscoreScreenshot from 2018-05-13 10-48-16
  6. Open a new terminal and start the mavros node:
    • $ roslaunch mavros px4.launch
    • As the connection is made, the node will display the parameters as they are read from the Pixhawk.
    • Please ignore the prefix on the parameters below – yours should just start with /mavros/…
    • Screenshot from 2018-05-13 10-49-24
  7. In another new terminal, list all the data streams (topics) being published by the mavros node:
    • $ rostopic list
    • Again, ignore the prefix…Screenshot from 2018-05-13 10-50-53
  8. These represent a rich source of data that the companion computer can access. Of course messages can also be sent the other way to command the flight controller.
  9. Take a closer look at one of the topics:
    • $ rostopic echo /mavros/stateScreenshot from 2018-05-13 11-05-02
  10. This topic can be used to test for the current flight mode. Use Ctrl-C to stop further listing.
  11. Find out how frequently a topic is published by default. Open a new terminal:
    • $ rostopic hz /mavros/stateScreenshot from 2018-05-13 11-06-01

You can use rostopic list and rostopic echo to explore information reported on many aspects of the operation of the flight controller.

Screenshot from 2018-05-13 11-06-40


Author: Mike Isted

A multi-disciplinary technologist. Embedded software engineer.