AP_RangeFinder_PWM.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. class AP_RangeFinder_PWM : public AP_RangeFinder_Backend
  17. {
  18. public:
  19. // constructor
  20. AP_RangeFinder_PWM(RangeFinder::RangeFinder_State &_state,
  21. AP_RangeFinder_Params &_params,
  22. float &_estimated_terrain_height);
  23. // destructor
  24. ~AP_RangeFinder_PWM(void) {};
  25. // static detection function
  26. static bool detect();
  27. // update state
  28. void update(void) override;
  29. protected:
  30. bool get_reading(uint16_t &reading_cm);
  31. MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override {
  32. return MAV_DISTANCE_SENSOR_UNKNOWN;
  33. }
  34. private:
  35. int8_t last_pin; // last pin used for reading pwm (used to recognise change in pin assignment)
  36. // the following three members are updated by the interrupt handler
  37. uint32_t irq_value_us; // some of calculated pwm values (irq copy)
  38. uint16_t irq_sample_count; // number of pwm values in irq_value_us (irq copy)
  39. uint32_t irq_pulse_start_us; // system time of start of pulse
  40. void irq_handler(uint8_t pin, bool pin_high, uint32_t timestamp_us);
  41. void check_pin();
  42. void check_stop_pin();
  43. void check_pins();
  44. uint8_t last_stop_pin = -1;
  45. float &estimated_terrain_height;
  46. // return true if we are beyond the power saving range
  47. bool out_of_range(void) const;
  48. bool was_out_of_range = -1; // this odd initialisation ensures we transition to new state
  49. };