diff --git a/firmware/can.cpp b/firmware/can.cpp index ecd336e..1f50ea6 100644 --- a/firmware/can.cpp +++ b/firmware/can.cpp @@ -61,8 +61,8 @@ void CanRxThread(void*) SetBatteryVoltage(vbatt); // data1 contains heater enable bit - // TODO use this - //bool heaterEnabled = (frame.data8[1] & 0x1) == 0x1; + bool heaterAllowed = (frame.data8[1] & 0x1) == 0x1; + SetHeaterAllowed(heaterAllowed); } // If it's a bootloader entry request, reboot to the bootloader! else if (frame.DLC == 0 && frame.EID == 0xEF0'0000) diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index 5b2d68e..a53b8c8 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -20,11 +20,20 @@ enum class HeaterState Stopped, }; -int timeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; -float rampVoltage = 0; +constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; +static int timeCounter = preheatTimeCounter; +static float rampVoltage = 0; +static bool heaterAllowed = false; static HeaterState GetNextState(HeaterState state, float sensorEsr) { + if (!heaterAllowed) + { + // ECU hasn't allowed preheat yet, reset timer, and force preheat state + timeCounter = preheatTimeCounter; + return HeaterState::Preheat; + } + switch (state) { case HeaterState::Preheat: @@ -187,3 +196,8 @@ void SetBatteryVoltage(float vbatt) batteryVoltage = vbatt; } } + +void SetHeaterAllowed(bool allowed) +{ + heaterAllowed = allowed; +} diff --git a/firmware/heater_control.h b/firmware/heater_control.h index 0dc0959..9e413b3 100644 --- a/firmware/heater_control.h +++ b/firmware/heater_control.h @@ -5,3 +5,4 @@ void StartHeaterControl(); bool IsRunningClosedLoop(); void SetBatteryVoltage(float vbatt); +void SetHeaterAllowed(bool allowed);