refactoring: encapsulation

This commit is contained in:
Andrey 2022-09-28 22:42:08 -04:00
parent 30a998bddf
commit e6b8b08853
9 changed files with 69 additions and 42 deletions

View File

@ -416,14 +416,18 @@ void Engine::efiWatchdog() {
void Engine::checkShutdown() { void Engine::checkShutdown() {
#if EFI_MAIN_RELAY_CONTROL #if EFI_MAIN_RELAY_CONTROL
// if we are already in the "ignition_on" mode, then do nothing // if we are already in the "ignition_on" mode, then do nothing
/* this logic is not alive
if (ignitionOnTimeNt > 0) { if (ignitionOnTimeNt > 0) {
return; return;
} }
todo: move to shutdown_controller.cpp
*/
// here we are in the shutdown (the ignition is off) or initial mode (after the firmware fresh start) // here we are in the shutdown (the ignition is off) or initial mode (after the firmware fresh start)
const efitick_t engineStopWaitTimeoutUs = 500000LL; // 0.5 sec const efitick_t engineStopWaitTimeoutUs = 500000LL; // 0.5 sec
// in shutdown mode, we need a small cooldown time between the ignition off and on // in shutdown mode, we need a small cooldown time between the ignition off and on
/* this needs work or tests /* this needs work or tests
todo: move to shutdown_controller.cpp
if (stopEngineRequestTimeNt == 0 || (getTimeNowNt() - stopEngineRequestTimeNt) > US2NT(engineStopWaitTimeoutUs)) { if (stopEngineRequestTimeNt == 0 || (getTimeNowNt() - stopEngineRequestTimeNt) > US2NT(engineStopWaitTimeoutUs)) {
// if the ignition key is turned on again, // if the ignition key is turned on again,
// we cancel the shutdown mode, but only if all shutdown procedures are complete // we cancel the shutdown mode, but only if all shutdown procedures are complete
@ -518,18 +522,6 @@ void Engine::periodicFastCallback() {
engine->engineModules.apply_all([](auto & m) { m.onFastCallback(); }); engine->engineModules.apply_all([](auto & m) { m.onFastCallback(); });
} }
void doScheduleStopEngine() {
efiPrintf("Starting doScheduleStopEngine");
engine->limpManager.stopEngine();
engine->ignitionOnTimeNt = 0;
// todo: initiate stepper motor parking
// make sure we have stored all the info
#if EFI_PROD_CODE
//todo: FIX kinetis build with this line
//backupRamFlush();
#endif // EFI_PROD_CODE
}
EngineRotationState * getEngineRotationState() { EngineRotationState * getEngineRotationState() {
return &engine->rpmCalculator; return &engine->rpmCalculator;
} }

View File

@ -231,11 +231,6 @@ public:
bool startStopState = false; bool startStopState = false;
int startStopStateToggleCounter = 0; int startStopStateToggleCounter = 0;
/**
* this is needed by and checkShutdown()
* todo: refactor to Timer?
*/
efitick_t ignitionOnTimeNt = 0;
Timer configBurnTimer; Timer configBurnTimer;
@ -329,7 +324,6 @@ void applyNonPersistentConfiguration();
void prepareOutputSignals(); void prepareOutputSignals();
void validateConfiguration(); void validateConfiguration();
void doScheduleStopEngine();
void scheduleReboot(); void scheduleReboot();
bool isLockedFromUser(); bool isLockedFromUser();
void unlockEcu(int password); void unlockEcu(int password);

View File

@ -213,6 +213,7 @@ angle_t getInjectionOffset(float rpm, float load) {
if (cisnan(value)) { if (cisnan(value)) {
// we could be here while resetting configuration for example // we could be here while resetting configuration for example
// huh? what? when do we have RPM while resetting configuration? is that CI edge case? shall we fix CI?
warning(CUSTOM_ERR_6569, "phase map not ready"); warning(CUSTOM_ERR_6569, "phase map not ready");
return 0; return 0;
} }

View File

@ -65,6 +65,7 @@ CONTROLLERS_SRC_CPP = \
$(CONTROLLERS_DIR)/tcu/gear_controller.cpp \ $(CONTROLLERS_DIR)/tcu/gear_controller.cpp \
$(CONTROLLERS_DIR)/tcu/simple_tcu.cpp \ $(CONTROLLERS_DIR)/tcu/simple_tcu.cpp \
$(CONTROLLERS_DIR)/tcu/tc_4l6x.cpp \ $(CONTROLLERS_DIR)/tcu/tc_4l6x.cpp \
$(CONTROLLERS_DIR)/shutdown_controller.cpp \
$(CONTROLLERS_DIR)/limp_manager.cpp \ $(CONTROLLERS_DIR)/limp_manager.cpp \
CONTROLLERS_INC=\ CONTROLLERS_INC=\

View File

@ -82,7 +82,7 @@ bool RpmCalculator::isRunning() const {
* @return true if engine is spinning (cranking or running) * @return true if engine is spinning (cranking or running)
*/ */
bool RpmCalculator::checkIfSpinning(efitick_t nowNt) const { bool RpmCalculator::checkIfSpinning(efitick_t nowNt) const {
if (engine->limpManager.isEngineStop(nowNt)) { if (engine->limpManager.shutdownController.isEngineStop(nowNt)) {
return false; return false;
} }

View File

@ -112,7 +112,7 @@ void LimpManager::updateState(int rpm, efitick_t nowNt) {
} }
// If we're in engine stop mode, inhibit fuel // If we're in engine stop mode, inhibit fuel
if (isEngineStop(nowNt)) { if (shutdownController.isEngineStop(nowNt)) {
/** /**
* todo: we need explicit clarification on why do we cut fuel but do not cut spark here! * todo: we need explicit clarification on why do we cut fuel but do not cut spark here!
*/ */
@ -173,21 +173,6 @@ void LimpManager::fatalError() {
setFaultRevLimit(0); setFaultRevLimit(0);
} }
void LimpManager::stopEngine() {
m_engineStopTimer.reset();
}
bool LimpManager::isEngineStop(efitick_t nowNt) const {
float timeSinceStop = getTimeSinceEngineStop(nowNt);
// If there was stop requested in the past 5 seconds, we're in stop mode
return timeSinceStop < 5;
}
float LimpManager::getTimeSinceEngineStop(efitick_t nowNt) const {
return m_engineStopTimer.getElapsedSeconds(nowNt);
}
void LimpManager::setFaultRevLimit(int limit) { void LimpManager::setFaultRevLimit(int limit) {
// Only allow decreasing the limit // Only allow decreasing the limit
// aka uses the limit of the worst fault to yet occur // aka uses the limit of the worst fault to yet occur

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "rusefi_types.h" #include "shutdown_controller.h"
#include <cstdint> #include <cstdint>
@ -22,6 +22,7 @@ enum class ClearReason : uint8_t {
KickStart, // 13 KickStart, // 13
// Keep this list in sync with fuelIgnCutCodeList in rusefi.input! // Keep this list in sync with fuelIgnCutCodeList in rusefi.input!
// todo: add a code generator between ClearReason and fuelIgnCutCodeList in rusefi.input
}; };
// Only allows clearing the value, but never resetting it. // Only allows clearing the value, but never resetting it.
@ -79,6 +80,8 @@ private:
class LimpManager { class LimpManager {
public: public:
ShutdownController shutdownController;
// This is called from periodicFastCallback to update internal state // This is called from periodicFastCallback to update internal state
void updateState(int rpm, efitick_t nowNt); void updateState(int rpm, efitick_t nowNt);
@ -93,10 +96,6 @@ public:
// Other subsystems call these APIs to indicate a problem has occured // Other subsystems call these APIs to indicate a problem has occured
void etbProblem(); void etbProblem();
void fatalError(); void fatalError();
void stopEngine();
bool isEngineStop(efitick_t nowNt) const;
float getTimeSinceEngineStop(efitick_t nowNt) const;
private: private:
void setFaultRevLimit(int limit); void setFaultRevLimit(int limit);
@ -117,8 +116,6 @@ private:
Clearable m_transientAllowIgnition = true; Clearable m_transientAllowIgnition = true;
bool m_hadOilPressureAfterStart = false; bool m_hadOilPressureAfterStart = false;
Timer m_engineStopTimer;
}; };
LimpManager * getLimpManager(); LimpManager * getLimpManager();

View File

@ -0,0 +1,19 @@
/*
* @file shutdown_controller.cpp
*
*/
#include "shutdown_controller.h"
#include "loggingcentral.h"
#include "limp_manager.h"
void doScheduleStopEngine() {
efiPrintf("Starting doScheduleStopEngine");
getLimpManager()->shutdownController.stopEngine();
// todo: initiate stepper motor parking
// make sure we have stored all the info
#if EFI_PROD_CODE
//todo: FIX kinetis build with this line
//backupRamFlush();
#endif // EFI_PROD_CODE
}

View File

@ -0,0 +1,38 @@
/*
* @file shutdown_controller.h
*
*/
#pragma once
#include "timer.h"
void doScheduleStopEngine();
class ShutdownController {
public:
void stopEngine() {
m_engineStopTimer.reset();
// ignitionOnTimeNt = 0;
}
float getTimeSinceEngineStop(efitick_t nowNt) const {
return m_engineStopTimer.getElapsedSeconds(nowNt);
}
bool isEngineStop(efitick_t nowNt) const {
float timeSinceStop = getTimeSinceEngineStop(nowNt);
// If there was stop requested in the past 5 seconds, we're in stop mode
return timeSinceStop < 5;
}
private:
Timer m_engineStopTimer;
/**
* this is needed by and checkShutdown()
*/
// is this an unused boolean value?
// efitick_t ignitionOnTimeNt = 0;
};