From aabc50e7a919499498695ae8f64b77986de51e61 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 24 Jan 2020 13:42:09 -0500 Subject: [PATCH] refactoring trigger --- .../controllers/trigger/trigger_central.cpp | 2 +- .../controllers/trigger/trigger_decoder.cpp | 29 ++++++++++--------- .../controllers/trigger/trigger_decoder.h | 4 +-- firmware/hw_layer/trigger_input_comp.cpp | 2 +- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 2bcd91187d..e7422b2836 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -381,7 +381,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta return; } - if (triggerState.isValidIndex(PASS_ENGINE_PARAMETER_SIGNATURE)) { + if (triggerState.isValidIndex(&ENGINE(triggerCentral.triggerShape))) { ScopePerf perf(PE::ShaftPositionListeners); #if TRIGGER_EXTREME_LOGGING diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 623343f9b7..41f9f73c45 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -263,8 +263,8 @@ void TriggerStateWithRunningStatistics::runtimeStatistics(efitick_t nowNt DECLAR } } -bool TriggerState::isValidIndex(DECLARE_ENGINE_PARAMETER_SIGNATURE) const { - return currentCycle.current_index < getTriggerSize(); +bool TriggerState::isValidIndex(TriggerWaveform *triggerShape) const { + return currentCycle.current_index < triggerShape->getSize(); } static trigger_wheel_e eventIndex[6] = { T_PRIMARY, T_PRIMARY, T_SECONDARY, T_SECONDARY, T_CHANNEL_3, T_CHANNEL_3 }; @@ -379,11 +379,8 @@ void TriggerState::handleTriggerError(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycleCallback, - efitick_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX) { - setShaftSynchronized(true); - // this call would update duty cycle values - nextTriggerEvent() - ; + efitick_t nowNt, trigger_wheel_e triggerWheel, TriggerWaveform *triggerShape) { + if (triggerCycleCallback != NULL) { triggerCycleCallback(this); @@ -392,7 +389,7 @@ void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycl startOfCycleNt = nowNt; resetCurrentCycleState(); incrementTotalEventCounter(); - totalEventCountBase += getTriggerSize(); + totalEventCountBase += triggerShape->getSize(); #if EFI_UNIT_TEST if (printTriggerDebug) { @@ -607,10 +604,10 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal printf("sync=%d index=%d size=%d\r\n", shaft_is_synchronized, currentCycle.current_index, - getTriggerSize()); + triggerShape->getSize()); } #endif /* EFI_UNIT_TEST */ - unsigned int endOfCycleIndex = getTriggerSize() - (CONFIG(useOnlyRisingEdgeForTrigger) ? 2 : 1); + unsigned int endOfCycleIndex = triggerShape->getSize() - (CONFIG(useOnlyRisingEdgeForTrigger) ? 2 : 1); isSynchronizationPoint = !shaft_is_synchronized || (currentCycle.current_index >= endOfCycleIndex); @@ -620,7 +617,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal printf("isSynchronizationPoint=%d index=%d size=%d\r\n", isSynchronizationPoint, currentCycle.current_index, - getTriggerSize()); + triggerShape->getSize()); } #endif /* EFI_UNIT_TEST */ @@ -641,8 +638,12 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal triggerStateListener->OnTriggerSyncronization(wasSynchronized); } + setShaftSynchronized(true); + // this call would update duty cycle values + nextTriggerEvent() + ; - onShaftSynchronization(triggerCycleCallback, nowNt, triggerWheel PASS_ENGINE_PARAMETER_SUFFIX); + onShaftSynchronization(triggerCycleCallback, nowNt, triggerWheel, triggerShape); } else { /* if (!isSynchronizationPoint) */ nextTriggerEvent() @@ -655,10 +656,10 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal toothed_previous_time = nowNt; } - if (!isValidIndex(PASS_ENGINE_PARAMETER_SIGNATURE) && triggerStateListener) { + if (!isValidIndex(triggerShape) && triggerStateListener) { // let's not show a warning if we are just starting to spin if (GET_RPM_VALUE != 0) { - warning(CUSTOM_SYNC_ERROR, "sync error: index #%d above total size %d", currentCycle.current_index, getTriggerSize()); + warning(CUSTOM_SYNC_ERROR, "sync error: index #%d above total size %d", currentCycle.current_index, triggerShape->getSize()); lastDecodingErrorTime = getTimeNowNt(); someSortOfTriggerError = true; } diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index 4c5612db22..7dd20efd79 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -73,13 +73,13 @@ public: bool validateEventCounters(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; void handleTriggerError(DECLARE_ENGINE_PARAMETER_SIGNATURE); void onShaftSynchronization(const TriggerStateCallback triggerCycleCallback, - efitick_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX); + 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(DECLARE_ENGINE_PARAMETER_SIGNATURE) const; + bool isValidIndex(TriggerWaveform *triggerShape) const; float getTriggerDutyCycle(int index); /** diff --git a/firmware/hw_layer/trigger_input_comp.cpp b/firmware/hw_layer/trigger_input_comp.cpp index e3d345c033..1158a4eaaa 100644 --- a/firmware/hw_layer/trigger_input_comp.cpp +++ b/firmware/hw_layer/trigger_input_comp.cpp @@ -129,7 +129,7 @@ void startTriggerInputPins(void) { // 20 rpm (60_2) = 1000*60/((2*60)*20) = 25 ms for 1 tooth event float satRpm = CONFIG(triggerCompSensorSatRpm) * RPM_1_BYTE_PACKING_MULT; - hystUpdatePeriodNumEvents = getTriggerSize(); // = 116 for "60-2" trigger wheel + hystUpdatePeriodNumEvents = ENGINE(triggerCentral.triggerShape).getSize(); // = 116 for "60-2" trigger wheel float saturatedToothDurationUs = 60.0f * US_PER_SECOND_F / satRpm / hystUpdatePeriodNumEvents; saturatedVrFreqNt = 1.0f / US2NT(saturatedToothDurationUs);