AP_RangeFinder_Bebop.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #pragma once
  14. #include "RangeFinder.h"
  15. #include "RangeFinder_Backend.h"
  16. #include <AP_HAL_Linux/Thread.h>
  17. /*
  18. * the size of the buffer sent over spi
  19. */
  20. #define RNFD_BEBOP_NB_PULSES_MAX 32
  21. /*
  22. * the size of the purge buffer sent over spi
  23. */
  24. #define RNFD_BEBOP_NB_PULSES_PURGE 64
  25. /*
  26. * default us frequency
  27. * 17 times by seconds
  28. */
  29. #define RNFD_BEBOP_DEFAULT_FREQ 17
  30. /*
  31. * default adc frequency
  32. */
  33. #define RNFD_BEBOP_DEFAULT_ADC_FREQ 160000
  34. /*
  35. * to filter data we make the average of (1 << this_value) datas
  36. */
  37. #define RNFD_BEBOP_FILTER_POWER 2
  38. /*
  39. * Speed of sound
  40. */
  41. #define RNFD_BEBOP_SOUND_SPEED 340
  42. /* above this altitude we should use mode 0 */
  43. #define RNFD_BEBOP_TRANSITION_HIGH_TO_LOW 0.75
  44. /* below this altitude we should use mode 1 */
  45. #define RNFD_BEBOP_TRANSITION_LOW_TO_HIGH 1.5
  46. /* count this times before switching mode */
  47. #define RNFD_BEBOP_TRANSITION_COUNT 5
  48. /*
  49. * the number of echoes we will keep at most
  50. */
  51. #define RNFD_BEBOP_MAX_ECHOES 30
  52. struct echo {
  53. int max_index; /* index in the capture buffer at which the maximum is reached */
  54. int distance_index; /* index in the capture buffer at which the signal is for
  55. the first time above a fixed threshold below the
  56. maximum => this corresponds to the real distance
  57. that should be attributed to this echo */
  58. };
  59. /*
  60. * struct related to adc
  61. * data to receive and process adc datas
  62. */
  63. struct adc_capture {
  64. struct iio_device *device;
  65. struct iio_buffer *buffer;
  66. unsigned int buffer_size;
  67. struct iio_channel *channel;
  68. unsigned int freq;
  69. /* Used in order to match two echoes of two ADC acquisitions */
  70. unsigned short threshold_time_rejection;
  71. };
  72. class AP_RangeFinder_Bebop : public AP_RangeFinder_Backend {
  73. public:
  74. AP_RangeFinder_Bebop(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params);
  75. ~AP_RangeFinder_Bebop(void);
  76. static bool detect();
  77. void update(void) override;
  78. protected:
  79. MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override {
  80. return MAV_DISTANCE_SENSOR_LASER;
  81. }
  82. private:
  83. void _init(void);
  84. int _launch(void);
  85. int _capture(void);
  86. int _update_mode(float altitude);
  87. void _configure_gpio(int value);
  88. int _configure_wave();
  89. void _reconfigure_wave();
  90. int _configure_capture();
  91. int _launch_purge();
  92. void _loop(void);
  93. Linux::Thread *_thread;
  94. unsigned short get_threshold_at(int i_capture);
  95. int _apply_averaging_filter(void);
  96. int _search_local_maxima(void);
  97. int _search_maximum_with_max_amplitude(void);
  98. AP_HAL::OwnPtr<AP_HAL::Device> _spi;
  99. AP_HAL::GPIO *_gpio;
  100. struct adc_capture _adc;
  101. struct iio_context *_iio;
  102. unsigned char _tx[2][RNFD_BEBOP_NB_PULSES_MAX];
  103. unsigned char _purge[RNFD_BEBOP_NB_PULSES_PURGE];
  104. unsigned char* _tx_buf;
  105. int _hysteresis_counter;
  106. const unsigned int threshold_echo_init = 1500;
  107. int _fd = -1;
  108. uint64_t _last_timestamp;
  109. int _mode;
  110. int _nb_echoes;
  111. int _freq;
  112. float _altitude;
  113. unsigned int *_filtered_capture;
  114. unsigned int _filtered_capture_size;
  115. struct echo _echoes[RNFD_BEBOP_MAX_ECHOES];
  116. unsigned int _filter_average = 4;
  117. int16_t _last_max_distance_cm = 850;
  118. int16_t _last_min_distance_cm = 32;
  119. };