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 <dron0gus@gmail.com>
Co-authored-by: rusefillc <sdfsdfqsf2334234234>
This commit is contained in:
rusefillc 2023-02-01 14:43:52 -05:00 committed by GitHub
parent f365492565
commit f4bd1a4197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 9 deletions

View File

@ -144,3 +144,8 @@ void SetConfiguration()
sizeof(Configuration)
);
}
SensorType GetSensorType()
{
return SensorType::LSU49;
}

View File

@ -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
}

View File

@ -73,3 +73,6 @@ uint8_t *GetConfiguratiuonPtr();
size_t GetConfigurationSize();
void SaveConfiguration();
const char *getTsSignature();
// LSU4.2, LSU4.9 or LSU_ADV
SensorType GetSensorType();

View File

@ -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();

View File

@ -39,6 +39,7 @@ static void SamplingThread(void*)
chRegSetThreadName("Sampling");
/* GD32: Insert 20us delay after ADC enable */
chThdSleepMilliseconds(1);

View File

@ -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