ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

MAVLink Mission transfers are not transactional as they ought to be

Open dongfang opened this issue 13 years ago • 9 comments

When uploading a mission to APM over a non perfect link, sometimes it fails. After a failed upload, APM may have parts of an old mission and parts of a new one stored. MAVLink states its waypoint protocol is transactional: "The protocol consists of different transactions, each transaction is completed successfully or fails in a way that the previous state of the waypoint list on the receiving side is unchanged" (http://qgroundcontrol.org/mavlink/waypoint_protocol) . However the current implementation is not. This demo program http://dongfang.ch/MissionUploadBug.tar.gz (adapt serial port name in source file to match, compile and run under linux (libpthread required)) reproduces the problem by first uploading a short mission in Germany successfully, and then failing at uploading a short mission in Brazil. The result is a mission from Brazil to Germany. This bug has cost me a plane..

dongfang avatar Mar 06 '13 20:03 dongfang

This bug happened to me in flight before, luckily there has no damage to the plane.

The problem is that the new received mission is not stored in a buffer before loading into the current mission. I don't think there is enough RAM to do this either.

Here is my analysis of what is going on. First lets look up the mavlink waypoint transaction: Wapoints The Waypoint count message is handled by the code code, which sets the total waypoint number before receiving the waypoints.

    case MAVLINK_MSG_ID_MISSION_COUNT:
    {
       ...
        // start waypoint receiving
        if (packet.count > MAX_WAYPOINTS) {
            packet.count = MAX_WAYPOINTS;
        }
        g.command_total.set_and_save(packet.count - 1);
       ...
        break;
    }

Then each subsequent mission item received is checked for the correct order and is added to the current mission. When all waypoints an acknowledgment is sent.

    // XXX receive a WP from GCS and store in EEPROM
    case MAVLINK_MSG_ID_MISSION_ITEM: {
...
        set_cmd_with_index(tell_command, packet.seq);
...
        if (waypoint_request_i > waypoint_request_last) {
            mavlink_msg_mission_ack_send(chan, msg->sysid, msg->compid, result);

            send_text_P(SEVERITY_LOW, PSTR("flight plan received"));
            waypoint_receiving = false;     
...
        }

This works well if there is a good link, but in the case of dropping a MISSION_ITEM packet the transaction gets corrupted. But since the command_total is already set , during the flight, the mission the commands after the dropped packet aren't updated, and anything that is in memory gets loaded when a new command is requested (from the previous mission, or could even be corrupted data).

I will follow up with a fix soon.

arthurbenemann avatar Mar 08 '13 18:03 arthurbenemann

Hi, Thanks for following up on this. Well maybe one could use a ring structure in eeprom and store the new mission after the existing one, possibly wrapping around the end of the ring. This will solve the problem in the cases where the sum of the lenghts of new and old mission is no greater than the available eeprom size (vast majority of cases), Of course ideally a solution should work in all cases (temporary storage somewhere).

dongfang avatar Mar 08 '13 19:03 dongfang

I fixed the bug, now when a mission start to be be transferred the old one is forgotten.

If in the middle of the transmission the connection fails, the updated mission runs until the last received waypoint. I think this is a good behavior for the code.

arthurbenemann avatar Mar 08 '13 19:03 arthurbenemann

I think it would be less surprising (and closer to the desired transactional nature) if a failure did not cause a partial mission to be executed. How about just clearing the mission and signaling an error if for any reason an complete mission isn't sent?

wiseman avatar Mar 08 '13 21:03 wiseman

The error is already signalized on mission planner, since it no longer receive acknowledgements the transaction times out and an error shows up.

arthurbenemann avatar Mar 08 '13 21:03 arthurbenemann

I tried to reproduce and it seems the effort to solve it has worked. Well it is still not transactional but a correct write of a partial mission is better than a mix up of 2 missions and there is no buffer space in APM to implement transactions. Ready to close from me.

dongfang avatar Jul 08 '13 23:07 dongfang

@dongfang
The code has not been pulled to the main branch yet, so I don't think the issue can be closed yet. I talked with @tridge about it, but it needs more testing since the fix I did my not work with partial mission upgrades.

arthurbenemann avatar Jul 08 '13 23:07 arthurbenemann

OK sorry, I ran my regression test (link in original comment above) against 2.73 and there it appeared to be (well enough) fixed. Reopening.

dongfang avatar Jul 09 '13 06:07 dongfang

see #147

squilter avatar Sep 12 '14 21:09 squilter