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)
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/>.
23 #include "common/time.h"
24 #include "current_ids.h"
27 CURRENT_METER_NONE
= 0,
29 CURRENT_METER_VIRTUAL
,
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
43 // WARNING - do not mix usage of CURRENT_SENSOR_* and CURRENT_METER_*, they are separate concerns.
45 typedef struct currentMeterMAhDrawnState_s
{
46 int32_t mAhDrawn
; // milliampere hours drawn from the battery since start
48 } currentMeterMAhDrawnState_t
;
55 CURRENT_SENSOR_VIRTUAL
= 0,
66 typedef struct currentMeterADCState_s
{
67 currentMeterMAhDrawnState_t mahDrawnState
;
68 int32_t amperage
; // current read by current sensor in centiampere (1/100th A)
69 int32_t amperageLatest
; // current read by current sensor in centiampere (1/100th A) (unfiltered)
70 } currentMeterADCState_t
;
72 typedef struct currentSensorADCConfig_s
{
73 int16_t scale
; // scale the current sensor output voltage to milliamps. Value in mV/10A
74 int16_t offset
; // offset of the current sensor in mA
75 } currentSensorADCConfig_t
;
77 PG_DECLARE(currentSensorADCConfig_t
, currentSensorADCConfig
);
83 typedef struct currentMeterVirtualState_s
{
84 currentMeterMAhDrawnState_t mahDrawnState
;
85 int32_t amperage
; // current read by current sensor in centiamperes (1/100th A)
86 } currentSensorVirtualState_t
;
88 typedef struct currentSensorVirtualConfig_s
{
89 int16_t scale
; // scale the throttle to centiamperes, using a hardcoded thrust linearization function (see Battery.md)
90 uint16_t offset
; // offset of the current sensor in centiamperes (1/100th A)
91 } currentSensorVirtualConfig_t
;
93 PG_DECLARE(currentSensorVirtualConfig_t
, currentSensorVirtualConfig
);
99 typedef struct currentMeterESCState_s
{
100 int32_t mAhDrawn
; // milliampere hours drawn from the battery since start
101 int32_t amperage
; // current read by current sensor in centiampere (1/100th A)
102 } currentMeterESCState_t
;
109 typedef struct currentMeterMSPState_s
{
110 int32_t mAhDrawn
; // milliampere hours drawn from the battery since start
111 int32_t amperage
; // current read by current sensor in centiampere (1/100th A)
112 } currentMeterMSPState_t
;
119 void currentMeterReset(currentMeter_t
*meter
);
121 void currentMeterADCInit(void);
122 void currentMeterADCRefresh(int32_t lastUpdateAt
);
123 void currentMeterADCRead(currentMeter_t
*meter
);
125 void currentMeterVirtualInit(void);
126 void currentMeterVirtualRefresh(int32_t lastUpdateAt
, bool armed
, bool throttleLowAndMotorStop
, int32_t throttleOffset
);
127 void currentMeterVirtualRead(currentMeter_t
*meter
);
129 void currentMeterESCInit(void);
130 void currentMeterESCRefresh(int32_t lastUpdateAt
);
131 void currentMeterESCReadCombined(currentMeter_t
*meter
);
132 void currentMeterESCReadMotor(uint8_t motorNumber
, currentMeter_t
*meter
);
134 void currentMeterMSPInit(void);
135 void currentMeterMSPRefresh(timeUs_t currentTimeUs
);
136 void currentMeterMSPRead(currentMeter_t
*meter
);
137 void currentMeterMSPSet(uint16_t amperage
, uint16_t mAhDrawn
);
140 // API for reading current meters by id.
142 extern const uint8_t supportedCurrentMeterCount
;
143 extern const uint8_t currentMeterIds
[];
144 void currentMeterRead(currentMeterId_e id
, currentMeter_t
*currentMeter
);