refactoring - extracting sensors storage class

This commit is contained in:
rusefi 2017-03-07 02:24:57 -05:00
parent 8eb162cbab
commit a9fca33dea
11 changed files with 64 additions and 46 deletions

View File

@ -707,7 +707,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_
tsOutputChannels->pedalPosition = hasPedalPositionSensor(PASS_ENGINE_PARAMETER_F) ? getPedalPosition(PASS_ENGINE_PARAMETER_F) : 0;
tsOutputChannels->knockCount = engine->knockCount;
tsOutputChannels->knockLevel = engine->knockVolts;
tsOutputChannels->fuelTankGauge = engine->engineState.fuelTankGauge;
tsOutputChannels->fuelTankGauge = engine->sensors.fuelTankGauge;
tsOutputChannels->hasFatalError = hasFirmwareError();
tsOutputChannels->totalTriggerErrorCounter = engine->triggerCentral.triggerState.totalTriggerErrorCounter;

View File

@ -79,10 +79,10 @@ static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAME
}
float iatCorrection;
if (cisnan(engine->engineState.iat)) {
if (cisnan(engine->sensors.iat)) {
iatCorrection = 0;
} else {
iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->engineState.iat);
iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, engine->sensors.iat);
}
if (engineConfiguration->debugMode == DBG_IGNITION_TIMING) {
#if !EFI_UNIT_TEST || defined(__DOXYGEN__)

View File

@ -69,13 +69,13 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) {
if (engineConfiguration->fuelLevelSensor != EFI_ADC_NONE) {
float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor);
engineState.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0,
sensors.fuelTankGauge = interpolate(boardConfiguration->fuelLevelEmptyTankVoltage, 0,
boardConfiguration->fuelLevelFullTankVoltage, 100,
fuelLevelVoltage);
}
engineState.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_F) ? getVBatt(PASS_ENGINE_PARAMETER_F) : 12;
sensors.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_F) ? getVBatt(PASS_ENGINE_PARAMETER_F) : 12;
engineState.injectorLag = getInjectorLag(engineState.vBatt PASS_ENGINE_PARAMETER);
engineState.injectorLag = getInjectorLag(sensors.vBatt PASS_ENGINE_PARAMETER);
}
void Engine::onTriggerEvent(efitick_t nowNt) {
@ -92,6 +92,15 @@ Engine::Engine(persistent_config_s *config) {
reset();
}
SensorsState::SensorsState() {
reset();
}
void SensorsState::reset() {
fuelTankGauge = vBatt = 0;
iat = clt = NAN;
}
void Engine::reset() {
withError = isEngineChartEnabled = false;
sensorChartMode = SC_OFF;
@ -110,7 +119,7 @@ void Engine::reset() {
isTestMode = false;
isSpinning = false;
adcToVoltageInputDividerCoefficient = NAN;
engineState.iat = engineState.clt = NAN;
sensors.reset();
memset(&ignitionPin, 0, sizeof(ignitionPin));
knockNow = false;
@ -143,20 +152,20 @@ EngineState::EngineState() {
cltTimingCorrection = 0;
runningFuel = baseFuel = currentVE = 0;
timeOfPreviousWarning = -10;
baseTableFuel = iat = iatFuelCorrection = 0;
baseTableFuel = iatFuelCorrection = 0;
fuelPidCorrection = 0;
vBatt = clt = cltFuelCorrection = postCrankingFuelCorrection = 0;
cltFuelCorrection = postCrankingFuelCorrection = 0;
warmupTargetAfr = airMass = 0;
baroCorrection = timingAdvance = fuelTankGauge = 0;
baroCorrection = timingAdvance = 0;
sparkDwell = mapAveragingDuration = 0;
totalLoggedBytes = injectionOffset = 0;
}
void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) {
iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F);
clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
engine->sensors.iat = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F);
engine->sensors.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
warmupTargetAfr = interpolate2d(clt, engineConfiguration->warmupTargetAfrBins,
warmupTargetAfr = interpolate2d(engine->sensors.clt, engineConfiguration->warmupTargetAfrBins,
engineConfiguration->warmupTargetAfr, WARMUP_TARGET_AFR_SIZE);
}
@ -172,17 +181,17 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) {
sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER);
dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm);
currentAfr = getAfr(PASS_ENGINE_PARAMETER_F);
engine->sensors.currentAfr = getAfr(PASS_ENGINE_PARAMETER_F);
// todo: move this into slow callback, no reason for IAT corr to be here
iatFuelCorrection = getIatFuelCorrection(iat PASS_ENGINE_PARAMETER);
iatFuelCorrection = getIatFuelCorrection(engine->sensors.iat PASS_ENGINE_PARAMETER);
// todo: move this into slow callback, no reason for CLT corr to be here
if (boardConfiguration->useWarmupPidAfr && clt < engineConfiguration->warmupAfrThreshold) {
if (boardConfiguration->useWarmupPidAfr && engine->sensors.clt < engineConfiguration->warmupAfrThreshold) {
if (rpm < 200) {
cltFuelCorrection = 1;
warmupAfrPid.reset();
} else {
cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, currentAfr, 1);
cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, engine->sensors.currentAfr, 1);
}
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) {
@ -207,8 +216,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) {
timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER);
if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) {
float coolantC = ENGINE(engineState.clt);
float intakeC = ENGINE(engineState.iat);
float coolantC = ENGINE(sensors.clt);
float intakeC = ENGINE(sensors.iat);
float tps = getTPS(PASS_ENGINE_PARAMETER_F);
tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC PASS_ENGINE_PARAMETER));
float map = getMap();

