From c03851c385729807edc9e109d1c29338733fc688 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 6 Feb 2016 12:02:24 -0500 Subject: [PATCH] auto-sync --- firmware/console/status_loop.cpp | 6 ++-- firmware/controllers/algo/engine.cpp | 13 ++++++--- firmware/controllers/algo/engine.h | 28 +++++++++++-------- firmware/controllers/algo/fuel_math.cpp | 2 +- firmware/controllers/alternatorController.cpp | 7 +---- firmware/controllers/math/pid.cpp | 22 +++++++++++++++ firmware/controllers/math/pid.h | 11 ++++++++ 7 files changed, 63 insertions(+), 26 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 82ca1687c4..9b7149cfdd 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -261,7 +261,7 @@ static void printState(void) { debugFloat(&logger, "fuel_base", baseFuel, 2); // debugFloat(&logger, "fuel_iat", getIatCorrection(getIntakeAirTemperature()), 2); // debugFloat(&logger, "fuel_clt", getCltCorrection(getCoolantTemperature()), 2); - debugFloat(&logger, "fuel_lag", engine->injectorLagMs, 2); + debugFloat(&logger, "fuel_lag", engine->engineState.injectorLag, 2); debugFloat(&logger, "fuel", ENGINE(actualLastInjection), 2); debugFloat(&logger, "timing", engine->engineState.timingAdvance, 2); @@ -418,7 +418,7 @@ static void showFuelInfo2(float rpm, float engineLoad) { if (engine->rpmCalculator.isRunning()) { float iatCorrection = engine->engineState.iatFuelCorrection; float cltCorrection = engine->engineState.cltFuelCorrection; - float injectorLag = engine->injectorLagMs; + floatms_t injectorLag = engine->engineState.injectorLag; scheduleMsg(&logger2, "rpm=%f engineLoad=%f", rpm, engineLoad); scheduleMsg(&logger2, "baseFuel=%f", baseFuelMs); @@ -603,7 +603,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->injectorDutyCycle = getInjectorDutyCycle(rpm PASS_ENGINE_PARAMETER); tsOutputChannels->runningFuel = ENGINE(engineState.runningFuel); - tsOutputChannels->injectorLagMs = ENGINE(injectorLagMs); + tsOutputChannels->injectorLagMs = ENGINE(engineState.injectorLag); if (engineConfiguration->debugMode == TPS_ACCEL) { tsOutputChannels->debugIntField1 = engine->tpsAccelEnrichment.cb.getSize(); diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 7e0a1022e7..2149c741f4 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -33,7 +33,6 @@ extern engine_pins_s enginePins; extern fuel_Map3D_t veMap; extern fuel_Map3D_t afrMap; - EXTERN_ENGINE ; @@ -74,7 +73,7 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_F) { } float vBatt = hasVBatt(PASS_ENGINE_PARAMETER_F) ? getVBatt(PASS_ENGINE_PARAMETER_F) : 12; - injectorLagMs = getInjectorLag(vBatt PASS_ENGINE_PARAMETER); + engineState.injectorLag = getInjectorLag(vBatt PASS_ENGINE_PARAMETER); } void Engine::onTriggerEvent(efitick_t nowNt) { @@ -92,6 +91,7 @@ void Engine::addConfigurationListener(configuration_callback_t callback) { Engine::Engine(persistent_config_s *config) { init(config); + engineState.warmupAfrPid.init(&config->engineConfiguration.warmupAfrPid, 0.1, 10); isEngineChartEnabled = false; sensorChartMode = SC_OFF; /** @@ -119,7 +119,7 @@ Engine::Engine(persistent_config_s *config) { timeOfLastKnockEvent = 0; - injectorLagMs = fuelMs = 0; + fuelMs = 0; clutchDownState = clutchUpState = false; memset(&m, 0, sizeof(m)); @@ -129,6 +129,7 @@ Engine::Engine(persistent_config_s *config) { EngineState::EngineState() { dwellAngle = 0; engineNoiseHipLevel = 0; + injectorLag = 0; } void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { @@ -138,7 +139,11 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm); iatFuelCorrection = getIatCorrection(iat PASS_ENGINE_PARAMETER); - cltFuelCorrection = getCltCorrection(clt PASS_ENGINE_PARAMETER); + if (boardConfiguration->useWarmupPidAfr && clt < 80) { + + } else { + cltFuelCorrection = getCltCorrection(clt PASS_ENGINE_PARAMETER); + } engineNoiseHipLevel = interpolate2d(rpm, engineConfiguration->knockNoiseRpmBins, engineConfiguration->knockNoise, ENGINE_NOISE_CURVE_SIZE); diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index c64814ccee..2745e8b45d 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -7,11 +7,11 @@ #ifndef ENGINE_H_ #define ENGINE_H_ +#include "global.h" #include "main.h" +#include "pid.h" #include "engine_configuration.h" #include "rpm_calculator.h" - -#include "global.h" #include "engine_configuration.h" #include "event_registry.h" #include "trigger_structure.h" @@ -102,9 +102,12 @@ public: void periodicFastCallback(DECLARE_ENGINE_PARAMETER_F); /** - * WIP: accessing these values here would be a performance optimization since log() function needed for - * thermistor logic is relatively heavy + * Performance optimization: + * log() function needed for thermistor logic is relatively heavy, to avoid it we have these + * pre-calculated values * Access to these two fields is not synchronized in any way - that should work since float read/write are atomic. + * + * values are in Celsius */ float iat; float clt; @@ -139,7 +142,15 @@ public: // fuel-related; float iatFuelCorrection; float cltFuelCorrection; - float injectorLag; + /** + * Global injector lag + injectorLag(VBatt) + * + * this value depends on a slow-changing VBatt value, so + * we update it once in a while + */ + floatms_t injectorLag; + + Pid warmupAfrPid; float baroCorrection; @@ -329,13 +340,6 @@ public: EngineState engineState; efitick_t lastTriggerEventTimeNt; - /** - * Global injector lag + injectorLag(VBatt) - * - * this value depends on a slow-changing VBatt value, so - * we update it once in a while - */ - float injectorLagMs; /** * This coefficient translates ADC value directly into voltage adjusted according to diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index ba3701af51..f1cf9f21da 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -128,7 +128,7 @@ floatms_t getFuelMs(int rpm DECLARE_ENGINE_PARAMETER_S) { theoreticalInjectionLength = fuelPerCycle / getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER); } - return theoreticalInjectionLength + ENGINE(injectorLagMs); + return theoreticalInjectionLength + ENGINE(engineState.injectorLag); } floatms_t getRunningFuel(floatms_t baseFuel, int rpm DECLARE_ENGINE_PARAMETER_S) { diff --git a/firmware/controllers/alternatorController.cpp b/firmware/controllers/alternatorController.cpp index c6851f81ab..56174757dc 100644 --- a/firmware/controllers/alternatorController.cpp +++ b/firmware/controllers/alternatorController.cpp @@ -72,12 +72,7 @@ static msg_t AltCtrlThread(int param) { #if ! EFI_UNIT_TEST || defined(__DOXYGEN__) if (engineConfiguration->debugMode == ALTERNATOR) { tsOutputChannels.debugFloatField1 = currentAltDuty; - tsOutputChannels.debugFloatField2 = altPid.getIntegration(); - tsOutputChannels.debugFloatField3 = altPid.getPrevError(); - tsOutputChannels.debugFloatField4 = altPid.getI(); - tsOutputChannels.debugFloatField5 = altPid.getD(); - tsOutputChannels.debugIntField1 = altPid.getP(); - tsOutputChannels.debugIntField2 = engineConfiguration->alternatorControl.offset; + altPid.postState(&tsOutputChannels); } #endif diff --git a/firmware/controllers/math/pid.cpp b/firmware/controllers/math/pid.cpp index 00b2c0e0cc..706cbb59a3 100644 --- a/firmware/controllers/math/pid.cpp +++ b/firmware/controllers/math/pid.cpp @@ -8,8 +8,17 @@ */ #include "pid.h" +#include "math.h" + +Pid::Pid() { + init(NULL, NAN, NAN); +} Pid::Pid(pid_s *pid, float minResult, float maxResult) { + init(pid, minResult, maxResult); +} + +void Pid::init(pid_s *pid, float minResult, float maxResult) { this->pid = pid; this->minResult = minResult; this->maxResult = maxResult; @@ -70,4 +79,17 @@ float Pid::getD(void) { return pid->dFactor; } +float Pid::getOffset(void) { + return pid->offset; +} +#if EFI_PROD_CODE || EFI_SIMULATOR +void Pid::postState(TunerStudioOutputChannels *tsOutputChannels) { + tsOutputChannels->debugFloatField2 = getIntegration(); + tsOutputChannels->debugFloatField3 = getPrevError(); + tsOutputChannels->debugFloatField4 = getI(); + tsOutputChannels->debugFloatField5 = getD(); + tsOutputChannels->debugIntField1 = getP(); + tsOutputChannels->debugIntField2 = getOffset(); +} +#endif diff --git a/firmware/controllers/math/pid.h b/firmware/controllers/math/pid.h index 6e8a50e3ac..3df71065e9 100644 --- a/firmware/controllers/math/pid.h +++ b/firmware/controllers/math/pid.h @@ -8,20 +8,31 @@ #ifndef PID_H_ #define PID_H_ +#include "global.h" #include "engine_configuration_generated_structures.h" +#if EFI_PROD_CODE || EFI_SIMULATOR +#include "tunerstudio_configuration.h" +#endif class Pid { public: + Pid(); Pid(pid_s *pid, float minResult, float maxResult); + void init(pid_s *pid, float minResult, float maxResult); + float getValue(float target, float input, float dTime); void updateFactors(float pFactor, float iFactor, float dFactor); void reset(void); float getP(void); float getI(void); float getD(void); + float getOffset(void); float getIntegration(void); float getPrevError(void); +#if EFI_PROD_CODE || EFI_SIMULATOR + void postState(TunerStudioOutputChannels *tsOutputChannels); +#endif private: pid_s *pid; float minResult;