kill off helpers to get heater/sampler stuff

This commit is contained in:
Matthew Kennedy 2023-06-23 14:38:17 -07:00
parent a8ac2698c4
commit d1c878bf13
12 changed files with 73 additions and 70 deletions

View File

@ -137,6 +137,9 @@ void SendRusefiFormat(uint8_t ch)
{
auto baseAddress = WB_DATA_BASE_ADDR + 2 * (ch + configuration->CanIndexOffset);
const auto& sampler = GetSampler(ch);
const auto& heater = GetHeaterController(ch);
{
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);
@ -145,17 +148,17 @@ void SendRusefiFormat(uint8_t ch)
uint16_t lambda = GetLambda(ch) * 10000;
frame.get().Lambda = lambda;
frame.get().TemperatureC = GetSensorTemperature(ch);
frame.get().Valid = IsRunningClosedLoop(ch) ? 0x01 : 0x00;
frame.get().TemperatureC = sampler.GetSensorTemperature();
frame.get().Valid = heater.IsRunningClosedLoop() ? 0x01 : 0x00;
}
{
auto esr = GetSensorInternalResistance(ch);
auto esr = sampler.GetSensorInternalResistance();
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);
frame.get().Esr = esr;
frame.get().NernstDc = GetNernstDc(ch) * 1000;
frame.get().NernstDc = sampler.GetNernstDc() * 1000;
frame.get().PumpDuty = GetPumpOutputDuty(ch) * 255;
frame.get().Status = GetCurrentFault(ch);
frame.get().HeaterDuty = GetHeaterDuty(ch) * 255;

View File

@ -68,7 +68,7 @@ static constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_P
static constexpr int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD;
static const struct sensorHeaterParams *heater;
class HeaterController
class HeaterController : public IHeaterController
{
public:
HeaterController(int ch, int pwm_ch)
@ -76,14 +76,28 @@ public:
{
}
void Update(const ISampler& sampler, HeaterAllow heaterAllowState);
void Update(const ISampler& sampler, HeaterAllow heaterAllowState) override;
bool IsRunningClosedLoop() const override
{
return heaterState == HeaterState::ClosedLoop;
}
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:
public:
private:
Pid heaterPid =
{
0.3f, // kP
@ -101,6 +115,9 @@ public:
#ifdef HEATER_MAX_DUTY
int cycle;
#endif
// TODO: private:
public:
const uint8_t ch;
const uint8_t pwm_ch;
};
@ -114,6 +131,11 @@ HeaterController heaterControllers[AFR_CHANNELS] =
#endif
};
const IHeaterController& GetHeaterController(int ch)
{
return heaterControllers[ch];
}
HeaterState HeaterController::GetNextState(HeaterAllow heaterAllowState, float batteryVoltage, float sensorTemp)
{
bool heaterAllowed = heaterAllowState == HeaterAllow::Allowed;
@ -320,24 +342,14 @@ void StartHeaterControl()
chThdCreateStatic(waHeaterThread, sizeof(waHeaterThread), NORMALPRIO + 1, HeaterThread, nullptr);
}
bool IsRunningClosedLoop(int ch)
{
return heaterControllers[ch].heaterState == HeaterState::ClosedLoop;
}
float GetHeaterDuty(int ch)
{
return heaterPwm.GetLastDuty(heaterControllers[ch].pwm_ch);
}
float GetHeaterEffVoltage(int ch)
{
return heaterControllers[ch].heaterVoltage;
}
HeaterState GetHeaterState(int ch)
{
return heaterControllers[ch].heaterState;
return heaterControllers[ch].GetHeaterState();
}
const char* describeHeaterState(HeaterState state)

View File

@ -2,6 +2,8 @@
#include <cstdint>
#include "can.h"
enum class HeaterState
{
Preheat,
@ -11,9 +13,19 @@ enum class HeaterState
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;
};
const IHeaterController& GetHeaterController(int ch);
void StartHeaterControl();
bool IsRunningClosedLoop(int ch);
float GetHeaterDuty(int ch);
float GetHeaterEffVoltage(int ch);
HeaterState GetHeaterState(int ch);
const char* describeHeaterState(HeaterState state);

View File

