Merge pull request #11195 from mathiasvr/pr-elrs-clean
[betaflight.git] / src / main / sensors / voltage.h
blobd0edd0b56d05beb8d637fe8ddbcee4c7b2402bb6
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "voltage_ids.h"
25 #define SLOW_VOLTAGE_TASK_FREQ_HZ 50
26 #define FAST_VOLTAGE_TASK_FREQ_HZ 200
29 // meters
32 typedef enum {
33 VOLTAGE_METER_NONE = 0,
34 VOLTAGE_METER_ADC,
35 VOLTAGE_METER_ESC,
36 VOLTAGE_METER_COUNT
37 } voltageMeterSource_e;
39 extern const char * const voltageMeterSourceNames[VOLTAGE_METER_COUNT];
41 // WARNING - do not mix usage of VOLTAGE_METER_* and VOLTAGE_SENSOR_*, they are separate concerns.
43 typedef struct voltageMeter_s {
44 uint16_t displayFiltered; // voltage in 0.01V steps
45 uint16_t unfiltered; // voltage in 0.01V steps
46 #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
47 uint16_t sagFiltered; // voltage in 0.01V steps
48 #endif
49 bool lowVoltageCutoff;
50 } voltageMeter_t;
54 // sensors
57 typedef enum {
58 VOLTAGE_SENSOR_TYPE_ADC_RESISTOR_DIVIDER = 0,
59 VOLTAGE_SENSOR_TYPE_ESC
60 } voltageSensorType_e;
64 // adc sensors
67 #define VBAT_SCALE_MIN 0
68 #define VBAT_SCALE_MAX 255
70 #define VBAT_DIVIDER_MIN 1
71 #define VBAT_DIVIDER_MAX 255
73 #define VBAT_MULTIPLIER_MIN 1
74 #define VBAT_MULTIPLIER_MAX 255
76 #ifndef MAX_VOLTAGE_SENSOR_ADC
77 #define MAX_VOLTAGE_SENSOR_ADC 1 // VBAT - some boards have external, 12V, 9V and 5V meters.
78 #endif
80 #define VOLTAGE_METER_ID_ESC_COUNT 12
82 typedef enum {
83 VOLTAGE_SENSOR_ADC_VBAT = 0,
84 VOLTAGE_SENSOR_ADC_12V = 1,
85 VOLTAGE_SENSOR_ADC_9V = 2,
86 VOLTAGE_SENSOR_ADC_5V = 3
87 } voltageSensorADC_e; // see also voltageMeterADCtoIDMap
90 typedef struct voltageSensorADCConfig_s {
91 uint8_t vbatscale; // adjust this to match battery voltage to reported value
92 uint8_t vbatresdivval; // resistor divider R2 (default NAZE 10(K))
93 uint8_t vbatresdivmultiplier; // multiplier for scale (e.g. 2.5:1 ratio with multiplier of 4 can use '100' instead of '25' in ratio) to get better precision
94 } voltageSensorADCConfig_t;
96 PG_DECLARE_ARRAY(voltageSensorADCConfig_t, MAX_VOLTAGE_SENSOR_ADC, voltageSensorADCConfig);
99 // Main API
101 void voltageMeterReset(voltageMeter_t *voltageMeter);
103 void voltageMeterGenericInit(void);
105 void voltageMeterADCInit(void);
106 void voltageMeterADCRefresh(void);
107 void voltageMeterADCRead(voltageSensorADC_e adcChannel, voltageMeter_t *voltageMeter);
109 void voltageMeterESCInit(void);
110 void voltageMeterESCRefresh(void);
111 void voltageMeterESCReadCombined(voltageMeter_t *voltageMeter);
112 void voltageMeterESCReadMotor(uint8_t motor, voltageMeter_t *voltageMeter);
116 // API for reading/configuring current meters by id.
118 extern const uint8_t voltageMeterADCtoIDMap[MAX_VOLTAGE_SENSOR_ADC];
120 extern const uint8_t supportedVoltageMeterCount;
121 extern const uint8_t voltageMeterIds[];
122 void voltageMeterRead(voltageMeterId_e id, voltageMeter_t *voltageMeter);
124 bool isSagCompensationConfigured(void);