2020-10-30 01:53:54 -07:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-15 15:00:11 -08:00
|
|
|
#include <cstdint>
|
|
|
|
|
2023-11-13 12:55:48 -08:00
|
|
|
#include "wideband_config.h"
|
|
|
|
|
2023-06-23 14:38:17 -07:00
|
|
|
#include "can.h"
|
2023-11-13 12:55:48 -08:00
|
|
|
#include "pid.h"
|
2023-06-23 14:38:17 -07:00
|
|
|
|
2022-05-11 01:41:07 -07:00
|
|
|
enum class HeaterState
|
|
|
|
{
|
|
|
|
Preheat,
|
|
|
|
WarmupRamp,
|
|
|
|
ClosedLoop,
|
|
|
|
Stopped,
|
2022-12-01 13:20:54 -08:00
|
|
|
NoHeaterSupply,
|
2022-05-11 01:41:07 -07:00
|
|
|
};
|
|
|
|
|
2023-06-23 14:38:17 -07:00
|
|
|
struct ISampler;
|
|
|
|
|
|
|
|
struct IHeaterController
|
|
|
|
{
|
|
|
|
virtual void Update(const ISampler& sampler, HeaterAllow heaterAllowState) = 0;
|
|
|
|
virtual bool IsRunningClosedLoop() const = 0;
|
|
|
|
virtual float GetHeaterEffectiveVoltage() const = 0;
|
|
|
|
virtual HeaterState GetHeaterState() const = 0;
|
|
|
|
};
|
|
|
|
|
2023-11-13 13:30:23 -08:00
|
|
|
class HeaterControllerBase : public IHeaterController
|
2023-11-13 12:55:48 -08:00
|
|
|
{
|
|
|
|
public:
|
2023-11-13 13:30:23 -08:00
|
|
|
HeaterControllerBase(int ch);
|
|
|
|
void Configure(float targetTempC, float targetEsr);
|
2023-11-13 12:55:48 -08:00
|
|
|
void Update(const ISampler& sampler, HeaterAllow heaterAllowState) override;
|
|
|
|
|
|
|
|
bool IsRunningClosedLoop() const override;
|
|
|
|
float GetHeaterEffectiveVoltage() const override;
|
|
|
|
HeaterState GetHeaterState() const override;
|
|
|
|
|
2023-11-13 13:30:23 -08:00
|
|
|
virtual void SetDuty(float duty) const = 0;
|
|
|
|
|
2023-11-13 13:56:03 -08:00
|
|
|
HeaterState GetNextState(HeaterState currentState, HeaterAllow haeterAllowState, float batteryVoltage, float sensorTemp);
|
|
|
|
float GetVoltageForState(HeaterState state, float sensorEsr);
|
2023-11-13 12:55:48 -08:00
|
|
|
|
2023-11-13 14:15:22 -08:00
|
|
|
int GetTimeCounter() const
|
|
|
|
{
|
|
|
|
return timeCounter;
|
|
|
|
}
|
|
|
|
|
2023-11-13 12:55:48 -08:00
|
|
|
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
|
|
|
|
|
2023-11-13 13:30:23 -08:00
|
|
|
float m_targetEsr = 0;
|
|
|
|
float m_targetTempC = 0;
|
|
|
|
|
2023-11-13 12:55:48 -08:00
|
|
|
// TODO: private:
|
|
|
|
public:
|
|
|
|
const uint8_t ch;
|
|
|
|
|
|
|
|
static const int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD;
|
|
|
|
static const int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD;
|
|
|
|
};
|
|
|
|
|
2023-06-23 14:38:17 -07:00
|
|
|
const IHeaterController& GetHeaterController(int ch);
|
|
|
|
|
2020-10-30 01:53:54 -07:00
|
|
|
void StartHeaterControl();
|
2022-08-29 17:19:30 -07:00
|
|
|
float GetHeaterDuty(int ch);
|
|
|
|
HeaterState GetHeaterState(int ch);
|
2022-05-11 01:41:07 -07:00
|
|
|
const char* describeHeaterState(HeaterState state);
|