2019-01-04 21:32:56 -08:00
|
|
|
/*
|
|
|
|
* engine2.cpp
|
|
|
|
*
|
|
|
|
* @date Jan 5, 2019
|
2020-01-07 21:02:40 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2019-01-04 21:32:56 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
// todo: move this code to more proper locations
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2021-08-03 19:05:01 -07:00
|
|
|
|
2022-09-07 12:56:45 -07:00
|
|
|
|
2019-01-04 21:32:56 -08:00
|
|
|
#include "speed_density.h"
|
|
|
|
#include "fuel_math.h"
|
|
|
|
#include "advance_map.h"
|
|
|
|
#include "aux_valves.h"
|
2020-05-31 13:59:05 -07:00
|
|
|
#include "closed_loop_fuel.h"
|
2020-12-01 10:03:42 -08:00
|
|
|
#include "launch_control.h"
|
2021-03-03 04:30:56 -08:00
|
|
|
#include "injector_model.h"
|
2022-07-30 09:23:53 -07:00
|
|
|
#include "tunerstudio.h"
|
2020-12-01 10:03:42 -08:00
|
|
|
|
2019-01-11 16:08:15 -08:00
|
|
|
#if EFI_PROD_CODE
|
|
|
|
#include "svnversion.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ! EFI_UNIT_TEST
|
|
|
|
#include "status_loop.h"
|
|
|
|
#endif
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2019-01-11 06:58:48 -08:00
|
|
|
WarningCodeState::WarningCodeState() {
|
2019-01-12 05:34:38 -08:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WarningCodeState::clear() {
|
2019-01-11 06:58:48 -08:00
|
|
|
warningCounter = 0;
|
|
|
|
lastErrorCode = 0;
|
2019-01-12 18:36:50 -08:00
|
|
|
recentWarnings.clear();
|
2019-01-11 06:58:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WarningCodeState::addWarningCode(obd_code_e code) {
|
|
|
|
warningCounter++;
|
|
|
|
lastErrorCode = code;
|
2022-08-16 22:12:25 -07:00
|
|
|
|
|
|
|
warning_t* existing = recentWarnings.find(code);
|
|
|
|
|
|
|
|
if (!existing) {
|
2021-11-20 00:01:11 -08:00
|
|
|
chibios_rt::CriticalSectionLocker csl;
|
|
|
|
|
2022-08-16 22:12:25 -07:00
|
|
|
// Add the code to the list
|
|
|
|
existing = recentWarnings.add(warning_t(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
// Reset the timer on the code to now
|
|
|
|
existing->LastTriggered.reset();
|
2019-01-12 11:19:21 -08:00
|
|
|
}
|
2022-08-16 22:12:25 -07:00
|
|
|
|
|
|
|
// Reset the "any warning" timer too
|
|
|
|
timeSinceLastWarning.reset();
|
2019-01-11 06:58:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param forIndicator if we want to retrieving value for TS indicator, this case a minimal period is applued
|
|
|
|
*/
|
2022-08-16 22:12:25 -07:00
|
|
|
bool WarningCodeState::isWarningNow() const {
|
|
|
|
int period = maxI(3, engineConfiguration->warningPeriod);
|
|
|
|
|
|
|
|
return !timeSinceLastWarning.hasElapsedSec(period);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether a particular warning is active
|
|
|
|
bool WarningCodeState::isWarningNow(obd_code_e code) const {
|
|
|
|
warning_t* warn = recentWarnings.find(code);
|
|
|
|
|
|
|
|
// No warning found at all
|
|
|
|
if (!warn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the warning is old, it is not active
|
|
|
|
return !warn->LastTriggered.hasElapsedSec(maxI(3, engineConfiguration->warningPeriod));
|
2019-01-11 06:58:48 -08:00
|
|
|
}
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2021-03-19 14:04:42 -07:00
|
|
|
void FuelConsumptionState::consumeFuel(float grams, efitick_t nowNt) {
|
|
|
|
m_consumedGrams += grams;
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2021-03-19 14:04:42 -07:00
|
|
|
float elapsedSecond = m_timer.getElapsedSecondsAndReset(nowNt);
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2021-03-19 14:04:42 -07:00
|
|
|
// If it's been a long time since last injection, ignore this pulse
|
|
|
|
if (elapsedSecond > 0.2f) {
|
|
|
|
m_rate = 0;
|
|
|
|
} else {
|
|
|
|
m_rate = grams / elapsedSecond;
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
2021-03-19 14:04:42 -07:00
|
|
|
}
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2021-03-19 14:04:42 -07:00
|
|
|
float FuelConsumptionState::getConsumedGrams() const {
|
|
|
|
return m_consumedGrams;
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
2021-03-19 14:04:42 -07:00
|
|
|
float FuelConsumptionState::getConsumptionGramPerSecond() const {
|
|
|
|
return m_rate;
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
EngineState::EngineState() {
|
|
|
|
timeSinceLastTChargeK = getTimeNowNt();
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void EngineState::updateSlowSensors() {
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void EngineState::periodicFastCallback() {
|
2019-10-11 17:43:21 -07:00
|
|
|
ScopePerf perf(PE::EngineStatePeriodicFastCallback);
|
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2019-01-14 05:57:08 -08:00
|
|
|
if (!engine->slowCallBackWasInvoked) {
|
2019-12-02 17:16:41 -08:00
|
|
|
warning(CUSTOM_SLOW_NOT_INVOKED, "Slow not invoked yet");
|
2019-01-14 05:57:08 -08:00
|
|
|
}
|
2019-01-04 21:32:56 -08:00
|
|
|
efitick_t nowNt = getTimeNowNt();
|
2022-06-17 18:20:47 -07:00
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engine->rpmCalculator.isCranking()) {
|
2022-06-17 18:20:47 -07:00
|
|
|
crankingTimer.reset(nowNt);
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
2022-06-17 18:20:47 -07:00
|
|
|
|
|
|
|
running.timeSinceCrankingInSecs = crankingTimer.getElapsedSeconds(nowNt);
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
recalculateAuxValveTiming();
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2022-01-20 19:12:04 -08:00
|
|
|
int rpm = Sensor::getOrZero(SensorType::Rpm);
|
2022-04-13 13:39:59 -07:00
|
|
|
sparkDwell = engine->ignitionState.getSparkDwell(rpm);
|
2019-01-04 21:32:56 -08:00
|
|
|
dwellAngle = cisnan(rpm) ? NAN : sparkDwell / getOneDegreeTimeMs(rpm);
|
|
|
|
|
|
|
|
// todo: move this into slow callback, no reason for IAT corr to be here
|
2021-11-16 01:15:29 -08:00
|
|
|
running.intakeTemperatureCoefficient = getIatFuelCorrection();
|
2019-01-04 21:32:56 -08:00
|
|
|
// todo: move this into slow callback, no reason for CLT corr to be here
|
2021-11-16 01:15:29 -08:00
|
|
|
running.coolantTemperatureCoefficient = getCltFuelCorrection();
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2022-01-23 16:44:41 -08:00
|
|
|
engine->module<DfcoController>()->update();
|
2019-01-04 21:32:56 -08:00
|
|
|
|
|
|
|
// post-cranking fuel enrichment.
|
2021-05-17 14:24:29 -07:00
|
|
|
// for compatibility reasons, apply only if the factor is greater than unity (only allow adding fuel)
|
|
|
|
if (engineConfiguration->postCrankingFactor > 1.0f) {
|
2019-01-04 21:32:56 -08:00
|
|
|
// use interpolation for correction taper
|
2019-08-26 20:41:04 -07:00
|
|
|
running.postCrankingFuelCorrection = interpolateClamped(0.0f, engineConfiguration->postCrankingFactor,
|
|
|
|
engineConfiguration->postCrankingDurationSec, 1.0f, running.timeSinceCrankingInSecs);
|
2019-01-04 21:32:56 -08:00
|
|
|
} else {
|
2019-08-26 20:41:04 -07:00
|
|
|
running.postCrankingFuelCorrection = 1.0f;
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
cltTimingCorrection = getCltTimingCorrection();
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
baroCorrection = getBaroCorrection();
|
2019-01-04 21:32:56 -08:00
|
|
|
|
2020-09-06 16:06:32 -07:00
|
|
|
auto tps = Sensor::get(SensorType::Tps1);
|
2021-11-16 01:15:29 -08:00
|
|
|
updateTChargeK(rpm, tps.value_or(0));
|
2021-03-03 04:30:56 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
float injectionMass = getInjectionMass(rpm);
|
|
|
|
auto clResult = fuelClosedLoopCorrection();
|
2021-05-08 14:41:50 -07:00
|
|
|
|
2021-03-03 04:30:56 -08:00
|
|
|
// Store the pre-wall wetting injection duration for scheduling purposes only, not the actual injection duration
|
2022-09-04 21:53:05 -07:00
|
|
|
engine->engineState.injectionDuration = engine->module<InjectorModel>()->getInjectionDuration(injectionMass);
|
2020-07-20 23:11:48 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
float fuelLoad = getFuelingLoad();
|
|
|
|
injectionOffset = getInjectionOffset(rpm, fuelLoad);
|
2020-07-20 23:11:48 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
float ignitionLoad = getIgnitionLoad();
|
2022-04-16 18:03:50 -07:00
|
|
|
float advance = getAdvance(rpm, ignitionLoad) * engine->ignitionState.luaTimingMult + engine->ignitionState.luaTimingAdd;
|
2021-12-06 18:19:37 -08:00
|
|
|
|
2021-12-31 23:19:59 -08:00
|
|
|
// compute per-bank fueling
|
|
|
|
for (size_t i = 0; i < STFT_BANK_COUNT; i++) {
|
|
|
|
float corr = clResult.banks[i];
|
|
|
|
engine->stftCorrection[i] = corr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now apply that to per-cylinder fueling and timing
|
2021-12-06 18:19:37 -08:00
|
|
|
for (size_t i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
|
2021-12-31 23:19:59 -08:00
|
|
|
uint8_t bankIndex = engineConfiguration->cylinderBankSelect[i];
|
|
|
|
auto bankTrim =engine->stftCorrection[bankIndex];
|
|
|
|
auto cylinderTrim = getCylinderFuelTrim(i, rpm, fuelLoad);
|
|
|
|
|
|
|
|
// Apply both per-bank and per-cylinder trims
|
2022-09-05 01:24:17 -07:00
|
|
|
engine->engineState.injectionMass[i] = injectionMass * bankTrim * cylinderTrim;
|
2021-12-31 23:19:59 -08:00
|
|
|
|
2022-01-01 12:47:47 -08:00
|
|
|
timingAdvance[i] = advance + getCylinderIgnitionTrim(i, rpm, ignitionLoad);
|
2021-12-06 18:19:37 -08:00
|
|
|
}
|
2021-07-09 05:37:46 -07:00
|
|
|
|
|
|
|
// TODO: calculate me from a table!
|
2021-11-17 00:54:21 -08:00
|
|
|
trailingSparkAngle = engineConfiguration->trailingSparkAngle;
|
2021-07-09 05:37:46 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
multispark.count = getMultiSparkCount(rpm);
|
2020-12-01 10:03:42 -08:00
|
|
|
|
|
|
|
#if EFI_LAUNCH_CONTROL
|
2021-11-15 15:57:12 -08:00
|
|
|
engine->launchController.update();
|
2020-12-01 10:03:42 -08:00
|
|
|
#endif //EFI_LAUNCH_CONTROL
|
2020-06-17 14:15:04 -07:00
|
|
|
#endif // EFI_ENGINE_CONTROL
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void EngineState::updateTChargeK(int rpm, float tps) {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2022-11-26 09:25:04 -08:00
|
|
|
float newTCharge = engine->fuelComputer.getTCharge(rpm, tps);
|
2019-01-04 21:32:56 -08:00
|
|
|
// convert to microsecs and then to seconds
|
|
|
|
efitick_t curTime = getTimeNowNt();
|
2021-08-09 13:33:06 -07:00
|
|
|
float secsPassed = (float)NT2US(curTime - timeSinceLastTChargeK) / US_PER_SECOND_F;
|
2019-01-04 21:32:56 -08:00
|
|
|
if (!cisnan(newTCharge)) {
|
|
|
|
// control the rate of change or just fill with the initial value
|
2021-11-17 00:54:21 -08:00
|
|
|
sd.tCharge = (sd.tChargeK == 0) ? newTCharge : limitRateOfChange(newTCharge, sd.tCharge, engineConfiguration->tChargeAirIncrLimit, engineConfiguration->tChargeAirDecrLimit, secsPassed);
|
2019-06-19 19:34:11 -07:00
|
|
|
sd.tChargeK = convertCelsiusToKelvin(sd.tCharge);
|
2019-01-04 21:32:56 -08:00
|
|
|
timeSinceLastTChargeK = curTime;
|
|
|
|
}
|
2019-01-31 08:57:15 -08:00
|
|
|
#endif
|
2019-01-04 21:32:56 -08:00
|
|
|
}
|
|
|
|
|
2019-01-11 16:08:15 -08:00
|
|
|
#if EFI_SIMULATOR
|
|
|
|
#define VCS_VERSION "123"
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 13:42:50 -07:00
|
|
|
void TriggerConfiguration::update() {
|
|
|
|
VerboseTriggerSynchDetails = isVerboseTriggerSynchDetails();
|
|
|
|
TriggerType = getType();
|
|
|
|
}
|
|
|
|
|
2022-05-30 16:36:47 -07:00
|
|
|
trigger_config_s PrimaryTriggerConfiguration::getType() const {
|
|
|
|
return engineConfiguration->trigger;
|
2020-08-23 22:21:42 -07:00
|
|
|
}
|
|
|
|
|
2020-08-23 23:01:50 -07:00
|
|
|
bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() const {
|
2021-11-17 00:54:21 -08:00
|
|
|
return engineConfiguration->verboseTriggerSynchDetails;
|
2020-08-26 20:35:11 -07:00
|
|
|
}
|
|
|
|
|
2022-05-30 16:36:47 -07:00
|
|
|
trigger_config_s VvtTriggerConfiguration::getType() const {
|
|
|
|
// Convert from VVT type to trigger_config_s
|
|
|
|
return { getVvtTriggerType(engineConfiguration->vvtMode[index]), 0, 0 };
|
2020-08-26 20:35:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VvtTriggerConfiguration::isVerboseTriggerSynchDetails() const {
|
2021-11-17 00:54:21 -08:00
|
|
|
return engineConfiguration->verboseVVTDecoding;
|
2020-08-26 20:35:11 -07:00
|
|
|
}
|
2022-07-30 09:23:53 -07:00
|
|
|
|
|
|
|
bool isLockedFromUser() {
|
|
|
|
int lock = engineConfiguration->tuneHidingKey;
|
|
|
|
bool isLocked = lock > 0;
|
|
|
|
if (isLocked) {
|
|
|
|
firmwareError(OBD_PCM_Processor_Fault, "password protected");
|
|
|
|
}
|
|
|
|
return isLocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlockEcu(int password) {
|
|
|
|
if (password != engineConfiguration->tuneHidingKey) {
|
|
|
|
efiPrintf("Nope rebooting...");
|
2022-07-30 11:16:11 -07:00
|
|
|
#if EFI_PROD_CODE
|
2022-07-30 09:23:53 -07:00
|
|
|
scheduleReboot();
|
2022-07-30 11:16:11 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
2022-07-30 09:23:53 -07:00
|
|
|
} else {
|
|
|
|
efiPrintf("Unlocked! Burning...");
|
|
|
|
engineConfiguration->tuneHidingKey = 0;
|
|
|
|
requestBurn();
|
|
|
|
}
|
|
|
|
}
|