From f4bd1a4197b878bc8ae44df7b6ebdea8b0628728 Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Wed, 1 Feb 2023 14:43:52 -0500 Subject: [PATCH] Smaller version of heater settings for other types of sensors (#183) * heater settings for other types of sensors * port: GetSensorType * fancier C++ version * fancier C++ version * Why not a function --------- Co-authored-by: Andrey Gusakov Co-authored-by: rusefillc --- firmware/boards/f0_module/port.cpp | 5 +++ firmware/boards/f1_common/f1_port.cpp | 13 +++++++ firmware/boards/port.h | 3 ++ firmware/heater_control.cpp | 49 ++++++++++++++++++++++++--- firmware/sampling.cpp | 1 + firmware/wideband_config.h | 16 ++++++--- 6 files changed, 78 insertions(+), 9 deletions(-) diff --git a/firmware/boards/f0_module/port.cpp b/firmware/boards/f0_module/port.cpp index 5834640..a0a06eb 100644 --- a/firmware/boards/f0_module/port.cpp +++ b/firmware/boards/f0_module/port.cpp @@ -144,3 +144,8 @@ void SetConfiguration() sizeof(Configuration) ); } + +SensorType GetSensorType() +{ + return SensorType::LSU49; +} diff --git a/firmware/boards/f1_common/f1_port.cpp b/firmware/boards/f1_common/f1_port.cpp index 0ed23c6..c1004c8 100644 --- a/firmware/boards/f1_common/f1_port.cpp +++ b/firmware/boards/f1_common/f1_port.cpp @@ -81,3 +81,16 @@ size_t GetConfigurationSize() const char *getTsSignature() { return TS_SIGNATURE; } + +SensorType GetSensorType() +{ + /* TODO: load from settings */ +#if defined(BOARD_SENSOR_LSU42) + return SensorType::LSU42; +#elif defined(BOARD_SENSOR_LSUADV) + return SensorType::LSUADV; +#else + /* default is LSU4.9 */ + return SensorType::LSU49; +#endif +} diff --git a/firmware/boards/port.h b/firmware/boards/port.h index 3c1fdf8..f718de8 100644 --- a/firmware/boards/port.h +++ b/firmware/boards/port.h @@ -73,3 +73,6 @@ uint8_t *GetConfiguratiuonPtr(); size_t GetConfigurationSize(); void SaveConfiguration(); const char *getTsSignature(); + +// LSU4.2, LSU4.9 or LSU_ADV +SensorType GetSensorType(); diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index b2dd3b0..321d83c 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -4,12 +4,47 @@ #include "ch.h" #include "hal.h" +#include "port.h" #include "fault.h" #include "pwm.h" #include "sampling.h" #include "pid.h" #include "can.h" +struct sensorHeaterParams { + uint16_t closedLoopThresholdESR; + uint16_t targetESR; + uint16_t overheatESR; + uint16_t underheatESR; +}; + +static const struct sensorHeaterParams heaterParams49 = { + .closedLoopThresholdESR = LSU49_HEATER_CLOSED_LOOP_THRESHOLD_ESR, + .targetESR = LSU49_HEATER_TARGET_ESR, + .overheatESR = LSU49_HEATER_OVERHEAT_ESR, + .underheatESR = LSU49_HEATER_UNDERHEAT_ESR, + }; +static const struct sensorHeaterParams heaterParams42 = { + .closedLoopThresholdESR = LSU42_HEATER_CLOSED_LOOP_THRESHOLD_ESR, + .targetESR = LSU42_HEATER_TARGET_ESR, + .overheatESR = LSU42_HEATER_OVERHEAT_ESR, + .underheatESR = LSU42_HEATER_UNDERHEAT_ESR, + }; +static const struct sensorHeaterParams heaterParamsAdv = { + //TODO + }; + +static const sensorHeaterParams *getHeaterParams(SensorType type) { + switch (type) { + case SensorType::LSU49: + return &heaterParams49; + case SensorType::LSU42: + return &heaterParams42; + case SensorType::LSUADV: + return &heaterParamsAdv; + } +} + using namespace wbo; // 400khz / 1024 = 390hz PWM @@ -33,6 +68,7 @@ static const PWMConfig heaterPwmConfig = { static constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; static constexpr int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD; +static const struct sensorHeaterParams *heater; struct heater_state { Pid heaterPid; @@ -113,7 +149,7 @@ static HeaterState GetNextState(struct heater_state &s, HeaterAllow heaterAllowS s.timeCounter--; // If preheat timeout, or sensor is already hot (engine running?) - if (s.timeCounter <= 0 || sensorEsr < HEATER_CLOSED_LOOP_THRESHOLD_ESR) + if (s.timeCounter <= 0 || sensorEsr < heater->closedLoopThresholdESR) { // If enough time has elapsed, start the ramp // Start the ramp at 4 volts @@ -128,7 +164,7 @@ static HeaterState GetNextState(struct heater_state &s, HeaterAllow heaterAllowS // Stay in preheat - wait for time to elapse break; case HeaterState::WarmupRamp: - if (sensorEsr < HEATER_CLOSED_LOOP_THRESHOLD_ESR) + if (sensorEsr < heater->closedLoopThresholdESR) { return HeaterState::ClosedLoop; } @@ -143,12 +179,12 @@ static HeaterState GetNextState(struct heater_state &s, HeaterAllow heaterAllowS break; case HeaterState::ClosedLoop: // Check that the sensor's ESR is acceptable for normal operation - if (sensorEsr < HEATER_OVERHEAT_ESR) + if (sensorEsr < heater->overheatESR) { SetFault(s.ch, Fault::SensorOverheat); return HeaterState::Stopped; } - else if (sensorEsr > HEATER_UNDERHEAT_ESR) + else if (sensorEsr > heater->underheatESR) { SetFault(s.ch, Fault::SensorUnderheat); return HeaterState::Stopped; @@ -181,7 +217,7 @@ static float GetVoltageForState(struct heater_state &s, float heaterEsr) case HeaterState::ClosedLoop: // "nominal" heater voltage is 7.5v, so apply correction around that point (instead of relying on integrator so much) // Negated because lower resistance -> hotter - return 7.5f - s.heaterPid.GetOutput(HEATER_TARGET_ESR, heaterEsr); + return 7.5f - s.heaterPid.GetOutput(heater->targetESR, heaterEsr); case HeaterState::Stopped: // Something has gone wrong, turn off the heater. return 0; @@ -202,6 +238,9 @@ static void HeaterThread(void*) // immediately think we overshot the target temperature chThdSleepMilliseconds(1000); + // Get sensor type and settings + heater = getHeaterParams(GetSensorType()); + while (true) { auto heaterAllowState = GetHeaterAllowed(); diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index 994147b..93f6372 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -39,6 +39,7 @@ static void SamplingThread(void*) chRegSetThreadName("Sampling"); + /* GD32: Insert 20us delay after ADC enable */ chThdSleepMilliseconds(1); diff --git a/firmware/wideband_config.h b/firmware/wideband_config.h index a0ceb41..41b3c99 100644 --- a/firmware/wideband_config.h +++ b/firmware/wideband_config.h @@ -52,10 +52,18 @@ // mininal battery voltage to continue heating #define HEATER_BATTETY_OFF_VOLTAGE 8.5 -#define HEATER_CLOSED_LOOP_THRESHOLD_ESR 500 -#define HEATER_TARGET_ESR 300 -#define HEATER_OVERHEAT_ESR 150 -#define HEATER_UNDERHEAT_ESR 700 +//LSU 4.9 +#define LSU49_HEATER_CLOSED_LOOP_THRESHOLD_ESR 500 +#define LSU49_HEATER_TARGET_ESR 300 +#define LSU49_HEATER_OVERHEAT_ESR 150 +#define LSU49_HEATER_UNDERHEAT_ESR 700 + +//LSU 4.2 +// TODO: check this!!! +#define LSU42_HEATER_CLOSED_LOOP_THRESHOLD_ESR 150 +#define LSU42_HEATER_TARGET_ESR 90 +#define LSU42_HEATER_OVERHEAT_ESR 45 +#define LSU42_HEATER_UNDERHEAT_ESR 250 // ******************************* // TunerStudio configuration