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

280 lines
8.7 KiB
C++
Raw Normal View History

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
#include "engine.h"
#include "thermistors.h"
#include "speed_density.h"
#include "allsensors.h"
#include "fuel_math.h"
#include "engine_math.h"
#include "advance_map.h"
#include "aux_valves.h"
2019-10-11 17:43:21 -07:00
#include "perf_trace.h"
#include "closed_loop_fuel.h"
#include "sensor.h"
2019-10-11 17:43:21 -07: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
EXTERN_ENGINE;
2019-01-04 21:32:56 -08:00
// this does not look exactly right
extern LoggingWithStorage engineLogger;
WarningCodeState::WarningCodeState() {
clear();
}
void WarningCodeState::clear() {
warningCounter = 0;
lastErrorCode = 0;
timeOfPreviousWarning = DEEP_IN_THE_PAST_SECONDS;
recentWarnings.clear();
}
void WarningCodeState::addWarningCode(obd_code_e code) {
warningCounter++;
lastErrorCode = code;
if (!recentWarnings.contains(code)) {
recentWarnings.add((int)code);
2019-01-12 11:19:21 -08:00
}
}
/**
* @param forIndicator if we want to retrieving value for TS indicator, this case a minimal period is applued
*/
2019-01-15 17:24:36 -08:00
bool WarningCodeState::isWarningNow(efitimesec_t now, bool forIndicator DECLARE_ENGINE_PARAMETER_SUFFIX) const {
int period = forIndicator ? maxI(3, engineConfiguration->warningPeriod) : engineConfiguration->warningPeriod;
return absI(now - timeOfPreviousWarning) < period;
}
2019-01-04 21:32:56 -08:00
MockAdcState::MockAdcState() {
memset(hasMockAdc, 0, sizeof(hasMockAdc));
}
2019-09-19 21:25:43 -07:00
#if EFI_ENABLE_MOCK_ADC
void MockAdcState::setMockVoltage(int hwChannel, float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) {
efiAssertVoid(OBD_PCM_Processor_Fault, hwChannel >= 0 && hwChannel < MOCK_ADC_SIZE, "hwChannel out of bounds");
2019-01-04 21:32:56 -08:00
scheduleMsg(&engineLogger, "fake voltage: channel %d value %.2f", hwChannel, voltage);
fakeAdcValues[hwChannel] = voltsToAdc(voltage);
hasMockAdc[hwChannel] = true;
}
#endif /* EFI_ENABLE_MOCK_ADC */
FuelConsumptionState::FuelConsumptionState() {
accumulatedSecondPrevNt = accumulatedMinutePrevNt = getTimeNowNt();
}
void FuelConsumptionState::addData(float durationMs) {
if (durationMs > 0.0f) {
perSecondAccumulator += durationMs;
perMinuteAccumulator += durationMs;
}
}
void FuelConsumptionState::update(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
efitick_t deltaNt = nowNt - accumulatedSecondPrevNt;
if (deltaNt >= NT_PER_SECOND) {
2019-01-04 21:32:56 -08:00
perSecondConsumption = getFuelRate(perSecondAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX);
perSecondAccumulator = 0;
accumulatedSecondPrevNt = nowNt;
}
deltaNt = nowNt - accumulatedMinutePrevNt;
if (deltaNt >= NT_PER_SECOND * 60) {
2019-01-04 21:32:56 -08:00
perMinuteConsumption = getFuelRate(perMinuteAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX);
perMinuteAccumulator = 0;
accumulatedMinutePrevNt = nowNt;
}
}
TransmissionState::TransmissionState() {
}
EngineState::EngineState() {
timeSinceLastTChargeK = getTimeNowNt();
}
void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
// this feeds rusEfi console Live Data
engine->engineState.isCrankingState = ENGINE(rpmCalculator).isCranking();
2019-01-04 21:32:56 -08:00
}
void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
2019-10-11 17:43:21 -07:00
ScopePerf perf(PE::EngineStatePeriodicFastCallback);
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL
if (!engine->slowCallBackWasInvoked) {
2019-12-02 17:16:41 -08:00
warning(CUSTOM_SLOW_NOT_INVOKED, "Slow not invoked yet");
}
2019-01-04 21:32:56 -08:00
efitick_t nowNt = getTimeNowNt();
if (ENGINE(rpmCalculator).isCranking()) {
2019-01-04 21:32:56 -08:00
crankingTime = nowNt;
timeSinceCranking = 0.0f;
} else {
timeSinceCranking = nowNt - crankingTime;
}
2020-10-17 15:45:08 -07:00
recalculateAuxValveTiming(PASS_ENGINE_PARAMETER_SIGNATURE);
2019-01-04 21:32:56 -08:00
int rpm = ENGINE(rpmCalculator).getRpm();
2019-01-04 21:32:56 -08:00
sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER_SUFFIX);
dwellAngle = cisnan(rpm) ? NAN : sparkDwell / getOneDegreeTimeMs(rpm);
// todo: move this into slow callback, no reason for IAT corr to be here
running.intakeTemperatureCoefficient = getIatFuelCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
2019-01-04 21:32:56 -08:00
// todo: move this into slow callback, no reason for CLT corr to be here
2019-08-26 20:41:04 -07:00
running.coolantTemperatureCoefficient = getCltFuelCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
2019-01-04 21:32:56 -08:00
running.pidCorrection = fuelClosedLoopCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
2019-01-04 21:32:56 -08:00
// update fuel consumption states
fuelConsumption.update(nowNt PASS_ENGINE_PARAMETER_SUFFIX);
// Fuel cut-off isn't just 0 or 1, it can be tapered
fuelCutoffCorrection = getFuelCutOffCorrection(nowNt, rpm PASS_ENGINE_PARAMETER_SUFFIX);
// 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
2019-08-26 20:41:04 -07:00
running.timeSinceCrankingInSecs = NT2US(timeSinceCranking) / 1000000.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
}
cltTimingCorrection = getCltTimingCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
engineNoiseHipLevel = interpolate2d("knock", rpm, engineConfiguration->knockNoiseRpmBins,
engineConfiguration->knockNoise);
2019-01-04 21:32:56 -08:00
baroCorrection = getBaroCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
auto tps = Sensor::get(SensorType::Tps1);
updateTChargeK(rpm, tps.value_or(0) PASS_ENGINE_PARAMETER_SUFFIX);
ENGINE(injectionDuration) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX);
2020-07-20 23:11:48 -07:00
float fuelLoad = getFuelingLoad(PASS_ENGINE_PARAMETER_SIGNATURE);
injectionOffset = getInjectionOffset(rpm, fuelLoad PASS_ENGINE_PARAMETER_SUFFIX);
float ignitionLoad = getIgnitionLoad(PASS_ENGINE_PARAMETER_SIGNATURE);
timingAdvance = getAdvance(rpm, ignitionLoad PASS_ENGINE_PARAMETER_SUFFIX);
multispark.count = getMultiSparkCount(rpm PASS_ENGINE_PARAMETER_SUFFIX);
#endif // EFI_ENGINE_CONTROL
2019-01-04 21:32:56 -08:00
}
void EngineState::updateTChargeK(int rpm, float tps DECLARE_ENGINE_PARAMETER_SUFFIX) {
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL
float newTCharge = getTCharge(rpm, tps PASS_ENGINE_PARAMETER_SUFFIX);
2019-01-04 21:32:56 -08:00
// convert to microsecs and then to seconds
efitick_t curTime = getTimeNowNt();
float secsPassed = (float)NT2US(curTime - timeSinceLastTChargeK) / 1000000.0f;
if (!cisnan(newTCharge)) {
// control the rate of change or just fill with the initial value
sd.tCharge = (sd.tChargeK == 0) ? newTCharge : limitRateOfChange(newTCharge, sd.tCharge, CONFIG(tChargeAirIncrLimit), CONFIG(tChargeAirDecrLimit), secsPassed);
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
}
SensorsState::SensorsState() {
}
2019-01-15 17:24:36 -08:00
int MockAdcState::getMockAdcValue(int hwChannel) const {
efiAssert(OBD_PCM_Processor_Fault, hwChannel >= 0 && hwChannel < MOCK_ADC_SIZE, "hwChannel out of bounds", -1);
2019-01-04 21:32:56 -08:00
return fakeAdcValues[hwChannel];
}
StartupFuelPumping::StartupFuelPumping() {
isTpsAbove50 = false;
pumpsCounter = 0;
}
void StartupFuelPumping::setPumpsCounter(int newValue) {
if (pumpsCounter != newValue) {
pumpsCounter = newValue;
if (pumpsCounter == PUMPS_TO_PRIME) {
scheduleMsg(&engineLogger, "let's squirt prime pulse %.2f", pumpsCounter);
pumpsCounter = 0;
} else {
scheduleMsg(&engineLogger, "setPumpsCounter %d", pumpsCounter);
}
}
}
void StartupFuelPumping::update(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
2019-01-21 18:48:58 -08:00
if (GET_RPM() == 0) {
bool isTpsAbove50 = Sensor::get(SensorType::DriverThrottleIntent).value_or(0) >= 50;
2019-01-04 21:32:56 -08:00
if (this->isTpsAbove50 != isTpsAbove50) {
setPumpsCounter(pumpsCounter + 1);
}
} else {
/**
* Engine is not stopped - not priming pumping mode
*/
setPumpsCounter(0);
isTpsAbove50 = false;
}
}
2019-01-11 16:08:15 -08:00
#if EFI_SIMULATOR
#define VCS_VERSION "123"
#endif
2019-07-13 11:08:08 -07:00
void printCurrentState(Logging *logging, int seconds, const char *engineTypeName, const char *firmwareBuildId) {
logging->appendPrintf("%s%s%d@%s%s %s %d%s", PROTOCOL_VERSION_TAG, DELIMETER,
2019-01-11 16:08:15 -08:00
getRusEfiVersion(), VCS_VERSION,
2019-07-13 11:08:08 -07:00
firmwareBuildId,
engineTypeName,
2019-01-11 16:08:15 -08:00
seconds,
DELIMETER);
}
2019-01-04 21:32:56 -08:00
void TriggerConfiguration::update() {
UseOnlyRisingEdgeForTrigger = isUseOnlyRisingEdgeForTrigger();
VerboseTriggerSynchDetails = isVerboseTriggerSynchDetails();
TriggerType = getType();
}
2020-08-23 23:01:50 -07:00
bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const {
return CONFIG(useOnlyRisingEdgeForTrigger);
2020-08-23 22:21:42 -07:00
}
2020-08-23 23:01:50 -07:00
trigger_type_e PrimaryTriggerConfiguration::getType() const {
return CONFIG(trigger.type);
2020-08-23 22:21:42 -07:00
}
2020-08-23 23:01:50 -07:00
bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() const {
return CONFIG(verboseTriggerSynchDetails);
2020-08-26 20:35:11 -07:00
}
bool VvtTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const {
return CONFIG(vvtCamSensorUseRise);
2020-08-26 20:35:11 -07:00
}
trigger_type_e VvtTriggerConfiguration::getType() const {
2020-08-26 21:06:10 -07:00
return engine->triggerCentral.vvtTriggerType;
2020-08-26 20:35:11 -07:00
}
bool VvtTriggerConfiguration::isVerboseTriggerSynchDetails() const {
return CONFIG(verboseVVTDecoding);
2020-08-26 20:35:11 -07:00
}