trigger emulator refactoring (#1358)

* trigger gen refactoring

* missed one

* fix makefile

* commentary

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-04-24 11:00:06 -07:00 committed by GitHub
parent 3d35ef2f6f
commit a8f466c615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 104 deletions

View File

@ -102,7 +102,7 @@
#endif /* EFI_PROD_CODE */
#if EFI_EMULATE_POSITION_SENSORS
#include "trigger_emulator.h"
#include "trigger_emulator_algo.h"
#endif /* EFI_EMULATE_POSITION_SENSORS */
#if EFI_TUNER_STUDIO

View File

@ -25,7 +25,7 @@
#include "idle_thread.h"
#include "allsensors.h"
#include "alternator_controller.h"
#include "trigger_emulator.h"
#include "trigger_emulator_algo.h"
#include "sensor.h"
#if EFI_PROD_CODE

View File

@ -35,6 +35,7 @@ bool needEvent(const int currentIndex, const int size, MultiChannelStateSequence
#include "engine_configuration.h"
#include "trigger_central.h"
#include "trigger_simulator.h"
#include "settings.h"
#include "pwm_generator.h"
@ -43,28 +44,27 @@ TriggerEmulatorHelper::TriggerEmulatorHelper() {
EXTERN_ENGINE;
static OutputPin emulatorOutputs[3];
void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) {
efitick_t stamp = getTimeNowNt();
// todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent?
MultiChannelStateSequence *multiChannelStateSequence = &state->multiChannelStateSequence;
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 0)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/0, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING, stamp);
}
for (size_t i = 0; i < efi::size(emulatorOutputs); i++)
{
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, i)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/i, stateIndex);
constexpr trigger_event_e riseEvents[] = { SHAFT_PRIMARY_RISING, SHAFT_SECONDARY_RISING, SHAFT_3RD_RISING };
constexpr trigger_event_e fallEvents[] = { SHAFT_PRIMARY_FALLING, SHAFT_SECONDARY_FALLING, SHAFT_3RD_FALLING };
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 1)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/1, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING, stamp);
}
trigger_event_e event = (currentValue ? riseEvents : fallEvents)[i];
if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 2)) {
pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/2, stateIndex);
hwHandleShaftSignal(currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING, stamp);
hwHandleShaftSignal(event, stamp);
}
}
// print("hello %d\r\n", chTimeNow());
}
/*
@ -131,6 +131,7 @@ static void updateTriggerWaveformIfNeeded(PwmConfig *state) {
}
static TriggerEmulatorHelper helper;
static bool hasStimPins = false;
static void emulatorApplyPinState(int stateIndex, PwmConfig *state) /* pwm_gen_callback */ {
if (stopEmulationAtIndex == stateIndex) {
@ -148,7 +149,10 @@ static void emulatorApplyPinState(int stateIndex, PwmConfig *state) /* pwm_gen_c
}
#if EFI_PROD_CODE
applyPinState(stateIndex, state);
// Only set pins if they're configured - no need to waste the cycles otherwise
if (hasStimPins) {
applyPinState(stateIndex, state);
}
#endif /* EFI_PROD_CODE */
}
@ -180,4 +184,36 @@ void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUF
addConsoleActionI("stop_stimulator_at_index", setEmulatorAtIndex);
addConsoleAction("resume_stimulator", resumeStimulator);
}
void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration) {
if (engineConfiguration->triggerSimulatorFrequency ==
previousConfiguration->triggerSimulatorFrequency) {
return;
}
setTriggerEmulatorRPM(engineConfiguration->triggerSimulatorFrequency);
}
void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
print("Emulating %s\r\n", getConfigurationName(engineConfiguration->engineType));
for (size_t i = 0; i < efi::size(emulatorOutputs); i++)
{
triggerSignal.outputPins[i] = &emulatorOutputs[i];
brain_pin_e pin = CONFIG(triggerSimulatorPins)[i];
// Only bother trying to set output pins if they're configured
if (pin != GPIO_UNASSIGNED) {
hasStimPins = true;
}
#if EFI_PROD_CODE
triggerSignal.outputPins[i]->initPin("Trigger emulator", pin,
&CONFIG(triggerSimulatorPinModes)[i]);
#endif
}
initTriggerEmulatorLogic(sharedLogger);
}
#endif /* EFI_EMULATE_POSITION_SENSORS */

View File

