03. MRes in UAV Co-operative Mapping. Getting Control of the Flight Management Computer

Connecting the Pixhawk FMU to a Raspberry Pi 2


Project Recap:

My Masters project within the Bristol Robotics Laboratory is to design a system of UAVs that can be deployed in groups to co-operatively map the structure of their environment.  This is envisaged as an internal environment, however it is expected that the technologies developed may be additionally adapted for external mapping.  This series of posts documents key elements of the project.  So far we have set the objectives and built an airframe based on a standard 450 quadcopter configuration.

Post Objective:

An on-board Raspberry Pi will have overall control of the UAV.  This post shows how we can set up communications between the Raspberry Pi 2 and a Pixhawk flight management unit,  using the Mavlink messaging protocol, so that the Raspberry Pi can take control of navigation.

I am Using…

  • A Raspberry Pi (v1 or 2 will do for this exercise);
  • A Pixhawk (3DR, Hobbyking Pilot32, Unmanned Tech Hawk etc);
  • USB mouse. keyboard and monitor (for the Raspberry Pi);
  • Windows laptop (for the Pixhawk);
  • Mains/microUSB power adapter (for the RPi);
  • Wireless dongle or CAT5 cable to connect the RPi to internet;
  • Connecter from Pixhawk to RPi (this will likely need to be made up, so cutters, soldering iron etc.);
  • A cheap SD/microSD USB card reader;
  • USB stick to backup the RPi code.

I am Assuming…

  • You have followed the previous post, or have set up your Pixhawk and radio gear as if it is set up on an airframe.
  • A basic level of technnical skills, such as soldering, computer skills and some programming and compiling experience (we are using C++).


At no time is the UAV battery installed, the propellers attached or the Pixhawk armed.  The Pixhawk is only ever powered by a USB connection to a laptop.


The best forum to use is at: https://groups.google.com/forum/#!forum/px4users

The Process:

This is quite a long and detailed post, so here are the main steps:

  1. Understand the high level architecture;
  2. Connect the Raspberry Pi to the Pixhawk with a cable;
  3. Set up the Pixhawk firmware;
  4. Set up the Raspberry Pi operating system;
  5. Get the communications code for the Raspberry Pi;
  6. Configure the Pixhawk to send Mavlink messages;
  7. Set the Raspberry Pi to listen and check the results.


1. High Level Architecture

To build a UAV capable of autonomous flight, we need to go beyond the capabilities of standard open-source autopilot systems, such as ArduPilot and 3DR PX4 software.  However, these platforms are well-developed and flight-proven, so we do not want to start from scratch.

The approach is therefore to ‘modularise’ the system into component parts.  At the highest level this comprises:

  1. A Pixhawk FMU running PX4 autopilot software;
  2. A Raspberry Pi 2 ‘companion computer’.
Fig 1.


The Pixhawk (PX4) will fly the UAV moment by moment and provide the interface to all the flight-essential sensors.  The PX4 will be adapted as little as possible, so as to minimise the risk of unintended consequences which may lead to unexpected flight behaviour, such as a crash.

The Raspberry Pi (RPi) will make the higher order decisions, such as where to fly to next.  It needs to be connected to the Pixhawk so that it can send a continuous stream of commands to achieve this.  Also, the RPi will need to constantly check the environment and most importantly where it is within the environment.  So there will also be a stream of messages from the Pixhawk to the RPi containing information such as position and attitude data.  Additionally the RPi will have it’s own sensors attached to help with high level decision making which may include camera(s), laser range-finders etc.

