MissionItemProtocol_Waypoints.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Implementation details for transfering waypoint information using
  3. the MISSION_ITEM protocol to and from a GCS.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "MissionItemProtocol_Waypoints.h"
  16. #include <AP_Logger/AP_Logger.h>
  17. #include <AP_Mission/AP_Mission.h>
  18. #include "GCS.h"
  19. MAV_MISSION_RESULT MissionItemProtocol_Waypoints::append_item(const mavlink_mission_item_int_t &mission_item_int)
  20. {
  21. // sanity check for DO_JUMP command
  22. AP_Mission::Mission_Command cmd;
  23. const MAV_MISSION_RESULT res = AP_Mission::mavlink_int_to_mission_cmd(mission_item_int, cmd);
  24. if (res != MAV_MISSION_ACCEPTED) {
  25. return res;
  26. }
  27. if (cmd.id == MAV_CMD_DO_JUMP) {
  28. if ((cmd.content.jump.target >= item_count() && cmd.content.jump.target > request_last) || cmd.content.jump.target == 0) {
  29. return MAV_MISSION_ERROR;
  30. }
  31. }
  32. if (!mission.add_cmd(cmd)) {
  33. return MAV_MISSION_ERROR;
  34. }
  35. return MAV_MISSION_ACCEPTED;
  36. }
  37. bool MissionItemProtocol_Waypoints::clear_all_items()
  38. {
  39. return mission.clear();
  40. }
  41. MAV_MISSION_RESULT MissionItemProtocol_Waypoints::complete(const GCS_MAVLINK &_link)
  42. {
  43. _link.send_text(MAV_SEVERITY_INFO, "Flight plan received");
  44. AP::logger().Write_EntireMission();
  45. return MAV_MISSION_ACCEPTED;
  46. }
  47. MAV_MISSION_RESULT MissionItemProtocol_Waypoints::get_item(const GCS_MAVLINK &_link,
  48. const mavlink_message_t &msg,
  49. const mavlink_mission_request_int_t &packet,
  50. mavlink_mission_item_int_t &ret_packet)
  51. {
  52. if (packet.seq != 0 && // always allow HOME to be read
  53. packet.seq >= mission.num_commands()) {
  54. // try to educate the GCS on the actual size of the mission:
  55. mavlink_msg_mission_count_send(_link.get_chan(),
  56. msg.sysid,
  57. msg.compid,
  58. mission.num_commands(),
  59. MAV_MISSION_TYPE_MISSION);
  60. return MAV_MISSION_ERROR;
  61. }
  62. AP_Mission::Mission_Command cmd;
  63. // retrieve mission from eeprom
  64. if (!mission.read_cmd_from_storage(packet.seq, cmd)) {
  65. return MAV_MISSION_ERROR;
  66. }
  67. if (!AP_Mission::mission_cmd_to_mavlink_int(cmd, ret_packet)) {
  68. return MAV_MISSION_ERROR;
  69. }
  70. // set packet's current field to 1 if this is the command being executed
  71. if (cmd.id == (uint16_t)mission.get_current_nav_cmd().index) {
  72. ret_packet.current = 1;
  73. } else {
  74. ret_packet.current = 0;
  75. }
  76. // set auto continue to 1
  77. ret_packet.autocontinue = 1; // 1 (true), 0 (false)
  78. ret_packet.command = cmd.id;
  79. return MAV_MISSION_ACCEPTED;
  80. }
  81. uint16_t MissionItemProtocol_Waypoints::item_count() const {
  82. return mission.num_commands();
  83. }
  84. uint16_t MissionItemProtocol_Waypoints::max_items() const {
  85. return mission.num_commands_max();
  86. }
  87. MAV_MISSION_RESULT MissionItemProtocol_Waypoints::replace_item(const mavlink_mission_item_int_t &mission_item_int)
  88. {
  89. AP_Mission::Mission_Command cmd;
  90. const MAV_MISSION_RESULT res = AP_Mission::mavlink_int_to_mission_cmd(mission_item_int, cmd);
  91. if (res != MAV_MISSION_ACCEPTED) {
  92. return res;
  93. }
  94. // sanity check for DO_JUMP command
  95. if (cmd.id == MAV_CMD_DO_JUMP) {
  96. if ((cmd.content.jump.target >= item_count() && cmd.content.jump.target > request_last) || cmd.content.jump.target == 0) {
  97. return MAV_MISSION_ERROR;
  98. }
  99. }
  100. if (!mission.replace_cmd(cmd.index, cmd)) {
  101. return MAV_MISSION_ERROR;
  102. }
  103. return MAV_MISSION_ACCEPTED;
  104. }
  105. void MissionItemProtocol_Waypoints::timeout()
  106. {
  107. link->send_text(MAV_SEVERITY_WARNING, "Mission upload timeout");
  108. }
  109. void MissionItemProtocol_Waypoints::truncate(const mavlink_mission_count_t &packet)
  110. {
  111. // new mission arriving, truncate mission to be the same length
  112. mission.truncate(packet.count);
  113. }