From a1023900e9eff902790d6e8c5c6eac6091baeff5 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Fri, 21 Jul 2023 15:50:29 -0400 Subject: [PATCH] sampling, heater: fix Battery vs Heater naming mess --- firmware/boards/f0_module/port.cpp | 2 +- firmware/boards/f1_dual/port.cpp | 4 ++-- firmware/boards/f1_dual_rev1/port.cpp | 4 ++-- firmware/boards/f1_rev2/port.cpp | 7 +++++-- firmware/boards/f1_rev3/port.cpp | 7 +++++-- firmware/boards/port.h | 3 ++- firmware/heater_control.cpp | 16 ++++++++-------- firmware/livedata.cpp | 2 +- firmware/sampling.cpp | 2 +- 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/firmware/boards/f0_module/port.cpp b/firmware/boards/f0_module/port.cpp index b2263fa2..0c4589d9 100644 --- a/firmware/boards/f0_module/port.cpp +++ b/firmware/boards/f0_module/port.cpp @@ -47,7 +47,7 @@ AnalogResult AnalogSample() { .NernstVoltage = AverageSamples(adcBuffer, 0) * NERNST_INPUT_GAIN, .PumpCurrentVoltage = AverageSamples(adcBuffer, 1), - .BatteryVoltage = 0, + .HeaterSupplyVoltage = 0, }, }, .VirtualGroundVoltageInt = AverageSamples(adcBuffer, 2), diff --git a/firmware/boards/f1_dual/port.cpp b/firmware/boards/f1_dual/port.cpp index 217dade2..91cc1d19 100644 --- a/firmware/boards/f1_dual/port.cpp +++ b/firmware/boards/f1_dual/port.cpp @@ -111,13 +111,13 @@ AnalogResult AnalogSample() /* left */ .NernstVoltage = AverageSamples(adcBuffer, 3) * NERNST_INPUT_GAIN, .PumpCurrentVoltage = AverageSamples(adcBuffer, 2), - .BatteryVoltage = l_heater_voltage, + .HeaterSupplyVoltage = l_heater_voltage, }, { /* right */ .NernstVoltage = AverageSamples(adcBuffer, 1) * NERNST_INPUT_GAIN, .PumpCurrentVoltage = AverageSamples(adcBuffer, 0), - .BatteryVoltage = r_heater_voltage, + .HeaterSupplyVoltage = r_heater_voltage, }, }, /* Dual board has separate internal virtual ground = 3.3V / 2 diff --git a/firmware/boards/f1_dual_rev1/port.cpp b/firmware/boards/f1_dual_rev1/port.cpp index 91d884d6..0df8210c 100644 --- a/firmware/boards/f1_dual_rev1/port.cpp +++ b/firmware/boards/f1_dual_rev1/port.cpp @@ -135,10 +135,10 @@ AnalogResult AnalogSample() } /* left */ res.ch[0].PumpCurrentVoltage = AverageSamples(adcBuffer, 2); - res.ch[0].BatteryVoltage = l_heater_voltage; + res.ch[0].HeaterSupplyVoltage = l_heater_voltage; /* right */ res.ch[1].PumpCurrentVoltage = AverageSamples(adcBuffer, 0); - res.ch[1].BatteryVoltage = r_heater_voltage; + res.ch[1].HeaterSupplyVoltage = r_heater_voltage; return res; } diff --git a/firmware/boards/f1_rev2/port.cpp b/firmware/boards/f1_rev2/port.cpp index ad7b0b02..0e5782ec 100644 --- a/firmware/boards/f1_rev2/port.cpp +++ b/firmware/boards/f1_rev2/port.cpp @@ -66,8 +66,11 @@ AnalogResult AnalogSample() .PumpCurrentVoltage = AverageSamples(adcBuffer, 1), /* We also can measure output virtual ground voltage for diagnostic purposes */ //.VirtualGroundVoltageExt = AverageSamples(adcBuffer, 0) / VM_INPUT_DIVIDER, - .BatteryVoltage = AverageSamples(adcBuffer, 3) / BATTERY_INPUT_DIVIDER, - /* .HeaterVoltage = AverageSamples(adcBuffer, 4) / HEATER_INPUT_DIVIDER, */ + /* Heater measurement circuit has incorrect RC filter making inposible accurate + * measurement when heater pwm has high duty + * Assume WBO supply voltage == heater supply voltage */ + .HeaterSupplyVoltage = AverageSamples(adcBuffer, 3) / BATTERY_INPUT_DIVIDER, + /* .HeaterSupplyVoltage = AverageSamples(adcBuffer, 4) / HEATER_INPUT_DIVIDER, */ }, }, /* Rev 2 board has separate internal virtual ground = 3.3V / 2 diff --git a/firmware/boards/f1_rev3/port.cpp b/firmware/boards/f1_rev3/port.cpp index b4e13d6a..2a13d782 100644 --- a/firmware/boards/f1_rev3/port.cpp +++ b/firmware/boards/f1_rev3/port.cpp @@ -63,8 +63,11 @@ AnalogResult AnalogSample() .PumpCurrentVoltage = AverageSamples(adcBuffer, 1), /* We also can measure output virtual ground voltage for diagnostic purposes */ //.VirtualGroundVoltageExt = AverageSamples(adcBuffer, 0) / VM_INPUT_DIVIDER, - .BatteryVoltage = AverageSamples(adcBuffer, 3) / BATTERY_INPUT_DIVIDER, - /* .HeaterVoltage = AverageSamples(adcBuffer, 4) / HEATER_INPUT_DIVIDER, */ + /* Heater measurement circuit has incorrect RC filter making inposible accurate + * measurement when heater pwm has high duty + * Assume WBO supply voltage == heater supply voltage */ + .HeaterSupplyVoltage = AverageSamples(adcBuffer, 3) / BATTERY_INPUT_DIVIDER, + /* .HeaterSupplyVoltage = AverageSamples(adcBuffer, 4) / HEATER_INPUT_DIVIDER, */ }, }, /* Rev 2 board has separate internal virtual ground = 3.3V / 2 diff --git a/firmware/boards/port.h b/firmware/boards/port.h index c7c063d3..d8a94e08 100644 --- a/firmware/boards/port.h +++ b/firmware/boards/port.h @@ -12,13 +12,14 @@ struct AnalogChannelResult float PumpCurrentVoltage; /* for dual version - this is voltage on Heater-, switches between zero and Vbatt with heater PWM, * used for both Vbatt measurement and Heater diagnostic */ - float BatteryVoltage; + float HeaterSupplyVoltage; }; struct AnalogResult { AnalogChannelResult ch[AFR_CHANNELS]; float VirtualGroundVoltageInt; + /* TODO: add SupplyVoltage - some boards can measure supply voltage */ }; AnalogResult AnalogSample(); diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index fca0d2cd..b1c00b50 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -37,7 +37,7 @@ HeaterState HeaterControllerBase::GetHeaterState() const return heaterState; } -HeaterState HeaterControllerBase::GetNextState(HeaterState currentState, HeaterAllow heaterAllowState, float batteryVoltage, float sensorTemp) +HeaterState HeaterControllerBase::GetNextState(HeaterState currentState, HeaterAllow heaterAllowState, float heaterSupplyVoltage, float sensorTemp) { bool heaterAllowed = heaterAllowState == HeaterAllow::Allowed; @@ -45,14 +45,14 @@ HeaterState HeaterControllerBase::GetNextState(HeaterState currentState, HeaterA if (heaterAllowState == HeaterAllow::Unknown) { // measured voltage too low to auto-start heating - if (batteryVoltage < HEATER_BATTETY_OFF_VOLTAGE) + if (heaterSupplyVoltage < HEATER_BATTETY_OFF_VOLTAGE) { m_batteryStableTimer.reset(); return HeaterState::NoHeaterSupply; } - else if (batteryVoltage > HEATER_BATTERY_ON_VOLTAGE) + else if (heaterSupplyVoltage > HEATER_BATTERY_ON_VOLTAGE) { - // measured voltage is high enougth to auto-start heating, wait some time to stabilize + // measured voltage is high enough to auto-start heating, wait some time to stabilize heaterAllowed = m_batteryStableTimer.hasElapsedSec(HEATER_BATTERY_STAB_TIME); } } @@ -176,12 +176,12 @@ void HeaterControllerBase::Update(const ISampler& sampler, HeaterAllow heaterAll // If we haven't heard from the ECU, use the internally sensed // battery voltage instead of voltage over CAN. - float batteryVoltage = heaterAllowState == HeaterAllow::Unknown + float heaterSupplyVoltage = heaterAllowState == HeaterAllow::Unknown ? sampler.GetInternalHeaterVoltage() : GetRemoteBatteryVoltage(); // Run the state machine - heaterState = GetNextState(heaterState, heaterAllowState, batteryVoltage, sensorTemperature); + heaterState = GetNextState(heaterState, heaterAllowState, heaterSupplyVoltage, sensorTemperature); float heaterVoltage = GetVoltageForState(heaterState, sensorEsr); // Limit to 11 volts @@ -190,7 +190,7 @@ void HeaterControllerBase::Update(const ISampler& sampler, HeaterAllow heaterAll } // duty = (V_eff / V_batt) ^ 2 - float voltageRatio = (batteryVoltage < 1.0f) ? 0 : heaterVoltage / batteryVoltage; + float voltageRatio = (heaterSupplyVoltage < 1.0f) ? 0 : heaterVoltage / heaterSupplyVoltage; float duty = voltageRatio * voltageRatio; #ifdef HEATER_MAX_DUTY @@ -203,7 +203,7 @@ void HeaterControllerBase::Update(const ISampler& sampler, HeaterAllow heaterAll } #endif - if (batteryVoltage >= 23) + if (heaterSupplyVoltage >= 23) { duty = 0; heaterVoltage = 0; diff --git a/firmware/livedata.cpp b/firmware/livedata.cpp index 7a0c4e7c..d69a37b7 100644 --- a/firmware/livedata.cpp +++ b/firmware/livedata.cpp @@ -36,7 +36,7 @@ void SamplingUpdateLiveData() data->heaterState = (uint8_t)GetHeaterState(ch); } - livedata_common.vbatt = GetSampler(0).GetInternalBatteryVoltage(); + livedata_common.vbatt = GetSampler(0).GetInternalHeaterVoltage(); } template<> diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index be37534b..415e5ca4 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -115,7 +115,7 @@ void Sampler::ApplySample(AnalogChannelResult& result, float virtualGroundVoltag PUMP_FILTER_ALPHA * (result.PumpCurrentVoltage - virtualGroundVoltageInt); #ifdef BATTERY_INPUT_DIVIDER - internalHeaterVoltage = result.HeaterVoltage; + internalHeaterVoltage = result.HeaterSupplyVoltage; #endif // Shift history over by one