From b8f11741b559c0bd46693ce4a74bf0ebfeee8a17 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 20 Jun 2023 17:26:54 -0700 Subject: [PATCH] heater uses sampling class --- firmware/heater_control.cpp | 8 ++-- firmware/sampling.cpp | 74 +++++++++++++++++++++++-------------- firmware/sampling.h | 2 + 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index b34c06f..487f0db 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -257,16 +257,18 @@ static void HeaterThread(void*) auto heaterAllowState = GetHeaterAllowed(); for (i = 0; i < AFR_CHANNELS; i++) { + const auto& sampler = GetSampler(i); + heater_state &s = state[i]; // Read sensor state - float heaterEsr = GetSensorInternalResistance(s.ch); - float sensorTemperature = GetSensorTemperature(s.ch); + float heaterEsr = sampler.GetSensorInternalResistance(); + float sensorTemperature = sampler.GetSensorTemperature(); // If we haven't heard from rusEFI, use the internally sensed // battery voltage instead of voltage over CAN. float batteryVoltage = heaterAllowState == HeaterAllow::Unknown - ? GetInternalBatteryVoltage(s.ch) + ? sampler.GetInternalBatteryVoltage() : GetRemoteBatteryVoltage(); // Run the state machine diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index ec2ebbc..c43a653 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -25,15 +25,18 @@ struct Sampler : public ISampler { public: void ApplySample(AnalogChannelResult& result, float virtualGroundVoltageInt); - float GetNernstDc() const override { + float GetNernstDc() const override + { return nernstDc; } - float GetNernstAc() const override { + float GetNernstAc() const override + { return nernstAc; } - float GetPumpNominalCurrent() const override { + float GetPumpNominalCurrent() const override + { // Gain is 10x, then a 61.9 ohm resistor // Effective resistance with the gain is 619 ohms // 1000 is to convert to milliamperes @@ -41,13 +44,45 @@ public: return pumpCurrentSenseVoltage * ratio; } - float GetInternalBatteryVoltage() const override { + float GetInternalBatteryVoltage() const override + { // Dual HW can measure heater voltage for each channel // by measuring voltage on Heater- while FET is off // TODO: rename function? return internalBatteryVoltage; } + float GetSensorTemperature() const override + { + float esr = GetSensorInternalResistance(); + + if (esr > 5000) + { + return 0; + } + + switch (GetSensorType()) { + case SensorType::LSU49: + return interpolate2d(esr, lsu49TempBins, lsu49TempValues); + case SensorType::LSU42: + return interpolate2d(esr, lsu42TempBins, lsu42TempValues); + case SensorType::LSUADV: + return interpolate2d(esr, lsuAdvTempBins, lsuAdvTempValues); + } + + return 0; + } + + float GetSensorInternalResistance() const override + { + // Sensor is the lowside of a divider, top side is GetESRSupplyR(), and 3.3v AC pk-pk is injected + float totalEsr = GetESRSupplyR() / (VCC_VOLTS / GetNernstAc() - 1); + + // There is a resistor between the opamp and Vm sensor pin. Remove the effect of that + // resistor so that the remainder is only the ESR of the sensor itself + return totalEsr - VM_RESISTOR_VALUE; + } + private: float r_2 = 0; float r_3 = 0; @@ -60,6 +95,11 @@ private: static Sampler samplers[AFR_CHANNELS]; +ISampler& GetSampler(int ch) +{ + return samplers[ch]; +} + constexpr float f_abs(float x) { return x > 0 ? x : -x; @@ -139,35 +179,15 @@ float GetNernstAc(int ch) return samplers[ch].GetNernstAc(); } +// TODO: remove these helpers float GetSensorInternalResistance(int ch) { - // Sensor is the lowside of a divider, top side is GetESRSupplyR(), and 3.3v AC pk-pk is injected - float totalEsr = GetESRSupplyR() / (VCC_VOLTS / GetNernstAc(ch) - 1); - - // There is a resistor between the opamp and Vm sensor pin. Remove the effect of that - // resistor so that the remainder is only the ESR of the sensor itself - return totalEsr - VM_RESISTOR_VALUE; + return samplers[ch].GetSensorInternalResistance(); } float GetSensorTemperature(int ch) { - float esr = GetSensorInternalResistance(ch); - - if (esr > 5000) - { - return 0; - } - - switch (GetSensorType()) { - case SensorType::LSU49: - return interpolate2d(esr, lsu49TempBins, lsu49TempValues); - case SensorType::LSU42: - return interpolate2d(esr, lsu42TempBins, lsu42TempValues); - case SensorType::LSUADV: - return interpolate2d(esr, lsuAdvTempBins, lsuAdvTempValues); - } - - return 0; + return samplers[ch].GetSensorTemperature(); } float GetNernstDc(int ch) diff --git a/firmware/sampling.h b/firmware/sampling.h index cdf2fb7..fbdfa87 100644 --- a/firmware/sampling.h +++ b/firmware/sampling.h @@ -6,6 +6,8 @@ struct ISampler virtual float GetNernstAc() const = 0; virtual float GetPumpNominalCurrent() const = 0; virtual float GetInternalBatteryVoltage() const = 0; + virtual float GetSensorTemperature() const = 0; + virtual float GetSensorInternalResistance() const = 0; }; // Get the sampler for a particular channel