rusefi-1/firmware/controllers/algo/engine.cpp

84 lines
1.9 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-08-29 07:52:33 -07:00
2014-09-26 13:03:02 -07:00
#if EFI_PROD_CODE || EFI_SIMULATOR
static Logging logger;
#endif
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-02 11:03:28 -07:00
engineState.iat = getIntakeAirTemperature(engineConfiguration2);
2014-10-02 10:03:00 -07:00
engineState.clt = getCoolantTemperature(engineConfiguration2);
2014-08-29 07:52:33 -07:00
}
2014-09-26 13:03:02 -07:00
void Engine::onTriggerEvent(uint64_t nowUs) {
isSpinning = true;
lastTriggerEventTimeUs = nowUs;
}
void Engine::init() {
#if EFI_PROD_CODE || EFI_SIMULATOR
initLogging(&logger, "engine");
#endif
}
2014-09-27 16:02:54 -07:00
static bool stopPin(io_pin_e pin) {
if (getOutputPinValue(pin)) {
setOutputPinValue(pin, 0);
#if EFI_PROD_CODE || EFI_SIMULATOR
scheduleMsg(&logger, "turning off %s", getPinName(pin));
#endif
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-09-27 16:02:54 -07:00
if (stopPins()) {
firmwareError("Some pins were turned off by 2nd pass watchdog");
}
2014-09-26 13:03:02 -07:00
return;
}
uint64_t nowUs = getTimeNowUs();
/**
* Lowest possible cranking is about 240 RPM, that's 4 revolutions per second.
* 0.25 second is 250000 uS
*/
if (nowUs - lastTriggerEventTimeUs < 250000) {
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-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
}