From f3045af367c91f6287773e8cc17c6f7f54167ed9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 1 Aug 2024 21:45:54 -0700 Subject: [PATCH] AE is engine module --- firmware/console/binary/live_data.cpp | 2 +- firmware/console/status_loop.cpp | 3 -- .../controllers/algo/accel_enrichment.cpp | 27 ++-------- firmware/controllers/algo/accel_enrichment.h | 6 +-- firmware/controllers/algo/engine.cpp | 2 +- firmware/controllers/algo/engine.h | 5 +- firmware/controllers/algo/engine_types.h | 2 +- firmware/controllers/algo/fuel_math.cpp | 2 +- firmware/controllers/engine_controller.cpp | 6 +-- .../controllers/trigger/trigger_central.cpp | 2 +- unit_tests/tests/test_accel_enrichment.cpp | 50 +++++++++---------- 11 files changed, 38 insertions(+), 69 deletions(-) diff --git a/firmware/console/binary/live_data.cpp b/firmware/console/binary/live_data.cpp index e4f04b923c..d6d17746bc 100644 --- a/firmware/console/binary/live_data.cpp +++ b/firmware/console/binary/live_data.cpp @@ -97,7 +97,7 @@ const engine_state_s* getLiveData(size_t) { template<> const tps_accel_state_s* getLiveData(size_t) { - return &engine->tpsAccelEnrichment; + return &engine->module().unmock(); } template<> diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 2343709751..3fada49671 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -608,9 +608,6 @@ void updateTunerStudioState() { #endif switch (engineConfiguration->debugMode) { - case DBG_TPS_ACCEL: - tsOutputChannels->debugIntField1 = engine->tpsAccelEnrichment.cb.getSize(); - break; case DBG_SR5_PROTOCOL: { const int _10_6 = 100000; tsOutputChannels->debugIntField1 = tsState.textCommandCounter * _10_6 + tsState.totalCounter; diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index 97e650b476..8d68534e17 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -70,19 +70,6 @@ floatms_t TpsAccelEnrichment::getTpsEnrichment() { resetFractionValues(); } -#if EFI_TUNER_STUDIO - if (engineConfiguration->debugMode == DBG_TPS_ACCEL) { - engine->outputChannels.debugFloatField1 = tpsFrom; - engine->outputChannels.debugFloatField2 = tpsTo; - engine->outputChannels.debugFloatField3 = valueFromTable; - engine->outputChannels.debugFloatField4 = extraFuel; - engine->outputChannels.debugFloatField5 = accumulatedValue; - engine->outputChannels.debugFloatField6 = maxExtraPerPeriod; - engine->outputChannels.debugFloatField7 = maxInjectedPerPeriod; - engine->outputChannels.debugIntField1 = cycleCnt; - } -#endif /* EFI_TUNER_STUDIO */ - float mult = interpolate2d(rpm, config->tpsTspCorrValuesBins, config->tpsTspCorrValues); if (mult != 0 && (mult < 0.01 || mult > 100)) { @@ -193,9 +180,7 @@ TpsAccelEnrichment::TpsAccelEnrichment() { cb.setSize(4); } -#if ! EFI_UNIT_TEST - -void updateAccelParameters() { +void TpsAccelEnrichment::onConfigurationChange(engine_configuration_s const* /*previousConfig*/) { constexpr float slowCallbackPeriodSecond = SLOW_CALLBACK_PERIOD_MS / 1000.0f; int length = engineConfiguration->tpsAccelLookback / slowCallbackPeriodSecond; @@ -203,18 +188,12 @@ void updateAccelParameters() { length = 1; } - engine->tpsAccelEnrichment.setLength(length); + setLength(length); } -#endif /* ! EFI_UNIT_TEST */ - - void initAccelEnrichment() { tpsTpsMap.init(config->tpsTpsAccelTable, config->tpsTpsAccelFromRpmBins, config->tpsTpsAccelToRpmBins); -#if ! EFI_UNIT_TEST - - updateAccelParameters(); -#endif /* ! EFI_UNIT_TEST */ + engine->module()->onConfigurationChange(nullptr); } diff --git a/firmware/controllers/algo/accel_enrichment.h b/firmware/controllers/algo/accel_enrichment.h index 1f653a6520..083d431367 100644 --- a/firmware/controllers/algo/accel_enrichment.h +++ b/firmware/controllers/algo/accel_enrichment.h @@ -16,10 +16,12 @@ typedef Map3D tps_tps_Map3D_t; -class TpsAccelEnrichment : public tps_accel_state_s { +class TpsAccelEnrichment : public tps_accel_state_s, public EngineModule { public: TpsAccelEnrichment(); + void onConfigurationChange(engine_configuration_s const* previousConfig) override; + int getMaxDeltaIndex(); float getMaxDelta(); @@ -38,5 +40,3 @@ public: }; void initAccelEnrichment(); - -void updateAccelParameters(); diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 0583d732b0..7d51b4c50d 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -137,7 +137,7 @@ void Engine::periodicSlowCallback() { updateSlowSensors(); checkShutdown(); - tpsAccelEnrichment.onNewValue(Sensor::getOrZero(SensorType::Tps1)); + module()->onNewValue(Sensor::getOrZero(SensorType::Tps1)); updateVrPwm(); diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 6735d3df4b..41cc128dcf 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -174,6 +174,7 @@ public: BoostController, #endif // EFI_BOOST_CONTROL LedBlinkingTask, + TpsAccelEnrichment, #include "modules_list_generated.h" @@ -220,8 +221,6 @@ public: void setConfig(); - LocalVersionHolder versionForConfigurationListeners; - AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]; #if EFI_UNIT_TEST @@ -272,8 +271,6 @@ public: */ int globalConfigurationVersion = 0; - TpsAccelEnrichment tpsAccelEnrichment; - #if EFI_SHAFT_POSITION_INPUT TriggerCentral triggerCentral; #endif // EFI_SHAFT_POSITION_INPUT diff --git a/firmware/controllers/algo/engine_types.h b/firmware/controllers/algo/engine_types.h index b3e7c44322..4b0b04a885 100644 --- a/firmware/controllers/algo/engine_types.h +++ b/firmware/controllers/algo/engine_types.h @@ -227,7 +227,7 @@ enum class engine_type_e : uint32_t { */ typedef enum __attribute__ ((__packed__)) { DBG_0 = 0, - DBG_TPS_ACCEL = 1, + DBG_1 = 1, DBG_2 = 2, DBG_STEPPER_IDLE_CONTROL = 3, DBG_EL_ACCEL = 4, diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index f44653002b..968985f90a 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -324,7 +324,7 @@ float getInjectionMass(int rpm) { engine->module()->prepare(); } - floatms_t tpsAccelEnrich = engine->tpsAccelEnrichment.getTpsEnrichment(); + floatms_t tpsAccelEnrich = engine->module()->getTpsEnrichment(); efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(tpsAccelEnrich), "NaN tpsAccelEnrich", 0); engine->engineState.tpsAccelEnrich = tpsAccelEnrich; diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 95f8ae4423..fc39f01778 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -131,7 +131,7 @@ private: static PeriodicFastController fastController; static void resetAccel() { - engine->tpsAccelEnrichment.resetAE(); + engine->module()->resetAE(); for (size_t i = 0; i < efi::size(engine->injectionEvents.elements); i++) { @@ -163,10 +163,6 @@ static void doPeriodicSlowCallback() { resetAccel(); } - if (engine->versionForConfigurationListeners.isOld(engine->getGlobalConfigurationVersion())) { - updateAccelParameters(); - } - engine->periodicSlowCallback(); #else /* if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT */ #if EFI_INTERNAL_FLASH diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index be269a3dbb..11fe8006af 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -671,7 +671,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta expectedNextPhase = expectNextPhase; if (engine->rpmCalculator.getCachedRpm() > 0 && triggerIndexForListeners == 0) { - engine->tpsAccelEnrichment.onEngineCycleTps(); + engine->module()->onEngineCycleTps(); } // Handle ignition and injection diff --git a/unit_tests/tests/test_accel_enrichment.cpp b/unit_tests/tests/test_accel_enrichment.cpp index 7af42b6bae..2a7511b852 100644 --- a/unit_tests/tests/test_accel_enrichment.cpp +++ b/unit_tests/tests/test_accel_enrichment.cpp @@ -20,23 +20,23 @@ TEST(fuel, testTpsAccelEnrichmentMath) { engine->rpmCalculator.setRpmValue(600); engine->periodicFastCallback(); - engine->tpsAccelEnrichment.setLength(4); + engine->module()->setLength(4); - engine->tpsAccelEnrichment.onNewValue(0); - ASSERT_EQ( 0, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta"; - engine->tpsAccelEnrichment.onNewValue(10); - ASSERT_EQ( 10, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta#1"; - engine->tpsAccelEnrichment.onNewValue(30); - ASSERT_EQ( 20, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta#2"; + engine->module()->onNewValue(0); + ASSERT_EQ( 0, engine->module()->getMaxDelta()) << "maxDelta"; + engine->module()->onNewValue(10); + ASSERT_EQ( 10, engine->module()->getMaxDelta()) << "maxDelta#1"; + engine->module()->onNewValue(30); + ASSERT_EQ( 20, engine->module()->getMaxDelta()) << "maxDelta#2"; - engine->tpsAccelEnrichment.onNewValue(0); - ASSERT_EQ( 20, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta#3"; - engine->tpsAccelEnrichment.onNewValue(0); - ASSERT_EQ( 20, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta#4"; - engine->tpsAccelEnrichment.onNewValue(0); - ASSERT_EQ( 0, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta#5"; - engine->tpsAccelEnrichment.onNewValue(0); - ASSERT_EQ( 0, engine->tpsAccelEnrichment.getMaxDelta()) << "maxDelta"; + engine->module()->onNewValue(0); + ASSERT_EQ( 20, engine->module()->getMaxDelta()) << "maxDelta#3"; + engine->module()->onNewValue(0); + ASSERT_EQ( 20, engine->module()->getMaxDelta()) << "maxDelta#4"; + engine->module()->onNewValue(0); + ASSERT_EQ( 0, engine->module()->getMaxDelta()) << "maxDelta#5"; + engine->module()->onNewValue(0); + ASSERT_EQ( 0, engine->module()->getMaxDelta()) << "maxDelta"; } TEST(fuel, testTpsAccelEnrichmentScheduling) { @@ -58,7 +58,7 @@ TEST(fuel, testTpsAccelEnrichmentScheduling) { eth.fireTriggerEvents2(/* count */ 4, 25 /* ms */); ASSERT_EQ( 1200, Sensor::getOrZero(SensorType::Rpm)) << "RPM"; int expectedInvocationCounter = 1; - ASSERT_EQ(expectedInvocationCounter, engine->tpsAccelEnrichment.onUpdateInvocationCounter); + ASSERT_EQ(expectedInvocationCounter, engine->module()->onUpdateInvocationCounter); Sensor::setMockValue(SensorType::Tps1, 70); eth.fireTriggerEvents2(/* count */ 1, 25 /* ms */); @@ -66,17 +66,17 @@ TEST(fuel, testTpsAccelEnrichmentScheduling) { float expectedAEValue = 1.4; // it does not matter how many times we invoke 'getTpsEnrichment' - state does not change for (int i = 0; i < 20; i++) { - ASSERT_NEAR(expectedAEValue, engine->tpsAccelEnrichment.getTpsEnrichment(), EPS4D); + ASSERT_NEAR(expectedAEValue, engine->module()->getTpsEnrichment(), EPS4D); } expectedInvocationCounter++; - ASSERT_EQ(expectedInvocationCounter, engine->tpsAccelEnrichment.onUpdateInvocationCounter); + ASSERT_EQ(expectedInvocationCounter, engine->module()->onUpdateInvocationCounter); eth.engine.periodicFastCallback(); eth.engine.periodicFastCallback(); eth.engine.periodicFastCallback(); - ASSERT_EQ(expectedInvocationCounter, engine->tpsAccelEnrichment.onUpdateInvocationCounter); + ASSERT_EQ(expectedInvocationCounter, engine->module()->onUpdateInvocationCounter); } static void doFractionalTpsIteration(int period, int divisor, int numCycles, std::vector &tpsEnrich) { @@ -85,12 +85,12 @@ static void doFractionalTpsIteration(int period, int divisor, int numCycles, std // split into 2 portions engineConfiguration->tpsAccelFractionDivisor = divisor; - engine->tpsAccelEnrichment.resetAE(); - engine->tpsAccelEnrichment.onNewValue(0); + engine->module()->resetAE(); + engine->module()->onNewValue(0); for (int i = 0; i < numCycles; i++) { - engine->tpsAccelEnrichment.onNewValue(10); - engine->tpsAccelEnrichment.onEngineCycleTps(); - tpsEnrich[i] = engine->tpsAccelEnrichment.getTpsEnrichment(); + engine->module()->onNewValue(10); + engine->module()->onEngineCycleTps(); + tpsEnrich[i] = engine->module()->getTpsEnrichment(); } } @@ -116,7 +116,7 @@ TEST(fuel, testAccelEnrichmentFractionalTps) { engine->rpmCalculator.setRpmValue(600); engine->periodicFastCallback(); - engine->tpsAccelEnrichment.setLength(2); + engine->module()->setLength(2); const int numCycles = 4;