@ -8,7 +8,15 @@
#pragma once
#include "engine.h"
#include "pwm_generator_logic.h"
class Logging;
class PwmConfig;
class MultiChannelStateSequence;
void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX);
void setTriggerEmulatorRPM(int value DECLARE_ENGINE_PARAMETER_SUFFIX);
void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration);
class TriggerEmulatorHelper {
public:

View File

@ -3,8 +3,7 @@ DEVELOPMENT_DIR=$(PROJECT_DIR)/development
DEV_SRC = $(DEVELOPMENT_DIR)/test/test.c \
$(DEVELOPMENT_DIR)/test/testbmk.c
DEV_SRC_CPP = $(DEVELOPMENT_DIR)/trigger_emulator.cpp \
$(DEVELOPMENT_DIR)/hw_layer/poten.cpp \
DEV_SRC_CPP = $(DEVELOPMENT_DIR)/hw_layer/poten.cpp \
$(DEVELOPMENT_DIR)/sensor_chart.cpp \
$(DEVELOPMENT_DIR)/rfi_perftest.cpp \
$(DEVELOPMENT_DIR)/engine_emulator.cpp \

View File

@ -20,7 +20,7 @@
#include "pwm_generator_logic.h"
#include "poten.h"
#include "trigger_emulator.h"
#include "trigger_emulator_algo.h"
EXTERN_ENGINE;

View File

@ -1,63 +0,0 @@
/**
* @file trigger_emulator.cpp
* @brief Position sensor(s) emulation code
*
* This file is mostly about initialization, the configuration is
* provided by the configureShaftPositionEmulatorShape method
*
* @date Dec 9, 2012
* @author Andrey Belomutskiy, (c) 2012-2020
*/
#include "trigger_emulator.h"
#include "eficonsole.h"
#include "main_trigger_callback.h"
#include "io_pins.h"
#include "trigger_emulator_algo.h"
#include "trigger_central.h"
#include "settings.h"
#if EFI_PROD_CODE
#include "pwm_generator.h"
#include "pin_repository.h"
#endif /* EFI_PROD_CODE */
extern PwmConfig triggerSignal;
static OutputPin emulatorOutputs[3];
EXTERN_ENGINE;
void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration) {
if (engineConfiguration->triggerSimulatorFrequency ==
previousConfiguration->triggerSimulatorFrequency) {
return;
}
setTriggerEmulatorRPM(engineConfiguration->triggerSimulatorFrequency);
}
void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
#if EFI_EMULATE_POSITION_SENSORS
print("Emulating %s\r\n", getConfigurationName(engineConfiguration->engineType));
triggerSignal.outputPins[0] = &emulatorOutputs[0];
triggerSignal.outputPins[1] = &emulatorOutputs[1];
triggerSignal.outputPins[2] = &emulatorOutputs[2];
#if EFI_PROD_CODE
// todo: refactor, make this a loop
triggerSignal.outputPins[0]->initPin("Trigger emulator 1", CONFIG(triggerSimulatorPins)[0],
&CONFIG(triggerSimulatorPinModes)[0]);
triggerSignal.outputPins[1]->initPin("Trigger emulator 2", CONFIG(triggerSimulatorPins)[1],
&CONFIG(triggerSimulatorPinModes)[1]);
triggerSignal.outputPins[2]->initPin("Trigger emulator 3", CONFIG(triggerSimulatorPins)[2],
&CONFIG(triggerSimulatorPinModes)[2]);
#endif /* EFI_PROD_CODE */
initTriggerEmulatorLogic(sharedLogger);
#else
print("No position sensor(s) emulation\r\n");
#endif /* EFI_EMULATE_POSITION_SENSORS */
}

View File

@ -1,19 +0,0 @@
/**
* @file trigger_emulator.h
* @brief Position sensor(s) emulation header
*
* @date Dec 9, 2012
* @author Andrey Belomutskiy, (c) 2012-2020
*/
#pragma once
#include "global.h"
#include "trigger_structure.h"
#include "engine.h"
void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX);
void setTriggerEmulatorRPM(int value DECLARE_ENGINE_PARAMETER_SUFFIX);
void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration);

View File

@ -156,7 +156,6 @@ CPPSRC = $(UTILSRC_CPP) \
$(CONTROLLERS_ALGO_SRC_CPP) \
$(CONTROLLERS_SRC_CPP) \
$(PROJECT_DIR)/development/sensor_chart.cpp \
$(PROJECT_DIR)/development/trigger_emulator.cpp \
$(HW_LAYER_EMS_CPP) \
$(HW_SENSORS_SRC) \
$(TRIGGER_SRC_CPP) \

View File

@ -25,7 +25,6 @@
#include "bench_test.h"
#include "engine.h"
#include "tunerstudio.h"
#include "trigger_emulator.h"
#include "engine_controller.h"
#include "map_averaging.h"
#include "memstreams.h"