TerrainMission.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. handle checking mission points for terrain data
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include <AP_Common/AP_Common.h>
  18. #include <AP_Math/AP_Math.h>
  19. #include <AP_Rally/AP_Rally.h>
  20. #include <GCS_MAVLink/GCS_MAVLink.h>
  21. #include <GCS_MAVLink/GCS.h>
  22. #include "AP_Terrain.h"
  23. #include <AP_GPS/AP_GPS.h>
  24. #if AP_TERRAIN_AVAILABLE
  25. extern const AP_HAL::HAL& hal;
  26. /*
  27. check that we have fetched all mission terrain data
  28. */
  29. void AP_Terrain::update_mission_data(void)
  30. {
  31. if (last_mission_change_ms != mission.last_change_time_ms() ||
  32. last_mission_spacing != grid_spacing) {
  33. // the mission has changed - start again
  34. next_mission_index = 1;
  35. next_mission_pos = 0;
  36. last_mission_change_ms = mission.last_change_time_ms();
  37. last_mission_spacing = grid_spacing;
  38. }
  39. if (next_mission_index == 0) {
  40. // nothing to do
  41. return;
  42. }
  43. uint16_t pending, loaded;
  44. get_statistics(pending, loaded);
  45. if (pending && AP::gps().status() >= AP_GPS::GPS_OK_FIX_3D) {
  46. // wait till we have fully filled the current set of grids
  47. return;
  48. }
  49. // don't do more than 20 waypoints at a time, to prevent too much
  50. // CPU usage
  51. for (uint8_t i=0; i<20; i++) {
  52. // get next mission command
  53. AP_Mission::Mission_Command cmd;
  54. if (!mission.read_cmd_from_storage(next_mission_index, cmd)) {
  55. // nothing more to do
  56. next_mission_index = 0;
  57. return;
  58. }
  59. // we only want nav waypoint commands. That should be enough to
  60. // prefill the terrain data and makes many things much simpler
  61. while ((cmd.id != MAV_CMD_NAV_WAYPOINT &&
  62. cmd.id != MAV_CMD_NAV_SPLINE_WAYPOINT) ||
  63. (cmd.content.location.lat == 0 && cmd.content.location.lng == 0)) {
  64. next_mission_index++;
  65. if (!mission.read_cmd_from_storage(next_mission_index, cmd)) {
  66. // nothing more to do
  67. next_mission_index = 0;
  68. next_mission_pos = 0;
  69. return;
  70. }
  71. }
  72. // we will fetch 5 points around the waypoint. Four at 10 grid
  73. // spacings away at 45, 135, 225 and 315 degrees, and the
  74. // point itself
  75. if (next_mission_pos != 4) {
  76. cmd.content.location.offset_bearing(45+90*next_mission_pos, grid_spacing.get() * 10);
  77. }
  78. // we have a mission command to check
  79. float height;
  80. if (!height_amsl(cmd.content.location, height, false)) {
  81. // if we can't get data for a mission item then return and
  82. // check again next time
  83. return;
  84. }
  85. next_mission_pos++;
  86. if (next_mission_pos == 5) {
  87. #if TERRAIN_DEBUG
  88. hal.console->printf("checked waypoint %u\n", (unsigned)next_mission_index);
  89. #endif
  90. // move to next waypoint
  91. next_mission_index++;
  92. next_mission_pos = 0;
  93. }
  94. }
  95. }
  96. /*
  97. check that we have fetched all rally terrain data
  98. */
  99. void AP_Terrain::update_rally_data(void)
  100. {
  101. const AP_Rally *rally = AP::rally();
  102. if (rally == nullptr) {
  103. return;
  104. }
  105. if (last_rally_change_ms != rally->last_change_time_ms() ||
  106. last_rally_spacing != grid_spacing) {
  107. // a rally point has changed - start again
  108. next_rally_index = 1;
  109. last_rally_change_ms = rally->last_change_time_ms();
  110. last_rally_spacing = grid_spacing;
  111. }
  112. if (next_rally_index == 0) {
  113. // nothing to do
  114. return;
  115. }
  116. uint16_t pending, loaded;
  117. get_statistics(pending, loaded);
  118. if (pending && AP::gps().status() >= AP_GPS::GPS_OK_FIX_3D) {
  119. // wait till we have fully filled the current set of grids
  120. return;
  121. }
  122. while (true) {
  123. // get next rally point
  124. struct RallyLocation rp;
  125. if (!rally->get_rally_point_with_index(next_rally_index, rp)) {
  126. // nothing more to do
  127. next_rally_index = 0;
  128. return;
  129. }
  130. Location loc;
  131. loc.lat = rp.lat;
  132. loc.lng = rp.lng;
  133. float height;
  134. if (!height_amsl(loc, height, false)) {
  135. // if we can't get data for a rally item then return and
  136. // check again next time
  137. return;
  138. }
  139. #if TERRAIN_DEBUG
  140. hal.console->printf("checked rally point %u\n", (unsigned)next_rally_index);
  141. #endif
  142. // move to next rally point
  143. next_rally_index++;
  144. }
  145. }
  146. #endif // AP_TERRAIN_AVAILABLE