refactoring trigger simulation

This commit is contained in:
rusefi 2019-03-04 01:10:31 -05:00
parent 8decc09edf
commit 84c1b3581d
4 changed files with 40 additions and 32 deletions

View File

@ -70,6 +70,10 @@ void MultiWave::checkSwitchTimes(const int size) {
}
pin_state_t MultiWave::getChannelState(const int channelIndex, const int phaseIndex) const {
if (channelIndex >= waveCount) {
// todo: would be nice to get this asserting working
//firmwareError(OBD_PCM_Processor_Fault, "channel index %d/%d", channelIndex, waveCount);
}
return channels[channelIndex].pinStates[phaseIndex];
}

View File

@ -14,11 +14,19 @@
* @author Andrey Belomutskiy, (c) 2012-2018
*/
#include "global.h"
#include "EfiWave.h"
int getPreviousIndex(const int currentIndex, const int size) {
return (currentIndex + size - 1) % size;
}
bool needEvent(const int currentIndex, const int size, MultiWave *multiWave, int channelIndex) {
int prevIndex = getPreviousIndex(currentIndex, size);
pin_state_t previousValue = multiWave->getChannelState(channelIndex, /*phaseIndex*/prevIndex);
pin_state_t currentValue = multiWave->getChannelState(channelIndex, /*phaseIndex*/currentIndex);
return previousValue != currentValue;
}
#if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__)
#include "engine.h"
@ -36,29 +44,22 @@ EXTERN_ENGINE
;
void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) {
int prevIndex = getPreviousIndex(stateIndex, state->phaseCount);
pin_state_t primaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/prevIndex);
pin_state_t newPrimaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/stateIndex);
pin_state_t secondaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 1, /*phaseIndex*/prevIndex);
pin_state_t newSecondaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 1, /*phaseIndex*/stateIndex);
pin_state_t thirdWheelState = state->multiWave.getChannelState(/* channelIndex*/ 2, /*phaseIndex*/prevIndex);
pin_state_t new3rdWheelState = state->multiWave.getChannelState(/* channelIndex*/ 2, /*phaseIndex*/stateIndex);
// todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent?
MultiWave *multiWave = &state->multiWave;
if (primaryWheelState != newPrimaryWheelState) {
hwHandleShaftSignal(primaryWheelState ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING);
if (needEvent(stateIndex, state->phaseCount, &state->multiWave, 0)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/0, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING);
}
if (secondaryWheelState != newSecondaryWheelState) {
hwHandleShaftSignal(secondaryWheelState ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING);
if (needEvent(stateIndex, state->phaseCount, &state->multiWave, 1)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/1, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING);
}
if (thirdWheelState != new3rdWheelState) {
hwHandleShaftSignal(thirdWheelState ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING);
if (needEvent(stateIndex, state->phaseCount, &state->multiWave, 2)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/2, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING);
}
// print("hello %d\r\n", chTimeNow());

View File

@ -20,5 +20,6 @@ public:
void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX);
int getPreviousIndex(const int currentIndex, const int size);
bool needEvent(const int currentIndex, const int size, MultiWave *multiWave, int channelIndex);
#endif /* TRIGGER_EMULATOR_ALGO_H_ */

View File

@ -33,6 +33,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerSha
DECLARE_ENGINE_PARAMETER_SUFFIX) {
efiAssertVoid(CUSTOM_ERR_6593, shape->getSize() > 0, "size not zero");
int stateIndex = i % shape->getSize();
int size = shape->getSize();
int prevIndex = getPreviousIndex(stateIndex, shape->getSize());
@ -40,14 +41,15 @@ void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerSha
int time = (int) (SIMULATION_CYCLE_PERIOD * (loopIndex + shape->wave.getSwitchTime(stateIndex)));
pin_state_t primaryWheelState = shape->wave.getChannelState(0, prevIndex);
pin_state_t newPrimaryWheelState = shape->wave.getChannelState(0, stateIndex);
MultiWave *multiWave = &shape->wave;
pin_state_t primaryWheelState = multiWave->getChannelState(0, prevIndex);
pin_state_t newPrimaryWheelState = multiWave->getChannelState(0, stateIndex);
pin_state_t secondaryWheelState = shape->wave.getChannelState(1, prevIndex);
pin_state_t newSecondaryWheelState = shape->wave.getChannelState(1, stateIndex);
pin_state_t secondaryWheelState = multiWave->getChannelState(1, prevIndex);
pin_state_t newSecondaryWheelState = multiWave->getChannelState(1, stateIndex);
pin_state_t thirdWheelState = shape->wave.getChannelState(2, prevIndex);
pin_state_t new3rdWheelState = shape->wave.getChannelState(2, stateIndex);
pin_state_t thirdWheelState = multiWave->getChannelState(2, prevIndex);
pin_state_t new3rdWheelState = multiWave->getChannelState(2, stateIndex);
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
if (printTriggerDebug) {
@ -59,23 +61,23 @@ void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerSha
// todo: code duplication with TriggerEmulatorHelper::handleEmulatorCallback?
if (primaryWheelState != newPrimaryWheelState) {
primaryWheelState = newPrimaryWheelState;
trigger_event_e s = primaryWheelState ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING;
if (needEvent(stateIndex, size, multiWave, 0)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/0, stateIndex);
trigger_event_e s = currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING;
if (isUsefulSignal(s, engineConfiguration))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
}
if (secondaryWheelState != newSecondaryWheelState) {
secondaryWheelState = newSecondaryWheelState;
trigger_event_e s = secondaryWheelState ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING;
if (needEvent(stateIndex, size, multiWave, 1)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/1, stateIndex);
trigger_event_e s = currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING;
if (isUsefulSignal(s, engineConfiguration))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
}
if (thirdWheelState != new3rdWheelState) {
thirdWheelState = new3rdWheelState;
trigger_event_e s = thirdWheelState ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING;
if (needEvent(stateIndex, size, multiWave, 2)) {
pin_state_t currentValue = multiWave->getChannelState(/*phaseIndex*/2, stateIndex);
trigger_event_e s = currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING;
if (isUsefulSignal(s, engineConfiguration))
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER_SUFFIX);
}