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
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "engine_state.h"
|
|
|
|
#include "efiGpio.h"
|
|
|
|
#include "trigger_central.h"
|
|
|
|
#include "fuel_math.h"
|
|
|
|
#include "engine_math.h"
|
|
|
|
#include "advance_map.h"
|
|
|
|
#include "speed_density.h"
|
2016-01-01 16:02:59 -08:00
|
|
|
#include "advance_map.h"
|
2016-01-25 00:02:33 -08:00
|
|
|
#include "efilib2.h"
|
2017-06-24 22:35:46 -07:00
|
|
|
#include "settings.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-23 15:01:40 -08:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "injector_central.h"
|
|
|
|
#else
|
|
|
|
#define isRunningBenchTest() true
|
2016-01-23 15:01:40 -08:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static LoggingWithStorage logger("engine");
|
|
|
|
|
2016-01-01 16:02:59 -08:00
|
|
|
extern fuel_Map3D_t veMap;
|
2016-07-01 18:01:48 -07:00
|
|
|
extern afr_Map3D_t afrMap;
|
2016-01-01 16:02:59 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
2016-02-10 16:01:47 -08:00
|
|
|
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
#endif
|
|
|
|
|
2016-01-23 15:01:40 -08:00
|
|
|
MockAdcState::MockAdcState() {
|
|
|
|
memset(hasMockAdc, 0, sizeof(hasMockAdc));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_ENABLE_MOCK_ADC || EFI_SIMULATOR
|
|
|
|
void MockAdcState::setMockVoltage(int hwChannel, float voltage) {
|
|
|
|
scheduleMsg(&logger, "fake voltage: channel %d value %f", hwChannel, voltage);
|
|
|
|
|
|
|
|
fakeAdcValues[hwChannel] = voltsToAdc(voltage);
|
|
|
|
hasMockAdc[hwChannel] = true;
|
|
|
|
}
|
|
|
|
#endif /* EFI_ENABLE_MOCK_ADC */
|
|
|
|
|
|
|
|
int MockAdcState::getMockAdcValue(int hwChannel) {
|
|
|
|
return fakeAdcValues[hwChannel];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2016-01-30 19:03:36 -08:00
|
|
|
int rpm = rpmCalculator.rpmValue;
|
|
|
|
isEngineChartEnabled = CONFIG(isEngineChartEnabled) && rpm < CONFIG(engineSnifferRpmThreshold);
|
|
|
|
sensorChartMode = rpm < CONFIG(sensorSnifferRpmThreshold) ? boardConfiguration->sensorChartMode : SC_OFF;
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
engineState.updateSlowSensors(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-11-24 16:16:00 -08:00
|
|
|
// todo: move this logic somewhere to sensors folder?
|
2015-07-10 06:01:56 -07:00
|
|
|
if (engineConfiguration->fuelLevelSensor != EFI_ADC_NONE) {
|
|
|
|
float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor);
|
2017-03-06 23:24:57 -08:00
|
|
|
sensors.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0,
|
2015-07-10 06:01:56 -07:00
|
|
|
boardConfiguration->fuelLevelFullTankVoltage, 100,
|
|
|
|
fuelLevelVoltage);
|
|
|
|
}
|
2017-05-15 20:28:49 -07:00
|
|
|
sensors.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) ? getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) : 12;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
engineState.injectorLag = getInjectorLag(sensors.vBatt PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-11-20 12:01:48 -08:00
|
|
|
void Engine::onTriggerSignalEvent(efitick_t nowNt) {
|
2015-07-10 06:01:56 -07:00
|
|
|
isSpinning = true;
|
2017-11-20 12:01:48 -08:00
|
|
|
lastTriggerToothEventTimeNt = nowNt;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2016-12-18 07:02:38 -08:00
|
|
|
Engine::Engine() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
Engine::Engine(persistent_config_s *config) {
|
2016-12-18 07:02:38 -08:00
|
|
|
setConfig(config);
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-08-17 06:10:22 -07:00
|
|
|
Accelerometer::Accelerometer() {
|
|
|
|
x = y = z = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:24:57 -08:00
|
|
|
SensorsState::SensorsState() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorsState::reset() {
|
|
|
|
fuelTankGauge = vBatt = 0;
|
|
|
|
iat = clt = NAN;
|
|
|
|
}
|
|
|
|
|
2016-12-18 07:02:38 -08:00
|
|
|
void Engine::reset() {
|
2017-02-24 16:20:33 -08:00
|
|
|
withError = isEngineChartEnabled = false;
|
2017-09-17 19:05:03 -07:00
|
|
|
etbAutoTune = false;
|
2016-01-30 19:03:36 -08:00
|
|
|
sensorChartMode = SC_OFF;
|
2016-08-30 18:02:38 -07:00
|
|
|
actualLastInjection = 0;
|
2017-07-25 17:37:46 -07:00
|
|
|
fsioTimingAdjustment = 0;
|
2016-08-30 19:02:21 -07:00
|
|
|
isAlternatorControlEnabled = false;
|
2017-05-04 14:03:23 -07:00
|
|
|
callFromPitStopEndTime = 0;
|
2017-07-26 17:27:08 -07:00
|
|
|
rpmHardLimitTimestamp = 0;
|
2016-08-30 19:02:21 -07:00
|
|
|
wallFuelCorrection = 0;
|
2016-01-14 21:01:42 -08:00
|
|
|
/**
|
|
|
|
* it's important for fixAngle() that engineCycle field never has zero
|
|
|
|
*/
|
|
|
|
engineCycle = getEngineCycle(FOUR_STROKE_CRANK_SENSOR);
|
2017-11-20 12:01:48 -08:00
|
|
|
lastTriggerToothEventTimeNt = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
isCylinderCleanupMode = false;
|
|
|
|
engineCycleEventCount = 0;
|
|
|
|
stopEngineRequestTimeNt = 0;
|
|
|
|
isRunningPwmTest = false;
|
|
|
|
isTestMode = false;
|
|
|
|
isSpinning = false;
|
2017-05-11 05:32:08 -07:00
|
|
|
isCltBroken = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
adcToVoltageInputDividerCoefficient = NAN;
|
2017-03-06 23:24:57 -08:00
|
|
|
sensors.reset();
|
2015-07-10 06:01:56 -07:00
|
|
|
memset(&ignitionPin, 0, sizeof(ignitionPin));
|
|
|
|
|
|
|
|
knockNow = false;
|
|
|
|
knockEver = false;
|
|
|
|
knockCount = 0;
|
|
|
|
knockDebug = false;
|
2015-07-11 13:01:31 -07:00
|
|
|
knockVolts = 0;
|
2016-01-26 20:01:44 -08:00
|
|
|
iHead = NULL;
|
|
|
|
|
2015-07-11 13:01:31 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
timeOfLastKnockEvent = 0;
|
2017-11-06 19:29:39 -08:00
|
|
|
injectionDuration = 0;
|
2017-05-15 02:08:17 -07:00
|
|
|
clutchDownState = clutchUpState = brakePedalState = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-24 04:35:44 -07:00
|
|
|
FuelConsumptionState::FuelConsumptionState() {
|
|
|
|
perSecondConsumption = perSecondAccumulator = 0;
|
|
|
|
perMinuteConsumption = perMinuteAccumulator = 0;
|
|
|
|
accumulatedSecond = accumulatedMinute = -1;
|
|
|
|
}
|
|
|
|
|
2017-08-03 19:30:47 -07:00
|
|
|
TransmissionState::TransmissionState() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
EngineState::EngineState() {
|
2016-01-18 11:01:39 -08:00
|
|
|
dwellAngle = 0;
|
2016-01-01 14:02:49 -08:00
|
|
|
engineNoiseHipLevel = 0;
|
2016-02-06 09:02:24 -08:00
|
|
|
injectorLag = 0;
|
2016-07-13 16:03:06 -07:00
|
|
|
warningCounter = 0;
|
2016-07-13 19:02:35 -07:00
|
|
|
lastErrorCode = 0;
|
2016-12-06 20:03:39 -08:00
|
|
|
crankingTime = 0;
|
2016-12-18 07:02:38 -08:00
|
|
|
timeSinceCranking = 0;
|
2017-05-21 20:17:08 -07:00
|
|
|
vssEventCounter = 0;
|
2016-07-13 19:02:35 -07:00
|
|
|
targetAFR = 0;
|
|
|
|
tpsAccelEnrich = 0;
|
|
|
|
tChargeK = 0;
|
2017-01-05 18:12:06 -08:00
|
|
|
cltTimingCorrection = 0;
|
2016-08-31 21:02:04 -07:00
|
|
|
runningFuel = baseFuel = currentVE = 0;
|
2016-07-14 20:02:55 -07:00
|
|
|
timeOfPreviousWarning = -10;
|
2017-03-06 23:24:57 -08:00
|
|
|
baseTableFuel = iatFuelCorrection = 0;
|
2017-01-22 21:06:44 -08:00
|
|
|
fuelPidCorrection = 0;
|
2017-03-06 23:24:57 -08:00
|
|
|
cltFuelCorrection = postCrankingFuelCorrection = 0;
|
2016-08-31 21:02:04 -07:00
|
|
|
warmupTargetAfr = airMass = 0;
|
2017-03-06 23:24:57 -08:00
|
|
|
baroCorrection = timingAdvance = 0;
|
2016-08-31 21:02:04 -07:00
|
|
|
sparkDwell = mapAveragingDuration = 0;
|
2017-02-09 19:02:07 -08:00
|
|
|
totalLoggedBytes = injectionOffset = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
engine->sensors.iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
engine->sensors.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-11-15 11:30:13 -08:00
|
|
|
engine->sensors.oilPressure = getOilPressure(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-02-10 14:01:44 -08:00
|
|
|
|
2017-05-29 08:31:07 -07:00
|
|
|
warmupTargetAfr = interpolate2d("warm", engine->sensors.clt, engineConfiguration->warmupTargetAfrBins,
|
2016-02-10 14:01:44 -08:00
|
|
|
engineConfiguration->warmupTargetAfr, WARMUP_TARGET_AFR_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2016-12-06 20:03:39 -08:00
|
|
|
efitick_t nowNt = getTimeNowNt();
|
2017-07-06 09:03:17 -07:00
|
|
|
if (ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
2016-12-06 20:03:39 -08:00
|
|
|
crankingTime = nowNt;
|
2017-11-02 15:10:42 -07:00
|
|
|
timeSinceCranking = 0.0f;
|
2016-12-06 20:03:39 -08:00
|
|
|
} else {
|
|
|
|
timeSinceCranking = nowNt - crankingTime;
|
|
|
|
}
|
|
|
|
|
2017-07-08 04:19:26 -07:00
|
|
|
int rpm = GET_RPM();
|
2017-05-15 20:28:49 -07:00
|
|
|
sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-01-01 14:02:49 -08:00
|
|
|
dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm);
|
2017-05-15 20:28:49 -07:00
|
|
|
engine->sensors.currentAfr = getAfr(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-01-01 14:02:49 -08:00
|
|
|
|
2016-08-26 15:02:39 -07:00
|
|
|
// todo: move this into slow callback, no reason for IAT corr to be here
|
2017-05-15 20:28:49 -07:00
|
|
|
iatFuelCorrection = getIatFuelCorrection(engine->sensors.iat PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-08-26 15:02:39 -07:00
|
|
|
// todo: move this into slow callback, no reason for CLT corr to be here
|
2017-03-06 23:24:57 -08:00
|
|
|
if (boardConfiguration->useWarmupPidAfr && engine->sensors.clt < engineConfiguration->warmupAfrThreshold) {
|
2016-02-07 13:01:55 -08:00
|
|
|
if (rpm < 200) {
|
|
|
|
cltFuelCorrection = 1;
|
2016-02-06 14:01:38 -08:00
|
|
|
warmupAfrPid.reset();
|
2016-02-07 13:01:55 -08:00
|
|
|
} else {
|
2017-03-06 23:24:57 -08:00
|
|
|
cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, engine->sensors.currentAfr, 1);
|
2016-02-07 13:01:55 -08:00
|
|
|
}
|
2016-02-10 16:01:47 -08:00
|
|
|
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
|
2017-02-12 18:04:18 -08:00
|
|
|
if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) {
|
2016-02-15 15:02:03 -08:00
|
|
|
tsOutputChannels.debugFloatField1 = warmupTargetAfr;
|
2016-02-10 16:01:47 -08:00
|
|
|
warmupAfrPid.postState(&tsOutputChannels);
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-06 09:02:24 -08:00
|
|
|
|
|
|
|
} else {
|
2017-05-15 20:28:49 -07:00
|
|
|
cltFuelCorrection = getCltFuelCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-02-06 09:02:24 -08:00
|
|
|
}
|
2016-01-01 14:02:49 -08:00
|
|
|
|
2017-11-02 15:10:42 -07:00
|
|
|
// post-cranking fuel enrichment.
|
|
|
|
// for compatibility reasons, apply only if the factor is greater than zero (0.01 margin used)
|
|
|
|
if (engineConfiguration->postCrankingFactor > 0.01f) {
|
|
|
|
// convert to microsecs and then to seconds
|
|
|
|
float timeSinceCrankingInSecs = NT2US(timeSinceCranking) / 1000000.0f;
|
|
|
|
// use interpolation for correction taper
|
|
|
|
postCrankingFuelCorrection = interpolateClamped(0.0f, engineConfiguration->postCrankingFactor,
|
|
|
|
engineConfiguration->postCrankingDurationSec, 1.0f, timeSinceCrankingInSecs);
|
|
|
|
} else {
|
|
|
|
postCrankingFuelCorrection = 1.0f;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
cltTimingCorrection = getCltTimingCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-01-05 18:12:06 -08:00
|
|
|
|
2017-05-29 08:31:07 -07:00
|
|
|
engineNoiseHipLevel = interpolate2d("knock", rpm, engineConfiguration->knockNoiseRpmBins,
|
2016-01-01 16:02:59 -08:00
|
|
|
engineConfiguration->knockNoise, ENGINE_NOISE_CURVE_SIZE);
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
baroCorrection = getBaroCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-01-01 16:02:59 -08:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
injectionOffset = getinjectionOffset(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-01-01 16:02:59 -08:00
|
|
|
|
2016-08-28 13:02:34 -07:00
|
|
|
if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) {
|
2017-03-06 23:24:57 -08:00
|
|
|
float coolantC = ENGINE(sensors.clt);
|
|
|
|
float intakeC = ENGINE(sensors.iat);
|
2017-05-15 20:28:49 -07:00
|
|
|
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC PASS_ENGINE_PARAMETER_SUFFIX));
|
2016-01-01 16:02:59 -08:00
|
|
|
float map = getMap();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* *0.01 because of https://sourceforge.net/p/rusefi/tickets/153/
|
|
|
|
*/
|
2016-03-11 12:01:58 -08:00
|
|
|
currentVE = baroCorrection * veMap.getValue(rpm, map) * 0.01;
|
2016-06-09 08:02:31 -07:00
|
|
|
targetAFR = afrMap.getValue(rpm, map);
|
2016-01-01 16:02:59 -08:00
|
|
|
} else {
|
2017-04-12 20:25:58 -07:00
|
|
|
baseTableFuel = getBaseTableFuel(rpm, engineLoad);
|
2016-01-01 16:02:59 -08: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() {
|
2017-02-18 12:01:47 -08:00
|
|
|
sparkTable.preCalc(engineConfiguration->sparkDwellRpmBins,
|
|
|
|
engineConfiguration->sparkDwellValues);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Here we prepare a fast, index-based MAF lookup from a slower curve description
|
|
|
|
*/
|
|
|
|
for (int i = 0; i < MAF_DECODING_CACHE_SIZE; i++) {
|
|
|
|
float volts = i / MAF_DECODING_CACHE_MULT;
|
2017-05-29 08:31:07 -07:00
|
|
|
float maf = interpolate2d("maf", volts, config->mafDecodingBins,
|
2015-07-10 06:01:56 -07:00
|
|
|
config->mafDecoding, MAF_DECODING_COUNT);
|
|
|
|
mafDecodingLookup[i] = maf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 07:02:38 -08:00
|
|
|
void Engine::setConfig(persistent_config_s *config) {
|
2016-01-25 00:02:33 -08:00
|
|
|
this->config = config;
|
|
|
|
engineConfiguration = &config->engineConfiguration;
|
|
|
|
memset(config, 0, sizeof(persistent_config_s));
|
2017-05-29 20:15:07 -07:00
|
|
|
engineState.warmupAfrPid.init(&config->engineConfiguration.warmupAfrPid);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::printKnockState(void) {
|
|
|
|
scheduleMsg(&logger, "knock now=%s/ever=%s", boolToString(knockNow), boolToString(knockEver));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::knockLogic(float knockVolts) {
|
2015-07-11 13:01:31 -07:00
|
|
|
this->knockVolts = knockVolts;
|
2015-07-10 06:01:56 -07:00
|
|
|
knockNow = knockVolts > engineConfiguration->knockVThreshold;
|
|
|
|
/**
|
|
|
|
* KnockCount is directly proportional to the degrees of ignition
|
|
|
|
* advance removed
|
|
|
|
* ex: degrees to subtract = knockCount;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO use knockLevel as a factor for amount of ignition advance
|
|
|
|
* to remove
|
|
|
|
* Perhaps allow the user to set a multiplier
|
|
|
|
* ex: degrees to subtract = knockCount + (knockLevel * X)
|
|
|
|
* X = user configurable multiplier
|
|
|
|
*/
|
|
|
|
if (knockNow) {
|
|
|
|
knockEver = true;
|
|
|
|
timeOfLastKnockEvent = getTimeNowUs();
|
|
|
|
if (knockCount < engineConfiguration->maxKnockSubDeg)
|
|
|
|
knockCount++;
|
|
|
|
} else if (knockCount >= 1) {
|
|
|
|
knockCount--;
|
|
|
|
} else {
|
|
|
|
knockCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::watchdog() {
|
|
|
|
#if EFI_ENGINE_CONTROL
|
|
|
|
if (isRunningPwmTest)
|
|
|
|
return;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
efitick_t nowNt = getTimeNowNt();
|
2017-11-04 16:35:38 -07:00
|
|
|
#ifndef RPM_LOW_THRESHOLD
|
|
|
|
#define RPM_LOW_THRESHOLD 240
|
|
|
|
#endif
|
2017-11-20 12:01:48 -08:00
|
|
|
// note that we are ignoring the number of tooth here - we
|
|
|
|
// check for duration between tooth as if we only have one tooth per revolution which is not the case
|
2017-11-04 16:35:38 -07:00
|
|
|
#define REVOLUTION_TIME_HIGH_THRESHOLD (60 * 1000000LL / RPM_LOW_THRESHOLD)
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* todo: better watch dog implementation should be implemented - see
|
|
|
|
* http://sourceforge.net/p/rusefi/tickets/96/
|
|
|
|
*
|
|
|
|
* note that the result of this subtraction could be negative, that would happen if
|
|
|
|
* we have a trigger event between the time we've invoked 'getTimeNow' and here
|
|
|
|
*/
|
2017-11-20 12:01:48 -08:00
|
|
|
efitick_t timeSinceLastTriggerEvent = nowNt - lastTriggerToothEventTimeNt;
|
2017-11-04 16:35:38 -07:00
|
|
|
if (timeSinceLastTriggerEvent < US2NT(REVOLUTION_TIME_HIGH_THRESHOLD)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
isSpinning = false;
|
2016-12-18 07:02:38 -08:00
|
|
|
ignitionEvents.isReady = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_PROD_CODE || EFI_SIMULATOR
|
|
|
|
scheduleMsg(&logger, "engine has STOPPED");
|
|
|
|
scheduleMsg(&logger, "templog engine has STOPPED [%x][%x] [%x][%x] %d",
|
|
|
|
(int)(nowNt >> 32), (int)nowNt,
|
2017-11-20 12:01:48 -08:00
|
|
|
(int)(lastTriggerToothEventTimeNt >> 32), (int)lastTriggerToothEventTimeNt,
|
2015-07-10 06:01:56 -07:00
|
|
|
(int)timeSinceLastTriggerEvent
|
|
|
|
);
|
|
|
|
triggerInfo();
|
|
|
|
#endif
|
|
|
|
|
2016-11-03 20:02:58 -07:00
|
|
|
enginePins.stopPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-24 22:35:46 -07:00
|
|
|
void Engine::checkShutdown() {
|
|
|
|
#if EFI_MAIN_RELAY_CONTROL || defined(__DOXYGEN__)
|
|
|
|
int rpm = rpmCalculator.rpmValue;
|
|
|
|
|
|
|
|
const float vBattThreshold = 5.0f;
|
2017-07-01 14:29:56 -07:00
|
|
|
if (isValidRpm(rpm) && sensors.vBatt < vBattThreshold && stopEngineRequestTimeNt == 0) {
|
2017-06-24 22:35:46 -07:00
|
|
|
stopEngine();
|
|
|
|
// todo: add stepper motor parking
|
|
|
|
}
|
|
|
|
#endif /* EFI_MAIN_RELAY_CONTROL */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Engine::isInShutdownMode() {
|
|
|
|
#if EFI_MAIN_RELAY_CONTROL || defined(__DOXYGEN__)
|
|
|
|
if (stopEngineRequestTimeNt == 0) // the shutdown procedure is not started
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const efitime_t engineStopWaitTimeoutNt = 5LL * 1000000LL;
|
|
|
|
// The engine is still spinning! Give it some time to stop (but wait no more than 5 secs)
|
|
|
|
if (isSpinning && (getTimeNowNt() - stopEngineRequestTimeNt) < US2NT(engineStopWaitTimeoutNt))
|
|
|
|
return true;
|
|
|
|
// todo: add checks for stepper motor parking
|
|
|
|
#endif /* EFI_MAIN_RELAY_CONTROL */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
injection_mode_e Engine::getCurrentInjectionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2016-01-25 09:01:30 -08:00
|
|
|
int rpm = rpmCalculator.rpmValue;
|
2017-07-06 05:49:55 -07:00
|
|
|
return rpmCalculator.isCranking(PASS_ENGINE_PARAMETER_SIGNATURE) ? CONFIG(crankingInjectionMode) : CONFIG(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
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
void Engine::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
int rpm = rpmCalculator.rpmValue;
|
|
|
|
|
2016-01-01 14:02:49 -08:00
|
|
|
if (isValidRpm(rpm)) {
|
|
|
|
MAP_sensor_config_s * c = &engineConfiguration->map;
|
2017-05-29 08:31:07 -07:00
|
|
|
angle_t start = interpolate2d("mapa", rpm, c->samplingAngleBins, c->samplingAngle, MAP_ANGLE_SIZE);
|
2016-01-01 14:02:49 -08:00
|
|
|
|
|
|
|
angle_t offsetAngle = TRIGGER_SHAPE(eventAngles[CONFIG(mapAveragingSchedulingAtIndex)]);
|
|
|
|
|
|
|
|
for (int i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
|
|
|
|
angle_t cylinderOffset = getEngineCycle(engineConfiguration->operationMode) * i / engineConfiguration->specs.cylindersCount;
|
|
|
|
float cylinderStart = start + cylinderOffset - offsetAngle + tdcPosition();
|
2016-12-05 19:01:54 -08:00
|
|
|
fixAngle(cylinderStart, "cylinderStart");
|
2016-01-01 14:02:49 -08:00
|
|
|
engine->engineState.mapAveragingStart[i] = cylinderStart;
|
|
|
|
}
|
2017-05-29 08:31:07 -07:00
|
|
|
engine->engineState.mapAveragingDuration = interpolate2d("samp", rpm, c->samplingWindowBins, c->samplingWindow, MAP_WINDOW_SIZE);
|
2016-01-01 14:02:49 -08:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
|
|
|
|
engine->engineState.mapAveragingStart[i] = NAN;
|
|
|
|
}
|
|
|
|
engine->engineState.mapAveragingDuration = NAN;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
engineState.periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-01-25 00:02:33 -08:00
|
|
|
|
2016-01-30 19:03:36 -08:00
|
|
|
engine->m.beforeFuelCalc = GET_TIMESTAMP();
|
2017-11-06 19:29:39 -08:00
|
|
|
ENGINE(injectionDuration) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-01-30 19:03:36 -08:00
|
|
|
engine->m.fuelCalcTime = GET_TIMESTAMP() - engine->m.beforeFuelCalc;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
StartupFuelPumping::StartupFuelPumping() {
|
|
|
|
isTpsAbove50 = false;
|
|
|
|
pumpsCounter = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-15 18:25:32 -07:00
|
|
|
void StartupFuelPumping::setPumpsCounter(int newValue) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (pumpsCounter != newValue) {
|
|
|
|
pumpsCounter = newValue;
|
|
|
|
|
|
|
|
if (pumpsCounter == PUMPS_TO_PRIME) {
|
|
|
|
scheduleMsg(&logger, "let's squirt prime pulse %f", pumpsCounter);
|
|
|
|
pumpsCounter = 0;
|
|
|
|
} else {
|
|
|
|
scheduleMsg(&logger, "setPumpsCounter %d", pumpsCounter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
void StartupFuelPumping::update(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
if (engine->rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE) == 0) {
|
|
|
|
bool isTpsAbove50 = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) >= 50;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
if (this->isTpsAbove50 != isTpsAbove50) {
|
2017-05-15 18:25:32 -07:00
|
|
|
setPumpsCounter(pumpsCounter + 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Engine is not stopped - not priming pumping mode
|
|
|
|
*/
|
2017-05-15 18:25:32 -07:00
|
|
|
setPumpsCounter(0);
|
2015-07-10 06:01:56 -07:00
|
|
|
isTpsAbove50 = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|