diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index f23d79f902..7b90f90729 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -398,35 +398,8 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no } } - runtimeStatistics(); + runtimeStatistics(signal, nowNt PASS_ENGINE_PARAMETER_SUFFIX); - if (ENGINE(sensorChartMode) == SC_RPM_ACCEL || ENGINE(sensorChartMode) == SC_DETAILED_RPM) { - angle_t currentAngle = TRIGGER_SHAPE(eventAngles[currentCycle.current_index]); - // todo: make this '90' depend on cylinder count? - angle_t prevAngle = currentAngle - 90; - fixAngle(prevAngle, "prevAngle"); - // todo: prevIndex should be pre-calculated - int prevIndex = TRIGGER_SHAPE(triggerIndexByAngle[(int)prevAngle]); - // now let's get precise angle for that event - prevAngle = TRIGGER_SHAPE(eventAngles[prevIndex]); -// todo: re-implement this as a subclass. we need two instances of -// uint32_t time = nowNt - timeOfLastEvent[prevIndex]; - angle_t angleDiff = currentAngle - prevAngle; - // todo: angle diff should be pre-calculated - fixAngle(angleDiff, "angleDiff"); - -// float r = (60000000.0 / 360 * US_TO_NT_MULTIPLIER) * angleDiff / time; - -#if EFI_SENSOR_CHART || defined(__DOXYGEN__) - if (boardConfiguration->sensorChartMode == SC_DETAILED_RPM) { -// scAddData(currentAngle, r); - } else { -// scAddData(currentAngle, r / instantRpmValue[prevIndex]); - } -#endif -// instantRpmValue[currentCycle.current_index] = r; -// timeOfLastEvent[currentCycle.current_index] = nowNt; - } } void configure3_1_cam(TriggerShape *s, operation_mode_e operationMode DECLARE_ENGINE_PARAMETER_SUFFIX) { diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index 969c587d97..ca81ae856d 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -93,7 +93,7 @@ public: void reset(); void resetRunningCounters(); - virtual void runtimeStatistics(); + virtual void runtimeStatistics(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); uint32_t runningRevolutionCounter; /** @@ -112,11 +112,15 @@ private: efitime_t prevCycleDuration; }; + +/** + * the reason for sub-class is simply to save RAM but not having statisics in the trigger initialization instance + */ class TriggerStateWithRunningStatistics : public TriggerState { public: uint32_t timeOfLastEvent[PWM_PHASE_MAX_COUNT]; float instantRpmValue[PWM_PHASE_MAX_COUNT]; - virtual void runtimeStatistics(); + virtual void runtimeStatistics(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); }; angle_t getEngineCycle(operation_mode_e operationMode); diff --git a/firmware/controllers/trigger/trigger_structure.cpp b/firmware/controllers/trigger/trigger_structure.cpp index 2185be4676..81a85c5e7f 100644 --- a/firmware/controllers/trigger/trigger_structure.cpp +++ b/firmware/controllers/trigger/trigger_structure.cpp @@ -24,6 +24,7 @@ #include "trigger_decoder.h" #include "engine_math.h" #include "trigger_universal.h" +#include "sensor_chart.h" EXTERN_ENGINE; @@ -205,12 +206,37 @@ void TriggerState::resetRunningCounters() { runningOrderingErrorCounter = 0; } -void TriggerState::runtimeStatistics() { +void TriggerState::runtimeStatistics(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { // empty base implementation } -void TriggerStateWithRunningStatistics::runtimeStatistics() { - // empty base implementation +void TriggerStateWithRunningStatistics::runtimeStatistics(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { + if (ENGINE(sensorChartMode) == SC_RPM_ACCEL || ENGINE(sensorChartMode) == SC_DETAILED_RPM) { + angle_t currentAngle = TRIGGER_SHAPE(eventAngles[currentCycle.current_index]); + // todo: make this '90' depend on cylinder count? + angle_t prevAngle = currentAngle - 90; + fixAngle(prevAngle, "prevAngle"); + // todo: prevIndex should be pre-calculated + int prevIndex = TRIGGER_SHAPE(triggerIndexByAngle[(int)prevAngle]); + // now let's get precise angle for that event + prevAngle = TRIGGER_SHAPE(eventAngles[prevIndex]); + uint32_t time = nowNt - timeOfLastEvent[prevIndex]; + angle_t angleDiff = currentAngle - prevAngle; + // todo: angle diff should be pre-calculated + fixAngle(angleDiff, "angleDiff"); + + float r = (60000000.0 / 360 * US_TO_NT_MULTIPLIER) * angleDiff / time; + +#if EFI_SENSOR_CHART || defined(__DOXYGEN__) + if (boardConfiguration->sensorChartMode == SC_DETAILED_RPM) { + scAddData(currentAngle, r); + } else { + scAddData(currentAngle, r / instantRpmValue[prevIndex]); + } +#endif + instantRpmValue[currentCycle.current_index] = r; + timeOfLastEvent[currentCycle.current_index] = nowNt; + } } efitime_t TriggerState::getTotalEventCounter() {