From 5b741631363bb94c7b18305f528fdcf49a0588ae Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 5 Jan 2019 23:33:04 -0500 Subject: [PATCH] refactoring around "stopEngine" logic --- firmware/controllers/algo/engine.cpp | 15 ++++++++++++++- firmware/controllers/algo/engine.h | 2 +- firmware/controllers/injector_central.cpp | 2 +- firmware/controllers/lcd_controller.cpp | 2 +- firmware/controllers/settings.cpp | 8 ++++++-- firmware/controllers/settings.h | 2 +- firmware/controllers/trigger/rpm_calculator.cpp | 12 ++++++------ 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 4ace141a89..7f184e7bc3 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -116,6 +116,15 @@ Engine::Engine(persistent_config_s *config) { reset(); } +/** + * @see scheduleStopEngine() + * @return true if there is a reason to stop engine + */ +bool Engine::needToStopEngine(efitick_t nowNt) { + return stopEngineRequestTimeNt != 0 && + nowNt - stopEngineRequestTimeNt < 3 * US2NT(US_PER_SECOND_LL); +} + void Engine::reset() { withError = isEngineChartEnabled = false; etbAutoTune = false; @@ -270,9 +279,13 @@ void Engine::checkShutdown() { #if EFI_MAIN_RELAY_CONTROL || defined(__DOXYGEN__) int rpm = rpmCalculator.getRpm(); + /** + * Something is weird here: "below 5.0 volts on battery" what is it about? Is this about + * Frankenso powering everything while driver has already turned ignition off? or what is this condition about? + */ const float vBattThreshold = 5.0f; if (isValidRpm(rpm) && sensors.vBatt < vBattThreshold && stopEngineRequestTimeNt == 0) { - stopEngine(); + scheduleStopEngine(); // todo: add stepper motor parking } #endif /* EFI_MAIN_RELAY_CONTROL */ diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 0bba9ded15..abc0b0ca67 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -304,7 +304,7 @@ public: float fsioLastValue[FSIO_COMMAND_COUNT]; WallFuel wallFuel; - + bool needToStopEngine(efitick_t nowNt); bool etbAutoTune; /** * That's the list of pending spark firing events diff --git a/firmware/controllers/injector_central.cpp b/firmware/controllers/injector_central.cpp index 7cf5cc3d38..02e199329b 100644 --- a/firmware/controllers/injector_central.cpp +++ b/firmware/controllers/injector_central.cpp @@ -258,7 +258,7 @@ void runIoTest(int subsystem, int index) { // call to pit setCallFromPitStop(30000); } else if (subsystem == 0x99) { - stopEngine(); + scheduleStopEngine(); } } diff --git a/firmware/controllers/lcd_controller.cpp b/firmware/controllers/lcd_controller.cpp index 830103f390..5c1da76d4a 100644 --- a/firmware/controllers/lcd_controller.cpp +++ b/firmware/controllers/lcd_controller.cpp @@ -75,7 +75,7 @@ static MenuItem miMapV(&miSensors, LL_MAF_V); static MenuItem miMapKgHr(&miSensors, LL_MAF_KG_HR); static MenuItem miKnock(&miSensors, LL_KNOCK); -static MenuItem miStopEngine(&miBench, "stop engine", stopEngine); +static MenuItem miStopEngine(&miBench, "stop engine", scheduleStopEngine); static MenuItem miTestFan(&miBench, "test fan", fanBench); static MenuItem miTestFuelPump(&miBench, "test pump", fuelPumpBench); static MenuItem miTestMIL(&miBench, "test MIL", milBench); diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 0d4b0d062a..e7e05afba1 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -970,7 +970,11 @@ static void disableSpi(int index) { setSpiMode(index, false); } -void stopEngine(void) { +/** + * See 'Engine::needToStopEngine' for code which actually stops engine + * weird: we stop pins from here? we probably should stop engine from the code which is actually stopping engine? + */ +void scheduleStopEngine(void) { engine->stopEngineRequestTimeNt = getTimeNowNt(); // let's close injectors or else if these happen to be open right now enginePins.stopPins(); @@ -1339,7 +1343,7 @@ void initSettings(void) { addConsoleActionSSS("set_timing_map", setTimingMap); - addConsoleAction("stopengine", (Void) stopEngine); + addConsoleAction("stopengine", (Void) scheduleStopEngine); // todo: refactor this - looks like all boolean flags should be controlled with less code duplication addConsoleActionI("enable_spi", enableSpi); diff --git a/firmware/controllers/settings.h b/firmware/controllers/settings.h index 40a47d9500..ee4ba26245 100644 --- a/firmware/controllers/settings.h +++ b/firmware/controllers/settings.h @@ -14,7 +14,7 @@ void initSettings(void); void printSpiState(Logging *logger, board_configuration_s *boardConfiguration); void printConfiguration(const engine_configuration_s *engineConfiguration); -void stopEngine(void); +void scheduleStopEngine(void); void setCallFromPitStop(int durationMs); void setEngineType(int value); /** diff --git a/firmware/controllers/trigger/rpm_calculator.cpp b/firmware/controllers/trigger/rpm_calculator.cpp index d5444dc70e..012c119041 100644 --- a/firmware/controllers/trigger/rpm_calculator.cpp +++ b/firmware/controllers/trigger/rpm_calculator.cpp @@ -90,6 +90,9 @@ bool RpmCalculator::isRunning(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return state == RUNNING; } +/** + * @return true if engine is spinning (cranking or running) + */ bool RpmCalculator::checkIfSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (lastRpmEventTimeNt == 0) { // here we assume 64 bit time does not overflow @@ -97,12 +100,9 @@ bool RpmCalculator::checkIfSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return false; } efitick_t nowNt = getTimeNowNt(); - if (ENGINE(stopEngineRequestTimeNt) != 0) { - if (nowNt - ENGINE(stopEngineRequestTimeNt) < 3 * US2NT(US_PER_SECOND_LL)) { - // 'stopengine' command implementation - setStopped(PASS_ENGINE_PARAMETER_SIGNATURE); - return false; - } + if (ENGINE(needToStopEngine(nowNt))) { + setStopped(PASS_ENGINE_PARAMETER_SIGNATURE); + return false; } /**