1. Coding UAVs with ROS. SetUp

What’s this about?

This series of posts is for those who want to take more control of their UAV (or other robotic vehicle) using a companion computer connected to the flight control unit.  Typically to get started, this might be a Raspberry Pi connected to a Pixhawk FCU.

The series will follow the development of a UAV which will autonomously recognise objects in it’s vicinity and track them in 3D space.  The focus of the posts will vary between different aspects, such as sensing, control and machine learning, for example.


A previous post detailed how to get started using a pre-built image from Ubiquity Robotics.  Parts of this post are repeated here alongside several important changes in order to provide a complete treatment in one place.


  • Pixhawk/Pixhawk 2/ Pixhawk 4 flight control unit
  • Raspberry Pi 3 or other Linux based companion computer

Development environment

For simplicity, development is directly on the companion computer.  Later we will network the companion computer and a desktop PC for simulation work.

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.  Following the instructions on the ROS Wiki, we will install a version of Ubuntu Mate from Ubiquity Robotics, which already includes ROS.  We then install the package MAVROS, which provides the node needed to communicate between the Pixhawk and Raspberry Pi.

Preparing the microSD Card

I suggest using a 32Gb, high speed card such as the SanDisk Extreme.  Pick a reliable brand and a fast card to support image transfer etc.  I am using a standard Windows 10 PC to prepare the microSD card.

  1. Format the card.  A suggested SD card formatter is available here.
  2. Download the Ubuntu image from Ubiquity Robotics here.

UR_download3.   Use Etcher or similar to create the boot microSD card for the Pi.

Prepare the Pi

  • Insert the microSD card, boot up the Pi and allow the card to expand to fill the available space.
  • Open the home folder and show ‘Hidden Files’ using View on the menu bar.
  • 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
  • Follow the instructions on the UR download page to remove specific UR configuration and allow more general use.
    • Remove the scripts for UR specific robot platforms
      $ sudo systemctl disable magni-base
    • Also remove the scripts that automatically start ROScore
      $ sudo systemctl disable roscore
  • Remove or comment out the line invoking the startup script in the bashrc file.Screenshot from 2018-05-13 10-11-13
  • Package installation – make sure you have a valid internet connection for the next sequence.
  • Install the MAVROS package as below (for info, 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 https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
    $ sudo ./install_geographiclib_datasets.sh
  • Update and upgrade.  This is important to prevent an error that will prevent mavros from launching.
    • $ sudo apt update
    • $ sudo apt upgrade

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 baud
      Screenshot 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:
    • $ roscore
      Screenshot 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/state
      Screenshot 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/state
      Screenshot from 2018-05-13 11-06-01
  12. 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.