From ac7c2290ca39834e87c3162949a1ebd4d1b1a35e Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 3 Dec 2017 23:58:48 -0500 Subject: [PATCH] #58 --- firmware/controllers/trigger/rpm_calculator.h | 9 +++- .../controllers/trigger/trigger_decoder.h | 7 +++ .../controllers/trigger/trigger_structure.cpp | 48 ++++++++++++------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/firmware/controllers/trigger/rpm_calculator.h b/firmware/controllers/trigger/rpm_calculator.h index efdab6cb31..cf9c6d29c2 100644 --- a/firmware/controllers/trigger/rpm_calculator.h +++ b/firmware/controllers/trigger/rpm_calculator.h @@ -79,15 +79,22 @@ public: uint32_t getRevolutionCounter(void); void setRpmValue(int value DECLARE_ENGINE_PARAMETER_SUFFIX); uint32_t getRevolutionCounterSinceStart(void); + /** + * RPM rate of change between current RPM and RPM measured during previous engine cycle + * see also SC_RPM_ACCEL + */ float getRpmAcceleration(); /** * This is public because sometimes we cannot afford to call isRunning() and the value is good enough * Zero if engine is not running */ volatile int rpmValue; + /** + * this is RPM on previous engine cycle. + */ int previousRpmValue; /** - * This is a performance optimization: let's pre-calulate this each time RPM changes + * This is a performance optimization: let's pre-calculate this each time RPM changes * NaN while engine is not spinning */ volatile floatus_t oneDegreeUs; diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index ca81ae856d..7868f309ed 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -118,8 +118,15 @@ private: */ class TriggerStateWithRunningStatistics : public TriggerState { public: + /** + * timestamp of each trigger wheel tooth + */ uint32_t timeOfLastEvent[PWM_PHASE_MAX_COUNT]; + /** + * instant RPM calculated at this trigger wheel tooth + */ float instantRpmValue[PWM_PHASE_MAX_COUNT]; + float calculateInstantRpm(int *prevIndex, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); virtual void runtimeStatistics(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); }; diff --git a/firmware/controllers/trigger/trigger_structure.cpp b/firmware/controllers/trigger/trigger_structure.cpp index cda678d8ed..149ee730f2 100644 --- a/firmware/controllers/trigger/trigger_structure.cpp +++ b/firmware/controllers/trigger/trigger_structure.cpp @@ -212,32 +212,44 @@ void TriggerState::runtimeStatistics(trigger_event_e const signal, efitime_t now // empty base implementation } +float TriggerStateWithRunningStatistics::calculateInstantRpm(int *prevIndex, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { + /** + * Here we calculate RPM based on last 90 degrees + */ + angle_t currentAngle = TRIGGER_SHAPE(eventAngles[currentCycle.current_index]); + // todo: make this '90' depend on cylinder count? + angle_t previousAngle = currentAngle - 90; + fixAngle(previousAngle, "prevAngle"); + // todo: prevIndex should be pre-calculated + *prevIndex = TRIGGER_SHAPE(triggerIndexByAngle[(int)previousAngle]); + + // now let's get precise angle for that event + angle_t prevIndexAngle = TRIGGER_SHAPE(eventAngles[*prevIndex]); + uint32_t time = nowNt - timeOfLastEvent[*prevIndex]; + angle_t angleDiff = currentAngle - prevIndexAngle; + // todo: angle diff should be pre-calculated + fixAngle(angleDiff, "angleDiff"); + + float instantRpm = (60000000.0 / 360 * US_TO_NT_MULTIPLIER) * angleDiff / time; + instantRpmValue[currentCycle.current_index] = instantRpm; + timeOfLastEvent[currentCycle.current_index] = nowNt; + + return instantRpm; +} + 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; + int prevIndex; + float instantRpm = calculateInstantRpm(&prevIndex, nowNt PASS_ENGINE_PARAMETER_SUFFIX); #if EFI_SENSOR_CHART || defined(__DOXYGEN__) + angle_t currentAngle = TRIGGER_SHAPE(eventAngles[currentCycle.current_index]); if (boardConfiguration->sensorChartMode == SC_DETAILED_RPM) { - scAddData(currentAngle, r); + scAddData(currentAngle, instantRpm); } else { - scAddData(currentAngle, r / instantRpmValue[prevIndex]); + scAddData(currentAngle, instantRpm / instantRpmValue[prevIndex]); } #endif /* EFI_SENSOR_CHART */ - instantRpmValue[currentCycle.current_index] = r; - timeOfLastEvent[currentCycle.current_index] = nowNt; } }