Raspberry Pi 4 as Companion Computer

Raspberry Pi 4

            Companion Computers can be used to interface and communicate with ArduPilot on a flight controller unit (FCU) using the MAVLink protocol. By doing this the companion computer gets all the MAVLink data produced by the autopilot (including GPS data) and can use it to make intelligent decisions during flight and reduce computing load over FCU. In this chapter we will configure Raspberry Pi 4 as companion computer. The Raspberry Pi 4 is the latest product in the Raspberry Pi range, boasting an updated 64-bit quad core processor running at 1.4GHz with built-in metal heatsink, USB 3 ports, dual-band 2.4GHz and 5GHz wireless LAN, faster (300 Mbps) Ethernet, and PoE capability via a separate PoE HAT. Some of the features are as below-

  • 4GB RAM
  • Quad-Core 64-bit Broadcom 2711, Cortex A72 Processor
  • WLAN 802.11 b/g/n/ac (2,4 + 5,0 GHz)
  • LAN RJ45 10/100/1000 Mbit (Gigabit LAN over USB 3.0)
  • Operating Power 5V@3A  via USB Type-C Port
  • Dual-Display Micro HDMI Ports which supports H 265 Decode for 4K Video @60p

Preparing Raspberry Pi 4

Step 1   Download the latest raspios-buster-armhf.img from official site. Use pi imager or any other SD card flashing software like Balena Etcher to flash SD card of 8/16/32 Gb. I am using Raspberry Pi imager and windows laptop to flash the SD card with Raspbian OS (Linux OS)

Flashing SD card for Raspberry Pi 4 using Raspberry Pi Imager
Flashing SD card for Raspberry Pi 4 using Raspberry Pi Imager

Step 2    To access Raspberry Pi on ssh (headless), after flashing SD Card, open the directory and add a .txt file and rename it ssh without any file extension.

Adding ssh file with no extension to SD card for headless access to Raspberry Pi 4
Adding ssh file with no extension to SD card for headless access to Raspberry Pi 4

Use this sd card to boot Raspi Pi 4

Identifying IP of Raspberry Pi 4 over local LAN for wireless ssh access

Step 1 Connect Raspberry Pi 4 on LAN through ethernet. Scan for its IP address using any IP Scanner tool to find IP allotted. Use that IP to access Raspberry Pi 4 on ssh using Putty client.

Using Advanced IP Scanner to detect IP Address allotted to Raspberry Pi 4 over LAN
Using Advanced IP Scanner to detect IP Address allotted to Raspberry Pi 4 over LAN

Use that IP address to access Raspberry Pi 4 through Putty application

Default username- pi
Default password- raspberry

Remote ssh access to Raspberry Pi 4 over local LAN
Remote ssh access to Raspberry Pi 4 over local LAN

Step 2   Now we have been able to access Raspberry Pi 4 over LAN. Now we provide it with Wifi credentials to connect over Wifi in future. We need to access raspi-config to save Wifi credentials.
sudo raspi-config

Step 3      Go to system options

Figure 5.6: Select System Options
Figure 5.6: Select System Options

Step 4     Select wireless LAN

Select Wireless LAN
Select Wireless LAN

Select country to India

Select country option
Select country option

Enter SSID of wifi

Enter SSID option
Enter SSID option

Enter password

Enabling, starting and checking status of vpn.service. The service started successfully
Enabling, starting and checking status of vpn.service. The service started successfully

Select finish

Select Finish Option
Select Finish Option

Reboot system. The Raspberry Pi 4 will now connect on wifi.

Option to reboot now
Option to reboot now

Reboot the Raspberry Pi when you are done

sudo reboot

After reboot, Raspberry Pi 4 will connect to wifi. We need to find the IP Address by using any IP Scanner tool and using Putty application take remote ssh for configuring Raspberry Pi 4.

Use below mentioned commands to update and upgrade Raspi OS.

sudo apt-get update
sudo apt-get upgrade

Installation of VPN as a service

            As we want to establish MAVlink using UDP protocol over internet using 4G LTE Modem, we need VPN. To install VPN, the process is similar to the once explained above for Beagle Bone Blue in Chapter 4.

Step1     First install Open VPN application on Raspberry Pi 4

sudo apt-get install openvpn

