Monitoring Drones Over Cellular Network — Part1
By: Sasanka Madawalagama
Tags: Raspberry Pi, Ardupilot, Mapping, Telemetry, Drones

1. Prerequisites

It is assumed that you have a good knowledge of how Ardupilot works and basic knowledge on how to build a drone using Ardupilot compatible hardware.

Ardupilot Wiki: https://ardupilot.org/ardupilot/

1.1 Things We Need

UAV with Mavlink enabled flight controller (Ardupilot running on a Pixhawk flight controller is used in our case)

(Ardupilot running on a Pixhawk flight controller is used in our case) RPi with a micro SD card — any works (>3 recommended for prototyping, Pi Zero is recommended for deployment)

with a micro SD card — any works (>3 recommended for prototyping, Pi Zero is recommended for deployment) RPi camera

USB modem (3G/4G)

Server (To bridge between RPi and GCS, we are using AWS)

(To bridge between RPi and GCS, we are using AWS) Ground Control Station (Mission Planner, QGroundControl, etc.)

2. Initial Setup

Raspberry pi will be your drone's companion computer. At this point, you have to set up your Raspberry Pi. A complete guide can be found here.

Summary of how to set up the RPi

Format and flash raspberry pi OS to a MicroSD card using the raspberry pi imager Connect RPi to the internet via wifi or LAN to install the needed packages. Alternatively, you can set up RealVNC and SSH to access your RPi remotely.

* Important: If you are using a headless system like RPi Zero (NO monitor, USB, keyboard, and etc.), enabling ssh is required.

Make sure your RPi is updated to have the latest packages

$ sudo apt update

$ sudo apt upgrade

* Important: It is assumed that you are using the default user for the raspberry pi for the setup and beyond. If you are using another user, it is necessary to change the codes in relevant locations.

3. Setting Up the Internet Connectivity

When the drone is up in the air, it needs internet connectivity to transmit the data to the GCS, which can be provided by a USB modem (cellular dongle). Using a USB modem is both reliable and compact.

There are two important factors to consider when setting up the internet connection to the drone.

The modem needs to be detected and connected automatically after booting. RPi should monitor internet connectivity and should auto-reconnect if the connection fails.

A cellular modem is not supported by the raspberry pi out of the box. So it is necessary to install the relevant packages and set up the internet connectivity. The common and mostly used package for network connections in Linux is the network-manager package.

Alternatively, you can also use sakis3g. The concept is the same.

Setting Up Linux Network Manager

First, we have to install the network manager and GUI for the network manager. As the packages openresolv and dhcpd5 conflicts with the network manager (reference), we have to remove them as well and reboot.

$ sudo apt install network-manager network-manager-gnome

$ sudo apt purge openresolv dhcpcd5 -y

$ sudo reboot

Add a connection profile as below.

* Replace YourConName — with a connection profile name (ex. TrueMoveInternet, VodafoneInternet), and YourNetworkAPN — with the APN of mobile broadband connection.

$ sudo nmcli con add type gsm ifname "*" con-name YourConName apn YourNetworkAPN

Check the connection by running the following command

$ nmcli connection up YourConName

To make sure the internet connection is always alive and running, you need to make a service running in the background which will monitor the connection and reconnect if it’s disconnected.

To make it happen, write an executable script that runs at system boot to connect to the internet via the mobile broadband connection and runs always in the background to keep the connection alive.

Create an executable script named connect.sh

$ sudo nano connect.sh

Include the following commands inside the connect.sh file and save it. This will continuously check the connection and if the connection is down, it will try reconnecting.

#!/bin/sh while true; do

LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"

if [ $? -eq 0 ]; then

nmcli con up id YourConnectionName

sleep 10

fi

sleep 5

done

After saving the file, add executing permission to it

sudo chmod +x connect.sh

After running the following command, you should have a connection to the internet via the USB modem.

sudo connection.sh

Now we need a way to make the script execute automatically when the RPi boots up. We can use crontab for this task. Crontab package comes pre-installed and all you need to do is add the task to run at reboot,

sudo crontab -e

The crontab config file will open. At the end of the file, add the following command, save, and exit.

Restart and check whether everything runs automatically as expected after boot.

