The asv-mavlink library provides a robust interface for communication with MAVLink compatible vehicles and payloads. This library is designed to facilitate the interaction with drones and other devices using the MAVLink protocol, enabling users to send commands, receive telemetry data, and perform various operations.
Additionally, the library includes a CLI utility Asv.Mavlink.Shell for simulating, testing and code generation.
Installation
To install the asv-mavlink library, you can use the following command:
This command starts a virtual ADS-B receiver that sends ADSB_VEHICLE packets at a specified rate for every vehicle defined in the configuration file.
Executing this command launches an emulator for an ADS-B receiver, generating and transmitting ADSB_VEHICLE data packets for virtual vehicles. These packets include information such as position, speed, and other ADS-B message parameters. The vehicles and their respective parameters are specified in the configuration file.
If the configuration file does not exist, the command generates a default configuration file named adsb.json with two vehicles that fly in a box pattern over an airport.
Configuration file: Base properies
{"SystemId":1,// Mavlink System ID for ADSB Receiver"ComponentId":240,// Mavlink Component ID for ADSB Receiver"Ports":[],// Connection ports"Vehicles": [] // Vehicles and their routes}
Configuration file: Connections
You can add multiple ports at once. All packets will be routed by other ports.
"Ports": [ {"ConnectionString":"tcp://127.0.0.1:5760",// TCP client example"Name":"TCP client","IsEnabled":true,"PacketLossChance":0// this is for packet loss testing. Must be 0. }, {"ConnectionString":"tcp://127.0.0.1:7341?srv=true",// TCP server example"Name":"TCP server","IsEnabled":true,"PacketLossChance":0 }, {"ConnectionString":"serial:COM1?br=115200",// Serial on Windows example"Name":"Serial on Windows","IsEnabled":true,"PacketLossChance":0 }, {"ConnectionString":"serial:/dev/ttyS0?br=115200",// Serial on Linux example"Name":"Serial on Linux","IsEnabled":true,"PacketLossChance":0 }, {"ConnectionString":"udp:127.0.0.1:7341?rhost=127.0.0.1&rport=7342",// UDP example"Name":"UDP","IsEnabled":true,"PacketLossChance":0 } ]
Configuration file: Vehicles
Base properties are needed to fill ADSB_VEHICLE. You can add multiple route points and different velocities for each point. Velocity will be interpolated between points. Latitude and Longitude can be in DMS or angle format (see GeoPointLatitudeTest.cs and GeoPointLongitudeTest.cs). Altitude is in meters. Velocity is in three dimensions in m/s. It will be separated by ground and vertical velocity if altitude between two route points is different. Velocity must be greater than 0.
"Vehicles": [ {"CallSign":"PLANE1",// Call sign ADSB_VEHICLE (max 9 char)"Squawk":777,"UpdateRateMs":500,// Rate for sending ADSB_VEHICLE packets"IcaoAddress":1234,// 24 bit ICAO address (DEC format)"Route": [ // Vehicle mission points list (must be > 2) {"Lat":"55.305641",// Latitude at first point"Lon":"61.500886",// Longitude at first point"Alt":250.0,// Altitude at first point (m)"Velocity":10.0// Velocity at first point (m/s). Must be > 0 }, {"Lat":"55.362666",// Latitude at second point"Lon":"61.210466",// Latitude at second point"Alt":1000.0,// Altitude at second point (m)"Velocity":300.0// Velocity at second point (m/s). Must be > 0 } ] }, {"CallSign":"PLANE2",// Second vehicle example ...
Here's an example of ADSB utility being used with Asv.Drones.
Here's an example of ADSB utility being used with Mission Planner
CLI: Packet code generation
Generate C# code for packet serialization\deserialization
This command load XML file with mavlink packet definition
<messageid="0"name="HEARTBEAT"> <description>The heartbeat message shows that a system or component is present and responding. The type and autopilot fields (along with the message component id), allow the receiving system to treat further messages from this system appropriately (e.g. by laying out the user interface based on the autopilot). This microservice is documented at https://mavlink.io/en/services/heartbeat.html</description>
<fieldtype="uint8_t"name="type"enum="MAV_TYPE">Vehicle or component type. For a flight controller component the vehicle type (quadrotor, helicopter, etc.). For other components the component type (e.g. camera, gimbal, etc.). This should be used in preference to component id for identifying the component type.</field> <fieldtype="uint8_t"name="autopilot"enum="MAV_AUTOPILOT">Autopilot type / class. Use MAV_AUTOPILOT_INVALID for components that are not flight controllers.</field> <fieldtype="uint8_t"name="base_mode"enum="MAV_MODE_FLAG"display="bitmask">System mode bitmap.</field> <fieldtype="uint32_t"name="custom_mode">A bitfield for use for autopilot-specific flags</field> <fieldtype="uint8_t"name="system_status"enum="MAV_STATE">System status flag.</field> <fieldtype="uint8_t_mavlink_version"name="mavlink_version">MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version</field></message>
And generate CSharp file, like this:
/// <summary> /// HEARTBEAT /// </summary>publicclassHeartbeatPayload:IPayload {publicbyteGetMaxByteSize() =>9; // Sum of byte sized of all fields (include extended)publicbyteGetMinByteSize() =>9; // of byte sized of fields (exclude extended)publicintGetByteSize() {var sum =0; sum+=4; //CustomMode sum+=1; // Type sum+=1; // Autopilot sum+=1; // BaseMode sum+=1; // SystemStatus sum+=1; //MavlinkVersionreturn (byte)sum; }publicvoidDeserialize(refReadOnlySpan<byte> buffer) { CustomMode =BinSerialize.ReadUInt(ref buffer); Type = (MavType)BinSerialize.ReadByte(ref buffer); Autopilot = (MavAutopilot)BinSerialize.ReadByte(ref buffer); BaseMode = (MavModeFlag)BinSerialize.ReadByte(ref buffer); SystemStatus = (MavState)BinSerialize.ReadByte(ref buffer); MavlinkVersion = (byte)BinSerialize.ReadByte(ref buffer); }publicvoidSerialize(refSpan<byte> buffer) {BinSerialize.WriteUInt(ref buffer,CustomMode);BinSerialize.WriteByte(ref buffer,(byte)Type);BinSerialize.WriteByte(ref buffer,(byte)Autopilot);BinSerialize.WriteByte(ref buffer,(byte)BaseMode);BinSerialize.WriteByte(ref buffer,(byte)SystemStatus);BinSerialize.WriteByte(ref buffer,(byte)MavlinkVersion);/* PayloadByteSize = 9 */; } /// <summary> /// A bitfield for use for autopilot-specific flags /// OriginName: custom_mode, Units: , IsExtended: false /// </summary>publicuint CustomMode { get; set; } /// <summary> /// Vehicle or component type. For a flight controller component the vehicle type (quadrotor, helicopter, etc.). For other components the component type (e.g. camera, gimbal, etc.). This should be used in preference to component id for identifying the component type. /// OriginName: type, Units: , IsExtended: false /// </summary>publicMavType Type { get; set; } /// <summary> /// Autopilot type / class. Use MAV_AUTOPILOT_INVALID for components that are not flight controllers. /// OriginName: autopilot, Units: , IsExtended: false /// </summary>publicMavAutopilot Autopilot { get; set; } /// <summary> /// System mode bitmap. /// OriginName: base_mode, Units: , IsExtended: false /// </summary>publicMavModeFlag BaseMode { get; set; } /// <summary> /// System status flag. /// OriginName: system_status, Units: , IsExtended: false /// </summary>publicMavState SystemStatus { get; set; } /// <summary> /// MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version /// OriginName: mavlink_version, Units: , IsExtended: false /// </summary>publicbyte MavlinkVersion { get; set; } }
CLI: Ftp tree
This command provides a tree representation of all available files and directories on the drone's FTP server. It allows users to see the entire file structure in a hierarchical format, making it easy to browse and understand the file layout without navigating through individual folders.
This command is a file manager for interacting with a drone's file system via FTP. It allows users to browse directories, view files, and perform various file operations (e.g., download, rename, remove, etc.) in an interactive console environment. The tool is designed for MAVLink-based systems and provides an intuitive way to manage the droneβs files and directories.
This command extracts SDR (Software Defined Radio) data from a binary file and exports it into a CSV format. The SDR data is deserialized using the AsvSdrRecordDataLlzPayload class, and each record is written as a row in the CSV file with specific data fields such as altitude, signal strength, and power levels.
Features:
Reads binary SDR data from an input file.
Exports the data to a CSV file for further analysis or storage.
Provides a simple and automated way to convert SDR logs into human-readable tabular data.
This command listens to incoming MAVLink packets and displays statistics on the received messages. It allows monitoring of the communication between a ground station and an unmanned vehicle, showing information like the frequency of each type of message and the last few received packets.
Features:
Connects to a MAVLink stream via the provided connection string.
Displays statistics such as message ID, message frequency, and the last received packets
Continually updates the display with real-time data and allows the user to stop the process by pressing 'Q'.
This command is used to connect a vehicle with multiple ground stations, creating a hub that routes MAVLink messages between them. It provides flexible filtering options to log specific MAVLink messages, and can output the filtered data to a file. It supports multiple connections (UDP or serial) and can operate in silent mode (without printing to the console).
Features:
Connects to multiple MAVLink streams, allowing you to route messages between different systems (e.g., vehicle and multiple ground stations).
Supports filtering by system ID, message ID, message name (using regex), and message content (JSON text).
Can log filtered MAVLink messages to a file.
Allows disabling console output for silent operation.
Automatically propagates MAVLink messages between the connected links.
This command benchmarks the serialization and deserialization performance of MAVLink packets. It uses BenchmarkDotNet to measure the efficiency of the serialization process, focusing on how MAVLink packets are serialized and deserialized using spans.### Features:
Connects to multiple MAVLink streams, allowing you to route messages between different systems (e.g., vehicle and multiple ground stations).
Supports filtering by system ID, message ID, message name (using regex), and message content (JSON text).
Can log filtered MAVLink messages to a file.
Allows disabling console output for silent operation.
Automatically propagates MAVLink messages between the connected links.
Asv.Mavlink.Shell.exebenchmark-serialization
CLI: Devices info
This command shows info about the mavlink device and all other mavlink devices that are connected to it.
You may also use some parameters in the command to customise the output
Usage:devices-info [options...] [-h|--help] [--version]CommandthatshowsinfoaboutdevicesinthemavlinknetworkOptions:-cs|--connection-string<string>Theaddressoftheconnectiontothemavlinkdevice (Required)-i|--iterations<uint?>Stateshowmanyiterationsshouldtheprogramworkthrough (Default: null)-dt|--devices-timeout<uint> (in seconds) States the lifetime of a mavlink device that shows no Heartbeat (Default:10)-r|--refresh-rate<uint> (in ms) States how fast should the console be refreshed (Default:3000)
This command starts the console implementation of packet viewer.
Packet Viewer sets up the Mavlink router and waits for a connection using parameters provided in the command line. Launch a real drone or simulator to connect and start receiving packets from it. Once the connection is established, the packets will be displayed in the "Packets" section below.
It provides the following actions:
Search for the packet you need;
Adjust the size of the output;
Pause the output;
Safely terminate the execution;
By default, the viewer has no filters enabled and displays all received packets.
CLI: Generate fake diagnostic data
This command generates fake diagnostic with customizable frequency.
Asv.Mavlink.Shell.exegenerate-diagnostics
The program generates a default configuration file by default, but you can provide a custom configuration. Simply pass the path to your configuration file as a command-line parameter.
Usage:generate-diagnostics [options...] [-h|--help] [--version]Commandcreatesfakediagnosticsdatafromfileandopensamavlinkconnection.Options:-cfg|--cfg-path<string?>locationoftheconfigfileforthegenerator (Default: null)-r|--refresh-rate<uint> (in ms) States how fast should the console be refreshed (Default:2000)
Commandcreatesdiagnosticclientthatretrievesdiagnosticdata.Options:-cs|--connection-string<string>Theaddressoftheconnectiontothemavlinkdiagnosticserver (Required)-tsid|--target-system-id<byte>Server's system id (Required) -tcid|--target-component-id <byte> Server'scomponentid (Required)-r|--refresh-rate<uint> (in ms) States how fast should the console be refreshed (Default:1000)
CLI: Create Virtual Ftp server
This command creates ftp server and opens connection to it.
Asv.Mavlink.Shell.exerun-ftp-server
The program generates a default configuration file by default, but you can provide a custom configuration. Simply pass the path to your configuration file as a command-line parameter.