Talking to a PX4 FMU with a RPi via Serial. #noUSB

A guide by a n00b for a n00b. Henceforth: RPi = Raspberry Pi

Click here.

DON’T CONNECT THE RED WIRE!

Seriously, I know you saw that wiring diagram with the red line between the Pixyhawk and the Raspberry Pi that’s being stealth edited out all over the place. Will it F your FMU? I dunno, sometimes grabbing that big capacitor doesn’t hurt by why take the chance? You’ll need a BEC to power the RPi.

..and here.

DON’T CONNECT Tx to Tx and/or Rx to Rx.

Did you see that other Pixyhawk/RPi post where they did that? I did :frowning:
It’s bad. because reasons: https://elinux.org/RPi_Serial_Connection#H.2FW_considerations

  1. Configuring the serial on the RPi

    • Install Raspbian via NOOBS

    • Enable SSH

    • Most RPi’s have two UARTs (serial) which it uses and if you tell it not to it gives you the shitty one of the two. So. Go here, ctrl-F " Disabling Linux’s use of console UART", and disable Linux’s use of console UART. If your RPi has bluetooth, murder it by editing config.txt with $ sudo nano /boot/config.txt and add the line: dtoverlay=disable-bt to the end of the file, then double murder it with $ sudo systemctl disable hciuart

    • reboot

    • The serial port we are going to use is now: /dev/serial0 all other ttys are just imitating.

  2. Make a connection.

    • Aquire solder skillz
    • Aquire wires and connectors.
    • Make thing that connects the RPi UART to TELEM 2 on your flight controller.
      • GND (Pin 6) to GND
      • TXD (Pin 8) to RXD
      • RXD (Pin 10) to TXD
  3. Install/build MAVSDK (This gonna suck)

    • We’re going to build this on the RPi if you know how to cross compile and have it work then wtf are you doing here?

    • Get the RPi ready for the pain:

      • $ sudo apt update
      • $ sudo apt upgrade ← Upgrade all the things
      • $ sudo apt install python3-pip
    • Go here and Follow the ‘Building SDK from Source’ instructions for ‘Linux’. Step 5 will take a while so get a drink or four then finish with cmake --build build/default then drink some more.

    • Install system-wide as described.

    • Now we’re going to build the “backend” that will be needed by MAVSDK-Python becasue you’re a n00b and as far as we’re concerned c++ is c+=1. If you follow the instructions the configuration(?) will fail and you’ll cry so do this instead:

      • Go to the MAVSDK directory.
      • $ sudo cmake -DBUILD_BACKEND=ON --symlink-install --cmake-args "-DCMAKE_SHARED_LINKER_FLAGS='-latomic'" "-DCMAKE_EXE_LINKER_FLAGS='-latomic'" -Bbuild/default -H.
      • $ sudo cmake --build build/default
      • Take note, the file mavsdk_server that you will need for MAVSDK-Python is in the totally obvious directory: MAVSDK/build/default/src/backend/src/
  4. Install MAVSDK-Python

    • Grab a beer or three and go here. Scroll down to “Build and run from sources” and follow along from there. You might have an issue when you try $ which protoc-gen-dcsdk and nothing shows up. In that case you’ll have to add /usr/local/bin/ to your PATH. I’d explain but I forgot how to do that.

    • You tried $ pip3 install mavsdk, didn’t you?

    • Copy that mavskd_server file into MAVSDK-Python/mavsdk/bin/

  5. Give it a whirl

    • Use that fancy connector to connect the RPi UART to TELEM 2 on your flight controller.

    • In QGroundControl set MAV_2_CONFIG to TELEM 2 and reboot the flight controller. (The baud rate is probably going to default to 921600 which you can check by looking at SER_TEL2_BAUD in QGroundControl)

    • I assume you can SSH into your RPi at this point over WiFi and probably already have. If not, you should do that now. Then go to the MAVSDK-Python directory and cd examples/. From there we are going to edit an example with $ sudo nano firmware_version.py and replace the await drone.connect... line with:

      await drone.connect(system_address=“serial:///dev/serial0:921600”)

    • Now cross your fingers and type (while still in the examples directory) $ python3 firmware_version.py. If all went well you should see something like:

      Waiting for mavsdk_server to be ready…
      Connected to mavsdk_server!
      Waiting for drone to connect…
      Drone discovered with UUID: 8670068434521822000
      Version: [flight_sw_major: 1, flight_sw_minor: 10, flight_sw_patch: 0, flight_sw_vendor_major: 0, flight_sw_vendor_minor: 0, flight_sw_vendor_patch: 0, os_sw_major: 7, os_sw_minor: 29, os_sw_patch: 0]

If you just see it hang at Waiting for drone to connect... then a bad thing happed somewhere. Sorry bro.

  1. Install MAVLINK Router (Optional)

    • Quick and dirty because Im tired:

      • Clone this repo and $ cd mavlink-router/
      • $ git submodule update --init --recursive
      • $ sudo apt install python-future
      • $ sudo apt install python3-future
      • $ sudo apt install libtool
      • $ sudo apt install autoconf
      • $ sudo -s ← root shell (yes rlly)
      • # ./autogen.sh && ./configure CFLAGS='-g -O2' \ --sysconfdir=/etc --localstatedir=/var --libdir=/usr/lib \ --prefix=/usr
      • # make
      • # make install
    • Example main.conf file:

[General]
   #Mavlink-router serves on this TCP port
   TcpServerPort=5790
   ReportStats=false
   MavlinkDialect=common
   Log=/home/pi/logs/mavlink-router
   DebugLogLevel=debug

[UartEndpoint alpha]
   Device=/dev/serial0
   Baud=921600,115200,57600
   FlowControl=false

[UdpEndpoint bravo]
   Mode=normal
   Address=127.0.0.1
   Port=14550





Goodnight you Princes of Maine, you Kings of New England!

13 Likes

Nice report! This reminds me that we should really try to get a python wheel for RPi on PyPi, so that you could just pip install mavsdk there as well :innocent:

3 Likes

A follow up to the issue with $ which protoc-gen-dcsdk not showing up in $PATH.

I used $ sudo find / -name protoc-gen-dcsdk to find the directory that contained protoc-gen-dcsdk (in my case it was: /home/pi/.local/bin/) I then added that directory to $PATH with the command $ export PATH=$PATH:<path to protoc-gen-dcsdk>. After that I was all set.

Quirks (Part 1?):

The RPi Zero W uses armv6 and has issues with libatomic which is why I added (read: looked up error on google and copied soln) the `-latomic’ flags for the MAVSDK build. For other RPi with armv7 + this might not be needed.

The RPi 3 A+ does not have enough memory to build MAVSDK which I didn’t realize until just now. If I have a free USB port on my RPi I usually put a low profile flash drive in and make it swap which solves memory issues and I suspect helps take load off the SD card. (https://askubuntu.com/questions/173676/how-to-make-a-usb-stick-swap-disk)

Hi I get this error while doing:pip3 install -r requirements.txt -r requirements-dev.txt:
Collecting protobuf>=3.5.1 Using cached https://files.pythonhosted.org/packages/02/ba/2d33a1ea34f672c088c08f0f8e70292ff770c7f0c2481690cae378d33d66/protobuf-3.11.2-py2.py3-none-any.whl ERROR: Could not find a version that satisfies the requirement aiogrpc>=1.5 (from -r requirements.txt (line 2)) (from versions: none) ERROR: No matching distribution found for aiogrpc>=1.5 (from -r requirements.txt (line 2))

This might work:
sudo apt install python3-pip
sudo pip3 install aiogrpc

From: aiogrpc pip error · Issue #93 · mavlink/MAVSDK-Python · GitHub

Seems familiar, I might have run into the same issue at some point :confused:

sudo apt install python3-pip shows:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version.

And sudo pip3 install aiogrpc shows:
Collecting aiogrpc
Could not find a version that satisfies the requirement aiogrpc (from versions: )
No matching distribution found for aiogrpc
You are using pip version 9.0.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

I tried using a venv:

pi@raspberrypi:~ $ python3 -m venv venv
pi@raspberrypi:~ $ source venv/bin/activate
(venv) pi@raspberrypi:~ $ pip3 install aiogrpc
Downloading/unpacking aiogrpc
Downloading aiogrpc-1.7-py3-none-any.whl
Downloading/unpacking grpcio>=1.12.0 (from aiogrpc)
Downloading grpcio-1.26.0.tar.gz (15.4MB): 15.4MB downloaded
Running setup.py (path:/tmp/pip-build-e7jz0yfr/grpcio/setup.py) egg_info for package grpcio
Found cython-generated files...
error in grpcio setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers
Complete output from command python setup.py egg_info:
Found cython-generated files...

error in grpcio setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-e7jz0yfr/grpcio
Storing debug log for failure in /home/pi/.pip/pip.log

Im not sure, but, the consensus online seems to be to try “upgrading setuptools” with:
pip3 install --upgrade setuptools

(running on Raspberry Pi V4 to Holybro Pixhawk 4)

Amazing explanation! Worked almost perfectly for me except my mavsdk_server could not start, i was able to quickly fix it by replacing it with the mavsdk_server_linux-armv7 binary release file from v0.24(latest release at time of writing) after renaming it to mavsdk_server.
I did accidently interrupt Step 5 from the ‘Building SDK from Source’, I don’t know enough about build processes to know if this could be the cause.

Additionaly, on the mavrouter part, copying the following line from the explanation does not work:
./autogen.sh && ./configure CFLAGS='-g -O2' \ --sysconfdir=/etc --localstatedir=/var --libdir=/usr/lib \ --prefix=/usr

but the one from the mavlink-router github readme.MD explanaition did work:

./autogen.sh && ./configure CFLAGS='-g -O2' \
       --sysconfdir=/etc --localstatedir=/var --libdir=/usr/lib64 \
   --prefix=/usr

Not sure what the difference is.

@JonasVautherin Did we sort out RPi on PIP?

I.e. looking at this I’m seeing comments like “… except my mavsdk_server could not start, i was able to quickly fix it by replacing it with the mavsdk_server_linux-armv7 binary release file from v0.24(latest release at time of writing) after renaming it to mavsdk_server.”

i was able to quickly fix it by replacing it with the mavsdk_server_linux-armv7

Yes, that’s what needs to be done. We don’t distribute mavsdk_server for arm through pip (yet). Would be nice to distribute arm64 there, but that requires some work on dockcross (if anybody wants to contribute, I started working on it here: linux-arm64 vs manylinux2014-aarch64 · Issue #367 · dockcross/dockcross · GitHub).

2 Likes

Hello,
first of all: Thank you for this guideline!

I went through your guideline but when I try
$ python3 firmware_version.py
all I get is
Waiting for mavsdk_server to be ready…

So I tried to start mavsdk_server stand alone, but this creates this error message:
~/MAVSDK/build/default/src/backend/src $ ./mavsdk_server
./mavsdk_server: error while loading shared libraries: /home/pi/MAVSDK/build/default/src/backend/src/libmavsdk_server.so: unexpected reloc type 0x03

I did google for the unexpected reloc type issue, but as I am a Linux-n00b this does not get me far…
==> Can you give me some advice how to proceed?

My setup:
Raspberry Pi Zero w / armv6l
Operating System: Raspbian GNU/Linux 10 (buster)
Kernel: Linux 4.19.97+

Thank you & best regards

Hi, I copied the mavsdk_server…armv7 and renamed it but when running the firmware_version.py I get: No module named ‘mavsdk’

I already tried pip3 install mavsdk and all is satisfied.

@physicsman, first of all, thanks for the guide, it’s already helping others and having a positive impact.

I had to edit the title of this post as it went against our Code of Conduct, please try to be inclusive towards other communities and fellow community members, please DM me if you have any questions.

Learn more about the community Code of Conduct

Code of Conduct

Would the mavsdk_server I compiled on the Raspberry Pi Zero (armv6) be useful to other armv6 platforms?

Hi physicsman thank you for the detailed post, I wish I had stumbled upon this before I spent a week or so to do the exact same thing. Did you get it to work via RPi uart port? All the documentation I found was to use FTDI through USB, and I just gave up on trying to connect directly.

@physicsman , credit to Adrian Rosebrock here is a quick fix for the memory issue. From https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ : sudo nano /etc/dphys-swapfile, change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=2048, then sudo /etc/init.d/dphys-swapfile stop then sudo /etc/init.d/dphys-swapfile stop. Once you’re done with compilation/link, change back the value to 100.

I manage to go throught the whole tutorial (I restarted from a clean NOOS image so will delete my previous post), I don’t even have the issue with “which protoc-gen-dcsdk” but I’m stuck at “Waiting for mavsdk_server to be ready…” :disappointed: I’ll have a look tonight

Thanks @GoMartin300, I followed your tricks with (https://github.com/mavlink/MAVSDK/releases/download/v0.25.0/mavsdk_server_linux-armv7) and it’s working fine!

For whatever reason I did not succeed in compiling mavsdk_server correctly on the Raspberry Pi Zero w (==> 3. Install/build MAVSDK) .

Instead I tried cross-compiling via Ubuntu (Inside an Oracle VM Virtualbox on PC/Windows10) according to these instructions:
Redirecting to latest version of document (main) , but replacing armv7 by armv6. At least it compiled and did the trick for me.

cd MAVSDK
docker run --rm dockcross/linux-armv6 > ./dockcross-linux-armv6
chmod +x ./dockcross-linux-armv6
./dockcross-linux-armv6 cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -DBUILD_SHARED_LIBS=OFF -Bbuild/linux-armv6 -S.
./dockcross-linux-armv6 cmake --build build/linux-armv6 -j 8