Merge pull request #11195 from mathiasvr/pr-elrs-clean
[betaflight.git] / src / main / sensors / current.h
blob351243d9d555d46f909260f323726f9463c19310
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 "common/time.h"
24 #include "current_ids.h"
26 typedef enum {
27 CURRENT_METER_NONE = 0,
28 CURRENT_METER_ADC,
29 CURRENT_METER_VIRTUAL,
30 CURRENT_METER_ESC,
31 CURRENT_METER_MSP,
32 CURRENT_METER_COUNT
33 } currentMeterSource_e;
35 extern const char * const currentMeterSourceNames[CURRENT_METER_COUNT];
37 typedef struct currentMeter_s {
38 int32_t amperage; // current read by current sensor in centiampere (1/100th A)
39 int32_t amperageLatest; // current read by current sensor in centiampere (1/100th A) (unfiltered)
40 int32_t mAhDrawn; // milliampere hours drawn from the battery since start
41 #ifdef USE_BATTERY_CONTINUE
42 int32_t mAhDrawnOffset; // mAh offset
43 #endif
44 } currentMeter_t;
46 // WARNING - do not mix usage of CURRENT_SENSOR_* and CURRENT_METER_*, they are separate concerns.
48 typedef struct currentMeterMAhDrawnState_s {
49 int32_t mAhDrawn; // milliampere hours drawn from the battery since start
50 float mAhDrawnF;
51 } currentMeterMAhDrawnState_t;
54 // Sensors
57 typedef enum {
58 CURRENT_SENSOR_VIRTUAL = 0,
59 CURRENT_SENSOR_ADC,
60 CURRENT_SENSOR_ESC,
61 CURRENT_SENSOR_MSP
62 } currentSensor_e;
66 // ADC
69 typedef struct currentMeterADCState_s {
70 currentMeterMAhDrawnState_t mahDrawnState;
71 int32_t amperage; // current read by current sensor in centiampere (1/100th A)
72 int32_t amperageLatest; // current read by current sensor in centiampere (1/100th A) (unfiltered)
73 } currentMeterADCState_t;
75 typedef struct currentSensorADCConfig_s {
76 int16_t scale; // scale the current sensor output voltage to milliamps. Value in mV/10A
77 int16_t offset; // offset of the current sensor in mA
78 } currentSensorADCConfig_t;
80 PG_DECLARE(currentSensorADCConfig_t, currentSensorADCConfig);
83 // Virtual
86 typedef struct currentMeterVirtualState_s {
87 currentMeterMAhDrawnState_t mahDrawnState;
88 int32_t amperage; // current read by current sensor in centiamperes (1/100th A)
89 } currentSensorVirtualState_t;
91 typedef struct currentSensorVirtualConfig_s {
92 int16_t scale; // scale the throttle to centiamperes, using a hardcoded thrust linearization function (see Battery.md)
93 uint16_t offset; // offset of the current sensor in centiamperes (1/100th A)
94 } currentSensorVirtualConfig_t;
96 PG_DECLARE(currentSensorVirtualConfig_t, currentSensorVirtualConfig);
99 // ESC
102 typedef struct currentMeterESCState_s {
103 int32_t mAhDrawn; // milliampere hours drawn from the battery since start
104 int32_t amperage; // current read by current sensor in centiampere (1/100th A)
105 } currentMeterESCState_t;
109 // MSP
112 typedef struct currentMeterMSPState_s {
113 int32_t mAhDrawn; // milliampere hours drawn from the battery since start
114 int32_t amperage; // current read by current sensor in centiampere (1/100th A)
115 } currentMeterMSPState_t;
119 // Current Meter API
122 void currentMeterReset(currentMeter_t *meter);
124 void currentMeterADCInit(void);
125 void currentMeterADCRefresh(int32_t lastUpdateAt);
126 void currentMeterADCRead(currentMeter_t *meter);
128 void currentMeterVirtualInit(void);
129 void currentMeterVirtualRefresh(int32_t lastUpdateAt, bool armed, bool throttleLowAndMotorStop, int32_t throttleOffset);
130 void currentMeterVirtualRead(currentMeter_t *meter);
132 void currentMeterESCInit(void);
133 void currentMeterESCRefresh(int32_t lastUpdateAt);
134 void currentMeterESCReadCombined(currentMeter_t *meter);
135 void currentMeterESCReadMotor(uint8_t motorNumber, currentMeter_t *meter);
137 void currentMeterMSPInit(void);
138 void currentMeterMSPRefresh(timeUs_t currentTimeUs);
139 void currentMeterMSPRead(currentMeter_t *meter);
140 void currentMeterMSPSet(uint16_t amperage, uint16_t mAhDrawn);
143 // API for reading current meters by id.
145 extern const uint8_t supportedCurrentMeterCount;
146 extern const uint8_t currentMeterIds[];
147 void currentMeterRead(currentMeterId_e id, currentMeter_t *currentMeter);