Change current/voltage filters from biquad to pt1

This commit is contained in:
Hans Christian Olaussen 2019-02-10 15:01:40 +01:00
parent b8bfc7ce12
commit 62fafe8041
2 changed files with 9 additions and 9 deletions

View File

@ -88,7 +88,7 @@ void currentMeterReset(currentMeter_t *meter)
// ADC/Virtual shared // ADC/Virtual shared
// //
static biquadFilter_t adciBatFilter; static pt1Filter_t adciBatFilter;
#ifndef CURRENT_METER_SCALE_DEFAULT #ifndef CURRENT_METER_SCALE_DEFAULT
#define CURRENT_METER_SCALE_DEFAULT 400 // for Allegro ACS758LCB-100U (40mV/A) #define CURRENT_METER_SCALE_DEFAULT 400 // for Allegro ACS758LCB-100U (40mV/A)
@ -141,7 +141,7 @@ currentMeterADCState_t currentMeterADCState;
void currentMeterADCInit(void) void currentMeterADCInit(void)
{ {
memset(&currentMeterADCState, 0, sizeof(currentMeterADCState_t)); memset(&currentMeterADCState, 0, sizeof(currentMeterADCState_t));
biquadFilterInitLPF(&adciBatFilter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->ibatLpfPeriod), HZ_TO_INTERVAL_US(50)); pt1FilterInit(&adciBatFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->ibatLpfPeriod), HZ_TO_INTERVAL(50)));
} }
void currentMeterADCRefresh(int32_t lastUpdateAt) void currentMeterADCRefresh(int32_t lastUpdateAt)
@ -149,7 +149,7 @@ void currentMeterADCRefresh(int32_t lastUpdateAt)
#ifdef USE_ADC #ifdef USE_ADC
const uint16_t iBatSample = adcGetChannel(ADC_CURRENT); const uint16_t iBatSample = adcGetChannel(ADC_CURRENT);
currentMeterADCState.amperageLatest = currentMeterADCToCentiamps(iBatSample); currentMeterADCState.amperageLatest = currentMeterADCToCentiamps(iBatSample);
currentMeterADCState.amperage = currentMeterADCToCentiamps(biquadFilterApply(&adciBatFilter, iBatSample)); currentMeterADCState.amperage = currentMeterADCToCentiamps(pt1FilterApply(&adciBatFilter, iBatSample));
updateCurrentmAhDrawnState(&currentMeterADCState.mahDrawnState, currentMeterADCState.amperageLatest, lastUpdateAt); updateCurrentmAhDrawnState(&currentMeterADCState.mahDrawnState, currentMeterADCState.amperageLatest, lastUpdateAt);
#else #else

View File

@ -106,7 +106,7 @@ void voltageMeterReset(voltageMeter_t *meter)
typedef struct voltageMeterADCState_s { typedef struct voltageMeterADCState_s {
uint16_t voltageFiltered; // battery voltage in 0.01V steps (filtered) uint16_t voltageFiltered; // battery voltage in 0.01V steps (filtered)
uint16_t voltageUnfiltered; // battery voltage in 0.01V steps (unfiltered) uint16_t voltageUnfiltered; // battery voltage in 0.01V steps (unfiltered)
biquadFilter_t filter; pt1Filter_t filter;
} voltageMeterADCState_t; } voltageMeterADCState_t;
extern voltageMeterADCState_t voltageMeterADCStates[MAX_VOLTAGE_SENSOR_ADC]; extern voltageMeterADCState_t voltageMeterADCStates[MAX_VOLTAGE_SENSOR_ADC];
@ -164,7 +164,7 @@ void voltageMeterADCRefresh(void)
uint8_t channel = voltageMeterAdcChannelMap[i]; uint8_t channel = voltageMeterAdcChannelMap[i];
uint16_t rawSample = adcGetChannel(channel); uint16_t rawSample = adcGetChannel(channel);
uint16_t filteredSample = biquadFilterApply(&state->filter, rawSample); uint16_t filteredSample = pt1FilterApply(&state->filter, rawSample);
// always calculate the latest voltage, see getLatestVoltage() which does the calculation on demand. // always calculate the latest voltage, see getLatestVoltage() which does the calculation on demand.
state->voltageFiltered = voltageAdcToVoltage(filteredSample, config); state->voltageFiltered = voltageAdcToVoltage(filteredSample, config);
@ -194,7 +194,7 @@ void voltageMeterADCInit(void)
voltageMeterADCState_t *state = &voltageMeterADCStates[i]; voltageMeterADCState_t *state = &voltageMeterADCStates[i];
memset(state, 0, sizeof(voltageMeterADCState_t)); memset(state, 0, sizeof(voltageMeterADCState_t));
biquadFilterInitLPF(&state->filter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL_US(50)); pt1FilterInit(&state->filter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL(50)));
} }
} }
@ -206,7 +206,7 @@ void voltageMeterADCInit(void)
typedef struct voltageMeterESCState_s { typedef struct voltageMeterESCState_s {
uint16_t voltageFiltered; // battery voltage in 0.01V steps (filtered) uint16_t voltageFiltered; // battery voltage in 0.01V steps (filtered)
uint16_t voltageUnfiltered; // battery voltage in 0.01V steps (unfiltered) uint16_t voltageUnfiltered; // battery voltage in 0.01V steps (unfiltered)
biquadFilter_t filter; pt1Filter_t filter;
} voltageMeterESCState_t; } voltageMeterESCState_t;
static voltageMeterESCState_t voltageMeterESCState; static voltageMeterESCState_t voltageMeterESCState;
@ -218,7 +218,7 @@ void voltageMeterESCInit(void)
{ {
#ifdef USE_ESC_SENSOR #ifdef USE_ESC_SENSOR
memset(&voltageMeterESCState, 0, sizeof(voltageMeterESCState_t)); memset(&voltageMeterESCState, 0, sizeof(voltageMeterESCState_t));
biquadFilterInitLPF(&voltageMeterESCState.filter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL_US(50)); pt1FilterInit(&voltageMeterESCState.filter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL(50)));
#endif #endif
} }
@ -228,7 +228,7 @@ void voltageMeterESCRefresh(void)
escSensorData_t *escData = getEscSensorData(ESC_SENSOR_COMBINED); escSensorData_t *escData = getEscSensorData(ESC_SENSOR_COMBINED);
if (escData) { if (escData) {
voltageMeterESCState.voltageUnfiltered = escData->dataAge <= ESC_BATTERY_AGE_MAX ? escData->voltage : 0; voltageMeterESCState.voltageUnfiltered = escData->dataAge <= ESC_BATTERY_AGE_MAX ? escData->voltage : 0;
voltageMeterESCState.voltageFiltered = biquadFilterApply(&voltageMeterESCState.filter, voltageMeterESCState.voltageUnfiltered); voltageMeterESCState.voltageFiltered = pt1FilterApply(&voltageMeterESCState.filter, voltageMeterESCState.voltageUnfiltered);
} }
#endif #endif
} }