sudo reboot

4. Connecting Flight Controller to RPi

There is excellent documentation in Ardupilot wiki on how to connect your flight controller to RPi.

In a nutshell, you need to prepare a cable that runs between connect flight controllers’ telemetry port and RPis’ serial port. It is not recommended to power your RPi using the serial port. Use a different connection from your power distribution board to power the RPi.

In our setup, we are using a pixhawk mini as the flight controller. Refer to your flight controller's pinout diagram.

4.1 Configure Serial Port of the Flight Controller

Identify the serial port number of the flight controller you used to connect to the RPi.

In our case, we connected the one and only telemetry port of pixhawk mini to RPi. It is defined as serial1 in ardupilot. Below is the serial port numbering of common flight controllers

Connect to your flight controller using mission planner

Go to the full parameter list and change the values corresponding to the serial port number you used to connect to RPi

# to enable MAVLink 2 on the serial port 1 (the default).

SERIAL1_PROTOCOL = 2

# communication baud rate of Serial 1 with the RPi: set at 921600 baud

SERIAL1_BAUD = 921

4.2 Configure Serial Port of Raspberry Pi

Go to the raspberry pi configuration

sudo raspi-config

Goto Interfacing Options → Serial →

when prompted “Would you like a login shell to be accessible over serial?”, select No →

When prompted, “Would you like the serial port hardware to be enabled?”, select Yes.

Reboot the Raspberry Pi when you are done. The Raspberry Pi’s serial port will now be usable on /dev/serial0

4.3 Install mavproxy on Raspberry Pi

Mavproxy is a command-line lightweight ground control station (GCS) that supports the Mavlink protocol used in Ardupilot. We are harnessing its port forwarding functions to use mavproxy as an Air Control Station (ACS).

More about Mavproxy

Run the following commands to install mavproxy and its dependencies. This will take a while to install, depending on your internet speed

$sudo apt-get install python3-dev python3-opencv python3-wxgtk4.0 python3-pip python3-matplotlib python3-lxml python3-pygame

$pip3 install PyYAML mavproxy --user

$echo "export PATH=$PATH:$HOME/.local/bin" >> ~/.bashrc

4.4 Check the Communication between Flight Controller and RPi

Power on both the flight controller and RPi, then check the connection by running the following command

mavproxy.py — master=/dev/serial0 — baudrate 921600 — aircraft testDrone

It will start mavproxy and if the connection is successful, you will see something like below.

5. Setting up the Video Stream

It provides additional assurance if we can monitor the drone by using a video stream. A low-resolution low-bit rate video is sufficient for this task. We can easily do this by integrating an RPi camera.

Connect the RPi camera to the RPi using the ribbon cable. More about setting up the raspberry pi camera.

Go to raspberry pi configuration to enable the RPi Camera.

sudo raspi-config

Goto interfacing options → camera → enable.

Run the following command in your home folder to check the camera is properly working. After running the command, the camera window will open and it will take a test image and save it in your home folder.

raspistill -v -o test.jpg

For streaming the video, we are using a package named MJPG-Streamer. To install MJPG-Streamer, execute the commands in the following order. More about MJPG-Streamer



git clone

sudo apt-get install cmake libjpeg8-dev

sudo apt-get install gcc g++ cd ~git clone https://github.com/jacksonliam/mjpg-streamer.git sudo apt-get install cmake libjpeg8-devsudo apt-get install gcc g++ cd mjpg-streamer-experimental

make

sudo make install

To Test the mjpeg streamer, execute the following commands. This will create a local web server that streams the video to the default port 8080

cd /home/pi/mjpg-streamer/mjpg-streamer-experimental/

export LD_LIBRARY_PATH=.

./mjpg_streamer -o “output_http.so -w ./www” -i “input_raspicam.so -fps 15”

Open the web browser of RPi and type in the address 127.0.0.1:8080. This will direct to a demo web page of mjpeg streamer. Go to the stream section and check the video stream.

Tip: If the image is upside down or mirrored, add -vf and/or -hf tags at the end, which stands for vertical flip and horizontal flip.

Ex: ./mjpg_streamer -o “output_http.so -w ./www” -i “input_raspicam.so -fps 15 -vf -hf”