From e2fa1af527912820e22b6a558f8563e31a3c2467 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 24 Nov 2021 22:36:36 -0500 Subject: [PATCH] MAP phase sensing #3544 --- firmware/controllers/algo/engine.cpp | 2 ++ .../engine_cycle/map_averaging.cpp | 13 +++++++++- .../controllers/trigger/trigger_central.h | 24 +++++++++++++++++++ unit_tests/tests/trigger/test_map_cam.cpp | 22 +---------------- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 14ceee8e9b..5bed1cd016 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -96,6 +96,8 @@ trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode) { return TT_VVT_NISSAN_VQ35; case VVT_NISSAN_MR: return TT_NISSAN_MR18_CAM_VVT; + case VVT_MAP_V_TWIN: + return TT_VVT_MAP_45_V_TWIN; default: firmwareError(OBD_PCM_Processor_Fault, "getVvtTriggerType for %s", getVvt_mode_e(vvtMode)); return TT_ONE; // we have to return something for the sake of -Werror=return-type diff --git a/firmware/controllers/engine_cycle/map_averaging.cpp b/firmware/controllers/engine_cycle/map_averaging.cpp index 8ce2222d7b..dfe8d02b06 100644 --- a/firmware/controllers/engine_cycle/map_averaging.cpp +++ b/firmware/controllers/engine_cycle/map_averaging.cpp @@ -94,7 +94,7 @@ static void startAveraging(scheduling_s *endAveragingScheduling) { #if HAL_USE_ADC /** * This method is invoked from ADC callback. - * @note This method is invoked OFTEN, this method is a potential bottle-next - the implementation should be + * @note This method is invoked OFTEN, this method is a potential bottleneck - the implementation should be * as fast as possible */ void mapAveragingAdcCallback(adcsample_t adcValue) { @@ -107,6 +107,17 @@ void mapAveragingAdcCallback(adcsample_t adcValue) { } #endif // EFI_TUNER_STUDIO + if (engineConfiguration->vvtMode[0] == VVT_MAP_V_TWIN) { + float voltage = adcToVoltsDivided(adcValue); + float instantMap = convertMap(voltage).value_or(0); + engine->triggerCentral.mapState.add(instantMap); + if (engine->triggerCentral.mapState.isPeak()) { + efitick_t stamp = getTimeNowNt(); + hwHandleVvtCamSignal(TV_RISE, stamp, /*index*/0); + hwHandleVvtCamSignal(TV_FALL, stamp, /*index*/0); + } + } + /* Calculates the average values from the ADC samples.*/ if (isAveraging) { // with locking we will have a consistent state diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 9cfeca2e63..9a5018cd85 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -15,6 +15,8 @@ #include "pin_repository.h" #include "local_version_holder.h" +#define MAP_CAM_BUFFER 8 + class Engine; typedef void (*ShaftPositionListener)(trigger_event_e signal, uint32_t index, efitick_t edgeTimestamp); @@ -32,6 +34,26 @@ public: efitick_t accumSignalPrevPeriods[HW_EVENT_TYPES]; }; +struct MapState { + float current, previous, prevPrevious; + cyclic_buffer mapBuffer; + + void add(float value) { + // rotate state + prevPrevious = previous; + previous = current; + + // add new value + mapBuffer.add(value); + current = mapBuffer.sum(MAP_CAM_BUFFER); + } + + bool isPeak() { + return previous > prevPrevious && previous >= current; + } +}; + + /** * Maybe merge TriggerCentral and TriggerState classes into one class? * Probably not: we have an instance of TriggerState which is used for trigger initialization, @@ -45,6 +67,8 @@ public: void resetCounters(); void validateCamVvtCounters(); + MapState mapState; + /** * true if a recent configuration change has changed any of the trigger settings which * we have not adjusted for yet diff --git a/unit_tests/tests/trigger/test_map_cam.cpp b/unit_tests/tests/trigger/test_map_cam.cpp index a2c538a3b3..e4ce363d82 100644 --- a/unit_tests/tests/trigger/test_map_cam.cpp +++ b/unit_tests/tests/trigger/test_map_cam.cpp @@ -1,8 +1,7 @@ #include "pch.h" - -#define MAP_CAM_BUFFER 8 +#include "trigger_central.h" static int getZigZag(int index) { index = index % 1000; @@ -23,25 +22,6 @@ static int getZigZag(int index) { return 1000 - index; } -struct MapState { - float current, previous, prevPrevious; - cyclic_buffer mapBuffer; - - void add(float value) { - // rotate state - prevPrevious = previous; - previous = current; - - // add new value - mapBuffer.add(value); - current = mapBuffer.sum(MAP_CAM_BUFFER); - } - - bool isPeak() { - return previous > prevPrevious && previous >= current; - } -}; - TEST(trigger, map_cam) { MapState state;