refactoring - extracting sensors storage class

This commit is contained in:
rusefi 2017-03-07 02:24:57 -05:00
parent 8eb162cbab
commit a9fca33dea
11 changed files with 64 additions and 46 deletions

View File

@ -707,7 +707,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_
tsOutputChannels->pedalPosition = hasPedalPositionSensor(PASS_ENGINE_PARAMETER_F) ? getPedalPosition(PASS_ENGINE_PARAMETER_F) : 0; tsOutputChannels->pedalPosition = hasPedalPositionSensor(PASS_ENGINE_PARAMETER_F) ? getPedalPosition(PASS_ENGINE_PARAMETER_F) : 0;
tsOutputChannels->knockCount = engine->knockCount; tsOutputChannels->knockCount = engine->knockCount;
tsOutputChannels->knockLevel = engine->knockVolts; tsOutputChannels->knockLevel = engine->knockVolts;
tsOutputChannels->fuelTankGauge = engine->engineState.fuelTankGauge; tsOutputChannels->fuelTankGauge = engine->sensors.fuelTankGauge;
tsOutputChannels->hasFatalError = hasFirmwareError(); tsOutputChannels->hasFatalError = hasFirmwareError();
tsOutputChannels->totalTriggerErrorCounter = engine->triggerCentral.triggerState.totalTriggerErrorCounter; tsOutputChannels->totalTriggerErrorCounter = engine->triggerCentral.triggerState.totalTriggerErrorCounter;

View File

@ -79,10 +79,10 @@ static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAME
} }
float iatCorrection; float iatCorrection;
if (cisnan(engine->engineState.iat)) { if (cisnan(engine->sensors.iat)) {
iatCorrection = 0; iatCorrection = 0;
} else { } else {
iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->engineState.iat); iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->sensors.iat);
} }
if (engineConfiguration->debugMode == DBG_IGNITION_TIMING) { if (engineConfiguration->debugMode == DBG_IGNITION_TIMING) {
#if !EFI_UNIT_TEST || defined(__DOXYGEN__) #if !EFI_UNIT_TEST || defined(__DOXYGEN__)

View File

@ -69,13 +69,13 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) {
if (engineConfiguration->fuelLevelSensor != EFI_ADC_NONE) { if (engineConfiguration->fuelLevelSensor != EFI_ADC_NONE) {
float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor); float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor);
engineState.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0, sensors.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0,
boardConfiguration->fuelLevelFullTankVoltage, 100, boardConfiguration->fuelLevelFullTankVoltage, 100,
fuelLevelVoltage); fuelLevelVoltage);
} }
engineState.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_F) ? getVBatt(PASS_ENGINE_PARAMETER_F) : 12; sensors.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_F) ? getVBatt(PASS_ENGINE_PARAMETER_F) : 12;
engineState.injectorLag = getInjectorLag(engineState.vBatt PASS_ENGINE_PARAMETER); engineState.injectorLag = getInjectorLag(sensors.vBatt PASS_ENGINE_PARAMETER);
} }
void Engine::onTriggerEvent(efitick_t nowNt) { void Engine::onTriggerEvent(efitick_t nowNt) {
@ -92,6 +92,15 @@ Engine::Engine(persistent_config_s *config) {
reset(); reset();
} }
SensorsState::SensorsState() {
reset();
}
void SensorsState::reset() {
fuelTankGauge = vBatt = 0;
iat = clt = NAN;
}
void Engine::reset() { void Engine::reset() {
withError = isEngineChartEnabled = false; withError = isEngineChartEnabled = false;
sensorChartMode = SC_OFF; sensorChartMode = SC_OFF;
@ -110,7 +119,7 @@ void Engine::reset() {
isTestMode = false; isTestMode = false;
isSpinning = false; isSpinning = false;
adcToVoltageInputDividerCoefficient = NAN; adcToVoltageInputDividerCoefficient = NAN;
engineState.iat = engineState.clt = NAN; sensors.reset();
memset(&ignitionPin, 0, sizeof(ignitionPin)); memset(&ignitionPin, 0, sizeof(ignitionPin));
knockNow = false; knockNow = false;
@ -143,20 +152,20 @@ EngineState::EngineState() {
cltTimingCorrection = 0; cltTimingCorrection = 0;
runningFuel = baseFuel = currentVE = 0; runningFuel = baseFuel = currentVE = 0;
timeOfPreviousWarning = -10; timeOfPreviousWarning = -10;
baseTableFuel = iat = iatFuelCorrection = 0; baseTableFuel = iatFuelCorrection = 0;
fuelPidCorrection = 0; fuelPidCorrection = 0;
vBatt = clt = cltFuelCorrection = postCrankingFuelCorrection = 0; cltFuelCorrection = postCrankingFuelCorrection = 0;
warmupTargetAfr = airMass = 0; warmupTargetAfr = airMass = 0;
baroCorrection = timingAdvance = fuelTankGauge = 0; baroCorrection = timingAdvance = 0;
sparkDwell = mapAveragingDuration = 0; sparkDwell = mapAveragingDuration = 0;
totalLoggedBytes = injectionOffset = 0; totalLoggedBytes = injectionOffset = 0;
} }
void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) { void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) {
iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F); engine->sensors.iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F);
clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F); engine->sensors.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
warmupTargetAfr = interpolate2d(clt, engineConfiguration->warmupTargetAfrBins, warmupTargetAfr = interpolate2d(engine->sensors.clt, engineConfiguration->warmupTargetAfrBins,
engineConfiguration->warmupTargetAfr, WARMUP_TARGET_AFR_SIZE); engineConfiguration->warmupTargetAfr, WARMUP_TARGET_AFR_SIZE);
} }
@ -172,17 +181,17 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) {
sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER); sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER);
dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm); dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm);
currentAfr = getAfr(PASS_ENGINE_PARAMETER_F); engine->sensors.currentAfr = getAfr(PASS_ENGINE_PARAMETER_F);
// todo: move this into slow callback, no reason for IAT corr to be here // todo: move this into slow callback, no reason for IAT corr to be here
iatFuelCorrection = getIatFuelCorrection(iat PASS_ENGINE_PARAMETER); iatFuelCorrection = getIatFuelCorrection(engine->sensors.iat PASS_ENGINE_PARAMETER);
// todo: move this into slow callback, no reason for CLT corr to be here // todo: move this into slow callback, no reason for CLT corr to be here
if (boardConfiguration->useWarmupPidAfr && clt < engineConfiguration->warmupAfrThreshold) { if (boardConfiguration->useWarmupPidAfr && engine->sensors.clt < engineConfiguration->warmupAfrThreshold) {
if (rpm < 200) { if (rpm < 200) {
cltFuelCorrection = 1; cltFuelCorrection = 1;
warmupAfrPid.reset(); warmupAfrPid.reset();
} else { } else {
cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, currentAfr, 1); cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, engine->sensors.currentAfr, 1);
} }
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__) #if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) { if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) {
@ -207,8 +216,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) {
timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER); timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER);
if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) { if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) {
float coolantC = ENGINE(engineState.clt); float coolantC = ENGINE(sensors.clt);
float intakeC = ENGINE(engineState.iat); float intakeC = ENGINE(sensors.iat);
float tps = getTPS(PASS_ENGINE_PARAMETER_F); float tps = getTPS(PASS_ENGINE_PARAMETER_F);
tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC PASS_ENGINE_PARAMETER)); tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC PASS_ENGINE_PARAMETER));
float map = getMap(); float map = getMap();

