From a7b081b882ae3de9477268ed0141ae785480f8b9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Mon, 13 Nov 2023 12:55:48 -0800 Subject: [PATCH] extract heater class declaration --- firmware/heater_control.cpp | 67 +++++++++---------------------------- firmware/heater_control.h | 46 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index d4de98a..693d069 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -7,7 +7,6 @@ #include "fault.h" #include "pwm.h" #include "sampling.h" -#include "pid.h" struct sensorHeaterParams { float targetTemp; @@ -62,63 +61,27 @@ static const PWMConfig heaterPwmConfig = { .dier = 0 }; -static constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; -static constexpr int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD; static const struct sensorHeaterParams *heater; -class HeaterController : public IHeaterController +HeaterController::HeaterController(int ch, int pwm_ch) + : ch(ch), pwm_ch(pwm_ch) { -public: - HeaterController(int ch, int pwm_ch) - : ch(ch), pwm_ch(pwm_ch) - { - } +} - void Update(const ISampler& sampler, HeaterAllow heaterAllowState) override; +bool HeaterController::IsRunningClosedLoop() const +{ + return heaterState == HeaterState::ClosedLoop; +} - bool IsRunningClosedLoop() const override - { - return heaterState == HeaterState::ClosedLoop; - } +float HeaterController::GetHeaterEffectiveVoltage() const +{ + return heaterVoltage; +} - float GetHeaterEffectiveVoltage() const override - { - return heaterVoltage; - } - - HeaterState GetHeaterState() const override - { - return heaterState; - } - -protected: - HeaterState GetNextState(HeaterAllow haeterAllowState, float batteryVoltage, float sensorTemp); - float GetVoltageForState(float heaterEsr); - -private: - Pid heaterPid = - { - 0.3f, // kP - 0.3f, // kI - 0.01f, // kD - 3.0f, // Integrator clamp (volts) - HEATER_CONTROL_PERIOD - }; - - int timeCounter = preheatTimeCounter; - int batteryStabTime = batteryStabTimeCounter; - float rampVoltage = 0; - float heaterVoltage = 0; - HeaterState heaterState = HeaterState::Preheat; -#ifdef HEATER_MAX_DUTY - int cycle; -#endif - -// TODO: private: -public: - const uint8_t ch; - const uint8_t pwm_ch; -}; +HeaterState HeaterController::GetHeaterState() const +{ + return heaterState; +} HeaterController heaterControllers[AFR_CHANNELS] = { diff --git a/firmware/heater_control.h b/firmware/heater_control.h index 47b34b5..0340d4a 100644 --- a/firmware/heater_control.h +++ b/firmware/heater_control.h @@ -2,7 +2,10 @@ #include +#include "wideband_config.h" + #include "can.h" +#include "pid.h" enum class HeaterState { @@ -23,6 +26,49 @@ struct IHeaterController virtual HeaterState GetHeaterState() const = 0; }; +class HeaterController : public IHeaterController +{ +public: + HeaterController(int ch, int pwm_ch); + + void Update(const ISampler& sampler, HeaterAllow heaterAllowState) override; + + bool IsRunningClosedLoop() const override; + float GetHeaterEffectiveVoltage() const override; + HeaterState GetHeaterState() const override; + +protected: + HeaterState GetNextState(HeaterAllow haeterAllowState, float batteryVoltage, float sensorTemp); + float GetVoltageForState(float heaterEsr); + +private: + Pid heaterPid = + { + 0.3f, // kP + 0.3f, // kI + 0.01f, // kD + 3.0f, // Integrator clamp (volts) + HEATER_CONTROL_PERIOD + }; + + int timeCounter = preheatTimeCounter; + int batteryStabTime = batteryStabTimeCounter; + float rampVoltage = 0; + float heaterVoltage = 0; + HeaterState heaterState = HeaterState::Preheat; +#ifdef HEATER_MAX_DUTY + int cycle; +#endif + +// TODO: private: +public: + const uint8_t ch; + const uint8_t pwm_ch; + + static const int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; + static const int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD; +}; + const IHeaterController& GetHeaterController(int ch); void StartHeaterControl();