Merge pull request #10492 from iNavFlight/MrD_Update-OSD.md-for-8.0
[inav.git] / src / main / sensors / battery.h
blob0ecde181b37f4545b23c818dabb140115761c840
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 "config/parameter_group.h"
22 #include "drivers/time.h"
24 #include "fc/settings.h"
26 #include "sensors/battery_config_structs.h"
28 #ifndef VBAT_SCALE_DEFAULT
29 #define VBAT_SCALE_DEFAULT 1100
30 #endif
32 #ifndef CURRENT_METER_SCALE
33 #define CURRENT_METER_SCALE 400 // for Allegro ACS758LCB-100U (40mV/A)
34 #endif
36 #ifndef CURRENT_METER_OFFSET
37 #define CURRENT_METER_OFFSET 0
38 #endif
40 #ifndef MAX_BATTERY_PROFILE_COUNT
41 #define MAX_BATTERY_PROFILE_COUNT SETTING_CONSTANT_MAX_BATTERY_PROFILE_COUNT
42 #endif
44 typedef struct {
45 uint8_t profile_index;
46 uint16_t max_voltage;
47 } profile_comp_t;
49 PG_DECLARE(batteryMetersConfig_t, batteryMetersConfig);
50 PG_DECLARE_ARRAY(batteryProfile_t, MAX_BATTERY_PROFILE_COUNT, batteryProfiles);
52 extern const batteryProfile_t *currentBatteryProfile;
54 #define currentBatteryProfileMutable ((batteryProfile_t*)currentBatteryProfile)
56 typedef enum {
57 BATTERY_OK = 0,
58 BATTERY_WARNING,
59 BATTERY_CRITICAL,
60 BATTERY_NOT_PRESENT
61 } batteryState_e;
64 uint16_t batteryAdcToVoltage(uint16_t src);
65 batteryState_e getBatteryState(void);
66 batteryState_e checkBatteryVoltageState(void);
67 bool batteryWasFullWhenPluggedIn(void);
68 bool batteryUsesCapacityThresholds(void);
69 void batteryInit(void);
70 void setBatteryProfile(uint8_t profileIndex);
71 void activateBatteryProfile(void);
72 void batteryDisableProfileAutoswitch(void);
74 bool isBatteryVoltageConfigured(void);
75 bool isPowerSupplyImpedanceValid(void);
76 uint16_t getBatteryVoltage(void);
77 uint16_t getBatteryRawVoltage(void);
78 uint16_t getBatterySagCompensatedVoltage(void);
79 uint8_t getBatteryCellCount(void);
80 uint16_t getBatteryRawAverageCellVoltage(void);
81 uint16_t getBatteryAverageCellVoltage(void);
82 uint16_t getBatterySagCompensatedAverageCellVoltage(void);
83 uint32_t getBatteryRemainingCapacity(void);
84 uint16_t getPowerSupplyImpedance(void);
86 bool isAmperageConfigured(void);
87 int16_t getAmperage(void);
88 int16_t getAmperageSample(void);
89 int32_t getPower(void);
90 int32_t getMAhDrawn(void);
91 int32_t getMWhDrawn(void);
93 #ifdef USE_ADC
94 void batteryUpdate(timeUs_t timeDelta);
95 void sagCompensatedVBatUpdate(timeUs_t currentTime, timeUs_t timeDelta);
96 void powerMeterUpdate(timeUs_t timeDelta);
97 uint16_t getVBatSample(void);
98 #endif
100 void currentMeterUpdate(timeUs_t timeDelta);
102 uint8_t calculateBatteryPercentage(void);
103 float calculateThrottleCompensationFactor(void);
104 int32_t calculateAveragePower(void);
105 int32_t calculateAverageEfficiency(void);
106 int32_t heatLossesCompensatedPower(int32_t power);