diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 887a2e3ce3..6036aa9b44 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -129,8 +129,7 @@ void TriggerState::reset() { triggerCycleCallback = NULL; shaft_is_synchronized = false; toothed_previous_time = 0; - toothed_previous_duration = 0; - durationBeforePrevious = 0; + memset(toothDurations, 0, sizeof(toothDurations)); totalRevolutionCounter = 0; @@ -238,12 +237,12 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no // todo: skip a number of signal from the beginning #if EFI_PROD_CODE || defined(__DOXYGEN__) -// scheduleMsg(&logger, "from %.2f to %.2f %d %d", triggerConfig->syncRatioFrom, triggerConfig->syncRatioTo, currentDuration, shaftPositionState->toothed_previous_duration); -// scheduleMsg(&logger, "ratio %.2f", 1.0 * currentDuration/ shaftPositionState->toothed_previous_duration); +// scheduleMsg(&logger, "from %.2f to %.2f %d %d", triggerConfig->syncRatioFrom, triggerConfig->syncRatioTo, currentDuration, shaftPositionState->toothDurations[0]); +// scheduleMsg(&logger, "ratio %.2f", 1.0 * currentDuration/ shaftPositionState->toothDurations[0]); #else if (printTriggerDebug) { - printf("ratio %.2f: current=%d previous=%d\r\n", 1.0 * currentDuration / toothed_previous_duration, - currentDuration, toothed_previous_duration); + printf("ratio %.2f: current=%d previous=%d\r\n", 1.0 * currentDuration / toothDurations[0], + currentDuration, toothDurations[0]); } #endif @@ -253,21 +252,21 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no // this is getting a little out of hand, any ideas? if (CONFIG(debugMode) == DBG_TRIGGER_SYNC) { - float currentGap = 1.0 * currentDuration / toothed_previous_duration; + float currentGap = 1.0 * currentDuration / toothDurations[0]; #if ! EFI_UNIT_TEST || defined(__DOXYGEN__) tsOutputChannels.debugFloatField1 = currentGap; tsOutputChannels.debugFloatField2 = currentCycle.current_index; #endif /* EFI_UNIT_TEST */ } - bool primaryGap = currentDuration > toothed_previous_duration * TRIGGER_SHAPE(syncronizationRatioFrom[0]) - && currentDuration < toothed_previous_duration * TRIGGER_SHAPE(syncronizationRatioTo[0]); + bool primaryGap = currentDuration > toothDurations[0] * TRIGGER_SHAPE(syncronizationRatioFrom[0]) + && currentDuration < toothDurations[0] * TRIGGER_SHAPE(syncronizationRatioTo[0]); - bool secondaryGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[1])) || (toothed_previous_duration > durationBeforePrevious * TRIGGER_SHAPE(syncronizationRatioFrom[1]) - && toothed_previous_duration < durationBeforePrevious * TRIGGER_SHAPE(syncronizationRatioTo[1])); + bool secondaryGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[1])) || (toothDurations[0] > toothDurations[1] * TRIGGER_SHAPE(syncronizationRatioFrom[1]) + && toothDurations[0] < toothDurations[1] * TRIGGER_SHAPE(syncronizationRatioTo[1])); - bool thirdGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[2])) || (durationBeforePrevious > toothDurations[2] * TRIGGER_SHAPE(syncronizationRatioFrom[2]) - && durationBeforePrevious < toothDurations[2] * TRIGGER_SHAPE(syncronizationRatioTo[2])); + bool thirdGap = cisnan(TRIGGER_SHAPE(syncronizationRatioFrom[2])) || (toothDurations[1] > toothDurations[2] * TRIGGER_SHAPE(syncronizationRatioFrom[2]) + && toothDurations[1] < toothDurations[2] * TRIGGER_SHAPE(syncronizationRatioTo[2])); /** * Here I prefer to have two multiplications instead of one division, that's a micro-optimization @@ -281,9 +280,9 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no #else if (printTriggerDebug) { #endif /* EFI_PROD_CODE */ - float gap = 1.0 * currentDuration / toothed_previous_duration; - float prevGap = 1.0 * toothed_previous_duration / durationBeforePrevious; - float gap3 = 1.0 * durationBeforePrevious / toothDurations[2]; + float gap = 1.0 * currentDuration / toothDurations[0]; + float prevGap = 1.0 * toothDurations[0] / toothDurations[1]; + float gap3 = 1.0 * toothDurations[1] / toothDurations[2]; #if EFI_PROD_CODE || defined(__DOXYGEN__) scheduleMsg(logger, "%d: cur=%.2f/prev=%.2f/3rd=%.2f @ %d while expected from %.2f to %.2f and 2nd from %.2f to %.2f and 3rd from %.2f to %.2f error=%d", getTimeNowSeconds(), @@ -300,7 +299,7 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no #else actualSynchGap = gap; - print("current gap %.2f/%.2f/%.2f c=%d prev=%d\r\n", gap, prevGap, gap3, currentDuration, toothed_previous_duration); + print("current gap %.2f/%.2f/%.2f c=%d prev=%d\r\n", gap, prevGap, gap3, currentDuration, toothDurations[0]); #endif /* EFI_PROD_CODE */ } @@ -424,9 +423,9 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no ; } - toothDurations[2] = durationBeforePrevious; - durationBeforePrevious = toothed_previous_duration; - toothed_previous_duration = currentDuration; + toothDurations[2] = toothDurations[1]; + toothDurations[1] = toothDurations[0]; + toothDurations[0] = currentDuration; toothed_previous_time = nowNt; } if (!isValidIndex(PASS_ENGINE_PARAMETER_SIGNATURE) && !isInitializingTrigger) { diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index f17af99fdb..5656b49b74 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -76,9 +76,6 @@ public: uint32_t toothDurations[GAP_TRACKING_LENGHT]; - uint32_t toothed_previous_duration; // todo: unify Camel_notation, what a mess :( - uint32_t durationBeforePrevious; // this one is before 'toothed_previous_duration' - /** * this could be a local variable, but it's better for debugging to have it as a field */