Skip to main content

MAVLink ADSB_VEHICLE

What this example shows​

Switching the receiver from CSV to MAVLink output and decoding the ADSB_VEHICLE message it sends for every tracked aircraft. The script also registers a generic frame callback that prints the raw payload bytes of every MAVLink message the receiver sends. CSV is the receiver's default output according to its datasheet, and the receiver on this board reports AT+ADSB_RX_PROTOCOL_DECODED=CSV with firmware 2.89.28.0, the version the datasheet is written for.

The example code​

This is Examples/ADSBReceiver-mavlinkAdsbVehicle.py from the pinned module, without the file's header comment. The script body is exactly as shipped. The UART1 wiring on IO17 and IO18 and the reset on IO21 are the board's own wiring.

from machine import UART
from ADSBReceiver import ADSBReceiver, DECODED_PROTOCOL_MAVLINK

uart = UART(1, baudrate=115200, rx=17, tx=18, rxbuf=4096)
adsb = ADSBReceiver(uart, reset_pin=21)


# Called once per decoded ADSB_VEHICLE message, one per tracked aircraft.
def onAdsbVehicle(v):
line = "ICAO {:X}".format(v.icaoAddress)
if v.hasValidCallsign():
line += " call={}".format(v.callsign)
if v.hasValidCoords():
line += " lat={:.5f} lon={:.5f}".format(v.latDegrees(), v.lonDegrees())
if v.hasValidAltitude():
line += " alt={}m".format(v.altitudeMeters())
if v.hasValidVelocity():
line += " vel={}m/s".format(v.horVelocity / 100.0)
if v.hasValidSquawk():
line += " squawk={}".format(v.squawk)
line += " tslc={}s".format(v.tslc)
print(line)


# Called for every MAVLink frame received, whatever its message id.
def onFrame(f):
payload_hex = "".join("{:02X}".format(b) for b in f.payload)
print(
"[mavlink] msgid={} sysid={} crcValid={} payload={}".format(
f.msgid, f.sysid, "yes" if f.crcValid else "no", payload_hex
)
)


# Register the callbacks that will receive the decoded and raw MAVLink data.
adsb.onMavlinkAdsbVehicle(onAdsbVehicle)
adsb.onMavlinkFrame(onFrame)

# The module defaults to CSV output, so switch it to MAVLink here.
if adsb.enterConfiguration():
adsb.setAdsbRxProtocolDecoded(DECODED_PROTOCOL_MAVLINK)
adsb.exitConfiguration(115200)

while True:
adsb.poll()

Save it to a file on your computer, then run it on the board with mpremote run <file.py>, putting your file's name in place of <file.py>. It needs the module installed as shown on the MicroPython Library page.

Expected result​

Once the receiver is in MAVLink mode, the script prints two kinds of lines. A line starting with ICAO and the aircraft's address in hex comes from the ADSB_VEHICLE message, followed by only the fields the message marks as valid: callsign, latitude and longitude, altitude in metres, speed in metres per second and squawk code, and always tslc, the seconds since the receiver last heard from that aircraft. A line starting with [mavlink] shows the message id, the system id, whether the checksum was valid and the payload as hex bytes. The module's own comment says the receiver also sends a heartbeat message (id 0) once a second alongside the aircraft messages, and the frame callback receives that one too.

ShellMicroPython
[mavlink] msgid=0 sysid=1 crcValid=yes payload=000000001B08000403
[mavlink] msgid=246 sysid=1 crcValid=yes payload=8C1F47000AFD261BEFBC190B2015AC00983A075A0000BF01B50A00575A5A33353237000003
ICAO 471F8C call=WZZ3527 lat=45.55400 lon=18.62361 alt=11277.6m vel=230.47m/s squawk=2741 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=D2A94C0068E9621B7327C60A6062B5000032DA5E0000BF012A1400525952364C4600000003
ICAO 4CA9D2 call=RYR6LF lat=45.94671 lon=18.07584 alt=11887.2m vel=242.82m/s squawk=5162 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=67AA4B0027A5171BEEDD610BC0BBB0002477AA610000BF01631800544859314A4B00000003
ICAO 4BAA67 call=THY1JK lat=45.45345 lon=19.09632 alt=11582.4m vel=250.02m/s squawk=6243 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=2D0C44008A12F91A1552840B806EA7001437A45C0000BF011F100041554136373500000003
ICAO 440C2D call=AUA675 lat=45.25308 lon=19.32211 alt=10972.8m vel=237.16m/s squawk=4127 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=15024C003B9AB31A3178BF0B1C2267006C39944F6EFCBF0105120041534C3131330000000301
ICAO 4C0215 call=ASL113 lat=44.79781 lon=19.70975 alt=6758.94m vel=203.72m/s squawk=4613 tslc=1s
[mavlink] msgid=0 sysid=1 crcValid=yes payload=000000001B08000403

