AP_Proximity_AirSimSITL.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <AP_HAL/AP_HAL.h>
  14. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  15. #include "AP_Proximity_AirSimSITL.h"
  16. #include <stdio.h>
  17. extern const AP_HAL::HAL& hal;
  18. #define PROXIMITY_MAX_RANGE 100.0f
  19. #define PROXIMITY_ACCURACY 0.1f
  20. /*
  21. The constructor also initialises the proximity sensor.
  22. */
  23. AP_Proximity_AirSimSITL::AP_Proximity_AirSimSITL(AP_Proximity &_frontend,
  24. AP_Proximity::Proximity_State &_state):
  25. AP_Proximity_Backend(_frontend, _state),
  26. sitl(AP::sitl())
  27. {
  28. }
  29. // update the state of the sensor
  30. void AP_Proximity_AirSimSITL::update(void)
  31. {
  32. SITL::vector3f_array &points = sitl->state.scanner.points;
  33. if (points.length == 0) {
  34. set_status(AP_Proximity::Proximity_NoData);
  35. return;
  36. }
  37. set_status(AP_Proximity::Proximity_Good);
  38. memset(_distance_valid, 0, sizeof(_distance_valid));
  39. memset(_angle, 0, sizeof(_angle));
  40. memset(_distance, 0, sizeof(_distance));
  41. // only use 8 sectors to match RPLidar
  42. const uint8_t nsectors = MIN(8, PROXIMITY_SECTORS_MAX);
  43. const uint16_t degrees_per_sector = 360 / nsectors;
  44. for (uint16_t i=0; i<points.length; i++) {
  45. Vector3f &point = points.data[i];
  46. if (point.is_zero()) {
  47. continue;
  48. }
  49. float angle_deg = wrap_360(degrees(atan2f(-point.y, point.x)));
  50. uint16_t angle_rounded = uint16_t(angle_deg+0.5);
  51. uint8_t sector = wrap_360(angle_rounded + 22.5f) / degrees_per_sector;
  52. if (!_distance_valid[sector] || PROXIMITY_MAX_RANGE < _distance[sector]) {
  53. _distance_valid[sector] = true;
  54. const Vector2f v = Vector2f(point.x, point.y);
  55. _distance[sector] = v.length();
  56. _angle[sector] = angle_deg;
  57. update_boundary_for_sector(sector, true);
  58. }
  59. }
  60. #if 0
  61. printf("npoints=%u\n", points.length);
  62. for (uint16_t i=0; i<nsectors; i++) {
  63. printf("sector[%u] ang=%.1f dist=%.1f\n", i, _angle[i], _distance[i]);
  64. }
  65. #endif
  66. }
  67. // get maximum and minimum distances (in meters) of primary sensor
  68. float AP_Proximity_AirSimSITL::distance_max() const
  69. {
  70. return PROXIMITY_MAX_RANGE;
  71. }
  72. float AP_Proximity_AirSimSITL::distance_min() const
  73. {
  74. return 0.0f;
  75. }
  76. // get distance upwards in meters. returns true on success
  77. bool AP_Proximity_AirSimSITL::get_upward_distance(float &distance) const
  78. {
  79. // we don't have an upward facing laser
  80. return false;
  81. }
  82. #endif // CONFIG_HAL_BOARD