View File

@ -67,12 +67,9 @@ private:
thermistor_conf_s currentConfig;
};
class EngineState {
class SensorsState {
public:
EngineState();
void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F);
void updateSlowSensors(DECLARE_ENGINE_PARAMETER_F);
SensorsState();
/**
* Performance optimization:
* log() function needed for thermistor logic is relatively heavy, to avoid it we have these
@ -85,6 +82,21 @@ public:
float clt;
float vBatt;
float currentAfr;
/**
* that's fuel in tank - just a gauge
*/
percent_t fuelTankGauge;
void reset();
};
class EngineState {
public:
EngineState();
void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F);
void updateSlowSensors(DECLARE_ENGINE_PARAMETER_F);
efitick_t crankingTime;
efitick_t timeSinceCranking;
@ -100,10 +112,6 @@ public:
float engineNoiseHipLevel;
/**
* that's fuel in tank - just a gauge
*/
percent_t fuelTankGauge;
ThermistorMath iatCurve;
ThermistorMath cltCurve;
@ -150,8 +158,6 @@ public:
float currentVE;
float targetAFR;
float currentAfr;
int vssCounter;
int totalLoggedBytes;
@ -358,6 +364,7 @@ public:
void onTriggerEvent(efitick_t nowNt);
EngineState engineState;
SensorsState sensors;
efitick_t lastTriggerEventTimeNt;

View File

