forked from betaflight/betaflight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voltage.h
129 lines (98 loc) · 3.7 KB
/
voltage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "voltage_ids.h"
#define SLOW_VOLTAGE_TASK_FREQ_HZ 50
#define FAST_VOLTAGE_TASK_FREQ_HZ 200
#define PREV_DISPLAY_FILTERED_TIME_DIFF 500 // ms
//
// meters
//
typedef enum {
VOLTAGE_METER_NONE = 0,
VOLTAGE_METER_ADC,
VOLTAGE_METER_ESC,
VOLTAGE_METER_COUNT
} voltageMeterSource_e;
extern const char * const voltageMeterSourceNames[VOLTAGE_METER_COUNT];
// WARNING - do not mix usage of VOLTAGE_METER_* and VOLTAGE_SENSOR_*, they are separate concerns.
typedef struct voltageMeter_s {
uint16_t displayFiltered; // voltage in 0.01V steps
uint16_t prevDisplayFiltered; // voltage in 0.01V steps
timeMs_t prevDisplayFilteredTime;
bool isVoltageStable;
uint16_t unfiltered; // voltage in 0.01V steps
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
uint16_t sagFiltered; // voltage in 0.01V steps
#endif
bool lowVoltageCutoff;
} voltageMeter_t;
//
// sensors
//
typedef enum {
VOLTAGE_SENSOR_TYPE_ADC_RESISTOR_DIVIDER = 0,
VOLTAGE_SENSOR_TYPE_ESC
} voltageSensorType_e;
//
// adc sensors
//
#define VBAT_SCALE_MIN 0
#define VBAT_SCALE_MAX 255
#define VBAT_DIVIDER_MIN 1
#define VBAT_DIVIDER_MAX 255
#define VBAT_MULTIPLIER_MIN 1
#define VBAT_MULTIPLIER_MAX 255
#ifndef MAX_VOLTAGE_SENSOR_ADC
#define MAX_VOLTAGE_SENSOR_ADC 1 // VBAT - some boards have external, 12V, 9V and 5V meters.
#endif
#define VOLTAGE_METER_ID_ESC_COUNT 12
typedef enum {
VOLTAGE_SENSOR_ADC_VBAT = 0,
VOLTAGE_SENSOR_ADC_12V = 1,
VOLTAGE_SENSOR_ADC_9V = 2,
VOLTAGE_SENSOR_ADC_5V = 3
} voltageSensorADC_e; // see also voltageMeterADCtoIDMap
typedef struct voltageSensorADCConfig_s {
uint8_t vbatscale; // adjust this to match battery voltage to reported value
uint8_t vbatresdivval; // resistor divider R2 (default NAZE 10(K))
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
} voltageSensorADCConfig_t;
PG_DECLARE_ARRAY(voltageSensorADCConfig_t, MAX_VOLTAGE_SENSOR_ADC, voltageSensorADCConfig);
//
// Main API
//
void voltageMeterReset(voltageMeter_t *voltageMeter);
void voltageMeterGenericInit(void);
void voltageMeterADCInit(void);
void voltageMeterADCRefresh(void);
void voltageMeterADCRead(voltageSensorADC_e adcChannel, voltageMeter_t *voltageMeter);
void voltageMeterESCInit(void);
void voltageMeterESCRefresh(void);
void voltageMeterESCReadCombined(voltageMeter_t *voltageMeter);
void voltageMeterESCReadMotor(uint8_t motor, voltageMeter_t *voltageMeter);
//
// API for reading/configuring current meters by id.
//
extern const uint8_t voltageMeterADCtoIDMap[MAX_VOLTAGE_SENSOR_ADC];
extern const uint8_t supportedVoltageMeterCount;
extern const uint8_t voltageMeterIds[];
void voltageMeterRead(voltageMeterId_e id, voltageMeter_t *voltageMeter);
bool isSagCompensationConfigured(void);