Bat measurment (#113)

* board: f1_dual: add way to measure battery through Heater-

* heater_control: allow board to limit max heater duty

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

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
This commit is contained in:
rusefillc 2022-07-15 03:07:13 -04:00 committed by GitHub
parent e51dc3ba0b
commit b298389fb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -41,6 +41,14 @@
#define R_HEATER_PORT GPIOB
#define R_HEATER_PIN 6
// PB7
#define L_HEATER_PORT GPIOB
#define L_HEATER_PIN 7
// PB6
#define R_HEATER_PORT GPIOB
#define R_HEATER_PIN 6
// PA1 TIM2_CH2
#define PUMP_DAC_PWM_DEVICE PWMD2
#define PUMP_DAC_PWM_CHANNEL 1

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

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

View File

@ -176,6 +176,12 @@ static void HeaterThread(void*)
float voltageRatio = heaterVoltage / batteryVoltage;
float duty = voltageRatio * voltageRatio;
#ifdef HEATER_MAX_DUTY
if (duty > HEATER_MAX_DUTY) {
duty = HEATER_MAX_DUTY;
}
#endif
if (batteryVoltage < 23)
{
// Pipe the output to the heater driver