rusefi-1/firmware/controllers/trigger/trigger_emulator_algo.cpp

183 lines
6.4 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file trigger_emulator_algo.cpp
*
2015-09-23 18:02:33 -07:00
* This file is about producing real electrical signals which emulate trigger signal based on
* a known TriggerShape.
*
* Historically this implementation was implemented based on PwmConfig which is maybe not the
* best way to implement it. (todo: why is not the best way?)
*
* A newer implementation of pretty much the same thing is TriggerStimulatorHelper
* todo: one emulator should be enough! another one should be eliminated
*
2015-07-10 06:01:56 -07:00
* @date Mar 3, 2014
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
#include "../core/state_sequence.h"
2018-09-16 19:26:57 -07:00
#include "global.h"
2019-10-08 00:14:21 -07:00
#include "efi_gpio.h"
2015-07-10 06:01:56 -07:00
2019-03-03 21:40:22 -08:00
int getPreviousIndex(const int currentIndex, const int size) {
return (currentIndex + size - 1) % size;
}
bool needEvent(const int currentIndex, const int size, MultiChannelStateSequence *multiChannelStateSequence, int channelIndex) {
2019-03-03 22:10:31 -08:00
int prevIndex = getPreviousIndex(currentIndex, size);
pin_state_t previousValue = multiChannelStateSequence->getChannelState(channelIndex, /*phaseIndex*/prevIndex);
pin_state_t currentValue = multiChannelStateSequence->getChannelState(channelIndex, /*phaseIndex*/currentIndex);
2019-03-03 22:10:31 -08:00
return previousValue != currentValue;
}
2019-04-12 19:07:03 -07:00
#if EFI_EMULATE_POSITION_SENSORS
2015-07-10 06:01:56 -07:00
2019-01-20 21:10:09 -08:00
#include "engine.h"
2015-07-10 06:01:56 -07:00
#include "trigger_emulator_algo.h"
#include "engine_configuration.h"
#include "trigger_central.h"
2017-03-03 21:17:53 -08:00
#include "trigger_simulator.h"
2015-07-10 06:01:56 -07:00
#include "pwm_generator.h"
TriggerEmulatorHelper::TriggerEmulatorHelper() {
}
EXTERN_ENGINE
;
void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) {
2018-02-05 14:41:05 -08:00
// todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent?
MultiChannelStateSequence *multiChannelStateSequence = &state->multiChannelStateSequence;
2017-03-03 21:08:56 -08:00
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 0)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/0, stateIndex);
2019-03-03 22:10:31 -08:00
hwHandleShaftSignal(currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING);
2015-07-10 06:01:56 -07:00
}
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 1)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/1, stateIndex);
2019-03-03 22:10:31 -08:00
hwHandleShaftSignal(currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING);
2015-07-10 06:01:56 -07:00
}
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 2)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/2, stateIndex);
2019-03-03 22:10:31 -08:00
hwHandleShaftSignal(currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING);
2015-07-10 06:01:56 -07:00
}
// print("hello %d\r\n", chTimeNow());
}
/*
* todo: should we simply re-use instances used by trigger_decoder?
* todo: since we are emulating same shape we are decoding
*/
static pin_state_t pinStates1[PWM_PHASE_MAX_COUNT];
static pin_state_t pinStates2[PWM_PHASE_MAX_COUNT];
static pin_state_t pinStates3[PWM_PHASE_MAX_COUNT];
static SingleChannelStateSequence waves[PWM_PHASE_MAX_WAVE_PER_PWM] = { SingleChannelStateSequence(pinStates1), SingleChannelStateSequence(pinStates2),
SingleChannelStateSequence(pinStates3) };
static SingleChannelStateSequence sr[PWM_PHASE_MAX_WAVE_PER_PWM] = { waves[0], waves[1], waves[2] };
2015-07-10 06:01:56 -07:00
2017-01-06 08:02:49 -08:00
static float switchTimesBuffer[PWM_PHASE_MAX_COUNT];
2015-07-10 06:01:56 -07:00
2017-01-06 08:02:49 -08:00
PwmConfig triggerSignal(switchTimesBuffer, sr);
2015-07-10 06:01:56 -07:00
#define DO_NOT_STOP 999999999
static int stopEmulationAtIndex = DO_NOT_STOP;
static bool isEmulating = true;
static Logging *logger;
2018-02-03 14:08:07 -08:00
static int atTriggerVersion = 0;
2015-07-10 06:01:56 -07:00
2015-07-15 18:01:45 -07:00
#if EFI_ENGINE_SNIFFER
#include "engine_sniffer.h"
2015-07-10 06:01:56 -07:00
extern WaveChart waveChart;
2015-07-15 18:01:45 -07:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
2017-05-15 20:28:49 -07:00
void setTriggerEmulatorRPM(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
2015-07-10 06:01:56 -07:00
engineConfiguration->bc.triggerSimulatorFrequency = rpm;
/**
* All we need to do here is to change the periodMs
* togglePwmState() would see that the periodMs has changed and act accordingly
*/
if (rpm == 0) {
2018-01-21 12:31:59 -08:00
triggerSignal.setFrequency(NAN);
2015-07-10 06:01:56 -07:00
} else {
float rpmM = getRpmMultiplier(engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE));
float rPerSecond = rpm * rpmM / 60.0; // per minute converted to per second
triggerSignal.setFrequency(rPerSecond);
2015-07-10 06:01:56 -07:00
}
2015-07-15 18:01:45 -07:00
#if EFI_ENGINE_SNIFFER
2015-07-10 06:01:56 -07:00
if (engine->isTestMode)
2016-01-30 19:03:36 -08:00
waveChart.reset();
2015-07-15 18:01:45 -07:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "Emulating position sensor(s). RPM=%d", rpm);
}
static void updateTriggerShapeIfNeeded(PwmConfig *state) {
2018-02-03 14:08:07 -08:00
if (atTriggerVersion < engine->triggerCentral.triggerShape.version) {
atTriggerVersion = engine->triggerCentral.triggerShape.version;
scheduleMsg(logger, "Stimulator: updating trigger shape: %d/%d %d", atTriggerVersion,
engine->getGlobalConfigurationVersion(), currentTimeMillis());
2015-07-10 06:01:56 -07:00
2017-03-01 19:18:25 -08:00
TriggerShape *s = &engine->triggerCentral.triggerShape;
2018-12-25 07:13:00 -08:00
pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = {
s->wave.channels[0].pinStates,
s->wave.channels[1].pinStates,
s->wave.channels[2].pinStates };
2015-07-10 06:01:56 -07:00
copyPwmParameters(state, s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, pinStates);
state->safe.periodNt = -1; // this would cause loop re-initialization
}
}
static TriggerEmulatorHelper helper;
static void emulatorApplyPinState(int stateIndex, PwmConfig *state) /* pwm_gen_callback */ {
2015-07-10 06:01:56 -07:00
if (stopEmulationAtIndex == stateIndex) {
isEmulating = false;
}
if (!isEmulating) {
return;
}
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
applyPinState(stateIndex, state);
2015-07-10 06:01:56 -07:00
#endif /* EFI_PROD_CODE */
if (engineConfiguration->directSelfStimulation) {
/**
* this callback would invoke the input signal handlers directly
*/
helper.handleEmulatorCallback(state, stateIndex);
}
}
2017-05-15 05:51:40 -07:00
static void setEmulatorAtIndex(int index) {
2015-07-10 06:01:56 -07:00
stopEmulationAtIndex = index;
}
2017-05-15 05:51:40 -07:00
static void resumeStimulator() {
2015-07-10 06:01:56 -07:00
isEmulating = true;
stopEmulationAtIndex = DO_NOT_STOP;
}
void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
2015-07-10 06:01:56 -07:00
logger = sharedLogger;
2017-03-01 19:18:25 -08:00
TriggerShape *s = &engine->triggerCentral.triggerShape;
2017-05-15 20:28:49 -07:00
setTriggerEmulatorRPM(engineConfiguration->bc.triggerSimulatorFrequency PASS_ENGINE_PARAMETER_SUFFIX);
2018-12-25 07:13:00 -08:00
pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = {
s->wave.channels[0].pinStates,
s->wave.channels[1].pinStates,
s->wave.channels[2].pinStates };
triggerSignal.weComplexInit("position sensor",
&engine->executor,
s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM,
pinStates, updateTriggerShapeIfNeeded, (pwm_gen_callback*)emulatorApplyPinState);
2015-07-10 06:01:56 -07:00
2017-05-15 05:51:40 -07:00
addConsoleActionI("rpm", setTriggerEmulatorRPM);
addConsoleActionI("stop_stimulator_at_index", setEmulatorAtIndex);
addConsoleAction("resume_stimulator", resumeStimulator);
2015-07-10 06:01:56 -07:00
}
2017-05-15 05:51:40 -07:00
#endif /* EFI_EMULATE_POSITION_SENSORS */