wideband/firmware/heater_control.h

94 lines
2.4 KiB
C
Raw Normal View History

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"
#include "can.h"
2023-11-13 12:55:48 -08:00
#include "pid.h"
#include "timer.h"
enum class HeaterState
{
Preheat,
WarmupRamp,
ClosedLoop,
Stopped,
2022-12-01 13:20:54 -08:00
NoHeaterSupply,
};
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;
virtual float GetTargetTemp() const = 0;
};
2023-11-13 13:30:23 -08:00
class HeaterControllerBase : public IHeaterController
2023-11-13 12:55:48 -08:00
{
public:
HeaterControllerBase(int ch, int preheatTimeSec, int warmupTimeSec);
2023-11-13 13:30:23 -08:00
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;
float GetTargetTemp() const override;
2023-11-13 12:55:48 -08:00
2023-11-13 13:30:23 -08:00
virtual void SetDuty(float duty) const = 0;
bool GetIsHeatingEnabled(HeaterAllow heaterAllowState, float batteryVoltage);
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
private:
Pid heaterPid =
{
0.3f, // kP
0.3f, // kI
0.01f, // kD
3.0f, // Integrator clamp (volts)
HEATER_CONTROL_PERIOD
};
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
const uint8_t ch;
const int m_preheatTimeSec;
const int m_warmupTimeSec;
Timer m_batteryStableTimer;
Timer m_preheatTimer;
Timer m_warmupTimer;
// Stores the time since a non-over/underheat condition
// If the timer reaches a threshold, an over/underheat has
// occured
Timer m_underheatTimer;
Timer m_overheatTimer;
2023-11-13 12:55:48 -08:00
static const int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD;
};
const IHeaterController& GetHeaterController(int ch);
2020-10-30 01:53:54 -07:00
void StartHeaterControl();
float GetHeaterDuty(int ch);
HeaterState GetHeaterState(int ch);
const char* describeHeaterState(HeaterState state);