VVT support for VAG trigger #883

This commit is contained in:
rusefi 2020-08-24 01:21:42 -04:00
parent 7fab55ad3e
commit 95bd50da94
9 changed files with 106 additions and 29 deletions

View File

@ -222,11 +222,11 @@ void Engine::onTriggerSignalEvent(efitick_t nowNt) {
lastTriggerToothEventTimeNt = nowNt;
}
Engine::Engine() {
Engine::Engine() : primaryTriggerConfiguration(this) {
reset();
}
Engine::Engine(persistent_config_s *config) {
Engine::Engine(persistent_config_s *config) : primaryTriggerConfiguration(this) {
setConfig(config);
reset();
}

View File

@ -57,6 +57,17 @@ public:
gear_e currentGear = NEUTRAL;
};
class PrimaryTriggerConfiguration : public TriggerConfiguration {
public:
PrimaryTriggerConfiguration(Engine *engine);
bool isUseOnlyRisingEdgeForTrigger();
bool isSilentTriggerError();
bool isVerboseTriggerSynchDetails();
debug_mode_e getDebugMode();
private:
Engine *engine;
};
class Engine : public TriggerStateListener {
public:
explicit Engine(persistent_config_s *config);
@ -68,6 +79,8 @@ public:
cyclic_buffer<int> triggerErrorDetection;
PrimaryTriggerConfiguration primaryTriggerConfiguration;
TCU tcu;
#if EFI_SHAFT_POSITION_INPUT

View File

@ -281,3 +281,23 @@ void printCurrentState(Logging *logging, int seconds, const char *engineTypeName
DELIMETER);
}
PrimaryTriggerConfiguration::PrimaryTriggerConfiguration(Engine *engine) {
this->engine = engine;
}
bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() {
return engine->engineConfigurationPtr->useOnlyRisingEdgeForTrigger;
}
debug_mode_e PrimaryTriggerConfiguration::getDebugMode() {
return engine->engineConfigurationPtr->debugMode;
}
bool PrimaryTriggerConfiguration::isSilentTriggerError() {
return engine->engineConfigurationPtr->silentTriggerError;
}
bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() {
return engine->engineConfigurationPtr->verboseTriggerSynchDetails;
}

View File

@ -452,7 +452,10 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta
* This invocation changes the state of triggerState
*/
triggerState.decodeTriggerEvent(&triggerShape,
nullptr, engine, signal, timestamp PASS_CONFIG_PARAMETER_SUFFIX);
nullptr,
engine,
&engine->primaryTriggerConfiguration,
signal, timestamp PASS_CONFIG_PARAMETER_SUFFIX);
/**
* If we only have a crank position sensor with four stroke, here we are extending crank revolutions with a 360 degree

View File

@ -128,7 +128,9 @@ void calculateTriggerSynchPoint(TriggerWaveform *shape, TriggerState *state DECL
trigger_config_s const*triggerConfig = &engineConfiguration->trigger;
engine->triggerErrorDetection.clear();
shape->triggerShapeSynchPointIndex = state->findTriggerZeroEventIndex(shape, triggerConfig PASS_CONFIG_PARAMETER_SUFFIX);
shape->triggerShapeSynchPointIndex = state->findTriggerZeroEventIndex(shape,
&engine->primaryTriggerConfiguration,
triggerConfig PASS_CONFIG_PARAMETER_SUFFIX);
int length = shape->getLength();
engine->engineCycleEventCount = length;
@ -382,9 +384,12 @@ void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycl
* @param signal type of event which just happened
* @param nowNt current time
*/
void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const TriggerStateCallback triggerCycleCallback,
void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape,
const TriggerStateCallback triggerCycleCallback,
TriggerStateListener * triggerStateListener,
trigger_event_e const signal, efitick_t nowNt DECLARE_CONFIG_PARAMETER_SUFFIX) {
const TriggerConfiguration * triggerConfiguration,
const trigger_event_e signal,
const efitick_t nowNt DECLARE_CONFIG_PARAMETER_SUFFIX) {
ScopePerf perf(PE::DecodeTriggerEvent);
if (nowNt - previousShaftEventTimeNt > NT_PER_SECOND) {
@ -400,7 +405,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const Trigg
previousShaftEventTimeNt = nowNt;
bool useOnlyRisingEdgeForTrigger = CONFIG(useOnlyRisingEdgeForTrigger);
bool useOnlyRisingEdgeForTrigger = triggerConfiguration->isUseOnlyRisingEdgeForTrigger();
efiAssertVoid(CUSTOM_TRIGGER_UNEXPECTED, signal <= SHAFT_3RD_RISING, "unexpected signal");
@ -503,7 +508,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const Trigg
currentGap = 1.0 * toothDurations[0] / toothDurations[1];
if (CONFIG(debugMode) == DBG_TRIGGER_COUNTERS) {
if (triggerConfiguration->getDebugMode() == DBG_TRIGGER_COUNTERS) {
#if EFI_TUNER_STUDIO
tsOutputChannels.debugFloatField6 = currentGap;
tsOutputChannels.debugIntField3 = currentCycle.current_index;
@ -529,14 +534,14 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const Trigg
* todo: figure out exact threshold as a function of RPM and tooth count?
* Open question what is 'triggerShape->getSize()' for 60/2 is it 58 or 58*2 or 58*4?
*/
bool silentTriggerError = triggerShape->getSize() > 40 && CONFIG(silentTriggerError);
bool silentTriggerError = triggerShape->getSize() > 40 && triggerConfiguration->isSilentTriggerError();
#if EFI_UNIT_TEST
actualSynchGap = 1.0 * toothDurations[0] / toothDurations[1];
#endif /* EFI_UNIT_TEST */
#if EFI_PROD_CODE || EFI_SIMULATOR
if (CONFIG(verboseTriggerSynchDetails) || (someSortOfTriggerError && !silentTriggerError)) {
if (triggerConfiguration->isVerboseTriggerSynchDetails() || (someSortOfTriggerError && !silentTriggerError)) {
for (int i = 0;i<GAP_TRACKING_LENGTH;i++) {
float ratioFrom = triggerShape->syncronizationRatioFrom[i];
if (cisnan(ratioFrom)) {
@ -594,7 +599,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const Trigg
triggerShape->getSize());
}
#endif /* EFI_UNIT_TEST */
unsigned int endOfCycleIndex = triggerShape->getSize() - (CONFIG(useOnlyRisingEdgeForTrigger) ? 2 : 1);
unsigned int endOfCycleIndex = triggerShape->getSize() - (triggerConfiguration->isUseOnlyRisingEdgeForTrigger() ? 2 : 1);
isSynchronizationPoint = !shaft_is_synchronized || (currentCycle.current_index >= endOfCycleIndex);
@ -673,6 +678,7 @@ static void onFindIndexCallback(TriggerState *state) {
* This function finds the index of synchronization event within TriggerWaveform
*/
uint32_t TriggerState::findTriggerZeroEventIndex(TriggerWaveform * shape,
const TriggerConfiguration * triggerConfiguration,
trigger_config_s const*triggerConfig DECLARE_CONFIG_PARAMETER_SUFFIX) {
UNUSED(triggerConfig);
#if EFI_PROD_CODE
@ -690,7 +696,9 @@ uint32_t TriggerState::findTriggerZeroEventIndex(TriggerWaveform * shape,
// todo: should this variable be declared 'static' to reduce stack usage?
TriggerStimulatorHelper helper;
uint32_t syncIndex = helper.findTriggerSyncPoint(shape, this PASS_CONFIG_PARAMETER_SUFFIX);
uint32_t syncIndex = helper.findTriggerSyncPoint(shape,
triggerConfiguration,
this PASS_CONFIG_PARAMETER_SUFFIX);
if (syncIndex == EFI_ERROR_CODE) {
return syncIndex;
}
@ -709,7 +717,8 @@ uint32_t TriggerState::findTriggerZeroEventIndex(TriggerWaveform * shape,
* todo: add a comment why are we doing '2 * shape->getSize()' here?
*/
helper.assertSyncPositionAndSetDutyCycle(onFindIndexCallback, syncIndex, this, shape PASS_CONFIG_PARAMETER_SUFFIX);
helper.assertSyncPositionAndSetDutyCycle(onFindIndexCallback, triggerConfiguration,
syncIndex, this, shape PASS_CONFIG_PARAMETER_SUFFIX);
return syncIndex % shape->getSize();
}

View File

@ -20,7 +20,15 @@ struct TriggerStateListener {
virtual void OnTriggerSyncronization(bool wasSynchronized) = 0;
virtual void OnTriggerInvalidIndex(int currentIndex) = 0;
virtual void OnTriggerSynchronizationLost() = 0;
#endif
#endif // EFI_SHAFT_POSITION_INPUT
};
class TriggerConfiguration {
public:
virtual bool isUseOnlyRisingEdgeForTrigger() = 0;
virtual bool isSilentTriggerError() = 0;
virtual bool isVerboseTriggerSynchDetails() = 0;
virtual debug_mode_e getDebugMode() = 0;
};
typedef void (*TriggerStateCallback)(TriggerState *);
@ -68,9 +76,12 @@ public:
void incrementTotalEventCounter();
efitime_t getTotalEventCounter() const;
void decodeTriggerEvent(TriggerWaveform *triggerShape, const TriggerStateCallback triggerCycleCallback,
void decodeTriggerEvent(TriggerWaveform *triggerShape,
const TriggerStateCallback triggerCycleCallback,
TriggerStateListener * triggerStateListener,
trigger_event_e const signal, efitime_t nowUs DECLARE_CONFIG_PARAMETER_SUFFIX);
const TriggerConfiguration * triggerConfiguration,
const trigger_event_e signal,
const efitime_t nowUs DECLARE_CONFIG_PARAMETER_SUFFIX);
bool validateEventCounters(TriggerWaveform *triggerShape) const;
void onShaftSynchronization(const TriggerStateCallback triggerCycleCallback,
@ -119,7 +130,9 @@ public:
*/
efitick_t startOfCycleNt;
uint32_t findTriggerZeroEventIndex(TriggerWaveform * shape, trigger_config_s const*triggerConfig
uint32_t findTriggerZeroEventIndex(TriggerWaveform * shape,
const TriggerConfiguration * triggerConfiguration,
trigger_config_s const*triggerConfig
DECLARE_CONFIG_PARAMETER_SUFFIX);
private:

View File

@ -30,6 +30,7 @@ extern bool printTriggerDebug;
#endif /* ! EFI_UNIT_TEST */
void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback,
const TriggerConfiguration * triggerConfiguration,
TriggerState *state, TriggerWaveform * shape, int i
DECLARE_CONFIG_PARAMETER_SUFFIX) {
efiAssertVoid(CUSTOM_ERR_6593, shape->getSize() > 0, "size not zero");
@ -70,6 +71,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
state->decodeTriggerEvent(shape,
triggerCycleCallback,
/* override */ nullptr,
triggerConfiguration,
s, time PASS_CONFIG_PARAMETER_SUFFIX);
}
}
@ -81,6 +83,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
state->decodeTriggerEvent(shape,
triggerCycleCallback,
/* override */ nullptr,
triggerConfiguration,
s, time PASS_CONFIG_PARAMETER_SUFFIX);
}
}
@ -92,12 +95,14 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
state->decodeTriggerEvent(shape,
triggerCycleCallback,
/* override */ nullptr,
triggerConfiguration,
s, time PASS_CONFIG_PARAMETER_SUFFIX);
}
}
}
void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const TriggerStateCallback triggerCycleCallback,
const TriggerConfiguration * triggerConfiguration,
const uint32_t syncIndex, TriggerState *state, TriggerWaveform * shape
DECLARE_CONFIG_PARAMETER_SUFFIX) {
@ -105,7 +110,9 @@ void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const TriggerSta
* let's feed two more cycles to validate shape definition
*/
for (uint32_t i = syncIndex + 1; i <= syncIndex + GAP_TRACKING_LENGTH * shape->getSize(); i++) {
feedSimulatedEvent(triggerCycleCallback, state, shape, i PASS_CONFIG_PARAMETER_SUFFIX);
feedSimulatedEvent(triggerCycleCallback,
triggerConfiguration,
state, shape, i PASS_CONFIG_PARAMETER_SUFFIX);
}
int revolutionCounter = state->getTotalRevolutionCounter();
if (revolutionCounter != GAP_TRACKING_LENGTH + 1) {
@ -124,9 +131,12 @@ void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const TriggerSta
* @return trigger synchronization point index, or error code if not found
*/
uint32_t TriggerStimulatorHelper::findTriggerSyncPoint(TriggerWaveform * shape,
const TriggerConfiguration * triggerConfiguration,
TriggerState *state DECLARE_CONFIG_PARAMETER_SUFFIX) {
for (int i = 0; i < 4 * PWM_PHASE_MAX_COUNT; i++) {
feedSimulatedEvent(nullptr, state, shape, i PASS_CONFIG_PARAMETER_SUFFIX);
feedSimulatedEvent(nullptr,
triggerConfiguration,
state, shape, i PASS_CONFIG_PARAMETER_SUFFIX);
if (state->shaft_is_synchronized) {
return i;

View File

@ -14,15 +14,19 @@ class TriggerStimulatorHelper {
public:
uint32_t findTriggerSyncPoint(TriggerWaveform * shape,
const TriggerConfiguration * triggerConfiguration,
TriggerState *state DECLARE_CONFIG_PARAMETER_SUFFIX);
void assertSyncPositionAndSetDutyCycle(const TriggerStateCallback triggerCycleCallback,
const TriggerConfiguration * triggerConfiguration,
const uint32_t index, TriggerState *state, TriggerWaveform * shape
DECLARE_CONFIG_PARAMETER_SUFFIX);
private:
// send next event so that we can see how state reacts
void feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback, TriggerState *state,
void feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback,
const TriggerConfiguration * triggerConfiguration,
TriggerState *state,
TriggerWaveform * shape, int i DECLARE_CONFIG_PARAMETER_SUFFIX);
};

View File

@ -42,8 +42,11 @@ static int getTriggerZeroEventIndex(engine_type_e engineType) {
initDataStructures(PASS_ENGINE_PARAMETER_SIGNATURE);
const TriggerConfiguration * triggerConfiguration = &engine->primaryTriggerConfiguration;
TriggerWaveform * shape = &eth.engine.triggerCentral.triggerShape;
return eth.engine.triggerCentral.triggerState.findTriggerZeroEventIndex(shape, &engineConfiguration->trigger
return eth.engine.triggerCentral.triggerState.findTriggerZeroEventIndex(shape, triggerConfiguration,
&engineConfiguration->trigger
PASS_CONFIG_PARAMETER_SUFFIX);
}
@ -118,31 +121,33 @@ TEST(misc, testSomethingWeird) {
TriggerState state_;
TriggerState *sta = &state_;
const TriggerConfiguration * triggerConfiguration = &engine->primaryTriggerConfiguration;
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized";
int r = 10;
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized"; // still no synchronization
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, ++r PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, ++r PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_TRUE(sta->shaft_is_synchronized); // first signal rise synchronize
ASSERT_EQ(0, sta->getCurrentIndex());
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_EQ(1, sta->getCurrentIndex());
for (int i = 2; i < 10;) {
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
assertEqualsM("even", i++, sta->getCurrentIndex());
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
assertEqualsM("odd", i++, sta->getCurrentIndex());
}
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_EQ(10, sta->getCurrentIndex());
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_EQ(11, sta->getCurrentIndex());
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
ASSERT_EQ(0, sta->getCurrentIndex()); // new revolution
}