diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index c2b16dd290..79b680751e 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -58,12 +58,8 @@ public: LocalVersionHolder auxParametersVersion; operation_mode_e getOperationMode(DECLARE_ENGINE_PARAMETER_SIGNATURE); + AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]; - scheduling_s auxTurnOnEvent[AUX_DIGITAL_VALVE_COUNT][/* we have two 180 cycles within engine 360 revolution */ 2][CYCLE_ALTERNATION]; - scheduling_s auxTurnOffEvent[AUX_DIGITAL_VALVE_COUNT][2][CYCLE_ALTERNATION]; - - - int auxSchedulingIndex = 2; bool needTdcCallback = true; /** diff --git a/firmware/controllers/algo/event_registry.h b/firmware/controllers/algo/event_registry.h index 49274e1594..78987c5607 100644 --- a/firmware/controllers/algo/event_registry.h +++ b/firmware/controllers/algo/event_registry.h @@ -120,3 +120,14 @@ public: IgnitionEvent elements[MAX_IGNITION_EVENT_COUNT]; bool isReady = false; }; + +class AuxActor { +public: + int phaseIndex; + int valveIndex; + angle_t extra; + + AngleBasedEvent open; + AngleBasedEvent close; + DECLARE_ENGINE_PTR; +}; diff --git a/firmware/controllers/trigger/aux_valves.cpp b/firmware/controllers/trigger/aux_valves.cpp index 6181d63803..9634e341e3 100644 --- a/firmware/controllers/trigger/aux_valves.cpp +++ b/firmware/controllers/trigger/aux_valves.cpp @@ -10,54 +10,53 @@ * https://github.com/rusefi/rusefi/issues/490 * * @date Nov 25, 2017 - * @author Andrey Belomutskiy, (c) 2012-2018 + * @author Andrey Belomutskiy, (c) 2012-2019 */ #include "engine_math.h" #include "aux_valves.h" #include "allsensors.h" #include "trigger_central.h" +#include "spark_logic.h" EXTERN_ENGINE ; -/** -//void plainPinTurnOn(AuxActor *current) { -// NamedOutputPin *output = &enginePins.auxValve[current->valveIndex]; -if (!engine->auxStarted) { - - for (int valveIndex = 0; valveIndex < AUX_DIGITAL_VALVE_COUNT; valveIndex++) { - for (int phaseIndex = 0; phaseIndex < 2; phaseIndex++) { - AuxActor *current = &actors[phaseIndex][valveIndex]; - -// if () - - - scheduleOrQueue(¤t->open, - trgEventIndex, - current->extra + engine->engineState.auxValveStart, - (schfunc_t)plainPinTurnOn, - current - PASS_ENGINE_PARAMETER_SUFFIX); - - - } - } - - - engine->auxStarted = true; - } - - */ - -void plainPinTurnOn(NamedOutputPin *output) { +void plainPinTurnOn(AuxActor *current) { + NamedOutputPin *output = &enginePins.auxValve[current->valveIndex]; output->setHigh(); -} + +#if EFI_UNIT_TEST + Engine *engine = current->engine; + EXPAND_Engine; +#endif /* EFI_UNIT_TEST */ + + scheduleOrQueue(¤t->open, + TRIGGER_EVENT_UNDEFINED, + current->extra + engine->engineState.auxValveStart, + (schfunc_t)plainPinTurnOn, + current + PASS_ENGINE_PARAMETER_SUFFIX + ); + + angle_t duration = engine->engineState.auxValveEnd - engine->engineState.auxValveStart; + + fixAngle(duration, "duration", CUSTOM_ERR_6557); + + scheduleOrQueue(¤t->close, + TRIGGER_EVENT_UNDEFINED, + current->extra + engine->engineState.auxValveEnd, + (schfunc_t)plainPinTurnOff, + output + PASS_ENGINE_PARAMETER_SUFFIX + ); + } void plainPinTurnOff(NamedOutputPin *output) { output->setLow(); } +/* static void auxValveTriggerCallback(trigger_event_e ckpSignalType, uint32_t index DECLARE_ENGINE_PARAMETER_SUFFIX) { UNUSED(ckpSignalType); @@ -69,12 +68,13 @@ static void auxValveTriggerCallback(trigger_event_e ckpSignalType, if (!isValidRpm(rpm)) { return; } - +*/ /** * Sometimes previous event has not yet been executed by the time we are scheduling new events. * We use this array alternation in order to bring events that are scheled and waiting to be executed from * events which are already being scheduled */ +/* int engineCycleAlternation = engine->triggerCentral.triggerState.getTotalRevolutionCounter() % CYCLE_ALTERNATION; for (int valveIndex = 0; valveIndex < AUX_DIGITAL_VALVE_COUNT; valveIndex++) { @@ -82,6 +82,7 @@ static void auxValveTriggerCallback(trigger_event_e ckpSignalType, NamedOutputPin *output = &enginePins.auxValve[valveIndex]; for (int phaseIndex = 0; phaseIndex < 2; phaseIndex++) { +*/ /* I believe a more correct implementation is the following: * here we properly account for trigger angle position in engine cycle coordinates // todo: at the moment this logic is assuming four-stroke 720-degree engine cycle @@ -91,6 +92,7 @@ static void auxValveTriggerCallback(trigger_event_e ckpSignalType, - ENGINE(triggerCentral.triggerShape.eventAngles[SCHEDULING_TRIGGER_INDEX]) ; */ +/* angle_t extra = phaseIndex * 360 + valveIndex * 180; angle_t onTime = extra + engine->engineState.auxValveStart; scheduling_s *onEvent = &engine->auxTurnOnEvent[valveIndex][phaseIndex][engineCycleAlternation]; @@ -115,13 +117,49 @@ static void auxValveTriggerCallback(trigger_event_e ckpSignalType, } } } +*/ void initAuxValves(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { UNUSED(sharedLogger); if (engineConfiguration->auxValves[0] == GPIO_UNASSIGNED) { return; } - addTriggerEventListener(auxValveTriggerCallback, "tach", engine); + +#if !EFI_UNIT_TEST + // let's give it time to grab TPS value + chThdSleepMilliseconds(50); +#endif /* EFI_UNIT_TESTS */ + + float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); + if (cisnan(tps)) { + firmwareError(CUSTOM_OBD_91, "No TPS for Aux Valves"); + return; + } + + updateAuxValves(PASS_ENGINE_PARAMETER_SIGNATURE); + + for (int valveIndex = 0; valveIndex < AUX_DIGITAL_VALVE_COUNT; valveIndex++) { + + for (int phaseIndex = 0; phaseIndex < 2; phaseIndex++) { + AuxActor *actor = &engine->auxValves[valveIndex][phaseIndex]; + actor->phaseIndex = phaseIndex; + actor->valveIndex = valveIndex; + actor->extra = phaseIndex * 360 + valveIndex * 180; + + INJECT_ENGINE_REFERENCE(actor); + + scheduleOrQueue(&actor->open, + TRIGGER_EVENT_UNDEFINED, + actor->extra + engine->engineState.auxValveStart, + (schfunc_t)plainPinTurnOn, + actor + PASS_ENGINE_PARAMETER_SUFFIX + ); + } + } + + +// addTriggerEventListener(auxValveTriggerCallback, "AuxV", engine); } void updateAuxValves(DECLARE_ENGINE_PARAMETER_SIGNATURE) { @@ -129,24 +167,23 @@ void updateAuxValves(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return; } - float x = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); - if (cisnan(x)) { + float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); + if (cisnan(tps)) { // error should be already reported by now return; } - engine->engineState.auxValveStart = interpolate2d("aux", x, + engine->engineState.auxValveStart = interpolate2d("aux", tps, engineConfiguration->fsioCurve1Bins, engineConfiguration->fsioCurve1); - engine->engineState.auxValveEnd = interpolate2d("aux", x, + engine->engineState.auxValveEnd = interpolate2d("aux", tps, engineConfiguration->fsioCurve2Bins, engineConfiguration->fsioCurve2); if (engine->engineState.auxValveStart >= engine->engineState.auxValveEnd) { // this is a fatal error to make this really visible - firmwareError(CUSTOM_AUX_OUT_OF_ORDER, "out of order at %.2f %.2f %.2f", x, + firmwareError(CUSTOM_AUX_OUT_OF_ORDER, "out of order at %.2f %.2f %.2f", tps, engine->engineState.auxValveStart, engine->engineState.auxValveEnd); } } - diff --git a/firmware/controllers/trigger/aux_valves.h b/firmware/controllers/trigger/aux_valves.h index d87e17f856..3f28d16e8f 100644 --- a/firmware/controllers/trigger/aux_valves.h +++ b/firmware/controllers/trigger/aux_valves.h @@ -2,28 +2,14 @@ * @file aux_valves.h * * @date Nov 25, 2017 - * @author Andrey Belomutskiy, (c) 2012-2017 + * @author Andrey Belomutskiy, (c) 2012-2019 */ #pragma once #include "engine.h" -/* -class AuxActor { -public: - int phaseIndex; - int valveIndex; - angle_t extra; - - AngleBasedEvent open; - AngleBasedEvent close; -}; -*/ - void initAuxValves(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); void updateAuxValves(DECLARE_ENGINE_PARAMETER_SIGNATURE); -//void plainPinTurnOn(AuxActor *current); -void plainPinTurnOn(NamedOutputPin *output); +void plainPinTurnOn(AuxActor *current); void plainPinTurnOff(NamedOutputPin *output); - diff --git a/unit_tests/tests/test_aux_valves.cpp b/unit_tests/tests/test_aux_valves.cpp index ca29648b5d..3871e00ddc 100644 --- a/unit_tests/tests/test_aux_valves.cpp +++ b/unit_tests/tests/test_aux_valves.cpp @@ -11,7 +11,6 @@ TEST(misc, testAuxValves) { WITH_ENGINE_TEST_HELPER(NISSAN_PRIMERA); - engine->auxSchedulingIndex = 0; engine->needTdcCallback = false; setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð, IM_SEQUENTIAL); @@ -20,8 +19,8 @@ TEST(misc, testAuxValves) { eth.fireTriggerEvents2(2 /* count */ , 600 /* ms */); ASSERT_EQ( 100, GET_RPM()) << "spinning-RPM#1"; - eth.assertScheduling("a0", 0, &engine->auxTurnOnEvent[0][0][0], (void*)&plainPinTurnOn, -600000); - eth.assertScheduling("a1", 1, &engine->auxTurnOffEvent[0][0][0], (void*)&plainPinTurnOff, -550000); - eth.assertScheduling("a2", 2, &engine->auxTurnOnEvent[1][0][0], (void*)&plainPinTurnOn, -300000); + eth.assertTriggerEvent("a0", 0, &engine->auxValves[0][0].open, (void*)&plainPinTurnOn, 7, 86); + eth.assertTriggerEvent("a1", 1, &engine->auxValves[0][1].open, (void*)&plainPinTurnOn, 3, 86); + eth.assertTriggerEvent("a2", 2, &engine->auxValves[1][0].open, (void*)&plainPinTurnOn, 1, 86); }