AnalogIn.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include "AP_HAL_ChibiOS.h"
  19. #define ANALOG_MAX_CHANNELS 16
  20. // number of samples on each channel to gather on each DMA callback
  21. #define ADC_DMA_BUF_DEPTH 8
  22. #if HAL_USE_ADC == TRUE && !defined(HAL_DISABLE_ADC_DRIVER)
  23. class ChibiOS::AnalogSource : public AP_HAL::AnalogSource {
  24. public:
  25. friend class ChibiOS::AnalogIn;
  26. AnalogSource(int16_t pin, float initial_value);
  27. float read_average() override;
  28. float read_latest() override;
  29. void set_pin(uint8_t p) override;
  30. float voltage_average() override;
  31. float voltage_latest() override;
  32. float voltage_average_ratiometric() override;
  33. private:
  34. // what value it has
  35. int16_t _pin;
  36. float _value;
  37. float _value_ratiometric;
  38. float _latest_value;
  39. uint8_t _sum_count;
  40. float _sum_value;
  41. float _sum_ratiometric;
  42. void _add_value(float v, float vcc5V);
  43. float _pin_scaler();
  44. HAL_Semaphore _semaphore;
  45. };
  46. class ChibiOS::AnalogIn : public AP_HAL::AnalogIn {
  47. public:
  48. friend class AnalogSource;
  49. void init() override;
  50. AP_HAL::AnalogSource* channel(int16_t pin) override;
  51. void _timer_tick(void);
  52. float board_voltage(void) override { return _board_voltage; }
  53. float servorail_voltage(void) override { return _servorail_voltage; }
  54. uint16_t power_status_flags(void) override { return _power_flags; }
  55. static void adccallback(ADCDriver *adcp);
  56. private:
  57. void read_adc(uint32_t *val);
  58. void update_power_flags(void);
  59. int _battery_handle;
  60. int _servorail_handle;
  61. int _system_power_handle;
  62. uint64_t _battery_timestamp;
  63. uint64_t _servorail_timestamp;
  64. ChibiOS::AnalogSource* _channels[ANALOG_MAX_CHANNELS];
  65. uint32_t _last_run;
  66. float _board_voltage;
  67. float _servorail_voltage;
  68. float _rssi_voltage;
  69. uint16_t _power_flags;
  70. ADCConversionGroup adcgrpcfg;
  71. struct pin_info {
  72. uint8_t channel;
  73. float scaling;
  74. };
  75. static const pin_info pin_config[];
  76. static adcsample_t *samples;
  77. static uint32_t sample_sum[];
  78. static uint32_t sample_count;
  79. };
  80. #endif // HAL_USE_ADC