diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index 10019b7bdd..5747b6936c 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -37,6 +37,10 @@ void contextSwitchHook() {} #endif /* ENABLE_PERF_TRACE */ #if EFI_ENABLE_MOCK_ADC +/** + * in 2021 the api is Sensor::setMockValue + * todo: remove this mockAdcState https://github.com/rusefi/rusefi/issues/3672 + */ void setMockVoltage(int hwChannel, float voltage) { engine->engineState.mockAdcState.setMockVoltage(hwChannel, voltage); } diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 90f252f84e..36161907cc 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -105,6 +105,7 @@ static bool vvtWithRealDecoder(vvt_mode_e vvtMode) { return vvtMode != VVT_INACTIVE && vvtMode != VVT_2JZ && vvtMode != VVT_HONDA_K + && vvtMode != VVT_MAP_V_TWIN_ANOTHER && && vvtMode != VVT_SECOND_HALF && vvtMode != VVT_FIRST_HALF; } @@ -143,6 +144,7 @@ static angle_t adjustCrankPhase(int camIndex) { switch (engineConfiguration->vvtMode[camIndex]) { case VVT_FIRST_HALF: + case VVT_MAP_V_TWIN_ANOTHER: return syncAndReport(tc, getCrankDivider(operationMode), 1); case VVT_SECOND_HALF: return syncAndReport(tc, getCrankDivider(operationMode), 0); @@ -662,11 +664,43 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta mainTriggerCallback(triggerIndexForListeners, timestamp); -#if EFI_TUNER_STUDIO auto toothAngle = engine->triggerCentral.triggerFormDetails.eventAngles[triggerIndexForListeners] - tdcPosition(); wrapAngle(toothAngle, "currentEnginePhase", CUSTOM_ERR_6555); +#if EFI_TUNER_STUDIO tsOutputChannels.currentEnginePhase = toothAngle; -#endif +#endif // EFI_TUNER_STUDIO + + if (engineConfiguration->vvtMode[0] == VVT_MAP_V_TWIN_ANOTHER) { + + if (mapCamPrevToothAngle < engineConfiguration->mapCamDetectionAnglePosition && toothAngle > engineConfiguration->mapCamDetectionAnglePosition) { + // we are somewhere close to 'mapCamDetectionAnglePosition' + + float map = Sensor::getOrZero(SensorType::Map); + + if (map > mapCamPrevCycleValue) { + mapCamCounter++; + + efitick_t stamp = getTimeNowNt(); + hwHandleVvtCamSignal(TV_RISE, stamp, /*index*/0); + hwHandleVvtCamSignal(TV_FALL, stamp, /*index*/0); + } +#if EFI_TUNER_STUDIO + tsOutputChannels.TEMPLOG_MAP_INSTANT_AVERAGE = map; + tsOutputChannels.TEMPLOG_map_peak = mapCamCounter; + tsOutputChannels.TEMPLOG_MAP_AT_DIFF = map - mapCamPrevCycleValue; +#endif // EFI_TUNER_STUDIO + + mapCamPrevCycleValue = map; + + + + } + + mapCamPrevToothAngle = toothAngle; + + } + + } } diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index f54da3151f..31805894ca 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -73,14 +73,20 @@ public: void resetCounters(); void validateCamVvtCounters(); + LocalVersionHolder triggerVersion; + MapState mapState; + + angle_t mapCamPrevToothAngle = -1; + float mapCamPrevCycleValue = 0; + uint8_t mapCamCounter = 0; + /** * true if a recent configuration change has changed any of the trigger settings which * we have not adjusted for yet */ bool triggerConfigChanged = false; - LocalVersionHolder triggerVersion; bool checkIfTriggerConfigChanged(); bool isTriggerConfigChanged(); diff --git a/unit_tests/tests/trigger/test_map_cam.cpp b/unit_tests/tests/trigger/test_map_cam.cpp index 15d3e9c4d8..0b38a46303 100644 --- a/unit_tests/tests/trigger/test_map_cam.cpp +++ b/unit_tests/tests/trigger/test_map_cam.cpp @@ -69,3 +69,26 @@ TEST(trigger, map_cam) { ASSERT_TRUE(state.isPeak(false)) << "high At " << i; ASSERT_TRUE(state.isPeak(false)) << "low At " << i; } + +TEST(trigger, map_cam_by_magic_point) { + + EngineTestHelper eth(TEST_ENGINE); + + engineConfiguration->camInputs[0] = GPIOA_0; + engineConfiguration->vvtMode[0] = VVT_MAP_V_TWIN_ANOTHER; + + Sensor::setMockValue(SensorType::Map, 100); + + engineConfiguration->mapCamDetectionAnglePosition = 90; + + eth.smartFireTriggerEvents2(/*count*/10, /*delayMs*/200); + ASSERT_EQ( 150, GET_RPM()) << "RPM"; + ASSERT_EQ(1, engine->triggerCentral.mapCamCounter); + + + Sensor::setMockValue(SensorType::Map, 120); + eth.smartFireTriggerEvents2(/*count*/2, /*delayMs*/200); + ASSERT_EQ(2, engine->triggerCentral.mapCamCounter); + + +}