The two computers talk to each other using Mavlink messages (http://www.qgroundcontrol.org/mavlink/start).  This protocol allows them to exchange a wide range of data, but most importantly permits the RPi to monitor the current position and command the PX4 where to fly to next.

This post provides a guide to establishing communications between the two computers using the Mavlink protocol.  Further posts will develop the software to achieve the autonomous flight consistent with the overall project objectives.

First the hardware…

2. Connect the RPi to the Pixhawk

The connection is going to be made using the serial ports of each.  On the Pixhawk, this will be the Telem2 port (dev/ttyS2).  On the RPi, this will be the port at dev/ttyAM0.

Make the connector cable

Fig 2.  I followed the excellent article at http://dev.ardupilot.com/wiki/companion-computers/raspberry-pi-via-mavlink/, but with one significant change.  Because the Pixhawk will be powered by the laptop at the same time the RPi is powered by the mains adapter, DO NOT CONNECT THE  5V PINS.  To do so might cause damage as the RPi attempts to power the laptop though the USB port. Apart from that, make up your cable and connect according to the diagram.  The pin connections are the same on versions 1 and 2 of the RPi.


Fig 2.


Image from http://dev.ardupilot.com/wiki/companion-computers/raspberry-pi-via-mavlink/

Fig 3. The wires leaving TELEM2 downwards are taped off for now (including the 5V wire).  Those leaving upwards are joined to the ground, TX and RX wires seen in the next image.  Please excuse the grass!

Fig 3.


Fig 4.  Note ground leftmost, two pins clear from the end.

Fig 4.


3. Set Up the Pixhawk Firmware

Because I want be able to make minor changes to the PX4 software running on the Pixhawk, I am using the developer version of PX4 built using Eclipse on Windows.  This is known as the PX4 Toolchain.  However, you can also simply use the standard release version of PX4, installed using QGroundControl (home page at http://www.qgroundcontrol.org/).  QGroundControl needs to be set up on a PC (or Mac etc.) in order to install the PX4 firmware onto the Pixhawk, which is where we start.

Install QGroundControl

Fig 5. Install QGroundControl from http://www.qgroundcontrol.org/downloads.

Fig 5. QGroundControl home page.


Fig 6.  Install one of those at the top of the page, where it says, “USE THIS VERSION AS NORMAL USER”

Fig 6.


Fig 7. When it’s downloaded, run it to install.

Fig 7.



Fig 8. For Windows, QGroundControl will also install the USB drivers for the Pixhawk.  Just follow the prompts.

Fig 8.



Install the PX4 Firmware onto the Pixhawk

Fig 9.  Run QGroundControl and go to the Setup tab.  Do not plug in the Pixhawk or connect.  Make sure you have internet access and click on ‘Upgrade’.  When prompted, plug in your Pixhawk to a USB port.  Any firmware already installed will first be erased, then the new standard firmware will be installed. This will take a couple of minutes and the Pixhawk will beep a little tune when it’s done.

Fig 9.


So that’s the Pixhawk set up for now… Over to the RPi.

4. Set Up the Raspberry Pi Operating System

It is possible to connect your RPi to your network and control it directly from a PC using PuTTY – there are some good instructions for this at http://dev.ardupilot.com/wiki/companion-computers/raspberry-pi-via-mavlink/  This means you can work on the Pixhawk and RPi from the same computer.  But it’s just another layer of complexity, so right now I am going to keep the two separate.

Fig 10.  Connect a separate keyboard, mouse and monitor to the RPi.


Fig 10.


  • The red blob is a microSD card reader and the USB stick is for backups.  You don’t need those just yet.
  • Connect your RPi to the internet.  You may have a wireless dongle – I simply plugged a  CAT5 cable into the back of the router from the RPi.

Install the Debian Wheezy Operating System

Fig 11.  You will need the Raspbian operating system download from https://www.raspberrypi.org/downloads/.  Setting up the RPi is well documented at: https://www.raspberrypi.org/documentation/installation/installing-images/README.md.

Follow the instructions for setting up with Debian (Wheezy), which has everything that you need (including C++ compilers etc.) without all the unwanted extras of NOOBS.

Fig 11.



If not already running, power up the RPi (I just use a mains/micro usb adapter) and login – if you get asked for a password, the username is pi and the password is raspberry.  Once loaded, type startx at the prompt to get the GUI interface.

5. Get the communications code for the Raspberry Pi

Fig 12. Open file manager (filing cabinet at top of screen) and create a folder to work from.  I have a folder to contain C++ code (‘CPPCode’) and then a working folder called ‘uart’ for this project.

Fig 12.



Fig 13 . The RPi is going to need some software to communicate with the Pixhawk, using Mavlink messages.  Fortunately, a great example of this has already been built for us by the Mavlink Development Team.  It’s called ‘c_uart_interface_example’ and it can be downloaded from github.

  • On the RPi, navigate to  https://github.com/mavlink/c_uart_interface_example. Take time to read this page and start to understand it.
  • Download the files and install them in your working directory.  I did this by downloading the zip file and using the RPi Archiver to unzip them.  They then needed to be copied into the working directory.
Fig 13.


Fig 14 .  You should now have all the example files in your working directory.

Fig 14.


Fig 15.  You also need the latest Mavlink library which contains the code for the very many kinds of message that can be sent.  (The mavlink folder in Fig 14  is currently empty.)

Fig 15.


By the way, if Mavlink is updated, just reinstall the new files from Github to this location.  Simple!

Fig 16.  Your Mavlink directory should now look like this.

Fig 16.



Fig 17.  Back up in the working directory, modify the makefile.  We have simplified the directory structure from the original, so the makefile needs to reflect this to find the files it needs:

  • Open ‘makefile’ in your working directory
  • Find the line invoking the compiler commencing:  g++ –I mavlink/include/mavlink/v1.0 mavlink_control.cpp…
  • Replace the first part to read (note the dot):  g++ –I ./mavlink mavlink_control.cpp…
  • Save it and close the file
Fig 17.



Fig 18. Set the code to open the right UART port on the RPi

  • Open mavlink_control.cpp for editing;
  • In the method ‘top’ – the first you come to – amend the code to open the port at /dev/ttyAMA0 and 115200 baud;
  • Save and close the file.
Fig 18.



Fig 19. Compile your code.

  • From your working directory, open a terminal console (under Tools)
  • Simply enter ‘make’ in the terminal to invoke the makefile, which in turn invokes the compiler to produce your executable.
  • If all goes OK, the file ‘mavlink_control’ is created and you simply get a terminal prompt as below.
  • Leave the terminal open as we will run the code next…
Fig 19.


Fig 20.


Note that to recompile after changes to the code have been made, I simply delete the executable from the working directory first.


6. Configure the Pixhawk to send Mavlink Messages

Fortunately, your Pixhawk firmware is already set up with everything it needs to send out the Mavlink messages we need.  These contain a range of information, but most importantly  positional data (so, ‘where do I think I am’) and attitude data (roll, pitch,yaw).  All we need to do is to tell the PX4 firmware to start sending it out.  This can be done by connecting to the Pixhawk with QGroundControl using the terminal console, then issuing the command directly.  I found this fiddly and it also needs to be done every time the Pixhawk is turned on.  I found it more convenient to place the command on the Pixhawk SD card itself, which executes every time the Pixhawk is turned on.

We need to put a file called ‘extras.txt’ in a folder called ‘etc’ at the top level of the microSD card of the Pixhawk.  This file can contain commands which are executed every time the Pixhawk starts (at the end of the boot sequence).  That is where we are going to place the commands to get the Pixhawk to start sending Mavlink messages.  Being a linux system, the file cannot be created using a Windows laptop.  There may be a great way to do this directly using the terminal console in QGroundControl, but my way to create the file was using the RPi GUI interface.

Fig 21.

  • Turn off the Pixhawk, remove the microSD card and plug it into the RPi using the cardReader.
  • The RPi should detect it, giving the card an arbitrary name.  Navigate it in the usual way using File Manager.
  • Find the top level folder ‘etc’ and create it if it doesn’t exist.
Fig 21.


Fig 22. Within the ‘etc’ folder create a file called ‘extras.txt’ and edit it.  On the first line write the command:

mavlink start –d /dev/ttyS2 –b 115200

Save the file and return the microSD card to the Pixhawk.

On boot-up, this will cause the Pixhawk to start streaming Mavlink commands through serial port Telem2 (dev/ttyS2) at a baudrate of 115200.

Fig 22.


7. Set the RPi to Listen to the Pixhawk

We are nearly there!  Time to power everything up…

  • Make sure the RPi and Pixhawk are connected with the cable;
  • Boot up the Pixhawk (by plugging it into the PC – QGroundstation is not needed);
  • If not already on, turn on the Raspberry Pi and navigate to your working directory.

At this point, the computers are connected and the Pixhawk is sending out Mavlink messages.  All we need do is to get the RPi to listen for them.

Fig 23. Open a terminal console in your working directory and run mavlink_control by entering: ./mavlink_control

Fig 23.


You should see some messages confirming the code is running and the computers are communicating.  The detail is at https://github.com/mavlink/c_uart_interface_example, but the gist of the messages are:

  • to confirm that the port is opening correctly on the RPi;
Fig 24.


  • to confirm the read thread (stream) is open and messages are being received;
Fig 25.


  • to confirm that a write thread (stream) has been set up, so messages are going in both directions…
  • and that the Pixhawk is effectively under navigational control of the RPi in ‘offboard mode’.
Fig 26.


  • to extract some more detailed data from the messages sent by the Pixhawk and display it.
Fig 27.



If you have this, congratulations, as you now have the Raspberry Pi in communication with the the Pixhawk, which is the beginning of the ability to control your UAV far beyond the capabilities of the autopilot alone.

Next Time

We will begin to explore the code in more detail, control which messages are sent and how often, and understand how to move from the simple example to code which can take full control of the autopilot system.