fome-fw/firmware/controllers/algo/engine.cpp

142 lines
3.3 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file engine.cpp
*
*
* This might be a http://en.wikipedia.org/wiki/God_object but that's best way I can
* express myself in C/C++. I am open for suggestions :)
*
* @date May 21, 2014
* @author Andrey Belomutskiy, (c) 2012-2014
*/
#include "main.h"
#include "engine.h"
#include "engine_state.h"
2014-09-27 16:02:54 -07:00
#include "efiGpio.h"
2014-12-14 20:03:37 -08:00
#include "trigger_central.h"
2014-08-29 07:52:33 -07:00
2014-10-31 15:03:42 -07:00
#if EFI_PROD_CODE
#include "injector_central.h"
#else
#define isRunningBenchTest() true
#endif
2014-09-26 13:03:02 -07:00
static Logging logger;
2014-12-14 20:03:37 -08:00
EXTERN_ENGINE
;
2014-11-24 19:03:19 -08:00
2014-08-29 07:52:33 -07:00
/**
* We are executing these heavy (logarithm) methods from outside the trigger callbacks for performance reasons.
*/
void Engine::updateSlowSensors() {
2014-10-31 12:03:00 -07:00
engineState.iat = getIntakeAirTemperature(this);
engineState.clt = getCoolantTemperature(this);
2014-08-29 07:52:33 -07:00
}
2014-11-11 06:03:18 -08:00
void Engine::onTriggerEvent(uint64_t nowNt) {
2014-09-26 13:03:02 -07:00
isSpinning = true;
2014-11-11 06:03:18 -08:00
lastTriggerEventTimeNt = nowNt;
2014-09-26 13:03:02 -07:00
}
2014-11-07 20:03:53 -08:00
Engine::Engine() {
2014-11-11 13:05:09 -08:00
lastTriggerEventTimeNt = 0;
isCylinderCleanupMode = false;
2014-11-25 06:03:01 -08:00
engineCycleEventCount = 0;
2015-01-01 14:03:28 -08:00
stopEngineRequestTimeNt = 0;
}
void Engine::precalc(engine_configuration_s *engineConfiguration) {
sparkTable.init(DWELL_CURVE_SIZE, sparkAtable, sparkBtable);
sparkTable.preCalc(engineConfiguration->sparkDwellBins, engineConfiguration->sparkDwell);
2014-11-07 20:03:53 -08:00
}
2014-09-26 13:03:02 -07:00
void Engine::init() {
initLogging(&logger, "engine");
}
2014-09-27 16:02:54 -07:00
static bool stopPin(io_pin_e pin) {
if (getOutputPinValue(pin)) {
setOutputPinValue(pin, 0);
scheduleMsg(&logger, "turning off %s", getPinName(pin));
return true;
}
return false;
}
bool Engine::stopPins() {
bool result = false;
for (int i = 0; i < engineConfiguration->cylindersCount; i++) {
io_pin_e pin = (io_pin_e) ((int) INJECTOR_1_OUTPUT + i);
result |= stopPin(pin);
pin = (io_pin_e) ((int) SPARKOUT_1_OUTPUT + i);
result |= stopPin(pin);
}
return result;
}
2014-09-26 13:03:02 -07:00
void Engine::watchdog() {
if (!isSpinning) {
2014-10-31 15:03:42 -07:00
if (!isRunningBenchTest() && stopPins()) {
2014-09-27 16:02:54 -07:00
firmwareError("Some pins were turned off by 2nd pass watchdog");
}
2014-09-26 13:03:02 -07:00
return;
}
2014-11-11 06:03:18 -08:00
uint64_t nowNt = getTimeNowNt();
2014-09-26 13:03:02 -07:00
/**
* Lowest possible cranking is about 240 RPM, that's 4 revolutions per second.
* 0.25 second is 250000 uS
2014-10-31 15:03:42 -07:00
*
* todo: better watch dog implementation should be implemented - see
* http://sourceforge.net/p/rusefi/tickets/96/
2014-09-26 13:03:02 -07:00
*/
2014-11-11 06:03:18 -08:00
if (nowNt - lastTriggerEventTimeNt < US2NT(250000LL)) {
2014-09-26 13:03:02 -07:00
return;
}
2014-09-27 16:02:54 -07:00
isSpinning = false;
2014-09-27 10:03:11 -07:00
#if EFI_PROD_CODE || EFI_SIMULATOR
2014-09-26 13:03:02 -07:00
scheduleMsg(&logger, "engine has STOPPED");
2014-12-14 20:03:37 -08:00
if (engineConfiguration->isPrintTriggerSynchDetails) {
triggerInfo(engine);
}
2014-09-27 10:03:11 -07:00
#endif
2014-09-26 13:03:02 -07:00
2014-09-27 16:02:54 -07:00
stopPins();
2014-09-26 13:03:02 -07:00
}
2014-10-17 12:02:59 -07:00
StartupFuelPumping::StartupFuelPumping() {
2014-10-17 13:05:16 -07:00
isTpsAbove50 = false;
pumpsCounter = 0;
2014-10-17 12:02:59 -07:00
}
2014-10-17 13:05:16 -07:00
void StartupFuelPumping::setPumpsCounter(engine_configuration_s *engineConfiguration, int newValue) {
2014-10-17 12:02:59 -07:00
if (pumpsCounter != newValue) {
pumpsCounter = newValue;
2014-10-17 13:05:16 -07:00
2014-10-17 14:03:00 -07:00
if (pumpsCounter == PUMPS_TO_PRIME) {
scheduleMsg(&logger, "let's squirt prime pulse %f", pumpsCounter);
2014-10-17 13:05:16 -07:00
pumpsCounter = 0;
} else {
2014-10-17 14:03:00 -07:00
scheduleMsg(&logger, "setPumpsCounter %d", pumpsCounter);
2014-10-17 13:05:16 -07:00
}
2014-10-17 12:02:59 -07:00
}
}
2014-11-24 19:03:19 -08:00
void StartupFuelPumping::update(DECLARE_ENGINE_PARAMETER_F) {
2014-12-03 14:03:09 -08:00
if (engine->rpmCalculator.rpm(PASS_ENGINE_PARAMETER_F) == 0) {
2014-11-24 19:03:19 -08:00
bool isTpsAbove50 = getTPS(PASS_ENGINE_PARAMETER_F) >= 50;
2014-10-17 13:05:16 -07:00
if (this->isTpsAbove50 != isTpsAbove50) {
setPumpsCounter(engineConfiguration, pumpsCounter + 1);
}
2014-10-17 12:02:59 -07:00
} else {
/**
* Engine is not stopped - not priming pumping mode
*/
2014-10-17 13:05:16 -07:00
setPumpsCounter(engineConfiguration, 0);
isTpsAbove50 = false;
2014-10-17 12:02:59 -07:00
}
}