diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index a117a95838..41004d31bb 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -175,8 +175,8 @@ void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index DECL tc->vvtCamCounter++; - efitick_t offsetNt = nowNt - tc->timeAtVirtualZeroNt; - angle_t currentPosition = NT2US(offsetNt) / oneDegreeUs; + float offsetUs = tc->virtualZeroTimer.getElapsedUs(nowNt); + angle_t currentPosition = offsetUs / oneDegreeUs; // convert engine cycle angle into trigger cycle angle currentPosition -= tdcPosition(); // https://github.com/rusefi/rusefi/issues/1713 currentPosition could be negative that's expected @@ -503,7 +503,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta triggerIndexForListeners = triggerState.getCurrentIndex() + (crankInternalIndex * getTriggerSize()); } if (triggerIndexForListeners == 0) { - timeAtVirtualZeroNt = timestamp; + 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 41bb3e7a6b..c0087df243 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -67,10 +67,7 @@ public: // synchronization event position angle_t vvtPosition[BANKS_COUNT][CAMS_PER_BANK]; - /** - * this is similar to TriggerState#startOfCycleNt but with the crank-only sensor magic - */ - efitick_t timeAtVirtualZeroNt = 0; + Timer virtualZeroTimer; efitick_t vvtSyncTimeNt[BANKS_COUNT][CAMS_PER_BANK]; diff --git a/firmware/util/timer.cpp b/firmware/util/timer.cpp index b8dc3ff480..5104fa0318 100644 --- a/firmware/util/timer.cpp +++ b/firmware/util/timer.cpp @@ -35,6 +35,10 @@ float Timer::getElapsedSeconds() const { } float Timer::getElapsedSeconds(efitick_t nowNt) const { + return 1e-6 * getElapsedUs(nowNt); +} + +float Timer::getElapsedUs(efitick_t nowNt) const { auto delta = nowNt - m_lastReset; // Yes, things can happen slightly in the future if we get a lucky interrupt between @@ -50,7 +54,7 @@ float Timer::getElapsedSeconds(efitick_t nowNt) const { auto delta32 = (uint32_t)delta; - return 1e-6 * NT2US(delta32); + return NT2US(delta32); } float Timer::getElapsedSecondsAndReset(efitick_t nowNt) { diff --git a/firmware/util/timer.h b/firmware/util/timer.h index 2c08639de1..6334f704f5 100644 --- a/firmware/util/timer.h +++ b/firmware/util/timer.h @@ -18,6 +18,7 @@ public: // then a time period representing 2^32 counts will be returned. float getElapsedSeconds() const; float getElapsedSeconds(efitick_t nowNt) const; + float getElapsedUs(efitick_t nowNt) const; // Perform an atomic update event based on the passed timestamp, // returning the delta between the last reset and the provided timestamp