Skip to content

MAVLink

Requires AMC 2.6.0+

MavlinkSubscription and Mavlink were added in AMC 2.6.0. On older versions a plugin using MavlinkSubscription fails to load, with an error in Settings → Plugins, and calls to Mavlink fail when made.

MavlinkSubscription passes one MAVLink message of one vehicle to your QML, and the Mavlink singleton sends messages and commands. Both work by message name, for the messages compiled into AMC and for those of a dialect your plugin ships.

Import: import QGroundControl.Plugin

Quick start

import QtQuick
import QGroundControl
import QGroundControl.Plugin

Item {
    id: root

    property var vehicle: QGroundControl.multiVehicleManager.activeVehicle
    property int autopilotType: -1

    MavlinkSubscription {
        vehicle: root.vehicle
        message: "HEARTBEAT"
        maxRate: 1
        onReceived: (values, systemId, componentId) => {
            // Logs a change, not every heartbeat
            if (componentId === 1 && values.type !== root.autopilotType) {
                root.autopilotType = values.type
                PluginManager.log("MyPlugin: autopilot reports MAV_TYPE " + values.type)
            }
        }
    }

    function requestCapabilities() {
        // MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES to the autopilot
        Mavlink.sendCommand(vehicle, 1, 520, [1], (result, noResponse) => {
            PluginManager.log("MyPlugin: " + (noResponse ? "no response" : "MAV_RESULT " + result))
        })
    }
}

MavlinkSubscription

A QML type. Create one per message and vehicle.

Properties

Property Type Writable Default Description
vehicle Vehicle Yes null The vehicle whose messages are passed on. Bind it, e.g. to QGroundControl.multiVehicleManager.activeVehicle.
message string Yes "" The message name, e.g. "HEARTBEAT". An unknown name logs a warning.
maxRate real Yes 0 At most this many notifications per second, counted separately for each sending component. Messages in between are dropped. 0 passes on every message.

Signals

Signal Parameters Description
received(values, systemId, componentId) object, int, int A message was passed on. values maps field names to values.

Which messages arrive

  • Only messages the vehicle accepts are passed on, mostly those from its own system id. A device on the vehicle with a different system id is not seen.
  • A vehicle that disconnects and comes back is a new object. A binding follows it; a vehicle assigned once in JS turns null (and vehicleChanged fires), and the subscription stays idle.
  • For several vehicles, create one subscription per vehicle, e.g. with an Instantiator over QGroundControl.multiVehicleManager.vehicles.

Choosing maxRate

Notifications are spaced 1 / maxRate seconds apart on average, with a quarter of that as tolerance, so a stream at exactly maxRate, e.g. a 1 Hz HEARTBEAT with maxRate: 1, delivers every message despite jitter.


A singleton. Messages and commands go out with AMC's own system and component id.

Methods

Method Parameters Description
send(vehicle, name, fields, allLinks) Vehicle, string, object, bool Sends message name with fields to vehicle. Returns true if it went out on at least one link. allLinks (default false) sends on every link of the vehicle instead of only its priority link.
sendCommand(vehicle, componentId, command, params, callback) Vehicle, int, int, array, function Sends a COMMAND_LONG. Returns true if it was queued. callback(result, noResponse) is optional.

send

if (vehicle) {
    Mavlink.send(vehicle, "EXAMPLE_PING", { target_system: vehicle.id, counter: 1, text: "hello" })
}
  • Fields left out are sent as zero.
  • Refused, with the reason logged, for an unknown field name or a value that does not fit its field: out of range, a fraction for an integer field, anything but a string for a char[] field or a string longer than it, anything but an array for another array field, or too many elements.
  • COMMAND_LONG and COMMAND_INT are refused: use sendCommand, so AMC can match the result.
  • The mission transfer messages (MISSION_COUNT, MISSION_ITEM, MISSION_ITEM_INT, MISSION_REQUEST, MISSION_REQUEST_INT, MISSION_REQUEST_LIST, MISSION_REQUEST_PARTIAL_LIST, MISSION_WRITE_PARTIAL_LIST, MISSION_CLEAR_ALL, MISSION_ACK) and FILE_TRANSFER_PROTOCOL are refused: the vehicle answers them to AMC, inside whatever mission or file transfer AMC has running.
  • Message ids above 255 cannot be sent on a MAVLink 1 link.

sendCommand

Mavlink.sendCommand(vehicle, 1, 520, [1], (result, noResponse) => {
    PluginManager.log(noResponse ? "no response" : "MAV_RESULT " + result)
})
  • componentId must be 1 to 255; the ack to a command for component 0 cannot be matched to it.
  • params holds up to seven numbers, param1 first. Missing params are 0, and NaN is sent as is. A param that is not a number, or is beyond float range, refuses the command.
  • The command is not retried. AMC waits for its ack for the normal 3 seconds (6 on a high-latency link).
  • callback is called once, always after sendCommand has returned: result is the MAV_RESULT, and noResponse is true if the vehicle never answered. It is not called when sendCommand returns false.
  • The command shares AMC's queue. sendCommand returns false while the same command is pending for that component, from the plugin or from AMC, and AMC's own copy is refused while the plugin's is pending.
  • Refused because their results could not be matched to the callback: commands AMC may supersede or send twice (e.g. MAV_CMD_DO_SET_MODE, MAV_CMD_DO_REPOSITION), MAV_CMD_REQUEST_MESSAGE, and the video streaming and stop-tracking commands the camera cancels.
  • A command AMC also sends without waiting for the ack, e.g. for camera or gimbal control, can complete the callback with the result of AMC's.

Field values

MAVLink type QML value
integers, float, double number
char[n] string, up to the first NUL
other arrays array of numbers
char its character code when received; a one-character string or a number when sent

int64_t and uint64_t lose precision above 2^53, the largest integer a JS number holds exactly. time_usec values are well below that.

See the MavlinkDialect example for a complete plugin.