From a9fca33dea0dda0bb089feef74f9d558e65f50d0 Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 7 Mar 2017 02:24:57 -0500 Subject: [PATCH] refactoring - extracting sensors storage class --- firmware/console/status_loop.cpp | 2 +- firmware/controllers/algo/advance_map.cpp | 4 +- firmware/controllers/algo/engine.cpp | 41 +++++++++++-------- firmware/controllers/algo/engine.h | 29 ++++++++----- firmware/controllers/algo/fuel_math.cpp | 8 ++-- firmware/controllers/idle_thread.cpp | 2 +- firmware/controllers/lcd_controller.cpp | 3 +- .../trigger/main_trigger_callback.cpp | 8 ++-- firmware/hw_layer/can_hw.cpp | 6 +-- unit_tests/test_fuel_map.cpp | 3 +- unit_tests/test_trigger_decoder.cpp | 4 +- 11 files changed, 64 insertions(+), 46 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index ceea8a6203..e83a657e5d 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -707,7 +707,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->pedalPosition = hasPedalPositionSensor(PASS_ENGINE_PARAMETER_F) ? getPedalPosition(PASS_ENGINE_PARAMETER_F) : 0; tsOutputChannels->knockCount = engine->knockCount; tsOutputChannels->knockLevel = engine->knockVolts; - tsOutputChannels->fuelTankGauge = engine->engineState.fuelTankGauge; + tsOutputChannels->fuelTankGauge = engine->sensors.fuelTankGauge; tsOutputChannels->hasFatalError = hasFirmwareError(); tsOutputChannels->totalTriggerErrorCounter = engine->triggerCentral.triggerState.totalTriggerErrorCounter; diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index 9dbc82dcc4..bba68364b7 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -79,10 +79,10 @@ static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAME } float iatCorrection; - if (cisnan(engine->engineState.iat)) { + if (cisnan(engine->sensors.iat)) { iatCorrection = 0; } else { - iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->engineState.iat); + iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->sensors.iat); } if (engineConfiguration->debugMode == DBG_IGNITION_TIMING) { #if !EFI_UNIT_TEST || defined(__DOXYGEN__) diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index e3bcd1925a..3a9d9e2d09 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -69,13 +69,13 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) { if (engineConfiguration->fuelLevelSensor != EFI_ADC_NONE) { float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor); - engineState.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0, + sensors.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0, boardConfiguration->fuelLevelFullTankVoltage, 100, 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) { @@ -92,6 +92,15 @@ Engine::Engine(persistent_config_s *config) { reset(); } +SensorsState::SensorsState() { + reset(); +} + +void SensorsState::reset() { + fuelTankGauge = vBatt = 0; + iat = clt = NAN; +} + void Engine::reset() { withError = isEngineChartEnabled = false; sensorChartMode = SC_OFF; @@ -110,7 +119,7 @@ void Engine::reset() { isTestMode = false; isSpinning = false; adcToVoltageInputDividerCoefficient = NAN; - engineState.iat = engineState.clt = NAN; + sensors.reset(); memset(&ignitionPin, 0, sizeof(ignitionPin)); knockNow = false; @@ -143,20 +152,20 @@ EngineState::EngineState() { cltTimingCorrection = 0; runningFuel = baseFuel = currentVE = 0; timeOfPreviousWarning = -10; - baseTableFuel = iat = iatFuelCorrection = 0; + baseTableFuel = iatFuelCorrection = 0; fuelPidCorrection = 0; - vBatt = clt = cltFuelCorrection = postCrankingFuelCorrection = 0; + cltFuelCorrection = postCrankingFuelCorrection = 0; warmupTargetAfr = airMass = 0; - baroCorrection = timingAdvance = fuelTankGauge = 0; + baroCorrection = timingAdvance = 0; sparkDwell = mapAveragingDuration = 0; totalLoggedBytes = injectionOffset = 0; } void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) { - iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F); - clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F); + engine->sensors.iat = getIntakeAirTemperature(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); } @@ -172,17 +181,17 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER); 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 - 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 - if (boardConfiguration->useWarmupPidAfr && clt < engineConfiguration->warmupAfrThreshold) { + if (boardConfiguration->useWarmupPidAfr && engine->sensors.clt < engineConfiguration->warmupAfrThreshold) { if (rpm < 200) { cltFuelCorrection = 1; warmupAfrPid.reset(); } else { - cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, currentAfr, 1); + cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, engine->sensors.currentAfr, 1); } #if ! EFI_UNIT_TEST || defined(__DOXYGEN__) if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) { @@ -207,8 +216,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER); if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) { - float coolantC = ENGINE(engineState.clt); - float intakeC = ENGINE(engineState.iat); + float coolantC = ENGINE(sensors.clt); + float intakeC = ENGINE(sensors.iat); float tps = getTPS(PASS_ENGINE_PARAMETER_F); tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC PASS_ENGINE_PARAMETER)); float map = getMap(); diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 4a50a2a5bc..ed42b84b96 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -67,12 +67,9 @@ private: thermistor_conf_s currentConfig; }; -class EngineState { +class SensorsState { public: - EngineState(); - void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F); - void updateSlowSensors(DECLARE_ENGINE_PARAMETER_F); - + SensorsState(); /** * Performance optimization: * log() function needed for thermistor logic is relatively heavy, to avoid it we have these @@ -85,6 +82,21 @@ public: float clt; 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 timeSinceCranking; @@ -100,10 +112,6 @@ public: float engineNoiseHipLevel; - /** - * that's fuel in tank - just a gauge - */ - percent_t fuelTankGauge; ThermistorMath iatCurve; ThermistorMath cltCurve; @@ -150,8 +158,6 @@ public: float currentVE; float targetAFR; - float currentAfr; - int vssCounter; int totalLoggedBytes; @@ -358,6 +364,7 @@ public: void onTriggerEvent(efitick_t nowNt); EngineState engineState; + SensorsState sensors; efitick_t lastTriggerEventTimeNt; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 240250f565..78b24fc1bb 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -179,15 +179,15 @@ void prepareFuelMap(DECLARE_ENGINE_PARAMETER_F) { * @brief Engine warm-up fuel correction. */ 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 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) { - 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 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) { diff --git a/firmware/controllers/idle_thread.cpp b/firmware/controllers/idle_thread.cpp index da92e7a4d2..caa962601c 100644 --- a/firmware/controllers/idle_thread.cpp +++ b/firmware/controllers/idle_thread.cpp @@ -108,7 +108,7 @@ static void manualIdleController(int positionPercent) { 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; // let's put the value into the right range diff --git a/firmware/controllers/lcd_controller.cpp b/firmware/controllers/lcd_controller.cpp index e407ab567d..7007d2f367 100644 --- a/firmware/controllers/lcd_controller.cpp +++ b/firmware/controllers/lcd_controller.cpp @@ -25,6 +25,7 @@ #include "engine_controller.h" #include "mmc_card.h" #include "idle_thread.h" +#include "fuel_math.h" #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()); return; case LL_FUEL_CLT_CORRECTION: - //lcdPrintf("CLT corr %fv", getVBatt(PASS_ENGINE_PARAMETER_F)); + lcdPrintf("CLT corr %fv", getCltFuelCorrection(PASS_ENGINE_PARAMETER_F)); return; case LL_VBATT: diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index 27fbff0764..caf16b90a0 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -374,16 +374,16 @@ static void scheduleOutput2(OutputSignalPair *pair, efitimeus_t nowUs, float del static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_F) { #if ! EFI_UNIT_TEST if (ENGINE(rpmCalculator.rpmValue) < CONFIG(fuelClosedLoopRpmThreshold) || - ENGINE(engineState.clt) < CONFIG(fuelClosedLoopCltThreshold) || + ENGINE(sensors.clt) < CONFIG(fuelClosedLoopCltThreshold) || getTPS(PASS_ENGINE_PARAMETER_F) > CONFIG(fuelClosedLoopTpsThreshold) || - ENGINE(engineState.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold || - ENGINE(engineState.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) { + ENGINE(sensors.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold || + ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) { engine->engineState.fuelPidCorrection = 0; fuelPid.reset(); 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) { tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection; fuelPid.postState(&tsOutputChannels); diff --git a/firmware/hw_layer/can_hw.cpp b/firmware/hw_layer/can_hw.cpp index 7dc164a37c..fa8c419ca6 100644 --- a/firmware/hw_layer/can_hw.cpp +++ b/firmware/hw_layer/can_hw.cpp @@ -118,7 +118,7 @@ static void canDashboardBMW(void) { sendMessage(); 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(); } @@ -163,7 +163,7 @@ static void canMazdaRX8(void) { static void canDashboardFiat(void) { //Fiat Dashboard 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 sendMessage(); } @@ -175,7 +175,7 @@ static void canDashboardVAG(void) { sendMessage(); 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(); } diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 1a95b4f66f..f3f67ce75b 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -57,6 +57,7 @@ void testFuelMap(void) { printf("*** getInjectorLag\r\n"); +// engine->engineState.vb assertEqualsM("lag", 1.04, getInjectorLag(12 PASS_ENGINE_PARAMETER)); for (int i = 0; i < VBAT_INJECTOR_CURVE_SIZE; i++) { @@ -90,7 +91,7 @@ void testFuelMap(void) { assertEquals(NAN, getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F)); float iatCorrection = getIatFuelCorrection(-KELV PASS_ENGINE_PARAMETER); 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); assertEqualsM("CLT", 1, cltCorrection); float injectorLag = getInjectorLag(getVBatt(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER); diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index a511fedc30..33134f591d 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -629,7 +629,7 @@ static void setTestBug299small(EngineTestHelper *eth) { // this is needed to update injectorLag engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F); - assertEqualsM("CLT", 70, engine->engineState.clt); + assertEqualsM("CLT", 70, engine->sensors.clt); engineConfiguration->trigger.type = TT_ONE; incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F); @@ -1131,7 +1131,7 @@ void testSparkReverseOrderBug319(void) { // this is needed to update injectorLag engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F); - assertEqualsM("CLT", 70, engine->engineState.clt); + assertEqualsM("CLT", 70, engine->sensors.clt); engineConfiguration->trigger.type = TT_ONE; incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F);