@ -49,7 +49,7 @@ static void IndicationThread(void *ptr)
palToggleLine(data->line);
// Slow blink if closed loop, fast if not
chThdSleepMilliseconds(IsRunningClosedLoop(data->idx) ? LED_BLINK_SLOW : LED_BLINK_FAST);
chThdSleepMilliseconds(GetHeaterController(data->idx).IsRunningClosedLoop() ? LED_BLINK_SLOW : LED_BLINK_FAST);
}
else
{

View File

@ -65,7 +65,7 @@ static float GetPhi(float pumpCurrent) {
float GetLambda(int ch)
{
float pumpCurrent = GetPumpNominalCurrent(ch);
float pumpCurrent = GetSampler(ch).GetPumpNominalCurrent();
// Lambda is reciprocal of phi
return 1 / GetPhi(pumpCurrent);

View File

@ -20,15 +20,18 @@ void SamplingUpdateLiveData()
{
volatile struct livedata_afr_s *data = &livedata_afr[ch];
const auto& sampler = GetSampler(ch);
const auto& heater = GetHeaterController(ch);
data->lambda = GetLambda(ch);
data->temperature = GetSensorTemperature(ch) * 10;
data->nernstDc = GetNernstDc(ch) * 1000;
data->nernstAc = GetNernstAc(ch) * 1000;
data->temperature = sampler.GetSensorTemperature() * 10;
data->nernstDc = sampler.GetNernstDc() * 1000;
data->nernstAc = sampler.GetNernstAc() * 1000;
data->pumpCurrentTarget = GetPumpCurrent(ch);
data->pumpCurrentMeasured = GetPumpNominalCurrent(ch);
data->pumpCurrentMeasured = sampler.GetPumpNominalCurrent();
data->heaterDuty = GetHeaterDuty(ch) * 1000; // 0.1 %
data->heaterEffectiveVoltage = GetHeaterEffVoltage(ch) * 100;
data->esr = GetSensorInternalResistance(ch);
data->heaterEffectiveVoltage = heater.GetHeaterEffectiveVoltage() * 100;
data->esr = sampler.GetSensorInternalResistance();
data->fault = (uint8_t)GetCurrentFault(ch);
data->heaterState = (uint8_t)GetHeaterState(ch);
}

View File

@ -67,7 +67,7 @@ int main() {
palTogglePad(LED_GREEN_PORT, LED_GREEN_PIN);
// Slow blink if closed loop, fast if not
chThdSleepMilliseconds(IsRunningClosedLoop(0) ? 700 : 50);
chThdSleepMilliseconds(GetHeaterController(0).IsRunningClosedLoop() ? 700 : 50);
}
else
{

View File

@ -34,10 +34,13 @@ static void PumpThread(void*)
{
pump_control_state &s = state[ch];
const auto& sampler = GetSampler(ch);
const auto& heater = GetHeaterController(ch);
// Only actuate pump when running closed loop!
if (IsRunningClosedLoop(ch))
if (heater.IsRunningClosedLoop())
{
float nernstVoltage = GetNernstDc(ch);
float nernstVoltage = sampler.GetNernstDc();
float result = s.pumpPid.GetOutput(NERNST_TARGET, nernstVoltage);

View File

@ -91,7 +91,7 @@ void InitPumpDac()
void SetPumpCurrentTarget(int ch, int32_t microampere)
{
// Don't allow pump current when the sensor isn't hot
if (!IsRunningClosedLoop(ch))
if (!GetHeaterController(ch).IsRunningClosedLoop())
{
microampere = 0;
}

View File

@ -95,7 +95,7 @@ private:
static Sampler samplers[AFR_CHANNELS];
ISampler& GetSampler(int ch)
const ISampler& GetSampler(int ch)
{
return samplers[ch];
}
@ -174,32 +174,7 @@ void StartSampling()
chThdCreateStatic(waSamplingThread, sizeof(waSamplingThread), NORMALPRIO + 5, SamplingThread, nullptr);
}
float GetNernstAc(int ch)
{
return samplers[ch].GetNernstAc();
}
// TODO: remove these helpers
float GetSensorInternalResistance(int ch)
{
return samplers[ch].GetSensorInternalResistance();
}
float GetSensorTemperature(int ch)
{
return samplers[ch].GetSensorTemperature();
}
float GetNernstDc(int ch)
{
return samplers[ch].GetNernstDc();
}
float GetPumpNominalCurrent(int ch)
{
return samplers[ch].GetPumpNominalCurrent();
}
float GetInternalBatteryVoltage(int ch)
{
return samplers[ch].GetInternalBatteryVoltage();

View File

@ -11,13 +11,8 @@ struct ISampler
};
// Get the sampler for a particular channel
ISampler& GetSampler(int ch);
const ISampler& GetSampler(int ch);
void StartSampling();
float GetNernstAc(int ch);
float GetSensorInternalResistance(int ch);
float GetSensorTemperature(int ch);
float GetNernstDc(int ch);
float GetPumpNominalCurrent(int ch);
float GetInternalBatteryVoltage(int ch);

View File

@ -46,11 +46,11 @@ static void UartThread(void*)
"[AFR%d]: %d.%03d DC: %4d mV AC: %4d mV ESR: %5d T: %4d C Ipump: %6d uA Vheater: %5d heater: %s (%d)\tfault: %s\r\n",
ch,
lambdaIntPart, lambdaThousandths,
(int)(GetNernstDc(ch) * 1000.0),
(int)(GetNernstAc(ch) * 1000.0),
(int)GetSensorInternalResistance(ch),
(int)GetSensorTemperature(ch),
(int)(GetPumpNominalCurrent(ch) * 1000),
(int)(GetSampler(ch).GetNernstDc(ch) * 1000.0),
(int)(GetSampler(ch).GetNernstAc(ch) * 1000.0),
(int)GetSampler(ch).GetSensorInternalResistance(ch),
(int)GetSampler(ch).GetSensorTemperature(ch),
(int)(GetSampler(ch).GetPumpNominalCurrent(ch) * 1000),
batteryVoltageMv,
describeHeaterState(GetHeaterState(ch)), duty,
describeFault(GetCurrentFault(ch)));