From c2be975f622967ffb185a01415fdf8fd52ada893 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 21 Oct 2018 09:31:58 -0400 Subject: [PATCH] refctoring trigger sync layer --- firmware/controllers/engine_controller.cpp | 2 +- .../controllers/trigger/trigger_decoder.cpp | 12 +++++----- .../controllers/trigger/trigger_structure.cpp | 16 +++++++------- .../controllers/trigger/trigger_structure.h | 22 +++++++++++-------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 45c8cef25f..003c6e561c 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -733,5 +733,5 @@ int getRusEfiVersion(void) { if (initBootloader() != 0) return 123; #endif /* EFI_BOOTLOADER_INCLUDE_CODE */ - return 20180924; + return 20181021; } diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index b0996055bf..fc874665f0 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -263,11 +263,11 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no bool primaryGap = currentDuration > toothed_previous_duration * TRIGGER_SHAPE(syncRatioFrom) && currentDuration < toothed_previous_duration * TRIGGER_SHAPE(syncRatioTo); - bool secondaryGap = cisnan(TRIGGER_SHAPE(secondSyncRatioFrom)) || (toothed_previous_duration > durationBeforePrevious * TRIGGER_SHAPE(secondSyncRatioFrom) - && toothed_previous_duration < durationBeforePrevious * TRIGGER_SHAPE(secondSyncRatioTo)); + bool secondaryGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[1])) || (toothed_previous_duration > durationBeforePrevious * TRIGGER_SHAPE(syncronizationRatioFrom[1]) + && toothed_previous_duration < durationBeforePrevious * TRIGGER_SHAPE(syncronizationRatioTo[1])); - bool thirdGap = cisnan(TRIGGER_SHAPE(thirdSyncRatioFrom)) || (durationBeforePrevious > thirdPreviousDuration * TRIGGER_SHAPE(thirdSyncRatioFrom) - && durationBeforePrevious < thirdPreviousDuration * TRIGGER_SHAPE(thirdSyncRatioTo)); + bool thirdGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[2])) || (durationBeforePrevious > thirdPreviousDuration * TRIGGER_SHAPE(syncronizationRatioFrom[2]) + && durationBeforePrevious < thirdPreviousDuration * TRIGGER_SHAPE(syncronizationRatioTo[2])); /** * Here I prefer to have two multiplications instead of one division, that's a micro-optimization @@ -290,8 +290,8 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no gap, prevGap, gap3, currentCycle.current_index, TRIGGER_SHAPE(syncRatioFrom), TRIGGER_SHAPE(syncRatioTo), - TRIGGER_SHAPE(secondSyncRatioFrom), TRIGGER_SHAPE(secondSyncRatioTo), - TRIGGER_SHAPE(thirdSyncRatioFrom), TRIGGER_SHAPE(thirdSyncRatioTo), + TRIGGER_SHAPE(syncronizationRatioFrom[1]), TRIGGER_SHAPE(syncronizationRatioTo[1]), + TRIGGER_SHAPE(syncronizationRatioFrom[2]), TRIGGER_SHAPE(syncronizationRatioTo[2]), someSortOfTriggerError); #else actualSynchGap = gap; diff --git a/firmware/controllers/trigger/trigger_structure.cpp b/firmware/controllers/trigger/trigger_structure.cpp index af4076c625..eba97f48fe 100644 --- a/firmware/controllers/trigger/trigger_structure.cpp +++ b/firmware/controllers/trigger/trigger_structure.cpp @@ -105,10 +105,10 @@ void TriggerShape::initialize(operation_mode_e operationMode, bool needSecondTri // memset(triggerIndexByAngle, 0, sizeof(triggerIndexByAngle)); setTriggerSynchronizationGap(2); - secondSyncRatioFrom = NAN; // NaN means do not use this ratio - secondSyncRatioTo = 100000; - thirdSyncRatioFrom = NAN; // NaN means do not use this ratio - thirdSyncRatioTo = 100000; + syncronizationRatioFrom[1] = NAN; // NaN means do not use this ratio + syncronizationRatioTo[1] = 100000; + syncronizationRatioFrom[2] = NAN; // NaN means do not use this ratio + syncronizationRatioTo[2] = 100000; tdcPosition = 0; @@ -457,8 +457,8 @@ void TriggerShape::setTriggerSynchronizationGap(float syncRatio) { void TriggerShape::setSecondTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo) { isSynchronizationNeeded = true; - this->secondSyncRatioFrom = syncRatioFrom; - this->secondSyncRatioTo = syncRatioTo; + this->syncronizationRatioFrom[1] = syncRatioFrom; + this->syncronizationRatioTo[1] = syncRatioTo; } void TriggerShape::setThirdTriggerSynchronizationGap(float syncRatio) { @@ -467,8 +467,8 @@ void TriggerShape::setThirdTriggerSynchronizationGap(float syncRatio) { void TriggerShape::setThirdTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo) { isSynchronizationNeeded = true; - this->thirdSyncRatioFrom = syncRatioFrom; - this->thirdSyncRatioTo = syncRatioTo; + this->syncronizationRatioFrom[2] = syncRatioFrom; + this->syncronizationRatioTo[2] = syncRatioTo; } void TriggerShape::setSecondTriggerSynchronizationGap(float syncRatio) { diff --git a/firmware/controllers/trigger/trigger_structure.h b/firmware/controllers/trigger/trigger_structure.h index d68c8e4888..73f9854074 100644 --- a/firmware/controllers/trigger/trigger_structure.h +++ b/firmware/controllers/trigger/trigger_structure.h @@ -45,6 +45,8 @@ private: class Engine; class TriggerState; +#define GAP_TRACKING_LENGHT 3 + /** * @brief Trigger shape has all the fields needed to describe and decode trigger signal. * @see TriggerState for trigger decoder state which works based on this trigger shape model @@ -89,6 +91,17 @@ public: */ int triggerIndexByAngle[720]; + + /** + * Depending on trigger shape, we use betweeb one and three previous gap ranges to detect synchronizaiton. + * + * Usually second or third gap is not needed, but some crazy triggers like 36-2-2-2 require two consecutive + * gaps ratios to sync + */ + + float syncronizationRatioFrom[GAP_TRACKING_LENGHT]; + float syncronizationRatioTo[GAP_TRACKING_LENGHT]; + float syncRatioFrom; float syncRatioTo; /** @@ -96,15 +109,6 @@ public: */ int syncRatioAvg; - /** - * Usually this is not needed, but some crazy triggers like 36-2-2-2 require two consecutive - * gaps ratios to sync - */ - float secondSyncRatioFrom; - float secondSyncRatioTo; - - float thirdSyncRatioFrom; - float thirdSyncRatioTo; /** * Trigger indexes within trigger cycle are counted from synchronization point, and all