trigger decode cleanup and perf (#1853)

* trigger decode cleanup

* test config ptr patchup

* disable CDM by default
This commit is contained in:
Matthew Kennedy 2020-10-04 16:29:26 -07:00 committed by GitHub
parent 1b9eee0357
commit ebc884e8d5
8 changed files with 53 additions and 91 deletions

View File

@ -18,7 +18,7 @@
#define EFI_FSIO TRUE #define EFI_FSIO TRUE
#ifndef EFI_CDM_INTEGRATION #ifndef EFI_CDM_INTEGRATION
#define EFI_CDM_INTEGRATION TRUE #define EFI_CDM_INTEGRATION FALSE
#endif #endif
#ifndef EFI_TOOTH_LOGGER #ifndef EFI_TOOTH_LOGGER

View File

@ -94,6 +94,7 @@ trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode) {
void Engine::initializeTriggerWaveform(Logging *logger DECLARE_ENGINE_PARAMETER_SUFFIX) { void Engine::initializeTriggerWaveform(Logging *logger DECLARE_ENGINE_PARAMETER_SUFFIX) {
static TriggerState initState; static TriggerState initState;
INJECT_ENGINE_REFERENCE(&initState);
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT #if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
// we have a confusing threading model so some synchronization would not hurt // we have a confusing threading model so some synchronization would not hurt
@ -245,11 +246,11 @@ void Engine::onTriggerSignalEvent(efitick_t nowNt) {
lastTriggerToothEventTimeNt = nowNt; lastTriggerToothEventTimeNt = nowNt;
} }
Engine::Engine() : primaryTriggerConfiguration(this), vvtTriggerConfiguration(this) { Engine::Engine() {
reset(); reset();
} }
Engine::Engine(persistent_config_s *config) : primaryTriggerConfiguration(this), vvtTriggerConfiguration(this) { Engine::Engine(persistent_config_s *config) {
setConfig(config); setConfig(config);
reset(); reset();
} }
@ -394,10 +395,21 @@ void Engine::OnTriggerSyncronization(bool wasSynchronized) {
} }
#endif #endif
void Engine::injectEngineReferences() {
Engine *engine = this;
EXPAND_Engine;
INJECT_ENGINE_REFERENCE(&primaryTriggerConfiguration);
INJECT_ENGINE_REFERENCE(&vvtTriggerConfiguration);
triggerCentral.init(PASS_ENGINE_PARAMETER_SIGNATURE);
}
void Engine::setConfig(persistent_config_s *config) { void Engine::setConfig(persistent_config_s *config) {
this->config = config; this->config = config;
engineConfigurationPtr = &config->engineConfiguration; engineConfigurationPtr = &config->engineConfiguration;
memset(config, 0, sizeof(persistent_config_s)); memset(config, 0, sizeof(persistent_config_s));
injectEngineReferences();
} }
void Engine::printKnockState(void) { void Engine::printKnockState(void) {

View File

@ -54,33 +54,25 @@ class IEtbController;
class IFuelComputer; class IFuelComputer;
class IInjectorModel; class IInjectorModel;
class PrimaryTriggerConfiguration : public TriggerConfiguration { class PrimaryTriggerConfiguration final : public TriggerConfiguration {
public: public:
PrimaryTriggerConfiguration(Engine *engine);
bool isUseOnlyRisingEdgeForTrigger() const; bool isUseOnlyRisingEdgeForTrigger() const;
const char * getPrintPrefix() const; const char * getPrintPrefix() const;
bool isSilentTriggerError() const;
bool isVerboseTriggerSynchDetails() const; bool isVerboseTriggerSynchDetails() const;
debug_mode_e getDebugMode() const; debug_mode_e getDebugMode() const;
trigger_type_e getType() const; trigger_type_e getType() const;
private:
Engine *engine;
}; };
class VvtTriggerConfiguration : public TriggerConfiguration { class VvtTriggerConfiguration final : public TriggerConfiguration {
public: public:
VvtTriggerConfiguration(Engine *engine);
bool isUseOnlyRisingEdgeForTrigger() const; bool isUseOnlyRisingEdgeForTrigger() const;
const char * getPrintPrefix() const; const char * getPrintPrefix() const;
bool isSilentTriggerError() const;
bool isVerboseTriggerSynchDetails() const; bool isVerboseTriggerSynchDetails() const;
debug_mode_e getDebugMode() const; debug_mode_e getDebugMode() const;
trigger_type_e getType() const; trigger_type_e getType() const;
private:
Engine *engine;
}; };
class Engine : public TriggerStateListener { class Engine final : public TriggerStateListener {
public: public:
explicit Engine(persistent_config_s *config); explicit Engine(persistent_config_s *config);
Engine(); Engine();
@ -398,6 +390,8 @@ private:
*/ */
bool isSpinning = false; bool isSpinning = false;
void reset(); void reset();
void injectEngineReferences();
}; };
void prepareShapes(DECLARE_ENGINE_PARAMETER_SIGNATURE); void prepareShapes(DECLARE_ENGINE_PARAMETER_SIGNATURE);

View File

@ -251,24 +251,12 @@ void printCurrentState(Logging *logging, int seconds, const char *engineTypeName
DELIMETER); DELIMETER);
} }
PrimaryTriggerConfiguration::PrimaryTriggerConfiguration(Engine *engine) {
this->engine = engine;
}
bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const { bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const {
return engine->engineConfigurationPtr->useOnlyRisingEdgeForTrigger; return CONFIG(useOnlyRisingEdgeForTrigger);
}
debug_mode_e PrimaryTriggerConfiguration::getDebugMode() const {
return engine->engineConfigurationPtr->debugMode;
} }
trigger_type_e PrimaryTriggerConfiguration::getType() const { trigger_type_e PrimaryTriggerConfiguration::getType() const {
return engine->engineConfigurationPtr->trigger.type; return CONFIG(trigger.type);
}
bool PrimaryTriggerConfiguration::isSilentTriggerError() const {
return engine->engineConfigurationPtr->silentTriggerError;
} }
const char * PrimaryTriggerConfiguration::getPrintPrefix() const { const char * PrimaryTriggerConfiguration::getPrintPrefix() const {
@ -276,33 +264,21 @@ const char * PrimaryTriggerConfiguration::getPrintPrefix() const {
} }
bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() const { bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() const {
return engine->engineConfigurationPtr->verboseTriggerSynchDetails; return CONFIG(verboseTriggerSynchDetails);
}
VvtTriggerConfiguration::VvtTriggerConfiguration(Engine *engine) {
this->engine = engine;
} }
bool VvtTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const { bool VvtTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const {
return engine->engineConfigurationPtr->vvtCamSensorUseRise; return CONFIG(vvtCamSensorUseRise);
} }
const char * VvtTriggerConfiguration::getPrintPrefix() const { const char * VvtTriggerConfiguration::getPrintPrefix() const {
return "VVT "; return "VVT ";
} }
debug_mode_e VvtTriggerConfiguration::getDebugMode() const {
return engine->engineConfigurationPtr->debugMode;
}
trigger_type_e VvtTriggerConfiguration::getType() const { trigger_type_e VvtTriggerConfiguration::getType() const {
return engine->triggerCentral.vvtTriggerType; return engine->triggerCentral.vvtTriggerType;
} }
bool VvtTriggerConfiguration::isSilentTriggerError() const {
return engine->engineConfigurationPtr->silentTriggerError;
}
bool VvtTriggerConfiguration::isVerboseTriggerSynchDetails() const { bool VvtTriggerConfiguration::isVerboseTriggerSynchDetails() const {
return engine->engineConfigurationPtr->verboseVVTDecoding; return CONFIG(verboseVVTDecoding);
} }

View File

@ -51,6 +51,11 @@ TriggerCentral::TriggerCentral() : trigger_central_s() {
noiseFilter.resetAccumSignalData(); noiseFilter.resetAccumSignalData();
} }
void TriggerCentral::init(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
INJECT_ENGINE_REFERENCE(&triggerState);
INJECT_ENGINE_REFERENCE(&vvtState);
}
void TriggerNoiseFilter::resetAccumSignalData() { void TriggerNoiseFilter::resetAccumSignalData() {
memset(lastSignalTimes, 0xff, sizeof(lastSignalTimes)); // = -1 memset(lastSignalTimes, 0xff, sizeof(lastSignalTimes)); // = -1
memset(accumSignalPeriods, 0, sizeof(accumSignalPeriods)); memset(accumSignalPeriods, 0, sizeof(accumSignalPeriods));
@ -429,8 +434,8 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta
if (!noiseFilter.noiseFilter(timestamp, &triggerState, signal PASS_ENGINE_PARAMETER_SUFFIX)) { if (!noiseFilter.noiseFilter(timestamp, &triggerState, signal PASS_ENGINE_PARAMETER_SUFFIX)) {
return; return;
} }
const TriggerConfiguration * triggerConfiguration = &ENGINE(primaryTriggerConfiguration); const TriggerConfiguration* triggerConfiguration = &ENGINE(primaryTriggerConfiguration);
// moved here from hwHandleShaftSignal()
if (!isUsefulSignal(signal, triggerConfiguration)) { if (!isUsefulSignal(signal, triggerConfiguration)) {
return; return;
} }

View File

@ -34,9 +34,10 @@ public:
* Probably not: we have an instance of TriggerState which is used for trigger initialization, * Probably not: we have an instance of TriggerState which is used for trigger initialization,
* also composition probably better than inheritance here * also composition probably better than inheritance here
*/ */
class TriggerCentral : public trigger_central_s { class TriggerCentral final : public trigger_central_s {
public: public:
TriggerCentral(); TriggerCentral();
void init(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void addEventListener(ShaftPositionListener handler, const char *name, Engine *engine); void addEventListener(ShaftPositionListener handler, const char *name, Engine *engine);
void handleShaftSignal(trigger_event_e signal, efitick_t timestamp DECLARE_ENGINE_PARAMETER_SUFFIX); void handleShaftSignal(trigger_event_e signal, efitick_t timestamp DECLARE_ENGINE_PARAMETER_SUFFIX);
int getHwEventCounter(int index) const; int getHwEventCounter(int index) const;

View File

@ -400,10 +400,10 @@ void TriggerState::onShaftSynchronization(
* @param nowNt current time * @param nowNt current time
*/ */
void TriggerState::decodeTriggerEvent( void TriggerState::decodeTriggerEvent(
const TriggerWaveform *triggerShape, const TriggerWaveform* const triggerShape,
const TriggerStateCallback triggerCycleCallback, const TriggerStateCallback triggerCycleCallback,
TriggerStateListener * triggerStateListener, TriggerStateListener* triggerStateListener,
const TriggerConfiguration * triggerConfiguration, const TriggerConfiguration* const triggerConfiguration,
const trigger_event_e signal, const trigger_event_e signal,
const efitick_t nowNt) { const efitick_t nowNt) {
ScopePerf perf(PE::DecodeTriggerEvent); ScopePerf perf(PE::DecodeTriggerEvent);
@ -468,29 +468,18 @@ void TriggerState::decodeTriggerEvent(
nextTriggerEvent() nextTriggerEvent()
; ;
} else { } else {
#if !EFI_PROD_CODE
#if EFI_UNIT_TEST
if (printTriggerTrace) { if (printTriggerTrace) {
printf("%s event %s %d\r\n", printf("%s event %s %d\r\n",
getTrigger_type_e(triggerConfiguration->getType()), getTrigger_type_e(triggerConfiguration->getType()),
getTrigger_event_e(signal), getTrigger_event_e(signal),
nowNt); nowNt);
}
#endif /* EFI_UNIT_TEST */
isFirstEvent = false;
// todo: skip a number of signal from the beginning
#if EFI_PROD_CODE
// scheduleMsg(&logger, "from %.2f to %.2f %d %d", triggerConfig->syncRatioFrom, triggerConfig->syncRatioTo, toothDurations[0], shaftPositionState->toothDurations[1]);
// scheduleMsg(&logger, "ratio %.2f", 1.0 * toothDurations[0]/ shaftPositionState->toothDurations[1]);
#else
if (printTriggerTrace) {
printf("decodeTriggerEvent ratio %.2f: current=%d previous=%d\r\n", 1.0 * toothDurations[0] / toothDurations[1], printf("decodeTriggerEvent ratio %.2f: current=%d previous=%d\r\n", 1.0 * toothDurations[0] / toothDurations[1],
toothDurations[0], toothDurations[1]); toothDurations[0], toothDurations[1]);
} }
#endif #endif
isFirstEvent = false;
bool isSynchronizationPoint; bool isSynchronizationPoint;
bool wasSynchronized = shaft_is_synchronized; bool wasSynchronized = shaft_is_synchronized;
@ -521,10 +510,9 @@ void TriggerState::decodeTriggerEvent(
DISPLAY(DISPLAY_FIELD(vvtCamCounter)); DISPLAY(DISPLAY_FIELD(vvtCamCounter));
if (triggerShape->isSynchronizationNeeded) { if (triggerShape->isSynchronizationNeeded) {
currentGap = 1.0 * toothDurations[0] / toothDurations[1]; currentGap = 1.0 * toothDurations[0] / toothDurations[1];
if (triggerConfiguration->getDebugMode() == DBG_TRIGGER_COUNTERS) { if (CONFIG(debugMode) == DBG_TRIGGER_COUNTERS) {
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
tsOutputChannels.debugFloatField6 = currentGap; tsOutputChannels.debugFloatField6 = currentGap;
tsOutputChannels.debugIntField3 = currentCycle.current_index; tsOutputChannels.debugIntField3 = currentCycle.current_index;
@ -532,25 +520,24 @@ void TriggerState::decodeTriggerEvent(
} }
bool isSync = true; bool isSync = true;
for (int i = 0;i<triggerShape->gapTrackingLength;i++) { for (int i = 0; i < triggerShape->gapTrackingLength; i++) {
bool isGapCondition = cisnan(triggerShape->syncronizationRatioFrom[i]) || (toothDurations[i] > toothDurations[i + 1] * triggerShape->syncronizationRatioFrom[i] bool isGapCondition = cisnan(triggerShape->syncronizationRatioFrom[i]) || (toothDurations[i] > toothDurations[i + 1] * triggerShape->syncronizationRatioFrom[i]
&& toothDurations[i] < toothDurations[i + 1] * triggerShape->syncronizationRatioTo[i]); && toothDurations[i] < toothDurations[i + 1] * triggerShape->syncronizationRatioTo[i]);
isSync &= isGapCondition; isSync &= isGapCondition;
} }
isSynchronizationPoint = isSync; isSynchronizationPoint = isSync;
if (isSynchronizationPoint) { if (isSynchronizationPoint) {
enginePins.debugTriggerSync.setValue(1); enginePins.debugTriggerSync.toggle();
} }
/** /**
* todo: technically we can afford detailed logging even with 60/2 as long as low RPM * todo: technically we can afford detailed logging even with 60/2 as long as low RPM
* todo: figure out exact threshold as a function of RPM and tooth count? * 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? * Open question what is 'triggerShape->getSize()' for 60/2 is it 58 or 58*2 or 58*4?
*/ */
bool silentTriggerError = triggerShape->getSize() > 40 && triggerConfiguration->isSilentTriggerError(); bool silentTriggerError = triggerShape->getSize() > 40 && CONFIG(silentTriggerError);
#if EFI_UNIT_TEST #if EFI_UNIT_TEST
actualSynchGap = 1.0 * toothDurations[0] / toothDurations[1]; actualSynchGap = 1.0 * toothDurations[0] / toothDurations[1];
@ -595,11 +582,7 @@ void TriggerState::decodeTriggerEvent(
boolToString(someSortOfTriggerError)); boolToString(someSortOfTriggerError));
} }
} }
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
enginePins.debugTriggerSync.setValue(0);
} else { } else {
/** /**
* We are here in case of a wheel without synchronization - we just need to count events, * We are here in case of a wheel without synchronization - we just need to count events,
@ -608,30 +591,20 @@ void TriggerState::decodeTriggerEvent(
* in case of noise the counter could be above the expected number of events, that's why 'more or equals' and not just 'equals' * in case of noise the counter could be above the expected number of events, that's why 'more or equals' and not just 'equals'
*/ */
#if EFI_UNIT_TEST
if (printTriggerTrace) {
printf("decodeTriggerEvent sync=%d index=%d size=%d\r\n",
shaft_is_synchronized,
currentCycle.current_index,
triggerShape->getSize());
}
#endif /* EFI_UNIT_TEST */
unsigned int endOfCycleIndex = triggerShape->getSize() - (triggerConfiguration->isUseOnlyRisingEdgeForTrigger() ? 2 : 1); unsigned int endOfCycleIndex = triggerShape->getSize() - (triggerConfiguration->isUseOnlyRisingEdgeForTrigger() ? 2 : 1);
isSynchronizationPoint = !shaft_is_synchronized || (currentCycle.current_index >= endOfCycleIndex); isSynchronizationPoint = !shaft_is_synchronized || (currentCycle.current_index >= endOfCycleIndex);
#if EFI_UNIT_TEST #if EFI_UNIT_TEST
if (printTriggerTrace) { if (printTriggerTrace) {
printf("decodeTriggerEvent decodeTriggerEvent isSynchronizationPoint=%d index=%d size=%d\r\n", printf("decodeTriggerEvent sync=%d isSynchronizationPoint=%d index=%d size=%d\r\n",
isSynchronizationPoint, shaft_is_synchronized,
currentCycle.current_index, isSynchronizationPoint,
triggerShape->getSize()); currentCycle.current_index,
triggerShape->getSize());
} }
#endif /* EFI_UNIT_TEST */ #endif /* EFI_UNIT_TEST */
} }
#if EFI_UNIT_TEST #if EFI_UNIT_TEST
if (printTriggerTrace) { if (printTriggerTrace) {
printf("decodeTriggerEvent %s isSynchronizationPoint=%d index=%d %s\r\n", printf("decodeTriggerEvent %s isSynchronizationPoint=%d index=%d %s\r\n",
@ -642,7 +615,6 @@ void TriggerState::decodeTriggerEvent(
#endif /* EFI_UNIT_TEST */ #endif /* EFI_UNIT_TEST */
if (isSynchronizationPoint) { if (isSynchronizationPoint) {
if (triggerStateListener) { if (triggerStateListener) {
triggerStateListener->OnTriggerSyncronization(wasSynchronized); triggerStateListener->OnTriggerSyncronization(wasSynchronized);
} }

View File

@ -25,11 +25,11 @@ struct TriggerStateListener {
class TriggerConfiguration { class TriggerConfiguration {
public: public:
DECLARE_ENGINE_PTR;
virtual bool isUseOnlyRisingEdgeForTrigger() const = 0; virtual bool isUseOnlyRisingEdgeForTrigger() const = 0;
virtual bool isSilentTriggerError() const = 0;
virtual bool isVerboseTriggerSynchDetails() const = 0; virtual bool isVerboseTriggerSynchDetails() const = 0;
virtual const char * getPrintPrefix() const = 0; virtual const char * getPrintPrefix() const = 0;
virtual debug_mode_e getDebugMode() const = 0;
virtual trigger_type_e getType() const = 0; virtual trigger_type_e getType() const = 0;
}; };
@ -65,6 +65,8 @@ typedef struct {
*/ */
class TriggerState : public trigger_state_s { class TriggerState : public trigger_state_s {
public: public:
DECLARE_ENGINE_PTR;
TriggerState(); TriggerState();
/** /**
* current trigger processing index, between zero and #size * current trigger processing index, between zero and #size