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

616 lines
19 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -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
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
#include "pch.h"
2015-07-10 06:01:56 -07:00
#include "trigger_central.h"
#include "fuel_math.h"
#include "advance_map.h"
#include "speed_density.h"
2016-01-01 16:02:59 -08:00
#include "advance_map.h"
2022-09-07 12:56:45 -07:00
2017-11-26 19:30:37 -08:00
#include "aux_valves.h"
#include "map_averaging.h"
2019-10-11 17:43:21 -07:00
#include "perf_trace.h"
2020-09-07 12:08:54 -07:00
#include "backup_ram.h"
#include "idle_thread.h"
#include "idle_hardware.h"
#include "gppwm.h"
#include "tachometer.h"
#include "dynoview.h"
#include "boost_control.h"
#include "fan_control.h"
#include "ac_control.h"
#include "vr_pwm.h"
2020-10-19 19:04:06 -07:00
#if EFI_MC33816
#include "mc33816.h"
#endif // EFI_MC33816
2015-07-10 06:01:56 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
#include "trigger_emulator_algo.h"
#include "bench_test.h"
2015-07-10 06:01:56 -07:00
#else
#define isRunningBenchTest() true
2016-01-23 15:01:40 -08:00
#endif /* EFI_PROD_CODE */
2015-07-10 06:01:56 -07:00
#if (BOARD_TLE8888_COUNT > 0)
#include "gpio/tle8888.h"
#endif
2019-12-21 17:43:11 -08:00
#if EFI_ENGINE_SNIFFER
#include "engine_sniffer.h"
extern int waveChartUsedSize;
extern WaveChart waveChart;
#endif /* EFI_ENGINE_SNIFFER */
void Engine::resetEngineSnifferIfInTestMode() {
#if EFI_ENGINE_SNIFFER
if (isFunctionalTestMode) {
// TODO: what is the exact reasoning for the exact engine sniffer pause time I wonder
waveChart.pauseEngineSnifferUntilNt = getTimeNowNt() + MS2NT(300);
2019-12-21 17:43:11 -08:00
waveChart.reset();
}
#endif /* EFI_ENGINE_SNIFFER */
}
2021-10-24 14:05:37 -07:00
/**
* VVT decoding delegates to universal trigger decoder. Here we map vvt_mode_e into corresponding trigger_type_e
*/
2020-08-26 10:30:55 -07:00
trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode) {
switch (vvtMode) {
case VVT_INACTIVE:
return TT_ONE;
2020-08-26 10:30:55 -07:00
case VVT_2JZ:
return TT_VVT_JZ;
2022-01-31 15:20:43 -08:00
case VVT_MIATA_NB:
return TT_VVT_MIATA_NB;
2020-08-26 21:43:23 -07:00
case VVT_BOSCH_QUICK_START:
return TT_VVT_BOSCH_QUICK_START;
2021-10-28 12:57:23 -07:00
case VVT_HONDA_K:
2020-08-26 10:30:55 -07:00
case VVT_FIRST_HALF:
case VVT_SECOND_HALF:
2022-08-31 21:18:06 -07:00
case VVT_MAP_V_TWIN:
2020-08-26 10:30:55 -07:00
return TT_ONE;
case VVT_FORD_ST170:
return TT_FORD_ST170;
case VVT_BARRA_3_PLUS_1:
return TT_VVT_BARRA_3_PLUS_1;
case VVT_MAZDA_SKYACTIV:
return TT_VVT_MAZDA_SKYACTIV;
2021-07-03 06:43:27 -07:00
case VVT_NISSAN_VQ:
return TT_VVT_NISSAN_VQ35;
2022-03-22 03:34:22 -07:00
case VVT_TOYOTA_4_1:
return TT_VVT_TOYOTA_4_1;
case VVT_MITSUBISHI_3A92:
return TT_VVT_MITSUBISHI_3A92;
case VVT_MITSUBISHI_6G75:
case VVT_NISSAN_MR:
return TT_NISSAN_MR18_CAM_VVT;
2020-08-26 10:30:55 -07:00
default:
2021-07-03 06:43:27 -07:00
firmwareError(OBD_PCM_Processor_Fault, "getVvtTriggerType for %s", getVvt_mode_e(vvtMode));
2021-07-03 06:48:59 -07:00
return TT_ONE; // we have to return something for the sake of -Werror=return-type
2020-08-26 10:30:55 -07:00
}
}
static void initVvtShape(TriggerWaveform& shape, const TriggerConfiguration& config, TriggerDecoderBase &initState) {
shape.initializeTriggerWaveform(FOUR_STROKE_CAM_SENSOR, config);
2021-02-08 18:28:57 -08:00
shape.initializeSyncPoint(initState, config);
2021-02-08 18:28:57 -08:00
}
2022-04-02 16:27:18 -07:00
void Engine::updateTriggerWaveform() {
static TriggerDecoderBase initState("init");
2020-08-26 20:35:11 -07:00
// Re-read config in case it's changed
triggerCentral.primaryTriggerConfiguration.update();
2021-02-08 19:21:02 -08:00
for (int camIndex = 0;camIndex < CAMS_PER_BANK;camIndex++) {
triggerCentral.vvtTriggerConfiguration[camIndex].update();
2021-02-08 19:21:02 -08:00
}
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2018-12-25 18:18:14 -08:00
// we have a confusing threading model so some synchronization would not hurt
chibios_rt::CriticalSectionLocker csl;
2018-12-25 18:18:14 -08:00
triggerCentral.triggerShape.initializeTriggerWaveform(lookupOperationMode(), triggerCentral.primaryTriggerConfiguration);
2018-12-25 18:18:14 -08:00
2021-05-23 17:52:18 -07:00
/**
* this is only useful while troubleshooting a new trigger shape in the field
* in very VERY rare circumstances
*/
if (engineConfiguration->overrideTriggerGaps) {
2021-05-25 14:15:48 -07:00
int gapIndex = 0;
2022-02-18 17:13:45 -08:00
// copy however many the user wants
for (; gapIndex < engineConfiguration->gapTrackingLengthOverride; gapIndex++) {
float gapOverrideFrom = engineConfiguration->triggerGapOverrideFrom[gapIndex];
float gapOverrideTo = engineConfiguration->triggerGapOverrideTo[gapIndex];
2021-10-30 07:45:32 -07:00
TRIGGER_WAVEFORM(setTriggerSynchronizationGap3(/*gapIndex*/gapIndex, gapOverrideFrom, gapOverrideTo));
2021-05-23 17:52:18 -07:00
}
2022-02-18 17:13:45 -08:00
// fill the remainder with the default gaps
for (; gapIndex < GAP_TRACKING_LENGTH; gapIndex++) {
engine->triggerCentral.triggerShape.syncronizationRatioFrom[gapIndex] = NAN;
engine->triggerCentral.triggerShape.syncronizationRatioTo[gapIndex] = NAN;
2021-05-25 14:15:48 -07:00
}
2021-05-23 17:52:18 -07:00
}
if (!TRIGGER_WAVEFORM(shapeDefinitionError)) {
2018-12-25 18:18:14 -08:00
/**
2022-05-10 01:41:39 -07:00
* 'initState' instance of TriggerDecoderBase is used only to initialize 'this' TriggerWaveform instance
2018-12-25 18:18:14 -08:00
* #192 BUG real hardware trigger events could be coming even while we are initializing trigger
*/
calculateTriggerSynchPoint(engine->triggerCentral.triggerShape,
initState);
2018-12-25 18:18:14 -08:00
engine->engineCycleEventCount = TRIGGER_WAVEFORM(getLength());
2018-12-25 18:18:14 -08:00
}
for (int camIndex = 0; camIndex < CAMS_PER_BANK; camIndex++) {
// todo: should 'vvtWithRealDecoder' be used here?
if (engineConfiguration->vvtMode[camIndex] != VVT_INACTIVE) {
initVvtShape(
triggerCentral.vvtShape[camIndex],
triggerCentral.vvtTriggerConfiguration[camIndex],
initState
);
}
2021-05-23 17:52:18 -07:00
}
2020-08-26 10:30:55 -07:00
// This is not the right place for this, but further refactoring has to happen before it can get moved.
engine->triggerCentral.triggerState.setNeedsDisambiguation(engine->triggerCentral.triggerShape.needsDisambiguation());
if (!TRIGGER_WAVEFORM(shapeDefinitionError)) {
prepareOutputSignals();
2018-12-25 18:18:14 -08:00
}
2019-01-31 14:55:23 -08:00
#endif /* EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT */
}
2020-12-27 14:41:28 -08:00
#if ANALOG_HW_CHECK_MODE
static void assertCloseTo(const char* msg, float actual, float expected) {
if (actual < 0.95f * expected || actual > 1.05f * expected) {
firmwareError(OBD_PCM_Processor_Fault, "%s validation failed actual=%f vs expected=%f", msg, actual, expected);
}
}
2020-12-27 14:41:28 -08:00
#endif // ANALOG_HW_CHECK_MODE
void Engine::periodicSlowCallback() {
2019-10-11 17:43:21 -07:00
ScopePerf perf(PE::EnginePeriodicSlowCallback);
// Re-read config in case it's changed
triggerCentral.primaryTriggerConfiguration.update();
2021-02-08 19:21:02 -08:00
for (int camIndex = 0;camIndex < CAMS_PER_BANK;camIndex++) {
triggerCentral.vvtTriggerConfiguration[camIndex].update();
2021-02-08 19:21:02 -08:00
}
2022-09-09 15:02:38 -07:00
efiWatchdog();
updateSlowSensors();
checkShutdown();
tpsAccelEnrichment.onNewValue(Sensor::getOrZero(SensorType::Tps1));
updateVrPwm();
2022-06-23 11:32:34 -07:00
enginePins.o2heater.setValue(engineConfiguration->forceO2Heating || engine->rpmCalculator.isRunning());
enginePins.starterRelayDisable.setValue(Sensor::getOrZero(SensorType::Rpm) < engineConfiguration->cranking.rpm);
2018-12-25 18:18:14 -08:00
updateGppwm();
engine->engineModules.apply_all([](auto & m) { m.onSlowCallback(); });
updateFans(module<AcController>().unmock().isAcEnabled());
#if EFI_BOOST_CONTROL
updateBoostControl();
#endif // EFI_BOOST_CONTROL
#if (BOARD_TLE8888_COUNT > 0)
tle8888startup();
#endif
#if EFI_DYNO_VIEW
updateDynoView();
#endif
slowCallBackWasInvoked = true;
#if EFI_PROD_CODE
void baroLps25Update();
baroLps25Update();
#endif // EFI_PROD_CODE
2020-12-27 14:41:28 -08:00
#if ANALOG_HW_CHECK_MODE
efiAssertVoid(OBD_PCM_Processor_Fault, isAdcChannelValid(engineConfiguration->clt.adcChannel), "No CLT setting");
efitimesec_t secondsNow = getTimeNowS();
#if ! HW_CHECK_ALWAYS_STIMULATE
fail("HW_CHECK_ALWAYS_STIMULATE required to have self-stimulation")
#endif
if (secondsNow > 2 && secondsNow < 180) {
assertCloseTo("RPM", Sensor::get(SensorType::Rpm).Value, HW_CHECK_RPM);
} else if (!hasFirmwareError() && secondsNow > 180) {
static bool isHappyTest = false;
if (!isHappyTest) {
setTriggerEmulatorRPM(5 * HW_CHECK_RPM);
efiPrintf("TEST PASSED");
isHappyTest = true;
}
}
assertCloseTo("clt", Sensor::getRaw(SensorType::Clt), 1.351f);
assertCloseTo("iat", Sensor::getRaw(SensorType::Iat), 2.245f);
assertCloseTo("aut1", Sensor::getRaw(SensorType::AuxTemp1), 2.750f);
assertCloseTo("aut2", Sensor::getRaw(SensorType::AuxTemp2), 3.176f);
2020-12-27 14:41:28 -08:00
#endif // ANALOG_HW_CHECK_MODE
2018-12-25 18:18:14 -08:00
}
2015-07-10 06:01:56 -07:00
/**
* We are executing these heavy (logarithm) methods from outside the trigger callbacks for performance reasons.
2016-01-01 14:02:49 -08:00
* See also periodicFastCallback
2015-07-10 06:01:56 -07:00
*/
void Engine::updateSlowSensors() {
updateSwitchInputs();
2020-12-26 18:31:41 -08:00
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL
2022-01-20 19:42:13 -08:00
int rpm = Sensor::getOrZero(SensorType::Rpm);
triggerCentral.isEngineSnifferEnabled = rpm < engineConfiguration->engineSnifferRpmThreshold;
sensorChartMode = rpm < engineConfiguration->sensorSnifferRpmThreshold ? engineConfiguration->sensorChartMode : SC_OFF;
2016-01-30 19:03:36 -08:00
engineState.updateSlowSensors();
2019-01-31 08:57:15 -08:00
#endif
2015-07-10 06:01:56 -07:00
}
2021-12-15 16:20:07 -08:00
#if EFI_GPIO_HARDWARE
static bool getClutchUpState() {
2021-12-15 14:48:21 -08:00
if (isBrainPinValid(engineConfiguration->clutchUpPin)) {
return engineConfiguration->clutchUpPinInverted ^ efiReadPin(engineConfiguration->clutchUpPin);
}
return engine->engineState.lua.clutchUpState;
2021-12-15 14:48:21 -08:00
}
static bool getBrakePedalState() {
if (isBrainPinValid(engineConfiguration->brakePedalPin)) {
2021-12-15 14:48:21 -08:00
return efiReadPin(engineConfiguration->brakePedalPin);
}
return engine->engineState.lua.brakePedalState;
2021-12-15 14:48:21 -08:00
}
#endif // EFI_GPIO_HARDWARE
2021-12-15 14:48:21 -08:00
void Engine::updateSwitchInputs() {
2020-12-26 18:31:41 -08:00
#if EFI_GPIO_HARDWARE
// this value is not used yet
if (isBrainPinValid(engineConfiguration->clutchDownPin)) {
engine->engineState.clutchDownState = engineConfiguration->clutchDownPinInverted ^ efiReadPin(engineConfiguration->clutchDownPin);
2020-12-26 18:31:41 -08:00
}
2022-07-07 21:51:53 -07:00
{
bool currentState;
if (hasAcToggle()) {
currentState = getAcToggle();
} else {
currentState = engine->engineState.lua.acRequestState;
}
AcController & acController = engine->module<AcController>().unmock();
2022-07-07 21:51:53 -07:00
if (acController.acButtonState != currentState) {
acController.acButtonState = currentState;
acController.acSwitchLastChangeTimeMs = US2MS(getTimeNowUs());
2020-12-26 18:31:41 -08:00
}
}
engine->engineState.clutchUpState = getClutchUpState();
2021-12-15 14:48:21 -08:00
if (isBrainPinValid(engineConfiguration->throttlePedalUpPin)) {
engine->module<IdleController>().unmock().throttlePedalUpState = efiReadPin(engineConfiguration->throttlePedalUpPin);
2020-12-26 18:31:41 -08:00
}
engine->engineState.brakePedalState = getBrakePedalState();
2021-12-15 14:48:21 -08:00
2020-12-26 18:31:41 -08:00
#endif // EFI_GPIO_HARDWARE
}
2021-03-15 07:23:19 -07:00
void Engine::onTriggerSignalEvent() {
2015-07-10 06:01:56 -07:00
isSpinning = true;
}
Engine::Engine() {
2016-12-18 07:02:38 -08:00
reset();
}
int Engine::getGlobalConfigurationVersion(void) const {
return globalConfigurationVersion;
}
2016-12-18 07:02:38 -08:00
void Engine::reset() {
2016-01-14 21:01:42 -08:00
/**
* it's important for fixAngle() that engineCycle field never has zero
*/
engineState.engineCycle = getEngineCycle(FOUR_STROKE_CRANK_SENSOR);
2015-07-10 06:01:56 -07:00
memset(&ignitionPin, 0, sizeof(ignitionPin));
2022-07-14 09:36:55 -07:00
resetLua();
2015-07-10 06:01:56 -07:00
}
2022-07-14 09:36:55 -07:00
void Engine::resetLua() {
// todo: https://github.com/rusefi/rusefi/issues/4308
engineState.lua = {};
engineState.lua.fuelMult = 1;
#if EFI_BOOST_CONTROL
boostController.resetLua();
#endif // EFI_BOOST_CONTROL
2022-07-14 09:36:55 -07:00
ignitionState.luaTimingAdd = 0;
ignitionState.luaTimingMult = 1;
module<IdleController>().unmock().luaAdd = 0;
2022-07-14 09:36:55 -07:00
}
2016-01-01 14:02:49 -08:00
2015-07-10 06:01:56 -07:00
/**
* Here we have a bunch of stuff which should invoked after configuration change
* so that we can prepare some helper structures
*/
void Engine::preCalculate() {
2020-05-09 19:43:39 -07:00
#if EFI_TUNER_STUDIO
// we take 2 bytes of crc32, no idea if it's right to call it crc16 or not
2020-05-09 21:59:32 -07:00
// we have a hack here - we rely on the fact that engineMake is the first of three relevant fields
engine->outputChannels.engineMakeCodeNameCrc16 = crc32(engineConfiguration->engineMake, 3 * VEHICLE_INFO_SIZE);
2020-06-04 21:43:05 -07:00
// we need and can empty warning message for CRC purposes
memset(config->warning_message, 0, sizeof(error_message_t));
engine->outputChannels.tuneCrc16 = crc32(config, sizeof(persistent_config_s));
2020-05-09 19:43:39 -07:00
#endif /* EFI_TUNER_STUDIO */
2015-07-10 06:01:56 -07:00
}
#if EFI_SHAFT_POSITION_INPUT
void Engine::OnTriggerStateProperState(efitick_t nowNt) {
rpmCalculator.setSpinningUp(nowNt);
}
2020-01-26 09:02:54 -08:00
void Engine::OnTriggerSynchronizationLost() {
// Needed for early instant-RPM detection
rpmCalculator.setStopSpinning();
triggerCentral.triggerState.resetTriggerState();
for (size_t i = 0; i < efi::size(triggerCentral.vvtState); i++) {
for (size_t j = 0; j < efi::size(triggerCentral.vvtState[0]); j++) {
triggerCentral.vvtState[i][j].resetTriggerState();
}
}
2020-01-26 09:02:54 -08:00
}
void Engine::OnTriggerSyncronization(bool wasSynchronized, bool isDecodingError) {
// TODO: this logic probably shouldn't be part of engine.cpp
2020-01-26 00:33:45 -08:00
2020-01-23 10:39:50 -08:00
// We only care about trigger shape once we have synchronized trigger. Anything could happen
// during first revolution and it's fine
if (wasSynchronized) {
enginePins.triggerDecoderErrorPin.setValue(isDecodingError);
// 'triggerStateListener is not null' means we are running a real engine and now just preparing trigger shape
// that's a bit of a hack, a sweet OOP solution would be a real callback or at least 'needDecodingErrorLogic' method?
if (isDecodingError) {
#if EFI_PROD_CODE
if (engineConfiguration->verboseTriggerSynchDetails || (triggerCentral.triggerState.someSortOfTriggerError() && !engineConfiguration->silentTriggerError)) {
efiPrintf("error: synchronizationPoint @ index %d expected %d/%d got %d/%d",
triggerCentral.triggerState.currentCycle.current_index,
2022-09-11 00:46:50 -07:00
TRIGGER_WAVEFORM(getExpectedEventCount(TriggerWheel::T_PRIMARY)),
TRIGGER_WAVEFORM(getExpectedEventCount(TriggerWheel::T_SECONDARY)),
triggerCentral.triggerState.currentCycle.eventCount[0],
triggerCentral.triggerState.currentCycle.eventCount[1]);
}
#endif /* EFI_PROD_CODE */
2020-01-23 10:39:50 -08:00
}
engine->triggerErrorDetection.add(isDecodingError);
}
2020-01-22 10:25:35 -08:00
}
#endif
void Engine::injectEngineReferences() {
triggerCentral.primaryTriggerConfiguration.update();
2021-02-08 19:21:02 -08:00
for (int camIndex = 0;camIndex < CAMS_PER_BANK;camIndex++) {
triggerCentral.vvtTriggerConfiguration[camIndex].update();
2021-02-08 19:21:02 -08:00
}
}
void Engine::setConfig() {
efi::clear(config);
injectEngineReferences();
2015-07-10 06:01:56 -07:00
}
2022-09-09 15:02:38 -07:00
void Engine::efiWatchdog() {
2015-07-10 06:01:56 -07:00
#if EFI_ENGINE_CONTROL
if (isRunningPwmTest)
return;
if (module<PrimeController>()->isPriming()) {
return;
}
static efitimems_t mostRecentMs = 0;
efitimems_t msNow = getTimeNowMs();
if (engineConfiguration->tempBooleanForVerySpecialLogic && engine->configBurnTimer.hasElapsedSec(5)) {
if (mostRecentMs != 0) {
efitimems_t gapInMs = msNow - mostRecentMs;
if (gapInMs > 500) {
firmwareError(WATCH_DOG_SECONDS, "gap in time: now=%d mS, was %d mS, gap=%dmS",
msNow, mostRecentMs, gapInMs);
}
}
}
mostRecentMs = msNow;
2015-07-10 06:01:56 -07:00
if (!isSpinning) {
2016-11-03 20:02:58 -07:00
if (!isRunningBenchTest() && enginePins.stopPins()) {
2016-11-04 19:02:42 -07:00
// todo: make this a firmwareError assuming functional tests would run
warning(CUSTOM_ERR_2ND_WATCHDOG, "Some pins were turned off by 2nd pass watchdog");
2015-07-10 06:01:56 -07:00
}
return;
}
2015-07-10 06:01:56 -07:00
/**
* todo: better watch dog implementation should be implemented - see
* http://sourceforge.net/p/rusefi/tickets/96/
*/
if (engine->triggerCentral.engineMovedRecently()) {
// Engine moved recently, no need to safe pins.
2015-07-10 06:01:56 -07:00
return;
}
isSpinning = false;
2016-12-18 07:02:38 -08:00
ignitionEvents.isReady = false;
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE || EFI_SIMULATOR
efiPrintf("engine has STOPPED");
2015-07-10 06:01:56 -07:00
triggerInfo();
#endif
2016-11-03 20:02:58 -07:00
enginePins.stopPins();
2015-07-10 06:01:56 -07:00
#endif
}
void Engine::checkShutdown() {
2019-04-12 19:07:03 -07:00
#if EFI_MAIN_RELAY_CONTROL
// if we are already in the "ignition_on" mode, then do nothing
if (ignitionOnTimeNt > 0) {
return;
}
// 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
// in shutdown mode, we need a small cooldown time between the ignition off and on
/* this needs work or tests
if (stopEngineRequestTimeNt == 0 || (getTimeNowNt() - stopEngineRequestTimeNt) > US2NT(engineStopWaitTimeoutUs)) {
// if the ignition key is turned on again,
// we cancel the shutdown mode, but only if all shutdown procedures are complete
const float vBattThresholdOn = 8.0f;
2021-12-01 08:22:13 -08:00
// we fallback into zero instead of VBAT_FALLBACK_VALUE because it's not safe to false-trigger the "ignition on" event,
// and we want to turn on the main relay only when 100% sure.
if ((Sensor::get(SensorType::BatteryVoltage).value_or(0) > vBattThresholdOn) && !isInShutdownMode()) {
ignitionOnTimeNt = getTimeNowNt();
efiPrintf("Ignition voltage detected!");
if (stopEngineRequestTimeNt != 0) {
efiPrintf("Cancel the engine shutdown!");
stopEngineRequestTimeNt = 0;
}
}
}
*/
#endif /* EFI_MAIN_RELAY_CONTROL */
}
bool Engine::isInMainRelayBench() {
2021-01-10 20:46:50 -08:00
if (mainRelayBenchStartNt == 0) {
return false;
}
return (getTimeNowNt() - mainRelayBenchStartNt) < NT_PER_SECOND;
}
bool Engine::isInShutdownMode() const {
// TODO: this logic is currently broken
#if 0 && EFI_MAIN_RELAY_CONTROL && EFI_PROD_CODE
// if we are in "ignition_on" mode and not in shutdown mode
if (stopEngineRequestTimeNt == 0 && ignitionOnTimeNt > 0) {
const float vBattThresholdOff = 5.0f;
// start the shutdown process if the ignition voltage dropped low
2021-03-11 19:52:34 -08:00
if (Sensor::get(SensorType::BatteryVoltage).value_or(VBAT_FALLBACK_VALUE) <= vBattThresholdOff) {
scheduleStopEngine();
}
}
// we are not in the shutdown mode?
if (stopEngineRequestTimeNt == 0) {
return false;
}
2021-01-10 20:46:50 -08:00
const efitick_t turnOffWaitTimeoutNt = NT_PER_SECOND;
// We don't want any transients to step in, so we wait at least 1 second whatever happens.
// Also it's good to give the stepper motor some time to start moving to the initial position (or parking)
2021-01-10 20:46:50 -08:00
if ((getTimeNowNt() - stopEngineRequestTimeNt) < turnOffWaitTimeoutNt)
return true;
2021-01-10 20:46:50 -08:00
const efitick_t engineSpinningWaitTimeoutNt = 5 * NT_PER_SECOND;
// The engine is still spinning! Give it some time to stop (but wait no more than 5 secs)
2021-01-10 20:46:50 -08:00
if (isSpinning && (getTimeNowNt() - stopEngineRequestTimeNt) < engineSpinningWaitTimeoutNt)
return true;
// The idle motor valve is still moving! Give it some time to park (but wait no more than 10 secs)
// Usually it can move to the initial 'cranking' position or zero 'parking' position.
2021-01-10 20:46:50 -08:00
const efitick_t idleMotorWaitTimeoutNt = 10 * NT_PER_SECOND;
if (isIdleMotorBusy() && (getTimeNowNt() - stopEngineRequestTimeNt) < idleMotorWaitTimeoutNt)
return true;
#endif /* EFI_MAIN_RELAY_CONTROL */
return false;
}
bool Engine::isMainRelayEnabled() const {
#if EFI_MAIN_RELAY_CONTROL
return enginePins.mainRelay.getLogicValue();
#else
// if no main relay control, we assume it's always turned on
return true;
#endif /* EFI_MAIN_RELAY_CONTROL */
}
injection_mode_e getCurrentInjectionMode() {
return getEngineRotationState()->isCranking() ? engineConfiguration->crankingInjectionMode : engineConfiguration->injectionMode;
2016-11-30 19:06:43 -08:00
}
2015-07-10 06:01:56 -07:00
/**
* The idea of this method is to execute all heavy calculations in a lower-priority thread,
2016-01-01 14:02:49 -08:00
* so that trigger event handler/IO scheduler tasks are faster.
2015-07-10 06:01:56 -07:00
*/
void Engine::periodicFastCallback() {
2019-10-11 17:43:21 -07:00
ScopePerf pc(PE::EnginePeriodicFastCallback);
2016-01-01 14:02:49 -08:00
#if EFI_MAP_AVERAGING
refreshMapAveragingPreCalc();
#endif
2016-01-01 14:02:49 -08:00
engineState.periodicFastCallback();
tachSignalCallback();
engine->engineModules.apply_all([](auto & m) { m.onFastCallback(); });
2015-07-10 06:01:56 -07:00
}
void doScheduleStopEngine() {
efiPrintf("Starting doScheduleStopEngine");
engine->limpManager.stopEngine();
engine->ignitionOnTimeNt = 0;
// todo: initiate stepper motor parking
// make sure we have stored all the info
2020-09-10 18:26:21 -07:00
#if EFI_PROD_CODE
//todo: FIX kinetis build with this line
//backupRamFlush();
2020-09-10 18:26:21 -07:00
#endif // EFI_PROD_CODE
}
EngineRotationState * getEngineRotationState() {
return &engine->rpmCalculator;
}
EngineState * getEngineState() {
return &engine->engineState;
}
2022-09-07 16:35:52 -07:00
TunerStudioOutputChannels *getTunerStudioOutputChannels() {
return &engine->outputChannels;
}
ExecutorInterface *getExecutorInterface() {
return &engine->executor;
}
TriggerCentral * getTriggerCentral() {
return &engine->triggerCentral;
}
LimpManager * getLimpManager() {
return &engine->limpManager;
}