Mavlink (developer page)

This page gives you a guide on how to implement the handling of (custom) mavlink messages on the px4 platform and on how to send mavlink messages from your own app via the mavlink app to the groundstation.

Implement handling of a message

The necessary steps are shown here with the example of the COMMAND_LONG mavlink message.

  1. First search the definition of your message here. If you don't find the message you can also check the header files in ~/src/mavlink/include/v1.0/common/
  2. Open the file Firmware/apps/mavlink/mavlink.c
  3. Look for the handleMessage function
  4. At the end insert the following code (example!):
if (msg->msgid == MAVLINK_MSG_ID_COMMAND_LONG) {
 
	mavlink_command_long_t cmd_mavlink;
	mavlink_msg_command_long_decode(msg, &cmd_mavlink);
 
	if (cmd_mavlink.target_system == mavlink_system.sysid
		&& ((cmd_mavlink.target_component == mavlink_system.compid)
		|| (cmd_mavlink.target_component == MAV_COMP_ID_ALL))) {
 
		/* Copy the content of mavlink_command_long_t cmd_mavlink into command_t cmd */
		vcmd.param1 = cmd_mavlink.param1;
		vcmd.param2 = cmd_mavlink.param2;
		vcmd.param3 = cmd_mavlink.param3;
		vcmd.param4 = cmd_mavlink.param4;
		vcmd.param5 = cmd_mavlink.param5;
		vcmd.param6 = cmd_mavlink.param6;
		vcmd.param7 = cmd_mavlink.param7;
		vcmd.command = cmd_mavlink.command;
		vcmd.target_system = cmd_mavlink.target_system;
		vcmd.target_component = cmd_mavlink.target_component;
		vcmd.source_system = msg->sysid;
		vcmd.source_component = msg->compid;
		vcmd.confirmation =  cmd_mavlink.confirmation;
 
		/* check if topic is advertised */
		if (cmd_pub <= 0) {
			cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);
		}
		/* publish */
		orb_publish(ORB_ID(vehicle_command), cmd_pub, &vcmd);
	}
}

The important parts are the two if-commands. The first checks the type of message, the second makes sure that the message is actually intended for the system on which the code is running. In the above code the values of the message are stored in shared memory and are then made available for other apps. You can read more about this here.


Mavlink proves an easy way to send “mavlink log” messages from your app to the groundstation. The following 3 functions are provided:

mavlink_log_emergency(_fd, _text)
mavlink_log_critical(_fd, _text)
mavlink_log_info(_fd, _text)

Example

#include <mavlink/mavlink_log.h>
static int mavlink_fd;
mavlink_fd = open(MAVLINK_LOG_DEVICE, 0);
mavlink_log_info(mavlink_fd, "[myapp] Hello, this is myapp talking.");

Other functions

The mavlink app is also responsible for waypoint management and the updating of parameters.

Waypoint management

Waypoints set in the groundstation are handled by the mavlink app. Furthermore, the mavlink app keeps track of the current vehicle position relative to the current active waypoint and checks whether the waypoint was reached. The current active waypoint is the input for the position controller

Parameters

As described in the parameters the mavlink app is responsible for receiving parameter values from the ground station.

Translations of this page:


Quick Links

QR Code: URL of current page