View File

@ -67,12 +67,9 @@ private:
thermistor_conf_s currentConfig; thermistor_conf_s currentConfig;
}; };
class EngineState { class SensorsState {
public: public:
EngineState(); SensorsState();
void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F);
void updateSlowSensors(DECLARE_ENGINE_PARAMETER_F);
/** /**
* Performance optimization: * Performance optimization:
* log() function needed for thermistor logic is relatively heavy, to avoid it we have these * log() function needed for thermistor logic is relatively heavy, to avoid it we have these
@ -85,6 +82,21 @@ public:
float clt; float clt;
float vBatt; float vBatt;
float currentAfr;
/**
* that's fuel in tank - just a gauge
*/
percent_t fuelTankGauge;
void reset();
};
class EngineState {
public:
EngineState();
void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F);
void updateSlowSensors(DECLARE_ENGINE_PARAMETER_F);
efitick_t crankingTime; efitick_t crankingTime;
efitick_t timeSinceCranking; efitick_t timeSinceCranking;
@ -100,10 +112,6 @@ public:
float engineNoiseHipLevel; float engineNoiseHipLevel;
/**
* that's fuel in tank - just a gauge
*/
percent_t fuelTankGauge;
ThermistorMath iatCurve; ThermistorMath iatCurve;
ThermistorMath cltCurve; ThermistorMath cltCurve;
@ -150,8 +158,6 @@ public:
float currentVE; float currentVE;
float targetAFR; float targetAFR;
float currentAfr;
int vssCounter; int vssCounter;
int totalLoggedBytes; int totalLoggedBytes;
@ -358,6 +364,7 @@ public:
void onTriggerEvent(efitick_t nowNt); void onTriggerEvent(efitick_t nowNt);
EngineState engineState; EngineState engineState;
SensorsState sensors;
efitick_t lastTriggerEventTimeNt; efitick_t lastTriggerEventTimeNt;

