mavlink-router
What is mavlink-router?
Whether the flight controller communicates with the Raspberry Pi or the Raspberry Pi communicates with the ground station, they are all through mavlink.
When the Raspberry Pi receives the mavlink data from the flight controller via the serial port, the mavlink-router will distribute the mavlink data to other devices. (e.g. ground station, simulator or your computer visual program, etc.)
Why we need mavlink-router?
MAVLink Router can be particularly helpful in cases where you have a drone with multiple peripherals (like a GCS, companion computer, etc.) that need to communicate with each other.
- It can route MAVLink traffic from any number of interfaces to any other number of interfaces.
- It supports UART and UDP for communication and can optionally log the routed traffic.
In our specific setup, the MAVLink Router reads flight controller data from the UART port on the Raspberry Pi, it then routes the data to a UDP port. This allows our Ground Station to receive and process the flight data efficiently.
Install mavlink-router
Login to your raspberry pi, then:
If you didn’t install git, you may install it
sudo apt update
sudo apt install git
Download the source
git clone https://github.com/intel/mavlink-router.git
cd mavlink-router
git submodule update --init --recursive
sudo apt install git meson ninja-build pkg-config gcc g++ systemd
Build and install
If you didn’t install pip3, you can install it by sudo apt install python3-pip
sudo pip3 install meson
meson setup build .
ninja -C build
For the detail of the installation, please read the document here.
Create a config file
sudo mkdir /etc/mavlink-router
cd /etc/mavlink-router
sudo pico main.conf
My config is:
[General]
TcpServerPort=5760
ReportStats=false
MavlinkDialect=common
[UartEndpoint serial0]
Device=/dev/serial0
Baud=38400
[UdpEndpoint local]
Mode=normal
Address=127.0.0.1
Port=14550
It tells mavlink-router where is the serial port of the mavlink input. In my case, it is /dev/serial0
.
And, it configed Baud=38400
, make sure it is same setting in the QGroundControl Parameters SER_TEL1_BAUD
Please find the detail settings of the config file here.
Run
The command below will read the mavlink data then forward it to the 192.168.1.100 port 14550 (assume your ground station is in 192.168.1.100).
mavlink-routerd -e 192.168.1.100:14550
Connect the Ground Station
In the QGroundControl open the Application Settings > Comm inks and add a UDP Connection Port 14550 then click “Connect” button.
It is a sample video that how it works:
Auto start mavlink-router when system boot
Create the file below to /etc/systemd/system/mavlink-router.service
[Unit]
Description=MAVLink Router
Wants=zerotier-one.service
After=network-online.target zerotier-one.service
[Service]
Type=simple
ExecStart=/usr/bin/mavlink-routerd -e {groundstation}:14550
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
Replace the {groundstation} to the IP of your ground station
Please remember, both the drone and the groundstation should use the zerotier IPs. If you didn’t setup the Zerotier, plaese read here.
Start the service
sudo systemctl start mavlink-router.service
View the log
If there is error, you can view the log:
sudo journalctl -u mavlink-router.service
Enable the service
If the start
runs fine, we can enable
the service.
sudo systemctl enable mavlink-router.service
Check the status
If you find the mavlink-router didn’t start up, you can check the status:
systemctl status mavlink-router.service
If you found any mistake in the document or you have difficulty following the setups, please leave your comments to me. Your comments can help me to improve this article. Thank you!
RC Bellergy
Next: 7. Setup Video Streaming
Hello, Thanks for your post.
Is it possible to follow your tutorial on ROS2??
Hello!
Thanks for your post it realy helped me
But, I have a problem..
I’m using Ubuntu 22.04 and ROS2 on Odroid board.
Is it possible to use mavlink-router on ROS2?
Is it OK just to follow your tutorial on ROS2?
There is one command missing in the above description:
“sudo ninja -C build install”
Without this install instruction, there will be no executable “mavlink-routerd”
Hi bellergy,
great toturial! I still have some issues: I want to run QGroundControl on Windows. I can confirm (via Wiresharkl) that UDP packages are received and firewall is turned off. But QGroundControl never gets any data from UDP.
Any idea what to do?
Thanks
@aur20
Thank you for your comment. For the QGroundControl connection issue, plaese read this page: https://bellergy.com/why-does-the-qgroundcontrol-not-connect-to-the-raspberry-pi/
Hi
I am working on the raspberry pi zero w to communicate with pixhawk4 via mavlink so I established a connection between rasp and pixhawk through UART and when I am connecting in QGC UDP connection is not working and when I done with TCP , in the terminal it is showing connection accepted, but it is not connecting to QGC.
Note: Rasp and PC connected to same wifi router
Install and setup mavlink-router (connecting drone to ground station)
https://bellergy.com/6-install-and-setup-mavlink-router/
when I run this command mavlink-routerd -e XXXX
I am getting connection accepted from QGC but in QGC I am not getting the vehicle plan view in QGC
Last login: Tue Apr 5 07:21:33 2022 from 192.168.0.199
robot@raspberrypi:~ $ sudo mavlink-routerd -e 192.168.0.131:14550
mavlink-router version v2-219-g1e1b7a1
Opened UART [4]ttyAMA0: /dev/ttyAMA0
UART [4]ttyAMA0: speed = 38400
Opened UDP Client [5]local: 127.0.0.1:14550
Opened UDP Client [7]CLI: 192.168.0.131:14550
Opened TCP Server [9] [::]:5760
TCP [11]dynamic: Connection accepted
Sorry, I didn’t use TCP, I can’t have any advice on it.
According to your screen-print, the mavlink-router should run fine. It is sending data to 192.168.0.131 through UDP.
You can check following items:
1. can you ping 192.168.0.131 in the raspberry pi?
2. check the following perameters in the QGC:
MAV_0_CONFIG = TELEM 1 (assume you connected to telem1)
SER_TEL1_BAUD = 38400 baud
Where should I create this file
Auto start mavlink-router when system boot
I cant able to see /etc/systemd/system/mavlink-router.service
when I runt his I am getting this
robot@raspberrypi:~ $ /etc/systemd/system/mavlink-router.service
-bash: /etc/systemd/system/mavlink-router.service: No such file or directory
create and open the file with:
“sudo nano /etc/systemd/system/mavlink-router.service”
paste the following text into the file:
[Unit]
Description=MAVLink Router
Wants=zerotier-one.service
After=network-online.target zerotier-one.service
[Service]
Type=simple
ExecStart=/usr/bin/mavlink-routerd -e {groundstation}:14550
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
Save the content: ctrl o
Exit the editor: ctrl x
Enable the service (to start it automatically after reboot):
sudo systemctl daemon-reload
sudo systemctl enable mavlink-router.service
Start the service:
sudo systemctl start mavlink-router.service
Have fun!
can you ping 192.168.0.131 in the raspberry pi?
the above ip is rasp, how can i ping rasp by itself???
Actually after this
robot@raspberrypi:~ $ sudo mavlink-routerd -e 192.168.0.131:14550
mavlink-router version v2-219-g1e1b7a1
Opened UART [4]ttyAMA0: /dev/ttyAMA0
UART [4]ttyAMA0: speed = 38400
Opened UDP Client [5]local: 127.0.0.1:14550
Opened UDP Client [7]CLI: 192.168.0.131:14550
Opened TCP Server [9] [::]:5760
TCP [11]dynamic: Connection accepted
I ran
sudo -s mavproxy.py –master=/dev/ttyAMA0 –baudrate 38400 –aircraft MyCopter.
And this is pinging pixhawk to rasp and when i connected pixhawk to pc with usb it is changing the mode when i changed in the terminal
arm safetyoff
MANUAL> Got COMMAND_ACK: DO_SET_MODE: ACCEPTED
disarm
MANUAL> Got COMMAND_ACK: COMPONENT_ARM_DISARM: ACCEPTED
and when this happens QGC changes the modes and arm disarm but
without USB i cant connect to QGC either with UDP or TCP
The command “mavlink-routerd -e 192.168.0.131:14550” means send mavlink from raspberrypi to 192.168.0.131. So, the 192.168.0.131 should be the IP of the grand station.
sudo mavlink-routerd -e {the_ip_of_grand_station}:14550
Are you using px4 flight stack? My tutorial is for “px4” not for “ardupilot”.
And the MAVProxy is not compatible with PX4, please read here.
Yes this is pinging to the ground station through TCP and now it is connecting to the QGC its a baudrate issue
And how to run two commands at the same time in rasp terminal since it got connected to QGC when I run this command
sudo mavlink-routerd -e {the_ip_of_grand_station}:14550
But to give mavlink command to QGC I need to give from the Rasp terminal but when I am running this command for Mavlink communicaton
sudo -s mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter
when I run the above one it is closing the connection between rasp and pc so can you please tell me how to run two parallel comands without affecting each other
I didn’t use mavproxy for long time since it is for Andupilot only. If you use mavproxy, you no need mavlink-router.
You may try this:
mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter –out=udp:{grandstation_ip}:14550
If you are using “Andupilot”, my tutoral settings are not right for you.
You can read here for mavproxy vs mavlink-router
I am using PX4 not Ardupilot even though I run sudo -s mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter
It is working and PX4 supports Mavproxy
The issue is I cant send the both commands parallely if I run this sudo mavlink-routerd -e {the_ip_of_grand_station}:14550
sudo -s mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter
this is not working
And vice versa if I connect the mavproxy command I am unable run the command at the same time
I need to come out of one command and should go to the other one..
but I want commands to activate parallely mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter –out=udp:{grandstation_ip}:14550 even this is working but again not connecting to QGC
IC.
The problem should be the mavlink-router and mavproxy both pointing to /dev/ttyAMA0. you can try:
mavlink-routerd -e {grandstation_ip}:14550 -e 127.0.0.1:14550
It will route the mavlink to the grand station and localhost, then set the mavproxy read the localhost:
mavproxy.py –master=udp:127.0.0.1:14550
Hope it works.
The problem in the rasp terminal is when I am running
mavlink-routerd -e {grandstation_ip}:14550 -e 127.0.0.1:14550
I cant run one more it is showing me like this
Last login: Tue Apr 5 13:02:55 2022 from 192.168.0.199
robot@raspberrypi:~ $ mavlink-routerd -e 192.168.0.131:14550 -e 127.0.0.1:14550 mavlink-router version v2-219-g1e1b7a1
Opened UART [4]ttyAMA0: /dev/ttyAMA0
UART [4]ttyAMA0: speed = 921000
Opened UDP Client [5]local: 127.0.0.1:14550
Opened UDP Client [7]CLI: 192.168.0.131:14550
Opened UDP Client [9]CLI: 127.0.0.1:14550
Opened TCP Server [11] [::]:5760
mavproxy.py –master=udp:127.0.0.1:14550
TCP [13]dynamic: Connection accepted
mavproxy.py –master=udp:127.0.0.1:14550
^CTCP [13]dynamic: Connection closed
robot@raspberrypi:~ $ mavproxy.py –master=udp:127.0.0.1:14550
ERROR: mavproxy takes no position arguments; got ([‘–master=udp:127.0.0.1:14550’])
robot@raspberrypi:~ $
I t is showing me like this after entering command in terminal it is closing the command line and if want to enter one more I need to press ctrlC this leads to stop the running comman dand opens the new command line
You can set the mavlink-routerd to system service. It will auto-start when system boot. Please read the section “Autostart mavlink-router when system boot” above the page.
I already did that after autostart that should run but when i run the
mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter –out=udp:{grandstation_ip}:14550
It is establishing connection between QGC and ground control but I am unable to send mav commands it is showing like this
robot@raspberrypi:/etc/mavlink-router $ sudo -s mavproxy.py –master=/dev/ttyAMA0 –baudrate 57600 –aircraft MyCopter –out=udp:192.168.0.131:14550
Connect /dev/ttyAMA0 source_system=255
no script MyCopter/mavinit.scr
Log Directory: MyCopter/logs/2022-04-06/flight1
Telemetry log: MyCopter/logs/2022-04-06/flight1/flight.tlog
Waiting for heartbeat from /dev/ttyAMA0
MAV> link 1 down
link 1 OK (when i connect QGC)
no link
link 1 down
no link
no link
no link
no link
no link
no link
no link
Hi,
at first – thank you for your detailed instructions! This is awsome!.
I am using ardupilot, do you maybe know what are differences or where should I look?
Both use Mavlink, so I assume setup would be simmilar.
I am using raspberry pi 3 or 3B+. I built the Raspberry Pi with the “ninja -C build” command. After lock is made in the next file, it does not proceed. What version of Raspberry Pi did you use? I need help.
pi@clover-4217:~/src/mavlink-router $ ninja -C build
ninja: Entering directory `build’
[11/32] Compiling C++ object src/mainloop_test.p/mainloop.cpp.o
I use raspberry pi zero 2. The mavlink-router can be builed on raspberry pi 3 too.
Probaly you may solve this problem by incresing your swap memory.
Fisrt turn off your swap memory
sudo dphus-swapfile swapoff
Go to swapfile and change the amount of swap-memory
sudo nano /etc/dphus-swapfile
The default config swapsize is 100(1Mi), you can change that to 2048 (2Gi)
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=2048
After that, you can setup the memory, and turn on the swapmemory
sudo dphus-swapfile setup
sudo dphus-swapfile swapon
I hope this can help you!
Great Work! Keep up!
Hi. I am using a pi4 with Ubuntu. I am getting the error “Could not open /dev/serial0 (No such file or directory)” when I try to do the mavlink-routerd command in the terminal.
Is it possible to open the port serial0 with Ubuntu like you can with raspberryOS? Thanks!
Hi Brandon,
The /dev/serial0 represent the serial port of the Raspberry Pi. Sometime we can use /dev/ttyAMA0 on difference version of Raspberry Pi.
The Raspberry Pi is using Debian, the serial port related to the hardware config but not the type of OS. What is the hardware that you are using? Are you configuring the system for the simulator (STIL)?
Thanks!
Thank you for your reply!
We are using a pixhawk for our flight controller that is connected to the Raspberry Pi. I am not using STIL for this at the moment.
I am new to this, so my school team and I are trying to figure out how to set up the the Pi and Pixhawk to interact with QGroundControl wirelessly with WiFi.
Any tips will be appreciated. Thanks!
Plaese check did you set enable_uart=1 in /boot/config.txt?
I played around with enable_uart = 1 and eventually got around to working after searching some other similar threads. Thank You bellergy!
HI Bellegry,
I found “mavlink-routerd : command not found” on Terminal Raspberry PI when we trying to connect.
We used orange cube pixhawk and raspberry pi 4.
any suggestion ?
Thanks
Hi Hans,
Did you build and install the mavlink-router successfully?
The default location of mavlink-routerd is /usr/bin/mavlink-routerd, can you find it?
HI Bellergy,
Sorry just reply. I found the mavlink-routerd in home/pi/mavlink-router/build/src. but when we run it we found that command not found when run the command mavlink-routerd (ground station).
how we solve this problem?
thank you
regards,
Hans
Hi Bellergy,
Iam also facing a problem during config:
“./autogen.sh && ./configure CFLAGS=’-g -O2′ \
–sysconfdir=/etc –localstatedir=/var –libdir=/usr/lib64 \
–prefix=/usr”
returns ./autogen.sh no such file or directory and the same for configure since there are no files in the mavlink-router repo with these names?
Iam not really familiar with using autogen, can you please point me in the direction of what I’m missing?
Thanks !
Hi YMO,
The autogen.sh seems to be removed when the source code updated.
You may try to follow the original install guide to install it.
https://github.com/mavlink-router/mavlink-router
(sorry, I have no time to test it so far. I will update the guide when I tasted it)
is there any update on this project? Would like to see the next steps!
RC Bellergy youre a great human being. thanks for sharing this.
hope all the best for you
Hi, thank you for this guide. I am facing one problem: The connection between Pi and flight controller (FC) is inconsistent. On some occasions I am able to connect and get log messages and also send specific Mac commands. However on some random occasions the connection does not work. Generally this happens if I leave the Pi and the FC in powered on and idle state for about 15 mins. Once this happens then I need to keep restarting the Pi and FC several times to finally get it to work.
I am using the following setup:
FC – Pixhawk Cube Orange
Firmware – Arducopter 4.0.7
Pi – Pi zero W and Pi 4 (faces similar problems on both)
Please let me know if you have come across such problem and can give me any indication on what may be the cause of this.
Thank you.
Hi Siddhesh,
I didn’t face similar problems. How do you connect the drone with your Mac (the software config)?
Hello, thanks a lot for this guide. I am a complete newbie when it comes to drones and working with ubuntu. I am trying to install the mavlink router on my ubuntu pc but I’m unable to do so since I’m not able to make sense of the Github guide to install it. Please let me know how I can proceed. Thanks!
@Aashish Kumar
Did you install the the git first? Or you have problem when running the git command?