mirror of https://github.com/rusefi/wideband.git
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:
parent
e51dc3ba0b
commit
b298389fb5
|
@ -41,6 +41,14 @@
|
||||||
#define R_HEATER_PORT GPIOB
|
#define R_HEATER_PORT GPIOB
|
||||||
#define R_HEATER_PIN 6
|
#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
|
// PA1 TIM2_CH2
|
||||||
#define PUMP_DAC_PWM_DEVICE PWMD2
|
#define PUMP_DAC_PWM_DEVICE PWMD2
|
||||||
#define PUMP_DAC_PWM_CHANNEL 1
|
#define PUMP_DAC_PWM_CHANNEL 1
|
||||||
|
|
|
@ -60,6 +60,20 @@ static float AverageSamples(adcsample_t* buffer, size_t idx)
|
||||||
return (float)sum * scale;
|
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;
|
static float l_vbatt = 0.0, r_vbatt = 0.0;
|
||||||
|
|
||||||
AnalogResult AnalogSample()
|
AnalogResult AnalogSample()
|
||||||
|
@ -70,9 +84,11 @@ AnalogResult AnalogSample()
|
||||||
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
|
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
|
||||||
|
|
||||||
if ((l_heater) && (!palReadPad(L_HEATER_PORT, L_HEATER_PIN)))
|
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)))
|
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
|
return
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
// *******************************
|
// *******************************
|
||||||
// 100K + 10K divider
|
// 100K + 10K divider
|
||||||
#define BATTERY_INPUT_DIVIDER (10.0 / (10.0 + 100.0))
|
#define BATTERY_INPUT_DIVIDER (10.0 / (10.0 + 100.0))
|
||||||
|
#define BATTERY_FILTER_ALPHA (0.1f)
|
||||||
|
|
||||||
// *******************************
|
// *******************************
|
||||||
// Heater low side Sensing
|
// Heater low side Sensing
|
||||||
|
|
|
@ -176,6 +176,12 @@ static void HeaterThread(void*)
|
||||||
float voltageRatio = heaterVoltage / batteryVoltage;
|
float voltageRatio = heaterVoltage / batteryVoltage;
|
||||||
float duty = voltageRatio * voltageRatio;
|
float duty = voltageRatio * voltageRatio;
|
||||||
|
|
||||||
|
#ifdef HEATER_MAX_DUTY
|
||||||
|
if (duty > HEATER_MAX_DUTY) {
|
||||||
|
duty = HEATER_MAX_DUTY;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (batteryVoltage < 23)
|
if (batteryVoltage < 23)
|
||||||
{
|
{
|
||||||
// Pipe the output to the heater driver
|
// Pipe the output to the heater driver
|
||||||
|
|
Loading…
Reference in New Issue