Implementation of the MAVLink protocol

npm install mavlink_ardupilotmega_v1.0
4 downloads in the last month

Generated MAVLink 1.0 protocol for ArdupilotMega

This module is auto-generated from the Javascript generator found in the main MAVLink repository. If you need implementations for other autopilots, check that out.

Initializing the parser

In your main node script, you need to include the generated parser and instantiate it; you also need some kind of binary stream library that can read/write binary data and emit an event when new data is ready to be parsed (TCP, UDP, serial port all have appropriate libraries in the npm-o-sphere). The connection's "data is ready" event is bound to invoke the MAVLink parser to try and extract a valid message.

 // requires Underscore.js, can use Winston for logging ,
 // see package.json for dependencies for the implementation
 var mavlink = require('mavlink_ardupilotmega_v1.0'), 
  net = require('net');

 // Instantiate the parser
 mavlinkParser = new mavlink();

 // Create a connection -- can be anything that can receive/send binary
 connection = net.createConnection(5760, '');

 // Provision the instantiated mavlinkParser object with the connection

 // When the connection issues a "got data" event, try and parse it
 connection.on('data', function(data) {

If the serial buffer has a valid MAVLink message, the message is removed from the buffer and parsed. Upon parsing a valid message, the MAVLink implementation emits two events: message (for any message) and the specific message name that was parsed, so you can listen for specific messages and handle them.

 // Attach an event handler for any valid MAVLink message
 mavlinkParser.on('message', function(message) {
  console.log('Got a message of any type!');

 // Attach an event handler for a specific MAVLink message
 mavlinkParser.on('HEARTBEAT', function(message) {
  console.log('Got a heartbeat message!');
  console.log(message); // message is a HEARTBEAT message

See the gotcha's and todo's section below for some important caveats. The below code is preliminary and will change to be more direct. At this point, the MAVLink parser doesn't manage any state information about the UAV or the connection itself, so a few fields need to be fudged, as indicated below.

Sending a MAVLink message is done by creating the message object, populating its fields, and packing/sending it across the wire. Messages are defined in the generated code, and you can look up the parameter list/docs for each message type there. For example, the message REQUEST_DATA_STREAM has this signature:

 mavlink.messages.request_data_stream = function(target_system, target_component, req_stream_id, req_message_rate, start_stop) //...

Creating the message is done like this:

 request = new mavlink.messages.request_data_stream(1, 1, mavlink.MAV_DATA_STREAM_ALL, 1, 1);
npm loves you