From 95798a524651ba0c456dd40a2fbb738d050d3b9f Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 5 Sep 2020 15:49:42 -0700 Subject: [PATCH] fix broken master, inject engine ptr to rpmcalculator (#1759) * fix * oops --- firmware/console/status_loop.cpp | 2 +- .../controllers/actuators/idle_thread.cpp | 2 +- .../controllers/algo/accel_enrichment.cpp | 2 +- firmware/controllers/algo/advance_map.cpp | 2 +- firmware/controllers/algo/engine.cpp | 10 ++-- firmware/controllers/algo/engine2.cpp | 6 +- firmware/controllers/algo/fuel_math.cpp | 2 +- firmware/controllers/core/fsio_impl.cpp | 4 +- firmware/controllers/engine_controller.cpp | 8 +-- .../controllers/engine_controller_misc.cpp | 6 +- .../engine_cycle/main_trigger_callback.cpp | 6 +- .../engine_cycle/rpm_calculator.cpp | 58 ++++++++++--------- .../controllers/engine_cycle/rpm_calculator.h | 35 ++++++----- .../controllers/math/closed_loop_fuel.cpp | 2 +- firmware/controllers/math/engine_math.cpp | 4 +- .../sensors/sensor_info_printing.cpp | 11 ++++ .../controllers/sensors/stored_value_sensor.h | 6 +- firmware/hw_layer/sensors/cj125.cpp | 2 +- firmware/hw_layer/stepper.cpp | 2 +- unit_tests/tests/test_accel_enrichment.cpp | 4 +- unit_tests/tests/test_fuel_wall_wetting.cpp | 2 +- .../tests/trigger/test_trigger_decoder.cpp | 14 ++--- 22 files changed, 104 insertions(+), 86 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index fe21ba58b4..e110a3a4fe 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -304,7 +304,7 @@ static void showFuelInfo2(float rpm, float engineLoad) { scheduleMsg(&logger, "base cranking fuel %.2f", engineConfiguration->cranking.baseFuel); scheduleMsg(&logger2, "cranking fuel: %.2f", ENGINE(engineState.cranking.fuel)); - if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (!engine->rpmCalculator.isStopped()) { float iatCorrection = engine->engineState.running.intakeTemperatureCoefficient; float cltCorrection = engine->engineState.running.coolantTemperatureCoefficient; floatms_t injectorLag = engine->engineState.running.injectorLag; diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 96c57bf296..9556f887f4 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -500,7 +500,7 @@ static percent_t automaticIdleController(float tpsPos DECLARE_ENGINE_PARAMETER_S const auto [cltValid, clt] = Sensor::get(SensorType::Clt); #if EFI_SHAFT_POSITION_INPUT - bool isRunning = engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isRunning = engine->rpmCalculator.isRunning(); #else bool isRunning = false; #endif /* EFI_SHAFT_POSITION_INPUT */ diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index 9f57435073..0fadc3a9ca 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -49,7 +49,7 @@ floatms_t WallFuel::adjust(floatms_t desiredFuel DECLARE_ENGINE_PARAMETER_SUFFIX return desiredFuel; } // disable this correction for cranking - if (ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (ENGINE(rpmCalculator).isCranking()) { return desiredFuel; } diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index efc8371859..a1643d12b5 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -203,7 +203,7 @@ angle_t getAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_SUFFIX) { angle_t angle; - bool isCranking = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isCranking = ENGINE(rpmCalculator).isCranking(); if (isCranking) { angle = getCrankingAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER_SUFFIX); assertAngleRange(angle, "crAngle", CUSTOM_ERR_6680); diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index ecf67e7861..49a72940ad 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -138,7 +138,7 @@ static void cylinderCleanupControl(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #if EFI_ENGINE_CONTROL bool newValue; if (engineConfiguration->isCylinderCleanupEnabled) { - newValue = !engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE) && Sensor::get(SensorType::DriverThrottleIntent).value_or(0) > CLEANUP_MODE_TPS; + newValue = !engine->rpmCalculator.isRunning() && Sensor::get(SensorType::DriverThrottleIntent).value_or(0) > CLEANUP_MODE_TPS; } else { newValue = false; } @@ -179,7 +179,7 @@ void Engine::periodicSlowCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #if (BOARD_TLE8888_COUNT > 0) static efitick_t tle8888CrankingResetTime = 0; - if (CONFIG(useTLE8888_cranking_hack) && ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (CONFIG(useTLE8888_cranking_hack) && ENGINE(rpmCalculator).isCranking()) { efitick_t nowNt = getTimeNowNt(); if (nowNt - tle8888CrankingResetTime > MS2NT(300)) { requestTLE8888initialization(); @@ -332,7 +332,7 @@ void Engine::OnTriggerStateProperState(efitick_t nowNt) { triggerCentral.triggerState.runtimeStatistics(&triggerCentral.triggerFormDetails, nowNt PASS_ENGINE_PARAMETER_SUFFIX); - rpmCalculator.setSpinningUp(nowNt PASS_ENGINE_PARAMETER_SUFFIX); + rpmCalculator.setSpinningUp(nowNt); } void Engine::OnTriggerSynchronizationLost() { @@ -340,7 +340,7 @@ void Engine::OnTriggerSynchronizationLost() { EXPAND_Engine; // Needed for early instant-RPM detection - engine->rpmCalculator.setStopSpinning(PASS_ENGINE_PARAMETER_SIGNATURE); + engine->rpmCalculator.setStopSpinning(); } void Engine::OnTriggerInvalidIndex(int currentIndex) { @@ -499,7 +499,7 @@ bool Engine::isInShutdownMode() const { } injection_mode_e Engine::getCurrentInjectionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return rpmCalculator.isCranking(PASS_ENGINE_PARAMETER_SIGNATURE) ? CONFIG(crankingInjectionMode) : CONFIG(injectionMode); + return rpmCalculator.isCranking() ? CONFIG(crankingInjectionMode) : CONFIG(injectionMode); } // see also in TunerStudio project '[doesTriggerImplyOperationMode] tag diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 4962740741..4a0019a365 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -116,7 +116,7 @@ EngineState::EngineState() { void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // this feeds rusEfi console Live Data - engine->engineState.isCrankingState = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE); + engine->engineState.isCrankingState = ENGINE(rpmCalculator).isCranking(); } void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { @@ -127,7 +127,7 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { warning(CUSTOM_SLOW_NOT_INVOKED, "Slow not invoked yet"); } efitick_t nowNt = getTimeNowNt(); - if (ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (ENGINE(rpmCalculator).isCranking()) { crankingTime = nowNt; timeSinceCranking = 0.0f; } else { @@ -135,7 +135,7 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } updateAuxValves(PASS_ENGINE_PARAMETER_SIGNATURE); - int rpm = ENGINE(rpmCalculator).getRpm(PASS_ENGINE_PARAMETER_SIGNATURE); + int rpm = ENGINE(rpmCalculator).getRpm(); sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER_SUFFIX); dwellAngle = cisnan(rpm) ? NAN : sparkDwell / getOneDegreeTimeMs(rpm); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 96be51e141..3cce0ba9d6 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -324,7 +324,7 @@ floatms_t getInjectionDuration(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { // Always update base fuel - some cranking modes use it floatms_t baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER_SUFFIX); - bool isCranking = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isCranking = ENGINE(rpmCalculator).isCranking(); floatms_t fuelPerCycle = getFuel(isCranking, baseFuel PASS_ENGINE_PARAMETER_SUFFIX); efiAssert(CUSTOM_ERR_ASSERT, !cisnan(fuelPerCycle), "NaN fuelPerCycle", 0); diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index 038f0594f2..6e94a4bdd5 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -471,7 +471,7 @@ void runFsio(DECLARE_ENGINE_PARAMETER_SIGNATURE) { * todo: convert to FSIO? * open question if heater should be ON during cranking */ - enginePins.o2heater.setValue(engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE)); + enginePins.o2heater.setValue(engine->rpmCalculator.isRunning()); if (CONFIG(acRelayPin) != GPIO_UNASSIGNED) { setPinState("A/C", &enginePins.acRelay, acRelayLogic PASS_ENGINE_PARAMETER_SUFFIX); @@ -767,7 +767,7 @@ void runHardcodedFsio(DECLARE_ENGINE_PARAMETER_SIGNATURE) { enginePins.fuelPumpRelay.setValue((getTimeNowSeconds() < engineConfiguration->startUpFuelPumpDuration) || (engine->rpmCalculator.getRpm() > 0)); } - enginePins.o2heater.setValue(engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE)); + enginePins.o2heater.setValue(engine->rpmCalculator.isRunning()); } #endif /* EFI_FSIO */ diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index d9cccd256d..72fc3a5f94 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -170,7 +170,7 @@ class EngineStateBlinkingTask : public PeriodicTimerController { void PeriodicTask() override { counter++; #if EFI_SHAFT_POSITION_INPUT - bool is_running = ENGINE(rpmCalculator).isRunning(PASS_ENGINE_PARAMETER_SIGNATURE); + bool is_running = ENGINE(rpmCalculator).isRunning(); #else bool is_running = false; #endif /* EFI_SHAFT_POSITION_INPUT */ @@ -179,7 +179,7 @@ class EngineStateBlinkingTask : public PeriodicTimerController { // blink in running mode enginePins.runningLedPin.setValue(counter % 2); } else { - int is_cranking = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE); + int is_cranking = ENGINE(rpmCalculator).isCranking(); enginePins.runningLedPin.setValue(is_cranking); } } @@ -240,7 +240,7 @@ static void doPeriodicSlowCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engine->rpmCalculator.setStopSpinning(PASS_ENGINE_PARAMETER_SIGNATURE); } - if (ENGINE(directSelfStimulation) || engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (ENGINE(directSelfStimulation) || engine->rpmCalculator.isStopped()) { /** * rusEfi usually runs on hardware which halts execution while writing to internal flash, so we * postpone writes to until engine is stopped. Writes in case of self-stimulation are fine. @@ -254,7 +254,7 @@ static void doPeriodicSlowCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } - if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (!engine->rpmCalculator.isStopped()) { updatePrimeInjectionPulseState(PASS_ENGINE_PARAMETER_SIGNATURE); } diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index be51bbe8c7..6dab7eb6c9 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -154,14 +154,14 @@ void touchTimeCounter() { static void onStartStopButtonToggle(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engine->startStopStateToggleCounter++; - if (engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (engine->rpmCalculator.isStopped()) { engine->startStopStateLastPushTime = getTimeNowNt(); bool wasStarterEngaged = enginePins.starterControl.getAndSet(1); if (!wasStarterEngaged) { scheduleMsg(&sharedLogger, "Let's crank this engine for up to %dseconds!", CONFIG(startCrankingDuration)); } - } else if (engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE)) { + } else if (engine->rpmCalculator.isRunning()) { scheduleMsg(&sharedLogger, "Let's stop this engine!"); scheduleStopEngine(); } @@ -192,7 +192,7 @@ void slowStartStopButtonCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // todo: should this be simply FSIO? - if (engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (engine->rpmCalculator.isRunning()) { // turn starter off once engine is running bool wasStarterEngaged = enginePins.starterControl.getAndSet(0); if (wasStarterEngaged) { diff --git a/firmware/controllers/engine_cycle/main_trigger_callback.cpp b/firmware/controllers/engine_cycle/main_trigger_callback.cpp index 8d89f251b9..8fdc288370 100644 --- a/firmware/controllers/engine_cycle/main_trigger_callback.cpp +++ b/firmware/controllers/engine_cycle/main_trigger_callback.cpp @@ -217,7 +217,7 @@ void InjectionEvent::onTriggerTooth(size_t trgEventIndex, int rpm, efitick_t now } #endif /*EFI_PRINTF_FUEL_DETAILS */ - bool isCranking = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isCranking = ENGINE(rpmCalculator).isCranking(); /** * todo: pre-calculate 'numberOfInjections' * see also injectorDutyCycle @@ -464,7 +464,7 @@ static void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEvent // Check if the engine is not stopped or cylinder cleanup is activated static bool isPrimeInjectionPulseSkipped(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) + if (!engine->rpmCalculator.isStopped()) return true; return CONFIG(isCylinderCleanupEnabled) && (Sensor::get(SensorType::Tps1).value_or(0) > CLEANUP_MODE_TPS); } @@ -520,7 +520,7 @@ void updatePrimeInjectionPulseState(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (counterWasReset) return; - if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (!engine->rpmCalculator.isStopped()) { backupRamSave(BACKUP_IGNITION_SWITCH_COUNTER, 0); counterWasReset = true; } diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index f09061973f..e400908873 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -48,17 +48,17 @@ float RpmCalculator::getRpmAcceleration() const { return 1.0 * previousRpmValue / rpmValue; } -bool RpmCalculator::isStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { +bool RpmCalculator::isStopped() const { // Spinning-up with zero RPM means that the engine is not ready yet, and is treated as 'stopped'. return state == STOPPED || (state == SPINNING_UP && rpmValue == 0); } -bool RpmCalculator::isCranking(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { +bool RpmCalculator::isCranking() const { // Spinning-up with non-zero RPM is suitable for all engine math, as good as cranking return state == CRANKING || (state == SPINNING_UP && rpmValue > 0); } -bool RpmCalculator::isSpinningUp(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { +bool RpmCalculator::isSpinningUp() const { return state == SPINNING_UP; } @@ -71,7 +71,7 @@ uint32_t RpmCalculator::getRevolutionCounterSinceStart(void) const { * See NOISY_RPM */ // todo: migrate to float return result or add a float version? this would have with calculations -int RpmCalculator::getRpm(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { +int RpmCalculator::getRpm() const { #if !EFI_PROD_CODE if (mockRpm != MOCK_UNDEFINED) { return mockRpm; @@ -87,7 +87,7 @@ EXTERN_ENGINE; static Logging * logger; RpmCalculator::RpmCalculator() : - rpmSensor(SensorType::Rpm, 0) + StoredValueSensor(SensorType::Rpm, 0) { #if !EFI_PROD_CODE mockRpm = MOCK_UNDEFINED; @@ -102,14 +102,14 @@ RpmCalculator::RpmCalculator() : /** * @return true if there was a full shaft revolution within the last second */ -bool RpmCalculator::isRunning(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { +bool RpmCalculator::isRunning() const { return state == RUNNING; } /** * @return true if engine is spinning (cranking or running) */ -bool RpmCalculator::checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) const { +bool RpmCalculator::checkIfSpinning(efitick_t nowNt) const { if (ENGINE(needToStopEngine(nowNt))) { return false; } @@ -130,18 +130,17 @@ bool RpmCalculator::checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUF return true; } -void RpmCalculator::assignRpmValue(float floatRpmValue DECLARE_ENGINE_PARAMETER_SUFFIX) { +void RpmCalculator::assignRpmValue(float floatRpmValue) { previousRpmValue = rpmValue; // we still persist integer RPM! todo: figure out the next steps rpmValue = floatRpmValue; - if (!CONFIG(consumeObdSensors)) { - rpmSensor.Register(); - } - if (rpmValue <= 0) { oneDegreeUs = NAN; + invalidate(); } else { + setValidValue(floatRpmValue, 0); // 0 for current time since RPM sensor never times out + // here it's really important to have more precise float RPM value, see #796 oneDegreeUs = getOneDegreeTimeUs(floatRpmValue); if (previousRpmValue == 0) { @@ -154,8 +153,8 @@ void RpmCalculator::assignRpmValue(float floatRpmValue DECLARE_ENGINE_PARAMETER_ } } -void RpmCalculator::setRpmValue(float value DECLARE_ENGINE_PARAMETER_SUFFIX) { - assignRpmValue(value PASS_ENGINE_PARAMETER_SUFFIX); +void RpmCalculator::setRpmValue(float value) { + assignRpmValue(value); spinning_state_e oldState = state; // Change state if (rpmValue == 0) { @@ -198,31 +197,31 @@ uint32_t RpmCalculator::getRevolutionCounterM(void) const { return revolutionCounterSinceBoot; } -void RpmCalculator::setStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +void RpmCalculator::setStopped() { revolutionCounterSinceStart = 0; if (rpmValue != 0) { - assignRpmValue(0 PASS_ENGINE_PARAMETER_SUFFIX); + assignRpmValue(0); scheduleMsg(logger, "engine stopped"); } state = STOPPED; } -void RpmCalculator::setStopSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +void RpmCalculator::setStopSpinning() { isSpinning = false; - setStopped(PASS_ENGINE_PARAMETER_SIGNATURE); + setStopped(); } -void RpmCalculator::setSpinningUp(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { +void RpmCalculator::setSpinningUp(efitick_t nowNt) { if (!CONFIG(isFasterEngineSpinUpEnabled)) return; // Only a completely stopped and non-spinning engine can enter the spinning-up state. - if (isStopped(PASS_ENGINE_PARAMETER_SIGNATURE) && !isSpinning) { + if (isStopped() && !isSpinning) { state = SPINNING_UP; engine->triggerCentral.triggerState.spinningEventIndex = 0; isSpinning = true; } // update variables needed by early instant RPM calc. - if (isSpinningUp(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (isSpinningUp()) { engine->triggerCentral.triggerState.setLastEventTimeForInstantRpm(nowNt PASS_ENGINE_PARAMETER_SUFFIX); } /** @@ -245,7 +244,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, RpmCalculator *rpmState = &engine->rpmCalculator; if (index == 0) { - bool hadRpmRecently = rpmState->checkIfSpinning(nowNt PASS_ENGINE_PARAMETER_SUFFIX); + bool hadRpmRecently = rpmState->checkIfSpinning(nowNt); if (hadRpmRecently) { efitick_t diffNt = nowNt - rpmState->lastRpmEventTimeNt; @@ -257,11 +256,11 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, * */ if (diffNt == 0) { - rpmState->setRpmValue(NOISY_RPM PASS_ENGINE_PARAMETER_SUFFIX); + rpmState->setRpmValue(NOISY_RPM); } else { int mult = (int)getEngineCycle(engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE)) / 360; float rpm = 60.0 * NT_PER_SECOND * mult / diffNt; - rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm PASS_ENGINE_PARAMETER_SUFFIX); + rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm); } } rpmState->onNewEngineCycle(); @@ -279,7 +278,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, } #endif /* EFI_SENSOR_CHART */ - if (rpmState->isSpinningUp(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (rpmState->isSpinningUp()) { // we are here only once trigger is synchronized for the first time // while transitioning from 'spinning' to 'running' // Replace 'normal' RPM with instant RPM for the initial spin-up period @@ -289,7 +288,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, &prevIndex, nowNt PASS_ENGINE_PARAMETER_SUFFIX); // validate instant RPM - we shouldn't skip the cranking state instantRpm = minI(instantRpm, CONFIG(cranking.rpm) - 1); - rpmState->assignRpmValue(instantRpm PASS_ENGINE_PARAMETER_SUFFIX); + rpmState->assignRpmValue(instantRpm); #if 0 scheduleMsg(logger, "** RPM: idx=%d sig=%d iRPM=%d", index, ckpSignalType, instantRpm); #endif @@ -361,11 +360,18 @@ float getCrankshaftAngleNt(efitick_t timeNt DECLARE_ENGINE_PARAMETER_SUFFIX) { } void initRpmCalculator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { + INJECT_ENGINE_REFERENCE(&ENGINE(rpmCalculator)); + logger = sharedLogger; if (hasFirmwareError()) { return; } + // Only register if not configured to read RPM over OBD2 + if (!CONFIG(consumeObdSensors)) { + ENGINE(rpmCalculator).Register(); + } + #if !EFI_UNIT_TEST addTriggerEventListener(tdcMarkCallback, "chart TDC mark", engine); #endif diff --git a/firmware/controllers/engine_cycle/rpm_calculator.h b/firmware/controllers/engine_cycle/rpm_calculator.h index 3e7430dfe4..fdf159c324 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.h +++ b/firmware/controllers/engine_cycle/rpm_calculator.h @@ -41,8 +41,10 @@ typedef enum { RUNNING, } spinning_state_e; -class RpmCalculator { +class RpmCalculator : public StoredValueSensor { public: + DECLARE_ENGINE_PTR; + #if !EFI_PROD_CODE int mockRpm; #endif /* EFI_PROD_CODE */ @@ -50,23 +52,21 @@ public: /** * Returns true if the engine is not spinning (RPM==0) */ - bool isStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + bool isStopped() const; /** * Returns true if the engine is spinning up */ - bool isSpinningUp(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + bool isSpinningUp() const; /** * Returns true if the engine is cranking OR spinning up */ - bool isCranking(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + bool isCranking() const; /** * Returns true if the engine is running and not cranking */ - bool isRunning(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + bool isRunning() const; - bool checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) const; - - StoredValueSensor rpmSensor; + bool checkIfSpinning(efitick_t nowNt) const; /** * This accessor is used in unit-tests. @@ -76,28 +76,28 @@ public: /** * Should be called on every trigger event when the engine is just starting to spin up. */ - void setSpinningUp(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); + void setSpinningUp(efitick_t nowNt ); /** * Called if the synchronization is lost due to a trigger timeout. */ - void setStopSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE); + void setStopSpinning(); /** * Just a getter for rpmValue * Also handles mockRpm if not EFI_PROD_CODE */ - int getRpm(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + int getRpm() const; /** * This method is invoked once per engine cycle right after we calculate new RPM value */ void onNewEngineCycle(); uint32_t getRevolutionCounterM(void) const; - void setRpmValue(float value DECLARE_ENGINE_PARAMETER_SUFFIX); + void setRpmValue(float value); /** * The same as setRpmValue() but without state change. * We need this to be public because of calling rpmState->assignRpmValue() from rpmShaftPositionCallback() */ - void assignRpmValue(float value DECLARE_ENGINE_PARAMETER_SUFFIX); + void assignRpmValue(float value); uint32_t getRevolutionCounterSinceStart(void) const; /** * RPM rate of change between current RPM and RPM measured during previous engine cycle @@ -114,6 +114,11 @@ public: */ volatile floatus_t oneDegreeUs = NAN; volatile efitick_t lastRpmEventTimeNt = 0; + +protected: + // Print sensor info - current RPM state + void showInfo(Logging* logger, const char* sensorName) const override; + private: /** * Sometimes we cannot afford to call isRunning() and the value is good enough @@ -123,7 +128,7 @@ private: /** * Should be called once we've realized engine is not spinning any more. */ - void setStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE); + void setStopped(); /** * This counter is incremented with each revolution of one of the shafts. Could be @@ -145,7 +150,7 @@ private: }; // Just a getter for rpmValue which also handles mockRpm if not EFI_PROD_CODE -#define GET_RPM() ( ENGINE(rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE)) ) +#define GET_RPM() ( ENGINE(rpmCalculator.getRpm()) ) #define isValidRpm(rpm) ((rpm) > 0 && (rpm) < UNREALISTIC_RPM) diff --git a/firmware/controllers/math/closed_loop_fuel.cpp b/firmware/controllers/math/closed_loop_fuel.cpp index 108a3287f4..4276d0473b 100644 --- a/firmware/controllers/math/closed_loop_fuel.cpp +++ b/firmware/controllers/math/closed_loop_fuel.cpp @@ -47,7 +47,7 @@ static bool shouldCorrect(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } // Don't correct if not running - if (!ENGINE(rpmCalculator).isRunning(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (!ENGINE(rpmCalculator).isRunning()) { return false; } diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index d51a02deb2..3a608a57ae 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -112,7 +112,7 @@ static floatms_t getCrankingSparkDwell(DECLARE_ENGINE_PARAMETER_SIGNATURE) { floatms_t getSparkDwell(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { #if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT float dwellMs; - if (ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (ENGINE(rpmCalculator).isCranking()) { dwellMs = getCrankingSparkDwell(PASS_ENGINE_PARAMETER_SIGNATURE); } else { efiAssert(CUSTOM_ERR_ASSERT, !cisnan(rpm), "invalid rpm", NAN); @@ -378,7 +378,7 @@ ignition_mode_e getCurrentIgnitionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE) { ignition_mode_e ignitionMode = CONFIG(ignitionMode); #if EFI_SHAFT_POSITION_INPUT // In spin-up cranking mode we don't have full phase sync. info yet, so wasted spark mode is better - if (ignitionMode == IM_INDIVIDUAL_COILS && ENGINE(rpmCalculator.isSpinningUp(PASS_ENGINE_PARAMETER_SIGNATURE))) + if (ignitionMode == IM_INDIVIDUAL_COILS && ENGINE(rpmCalculator.isSpinningUp())) ignitionMode = IM_WASTED_SPARK; #endif /* EFI_SHAFT_POSITION_INPUT */ return ignitionMode; diff --git a/firmware/controllers/sensors/sensor_info_printing.cpp b/firmware/controllers/sensors/sensor_info_printing.cpp index c89c80d873..d8c55052e1 100644 --- a/firmware/controllers/sensors/sensor_info_printing.cpp +++ b/firmware/controllers/sensors/sensor_info_printing.cpp @@ -2,6 +2,7 @@ #include "proxy_sensor.h" #include "functional_sensor.h" #include "redundant_sensor.h" +#include "rpm_calculator.h" #include "linear_func.h" #include "resistance_func.h" #include "thermistor_func.h" @@ -35,6 +36,16 @@ void RedundantSensor::showInfo(Logging* logger, const char* sensorName) const { scheduleMsg(logger, "Sensor \"%s\" is redundant combining \"%s\" and \"%s\"", sensorName, getSensorName(m_first), getSensorName(m_second)); } +void RpmCalculator::showInfo(Logging* logger, const char* sensorName) const { + scheduleMsg(logger, "RPM sensor: stopped: %d spinning up: %d cranking: %d running: %d rpm: %f", + isStopped(), + isSpinningUp(), + isCranking(), + isRunning(), + get().value_or(0) + ); +} + void LinearFunc::showInfo(Logging* logger, float testRawValue) const { scheduleMsg(logger, " Linear function slope: %.2f offset: %.2f min: %.1f max: %.1f", m_a, m_b, m_minOutput, m_maxOutput); const auto [valid, value] = convert(testRawValue); diff --git a/firmware/controllers/sensors/stored_value_sensor.h b/firmware/controllers/sensors/stored_value_sensor.h index fcb521b95a..ed17e70541 100644 --- a/firmware/controllers/sensors/stored_value_sensor.h +++ b/firmware/controllers/sensors/stored_value_sensor.h @@ -46,17 +46,13 @@ public: return value; } - void showInfo(Logging* logger, const char* sensorName) const override { - // todo: just print name and value? - } - +protected: StoredValueSensor(SensorType type, efitick_t timeoutNt) : Sensor(type) , m_timeoutPeriod(timeoutNt) { } -protected: // Invalidate the stored value. void invalidate() { m_isValid = false; diff --git a/firmware/hw_layer/sensors/cj125.cpp b/firmware/hw_layer/sensors/cj125.cpp index e1b1a5c991..a4ecc4ae96 100644 --- a/firmware/hw_layer/sensors/cj125.cpp +++ b/firmware/hw_layer/sensors/cj125.cpp @@ -421,7 +421,7 @@ static bool cjStartSpi(DECLARE_ENGINE_PARAMETER_SIGNATURE) { static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) { { efitick_t nowNt = getTimeNowNt(); - bool isStopped = engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isStopped = engine->rpmCalculator.isStopped(); cjUpdateAnalogValues(); diff --git a/firmware/hw_layer/stepper.cpp b/firmware/hw_layer/stepper.cpp index 3e4e76803b..473e5f2fe4 100644 --- a/firmware/hw_layer/stepper.cpp +++ b/firmware/hw_layer/stepper.cpp @@ -50,7 +50,7 @@ void StepperMotor::ThreadTask() { waitForSlowAdc(); #endif #if EFI_SHAFT_POSITION_INPUT - bool isRunning = engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE); + bool isRunning = engine->rpmCalculator.isRunning(); #else bool isRunning = false; #endif /* EFI_SHAFT_POSITION_INPUT */ diff --git a/unit_tests/tests/test_accel_enrichment.cpp b/unit_tests/tests/test_accel_enrichment.cpp index 8259f6f4c1..79531a4f69 100644 --- a/unit_tests/tests/test_accel_enrichment.cpp +++ b/unit_tests/tests/test_accel_enrichment.cpp @@ -17,7 +17,7 @@ TEST(fuel, testTpsAccelEnrichmentMath) { WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); - engine->rpmCalculator.setRpmValue(600 PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(600); engine->periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE); engine->tpsAccelEnrichment.setLength(4); @@ -107,7 +107,7 @@ TEST(fuel, testAccelEnrichmentFractionalTps) { Logging logger; initAccelEnrichment(&logger PASS_ENGINE_PARAMETER_SUFFIX); - engine->rpmCalculator.setRpmValue(600 PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(600); engine->periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE); engine->tpsAccelEnrichment.setLength(2); diff --git a/unit_tests/tests/test_fuel_wall_wetting.cpp b/unit_tests/tests/test_fuel_wall_wetting.cpp index 54acc45c17..b875ccc0ad 100644 --- a/unit_tests/tests/test_fuel_wall_wetting.cpp +++ b/unit_tests/tests/test_fuel_wall_wetting.cpp @@ -15,7 +15,7 @@ TEST(fuel, testWallWettingEnrichmentMath) { engineConfiguration->wwaeTau = 1.0f; engineConfiguration->wwaeBeta = 0.40f; - engine->rpmCalculator.setRpmValue(3000 PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(3000); WallFuel wallFuel; diff --git a/unit_tests/tests/trigger/test_trigger_decoder.cpp b/unit_tests/tests/trigger/test_trigger_decoder.cpp index 4229c72596..deed5c98fc 100644 --- a/unit_tests/tests/trigger/test_trigger_decoder.cpp +++ b/unit_tests/tests/trigger/test_trigger_decoder.cpp @@ -205,7 +205,7 @@ TEST(misc, testGetCoilDutyCycleIssue977) { WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); int rpm = 2000; - engine->rpmCalculator.setRpmValue(rpm PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(rpm); ASSERT_EQ( 4, getSparkDwell(rpm PASS_ENGINE_PARAMETER_SUFFIX)) << "running dwell"; ASSERT_NEAR( 26.66666, getCoilDutyCycle(rpm PASS_ENGINE_PARAMETER_SUFFIX), 0.0001); @@ -224,13 +224,13 @@ TEST(misc, testFordAspire) { engineConfiguration->crankingTimingAngle = 31; engineConfiguration->useConstantDwellDuringCranking = false; - engine->rpmCalculator.setRpmValue(200 PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(200); assertEqualsM("cranking dwell", 54.166670, getSparkDwell(200 PASS_ENGINE_PARAMETER_SUFFIX)); int rpm = 2000; - engine->rpmCalculator.setRpmValue(rpm PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(rpm); ASSERT_EQ( 4, getSparkDwell(rpm PASS_ENGINE_PARAMETER_SUFFIX)) << "running dwell"; - engine->rpmCalculator.setRpmValue(6000 PASS_ENGINE_PARAMETER_SUFFIX); + engine->rpmCalculator.setRpmValue(6000); assertEqualsM("higher rpm dwell", 3.25, getSparkDwell(6000 PASS_ENGINE_PARAMETER_SUFFIX)); } @@ -353,7 +353,7 @@ TEST(misc, testRpmCalculator) { assertEqualsM("injection angle", 31.365, ie0->injectionStart.angleOffsetFromTriggerEvent); eth.firePrimaryTriggerRise(); - ASSERT_EQ(1500, eth.engine.rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE)); + ASSERT_EQ(1500, eth.engine.rpmCalculator.getRpm()); assertEqualsM("dwell", 4.5, engine->engineState.dwellAngle); assertEqualsM("fuel #2", 4.5450, engine->injectionDuration); @@ -407,7 +407,7 @@ TEST(misc, testRpmCalculator) { assertEqualsM("dwell", 4.5, eth.engine.engineState.dwellAngle); assertEqualsM("fuel #3", 4.5450, eth.engine.injectionDuration); - ASSERT_EQ(1500, eth.engine.rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE)); + ASSERT_EQ(1500, eth.engine.rpmCalculator.getRpm()); eth.assertInjectorUpEvent("ev 0/2", 0, -4849, 2); @@ -1283,7 +1283,7 @@ TEST(big, testMissedSpark299) { printf("*************************************************** testMissedSpark299 start\r\n"); - ASSERT_EQ(3000, eth.engine.rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE)); + ASSERT_EQ(3000, eth.engine.rpmCalculator.getRpm()); setWholeTimingTable(3); eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE);