2020-10-30 01:53:54 -07:00
|
|
|
#include "heater_control.h"
|
2020-10-31 14:58:34 -07:00
|
|
|
#include "wideband_config.h"
|
2020-10-30 01:53:54 -07:00
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
#include "ch.h"
|
2020-10-30 01:53:54 -07:00
|
|
|
#include "hal.h"
|
2020-12-10 18:32:41 -08:00
|
|
|
|
|
|
|
#include "fault.h"
|
2020-10-30 01:53:54 -07:00
|
|
|
#include "pwm.h"
|
|
|
|
#include "sampling.h"
|
2020-10-31 14:58:34 -07:00
|
|
|
#include "pid.h"
|
2022-04-06 01:22:09 -07:00
|
|
|
#include "can.h"
|
2020-10-30 01:53:54 -07:00
|
|
|
|
2022-01-01 21:10:55 -08:00
|
|
|
using namespace wbo;
|
|
|
|
|
2020-10-30 01:53:54 -07:00
|
|
|
// 400khz / 1024 = 390hz PWM
|
2022-01-25 19:11:57 -08:00
|
|
|
static Pwm heaterPwm(HEATER_PWM_DEVICE, HEATER_PWM_CHANNEL, 400'000, 1024);
|
2020-10-30 01:53:54 -07:00
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
enum class HeaterState
|
|
|
|
{
|
|
|
|
Preheat,
|
|
|
|
WarmupRamp,
|
|
|
|
ClosedLoop,
|
2020-12-10 18:32:41 -08:00
|
|
|
Stopped,
|
2020-10-30 02:40:18 -07:00
|
|
|
};
|
|
|
|
|
2021-07-15 21:50:22 -07:00
|
|
|
constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD;
|
|
|
|
static int timeCounter = preheatTimeCounter;
|
|
|
|
static float rampVoltage = 0;
|
2020-10-30 02:40:18 -07:00
|
|
|
|
2022-04-06 01:22:09 -07:00
|
|
|
static HeaterState GetNextState(HeaterState state, HeaterAllow heaterAllowState, float batteryVoltage, float sensorEsr)
|
2020-10-30 02:40:18 -07:00
|
|
|
{
|
2022-04-06 01:22:09 -07:00
|
|
|
// TODO: smarter state machine to use internal vbatt when CAN not connected
|
|
|
|
bool heaterAllowed = heaterAllowState == HeaterAllow::Allowed;
|
|
|
|
|
2021-07-15 21:50:22 -07:00
|
|
|
if (!heaterAllowed)
|
|
|
|
{
|
|
|
|
// ECU hasn't allowed preheat yet, reset timer, and force preheat state
|
|
|
|
timeCounter = preheatTimeCounter;
|
|
|
|
return HeaterState::Preheat;
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case HeaterState::Preheat:
|
2020-12-10 18:32:41 -08:00
|
|
|
timeCounter--;
|
2020-10-30 02:40:18 -07:00
|
|
|
|
2020-12-13 17:24:33 -08:00
|
|
|
// If preheat timeout, or sensor is already hot (engine running?)
|
|
|
|
if (timeCounter <= 0 || sensorEsr < HEATER_CLOSED_LOOP_THRESHOLD_ESR)
|
2020-10-30 02:40:18 -07:00
|
|
|
{
|
|
|
|
// If enough time has elapsed, start the ramp
|
2021-07-12 15:24:20 -07:00
|
|
|
// Start the ramp at 4 volts
|
2021-07-16 10:36:05 -07:00
|
|
|
rampVoltage = 4;
|
2020-12-10 18:32:41 -08:00
|
|
|
|
|
|
|
// Next phase times out at 15 seconds
|
2020-12-10 22:08:00 -08:00
|
|
|
timeCounter = HEATER_WARMUP_TIMEOUT / HEATER_CONTROL_PERIOD;
|
2020-12-10 18:32:41 -08:00
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
return HeaterState::WarmupRamp;
|
|
|
|
}
|
2020-10-31 14:54:50 -07:00
|
|
|
|
|
|
|
// Stay in preheat - wait for time to elapse
|
|
|
|
break;
|
2020-10-30 02:40:18 -07:00
|
|
|
case HeaterState::WarmupRamp:
|
2020-12-10 22:08:00 -08:00
|
|
|
if (sensorEsr < HEATER_CLOSED_LOOP_THRESHOLD_ESR)
|
2020-10-30 02:40:18 -07:00
|
|
|
{
|
|
|
|
return HeaterState::ClosedLoop;
|
|
|
|
}
|
2020-12-10 18:32:41 -08:00
|
|
|
else if (timeCounter == 0)
|
|
|
|
{
|
2021-11-03 23:02:11 -07:00
|
|
|
SetFault(Fault::SensorDidntHeat);
|
2020-12-10 18:32:41 -08:00
|
|
|
return HeaterState::Stopped;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeCounter--;
|
2020-10-31 14:54:50 -07:00
|
|
|
|
|
|
|
break;
|
2020-12-10 18:32:41 -08:00
|
|
|
case HeaterState::ClosedLoop:
|
2021-11-04 15:07:47 -07:00
|
|
|
// Check that the sensor's ESR is acceptable for normal operation
|
2020-12-11 15:46:03 -08:00
|
|
|
if (sensorEsr < HEATER_OVERHEAT_ESR)
|
2020-12-10 22:08:00 -08:00
|
|
|
{
|
2021-11-03 23:02:11 -07:00
|
|
|
SetFault(Fault::SensorOverheat);
|
2020-12-11 15:10:04 -08:00
|
|
|
return HeaterState::Stopped;
|
2020-12-10 22:08:00 -08:00
|
|
|
}
|
2020-12-11 15:46:03 -08:00
|
|
|
else if (sensorEsr > HEATER_UNDERHEAT_ESR)
|
|
|
|
{
|
2021-11-03 23:02:11 -07:00
|
|
|
SetFault(Fault::SensorUnderheat);
|
2020-12-11 15:46:03 -08:00
|
|
|
return HeaterState::Stopped;
|
|
|
|
}
|
2020-12-10 22:08:00 -08:00
|
|
|
|
2020-12-10 18:32:41 -08:00
|
|
|
break;
|
|
|
|
case HeaterState::Stopped: break;
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
2020-10-31 14:54:50 -07:00
|
|
|
|
|
|
|
return state;
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
|
|
|
|
2021-07-12 15:46:10 -07:00
|
|
|
static Pid heaterPid(
|
2021-07-12 22:40:47 -07:00
|
|
|
0.3f, // kP
|
|
|
|
0.3f, // kI
|
2021-07-12 23:49:55 -07:00
|
|
|
0.01f, // kD
|
2021-07-12 22:40:47 -07:00
|
|
|
3.0f, // Integrator clamp (volts)
|
2021-07-12 15:46:10 -07:00
|
|
|
HEATER_CONTROL_PERIOD
|
|
|
|
);
|
2021-07-09 23:15:24 -07:00
|
|
|
|
2021-07-12 15:24:20 -07:00
|
|
|
static float GetVoltageForState(HeaterState state, float heaterEsr)
|
2020-10-30 02:40:18 -07:00
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
2021-07-15 21:48:24 -07:00
|
|
|
case HeaterState::Preheat:
|
|
|
|
// Max allowed during condensation phase (preheat) is 2v
|
|
|
|
return 1.5f;
|
2020-10-30 02:40:18 -07:00
|
|
|
case HeaterState::WarmupRamp:
|
2021-07-12 23:45:35 -07:00
|
|
|
if (rampVoltage < 10)
|
2020-10-30 02:40:18 -07:00
|
|
|
{
|
2021-07-09 23:15:24 -07:00
|
|
|
// 0.3 volt per second, divided by battery voltage and update rate
|
|
|
|
constexpr float rampRateVoltPerSecond = 0.3f;
|
2021-04-26 17:40:06 -07:00
|
|
|
constexpr float heaterFrequency = 1000.0f / HEATER_CONTROL_PERIOD;
|
2021-07-12 15:24:20 -07:00
|
|
|
rampVoltage += (rampRateVoltPerSecond / heaterFrequency);
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
|
|
|
|
2021-07-12 15:24:20 -07:00
|
|
|
return rampVoltage;
|
2020-10-30 02:40:18 -07:00
|
|
|
case HeaterState::ClosedLoop:
|
2021-07-12 15:46:10 -07:00
|
|
|
// "nominal" heater voltage is 7.5v, so apply correction around that point (instead of relying on integrator so much)
|
2020-10-31 17:34:36 -07:00
|
|
|
// Negated because lower resistance -> hotter
|
2021-07-12 15:46:10 -07:00
|
|
|
return 7.5f - heaterPid.GetOutput(HEATER_TARGET_ESR, heaterEsr);
|
2020-12-10 18:32:41 -08:00
|
|
|
case HeaterState::Stopped:
|
2021-07-12 15:24:20 -07:00
|
|
|
// Something has gone wrong, turn off the heater.
|
2020-12-10 18:32:41 -08:00
|
|
|
return 0;
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
2020-12-10 21:05:02 -08:00
|
|
|
|
|
|
|
// should be unreachable
|
|
|
|
return 0;
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:54:50 -07:00
|
|
|
static HeaterState state = HeaterState::Preheat;
|
|
|
|
|
2021-07-12 15:31:02 -07:00
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
static THD_WORKING_AREA(waHeaterThread, 256);
|
|
|
|
static void HeaterThread(void*)
|
|
|
|
{
|
2021-06-03 21:03:41 -07:00
|
|
|
// Wait for temperature sensing to stabilize so we don't
|
|
|
|
// immediately think we overshot the target temperature
|
|
|
|
chThdSleepMilliseconds(1000);
|
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Read sensor state
|
|
|
|
float heaterEsr = GetSensorInternalResistance();
|
|
|
|
|
2022-04-06 01:22:09 -07:00
|
|
|
auto heaterAllowState = GetHeaterAllowed();
|
|
|
|
|
|
|
|
// If we haven't heard from rusEFI, use the internally sensed
|
|
|
|
// battery voltage instead of voltage over CAN.
|
|
|
|
float batteryVoltage = heaterAllowState == HeaterAllow::Unknown
|
|
|
|
? GetInternalBatteryVoltage()
|
|
|
|
: GetRemoteBatteryVoltage();
|
|
|
|
|
2020-10-30 02:40:18 -07:00
|
|
|
// Run the state machine
|
2022-04-06 01:22:09 -07:00
|
|
|
state = GetNextState(state, heaterAllowState, batteryVoltage, heaterEsr);
|
2021-07-12 15:33:10 -07:00
|
|
|
float heaterVoltage = GetVoltageForState(state, heaterEsr);
|
2020-10-30 02:40:18 -07:00
|
|
|
|
2021-07-12 23:45:35 -07:00
|
|
|
// Limit to 11 volts
|
|
|
|
if (heaterVoltage > 11) {
|
|
|
|
heaterVoltage = 11;
|
2021-07-09 23:15:24 -07:00
|
|
|
}
|
|
|
|
|
2021-07-12 15:32:45 -07:00
|
|
|
// duty = (V_eff / V_batt) ^ 2
|
2021-07-12 15:33:10 -07:00
|
|
|
float voltageRatio = heaterVoltage / batteryVoltage;
|
2021-07-12 15:32:45 -07:00
|
|
|
float duty = voltageRatio * voltageRatio;
|
2021-07-12 15:24:20 -07:00
|
|
|
|
2021-07-12 22:44:42 -07:00
|
|
|
if (batteryVoltage < 23)
|
|
|
|
{
|
|
|
|
// Pipe the output to the heater driver
|
|
|
|
heaterPwm.SetDuty(duty);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Overvoltage protection - sensor not rated for PWM above 24v
|
|
|
|
heaterPwm.SetDuty(0);
|
|
|
|
}
|
2020-10-30 02:40:18 -07:00
|
|
|
|
|
|
|
// Loop at ~20hz
|
2020-10-31 14:58:34 -07:00
|
|
|
chThdSleepMilliseconds(HEATER_CONTROL_PERIOD);
|
2020-10-30 02:40:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 01:53:54 -07:00
|
|
|
void StartHeaterControl()
|
|
|
|
{
|
|
|
|
heaterPwm.Start();
|
|
|
|
heaterPwm.SetDuty(0);
|
2020-10-30 02:40:18 -07:00
|
|
|
|
|
|
|
chThdCreateStatic(waHeaterThread, sizeof(waHeaterThread), NORMALPRIO + 1, HeaterThread, nullptr);
|
2020-10-30 01:53:54 -07:00
|
|
|
}
|
2020-10-31 14:54:50 -07:00
|
|
|
|
|
|
|
bool IsRunningClosedLoop()
|
|
|
|
{
|
|
|
|
return state == HeaterState::ClosedLoop;
|
|
|
|
}
|
2021-07-12 15:31:02 -07:00
|
|
|
|
2022-01-26 10:56:58 -08:00
|
|
|
float GetHeaterDuty()
|
2022-01-04 11:16:46 -08:00
|
|
|
{
|
|
|
|
return heaterPwm.GetLastDuty();
|
|
|
|
}
|