MAP phase sensing #3544

This commit is contained in:
Andrey 2021-11-24 22:36:36 -05:00
parent f1b8acb1fd
commit e2fa1af527
4 changed files with 39 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -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<float, MAP_CAM_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

View File

@ -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<float, MAP_CAM_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;