TerrainUtil.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 disk IO for terrain code
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include <AP_Common/AP_Common.h>
  18. #include <AP_Math/AP_Math.h>
  19. #include <GCS_MAVLink/GCS_MAVLink.h>
  20. #include <GCS_MAVLink/GCS.h>
  21. #include "AP_Terrain.h"
  22. #if AP_TERRAIN_AVAILABLE
  23. #include <AP_Filesystem/AP_Filesystem.h>
  24. extern const AP_HAL::HAL& hal;
  25. /*
  26. calculate bit number in grid_block bitmap. This corresponds to a
  27. bit representing a 4x4 mavlink transmitted block
  28. */
  29. uint8_t AP_Terrain::grid_bitnum(uint8_t idx_x, uint8_t idx_y)
  30. {
  31. ASSERT_RANGE(idx_x,0,27);
  32. ASSERT_RANGE(idx_y,0,31);
  33. uint8_t subgrid_x = idx_x / TERRAIN_GRID_MAVLINK_SIZE;
  34. uint8_t subgrid_y = idx_y / TERRAIN_GRID_MAVLINK_SIZE;
  35. ASSERT_RANGE(subgrid_x,0,TERRAIN_GRID_BLOCK_MUL_X-1);
  36. ASSERT_RANGE(subgrid_y,0,TERRAIN_GRID_BLOCK_MUL_Y-1);
  37. return subgrid_y + TERRAIN_GRID_BLOCK_MUL_Y*subgrid_x;
  38. }
  39. /*
  40. given a grid_info check that a given idx_x/idx_y is available (set
  41. in the bitmap)
  42. */
  43. bool AP_Terrain::check_bitmap(const struct grid_block &grid, uint8_t idx_x, uint8_t idx_y)
  44. {
  45. uint8_t bitnum = grid_bitnum(idx_x, idx_y);
  46. return (grid.bitmap & (((uint64_t)1U)<<bitnum)) != 0;
  47. }
  48. /*
  49. given a location, calculate the 32x28 grid SW corner, plus the
  50. grid indices
  51. */
  52. void AP_Terrain::calculate_grid_info(const Location &loc, struct grid_info &info) const
  53. {
  54. // grids start on integer degrees. This makes storing terrain data
  55. // on the SD card a bit easier
  56. info.lat_degrees = (loc.lat<0?(loc.lat-9999999L):loc.lat) / (10*1000*1000L);
  57. info.lon_degrees = (loc.lng<0?(loc.lng-9999999L):loc.lng) / (10*1000*1000L);
  58. // create reference position for this rounded degree position
  59. Location ref;
  60. ref.lat = info.lat_degrees*10*1000*1000L;
  61. ref.lng = info.lon_degrees*10*1000*1000L;
  62. // find offset from reference
  63. const Vector2f offset = ref.get_distance_NE(loc);
  64. // get indices in terms of grid_spacing elements
  65. uint32_t idx_x = offset.x / grid_spacing;
  66. uint32_t idx_y = offset.y / grid_spacing;
  67. // find indexes into 32*28 grids for this degree reference. Note
  68. // the use of TERRAIN_GRID_BLOCK_SPACING_{X,Y} which gives a one square
  69. // overlap between grids
  70. info.grid_idx_x = idx_x / TERRAIN_GRID_BLOCK_SPACING_X;
  71. info.grid_idx_y = idx_y / TERRAIN_GRID_BLOCK_SPACING_Y;
  72. // find the indices within the 32*28 grid
  73. info.idx_x = idx_x % TERRAIN_GRID_BLOCK_SPACING_X;
  74. info.idx_y = idx_y % TERRAIN_GRID_BLOCK_SPACING_Y;
  75. // find the fraction (0..1) within the square
  76. info.frac_x = (offset.x - idx_x * grid_spacing) / grid_spacing;
  77. info.frac_y = (offset.y - idx_y * grid_spacing) / grid_spacing;
  78. // calculate lat/lon of SW corner of 32*28 grid_block
  79. ref.offset(info.grid_idx_x * TERRAIN_GRID_BLOCK_SPACING_X * (float)grid_spacing,
  80. info.grid_idx_y * TERRAIN_GRID_BLOCK_SPACING_Y * (float)grid_spacing);
  81. info.grid_lat = ref.lat;
  82. info.grid_lon = ref.lng;
  83. ASSERT_RANGE(info.idx_x,0,TERRAIN_GRID_BLOCK_SPACING_X-1);
  84. ASSERT_RANGE(info.idx_y,0,TERRAIN_GRID_BLOCK_SPACING_Y-1);
  85. ASSERT_RANGE(info.frac_x,0,1);
  86. ASSERT_RANGE(info.frac_y,0,1);
  87. }
  88. /*
  89. find a grid structure given a grid_info
  90. */
  91. AP_Terrain::grid_cache &AP_Terrain::find_grid_cache(const struct grid_info &info)
  92. {
  93. uint16_t oldest_i = 0;
  94. // see if we have that grid
  95. for (uint16_t i=0; i<cache_size; i++) {
  96. if (cache[i].grid.lat == info.grid_lat &&
  97. cache[i].grid.lon == info.grid_lon &&
  98. cache[i].grid.spacing == grid_spacing) {
  99. cache[i].last_access_ms = AP_HAL::millis();
  100. return cache[i];
  101. }
  102. if (cache[i].last_access_ms < cache[oldest_i].last_access_ms) {
  103. oldest_i = i;
  104. }
  105. }
  106. // Not found. Use the oldest grid and make it this grid,
  107. // initially unpopulated
  108. struct grid_cache &grid = cache[oldest_i];
  109. memset(&grid, 0, sizeof(grid));
  110. grid.grid.lat = info.grid_lat;
  111. grid.grid.lon = info.grid_lon;
  112. grid.grid.spacing = grid_spacing;
  113. grid.grid.grid_idx_x = info.grid_idx_x;
  114. grid.grid.grid_idx_y = info.grid_idx_y;
  115. grid.grid.lat_degrees = info.lat_degrees;
  116. grid.grid.lon_degrees = info.lon_degrees;
  117. grid.grid.version = TERRAIN_GRID_FORMAT_VERSION;
  118. grid.last_access_ms = AP_HAL::millis();
  119. // mark as waiting for disk read
  120. grid.state = GRID_CACHE_DISKWAIT;
  121. return grid;
  122. }
  123. /*
  124. find cache index of disk_block
  125. */
  126. int16_t AP_Terrain::find_io_idx(enum GridCacheState state)
  127. {
  128. // try first with given state
  129. for (uint16_t i=0; i<cache_size; i++) {
  130. if (disk_block.block.lat == cache[i].grid.lat &&
  131. disk_block.block.lon == cache[i].grid.lon &&
  132. cache[i].state == state) {
  133. return i;
  134. }
  135. }
  136. // then any state
  137. for (uint16_t i=0; i<cache_size; i++) {
  138. if (disk_block.block.lat == cache[i].grid.lat &&
  139. disk_block.block.lon == cache[i].grid.lon) {
  140. return i;
  141. }
  142. }
  143. return -1;
  144. }
  145. /*
  146. get CRC for a block
  147. */
  148. uint16_t AP_Terrain::get_block_crc(struct grid_block &block)
  149. {
  150. uint16_t saved_crc = block.crc;
  151. block.crc = 0;
  152. uint16_t ret = crc16_ccitt((const uint8_t *)&block, sizeof(block), 0);
  153. block.crc = saved_crc;
  154. return ret;
  155. }
  156. #endif // AP_TERRAIN_AVAILABLE