@ -179,15 +179,15 @@ void prepareFuelMap(DECLARE_ENGINE_PARAMETER_F) {
* @brief Engine warm-up fuel correction.
*/
float getCltFuelCorrection(DECLARE_ENGINE_PARAMETER_F) {
if (cisnan(engine->engineState.clt))
if (cisnan(engine->sensors.clt))
return 1; // this error should be already reported somewhere else, let's just handle it
return interpolate2d(engine->engineState.clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
return interpolate2d(engine->sensors.clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
}
angle_t getCltTimingCorrection(DECLARE_ENGINE_PARAMETER_F) {
if (cisnan(engine->engineState.clt))
if (cisnan(engine->sensors.clt))
return 0; // this error should be already reported somewhere else, let's just handle it
return interpolate2d(engine->engineState.clt, engineConfiguration->cltTimingBins, engineConfiguration->cltTimingExtra, CLT_TIMING_CURVE_SIZE);
return interpolate2d(engine->sensors.clt, engineConfiguration->cltTimingBins, engineConfiguration->cltTimingExtra, CLT_TIMING_CURVE_SIZE);
}
float getIatFuelCorrection(float iat DECLARE_ENGINE_PARAMETER_S) {

View File

@ -108,7 +108,7 @@ static void manualIdleController(int positionPercent) {
positionPercent += engineConfiguration->crankingIdleAdjustment;
}
percent_t cltCorrectedPosition = interpolate2d(engine->engineState.clt, config->cltIdleCorrBins, config->cltIdleCorr,
percent_t cltCorrectedPosition = interpolate2d(engine->sensors.clt, config->cltIdleCorrBins, config->cltIdleCorr,
CLT_CURVE_SIZE) / PERCENT_MULT * positionPercent;
// let's put the value into the right range

View File

@ -25,6 +25,7 @@
#include "engine_controller.h"
#include "mmc_card.h"
#include "idle_thread.h"
#include "fuel_math.h"
#if EFI_HD44780_LCD || defined(__DOXYGEN__)
@ -254,7 +255,7 @@ static void showLine(lcd_line_e line, int screenY) {
lcdPrintf("Throttle %s %f%%", buffer, getTPS());
return;
case LL_FUEL_CLT_CORRECTION:
//lcdPrintf("CLT corr %fv", getVBatt(PASS_ENGINE_PARAMETER_F));
lcdPrintf("CLT corr %fv", getCltFuelCorrection(PASS_ENGINE_PARAMETER_F));
return;
case LL_VBATT:

View File

@ -374,16 +374,16 @@ static void scheduleOutput2(OutputSignalPair *pair, efitimeus_t nowUs, float del
static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_F) {
#if ! EFI_UNIT_TEST
if (ENGINE(rpmCalculator.rpmValue) < CONFIG(fuelClosedLoopRpmThreshold) ||
ENGINE(engineState.clt) < CONFIG(fuelClosedLoopCltThreshold) ||
ENGINE(sensors.clt) < CONFIG(fuelClosedLoopCltThreshold) ||
getTPS(PASS_ENGINE_PARAMETER_F) > CONFIG(fuelClosedLoopTpsThreshold) ||
ENGINE(engineState.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold ||
ENGINE(engineState.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) {
ENGINE(sensors.currentAfr) < engineConfiguration->fuelClosedLoopAfrLowThreshold ||
ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) {
engine->engineState.fuelPidCorrection = 0;
fuelPid.reset();
return;
}
engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(engineState.currentAfr), 1);
engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), 1);
if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) {
tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection;
fuelPid.postState(&tsOutputChannels);

View File

@ -118,7 +118,7 @@ static void canDashboardBMW(void) {
sendMessage();
commonTxInit(CAN_BMW_E46_DME2);
setShortValue(&txmsg, (int) ((engine->engineState.clt + 48.373) / 0.75), 1);
setShortValue(&txmsg, (int) ((engine->sensors.clt + 48.373) / 0.75), 1);
sendMessage();
}
@ -163,7 +163,7 @@ static void canMazdaRX8(void) {
static void canDashboardFiat(void) {
//Fiat Dashboard
commonTxInit(CAN_FIAT_MOTOR_INFO);
setShortValue(&txmsg, (int) (engine->engineState.clt - 40), 3); //Coolant Temp
setShortValue(&txmsg, (int) (engine->sensors.clt - 40), 3); //Coolant Temp
setShortValue(&txmsg, getRpmE(engine) / 32, 6); //RPM
sendMessage();
}
@ -175,7 +175,7 @@ static void canDashboardVAG(void) {
sendMessage();
commonTxInit(CAN_VAG_CLT);
setShortValue(&txmsg, (int) ((engine->engineState.clt + 48.373) / 0.75), 1); //Coolant Temp
setShortValue(&txmsg, (int) ((engine->sensors.clt + 48.373) / 0.75), 1); //Coolant Temp
sendMessage();
}

View File

@ -57,6 +57,7 @@ void testFuelMap(void) {
printf("*** getInjectorLag\r\n");
// engine->engineState.vb
assertEqualsM("lag", 1.04, getInjectorLag(12 PASS_ENGINE_PARAMETER));
for (int i = 0; i < VBAT_INJECTOR_CURVE_SIZE; i++) {
@ -90,7 +91,7 @@ void testFuelMap(void) {
assertEquals(NAN, getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F));
float iatCorrection = getIatFuelCorrection(-KELV PASS_ENGINE_PARAMETER);
assertEqualsM("IAT", 2, iatCorrection);
engine->engineState.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
engine->sensors.clt = getCoolantTemperature(PASS_ENGINE_PARAMETER_F);
float cltCorrection = getCltFuelCorrection(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 1, cltCorrection);
float injectorLag = getInjectorLag(getVBatt(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER);

View File

@ -629,7 +629,7 @@ static void setTestBug299small(EngineTestHelper *eth) {
// this is needed to update injectorLag
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 70, engine->engineState.clt);
assertEqualsM("CLT", 70, engine->sensors.clt);
engineConfiguration->trigger.type = TT_ONE;
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F);
@ -1131,7 +1131,7 @@ void testSparkReverseOrderBug319(void) {
// this is needed to update injectorLag
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F);
assertEqualsM("CLT", 70, engine->engineState.clt);
assertEqualsM("CLT", 70, engine->sensors.clt);
engineConfiguration->trigger.type = TT_ONE;
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_F);