From 241d491625a9b6e3688e3d3c583781480a13a588 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 26 Jan 2020 12:02:54 -0500 Subject: [PATCH] trigger refactoring --- firmware/controllers/algo/engine.cpp | 8 ++++++ firmware/controllers/algo/engine.h | 1 + .../engine_cycle/rpm_calculator.cpp | 2 +- .../controllers/trigger/trigger_central.cpp | 10 -------- .../controllers/trigger/trigger_central.h | 1 - .../controllers/trigger/trigger_decoder.cpp | 25 +++++++++++++------ .../controllers/trigger/trigger_decoder.h | 7 +++--- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 2d76d49615..b83b2b8d99 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -289,6 +289,14 @@ void Engine::OnTriggerStateProperState(efitick_t nowNt) { rpmCalculator.setSpinningUp(nowNt PASS_ENGINE_PARAMETER_SUFFIX); } +void Engine::OnTriggerSynchronizationLost() { + Engine *engine = this; + EXPAND_Engine; + + // Needed for early instant-RPM detection + engine->rpmCalculator.setStopSpinning(PASS_ENGINE_PARAMETER_SIGNATURE); +} + void Engine::OnTriggerInvalidIndex(int currentIndex) { Engine *engine = this; EXPAND_Engine; diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index b11c384c58..d73c1741e8 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -62,6 +62,7 @@ public: void OnTriggerStateProperState(efitick_t nowNt) override; void OnTriggerSyncronization(bool wasSynchronized) override; void OnTriggerInvalidIndex(int currentIndex) override; + void OnTriggerSynchronizationLost() override; void setConfig(persistent_config_s *config); injection_mode_e getCurrentInjectionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index 5c0e0470f3..042b90ac8a 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -122,7 +122,7 @@ bool RpmCalculator::checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUF /** * Also check if there were no trigger events */ - bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.previousShaftEventTimeNt >= NT_PER_SECOND; + bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.triggerState.previousShaftEventTimeNt >= NT_PER_SECOND; if (noRpmEventsForTooLong || noTriggerEventsForTooLong) { return false; } diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index cd527e420e..811bb4ceaa 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -45,8 +45,6 @@ trigger_central_s::trigger_central_s() : hwEventCounters() { } TriggerCentral::TriggerCentral() : trigger_central_s() { - // we need this initial to have not_running at first invocation - previousShaftEventTimeNt = (efitimems_t) -10 * NT_PER_SECOND; clearCallbacks(&triggerListeneres); triggerState.resetTriggerState(); @@ -340,14 +338,6 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta efiAssertVoid(CUSTOM_ERR_6638, eventIndex >= 0 && eventIndex < HW_EVENT_TYPES, "signal type"); hwEventCounters[eventIndex]++; - if (timestamp - previousShaftEventTimeNt > NT_PER_SECOND) { - /** - * We are here if there is a time gap between now and previous shaft event - that means the engine is not running. - * That means we have lost synchronization since the engine is not running :) - */ - triggerState.onSynchronizationLost(PASS_ENGINE_PARAMETER_SIGNATURE); - } - previousShaftEventTimeNt = timestamp; /** * This invocation changes the state of triggerState diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 41a2f58df5..ef6f12e756 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -45,7 +45,6 @@ public: efitick_t previousVvtCamTime = 0; efitick_t previousVvtCamDuration = 0; - volatile efitick_t previousShaftEventTimeNt; private: IntListenerArray<15> triggerListeneres; diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 87d069c925..61018709be 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -67,6 +67,8 @@ void TriggerState::resetTriggerState() { totalRevolutionCounter = 0; totalTriggerErrorCounter = 0; orderingErrorCounter = 0; + // we need this initial to have not_running at first invocation + previousShaftEventTimeNt = (efitimems_t) -10 * NT_PER_SECOND; lastDecodingErrorTime = US2NT(-10000000LL); someSortOfTriggerError = false; @@ -203,7 +205,7 @@ float TriggerStateWithRunningStatistics::calculateInstantRpm(int *prevIndexOut, // todo: prevIndex should be pre-calculated int prevIndex = TRIGGER_WAVEFORM(triggerIndexByAngle[(int)previousAngle]); - if (prevIndexOut != NULL) { + if (prevIndexOut) { *prevIndexOut = prevIndex; } @@ -330,12 +332,6 @@ bool TriggerState::isEvenRevolution() const { return totalRevolutionCounter & 1; } -void TriggerState::onSynchronizationLost(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - setShaftSynchronized(false); - // Needed for early instant-RPM detection - engine->rpmCalculator.setStopSpinning(PASS_ENGINE_PARAMETER_SIGNATURE); -} - bool TriggerState::validateEventCounters(TriggerWaveform *triggerShape) const { bool isDecodingError = false; for (int i = 0;i < PWM_PHASE_MAX_WAVE_PER_PWM;i++) { @@ -359,7 +355,7 @@ void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycl efitick_t nowNt, trigger_wheel_e triggerWheel, TriggerWaveform *triggerShape) { - if (triggerCycleCallback != NULL) { + if (triggerCycleCallback) { triggerCycleCallback(this); } @@ -389,6 +385,19 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const Trigg trigger_event_e const signal, efitick_t nowNt DECLARE_CONFIG_PARAMETER_SUFFIX) { ScopePerf perf(PE::DecodeTriggerEvent, static_cast(signal)); + if (nowNt - previousShaftEventTimeNt > NT_PER_SECOND) { + /** + * We are here if there is a time gap between now and previous shaft event - that means the engine is not running. + * That means we have lost synchronization since the engine is not running :) + */ + setShaftSynchronized(false); + if (triggerStateListener) { + triggerStateListener->OnTriggerSynchronizationLost(); + } + } + previousShaftEventTimeNt = nowNt; + + bool useOnlyRisingEdgeForTrigger = CONFIG(useOnlyRisingEdgeForTrigger); diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index 25ded30b57..36b39eadf6 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -19,6 +19,7 @@ class TriggerStateListener { virtual void OnTriggerStateProperState(efitick_t nowNt) = 0; virtual void OnTriggerSyncronization(bool wasSynchronized) = 0; virtual void OnTriggerInvalidIndex(int currentIndex) = 0; + virtual void OnTriggerSynchronizationLost() = 0; }; typedef void (*TriggerStateCallback)(TriggerState *); @@ -73,10 +74,7 @@ public: bool validateEventCounters(TriggerWaveform *triggerShape) const; void onShaftSynchronization(const TriggerStateCallback triggerCycleCallback, efitick_t nowNt, trigger_wheel_e triggerWheel, TriggerWaveform *triggerShape); - /** - * Resets synchronization flag and alerts rpm_calculator to reset engine spinning flag. - */ - void onSynchronizationLost(DECLARE_ENGINE_PARAMETER_SIGNATURE); + bool isValidIndex(TriggerWaveform *triggerShape) const; float getTriggerDutyCycle(int index); @@ -86,6 +84,7 @@ public: */ bool shaft_is_synchronized; efitick_t mostRecentSyncTime; + volatile efitick_t previousShaftEventTimeNt; void setTriggerErrorState();