diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index bd8e8cc487..f4b709547c 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -461,6 +461,7 @@ void Engine::OnTriggerSyncronization(bool wasSynchronized) { #endif void Engine::injectEngineReferences() { + INJECT_ENGINE_REFERENCE(&triggerCentral); INJECT_ENGINE_REFERENCE(&primaryTriggerConfiguration); for (int camIndex = 0;camIndex < CAMS_PER_BANK;camIndex++) { INJECT_ENGINE_REFERENCE(&vvtTriggerConfiguration[camIndex]); diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index 05b6b1cf36..272f5ddb5a 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -85,6 +85,7 @@ RpmCalculator::RpmCalculator() : #endif /* EFI_PROD_CODE */ // todo: reuse assignRpmValue() method which needs PASS_ENGINE_PARAMETER_SUFFIX // which we cannot provide inside this parameter-less constructor. need a solution for this minor mess + setValidValue(0, 0); // 0 for current time since RPM sensor never times out } /** @@ -127,7 +128,7 @@ void RpmCalculator::assignRpmValue(float floatRpmValue) { if (rpmValue <= 0) { oneDegreeUs = NAN; - invalidate(); + setValidValue(0, 0); // 0 for current time since RPM sensor never times out } else { setValidValue(floatRpmValue, 0); // 0 for current time since RPM sensor never times out diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 6248cbe2d4..aecf340892 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -78,6 +78,16 @@ angle_t TriggerCentral::getVVTPosition(uint8_t bankIndex, uint8_t camIndex) { return vvtPosition[bankIndex][camIndex]; } +expected TriggerCentral::getCurrentEnginePhase(efitick_t nowNt) const { + floatus_t oneDegreeUs = engine->rpmCalculator.oneDegreeUs; + + if (cisnan(oneDegreeUs)) { + return unexpected; + } + + return m_virtualZeroTimer.getElapsedUs(nowNt) / oneDegreeUs; +} + /** * todo: why is this method NOT reciprocal to getRpmMultiplier?! */ @@ -259,8 +269,8 @@ void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index DECL logFront(isImportantFront, nowNt, index PASS_ENGINE_PARAMETER_SUFFIX); - floatus_t oneDegreeUs = engine->rpmCalculator.oneDegreeUs; - if (cisnan(oneDegreeUs)) { + auto currentPhase = tc->getCurrentEnginePhase(nowNt); + if (!currentPhase) { // todo: this code branch is slowing NB2 cranking since we require RPM sync for VVT sync! // todo: smarter code // @@ -278,11 +288,9 @@ void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index DECL front == TV_RISE ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING, nowNt); } - tc->vvtCamCounter++; - float offsetUs = tc->virtualZeroTimer.getElapsedUs(nowNt); - angle_t currentPosition = offsetUs / oneDegreeUs; + angle_t currentPosition = currentPhase.Value; // convert engine cycle angle into trigger cycle angle currentPosition -= tdcPosition(); // https://github.com/rusefi/rusefi/issues/1713 currentPosition could be negative that's expected @@ -619,7 +627,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta int crankInternalIndex = triggerState.getTotalRevolutionCounter() % crankDivider; int triggerIndexForListeners = triggerState.getCurrentIndex() + (crankInternalIndex * getTriggerSize()); if (triggerIndexForListeners == 0) { - virtualZeroTimer.reset(timestamp); + m_virtualZeroTimer.reset(timestamp); } reportEventToWaveChart(signal, triggerIndexForListeners PASS_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 4b438217bf..431e4a1174 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -38,6 +38,8 @@ public: */ class TriggerCentral final : public trigger_central_s { public: + DECLARE_ENGINE_PTR; + TriggerCentral(); void init(DECLARE_ENGINE_PARAMETER_SIGNATURE); void handleShaftSignal(trigger_event_e signal, efitick_t timestamp DECLARE_ENGINE_PARAMETER_SUFFIX); @@ -45,6 +47,8 @@ public: void resetCounters(); void validateCamVvtCounters(); + expected getCurrentEnginePhase(efitick_t nowNt) const; + float getTimeSinceTriggerEvent(efitick_t nowNt) const { return m_lastEventTimer.getElapsedSeconds(nowNt); } @@ -72,8 +76,6 @@ public: // synchronization event position angle_t vvtPosition[BANKS_COUNT][CAMS_PER_BANK]; - Timer virtualZeroTimer; - efitick_t vvtSyncTimeNt[BANKS_COUNT][CAMS_PER_BANK]; TriggerStateWithRunningStatistics triggerState; @@ -84,8 +86,12 @@ public: TriggerFormDetails triggerFormDetails; +private: // Keep track of the last time we got a valid trigger event Timer m_lastEventTimer; + + // Keep track of the last time we saw the sync tooth go by (trigger index 0) + Timer m_virtualZeroTimer; }; void triggerInfo(void);