Refactor Trigger System #635

injecting callback via parameters via magic and potentially broken field manipulation
This commit is contained in:
rusefi 2019-12-06 01:05:19 -05:00
parent 28a12d9d04
commit 717abd6b67
6 changed files with 35 additions and 36 deletions

View File

@ -364,7 +364,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal DECLARE_ENGINE_PAR
/**
* This invocation changes the state of triggerState
*/
triggerState.decodeTriggerEvent(signal, nowNt PASS_ENGINE_PARAMETER_SUFFIX);
triggerState.decodeTriggerEvent(nullptr, signal, nowNt PASS_ENGINE_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

@ -59,7 +59,6 @@ void TriggerState::setShaftSynchronized(bool value) {
}
void TriggerState::resetTriggerState() {
triggerCycleCallback = nullptr;
setShaftSynchronized(false);
toothed_previous_time = 0;
@ -385,7 +384,8 @@ void TriggerState::handleTriggerError(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
}
}
void TriggerState::onShaftSynchronization(efitime_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX) {
void TriggerState::onShaftSynchronization(const TriggerStateCallback triggerCycleCallback,
efitime_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX) {
setShaftSynchronized(true);
// this call would update duty cycle values
nextTriggerEvent()
@ -416,7 +416,8 @@ void TriggerState::onShaftSynchronization(efitime_t nowNt, trigger_wheel_e trigg
* @param signal type of event which just happened
* @param nowNt current time
*/
void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCallback,
trigger_event_e const signal, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
ScopePerf perf(PE::DecodeTriggerEvent, static_cast<uint8_t>(signal));
bool useOnlyRisingEdgeForTrigger = CONFIG(useOnlyRisingEdgeForTrigger);
@ -660,7 +661,7 @@ void TriggerState::decodeTriggerEvent(trigger_event_e const signal, efitime_t no
}
}
onShaftSynchronization(nowNt, triggerWheel PASS_ENGINE_PARAMETER_SUFFIX);
onShaftSynchronization(triggerCycleCallback, nowNt, triggerWheel PASS_ENGINE_PARAMETER_SUFFIX);
} else { /* if (!isSynchronizationPoint) */
nextTriggerEvent()
@ -747,9 +748,8 @@ uint32_t findTriggerZeroEventIndex(TriggerState *state, TriggerShape * shape,
*
* todo: add a comment why are we doing '2 * shape->getSize()' here?
*/
state->triggerCycleCallback = onFindIndexCallback;
helper.assertSyncPositionAndSetDutyCycle(syncIndex, state, shape PASS_ENGINE_PARAMETER_SUFFIX);
helper.assertSyncPositionAndSetDutyCycle(onFindIndexCallback, syncIndex, state, shape PASS_ENGINE_PARAMETER_SUFFIX);
engine->isInitializingTrigger = false;
return syncIndex % shape->getSize();

View File

@ -58,10 +58,14 @@ public:
bool isEvenRevolution() const;
void incrementTotalEventCounter();
efitime_t getTotalEventCounter() const;
void decodeTriggerEvent(trigger_event_e const signal, efitime_t nowUs DECLARE_ENGINE_PARAMETER_SUFFIX);
void decodeTriggerEvent(const TriggerStateCallback triggerCycleCallback,
trigger_event_e const signal, efitime_t nowUs DECLARE_ENGINE_PARAMETER_SUFFIX);
bool validateEventCounters(DECLARE_ENGINE_PARAMETER_SIGNATURE) const;
void handleTriggerError(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void onShaftSynchronization(efitime_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX);
void onShaftSynchronization(const TriggerStateCallback triggerCycleCallback,
efitime_t nowNt, trigger_wheel_e triggerWheel DECLARE_ENGINE_PARAMETER_SUFFIX);
/**
* Resets synchronization flag and alerts rpm_calculator to reset engine spinning flag.
*/
@ -69,7 +73,6 @@ public:
bool isValidIndex(DECLARE_ENGINE_PARAMETER_SIGNATURE) const;
float getTriggerDutyCycle(int index);
TriggerStateCallback triggerCycleCallback;
/**
* TRUE if we know where we are

View File

@ -14,9 +14,6 @@
EXTERN_ENGINE;
TriggerStimulatorHelper::TriggerStimulatorHelper() {
}
// this is not the only place where we have 'isUpEvent'. todo: reuse
static const bool isRisingEdge[6] = { false, true, false, true, false, true };
@ -29,7 +26,8 @@ bool isUsefulSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_SUFFIX) {
extern bool printTriggerDebug;
#endif /* ! EFI_UNIT_TEST */
void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerShape * shape, int i
void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback,
TriggerState *state, TriggerShape * shape, int i
DECLARE_ENGINE_PARAMETER_SUFFIX) {
efiAssertVoid(CUSTOM_ERR_6593, shape->getSize() > 0, "size not zero");
int stateIndex = i % shape->getSize();
@ -66,32 +64,33 @@ void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerSha
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/0, stateIndex);
trigger_event_e s = currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING;
if (isUsefulSignal(s PASS_ENGINE_PARAMETER_SUFFIX))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
state->decodeTriggerEvent(triggerCycleCallback, s, time PASS_ENGINE_PARAMETER_SUFFIX);
}
if (needEvent(stateIndex, size, multiChannelStateSequence, 1)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/1, stateIndex);
trigger_event_e s = currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING;
if (isUsefulSignal(s PASS_ENGINE_PARAMETER_SUFFIX))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
state->decodeTriggerEvent(triggerCycleCallback, s, time PASS_ENGINE_PARAMETER_SUFFIX);
}
if (needEvent(stateIndex, size, multiChannelStateSequence, 2)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/2, stateIndex);
trigger_event_e s = currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING;
if (isUsefulSignal(s PASS_ENGINE_PARAMETER_SUFFIX))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
state->decodeTriggerEvent(triggerCycleCallback, s, time PASS_ENGINE_PARAMETER_SUFFIX);
}
}
void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const uint32_t syncIndex, TriggerState *state, TriggerShape * shape
void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const TriggerStateCallback triggerCycleCallback,
const uint32_t syncIndex, TriggerState *state, TriggerShape * shape
DECLARE_ENGINE_PARAMETER_SUFFIX) {
/**
* 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(state, shape, i PASS_ENGINE_PARAMETER_SUFFIX);
feedSimulatedEvent(triggerCycleCallback, state, shape, i PASS_ENGINE_PARAMETER_SUFFIX);
}
int revolutionCounter = state->getTotalRevolutionCounter();
if (revolutionCounter != GAP_TRACKING_LENGTH + 1) {
@ -112,7 +111,7 @@ void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(const uint32_t s
uint32_t TriggerStimulatorHelper::findTriggerSyncPoint(TriggerShape * shape,
TriggerState *state DECLARE_ENGINE_PARAMETER_SUFFIX) {
for (int i = 0; i < 4 * PWM_PHASE_MAX_COUNT; i++) {
feedSimulatedEvent(state, shape, i PASS_ENGINE_PARAMETER_SUFFIX);
feedSimulatedEvent(nullptr, state, shape, i PASS_ENGINE_PARAMETER_SUFFIX);
if (state->shaft_is_synchronized)
return i;

View File

@ -3,29 +3,26 @@
* @brief This class knows how to produce synthetic shaft signals based on triggerShape
*
* @date Sep 23, 2015
* @author Andrey Belomutskiy, (c) 2012-2017
* @author Andrey Belomutskiy, (c) 2012-2019
*/
#ifndef CONTROLLERS_TRIGGER_TRIGGER_SIMULATOR_H_
#define CONTROLLERS_TRIGGER_TRIGGER_SIMULATOR_H_
#pragma once
#include "trigger_decoder.h"
class TriggerStimulatorHelper {
public:
TriggerStimulatorHelper();
uint32_t findTriggerSyncPoint(TriggerShape * shape,
TriggerState *state DECLARE_ENGINE_PARAMETER_SUFFIX);
void assertSyncPositionAndSetDutyCycle(const uint32_t index, TriggerState *state, TriggerShape * shape
void assertSyncPositionAndSetDutyCycle(const TriggerStateCallback triggerCycleCallback,
const uint32_t index, TriggerState *state, TriggerShape * shape
DECLARE_ENGINE_PARAMETER_SUFFIX);
private:
// send next event so that we can see how state reacts
void feedSimulatedEvent(TriggerState *state, TriggerShape * shape, int i DECLARE_ENGINE_PARAMETER_SUFFIX);
void feedSimulatedEvent(const TriggerStateCallback triggerCycleCallback, TriggerState *state, TriggerShape * shape, int i DECLARE_ENGINE_PARAMETER_SUFFIX);
};
bool isUsefulSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_SUFFIX);
#endif /* CONTROLLERS_TRIGGER_TRIGGER_SIMULATOR_H_ */

View File

@ -118,28 +118,28 @@ TEST(misc, testSomethingWeird) {
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized";
int r = 10;
sta->decodeTriggerEvent(SHAFT_PRIMARY_FALLING, r PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_FALLING, r PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized"; // still no synchronization
sta->decodeTriggerEvent(SHAFT_PRIMARY_RISING, ++r PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_RISING, ++r PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_TRUE(sta->shaft_is_synchronized); // first signal rise synchronize
ASSERT_EQ(0, sta->getCurrentIndex());
sta->decodeTriggerEvent(SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(1, sta->getCurrentIndex());
for (int i = 2; i < 10;) {
sta->decodeTriggerEvent(SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
assertEqualsM("even", i++, sta->getCurrentIndex());
sta->decodeTriggerEvent(SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
assertEqualsM("odd", i++, sta->getCurrentIndex());
}
sta->decodeTriggerEvent(SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(10, sta->getCurrentIndex());
sta->decodeTriggerEvent(SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_FALLING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(11, sta->getCurrentIndex());
sta->decodeTriggerEvent(SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
sta->decodeTriggerEvent(nullptr, SHAFT_PRIMARY_RISING, r++ PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(0, sta->getCurrentIndex()); // new revolution
}