Functions used​

ADSBReceiver(uart, reset_pin=None)returns ADSBReceiver

Creates the receiver object on a UART you have already opened. If you give a reset pin, the module makes it a high-impedance input, so the pin is only driven during hardwareReset().

ReturnsA receiver object ready for the other functions.

Parameters

TypeNameDescription
machine.UARTuartAn open UART connected to the receiver. Size its receive buffer when you create it, as the example does with rxbuf=4096: dense traffic can overflow the default buffer and corrupt lines.
int or Nonereset_pinGPIO wired to the receiver's RESET pin. Optional, default None, which disables hardwareReset(). This board uses 21.
onMavlinkAdsbVehicle(cb)returns None

Registers the function that is called for every MAVLink ADSB_VEHICLE message (message id 246) whose checksum is valid.

ReturnsNothing.

Parameters

TypeNameDescription
function or NonecbThe function to call, given a MavlinkAdsbVehicle object.

The MavlinkAdsbVehicle object the example reads has these attributes and functions:

NameDescription
icaoAddressThe aircraft's ICAO address as an integer.
callsignThe callsign as a string.
latDegrees(), lonDegrees()Latitude and longitude in degrees. The message carries them as degrees times 10 million.
altitudeMeters()The altitude in metres. The message carries it in millimetres.
horVelocityHorizontal speed in centimetres per second, which is why the example divides it by 100.
squawkThe squawk code as an integer.
tslcSeconds since the last contact with this aircraft.
hasValidCallsign(), hasValidCoords(), hasValidAltitude(), hasValidVelocity(), hasValidSquawk()True when the message's flags mark that field as valid. Check them before you use the field.
onMavlinkFrame(cb)returns None

Registers the function that is called for every complete MAVLink frame received, whatever its message id. A frame's crcValid is only meaningful for message ids the module can verify, currently the heartbeat (id 0) and ADSB_VEHICLE (id 246).

ReturnsNothing.

Parameters

TypeNameDescription
function or NonecbThe function to call, given a MavlinkFrame object with msgid, sysid, crcValid and payload (bytes) among its attributes.
enterConfiguration()returns bool

Moves the receiver from RUN to CONFIGURATION state. It sends AT+CONFIG=1, switches the UART to 115200 (the receiver's fixed CONFIGURATION baud rate) and confirms the new state with AT+CONFIG?. Settings can only be written in this state.

ReturnsTrue if the receiver reports being in CONFIGURATION state, False otherwise.

setAdsbRxProtocolDecoded(protocol)returns bool

Chooses the format of the decoded aircraft data the receiver sends. The receiver takes this setting as a name, so the module sends CSV, Mavlink or None.

ReturnsTrue on success, False otherwise.

Parameters

TypeNameDescription
intprotocolOne of DECODED_PROTOCOL_NONE (0), DECODED_PROTOCOL_CSV (1) or DECODED_PROTOCOL_MAVLINK (2).
exitConfiguration(run_baud=115200)returns bool

Moves the receiver from CONFIGURATION back to RUN state. It sends AT+CONFIG=0, switches the UART to run_baud and confirms the new state with AT+CONFIG?.

ReturnsTrue if the receiver reports being in RUN state, False otherwise.

Parameters

TypeNameDescription
intrun_baudThe baud rate the receiver's BAUDRATE setting is stored as, for example 115200. Optional, default 115200.
poll()returns None

Reads every byte that has arrived, parses the complete lines and MAVLink frames (it tells them apart byte by byte) and calls the registered functions. It does not block, so call it on every pass of your loop.

ReturnsNothing.

Putting it together​

Register both callbacks, switch the decoded protocol to MAVLink while the receiver is in CONFIGURATION state, return to RUN state and keep calling poll(). The frame callback sees every frame first, and the aircraft callback follows for each ADSB_VEHICLE frame that passed its checksum.