1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 16:25:31 +03:00

CF/BF - add support for reading all voltage and current meters via MSP.

Refactored current meter API and state, now it's more closely aligned
with the the voltage meter API.
This commit is contained in:
Hydra 2017-03-17 19:42:28 +00:00 committed by Dominic Clifton
parent b0c49caa3d
commit 2f99749003
8 changed files with 460 additions and 174 deletions

View file

@ -17,6 +17,7 @@
#pragma once
#include "current_ids.h"
typedef enum {
CURRENT_METER_NONE = 0,
@ -26,54 +27,10 @@ typedef enum {
CURRENT_METER_MAX = CURRENT_METER_ESC
} currentMeterSource_e;
//
// fixed ids, current can be measured at many different places, these identifiers are the ones we support or would consider supporting.
//
typedef enum {
CURRENT_METER_ID_NONE = 0,
CURRENT_METER_ID_VBAT_1 = 10, // 10-19 for battery meters
CURRENT_METER_ID_VBAT_2,
//..
CURRENT_METER_ID_VBAT_10 = 19,
CURRENT_METER_ID_5V_1 = 20, // 20-29 for 5V meters
CURRENT_METER_ID_5V_2,
//..
CURRENT_METER_ID_5V_10 = 29,
CURRENT_METER_ID_9V_1 = 30, // 30-39 for 9V meters
CURRENT_METER_ID_9V_2,
//..
CURRENT_METER_ID_9V_10 = 39,
CURRENT_METER_ID_12V_1 = 40, // 40-49 for 12V meters
CURRENT_METER_ID_12V_2,
//..
CURRENT_METER_ID_12V_10 = 49,
CURRENT_METER_ID_ESC_COMBINED_1 = 50, // 50-59 for ESC combined (it's doubtful an FC would ever expose 51-59 however)
// ...
CURRENT_METER_ID_ESC_COMBINED_10 = 59,
CURRENT_METER_ID_ESC_MOTOR_1 = 60, // 60-79 for ESC motors (20 motors)
CURRENT_METER_ID_ESC_MOTOR_2,
CURRENT_METER_ID_ESC_MOTOR_3,
CURRENT_METER_ID_ESC_MOTOR_4,
CURRENT_METER_ID_ESC_MOTOR_5,
CURRENT_METER_ID_ESC_MOTOR_6,
CURRENT_METER_ID_ESC_MOTOR_7,
CURRENT_METER_ID_ESC_MOTOR_8,
//...
CURRENT_METER_ID_ESC_MOTOR_20 = 79,
} currentMeterId_e;
typedef struct currentMeter_s {
int32_t amperage; // current read by current sensor in centiampere (1/100th A)
int32_t amperageLatest; // current read by current sensor in centiampere (1/100th A) (unfiltered)
int32_t mAhDrawn; // milliampere hours drawn from the battery since start
float mAhDrawnF;
} currentMeter_t;
// NOTE: currentMeterConfig is only used by physical and virtual current meters, not ESC based current meters.
@ -86,6 +43,27 @@ typedef enum {
// WARNING - do not mix usage of CURRENT_SENSOR_* and CURRENT_METER_*, they are separate concerns.
typedef struct currentMeterMAhDrawnState_s {
int32_t mAhDrawn; // milliampere hours drawn from the battery since start
float mAhDrawnF;
} currentMeterMAhDrawnState_t;
typedef struct currentMeterADCState_s {
currentMeterMAhDrawnState_t mahDrawnState;
int32_t amperage; // current read by current sensor in centiampere (1/100th A)
int32_t amperageLatest; // current read by current sensor in centiampere (1/100th A) (unfiltered)
} currentMeterADCState_t;
typedef struct currentMeterVirtualState_s {
currentMeterMAhDrawnState_t mahDrawnState;
int32_t amperage; // current read by current sensor in centiampere (1/100th A)
} currentMeterVirtualState_t;
typedef struct currentMeterESCState_s {
int32_t mAhDrawn; // milliampere hours drawn from the battery since start
int32_t amperage; // current read by current sensor in centiampere (1/100th A)
} currentMeterESCState_t;
typedef struct currentMeterADCOrVirtualConfig_s {
int16_t scale; // scale the current sensor output voltage to milliamps. Value in 1/10th mV/A
uint16_t offset; // offset of the current sensor in millivolt steps
@ -93,10 +71,28 @@ typedef struct currentMeterADCOrVirtualConfig_s {
PG_DECLARE_ARRAY(currentMeterADCOrVirtualConfig_t, MAX_ADC_OR_VIRTUAL_CURRENT_METERS, currentMeterADCOrVirtualConfig);
//
// Main API
//
void currentMeterReset(currentMeter_t *meter);
void currentMeterADCInit(void);
void currentMeterADCRefresh(int32_t lastUpdateAt);
void currentMeterADCRead(currentMeter_t *meter);
void currentUpdateADCMeter(currentMeter_t *state, int32_t lastUpdateAt);
void currentUpdateESCMeter(currentMeter_t *state, int32_t lastUpdateAt);
void currentUpdateVirtualMeter(currentMeter_t *state, int32_t lastUpdateAt, bool armed, bool throttleLowAndMotorStop, int32_t throttleOffset);
void currentMeterVirtualInit(void);
void currentMeterVirtualRefresh(int32_t lastUpdateAt, bool armed, bool throttleLowAndMotorStop, int32_t throttleOffset);
void currentMeterVirtualRead(currentMeter_t *meter);
void resetCurrentMeterState(currentMeter_t *state);
void currentMeterESCInit(void);
void currentMeterESCRefresh(int32_t lastUpdateAt);
void currentMeterESCReadCombined(currentMeter_t *meter);
void currentMeterESCReadMotor(uint8_t motorNumber, currentMeter_t *meter);
//
// API for reading current meters by id.
//
extern const uint8_t supportedCurrentMeterCount;
extern const uint8_t currentMeterIds[];
void currentMeterRead(currentMeterId_e id, currentMeter_t *currentMeter);