Planning missions via Mavlink

Thank you for your reply jmack.

I don’t really understand how GPS_RAW works. Do I need to send the command GPS_RAW_INT and then wait for answer? I would read the variables fix_type, lat,lon and alt. No problem. But For me it doesn’t make any sense to send GPS_RAW_INT with the following parameters:

mavlink_msg_gps_raw_int_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, uint64_t time_usec, uint8_t fix_type, int32_t lat, int32_t lon, int32_t alt, uint16_t eph, uint16_t epv, uint16_t vel, uint16_t cog, uint8_t satellites_visible)

Lat, lon, alt -> I don’t know them, I’m asking for them.
fix_type -> the same, I don’t know, I’m asking for it.

And the same for the others, I find that all the parameters of the function are what I would like to know…I’m sorry if this is such a trivial question…

Apart from that, Is it possible to plan missions in the flight time? Once I’m flying?

Thank you.

Either GPS_RAW or GPS_RAW_INT should be in the telemetry stream if your system has a GPS attached. You should be able to wait for the autopilot to send them just like the HEARTBEAT packets. You should not have to send anything new.

I don’t know which one is sent by default, but I think it’s actually GPS_RAW_INT. The difference is one is “interpolated” so it can be sent at a different message rate from the actual GPS data.

Of course, there’s a chance your system has a non-standard configuration and those messages are not being sent by the autopilot. But we can address that later if you’re not seeing either GPS_RAW or GPS_RAW_INT in the autopilot telemetry.

So I think it could be the last option. I’m not receiving GPS_RAW_INT. And GPS_RAW doesn’t let me compile the code. It tells me it doesn’t exists. Can I configure the APM to send the GPS_RAW_INT messages?

Thank you!

What is the APM’s parameter SR1_EXT_STAT set to? If it’s 0, set it to something like 4 (4 times per second).

I got it set to 2… It should be working. Is there an other way to ask for the home position? I don’t want to use it as a home backup. I’m planning to store and compare it with a list of coordinates and make the Copter decide where to go on its own depending on the proximity compared to the home position.

You can ask for the home position by downloading the current mission immediately after arming the copter. The home location will be waypoint 0. This is done by sending a MISSION_REQUEST_LIST, waiting for a MISSION_COUNT, and sending a series of MISSION_REQUEST as you receive MISSION_ITEM replies.

It’s much simpler to get GPS_RAW_INT working, so I’d try and debug that. What messages (besides HEARTBEAT) are you getting in the telemetry stream?

Yes, it’s so much simpler GPS_RAW_INT method. I’m only listening for HEARTBEAT and GPS_RAW_INT. What other messages I should be receiving?

I don’t have the specific list, but there are 20-30 different messages as part of the default telemetry. You might try printing out the msgid of each decoded mavlink packet as they come through.

Are you using the “ardupilotmega” dialect when generating your C library? If not, that might also explain why you get occasional missed messages when sending a mission.

I’ve listened for msgid and I’m only receiving 0 (MAVLINK_MSG_ID_HEARTBEAT), 30 (MAVLINK_MSG_ID_ATTITUDE) and 253 (MAVLINK_MSG_ID_STATUSTEXT). There’s no trace of MAVLINK_MSG_ID_GPS_RAW_INT… Is there any parameter to activate it?

Yes, I’m using the “ardupilotmega” to generate the C library.

The parameter to activate it has already been set, SR1_EXT_STAT. But there’s more than one way to do this so maybe this other way will work.

You could try sending the following near the start of your code:

// mavlink_msg_request_data_stream_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, uint8_t target_system, uint8_t target_component, uint8_t req_stream_id, uint16_t req_message_rate, uint8_t start_stop)
mavlink_msg_request_data_stream_pack(255, 190, &msg, 1, 1, MAV_DATA_STREAM_EXTENDED_STATUS, 2, 1);

This will tell the APM to send GPS_RAW_INT at 2Hz.