Use WinSCP application on windows to transport the folder VPN_Folder             that contains all my .ovpn files. Let me remind you that the       VPN_Folder contains .ovpn files for FCU, Companion computer and Ground Control Station (GCS). I only need .ovpn file that I created for Raspberry Pi 4 from the folder. However, I am transporting the entire folder just for the ease of it. I have done the same process for Beagle Bone Blue above in Chapter 4. Please note the relevant file in VPN_Folder for me is raspi_4.ovpn which I renamed after download from OPENVPN website. The folder was first copied to location  /home/pi being default directory as shown below:-

Using WinSCP to port VPN_Folder from windows Laptop to Default location in Raspberry Pi 4 OS
Using WinSCP to port VPN_Folder from windows Laptop to Default location in Raspberry Pi 4 OS

Now move the folder from /home/pi to /etc/openvpn for execution
sudo mv /home/pi/VPN_Folder /etc/openvpn/

Step 2     Create systemd file for startup at boot
sudo nano /etc/systemd/system/vpn.service

Creating vpn.service at /etc/systemd/system location
Creating vpn.service at /etc/systemd/system location

[Unit]
Description=VPN Service
After=networking.service
StartLimitIntervalSec=0

[Service]
Type=idle
ExecStart=/usr/sbin/openvpn /etc/openvpn/VPN_Folder/raspi_4.ovpn
Restart=on-failure
RestartSec=1

[Install]
WantedBy=multi-user.target

Creating content of vpn.service
Creating content of vpn.service

sudo systemctl enable vpn.service
sudo systemctl start vpn.service
sudo systemctl status vpn.service
sudo systemctl daemon-reload
(only to reload service incase some changes are made)

Installation of MAVPROXY (Command Line Ground Control Station) as a service

          MAVPROXY is the command line Ground Control Station (GCS) to be installed in companion computer. In my case its Raspberry Pi 4. This MAVPROXY will receive MAVlink data (link between Ardupilot running on FCU i.e Beagle Bone Blue and companion computer) over serial interface and relay it back to the ground control station i.e Mission Planner running over laptop/PC. The MAVlink is installed over secure VPN using UDP protocol over internet connection extended by 4G LTE Modem.  

Step 1 First disable SSH login on serial port
sudo raspi-config

And in the utility, select “Interfacing Options”:

Accessing Interfacing options through raspi-config
Accessing Interfacing options through raspi-config

Step 2.    Select Serial Port option

Disabling shell messages on serial port
Disabling shell messages on serial port

Step 3.   When prompted, select no to “Would you like a login shell to be accessible over serial?”

Disabling shell messages on serial port
Disabling shell messages on serial port

Step 4     When prompted, select yes to “Would you like serial port hardware to be enabled?

Enabling serial port hardware option
Enabling serial port hardware option
Final dialog box displaying login shell disabled and serial interface enabled
Final dialog box displaying login shell disabled and serial interface enabled
Closing raspi.config option
Closing raspi.config option

Step 5    Reboot the system by clicking yes

Reboot system after making changes to configuration file
Reboot system after making changes to configuration file

Installing dependencies for MAVPROXY

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

Incase user permission issues (optional)
sudo usermod -a -G dialout <username>
To update an existing installation with the current release on Python 3 based systems
pip3 install mavproxy --user –upgrade

Interfacing with Beagle Bone Blue Flight Controller Unit (FCU)

        Connectivity Diagram Raspberry Pi 4 serial interface will connect with UART1 of Beagle Bone Blue as mentioned below: –

Connectivity diagram for physical interfacing between Raspberry Pi 4 as companion computer and Beagle Bone Blue as FCU
Connectivity diagram for physical interfacing between Raspberry Pi 4 as companion computer and Beagle Bone Blue as FCU

               Testing MAVlink over MAVPROXY               Testing MAVlink though MAVPROXY Command Line GCS. In order to test the configuration, the below mentioned command will have to be run in Terminal window of Raspberry Pi 4. Both Beagle Bone Blue and Raspberry Pi 4 have to be configured as explained till now to be able to test it.

(a)           Both Companion computer and GCS on local LAN, then use
python3 $HOME/.local/bin/mavproxy.py --master=/dev/ttyS0 –out=udp:192.168.1.7:14550

(b)          VPN Command Example.
python3 $HOME/.local/bin/mavproxy.py --master=/dev/serial0 --out=udp:100.96.1.34:1194

Kindly note that the Raspberry Pi’s serial port will now be usable on /dev/serial0 or dev/ttyS0 and 1194 is port for VPN while local LAN uses 14550. The IP Address 192.168.1.7 is the IP Address of GCS on local LAN while 100.96.1.34 is IP on VPN. Replace with your own IP Addresses.

MAXPROXY command output in console window
MAXPROXY command output in console window

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these