Graft of 'cli_diff_command' into 'master'.
[betaflight.git] / src / main / sensors / battery.h
blobf459a3bcc420950101df51e72a12c7a12abba713
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include "common/maths.h"
22 #ifndef VBAT_SCALE_DEFAULT
23 #define VBAT_SCALE_DEFAULT 110
24 #endif
25 #define VBAT_RESDIVVAL_DEFAULT 10
26 #define VBAT_RESDIVMULTIPLIER_DEFAULT 1
27 #define VBAT_SCALE_MIN 0
28 #define VBAT_SCALE_MAX 255
30 typedef enum {
31 CURRENT_SENSOR_NONE = 0,
32 CURRENT_SENSOR_ADC,
33 CURRENT_SENSOR_VIRTUAL,
34 CURRENT_SENSOR_MAX = CURRENT_SENSOR_VIRTUAL
35 } currentSensor_e;
37 typedef struct batteryConfig_s {
38 uint8_t vbatscale; // adjust this to match battery voltage to reported value
39 uint8_t vbatresdivval; // resistor divider R2 (default NAZE 10(K))
40 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
41 uint8_t vbatmaxcellvoltage; // maximum voltage per cell, used for auto-detecting battery voltage in 0.1V units, default is 43 (4.3V)
42 uint8_t vbatmincellvoltage; // minimum voltage per cell, this triggers battery critical alarm, in 0.1V units, default is 33 (3.3V)
43 uint8_t vbatwarningcellvoltage; // warning voltage per cell, this triggers battery warning alarm, in 0.1V units, default is 35 (3.5V)
44 uint8_t vbathysteresis; // hysteresis for alarm, default 1 = 0.1V
46 int16_t currentMeterScale; // scale the current sensor output voltage to milliamps. Value in 1/10th mV/A
47 uint16_t currentMeterOffset; // offset of the current sensor in millivolt steps
48 currentSensor_e currentMeterType; // type of current meter used, either ADC or virtual
50 // FIXME this doesn't belong in here since it's a concern of MSP, not of the battery code.
51 uint8_t multiwiiCurrentMeterOutput; // if set to 1 output the amperage in milliamp steps instead of 0.01A steps via msp
52 uint16_t batteryCapacity; // mAh
53 } batteryConfig_t;
55 typedef enum {
56 BATTERY_OK = 0,
57 BATTERY_WARNING,
58 BATTERY_CRITICAL,
59 BATTERY_NOT_PRESENT
60 } batteryState_e;
62 extern uint16_t vbat;
63 extern uint16_t vbatRaw;
64 extern uint16_t vbatLatestADC;
65 extern uint8_t batteryCellCount;
66 extern uint16_t batteryWarningVoltage;
67 extern uint16_t amperageLatestADC;
68 extern int32_t amperage;
69 extern int32_t mAhDrawn;
71 batteryState_e getBatteryState(void);
72 const char * getBatteryStateString(void);
73 void updateBattery(void);
74 void batteryInit(batteryConfig_t *initialBatteryConfig);
75 batteryConfig_t *batteryConfig;
77 struct rxConfig_s;
78 void updateCurrentMeter(int32_t lastUpdateAt, struct rxConfig_s *rxConfig, uint16_t deadband3d_throttle);
79 int32_t currentMeterToCentiamps(uint16_t src);
81 fix12_t calculateVbatPidCompensation(void);
82 uint8_t calculateBatteryPercentage(void);
83 uint8_t calculateBatteryCapacityRemainingPercentage(void);