I tried the previous command (REQUEST_DATA_STREAM) with no success. Once I run this command I start receiving new telemetry messages but there’s no trace of GPS_RAW_INT. I’ve tried to change the req_stream_id argument to MAV_DATA_STREAM_ALL and MAV_DATA_STREAM_RAW_SENSORS. With this change I receive different messages but not the one I’m waiting for. Also I’ve read REQUEST_DATA_STREAM is deprecated so I’ve tried SET_MESSAGE_INTERVAL instead, which seems to be the substitute. I didin’t get the thing working.

This is what I’ve tried: (Should it work?)

  //mavlink_msg_message_interval_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,uint16_t message_id, int32_t interval_us)

  mavlink_msg_message_interval_pack(255, 190, &msg, 24, 500000);

message_id = 24 (GPS_RAW_INT)

Thank you for your help!

REQUEST_DATA_STREAM is the proper way to request data from the APM. SET_MESSAGE_INTERVAL is not supported (yet?).

Maybe it’s worth seeing if GPS is working properly by connecting your ground station (over USB)? If you see the number of satellites and/or HDOP data then it should be working. In mission planner, this info is at the lower left corner of the map window.

I would also try turning on MAV_DATA_STREAM_POSITION or MAV_DATA_STREAM_ALL and seeing if GLOBAL_POSITION_INT (id=33) messages come through.

YES! It’s working! Thank you so much jmack!

There’s no trace of GPS_RAW_INT but GLOBAL_POSITION_INT. I checked the coordinates and they are correct. So strange the GPS_RAW_INT issue.

An other question, can a mission be planned on the flight time? I think that’s possible as GUIDED MODE let’s you control the Copter on the flight time, but I wanna be sure not to crash it and throw away all the work.

You’ve been so useful to me.

Yeah, that GPS_RAW_INT issue is worth tracking down eventually but GLOBAL_POSITION_INT should work for now. The drawback to GLOBAL_POSITION_INT is that you won’t know how good the GPS fix is, so you may use bad coordinates if you grab them prior to getting a good GPS lock.

A mission may be planned at flight time. Just note that in AUTO, you cannot change the current waypoint, only future ones. So you’ll need to send waypoint 1 before switching to AUTO mode, and send all other waypoints before they become active. This should also apply to the HOME waypoint, you’d want to set it before reaching waypoint 3 (RTL) if you were to change it mid flight.

If you do need to change a waypoint while it’s active, you’d need to toggle another waypoint active and then switch back to the intended waypoint using MISSION_SET_CURRENT after you update the waypoint. If you want to update the current waypoint frequently during flight, it’s best to use GUIDED (and you can switch between AUTO and GUIDED mid-flight if that’s what you need).

I can tell you’re becoming a pro at this now. :slight_smile:

Thank you so much jmack!

I’ll have to try the GUIDED mode and use MISSION_SET_CURRENT. I’ll take that into account.

I’m really grateful for your help. You are worth much. :slight_smile:

Last doubt I promise (well, I can’t promise that). I will receive automatically MISSION_ITEM_REACHED once a mission item is done? It needs to be activated?

Also, I’ve just checked that lat and lon variables of GLOBAL_POSITION_INT messages are zero until I get the GPS lock. Enough for me!

You should get MISSION_ITEM_REACHED. You’ll also get a STATUSTEXT when reaching a waypoint. Neither of those needs to be activated, they are automatic.

Be careful using zeroes to check for good GPS. Zeros indicate bad GPS, but non-zeroes don’t indicate good GPS. In particular, when you first start acquiring, it will go from zeroes to values hundreds of km away before finally giving good values. If nothing else, wait for 10-20 seconds of non-zero values before using the data, as the first non-zero GPS values are almost surely bad.

That’s what’s good about GPS_RAW_INT, it has a fix_type flag that tells you if the data is valid.

I forgot to mention, you will probably also get MISSION_CURRENT messages regularly. Definitely check for those if MISSION_ITEM_REACHED doesn’t work.

(Just a quick suggestion @sergi_torrabadella: whenever you quote code or logfiles, you might want to quote it as `pre-formatted text` by enclosing it with ``, or as a

> blockquote by prepending it with > ,

just to make things more readable…)

Sergi, I am hitting the same issue as you were with updating waypoints using an arduino to a pixhawk. Can you share the code you used to get the flight plan updated to the APM using an arduino?