APM:Libraries
AnalogIn.h
Go to the documentation of this file.
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 
18 #pragma once
19 
20 #include "AP_HAL_ChibiOS.h"
21 
22 #define ANALOG_MAX_CHANNELS 16
23 
24 // number of samples on each channel to gather on each DMA callback
25 #define ADC_DMA_BUF_DEPTH 8
26 
27 #if HAL_USE_ADC == TRUE
28 
30 public:
31  friend class ChibiOS::AnalogIn;
32  AnalogSource(int16_t pin, float initial_value);
33  float read_average();
34  float read_latest();
35  void set_pin(uint8_t p);
36  float voltage_average();
37  float voltage_latest();
39  void set_stop_pin(uint8_t p) {}
40  void set_settle_time(uint16_t settle_time_ms) {}
41 
42 private:
43  // what value it has
44  int16_t _pin;
45  float _value;
48  uint8_t _sum_count;
49  float _sum_value;
51  void _add_value(float v, float vcc5V);
52  float _pin_scaler();
53 };
54 
56 public:
57  friend class AnalogSource;
58 
59  AnalogIn();
60  void init() override;
61  AP_HAL::AnalogSource* channel(int16_t pin) override;
62  void _timer_tick(void);
63  float board_voltage(void) override { return _board_voltage; }
64  float servorail_voltage(void) override { return _servorail_voltage; }
65  uint16_t power_status_flags(void) override { return _power_flags; }
66  static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n);
67 
68 private:
69  void read_adc(uint32_t *val);
70  void update_power_flags(void);
71 
78 
79  uint32_t _last_run;
82  uint16_t _power_flags;
83  ADCConversionGroup adcgrpcfg;
84 
85  struct pin_info {
86  uint8_t channel;
87  float scaling;
88  };
89  static const pin_info pin_config[];
90 
91  static adcsample_t samples[];
92  static uint32_t sample_sum[];
93  static uint32_t sample_count;
94 };
95 
96 #endif // HAL_USE_ADC
int _system_power_handle
Definition: AnalogIn.h:74
uint16_t power_status_flags(void) override
Definition: AnalogIn.h:65
int _servorail_handle
Definition: AnalogIn.h:73
void set_settle_time(uint16_t settle_time_ms)
Definition: AnalogIn.h:40
static uint8_t buffer[SRXL_FRAMELEN_MAX]
Definition: srxl.cpp:56
float _servorail_voltage
Definition: AnalogIn.h:81
void set_stop_pin(uint8_t p)
Definition: AnalogIn.h:39
float board_voltage(void) override
Definition: AnalogIn.h:63
uint64_t _battery_timestamp
Definition: AnalogIn.h:75
void set_pin(uint8_t p)
Definition: AnalogIn.cpp:136
AnalogSource(int16_t pin, float initial_value)
Definition: AnalogIn.cpp:65
void _add_value(float v, float vcc5V)
Definition: AnalogIn.cpp:153
float v
Definition: Printf.cpp:15
uint16_t _power_flags
Definition: AnalogIn.h:82
float voltage_average_ratiometric()
Definition: AnalogIn.cpp:122
uint32_t _last_run
Definition: AnalogIn.h:79
void init()
Generic board initialization function.
Definition: system.cpp:136
uint64_t _servorail_timestamp
Definition: AnalogIn.h:76
static uint32_t sample_count
Definition: AnalogIn.h:93
float _value_ratiometric
Definition: AnalogIn.h:46
static int8_t pin
Definition: AnalogIn.cpp:15
#define ANALOG_MAX_CHANNELS
Definition: AnalogIn.h:22
float servorail_voltage(void) override
Definition: AnalogIn.h:64
ADCConversionGroup adcgrpcfg
Definition: AnalogIn.h:83
float _board_voltage
Definition: AnalogIn.h:80