From 6fb745d29c3ce48b82c35f33192ffc4a679ddaf6 Mon Sep 17 00:00:00 2001 From: shadowm60 Date: Tue, 1 Dec 2020 20:03:42 +0200 Subject: [PATCH] Launch Control refactoring (#1992) * refactor - called from periodicFastCallback - removed defines * Update rusefi.input removed unused configurations. added clutch inversion option * Added new tests fixed limiting only in case we have launch condition and rpm outside of window * Update launch_control.cpp Unintialized config update, now it is working fine with VSS and with clutch as well. * small fixes updated after review findings * Fix breaking engine rev limiter applyLaunchControlLimiting should not overwrite booleans to false! * Delay timer in seconds timer uses ms, so convert seconds to ms --- firmware/controllers/algo/engine2.cpp | 7 ++ firmware/controllers/algo/launch_control.cpp | 52 ++++++------ firmware/controllers/algo/launch_control.h | 2 +- firmware/tunerstudio/rusefi.input | 5 +- unit_tests/tests/test_launch.cpp | 85 +++++++++++++++++--- 5 files changed, 109 insertions(+), 42 deletions(-) diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 901c1009a0..8597a9f5ba 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -18,6 +18,8 @@ #include "perf_trace.h" #include "closed_loop_fuel.h" #include "sensor.h" +#include "launch_control.h" + #if EFI_PROD_CODE #include "svnversion.h" @@ -174,6 +176,11 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { float ignitionLoad = getIgnitionLoad(PASS_ENGINE_PARAMETER_SIGNATURE); timingAdvance = getAdvance(rpm, ignitionLoad PASS_ENGINE_PARAMETER_SUFFIX); multispark.count = getMultiSparkCount(rpm PASS_ENGINE_PARAMETER_SUFFIX); + +#if EFI_LAUNCH_CONTROL + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); +#endif //EFI_LAUNCH_CONTROL + #endif // EFI_ENGINE_CONTROL } diff --git a/firmware/controllers/algo/launch_control.cpp b/firmware/controllers/algo/launch_control.cpp index ebdf33e812..252f1000dc 100644 --- a/firmware/controllers/algo/launch_control.cpp +++ b/firmware/controllers/algo/launch_control.cpp @@ -24,8 +24,11 @@ #include "engine_state.h" #include "advance_map.h" +static bool isInit = false; static Logging *logger; +LaunchControlBase launchInstance; + #if EFI_TUNER_STUDIO #include "tunerstudio_outputs.h" extern TunerStudioOutputChannels tsOutputChannels; @@ -33,22 +36,8 @@ extern TunerStudioOutputChannels tsOutputChannels; EXTERN_ENGINE; -#define RETART_THD_CALC CONFIG(launchRpm) +\ - (CONFIG(enableLaunchRetard) ? CONFIG(launchAdvanceRpmRange) : 0) +\ - CONFIG(hardCutRpmRange) static int retardThresholdRpm; - -class LaunchControlImpl : public LaunchControlBase, public PeriodicTimerController { - int getPeriodMs() override { - return 50; - } - - void PeriodicTask() { - update(); - } -}; - /** * We can have active condition from switch or from clutch. * In case we are dependent on VSS we just return true. @@ -90,7 +79,8 @@ bool LaunchControlBase::isInsideSwitchCondition() const { */ bool LaunchControlBase::isInsideSpeedCondition() const { int speed = getVehicleSpeed(); - return (CONFIG(launchSpeedTreshold) > speed) || !engineConfiguration->launchDisableBySpeed; + + return (CONFIG(launchSpeedTreshold) > speed) || (!(CONFIG(launchActivationMode) == ALWAYS_ACTIVE_LAUNCH)); } /** @@ -134,21 +124,30 @@ bool LaunchControlBase::isLaunchConditionMet(int rpm) const { return speedCondition && activateSwitchCondition && rpmCondition && tpsCondition; } +void updateLaunchConditions(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + launchInstance.update(); +} + void LaunchControlBase::update() { + if (!CONFIG(launchControlEnabled)) { return; } +#if ! EFI_UNIT_TEST + if(!isInit) { + return; + } +#endif + int rpm = GET_RPM(); bool combinedConditions = isLaunchConditionMet(rpm); - float timeDelay = CONFIG(launchActivateDelay); - int cutRpmRange = CONFIG(hardCutRpmRange); - int launchAdvanceRpmRange = CONFIG(launchTimingRpmRange); //recalculate in periodic task, this way we save time in applyLaunchControlLimiting //and still recalculat in case user changed the values - retardThresholdRpm = RETART_THD_CALC; + retardThresholdRpm = CONFIG(launchRpm) + (CONFIG(enableLaunchRetard) ? + CONFIG(launchAdvanceRpmRange) : 0) + CONFIG(hardCutRpmRange); if (!combinedConditions) { // conditions not met, reset timer @@ -159,7 +158,7 @@ void LaunchControlBase::update() { engine->applyLaunchExtraFuel = false; } else { // If conditions are met... - if (m_launchTimer.hasElapsedMs(timeDelay) && combinedConditions) { + if (m_launchTimer.hasElapsedMs(timeDelay*1000) && combinedConditions) { engine->isLaunchCondition = true; // ...enable launch! engine->applyLaunchExtraFuel = true; } @@ -181,8 +180,6 @@ void LaunchControlBase::update() { #endif /* EFI_TUNER_STUDIO */ } -static LaunchControlImpl Launch; - void setDefaultLaunchParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) { engineConfiguration->launchRpm = 4000; // Rpm to trigger Launch condition engineConfiguration->launchTimingRetard = 10; // retard in absolute degrees ATDC @@ -198,20 +195,21 @@ void setDefaultLaunchParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) { engineConfiguration->enableLaunchBoost = true; engineConfiguration->launchSmoothRetard = true; //interpolates the advance linear from launchrpm to fully retarded at launchtimingrpmrange engineConfiguration->antiLagRpmTreshold = 3000; + } void applyLaunchControlLimiting(bool *limitedSpark, bool *limitedFuel DECLARE_ENGINE_PARAMETER_SUFFIX) { - - if (retardThresholdRpm < GET_RPM()) { + if (( engine->isLaunchCondition ) && ( retardThresholdRpm < GET_RPM() )) { *limitedSpark = engineConfiguration->launchSparkCutEnable; *limitedFuel = engineConfiguration->launchFuelCutEnable; - } + } } void initLaunchControl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { logger = sharedLogger; - retardThresholdRpm = RETART_THD_CALC; - Launch.Start(); + INJECT_ENGINE_REFERENCE(&launchInstance); + + isInit = true; } #endif /* EFI_LAUNCH_CONTROL */ diff --git a/firmware/controllers/algo/launch_control.h b/firmware/controllers/algo/launch_control.h index f9d61baacf..6bf209655d 100644 --- a/firmware/controllers/algo/launch_control.h +++ b/firmware/controllers/algo/launch_control.h @@ -14,6 +14,7 @@ class Logging; void initLaunchControl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); void setDefaultLaunchParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE); void applyLaunchControlLimiting(bool *limitedSpark, bool *limitedFuel DECLARE_ENGINE_PARAMETER_SUFFIX); +void updateLaunchConditions(DECLARE_ENGINE_PARAMETER_SIGNATURE); class LaunchControlBase { public: @@ -26,7 +27,6 @@ public: bool isInsideTpsCondition() const; bool isInsideSwitchCondition() const; bool isInsideRPMCondition(int rpm) const; - bool isLaunchConditionMet(int rpm) const; private: diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index 88c0023bcc..077eac9ea6 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -3277,13 +3277,14 @@ cmd_set_engine_type_default = "@@TS_IO_TEST_COMMAND_char@@\x00\x31\x00\x00" field = "Activation Mode", launchActivationMode field = "Switch Input", launchActivatePin, {launchActivationMode == 0 && launchControlEnabled == 1} field = "Clutch Input", clutchDownPin, {launchActivationMode == 1 && launchControlEnabled == 1} + field = "Clutch Input inverted", clutchDownPinMode, {launchActivationMode == 1 && launchControlEnabled == 1} field = "" field = "Rpm Treshold", launchRpmTreshold, {launchControlEnabled == 1} field = "Speed Treshold", launchSpeedTreshold, {launchControlEnabled == 1} field = "" field = "Launch RPM", launchRpm, {launchControlEnabled == 1} - field = "Extra Fuel", launchFuelAdded, {launchControlEnabled == 1} - field = "Boost Solenoid Duty", launchBoostDuty, {launchControlEnabled == 1} + ;field = "Extra Fuel", launchFuelAdded, {launchControlEnabled == 1} + ;field = "Boost Solenoid Duty", launchBoostDuty, {launchControlEnabled == 1} field = "Ignition Retard enable", enableLaunchRetard, {launchControlEnabled == 1} field = "Ignition Retard", launchTimingRetard, {launchControlEnabled == 1} field = "Ignition Retard RPM Range", launchTimingRpmRange, {launchControlEnabled == 1} diff --git a/unit_tests/tests/test_launch.cpp b/unit_tests/tests/test_launch.cpp index 614c257bd1..86ff2fb34b 100644 --- a/unit_tests/tests/test_launch.cpp +++ b/unit_tests/tests/test_launch.cpp @@ -34,6 +34,7 @@ TEST(LaunchControl, VSSCondition) { INJECT_ENGINE_REFERENCE(&dut); // Test Speed trashold + engineConfiguration->launchActivationMode = ALWAYS_ACTIVE_LAUNCH; engineConfiguration->launchSpeedTreshold = 30; engineConfiguration->launchDisableBySpeed = 1; setMockVehicleSpeed(10); @@ -42,15 +43,6 @@ TEST(LaunchControl, VSSCondition) { setMockVehicleSpeed(40); EXPECT_FALSE(dut.isInsideSpeedCondition()); - //we do not want to disable launch by speed - engineConfiguration->launchSpeedTreshold = 30; - engineConfiguration->launchDisableBySpeed = 0; - - setMockVehicleSpeed(10.0); - EXPECT_TRUE(dut.isInsideSpeedCondition()); - - setMockVehicleSpeed(40.0); - EXPECT_TRUE(dut.isInsideSpeedCondition()); } TEST(LaunchControl, RPMCondition) { @@ -73,11 +65,11 @@ TEST(LaunchControl, SwitchInputCondition) { INJECT_ENGINE_REFERENCE(&dut); //activation based on VSS - engineConfiguration->launchActivationMode=ALWAYS_ACTIVE_LAUNCH; + engineConfiguration->launchActivationMode = ALWAYS_ACTIVE_LAUNCH; EXPECT_TRUE(dut.isInsideSwitchCondition()); //active by switch - engineConfiguration->launchActivationMode=SWITCH_INPUT_LAUNCH; + engineConfiguration->launchActivationMode = SWITCH_INPUT_LAUNCH; engineConfiguration->launchActivatePin = GPIOG_1; setMockState(engineConfiguration->launchActivatePin, true); EXPECT_TRUE(dut.isInsideSwitchCondition()); @@ -86,7 +78,7 @@ TEST(LaunchControl, SwitchInputCondition) { EXPECT_FALSE(dut.isInsideSwitchCondition()); //by clutch - engineConfiguration->launchActivationMode=CLUTCH_INPUT_LAUNCH; + engineConfiguration->launchActivationMode = CLUTCH_INPUT_LAUNCH; engineConfiguration->clutchDownPin = GPIOG_2; engineConfiguration->clutchDownPinMode = PI_PULLUP; setMockState(engineConfiguration->clutchDownPin, true); @@ -131,3 +123,72 @@ TEST(LaunchControl, CombinedCondition) { EXPECT_FALSE(dut.isLaunchConditionMet(3200)); } + +TEST(LaunchControl, CompleteRun) { + bool spark, fuel; + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + LoggingWithStorage logger("test"); + + initLaunchControl(&logger,PASS_ENGINE_PARAMETER_SIGNATURE); + + //load default config + setDefaultLaunchParameters(PASS_CONFIG_PARAMETER_SIGNATURE); + + //check VSS normal usage + engineConfiguration->launchActivationMode = ALWAYS_ACTIVE_LAUNCH; + engineConfiguration->launchSpeedTreshold = 30; + engineConfiguration->launchDisableBySpeed = 1; + engineConfiguration->launchRpm = 3000; + engineConfiguration->launchTpsTreshold = 10; + engineConfiguration->launchControlEnabled = 1; + //valid TPS + Sensor::setMockValue(SensorType::DriverThrottleIntent, 20.0f); + + setMockVehicleSpeed(10); + engine->rpmCalculator.mockRpm = 1200; + + //update condition check + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); + + //check if we have some sort of cut? we should not have at this point + spark = false; + fuel = false; + applyLaunchControlLimiting(&spark, &fuel PASS_ENGINE_PARAMETER_SUFFIX); + EXPECT_FALSE(spark); + EXPECT_FALSE(fuel); + + + engine->rpmCalculator.mockRpm = 3510; + //update condition check + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); + + + //we have a 3 seconds delay to actually enable it! + eth.smartMoveTimeForwardSeconds(1); + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); + spark = false; + fuel = false; + applyLaunchControlLimiting(&spark, &fuel PASS_ENGINE_PARAMETER_SUFFIX); + + EXPECT_FALSE(spark); + EXPECT_FALSE(fuel); + + eth.smartMoveTimeForwardSeconds(3); + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); + spark = false; + fuel = false; + applyLaunchControlLimiting(&spark, &fuel PASS_ENGINE_PARAMETER_SUFFIX); + + EXPECT_TRUE(spark); + EXPECT_FALSE(fuel); + + setMockVehicleSpeed(40); + updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); + spark = false; + fuel = false; + applyLaunchControlLimiting(&spark, &fuel PASS_ENGINE_PARAMETER_SUFFIX); + EXPECT_FALSE(spark); + EXPECT_FALSE(fuel); + +}