From c729b10bbeb5ee50bcfed94faed28994320aeba7 Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Sat, 26 Nov 2022 12:25:04 -0500 Subject: [PATCH] refactoring: constexpr fuel computer (#4830) * refactoring: constexpr fuel computer * refactoring: constexpr fuel computer * refactoring: constexpr fuel computer Co-authored-by: rusefillc --- firmware/console/binary/live_data.cpp | 2 +- firmware/console/status_loop.cpp | 6 +++--- firmware/controllers/algo/engine.h | 4 ++-- firmware/controllers/algo/engine2.cpp | 2 +- firmware/controllers/algo/fuel_math.cpp | 10 +++------- firmware/controllers/can/can_dash.cpp | 4 ++-- firmware/controllers/can/can_verbose.cpp | 2 +- firmware/controllers/math/closed_loop_fuel_cell.cpp | 2 +- firmware/controllers/tcu/tc_4l6x.cpp | 2 +- unit_tests/tests/test_engine_math.cpp | 4 ++-- 10 files changed, 17 insertions(+), 21 deletions(-) diff --git a/firmware/console/binary/live_data.cpp b/firmware/console/binary/live_data.cpp index 3ef35ebc5f..a2448c950f 100644 --- a/firmware/console/binary/live_data.cpp +++ b/firmware/console/binary/live_data.cpp @@ -57,7 +57,7 @@ const ac_control_s* getLiveDataAddr() { template<> const fuel_computer_s* getLiveDataAddr() { - return engine->fuelComputer; + return &engine->fuelComputer; } template<> diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 43eaedf9fc..54d1601902 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -458,11 +458,11 @@ static void updateThrottles() { static void updateLambda() { float lambdaValue = Sensor::getOrZero(SensorType::Lambda1); engine->outputChannels.lambdaValue = lambdaValue; - engine->outputChannels.AFRValue = lambdaValue * engine->fuelComputer->stoichiometricRatio; + engine->outputChannels.AFRValue = lambdaValue * engine->fuelComputer.stoichiometricRatio; float lambda2Value = Sensor::getOrZero(SensorType::Lambda2); engine->outputChannels.lambdaValue2 = lambda2Value; - engine->outputChannels.AFRValue2 = lambda2Value * engine->fuelComputer->stoichiometricRatio; + engine->outputChannels.AFRValue2 = lambda2Value * engine->fuelComputer.stoichiometricRatio; } static void updateFuelSensors() { @@ -576,7 +576,7 @@ static void updateFuelCorrections() { static void updateFuelResults() { // todo: kill outputChannel while taking care of gauge name and scale! - engine->outputChannels.chargeAirMass = engine->fuelComputer->sdAirMassInOneCylinder; + engine->outputChannels.chargeAirMass = engine->fuelComputer.sdAirMassInOneCylinder; engine->outputChannels.baseFuel = engine->engineState.baseFuel * 1000; // Convert grams to mg engine->outputChannels.fuelRunning = engine->engineState.running.fuel; diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 95da6d85fc..672a097f2d 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -115,8 +115,8 @@ public: PinRepository pinRepository; IEtbController *etbControllers[ETB_COUNT] = {nullptr}; - // we have pointers mixed with... not pointers (reference?) between different controllers - IFuelComputer *fuelComputer = nullptr; + + FuelComputer fuelComputer; type_list< Mockable, diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 6067945fcc..d5d50ac7df 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -197,7 +197,7 @@ void EngineState::periodicFastCallback() { void EngineState::updateTChargeK(int rpm, float tps) { #if EFI_ENGINE_CONTROL - float newTCharge = engine->fuelComputer->getTCharge(rpm, tps); + float newTCharge = engine->fuelComputer.getTCharge(rpm, tps); // convert to microsecs and then to seconds efitick_t curTime = getTimeNowNt(); float secsPassed = (float)NT2US(curTime - timeSinceLastTChargeK) / US_PER_SECOND_F; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index c2793fe634..7cbd7c7c1e 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -176,9 +176,9 @@ static float getBaseFuelMass(int rpm) { auto airmass = model->getAirmass(rpm); // Plop some state for others to read - engine->fuelComputer->sdAirMassInOneCylinder = airmass.CylinderAirmass; + engine->fuelComputer.sdAirMassInOneCylinder = airmass.CylinderAirmass; engine->engineState.fuelingLoad = airmass.EngineLoadPercent; - engine->engineState.ignitionLoad = engine->fuelComputer->getLoadOverride(airmass.EngineLoadPercent, engineConfiguration->ignOverrideMode); + engine->engineState.ignitionLoad = engine->fuelComputer.getLoadOverride(airmass.EngineLoadPercent, engineConfiguration->ignOverrideMode); auto gramPerCycle = airmass.CylinderAirmass * engineConfiguration->specs.cylindersCount; auto gramPerMs = rpm == 0 ? 0 : gramPerCycle / getEngineCycleDuration(rpm); @@ -186,7 +186,7 @@ static float getBaseFuelMass(int rpm) { // convert g/s -> kg/h engine->engineState.airflowEstimate = gramPerMs * 3600000 /* milliseconds per hour */ / 1000 /* grams per kg */;; - float baseFuelMass = engine->fuelComputer->getCycleFuel(airmass.CylinderAirmass, rpm, airmass.EngineLoadPercent); + float baseFuelMass = engine->fuelComputer.getCycleFuel(airmass.CylinderAirmass, rpm, airmass.EngineLoadPercent); // Fudge it by the global correction factor baseFuelMass *= engineConfiguration->globalFuelCorrection; @@ -330,16 +330,12 @@ float getInjectionMass(int rpm) { #endif } -static FuelComputer fuelComputer; - /** * @brief Initialize fuel map data structure * @note this method has nothing to do with fuel map VALUES - it's job * is to prepare the fuel map data structure for 3d interpolation */ void initFuelMap() { - engine->fuelComputer = &fuelComputer; - mapEstimationTable.init(config->mapEstimateTable, config->mapEstimateTpsBins, config->mapEstimateRpmBins); } diff --git a/firmware/controllers/can/can_dash.cpp b/firmware/controllers/can/can_dash.cpp index a3e26f58ed..bbbd0ca81b 100644 --- a/firmware/controllers/can/can_dash.cpp +++ b/firmware/controllers/can/can_dash.cpp @@ -1284,8 +1284,8 @@ static void populateFrame(Aim5f7& msg) { msg.LambdaErr1 = 0; msg.LambdaErr2 = 0; // both targets are the same for now - msg.LambdaTarget1 = (float)engine->fuelComputer->targetLambda; - msg.LambdaTarget2 = (float)engine->fuelComputer->targetLambda; + msg.LambdaTarget1 = (float)engine->fuelComputer.targetLambda; + msg.LambdaTarget2 = (float)engine->fuelComputer.targetLambda; } void canDashboardAim(CanCycle cycle) { diff --git a/firmware/controllers/can/can_verbose.cpp b/firmware/controllers/can/can_verbose.cpp index 2bb8c18a39..11bf28650b 100644 --- a/firmware/controllers/can/can_verbose.cpp +++ b/firmware/controllers/can/can_verbose.cpp @@ -134,7 +134,7 @@ struct Fueling { }; static void populateFrame(Fueling& msg) { - msg.cylAirmass = engine->fuelComputer->sdAirMassInOneCylinder; + msg.cylAirmass = engine->fuelComputer.sdAirMassInOneCylinder; msg.estAirflow = engine->engineState.airflowEstimate; msg.fuel_pulse = (float)engine->outputChannels.actualLastInjection; msg.knockCount = engine->module()->getKnockCount(); diff --git a/firmware/controllers/math/closed_loop_fuel_cell.cpp b/firmware/controllers/math/closed_loop_fuel_cell.cpp index 9a7b79b165..de6cb7f61c 100644 --- a/firmware/controllers/math/closed_loop_fuel_cell.cpp +++ b/firmware/controllers/math/closed_loop_fuel_cell.cpp @@ -53,7 +53,7 @@ float ClosedLoopFuelCellImpl::getLambdaError() const { return 0; } - return lambda.Value - engine->fuelComputer->targetLambda; + return lambda.Value - engine->fuelComputer.targetLambda; } #define MAX_ADJ (0.25f) diff --git a/firmware/controllers/tcu/tc_4l6x.cpp b/firmware/controllers/tcu/tc_4l6x.cpp index b14003827b..eb84adea64 100644 --- a/firmware/controllers/tcu/tc_4l6x.cpp +++ b/firmware/controllers/tcu/tc_4l6x.cpp @@ -143,7 +143,7 @@ void Gm4l6xTransmissionController::setPcState(gear_e gear) { return; } - float duty = 0.01f * interpolate2d(engine->fuelComputer->sdAirMassInOneCylinder, config->tcu_pcAirmassBins, *pcts); + float duty = 0.01f * interpolate2d(engine->fuelComputer.sdAirMassInOneCylinder, config->tcu_pcAirmassBins, *pcts); pcPwm.setSimplePwmDutyCycle(duty); } diff --git a/unit_tests/tests/test_engine_math.cpp b/unit_tests/tests/test_engine_math.cpp index 84825c9a87..727740043f 100644 --- a/unit_tests/tests/test_engine_math.cpp +++ b/unit_tests/tests/test_engine_math.cpp @@ -16,7 +16,7 @@ TEST(misc, testIgnitionPlanning) { EngineTestHelper eth(FORD_ESCORT_GT); engine->periodicFastCallback(); - assertEqualsM("testIgnitionPlanning_AFR", 13.5, engine->fuelComputer->targetAFR); + assertEqualsM("testIgnitionPlanning_AFR", 13.5, engine->fuelComputer.targetAFR); ASSERT_EQ(IM_BATCH, engineConfiguration->injectionMode); } @@ -33,7 +33,7 @@ TEST(misc, testEngineMath) { ASSERT_NEAR( 50, getOneDegreeTimeMs(600) * 180, EPS4D) << "600 RPM"; ASSERT_EQ( 5, getOneDegreeTimeMs(6000) * 180) << "6000 RPM"; - IFuelComputer *fuelComputer = engine->fuelComputer; + auto fuelComputer = &engine->fuelComputer; Sensor::setMockValue(SensorType::Clt, 300); Sensor::setMockValue(SensorType::Iat, 350);