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

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!

3 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:

1 Like