board: f1_dual: use max sample for battery measurement, filter

This commit is contained in:
Andrey Gusakov 2022-06-06 22:18:43 +03:00
parent 7edd3bd753
commit a754ceeeb1
2 changed files with 19 additions and 2 deletions

View File

@ -60,6 +60,20 @@ static float AverageSamples(adcsample_t* buffer, size_t idx)
return (float)sum * scale;
}
static float GetMaxSample(adcsample_t* buffer, size_t idx)
{
uint32_t max = 0;
for (size_t i = 0; i < ADC_OVERSAMPLE; i++) {
if (buffer[idx] > max)
max = buffer[idx];
idx += ADC_CHANNEL_COUNT;
}
constexpr float scale = VCC_VOLTS / ADC_MAX_COUNT;
return (float) max * scale;
}
static float l_vbatt = 0.0, r_vbatt = 0.0;
AnalogResult AnalogSample()
@ -70,9 +84,11 @@ AnalogResult AnalogSample()
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
if ((l_heater) && (!palReadPad(L_HEATER_PORT, L_HEATER_PIN)))
l_vbatt = AverageSamples(adcBuffer, 6) / BATTERY_INPUT_DIVIDER;
l_vbatt = BATTERY_FILTER_ALPHA * GetMaxSample(adcBuffer, 6) / BATTERY_INPUT_DIVIDER
+ (1.0 - BATTERY_FILTER_ALPHA) * l_vbatt;
if ((r_heater) && (!palReadPad(R_HEATER_PORT, R_HEATER_PIN)))
r_vbatt = AverageSamples(adcBuffer, 7) / BATTERY_INPUT_DIVIDER;
r_vbatt = BATTERY_FILTER_ALPHA * GetMaxSample(adcBuffer, 7) / BATTERY_INPUT_DIVIDER
+ (1.0 - BATTERY_FILTER_ALPHA) * r_vbatt;
return
{

View File

@ -19,6 +19,7 @@
// *******************************
// 100K + 10K divider
#define BATTERY_INPUT_DIVIDER (10.0 / (10.0 + 100.0))
#define BATTERY_FILTER_ALPHA (0.1f)
// *******************************
// Heater low side Sensing