mirror of https://github.com/rusefi/rusefi-1.git
refactoring trigger
This commit is contained in:
parent
1817b8654e
commit
db2ab72d12
|
@ -258,6 +258,17 @@ void Engine::OnTriggerStateProperState(efitick_t nowNt) {
|
|||
rpmCalculator.setSpinningUp(nowNt PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
}
|
||||
|
||||
void Engine::OnTriggerInvalidIndex(int currentIndex) {
|
||||
Engine *engine = this;
|
||||
EXPAND_Engine;
|
||||
// let's not show a warning if we are just starting to spin
|
||||
if (GET_RPM_VALUE != 0) {
|
||||
warning(CUSTOM_SYNC_ERROR, "sync error: index #%d above total size %d", currentIndex, triggerCentral.triggerShape.getSize());
|
||||
triggerCentral.triggerState.lastDecodingErrorTime = getTimeNowNt();
|
||||
triggerCentral.triggerState.someSortOfTriggerError = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::OnTriggerSyncronization(bool wasSynchronized) {
|
||||
// We only care about trigger shape once we have synchronized trigger. Anything could happen
|
||||
// during first revolution and it's fine
|
||||
|
|
|
@ -61,6 +61,7 @@ public:
|
|||
void OnTriggerStateDecodingError() override;
|
||||
void OnTriggerStateProperState(efitick_t nowNt) override;
|
||||
void OnTriggerSyncronization(bool wasSynchronized) override;
|
||||
void OnTriggerInvalidIndex(int currentIndex) override;
|
||||
|
||||
void setConfig(persistent_config_s *config);
|
||||
injection_mode_e getCurrentInjectionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||
|
|
|
@ -352,7 +352,8 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta
|
|||
/**
|
||||
* This invocation changes the state of triggerState
|
||||
*/
|
||||
triggerState.decodeTriggerEvent(nullptr, engine, signal, timestamp PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
triggerState.decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape),
|
||||
nullptr, engine, 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
|
||||
|
|
|
@ -297,10 +297,10 @@ static trigger_value_e eventType[6] = { TV_FALL, TV_RISE, TV_FALL, TV_RISE, TV_F
|
|||
PRINT_INC_INDEX; \
|
||||
}
|
||||
|
||||
#define considerEventForGap() (!TRIGGER_WAVEFORM(useOnlyPrimaryForSync) || isPrimary)
|
||||
#define considerEventForGap() (!triggerShape->useOnlyPrimaryForSync || isPrimary)
|
||||
|
||||
#define needToSkipFall(type) ((!TRIGGER_WAVEFORM(gapBothDirections)) && (( TRIGGER_WAVEFORM(useRiseEdge)) && (type != TV_RISE)))
|
||||
#define needToSkipRise(type) ((!TRIGGER_WAVEFORM(gapBothDirections)) && ((!TRIGGER_WAVEFORM(useRiseEdge)) && (type != TV_FALL)))
|
||||
#define needToSkipFall(type) ((!triggerShape->gapBothDirections) && (( triggerShape->useRiseEdge) && (type != TV_RISE)))
|
||||
#define needToSkipRise(type) ((!triggerShape->gapBothDirections) && ((!triggerShape->useRiseEdge) && (type != TV_FALL)))
|
||||
|
||||
int TriggerState::getCurrentIndex() const {
|
||||
return currentCycle.current_index;
|
||||
|
@ -407,14 +407,13 @@ void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycl
|
|||
* @param signal type of event which just happened
|
||||
* @param nowNt current time
|
||||
*/
|
||||
void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCallback,
|
||||
void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, const TriggerStateCallback triggerCycleCallback,
|
||||
TriggerStateListener * triggerStateListener,
|
||||
trigger_event_e const signal, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
||||
trigger_event_e const signal, efitick_t nowNt DECLARE_CONFIG_PARAMETER_SUFFIX) {
|
||||
ScopePerf perf(PE::DecodeTriggerEvent, static_cast<uint8_t>(signal));
|
||||
|
||||
bool useOnlyRisingEdgeForTrigger = CONFIG(useOnlyRisingEdgeForTrigger);
|
||||
// todo: use 'triggerShape' instead of TRIGGER_WAVEFORM in order to decouple this method from engine #635
|
||||
TriggerWaveform *triggerShape = &ENGINE(triggerCentral.triggerShape);
|
||||
|
||||
|
||||
efiAssertVoid(CUSTOM_ERR_6640, signal <= SHAFT_3RD_RISING, "unexpected signal");
|
||||
|
||||
|
@ -526,7 +525,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
|
|||
|
||||
bool isSync = true;
|
||||
for (int i = 0;i<GAP_TRACKING_LENGTH;i++) {
|
||||
bool isGapCondition = cisnan(triggerShape->syncronizationRatioFrom[i]) || (toothDurations[i] > toothDurations[i + 1] * TRIGGER_WAVEFORM(syncronizationRatioFrom[i])
|
||||
bool isGapCondition = cisnan(triggerShape->syncronizationRatioFrom[i]) || (toothDurations[i] > toothDurations[i + 1] * triggerShape->syncronizationRatioFrom[i]
|
||||
&& toothDurations[i] < toothDurations[i + 1] * triggerShape->syncronizationRatioTo[i]);
|
||||
|
||||
isSync &= isGapCondition;
|
||||
|
@ -552,7 +551,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
|
|||
#if EFI_PROD_CODE || EFI_SIMULATOR
|
||||
if (CONFIG(verboseTriggerSynchDetails) || (someSortOfTriggerError && !silentTriggerError)) {
|
||||
for (int i = 0;i<GAP_TRACKING_LENGTH;i++) {
|
||||
float ratioFrom = TRIGGER_WAVEFORM(syncronizationRatioFrom[i]);
|
||||
float ratioFrom = triggerShape->syncronizationRatioFrom[i];
|
||||
if (cisnan(ratioFrom)) {
|
||||
// we do not track gap at this depth
|
||||
continue;
|
||||
|
@ -568,7 +567,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
|
|||
i,
|
||||
gap,
|
||||
ratioFrom,
|
||||
TRIGGER_WAVEFORM(syncronizationRatioTo[i]),
|
||||
triggerShape->syncronizationRatioTo[i],
|
||||
boolToString(someSortOfTriggerError));
|
||||
}
|
||||
}
|
||||
|
@ -581,8 +580,8 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
|
|||
print("index=%d: gap=%.2f expected from %.2f to %.2f error=%s\r\n",
|
||||
i,
|
||||
gap,
|
||||
TRIGGER_WAVEFORM(syncronizationRatioFrom[i]),
|
||||
TRIGGER_WAVEFORM(syncronizationRatioTo[i]),
|
||||
triggerShape->syncronizationRatioFrom[i],
|
||||
triggerShape->syncronizationRatioTo[i],
|
||||
boolToString(someSortOfTriggerError));
|
||||
}
|
||||
}
|
||||
|
@ -657,12 +656,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
|
|||
toothed_previous_time = nowNt;
|
||||
}
|
||||
if (!isValidIndex(triggerShape) && triggerStateListener) {
|
||||
// let's not show a warning if we are just starting to spin
|
||||
if (GET_RPM_VALUE != 0) {
|
||||
warning(CUSTOM_SYNC_ERROR, "sync error: index #%d above total size %d", currentCycle.current_index, triggerShape->getSize());
|
||||
lastDecodingErrorTime = getTimeNowNt();
|
||||
someSortOfTriggerError = true;
|
||||
}
|
||||
triggerStateListener->OnTriggerInvalidIndex(currentCycle.current_index);
|
||||
}
|
||||
if (someSortOfTriggerError) {
|
||||
if (getTimeNowNt() - lastDecodingErrorTime > NT_PER_SECOND) {
|
||||
|
|
|
@ -19,6 +19,7 @@ class TriggerStateListener {
|
|||
virtual void OnTriggerStateDecodingError() = 0;
|
||||
virtual void OnTriggerStateProperState(efitick_t nowNt) = 0;
|
||||
virtual void OnTriggerSyncronization(bool wasSynchronized) = 0;
|
||||
virtual void OnTriggerInvalidIndex(int currentIndex) = 0;
|
||||
};
|
||||
|
||||
typedef void (*TriggerStateCallback)(TriggerState *);
|
||||
|
@ -66,9 +67,9 @@ public:
|
|||
void incrementTotalEventCounter();
|
||||
efitime_t getTotalEventCounter() const;
|
||||
|
||||
void decodeTriggerEvent(const TriggerStateCallback triggerCycleCallback,
|
||||
void decodeTriggerEvent(TriggerWaveform *triggerShape, const TriggerStateCallback triggerCycleCallback,
|
||||
TriggerStateListener * triggerStateListener,
|
||||
trigger_event_e const signal, efitime_t nowUs DECLARE_ENGINE_PARAMETER_SUFFIX);
|
||||
trigger_event_e const signal, efitime_t nowUs DECLARE_CONFIG_PARAMETER_SUFFIX);
|
||||
|
||||
bool validateEventCounters(DECLARE_ENGINE_PARAMETER_SIGNATURE) const;
|
||||
void handleTriggerError(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||
|
|
|
@ -64,9 +64,10 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
|
|||
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/0, stateIndex);
|
||||
trigger_event_e s = currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING;
|
||||
if (isUsefulSignal(s PASS_CONFIG_PARAMETER_SUFFIX)) {
|
||||
state->decodeTriggerEvent(triggerCycleCallback,
|
||||
state->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape),
|
||||
triggerCycleCallback,
|
||||
/* override */ nullptr,
|
||||
s, time PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
s, time PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,9 +75,10 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
|
|||
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/1, stateIndex);
|
||||
trigger_event_e s = currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING;
|
||||
if (isUsefulSignal(s PASS_CONFIG_PARAMETER_SUFFIX)) {
|
||||
state->decodeTriggerEvent(triggerCycleCallback,
|
||||
state->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape),
|
||||
triggerCycleCallback,
|
||||
/* override */ nullptr,
|
||||
s, time PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
s, time PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,9 +86,10 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig
|
|||
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/2, stateIndex);
|
||||
trigger_event_e s = currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING;
|
||||
if (isUsefulSignal(s PASS_CONFIG_PARAMETER_SUFFIX)) {
|
||||
state->decodeTriggerEvent(triggerCycleCallback,
|
||||
state->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape),
|
||||
triggerCycleCallback,
|
||||
/* override */ nullptr,
|
||||
s, time PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
s, time PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,28 +109,28 @@ TEST(misc, testSomethingWeird) {
|
|||
|
||||
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized";
|
||||
int r = 10;
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized"; // still no synchronization
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, ++r PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,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(nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
ASSERT_EQ(1, sta->getCurrentIndex());
|
||||
|
||||
for (int i = 2; i < 10;) {
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
assertEqualsM("even", i++, sta->getCurrentIndex());
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
assertEqualsM("odd", i++, sta->getCurrentIndex());
|
||||
}
|
||||
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
ASSERT_EQ(10, sta->getCurrentIndex());
|
||||
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
ASSERT_EQ(11, sta->getCurrentIndex());
|
||||
|
||||
sta->decodeTriggerEvent(nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
|
||||
sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr,SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX);
|
||||
ASSERT_EQ(0, sta->getCurrentIndex()); // new revolution
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue