From e3e5e57eac5844154d9dc151b20e3aaba2896a1a Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Fri, 30 Jul 2021 05:18:24 -0700 Subject: [PATCH] instant rpm uses full engine cycle (#3077) * instant rpm uses full engine cycle * turn off fast spinup for this test --- .../engine_cycle/rpm_calculator.cpp | 2 +- .../controllers/trigger/trigger_decoder.cpp | 25 ++++++++++--------- .../controllers/trigger/trigger_decoder.h | 4 +-- .../tests/trigger/test_cam_vvt_input.cpp | 1 + 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index 66294b399a..b4cdd45b3d 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -291,7 +291,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, #endif /* EFI_SENSOR_CHART */ // Always update instant RPM even when not spinning up - engine->triggerCentral.triggerState.updateInstantRpm(&engine->triggerCentral.triggerFormDetails, nowNt PASS_ENGINE_PARAMETER_SUFFIX); + engine->triggerCentral.triggerState.updateInstantRpm(&engine->triggerCentral.triggerFormDetails, index, nowNt PASS_ENGINE_PARAMETER_SUFFIX); if (rpmState->isSpinningUp()) { float instantRpm = engine->triggerCentral.triggerState.getInstantRpm(); diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index e05afd6e5b..cd07de5da7 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -242,21 +242,21 @@ void TriggerStateWithRunningStatistics::movePreSynchTimestamps(DECLARE_ENGINE_PA memcpy(timeOfLastEvent + firstDst, spinningEvents + firstSrc, eventsToCopy * sizeof(timeOfLastEvent[0])); } -float TriggerStateWithRunningStatistics::calculateInstantRpm(TriggerFormDetails *triggerFormDetails, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { - int current_index = currentCycle.current_index; // local copy so that noone changes the value on us - assertIsInBoundsWithResult(current_index, timeOfLastEvent, "calc timeOfLastEvent", 0); +float TriggerStateWithRunningStatistics::calculateInstantRpm(TriggerFormDetails *triggerFormDetails, uint32_t current_index, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { + assertIsInBoundsWithResult((int)current_index, timeOfLastEvent, "calc timeOfLastEvent", 0); + + // Record the time of this event so we can calculate RPM from it later timeOfLastEvent[current_index] = nowNt; - /** - * Here we calculate RPM based on last 90 degrees - */ + + // Determine where we currently are in the revolution angle_t currentAngle = triggerFormDetails->eventAngles[current_index]; - // todo: make this '90' depend on cylinder count or trigger shape? if (cisnan(currentAngle)) { return NOISY_RPM; } + + // Hunt for a tooth ~90 degrees ago to compare to the current time angle_t previousAngle = currentAngle - 90; fixAngle(previousAngle, "prevAngle", CUSTOM_ERR_TRIGGER_ANGLE_RANGE); - // todo: prevIndex should be pre-calculated int prevIndex = triggerFormDetails->triggerIndexByAngle[(int)previousAngle]; // now let's get precise angle for that event @@ -269,7 +269,8 @@ float TriggerStateWithRunningStatistics::calculateInstantRpm(TriggerFormDetails // OK for small time differences like this one uint32_t time = nowNt - time90ago; angle_t angleDiff = currentAngle - prevIndexAngle; - // todo: angle diff should be pre-calculated + + // Wrap the angle in to the correct range (ie, could be -630 when we want +90) fixAngle(angleDiff, "angleDiff", CUSTOM_ERR_6561); // just for safety @@ -277,7 +278,7 @@ float TriggerStateWithRunningStatistics::calculateInstantRpm(TriggerFormDetails return prevInstantRpmValue; float instantRpm = (60000000.0 / 360 * US_TO_NT_MULTIPLIER) * angleDiff / time; - assertIsInBoundsWithResult(current_index, instantRpmValue, "instantRpmValue", 0); + assertIsInBoundsWithResult((int)current_index, instantRpmValue, "instantRpmValue", 0); instantRpmValue[current_index] = instantRpm; // This fixes early RPM instability based on incomplete data @@ -306,8 +307,8 @@ void TriggerStateWithRunningStatistics::setLastEventTimeForInstantRpm(efitick_t spinningEvents[spinningEventIndex++] = nowNt; } -void TriggerStateWithRunningStatistics::updateInstantRpm(TriggerFormDetails *triggerFormDetails, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { - m_instantRpm = calculateInstantRpm(triggerFormDetails, nowNt PASS_ENGINE_PARAMETER_SUFFIX); +void TriggerStateWithRunningStatistics::updateInstantRpm(TriggerFormDetails *triggerFormDetails, uint32_t index, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { + m_instantRpm = calculateInstantRpm(triggerFormDetails, index, nowNt PASS_ENGINE_PARAMETER_SUFFIX); #if EFI_SENSOR_CHART diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index 18f205f215..a697d6b900 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -202,7 +202,7 @@ public: void movePreSynchTimestamps(DECLARE_ENGINE_PARAMETER_SIGNATURE); #if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT - void updateInstantRpm(TriggerFormDetails *triggerFormDetails, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); + void updateInstantRpm(TriggerFormDetails *triggerFormDetails, uint32_t index, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); #endif /** * Update timeOfLastEvent[] on every trigger event - even without synchronization @@ -211,7 +211,7 @@ public: void setLastEventTimeForInstantRpm(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); private: - float calculateInstantRpm(TriggerFormDetails *triggerFormDetails, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); + float calculateInstantRpm(TriggerFormDetails *triggerFormDetails, uint32_t index, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX); float m_instantRpm = 0; float m_instantRpmRatio = 0; diff --git a/unit_tests/tests/trigger/test_cam_vvt_input.cpp b/unit_tests/tests/trigger/test_cam_vvt_input.cpp index 6f4a27096e..40dc9008f6 100644 --- a/unit_tests/tests/trigger/test_cam_vvt_input.cpp +++ b/unit_tests/tests/trigger/test_cam_vvt_input.cpp @@ -125,6 +125,7 @@ TEST(trigger, testNB2CamInput) { // this crank trigger would be easier to test, crank shape is less important for this test eth.setTriggerType(TT_ONE PASS_ENGINE_PARAMETER_SUFFIX); + engineConfiguration->isFasterEngineSpinUpEnabled = false; engineConfiguration->useOnlyRisingEdgeForTrigger = true;