AP_RSSI.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <AP_HAL/AP_HAL.h>
  15. #include <AP_Param/AP_Param.h>
  16. #include <AP_Math/AP_Math.h>
  17. class AP_RSSI
  18. {
  19. public:
  20. enum class RssiType {
  21. TYPE_DISABLED = 0,
  22. ANALOG_PIN = 1,
  23. RC_CHANNEL_VALUE = 2,
  24. RECEIVER = 3,
  25. PWM_PIN = 4
  26. };
  27. AP_RSSI();
  28. /* Do not allow copies */
  29. AP_RSSI(const AP_RSSI &other) = delete;
  30. AP_RSSI &operator=(const AP_RSSI&) = delete;
  31. // destructor
  32. ~AP_RSSI(void);
  33. static AP_RSSI *get_singleton();
  34. // Initialize the rssi object and prepare it for use
  35. void init();
  36. // return true if rssi reading is enabled
  37. bool enabled() const { return RssiType(rssi_type.get()) != RssiType::TYPE_DISABLED; }
  38. // Read the receiver RSSI value as a float 0.0f - 1.0f.
  39. // 0.0 represents weakest signal, 1.0 represents maximum signal.
  40. float read_receiver_rssi();
  41. // Read the receiver RSSI value as an 8-bit integer
  42. // 0 represents weakest signal, 255 represents maximum signal.
  43. uint8_t read_receiver_rssi_uint8();
  44. // parameter block
  45. static const struct AP_Param::GroupInfo var_info[];
  46. private:
  47. static AP_RSSI *_singleton;
  48. // RSSI parameters
  49. AP_Int8 rssi_type; // Type of RSSI being used
  50. AP_Int8 rssi_analog_pin; // Analog pin RSSI value found on
  51. AP_Float rssi_analog_pin_range_low; // Voltage value for weakest rssi signal
  52. AP_Float rssi_analog_pin_range_high; // Voltage value for strongest rssi signal
  53. AP_Int8 rssi_channel; // allows rssi to be read from given channel as PWM value
  54. AP_Int16 rssi_channel_low_pwm_value; // PWM value for weakest rssi signal
  55. AP_Int16 rssi_channel_high_pwm_value; // PWM value for strongest rssi signal
  56. // Analog Inputs
  57. // a pin for reading the receiver RSSI voltage.
  58. AP_HAL::AnalogSource *rssi_analog_source;
  59. // PWM input
  60. struct PWMState {
  61. int8_t last_rssi_analog_pin; // last pin used for reading pwm (used to recognise change in pin assignment)
  62. uint32_t last_reading_ms; // system time of last read (used for health reporting)
  63. float rssi_value; // last calculated RSSI value
  64. // the following two members are updated by the interrupt handler
  65. uint32_t irq_value_us; // last calculated pwm value (irq copy)
  66. uint32_t pulse_start_us; // system time of start of pulse
  67. } pwm_state;
  68. // read the RSSI value from an analog pin - returns float in range 0.0 to 1.0
  69. float read_pin_rssi();
  70. // check if pin has changed and configure interrupt handlers if required
  71. void check_pwm_pin_rssi();
  72. // read the RSSI value from a PWM value on a RC channel
  73. float read_channel_rssi();
  74. // read the PWM value from a pin
  75. float read_pwm_pin_rssi();
  76. // Scale and constrain a float rssi value to 0.0 to 1.0 range
  77. float scale_and_constrain_float_rssi(float current_rssi_value, float low_rssi_range, float high_rssi_range);
  78. // PWM input handling
  79. void irq_handler(uint8_t pin,
  80. bool pin_state,
  81. uint32_t timestamp);
  82. };
  83. namespace AP {
  84. AP_RSSI *rssi();
  85. };