diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index b4e1e879e9..b436f2bbd2 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -106,7 +106,7 @@ static operation_mode_e lookupOperationMode() { } } -static void initVvtShape(int camIndex, TriggerState &initState) { +static void initVvtShape(int camIndex, TriggerDecoderBase &initState) { vvt_mode_e vvtMode = engineConfiguration->vvtMode[camIndex]; if (vvtMode != VVT_INACTIVE) { @@ -126,7 +126,7 @@ static void initVvtShape(int camIndex, TriggerState &initState) { } void Engine::updateTriggerWaveform() { - static TriggerState initState; + static TriggerDecoderBase initState; // Re-read config in case it's changed primaryTriggerConfiguration.update(); @@ -165,7 +165,7 @@ void Engine::updateTriggerWaveform() { if (!TRIGGER_WAVEFORM(shapeDefinitionError)) { /** - * 'initState' instance of TriggerState is used only to initialize 'this' TriggerWaveform instance + * 'initState' instance of TriggerDecoderBase is used only to initialize 'this' TriggerWaveform instance * #192 BUG real hardware trigger events could be coming even while we are initializing trigger */ calculateTriggerSynchPoint(engine->triggerCentral.triggerShape, diff --git a/firmware/controllers/trigger/decoders/trigger_structure.h b/firmware/controllers/trigger/decoders/trigger_structure.h index 8d56801300..c68f4bde62 100644 --- a/firmware/controllers/trigger/decoders/trigger_structure.h +++ b/firmware/controllers/trigger/decoders/trigger_structure.h @@ -61,7 +61,7 @@ public: #define TRIGGER_CHANNEL_COUNT 3 class Engine; -class TriggerState; +class TriggerDecoderBase; class TriggerFormDetails; class TriggerConfiguration; @@ -264,7 +264,7 @@ public: int triggerShapeSynchPointIndex; void initializeSyncPoint( - TriggerState& state, + TriggerDecoderBase& state, const TriggerConfiguration& triggerConfiguration, const trigger_config_s& triggerConfig ); diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 0f3f099fc1..70dc32ff57 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -286,9 +286,9 @@ void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index) { } if (isVvtWithRealDecoder) { - TriggerState *vvtState = &tc->vvtState[bankIndex][camIndex]; + TriggerDecoderBase& vvtDecoder = tc->vvtState[bankIndex][camIndex]; - vvtState->decodeTriggerEvent( + vvtDecoder.decodeTriggerEvent( "vvt", tc->vvtShape[camIndex], nullptr, @@ -296,8 +296,8 @@ void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index) { engine->vvtTriggerConfiguration[camIndex], front == TV_RISE ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING, nowNt); // yes we log data from all VVT channels into same fields for now - tc->triggerState.vvtSyncGapRatio = vvtState->triggerSyncGapRatio; - tc->triggerState.vvtStateIndex = vvtState->currentCycle.current_index; + tc->triggerState.vvtSyncGapRatio = vvtDecoder.triggerSyncGapRatio; + tc->triggerState.vvtStateIndex = vvtDecoder.currentCycle.current_index; } tc->vvtCamCounter++; @@ -529,7 +529,7 @@ static void reportEventToWaveChart(trigger_event_e ckpSignalType, int index) { * @return true if the signal is passed through. */ bool TriggerNoiseFilter::noiseFilter(efitick_t nowNt, - TriggerState * triggerState, + TriggerDecoderBase * triggerState, trigger_event_e signal) { // todo: find a better place for these defs static const trigger_event_e opposite[6] = { SHAFT_PRIMARY_RISING, SHAFT_PRIMARY_FALLING, SHAFT_SECONDARY_RISING, SHAFT_SECONDARY_FALLING, diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index e72bb4e141..695f2804be 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -31,7 +31,7 @@ class TriggerNoiseFilter { public: void resetAccumSignalData(); bool noiseFilter(efitick_t nowNt, - TriggerState * triggerState, + TriggerDecoderBase* triggerState, trigger_event_e signal); efitick_t lastSignalTimes[HW_EVENT_TYPES]; @@ -108,12 +108,12 @@ public: angle_t vvtPosition[BANKS_COUNT][CAMS_PER_BANK]; #if EFI_SHAFT_POSITION_INPUT - TriggerStateWithRunningStatistics triggerState; + PrimaryTriggerDecoder triggerState; #endif //EFI_SHAFT_POSITION_INPUT TriggerWaveform triggerShape; - TriggerState vvtState[BANKS_COUNT][CAMS_PER_BANK]; + VvtTriggerDecoder vvtState[BANKS_COUNT][CAMS_PER_BANK]; TriggerWaveform vvtShape[CAMS_PER_BANK]; TriggerFormDetails triggerFormDetails; diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 15fb25c8ab..e3272a86fc 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -38,15 +38,15 @@ #include "sensor_chart.h" #endif -TriggerState::TriggerState() { +TriggerDecoderBase::TriggerDecoderBase() { resetTriggerState(); } -bool TriggerState::getShaftSynchronized() { +bool TriggerDecoderBase::getShaftSynchronized() { return shaft_is_synchronized; } -void TriggerState::setShaftSynchronized(bool value) { +void TriggerDecoderBase::setShaftSynchronized(bool value) { if (value) { if (!shaft_is_synchronized) { // just got synchronized @@ -59,7 +59,7 @@ void TriggerState::setShaftSynchronized(bool value) { shaft_is_synchronized = value; } -void TriggerState::resetTriggerState() { +void TriggerDecoderBase::resetTriggerState() { setShaftSynchronized(false); toothed_previous_time = 0; @@ -82,12 +82,12 @@ void TriggerState::resetTriggerState() { isFirstEvent = true; } -void TriggerState::setTriggerErrorState() { +void TriggerDecoderBase::setTriggerErrorState() { lastDecodingErrorTime = getTimeNowNt(); someSortOfTriggerError = true; } -void TriggerState::resetCurrentCycleState() { +void TriggerDecoderBase::resetCurrentCycleState() { memset(currentCycle.eventCount, 0, sizeof(currentCycle.eventCount)); memset(currentCycle.timeOfPreviousEventNt, 0, sizeof(currentCycle.timeOfPreviousEventNt)); #if EFI_UNIT_TEST @@ -99,7 +99,7 @@ void TriggerState::resetCurrentCycleState() { #if EFI_SHAFT_POSITION_INPUT -TriggerStateWithRunningStatistics::TriggerStateWithRunningStatistics() : +PrimaryTriggerDecoder::PrimaryTriggerDecoder() : //https://en.cppreference.com/w/cpp/language/zero_initialization timeOfLastEvent(), instantRpmValue() { @@ -111,7 +111,7 @@ bool printTriggerTrace = false; float actualSynchGap; #endif /* ! EFI_PROD_CODE */ -void TriggerWaveform::initializeSyncPoint(TriggerState& state, +void TriggerWaveform::initializeSyncPoint(TriggerDecoderBase& state, const TriggerConfiguration& triggerConfiguration, const trigger_config_s& triggerConfig) { triggerShapeSynchPointIndex = state.findTriggerZeroEventIndex(*this, @@ -119,11 +119,11 @@ void TriggerWaveform::initializeSyncPoint(TriggerState& state, } /** - * Calculate 'shape.triggerShapeSynchPointIndex' value using 'TriggerState *state' + * Calculate 'shape.triggerShapeSynchPointIndex' value using 'TriggerDecoderBase *state' */ void calculateTriggerSynchPoint( TriggerWaveform& shape, - TriggerState& state) { + TriggerDecoderBase& state) { state.resetTriggerState(); #if EFI_PROD_CODE @@ -209,16 +209,16 @@ void prepareEventAngles(TriggerWaveform *shape, } } -int64_t TriggerState::getTotalEventCounter() const { +int64_t TriggerDecoderBase::getTotalEventCounter() const { return totalEventCountBase + currentCycle.current_index; } -int TriggerState::getTotalRevolutionCounter() const { +int TriggerDecoderBase::getTotalRevolutionCounter() const { return totalRevolutionCounter; } -void TriggerStateWithRunningStatistics::resetTriggerState() { - TriggerState::resetTriggerState(); +void PrimaryTriggerDecoder::resetTriggerState() { + TriggerDecoderBase::resetTriggerState(); memset(timeOfLastEvent, 0, sizeof(timeOfLastEvent)); memset(spinningEvents, 0, sizeof(spinningEvents)); @@ -229,7 +229,7 @@ void TriggerStateWithRunningStatistics::resetTriggerState() { m_hasSynchronizedPhase = false; } -void TriggerStateWithRunningStatistics::movePreSynchTimestamps() { +void PrimaryTriggerDecoder::movePreSynchTimestamps() { // here we take timestamps of events which happened prior to synchronization and place them // at appropriate locations auto triggerSize = getTriggerSize(); @@ -252,13 +252,10 @@ void TriggerStateWithRunningStatistics::movePreSynchTimestamps() { memcpy(timeOfLastEvent + firstDst, spinningEvents + firstSrc, eventsToCopy * sizeof(timeOfLastEvent[0])); } -float TriggerStateWithRunningStatistics::calculateInstantRpm( +float PrimaryTriggerDecoder::calculateInstantRpm( TriggerWaveform const & triggerShape, TriggerFormDetails *triggerFormDetails, uint32_t current_index, efitick_t nowNt) { - /** - * todo: Martin has this fatal error while feeding external RPM and changing trigger mode from 4 stoke cam to 4 stroke symmetrical - */ assertIsInBoundsWithResult(current_index, timeOfLastEvent, "calc timeOfLastEvent", 0); // Record the time of this event so we can calculate RPM from it later @@ -309,7 +306,7 @@ float TriggerStateWithRunningStatistics::calculateInstantRpm( return instantRpm; } -void TriggerStateWithRunningStatistics::setLastEventTimeForInstantRpm(efitick_t nowNt) { +void PrimaryTriggerDecoder::setLastEventTimeForInstantRpm(efitick_t nowNt) { if (getShaftSynchronized()) { return; } @@ -323,7 +320,7 @@ void TriggerStateWithRunningStatistics::setLastEventTimeForInstantRpm(efitick_t spinningEvents[spinningEventIndex++] = nowNt; } -void TriggerStateWithRunningStatistics::updateInstantRpm( +void PrimaryTriggerDecoder::updateInstantRpm( TriggerWaveform const & triggerShape, TriggerFormDetails *triggerFormDetails, uint32_t index, efitick_t nowNt) { @@ -343,7 +340,7 @@ void TriggerStateWithRunningStatistics::updateInstantRpm( #endif /* EFI_SENSOR_CHART */ } -bool TriggerState::isValidIndex(const TriggerWaveform& triggerShape) const { +bool TriggerDecoderBase::isValidIndex(const TriggerWaveform& triggerShape) const { return currentCycle.current_index < triggerShape.getSize(); } @@ -379,7 +376,7 @@ static trigger_value_e eventType[6] = { TV_FALL, TV_RISE, TV_FALL, TV_RISE, TV_F #define needToSkipFall(type) ((!triggerShape.gapBothDirections) && (( triggerShape.useRiseEdge) && (type != TV_RISE))) #define needToSkipRise(type) ((!triggerShape.gapBothDirections) && ((!triggerShape.useRiseEdge) && (type != TV_FALL))) -int TriggerState::getCurrentIndex() const { +int TriggerDecoderBase::getCurrentIndex() const { return currentCycle.current_index; } @@ -394,7 +391,7 @@ void TriggerCentral::validateCamVvtCounters() { } } -angle_t TriggerStateWithRunningStatistics::syncEnginePhase(int divider, int remainder, angle_t engineCycle) { +angle_t PrimaryTriggerDecoder::syncEnginePhase(int divider, int remainder, angle_t engineCycle) { efiAssert(OBD_PCM_Processor_Fault, remainder < divider, "syncEnginePhase", false); angle_t totalShift = 0; while (getTotalRevolutionCounter() % divider != remainder) { @@ -417,11 +414,11 @@ angle_t TriggerStateWithRunningStatistics::syncEnginePhase(int divider, int rema return totalShift; } -void TriggerState::incrementTotalEventCounter() { +void TriggerDecoderBase::incrementTotalEventCounter() { totalRevolutionCounter++; } -bool TriggerState::validateEventCounters(const TriggerWaveform& triggerShape) const { +bool TriggerDecoderBase::validateEventCounters(const TriggerWaveform& triggerShape) const { bool isDecodingError = false; for (int i = 0;i < PWM_PHASE_MAX_WAVE_PER_PWM;i++) { isDecodingError |= (currentCycle.eventCount[i] != triggerShape.getExpectedEventCount(i)); @@ -440,7 +437,7 @@ bool TriggerState::validateEventCounters(const TriggerWaveform& triggerShape) co return isDecodingError; } -void TriggerState::onShaftSynchronization( +void TriggerDecoderBase::onShaftSynchronization( const TriggerStateCallback triggerCycleCallback, bool wasSynchronized, const efitick_t nowNt, @@ -480,7 +477,7 @@ void TriggerState::onShaftSynchronization( * @param signal type of event which just happened * @param nowNt current time */ -void TriggerState::decodeTriggerEvent( +void TriggerDecoderBase::decodeTriggerEvent( const char *msg, const TriggerWaveform& triggerShape, const TriggerStateCallback triggerCycleCallback, @@ -708,7 +705,7 @@ void TriggerState::decodeTriggerEvent( } } -bool TriggerState::isSyncPoint(const TriggerWaveform& triggerShape, trigger_type_e triggerType) const { +bool TriggerDecoderBase::isSyncPoint(const TriggerWaveform& triggerShape, trigger_type_e triggerType) const { // Miata NB needs a special decoder. // The problem is that the crank wheel only has 4 teeth, also symmetrical, so the pattern // is long-short-long-short for one crank rotation. @@ -760,7 +757,7 @@ bool TriggerState::isSyncPoint(const TriggerWaveform& triggerShape, trigger_type return true; } -static void onFindIndexCallback(TriggerState *state) { +static void onFindIndexCallback(TriggerDecoderBase *state) { for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) { // todo: that's not the best place for this intermediate data storage, fix it! state->expectedTotalTime[i] = state->currentCycle.totalTimeNt[i]; @@ -773,7 +770,7 @@ static void onFindIndexCallback(TriggerState *state) { * * This function finds the index of synchronization event within TriggerWaveform */ -uint32_t TriggerState::findTriggerZeroEventIndex( +uint32_t TriggerDecoderBase::findTriggerZeroEventIndex( TriggerWaveform& shape, const TriggerConfiguration& triggerConfiguration, const trigger_config_s& triggerConfig) { diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index be4769253c..d5c1b92ae1 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -13,7 +13,7 @@ #include "trigger_state_generated.h" #include "timer.h" -class TriggerState; +class TriggerDecoderBase; struct TriggerStateListener { #if EFI_SHAFT_POSITION_INPUT @@ -40,7 +40,7 @@ protected: virtual trigger_type_e getType() const = 0; }; -typedef void (*TriggerStateCallback)(TriggerState *); +typedef void (*TriggerStateCallback)(TriggerDecoderBase*); typedef struct { /** @@ -74,9 +74,9 @@ typedef struct { /** * @see TriggerWaveform for trigger wheel shape definition */ -class TriggerState : public trigger_state_s { +class TriggerDecoderBase : public trigger_state_s { public: - TriggerState(); + TriggerDecoderBase(); /** * current trigger processing index, between zero and #size */ @@ -173,9 +173,9 @@ private: /** * the reason for sub-class is simply to save RAM but not having statistics in the trigger initialization instance */ -class TriggerStateWithRunningStatistics : public TriggerState { +class PrimaryTriggerDecoder : public TriggerDecoderBase { public: - TriggerStateWithRunningStatistics(); + PrimaryTriggerDecoder(); void resetTriggerState() override; angle_t syncEnginePhase(int divider, int remainder, angle_t engineCycle); @@ -231,12 +231,14 @@ private: bool m_hasSynchronizedPhase = false; }; +class VvtTriggerDecoder : public TriggerDecoderBase { }; + angle_t getEngineCycle(operation_mode_e operationMode); class Engine; void calculateTriggerSynchPoint( TriggerWaveform& shape, - TriggerState& state); + TriggerDecoderBase& state); void prepareEventAngles(TriggerWaveform *shape, TriggerFormDetails *details); diff --git a/firmware/controllers/trigger/trigger_simulator.cpp b/firmware/controllers/trigger/trigger_simulator.cpp index 63345d4555..b1a0724a27 100644 --- a/firmware/controllers/trigger/trigger_simulator.cpp +++ b/firmware/controllers/trigger/trigger_simulator.cpp @@ -39,7 +39,7 @@ int getSimulatedEventTime(const TriggerWaveform& shape, int i) { void TriggerStimulatorHelper::feedSimulatedEvent( const TriggerStateCallback triggerCycleCallback, const TriggerConfiguration& triggerConfiguration, - TriggerState& state, + TriggerDecoderBase& state, const TriggerWaveform& shape, int i ) { @@ -97,7 +97,7 @@ void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle( const TriggerStateCallback triggerCycleCallback, const TriggerConfiguration& triggerConfiguration, const uint32_t syncIndex, - TriggerState& state, + TriggerDecoderBase& state, TriggerWaveform& shape ) { @@ -141,7 +141,7 @@ void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle( uint32_t TriggerStimulatorHelper::findTriggerSyncPoint( TriggerWaveform& shape, const TriggerConfiguration& triggerConfiguration, - TriggerState& state) { + TriggerDecoderBase& state) { for (int i = 0; i < 4 * PWM_PHASE_MAX_COUNT; i++) { feedSimulatedEvent(nullptr, triggerConfiguration, diff --git a/firmware/controllers/trigger/trigger_simulator.h b/firmware/controllers/trigger/trigger_simulator.h index 14db33cdcf..8469e2be0b 100644 --- a/firmware/controllers/trigger/trigger_simulator.h +++ b/firmware/controllers/trigger/trigger_simulator.h @@ -20,20 +20,20 @@ public: uint32_t findTriggerSyncPoint( TriggerWaveform& shape, const TriggerConfiguration& triggerConfiguration, - TriggerState& state); + TriggerDecoderBase& state); void assertSyncPositionAndSetDutyCycle( const TriggerStateCallback triggerCycleCallback, const TriggerConfiguration& triggerConfiguration, const uint32_t index, - TriggerState& state, + TriggerDecoderBase& state, TriggerWaveform& shape ); // send next event so that we can see how state reacts void feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback, const TriggerConfiguration& triggerConfiguration, - TriggerState& state, + TriggerDecoderBase& state, const TriggerWaveform& shape, int i); }; diff --git a/unit_tests/tests/trigger/test_trigger_decoder.cpp b/unit_tests/tests/trigger/test_trigger_decoder.cpp index e43e890fef..1c41ea7ba0 100644 --- a/unit_tests/tests/trigger/test_trigger_decoder.cpp +++ b/unit_tests/tests/trigger/test_trigger_decoder.cpp @@ -65,7 +65,7 @@ static void testDodgeNeonDecoder() { TriggerWaveform * shape = ð.engine.triggerCentral.triggerShape; ASSERT_EQ(8, shape->getTriggerWaveformSynchPointIndex()); - TriggerState state; + TriggerDecoderBase state; ASSERT_FALSE(state.getShaftSynchronized()) << "1 shaft_is_synchronized"; @@ -112,8 +112,8 @@ static void assertTriggerPosition(event_trigger_position_s *position, int eventI TEST(trigger, testSomethingWeird) { EngineTestHelper eth(FORD_INLINE_6_1995); - TriggerState state_; - TriggerState *sta = &state_; + TriggerDecoderBase state_; + TriggerDecoderBase *sta = &state_; const auto& triggerConfiguration = engine->primaryTriggerConfiguration;