Setting a mission with Mavlink protocol in C

Hi everyone,

I’ve been writing a code to communicate between an Arduino and APM2.8 via Mavlink protocol. I used this library: https://github.com/mavlink/mavlink (The one which is created with the mavgenerator program).

Till now I’ve been able to code a heartbeat message send it through serial port and then decode it properly. My aim is to set a simple mission. Like this: 1- take off, 2-Go to a certain position, 3-RTH, 4-Land. I don’t have any idea how to continue from this point.

Can anyone give some light in this issue?

I will really appreciate your help.

Thank you!

The process is described here.
http://qgroundcontrol.org/mavlink/waypoint_protocol

And here’s an example in Python that should be pretty easily adapted.
http://www.colorado.edu/recuv/2015/05/25/mavlink-protocol-waypoints

Thank you so much for your answer jmack!

I found the first link very useful to understand the process to send waypoints to the APM through mavlink protocol. But I get new doubts:
-Timeouts, secure transfer data and all of this stuff has to be implemented by me?
-Where can I find this fuctions? (WAYPOINT_COUNT, WAYPOINT, WAYPOINT_REQUEST…). I’m using the library created as a result of executing mavgenerator.py file. I chose the ardupilotmega.xml file to generate the library, I wanna talk with a APM2.8 (Is the process correct?). Once I’ve done all of this, the library is created but there’s no trace of this functions.

Also, can the APM2.8 be used in GUIDED MODE. I’ve read APM2.8 works until version 3.2.1. The official documentation says that GUIDED MODE was added in the version 3.4… Someone told me that should be no problem with GUIDED MODE in APM2.8 but I’m not really sure of that. Could anybody clarify this? If it’s possible, could my aim be easier in GUIDED MODE? (take off, go to position, RTH, LAND).

Please I need help I’m so stuck!

Thank you!

The QGroundcontrol page actually has some old terminology for the messages. They are now MISSION_COUNT, MISSION_ITEM, MISSION_REQUEST. You should be able to encode and decode these messages just as you have the heartbeat messages.

As you can see in the second link, the procedure is to establish a list of waypoints internally, clear all existing waypoints (MISSION_CLEAR_ALL), send the number of waypoints you want to send (MISSION_COUNT), and then wait for the MISSION_REQUEST messages and acknowledge with the corresponding MISSION_ITEM messages.

You could also use GUIDED, but it’s not necessarily easier because you have to monitor the autopilot during flight to determine when to send the next command. GUIDED is available for APM on 3.2.1, it’s GUIDED_NoGPS that was introduced in 3.4.

Thank you jmack,

I found the mission functions to encode and decode you told me in your previous post. I couldn’t find them due to the old terminology. Now I can encode and decode and I should be able to implement the waypoint protocol you described me, but what’s needed to stablish a full conversation with the APM? I mean, can it be like this? 1- HEARTBEAT (How frequently?), 2- THE WHOLE MISSION PROTOCOL. Or APM needs other messages in order to plan a mission?

The question is: What’s the most simple execution to plan a mission in an APM?

Thank you so much!

Hi everyone,

I’ve been working on the C code to set missions with Mavlink protocol with no success for the moment.
I’m stuck in new doubts:
1.- Is it possible to arm the ArduCopter through Mavlink protocol?
2.- Once I get the mission planned is there any function to start the mission, or is only possible switching ArduCopter mode to AUTO through the radio transmitter?

Thanks for your answers!

You can arm through mavlink. First, you have to be in an arm-able mode, and STABILIZE or GUIDED work well for this. If you’re in AUTO when you try to arm, the command will be rejected. Once in the right mode, you use the COMMAND_LONG message with an arming code (MAV_CMD_ARM_DISARM with param1 set to 1) to arm. Then you use a SET_MODE message to change to AUTO mode. All of this has to be done within a few seconds, or the copter will automatically disarm.

Docs on those messages below.
COMMAND_LONG
SET_MODE
MAV_CMD enum and other definitions

Sorry I couldn’t find a good code example, but hopefully this points you in the right direction.

Thank you so much for your answer jmack! You’re so helpful for me.

I’ll try this messages. I hope to get it working soon.

I’m sorry to ask such maybe trivial questions but I’ve more doubts about that. It is so difficult to found information or examples on the internet.
1.- How can I enter the last waypoint as RTH?
2.- I don’t really understand the arguments of the message MISSION_ITEM. What kind of values should be there?

Thank you in advance!

Sorry for the delay, I missed your reply.

For RTH, there’s a “RETURN_TO_LAUNCH” mission command. arducopter docs link

The mission item message is described here: MISSION_ITEM. The values depend on the type of mission item, and each one is described in the MAV_CMD enum. (https://pixhawk.ethz.ch/mavlink/)

Any particular mission item that’s giving trouble?

Thank you for your answer jmack!

Okey I get it! There are some particular mission items which I don’t really understand the arguments:

  • MAV_CMD_NAV_TAKEOFF:

  • Param1 and 4-> What does it mean minimum pitch? And yaw Angle? I want the arducopter to takeoff straight up.

  • Param2,3 -> Empty? Zeros?

  • Param5,6,7 -> What coordinates? (I only want to takeoff here).

  • MAV_CMD_NAV_WAYPOINT:

  • Param3 -> What does it mean?

  • Param4 -> Yaw angle? Why?

I’m so grateful for your answers.

Thank you!