View File

@ -179,15 +179,15 @@ void prepareFuelMap(DECLARE_ENGINE_PARAMETER_F) {
* @brief Engine warm-up fuel correction. * @brief Engine warm-up fuel correction.
*/ */
float getCltFuelCorrection(DECLARE_ENGINE_PARAMETER_F) { float getCltFuelCorrection(DECLARE_ENGINE_PARAMETER_F) {
if (cisnan(engine->engineState.clt)) if (cisnan(engine->sensors.clt))
return 1; // this error should be already reported somewhere else, let's just handle it return 1; // this error should be already reported somewhere else, let's just handle it
return interpolate2d(engine->engineState.clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT; return interpolate2d(engine->sensors.clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
} }
angle_t getCltTimingCorrection(DECLARE_ENGINE_PARAMETER_F) { angle_t getCltTimingCorrection(DECLARE_ENGINE_PARAMETER_F) {
if (cisnan(engine->engineState.clt)) if (cisnan(engine->sensors.clt))
return 0; // this error should be already reported somewhere else, let's just handle it return 0; // this error should be already reported somewhere else, let's just handle it
return interpolate2d(engine->engineState.clt, engineConfiguration->cltTimingBins, engineConfiguration->cltTimingExtra, CLT_TIMING_CURVE_SIZE); return interpolate2d(engine->sensors.clt, engineConfiguration->cltTimingBins, engineConfiguration->cltTimingExtra, CLT_TIMING_CURVE_SIZE);
} }
float getIatFuelCorrection(float iat DECLARE_ENGINE_PARAMETER_S) { float getIatFuelCorrection(float iat DECLARE_ENGINE_PARAMETER_S) {

View File

@ -108,7 +108,7 @@ static void manualIdleController(int positionPercent) {
positionPercent += engineConfiguration->crankingIdleAdjustment; positionPercent += engineConfiguration->crankingIdleAdjustment;
} }
percent_t cltCorrectedPosition = interpolate2d(engine->engineState.clt, config->cltIdleCorrBins, config->cltIdleCorr, percent_t cltCorrectedPosition = interpolate2d(engine->sensors.clt, config->cltIdleCorrBins, config->cltIdleCorr,
CLT_CURVE_SIZE) / PERCENT_MULT * positionPercent; CLT_CURVE_SIZE) / PERCENT_MULT * positionPercent;
// let's put the value into the right range // let's put the value into the right range

View File

@ -25,6 +25,7 @@
#include "engine_controller.h" #include "engine_controller.h"
#include "mmc_card.h" #include "mmc_card.h"
#include "idle_thread.h" #include "idle_thread.h"
#include "fuel_math.h"
#if EFI_HD44780_LCD || defined(__DOXYGEN__) #if EFI_HD44780_LCD || defined(__DOXYGEN__)
@ -254,7 +255,7 @@ static void showLine(lcd_line_e line, int screenY) {
lcdPrintf("Throttle %s %f%%", buffer, getTPS()); lcdPrintf("Throttle %s %f%%", buffer, getTPS());
return; return;
case LL_FUEL_CLT_CORRECTION: case LL_FUEL_CLT_CORRECTION:
//lcdPrintf("CLT corr %fv", getVBatt(PASS_ENGINE_PARAMETER_F)); lcdPrintf("CLT corr %fv", getCltFuelCorrection(PASS_ENGINE_PARAMETER_F));
return; return;
case LL_VBATT: case LL_VBATT:

View File

@ -374,16 +374,16 @@ static void scheduleOutput2(OutputSignalPair *pair, efitimeus_t nowUs, float del
static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_F) { static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_F) {
#if ! EFI_UNIT_TEST #if ! EFI_UNIT_TEST
if (ENGINE(rpmCalculator.rpmValue) < CONFIG(fuelClosedLoopRpmThreshold) || if (ENGINE(rpmCalculator.rpmValue) < CONFIG(fuelClosedLoopRpmThreshold) ||
ENGINE(engineState.clt) < CONFIG(fuelClosedLoopCltThreshold) || ENGINE(sensors.clt) < CONFIG(fuelClosedLoopCltThreshold) ||
getTPS(PASS_ENGINE_PARAMETER_F) > CONFIG(fuelClosedLoopTpsThreshold) || getTPS(PASS_ENGINE_PARAMETER_F) > CONFIG(fuelClosedLoopTpsThreshold) ||
ENGINE(engineState.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold || ENGINE(sensors.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold ||
ENGINE(engineState.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) { ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) {
engine->engineState.fuelPidCorrection = 0; engine->engineState.fuelPidCorrection = 0;
fuelPid.reset(); fuelPid.reset();
return; return;
} }
engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(engineState.currentAfr), 1); engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), 1);
if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) { if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) {
tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection; tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection;
fuelPid.postState(&tsOutputChannels); fuelPid.postState(&tsOutputChannels);

View File

@ -118,7 +118,7 @@ static void canDashboardBMW(void) {
sendMessage(); sendMessage();
commonTxInit(CAN_BMW_E46_DME2); commonTxInit(CAN_BMW_E46_DME2);
setShortValue(&txmsg, (int) ((engine->engineState.clt + 48.373) / 0.75), 1); setShortValue(&txmsg, (int) ((engine->sensors.clt + 48.373) / 0.75), 1);
sendMessage(); sendMessage();
} }
@ -163,7 +163,7 @@ static void canMazdaRX8(void) {
static void canDashboardFiat(void) { static void canDashboardFiat(void) {
//Fiat Dashboard //Fiat Dashboard
commonTxInit(CAN_FIAT_MOTOR_INFO); commonTxInit(CAN_FIAT_MOTOR_INFO);
setShortValue(&txmsg, (int) (engine->engineState.clt - 40), 3); //Coolant Temp setShortValue(&txmsg, (int) (engine->sensors.clt - 40), 3); //Coolant Temp
setShortValue(&txmsg, getRpmE(engine) / 32, 6); //RPM setShortValue(&txmsg, getRpmE(engine) / 32, 6); //RPM
sendMessage(); sendMessage();
} }
@ -175,7 +175,7 @@ static void canDashboardVAG(void) {
sendMessage(); sendMessage();
commonTxInit(CAN_VAG_CLT); commonTxInit(CAN_VAG_CLT);
setShortValue(&txmsg, (int) ((engine->engineState.clt + 48.373) / 0.75), 1); //Coolant Temp setShortValue(&txmsg, (int) ((engine->sensors.clt + 48.373) / 0.75), 1); //Coolant Temp
sendMessage(); sendMessage();
} }

View File

@ -57,6 +57,7 @@ void testFuelMap(void) {
printf("*** getInjectorLag\r\n"); printf("*** getInjectorLag\r\n");
// engine->engineState.vb
assertEqualsM("lag", 1.04, getInjectorLag(12 PASS_ENGINE_PARAMETER)); assertEqualsM("lag", 1.04, getInjectorLag(12 PASS_ENGINE_PARAMETER));
for (int i = 0; i < VBAT_INJECTOR_CURVE_SIZE; i++) { for (int i = 0; i < VBAT_INJECTOR_CURVE_SIZE; i++) {
@ -90,7 +91,7 @@ void testFuelMap(void) {
assertEquals(NAN, getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F)); assertEquals(NAN, getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F));
float iatCorrection = getIatFuelCorrection(-KELV PASS_ENGINE_PARAMETER); float iatCorrection = getIatFuelCorrection(-KELV PASS_ENGINE_PARAMETER);
assertEqualsM("IAT", 2, iatCorrection); assertEqualsM("IAT", 2, iatCorrection);
engine->engineState.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F); engine->sensors.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
float cltCorrection = getCltFuelCorrection(PASS_ENGINE_PARAMETER_F); float cltCorrection = getCltFuelCorrection(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 1, cltCorrection); assertEqualsM("CLT", 1, cltCorrection);
float injectorLag = getInjectorLag(getVBatt(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER); float injectorLag = getInjectorLag(getVBatt(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER);

View File

@ -629,7 +629,7 @@ static void setTestBug299small(EngineTestHelper *eth) {
// this is needed to update injectorLag // this is needed to update injectorLag
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F); engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 70, engine->engineState.clt); assertEqualsM("CLT", 70, engine->sensors.clt);
engineConfiguration->trigger.type = TT_ONE; engineConfiguration->trigger.type = TT_ONE;
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F); incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F);
@ -1131,7 +1131,7 @@ void testSparkReverseOrderBug319(void) {
// this is needed to update injectorLag // this is needed to update injectorLag
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F); engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 70, engine->engineState.clt); assertEqualsM("CLT", 70, engine->sensors.clt);
engineConfiguration->trigger.type = TT_ONE; engineConfiguration->trigger.type = TT_ONE;
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F); incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F);