rusefi/firmware/controllers/trigger/trigger_simulator.cpp

98 lines
3.6 KiB
C++
Raw Normal View History

2015-09-23 18:02:33 -07:00
/*
* @file trigger_simulator.cpp
*
* @date Sep 23, 2015
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-09-23 18:02:33 -07:00
*/
#include "main.h"
#include "engine.h"
#include "trigger_simulator.h"
2017-03-03 21:17:53 -08:00
#include "trigger_emulator_algo.h"
2015-09-23 18:02:33 -07:00
#define SIMULATION_CYCLE_PERIOD 720000
EXTERN_ENGINE;
TriggerStimulatorHelper::TriggerStimulatorHelper() {
}
2017-03-03 21:17:53 -08:00
// this is not the only place where we have 'isUpEvent'. todo: reuse
static bool isRisingEdge[6] = { false, true, false, true, false, true };
bool isUsefulSignal(trigger_event_e signal, engine_configuration_s *engineConfiguration) {
return !engineConfiguration->useOnlyRisingEdgeForTrigger || isRisingEdge[(int) signal];
}
2015-09-23 18:02:33 -07:00
void TriggerStimulatorHelper::nextStep(TriggerState *state, TriggerShape * shape, int i,
trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_S) {
int stateIndex = i % shape->getSize();
int prevIndex = (stateIndex + shape->getSize() - 1 ) % shape->getSize();
int loopIndex = i / shape->getSize();
int time = (int) (SIMULATION_CYCLE_PERIOD * (loopIndex + shape->wave.getSwitchTime(stateIndex)));
2016-01-11 14:01:33 -08:00
bool primaryWheelState = shape->wave.getChannelState(0, prevIndex);
bool newPrimaryWheelState = shape->wave.getChannelState(0, stateIndex);
2015-09-23 18:02:33 -07:00
2016-01-11 14:01:33 -08:00
bool secondaryWheelState = shape->wave.getChannelState(1, prevIndex);
bool newSecondaryWheelState = shape->wave.getChannelState(1, stateIndex);
2015-09-23 18:02:33 -07:00
2016-01-11 14:01:33 -08:00
bool thirdWheelState = shape->wave.getChannelState(2, prevIndex);
bool new3rdWheelState = shape->wave.getChannelState(2, stateIndex);
2015-09-23 18:02:33 -07:00
2017-03-03 21:08:56 -08:00
// todo: code duplication with TriggerEmulatorHelper::handleEmulatorCallback?
2015-09-23 18:02:33 -07:00
if (primaryWheelState != newPrimaryWheelState) {
primaryWheelState = newPrimaryWheelState;
2016-02-27 20:03:34 -08:00
trigger_event_e s = primaryWheelState ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING;
2015-09-23 18:02:33 -07:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
}
if (secondaryWheelState != newSecondaryWheelState) {
secondaryWheelState = newSecondaryWheelState;
2016-02-27 20:03:34 -08:00
trigger_event_e s = secondaryWheelState ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING;
2015-09-23 18:02:33 -07:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
}
if (thirdWheelState != new3rdWheelState) {
thirdWheelState = new3rdWheelState;
2016-02-27 20:03:34 -08:00
trigger_event_e s = thirdWheelState ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING;
2015-09-23 18:02:33 -07:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
}
}
void TriggerStimulatorHelper::assertSyncPositionAndSetDutyCycle(uint32_t index, TriggerState *state, TriggerShape * shape,
trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_S) {
2016-02-27 20:03:34 -08:00
int startIndex = engineConfiguration->useOnlyRisingEdgeForTrigger ? index + 2 : index + 1;
2015-09-23 18:02:33 -07:00
for (uint32_t i = startIndex; i <= index + 2 * shape->getSize(); i++) {
nextStep(state, shape, i, triggerConfig PASS_ENGINE_PARAMETER);
}
2017-03-04 05:35:22 -08:00
int revolutionCounter = state->getTotalRevolutionCounter();
if (revolutionCounter != 3) {
warning(CUSTOM_OBD_TRIGGER_SHAPE, "sync failed/wrong gap parameters trigger=%s rc=%d", getTrigger_type_e(engineConfiguration->trigger.type), revolutionCounter);
2017-03-01 19:37:10 -08:00
shape->shapeDefinitionError = true;
2016-01-24 15:01:56 -08:00
return;
}
2017-03-01 19:37:10 -08:00
shape->shapeDefinitionError = false;
2015-09-23 18:02:33 -07:00
for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) {
shape->dutyCycle[i] = 1.0 * state->expectedTotalTime[i] / SIMULATION_CYCLE_PERIOD;
}
}
uint32_t TriggerStimulatorHelper::doFindTrigger(TriggerShape * shape,
trigger_config_s const*triggerConfig, TriggerState *state DECLARE_ENGINE_PARAMETER_S) {
for (int i = 0; i < 4 * PWM_PHASE_MAX_COUNT; i++) {
nextStep(state, shape, i, triggerConfig PASS_ENGINE_PARAMETER);
if (state->shaft_is_synchronized)
return i;
}
2016-10-10 13:02:39 -07:00
firmwareError(CUSTOM_ERR_TRIGGER_SYNC, "findTriggerZeroEventIndex() failed");
2015-09-23 18:02:33 -07:00
return EFI_ERROR_CODE;
}