From 6361b9ef319c87b4a720a22ca88a95b431148c8c Mon Sep 17 00:00:00 2001 From: kifir23917 <159958110+kifir23917@users.noreply.github.com> Date: Tue, 5 Nov 2024 00:09:39 +0200 Subject: [PATCH] Flat shift tests #5608 (#7042) * only:refactoring: extract part of functionality into `TestEngineConfiguration` class * only:refactoring: rename file * only:refactoring: Now we reuse `TestBase` class functionality * only:refactoring: extract part of functionality into `TestEngineState` class * only:refactoring: extract part of functionality into `TestEngineConfiguration` class * implement test for `Torque Reduction Ignition Cut` setting #5608 --- .../algo/engine_configuration_defaults.h | 1 + unit_tests/tests/launch/launch_test_base.cpp | 102 +------- unit_tests/tests/launch/launch_test_base.h | 14 -- .../launch/test_launch_target_skip_ratio.cpp | 45 ++++ .../tests/launch/test_target_skip_ratio.cpp | 41 ---- .../flat_shift_condition_test_base.cpp | 40 ++++ .../flat_shift_condition_test_base.h | 22 ++ .../shift_torque_reduction_test_base.cpp | 137 ++--------- .../shift_torque_reduction_test_base.h | 14 +- ...hift_torque_reduction_spark_skip_ratio.cpp | 59 +++++ unit_tests/tests/tests.mk | 6 +- unit_tests/tests/util/test_base.cpp | 8 + unit_tests/tests/util/test_base.h | 5 + .../tests/util/test_engine_configuration.cpp | 223 ++++++++++++++++++ .../tests/util/test_engine_configuration.h | 43 ++++ unit_tests/tests/util/test_engine_state.cpp | 24 ++ unit_tests/tests/util/test_engine_state.h | 16 ++ 17 files changed, 520 insertions(+), 280 deletions(-) create mode 100644 unit_tests/tests/launch/test_launch_target_skip_ratio.cpp delete mode 100644 unit_tests/tests/launch/test_target_skip_ratio.cpp create mode 100644 unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.cpp create mode 100644 unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.h create mode 100644 unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_spark_skip_ratio.cpp create mode 100644 unit_tests/tests/util/test_engine_configuration.cpp create mode 100644 unit_tests/tests/util/test_engine_configuration.h create mode 100644 unit_tests/tests/util/test_engine_state.cpp create mode 100644 unit_tests/tests/util/test_engine_state.h diff --git a/firmware/controllers/algo/engine_configuration_defaults.h b/firmware/controllers/algo/engine_configuration_defaults.h index 0a4cc8d863..b9a4c5c28e 100644 --- a/firmware/controllers/algo/engine_configuration_defaults.h +++ b/firmware/controllers/algo/engine_configuration_defaults.h @@ -20,6 +20,7 @@ namespace engine_configuration_defaults { constexpr bool LIMIT_TORQUE_REDUCTION_TIME = false; constexpr float TORQUE_REDUCTION_ARMING_RPM = 0.0f; constexpr float TORQUE_REDUCTION_ARMING_APP = 0.0f; + constexpr int8_t TORQUE_REDUCTION_IGNITION_CUT = 0; /* Launch Control: */ constexpr switch_input_pin_e LAUNCH_ACTIVATE_PIN = Gpio::Unassigned; diff --git a/unit_tests/tests/launch/launch_test_base.cpp b/unit_tests/tests/launch/launch_test_base.cpp index 4da042cb02..7b13b11e05 100644 --- a/unit_tests/tests/launch/launch_test_base.cpp +++ b/unit_tests/tests/launch/launch_test_base.cpp @@ -7,107 +7,25 @@ #include "launch_test_base.h" void LaunchTestBase::setUpTestConfig(const LaunchTestConfig& config) { - configureLaunchControlEnabled(config.getLaunchControlEnabled()); + getTestEngineConfiguration().configureLaunchControlEnabled(config.getLaunchControlEnabled()); - configureLaunchRpm(config.getLaunchRpm()); - configureLaunchRpmWindow(config.getLaunchRpmWindow()); - configureLaunchCorrectionsEndRpm(config.getLaunchCorrectionsEndRpm()); + getTestEngineConfiguration().configureLaunchRpm(config.getLaunchRpm()); + getTestEngineConfiguration().configureLaunchRpmWindow(config.getLaunchRpmWindow()); + getTestEngineConfiguration().configureLaunchCorrectionsEndRpm(config.getLaunchCorrectionsEndRpm()); - configureIgnitionRetardEnable(config.getIgnitionRetardEnable()); - configureIgnitionRetard(config.getIgnitionRetard()); - configureSmoothRetardMode(config.getSmoothRetardMode()); + getTestEngineConfiguration().configureIgnitionRetardEnable(config.getIgnitionRetardEnable()); + getTestEngineConfiguration().configureIgnitionRetard(config.getIgnitionRetard()); + getTestEngineConfiguration().configureSmoothRetardMode(config.getSmoothRetardMode()); - configureEnableIgnitionCut(config.getEnableIgnitionCut()); - configureInitialIgnitionCutPercent(config.getInitialIgnitionCut()); - configureFinalIgnitionCutPercentBeforeLaunch(config.getFinalIgnitionCutBeforeLaunch()); + getTestEngineConfiguration().configureEnableIgnitionCut(config.getEnableIgnitionCut()); + getTestEngineConfiguration().configureInitialIgnitionCutPercent(config.getInitialIgnitionCut()); + getTestEngineConfiguration().configureFinalIgnitionCutPercentBeforeLaunch(config.getFinalIgnitionCutBeforeLaunch()); if (config.getSatisfyActivationSwithSpeedAndTpsConditions()) { configureSatisfiedActivationSwithSpeedAndTpsConditions(); } } -void LaunchTestBase::configureLaunchControlEnabled(const std::optional launchControlEnabled) { - if (launchControlEnabled.has_value()) { - engineConfiguration->launchControlEnabled = launchControlEnabled.value(); - } else { - ASSERT_FALSE(engineConfiguration->launchControlEnabled); // check default value - } -} - -void LaunchTestBase::configureLaunchRpm(const std::optional launchRpm) { - if (launchRpm.has_value()) { - engineConfiguration->launchRpm = launchRpm.value(); - } else { - ASSERT_EQ(engineConfiguration->launchRpm, 0); // check default value - } -} - -void LaunchTestBase::configureLaunchRpmWindow(const std::optional launchRpmWindow) { - if (launchRpmWindow.has_value()) { - engineConfiguration->launchRpmWindow = launchRpmWindow.value(); - } else { - ASSERT_EQ(engineConfiguration->launchRpmWindow, 0); // check default value - } -} - -void LaunchTestBase::configureLaunchCorrectionsEndRpm(const std::optional launchCorrectionsEndRpm) { - if (launchCorrectionsEndRpm.has_value()) { - engineConfiguration->launchCorrectionsEndRpm = launchCorrectionsEndRpm.value(); - } else { - ASSERT_EQ(engineConfiguration->launchCorrectionsEndRpm, 0); // check default value - } -} - -void LaunchTestBase::configureIgnitionRetardEnable(std::optional ignitionRetardEnable) { - if (ignitionRetardEnable.has_value()) { - engineConfiguration->enableLaunchRetard = ignitionRetardEnable.value(); - } else { - ASSERT_FALSE(engineConfiguration->enableLaunchRetard); // check default value - } -} - -void LaunchTestBase::configureIgnitionRetard(std::optional ignitionRetard) { - if (ignitionRetard.has_value()) { - engineConfiguration->launchTimingRetard = ignitionRetard.value(); - } else { - ASSERT_EQ(engineConfiguration->launchTimingRetard, 0); // check default value - } -} - -void LaunchTestBase::configureSmoothRetardMode(std::optional smoothRetardMode) { - if (smoothRetardMode.has_value()) { - engineConfiguration->launchSmoothRetard = smoothRetardMode.value(); - } else { - ASSERT_FALSE(engineConfiguration->launchSmoothRetard); // check default value - } -} - -void LaunchTestBase::configureEnableIgnitionCut(const std::optional enableIgnitionCut) { - if (enableIgnitionCut.has_value()) { - engineConfiguration->launchSparkCutEnable = enableIgnitionCut.value(); - } else { - ASSERT_FALSE(engineConfiguration->launchSparkCutEnable); // check default value - } -} - -void LaunchTestBase::configureInitialIgnitionCutPercent(const std::optional initialIgnitionCutPercent) { - if (initialIgnitionCutPercent.has_value()) { - engineConfiguration->initialIgnitionCutPercent = initialIgnitionCutPercent.value(); - } else { - ASSERT_EQ(engineConfiguration->initialIgnitionCutPercent, 0); // check default value - } -} - -void LaunchTestBase::configureFinalIgnitionCutPercentBeforeLaunch( - const std::optional finalIgnitionCutPercentBeforeLaunch -) { - if (finalIgnitionCutPercentBeforeLaunch.has_value()) { - engineConfiguration->finalIgnitionCutPercentBeforeLaunch = finalIgnitionCutPercentBeforeLaunch.value(); - } else { - ASSERT_EQ(engineConfiguration->finalIgnitionCutPercentBeforeLaunch, 0); // check default value - } -} - void LaunchTestBase::configureSatisfiedActivationSwithSpeedAndTpsConditions() { engineConfiguration->launchActivationMode = ALWAYS_ACTIVE_LAUNCH; // to satisfy activateSwitchCondition engineConfiguration->launchSpeedThreshold = 0; // to satisfy speedCondition diff --git a/unit_tests/tests/launch/launch_test_base.h b/unit_tests/tests/launch/launch_test_base.h index 3524ba3d46..3adad8ec0d 100644 --- a/unit_tests/tests/launch/launch_test_base.h +++ b/unit_tests/tests/launch/launch_test_base.h @@ -66,19 +66,5 @@ protected: void setUpTestConfig(const LaunchTestConfig& config); private: - void configureLaunchControlEnabled(std::optional launchControlEnabled); - - void configureLaunchRpm(std::optional launchRpm); - void configureLaunchRpmWindow(std::optional launchRpmWindow); - void configureLaunchCorrectionsEndRpm(std::optional launchCorrectionsEndRpm); - - void configureIgnitionRetardEnable(std::optional ignitionRetardEnable); - void configureIgnitionRetard(std::optional ignitionRetard); - void configureSmoothRetardMode(std::optional smoothRetardMode); - - void configureEnableIgnitionCut(std::optional enableIgnitionCut); - void configureInitialIgnitionCutPercent(std::optional initialIgnitionCut); - void configureFinalIgnitionCutPercentBeforeLaunch(std::optional finalIgnitionCutBeforeLaunch); - void configureSatisfiedActivationSwithSpeedAndTpsConditions(); }; \ No newline at end of file diff --git a/unit_tests/tests/launch/test_launch_target_skip_ratio.cpp b/unit_tests/tests/launch/test_launch_target_skip_ratio.cpp new file mode 100644 index 0000000000..888246980b --- /dev/null +++ b/unit_tests/tests/launch/test_launch_target_skip_ratio.cpp @@ -0,0 +1,45 @@ +// +// Created by kifir on 5/17/24. +// + +#include "pch.h" + +#include "util/test_base.h" + +namespace { + constexpr float TEST_TRACTION_CONTROL_IGNITION_SKIP = 17.0f; + constexpr float TEST_LUA_SOFT_SPARK_SKIP = 239.0f; + constexpr float TEST_LUA_HARD_SPARK_SKIP = 174.0f; + + class LaunchTargetSkipRatioTest : public TestBase { + protected: + void SetUp() override; + }; + + void LaunchTargetSkipRatioTest::SetUp() { + TestBase::SetUp(); + std::fill_n( + &engineConfiguration->tractionControlIgnitionSkip[0][0], + TRACTION_CONTROL_ETB_DROP_SIZE * TRACTION_CONTROL_ETB_DROP_SIZE, + static_cast(TEST_TRACTION_CONTROL_IGNITION_SKIP) + ); + getTestEngineState().setLuaSoftSparkSkip(TEST_LUA_SOFT_SPARK_SKIP); + getTestEngineState().setLuaHardSparkSkip(TEST_LUA_HARD_SPARK_SKIP); + } + + TEST_F(LaunchTargetSkipRatioTest, doNotUseSkipInTraction) { + EXPECT_FALSE(engineConfiguration->useHardSkipInTraction); + + periodicFastCallback(); + EXPECT_EQ(engine->softSparkLimiter.getTargetSkipRatio(), TEST_LUA_SOFT_SPARK_SKIP + TEST_TRACTION_CONTROL_IGNITION_SKIP); + EXPECT_EQ(engine->hardSparkLimiter.getTargetSkipRatio(), TEST_LUA_HARD_SPARK_SKIP); + } + + TEST_F(LaunchTargetSkipRatioTest, useSkipInTraction) { + engineConfiguration->useHardSkipInTraction = true; + + periodicFastCallback(); + EXPECT_EQ(engine->softSparkLimiter.getTargetSkipRatio(), TEST_LUA_SOFT_SPARK_SKIP); + EXPECT_EQ(engine->hardSparkLimiter.getTargetSkipRatio(), TEST_LUA_HARD_SPARK_SKIP + TEST_TRACTION_CONTROL_IGNITION_SKIP); + } +} \ No newline at end of file diff --git a/unit_tests/tests/launch/test_target_skip_ratio.cpp b/unit_tests/tests/launch/test_target_skip_ratio.cpp deleted file mode 100644 index 76e6a1f5e0..0000000000 --- a/unit_tests/tests/launch/test_target_skip_ratio.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by kifir on 5/17/24. -// - -#include "pch.h" - -constexpr float TEST_TRACTION_CONTROL_IGNITION_SKIP = 17.0f; -constexpr float TEST_LUA_SOFT_SPARK_SKIP = 239.0f; -constexpr float TEST_LUA_HARD_SPARK_SKIP = 174.0f; - -static void setUpTestParameters() { - std::fill_n( - &engineConfiguration->tractionControlIgnitionSkip[0][0], - TRACTION_CONTROL_ETB_DROP_SIZE * TRACTION_CONTROL_ETB_DROP_SIZE, - static_cast(TEST_TRACTION_CONTROL_IGNITION_SKIP) - ); - engine->engineState.luaSoftSparkSkip = TEST_LUA_SOFT_SPARK_SKIP; - engine->engineState.luaHardSparkSkip = TEST_LUA_HARD_SPARK_SKIP; -} - -TEST(targetSkipRatio, doNotUseSkipInTraction) { - EngineTestHelper eth(engine_type_e::TEST_ENGINE); - - setUpTestParameters(); - EXPECT_FALSE(engineConfiguration->useHardSkipInTraction); - - eth.engine.periodicFastCallback(); - EXPECT_EQ(engine->softSparkLimiter.getTargetSkipRatio(), TEST_LUA_SOFT_SPARK_SKIP + TEST_TRACTION_CONTROL_IGNITION_SKIP); - EXPECT_EQ(engine->hardSparkLimiter.getTargetSkipRatio(), TEST_LUA_HARD_SPARK_SKIP); -} - -TEST(targetSkipRatio, useSkipInTraction) { - EngineTestHelper eth(engine_type_e::TEST_ENGINE); - - setUpTestParameters(); - engineConfiguration->useHardSkipInTraction = true; - - eth.engine.periodicFastCallback(); - EXPECT_EQ(engine->softSparkLimiter.getTargetSkipRatio(), TEST_LUA_SOFT_SPARK_SKIP); - EXPECT_EQ(engine->hardSparkLimiter.getTargetSkipRatio(), TEST_LUA_HARD_SPARK_SKIP + TEST_TRACTION_CONTROL_IGNITION_SKIP); -} diff --git a/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.cpp b/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.cpp new file mode 100644 index 0000000000..585701e0ce --- /dev/null +++ b/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.cpp @@ -0,0 +1,40 @@ +// +// Created by kifir on 11/4/24. +// + +#include "pch.h" + +#include "flat_shift_condition_test_base.h" + +FlatShiftConditionTestBase::FlatShiftConditionTestBase(const int8_t torqueReductionIgnitionCut) +: m_torqueReductionIgnitionCut(torqueReductionIgnitionCut) { +} + +void FlatShiftConditionTestBase::SetUp() { + ShiftTorqueReductionTestBase::SetUp(); + + setUpTestConfig(ShiftTorqueReductionTestConfig() + .setTorqueReductionEnabled(true) + .setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON) + .setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN) + .setLimitTorqueReductionTime(false) + .setTorqueReductionIgnitionCut(m_torqueReductionIgnitionCut) + ); +} + +void FlatShiftConditionTestBase::satisfyFlatShiftCondition() { + setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true); + updateApp(TEST_TORQUE_REDUCTION_ARMING_APP); + + periodicFastCallback(); + + EXPECT_TRUE(engine->shiftTorqueReductionController.isFlatShiftConditionSatisfied); +} + +void FlatShiftConditionTestBase::unsatisfyFlatShiftCondition() { + setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, false); + + periodicFastCallback(); + + EXPECT_FALSE(engine->shiftTorqueReductionController.isFlatShiftConditionSatisfied); +} diff --git a/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.h b/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.h new file mode 100644 index 0000000000..9fed7ebfd8 --- /dev/null +++ b/unit_tests/tests/shift_torque_reduction/flat_shift_condition_test_base.h @@ -0,0 +1,22 @@ +// +// Created by kifir on 11/4/24. +// + +#pragma once + +#include "shift_torque_reduction_test_base.h" + +class FlatShiftConditionTestBase : public ShiftTorqueReductionTestBase { +protected: + static constexpr switch_input_pin_e TEST_TORQUE_REDUCTION_BUTTON_PIN = Gpio::G10; + static constexpr float TEST_TORQUE_REDUCTION_ARMING_APP = 7.89; + + FlatShiftConditionTestBase(int8_t torqueReductionIgnitionCut); + + void SetUp() override; + + void satisfyFlatShiftCondition(); + void unsatisfyFlatShiftCondition(); +private: + const int8_t m_torqueReductionIgnitionCut; +}; \ No newline at end of file diff --git a/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp b/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp index e0523158ba..7c500966a8 100644 --- a/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp +++ b/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp @@ -5,7 +5,6 @@ #include "pch.h" #include "shift_torque_reduction_test_base.h" -#include "engine_configuration_defaults.h" ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionEnabled( const std::optional value @@ -69,127 +68,23 @@ ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductio return *this; } -void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTestConfig& config) { - configureTorqueReductionEnabled(config.getTorqueReductionEnabled()); - configureTorqueReductionActivationMode(config.getTorqueReductionActivationMode()); - configureTorqueReductionButton(config.getTriggerPin()); - configureTorqueReductionButtonInverted(config.getPinInverted()); - configureLaunchActivatePin(config.getLaunchActivatePin()); - configureLaunchActivateInverted(config.getLaunchActivateInverted()); - configureLimitTorqueReductionTime(config.getLimitTorqueReductionTime()); - configureTorqueReductionTime(config.getTorqueReductionTime()); - configureTorqueReductionArmingRpm(config.getTorqueReductionArmingRpm()); - configureTorqueReductionArmingApp(config.getTorqueReductionArmingApp()); -} - -void ShiftTorqueReductionTestBase::configureTorqueReductionEnabled(const std::optional torqueReductionEnabled) { - if (torqueReductionEnabled.has_value()) { - engineConfiguration->torqueReductionEnabled = torqueReductionEnabled.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionEnabled, - engine_configuration_defaults::ENABLE_SHIFT_TORQUE_REDUCTION - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureTorqueReductionActivationMode( - const std::optional activationMode +ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionIgnitionCut( + std::optional value ) { - if (activationMode.has_value()) { - engineConfiguration->torqueReductionActivationMode = activationMode.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionActivationMode, - engine_configuration_defaults::TORQUE_REDUCTION_ACTIVATION_MODE - ); // check default value - } + m_torqueReductionIgnitionCut = value; + return *this; } -void ShiftTorqueReductionTestBase::configureTorqueReductionButton(const std::optional pin) { - if (pin.has_value()) { - engineConfiguration->torqueReductionTriggerPin = pin.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionTriggerPin, - engine_configuration_defaults::TORQUE_REDUCTION_TRIGGER_PIN - ); // check default value - } +void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTestConfig& config) { + getTestEngineConfiguration().configureTorqueReductionEnabled(config.getTorqueReductionEnabled()); + getTestEngineConfiguration().configureTorqueReductionActivationMode(config.getTorqueReductionActivationMode()); + getTestEngineConfiguration().configureTorqueReductionButton(config.getTriggerPin()); + getTestEngineConfiguration().configureTorqueReductionButtonInverted(config.getPinInverted()); + getTestEngineConfiguration().configureLaunchActivatePin(config.getLaunchActivatePin()); + getTestEngineConfiguration().configureLaunchActivateInverted(config.getLaunchActivateInverted()); + getTestEngineConfiguration().configureLimitTorqueReductionTime(config.getLimitTorqueReductionTime()); + getTestEngineConfiguration().configureTorqueReductionTime(config.getTorqueReductionTime()); + getTestEngineConfiguration().configureTorqueReductionArmingRpm(config.getTorqueReductionArmingRpm()); + getTestEngineConfiguration().configureTorqueReductionArmingApp(config.getTorqueReductionArmingApp()); + getTestEngineConfiguration().configureTorqueReductionIgnitionCut(config.getTorqueReductionIgnitionCut()); } - -void ShiftTorqueReductionTestBase::configureTorqueReductionButtonInverted(const std::optional pinInverted) { - if (pinInverted.has_value()) { - engineConfiguration->torqueReductionTriggerPinInverted = pinInverted.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionTriggerPinInverted, - engine_configuration_defaults::TORQUE_REDUCTION_TRIGGER_PIN_INVERTED - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureLaunchActivatePin(const std::optional pin) { - if (pin.has_value()) { - engineConfiguration->launchActivatePin = pin.value(); - } else { - ASSERT_EQ( - engineConfiguration->launchActivatePin, - engine_configuration_defaults::LAUNCH_ACTIVATE_PIN - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureLaunchActivateInverted(const std::optional pinInverted) { - if (pinInverted.has_value()) { - engineConfiguration->launchActivateInverted = pinInverted.value(); - } else { - ASSERT_EQ( - engineConfiguration->launchActivateInverted, - engine_configuration_defaults::LAUNCH_ACTIVATE_PIN_INVERTED - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureLimitTorqueReductionTime(std::optional limitTorqueReductionTime) { - if (limitTorqueReductionTime.has_value()) { - engineConfiguration->limitTorqueReductionTime = limitTorqueReductionTime.value(); - } else { - ASSERT_EQ( - engineConfiguration->limitTorqueReductionTime, - engine_configuration_defaults::LIMIT_TORQUE_REDUCTION_TIME - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureTorqueReductionTime(std::optional timeout) { - if (timeout.has_value()) { - engineConfiguration->torqueReductionTime = timeout.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionTime, - engine_configuration_defaults::TORQUE_REDUCTION_TIME - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureTorqueReductionArmingRpm(const std::optional armingRpm) { - if (armingRpm.has_value()) { - engineConfiguration->torqueReductionArmingRpm = armingRpm.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionArmingRpm, - engine_configuration_defaults::TORQUE_REDUCTION_ARMING_RPM - ); // check default value - } -} - -void ShiftTorqueReductionTestBase::configureTorqueReductionArmingApp(const std::optional armingApp) { - if (armingApp.has_value()) { - engineConfiguration->torqueReductionArmingApp = armingApp.value(); - } else { - ASSERT_EQ( - engineConfiguration->torqueReductionArmingApp, - engine_configuration_defaults::TORQUE_REDUCTION_ARMING_APP - ); // check default value - } -} \ No newline at end of file diff --git a/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.h b/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.h index 3b26051047..9af8d86cb7 100644 --- a/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.h +++ b/unit_tests/tests/shift_torque_reduction/shift_torque_reduction_test_base.h @@ -20,6 +20,7 @@ public: std::optional getTorqueReductionTime() const { return m_torqueReductionTime; } std::optional getTorqueReductionArmingRpm() const { return m_torqueReductionArmingRpm; } std::optional getTorqueReductionArmingApp() const { return m_torqueReductionArmingApp; } + std::optional getTorqueReductionIgnitionCut() const { return m_torqueReductionIgnitionCut; } // We do not core about performance in tests, but we want to use builder-like style, so setters return new instance // of configuration: @@ -35,6 +36,7 @@ public: ShiftTorqueReductionTestConfig setTorqueReductionTime(std::optional value); ShiftTorqueReductionTestConfig setTorqueReductionArmingRpm(std::optional value); ShiftTorqueReductionTestConfig setTorqueReductionArmingApp(std::optional value); + ShiftTorqueReductionTestConfig setTorqueReductionIgnitionCut(std::optional value); private: std::optional m_isTorqueReductionEnabled; std::optional m_torqueReductionActivationMode; @@ -46,20 +48,10 @@ private: std::optional m_torqueReductionTime; std::optional m_torqueReductionArmingRpm; std::optional m_torqueReductionArmingApp; + std::optional m_torqueReductionIgnitionCut; }; class ShiftTorqueReductionTestBase : public TestBase { protected: void setUpTestConfig(const ShiftTorqueReductionTestConfig& config); -private: - void configureTorqueReductionEnabled(std::optional isTorqueReductionEnabled); - void configureTorqueReductionActivationMode(std::optional activationMode); - void configureTorqueReductionButton(std::optional pin); - void configureTorqueReductionButtonInverted(std::optional pinInverted); - void configureLaunchActivatePin(std::optional pin); - void configureLaunchActivateInverted(std::optional pinInverted); - void configureLimitTorqueReductionTime(std::optional limitTorqueReductionTime); - void configureTorqueReductionTime(std::optional timeout); - void configureTorqueReductionArmingRpm(std::optional armingRpm); - void configureTorqueReductionArmingApp(std::optional armingApp); }; diff --git a/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_spark_skip_ratio.cpp b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_spark_skip_ratio.cpp new file mode 100644 index 0000000000..f92f04fffb --- /dev/null +++ b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_spark_skip_ratio.cpp @@ -0,0 +1,59 @@ +// +// Created by kifir on 11/4/24. +// + +#include "pch.h" + +#include "flat_shift_condition_test_base.h" + +namespace { + constexpr float TEST_LUA_SOFT_SPARK_SKIP = 1.23f; + constexpr float TEST_LUA_HARD_SPARK_SKIP = 4.56f; + + constexpr int8_t TEST_TORQUE_REDUCTION_IGNITION_CUT = 17; + + class ShiftTorqueReductionSparkSkipRatioTest : public FlatShiftConditionTestBase { + protected: + ShiftTorqueReductionSparkSkipRatioTest(); + + void SetUp() override; + + void checkTargetSkipRatios( + const char* context, + float expectedHardSparkLimiterTargetSkipRatio = TEST_LUA_HARD_SPARK_SKIP + ); + }; + + ShiftTorqueReductionSparkSkipRatioTest::ShiftTorqueReductionSparkSkipRatioTest() + : FlatShiftConditionTestBase(TEST_TORQUE_REDUCTION_IGNITION_CUT) { + } + + void ShiftTorqueReductionSparkSkipRatioTest::SetUp() { + FlatShiftConditionTestBase::SetUp(); + + getTestEngineState().setLuaSoftSparkSkip(TEST_LUA_SOFT_SPARK_SKIP); + getTestEngineState().setLuaHardSparkSkip(TEST_LUA_HARD_SPARK_SKIP); + } + + void ShiftTorqueReductionSparkSkipRatioTest::checkTargetSkipRatios( + const char* const context, + const float expectedHardSparkLimiterTargetSkipRatio + ) { + periodicFastCallback(); + EXPECT_EQ(engine->softSparkLimiter.getTargetSkipRatio(), TEST_LUA_SOFT_SPARK_SKIP) << context; + EXPECT_EQ(engine->hardSparkLimiter.getTargetSkipRatio(), expectedHardSparkLimiterTargetSkipRatio) << context; + } + + TEST_F(ShiftTorqueReductionSparkSkipRatioTest, checkHardSparkLimiterTargetSkipRatio) { + checkTargetSkipRatios("Initial state"); + + satisfyFlatShiftCondition(); + checkTargetSkipRatios( + "Flat Shift condition is satisfied", + TEST_LUA_HARD_SPARK_SKIP + TEST_TORQUE_REDUCTION_IGNITION_CUT / 100.0f + ); + + unsatisfyFlatShiftCondition(); + checkTargetSkipRatios("Flat Shift condition is not satisfied"); + } +} \ No newline at end of file diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index 55f4979f6c..5f5b7d47a6 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -49,20 +49,24 @@ TESTS_SRC_CPP = \ tests/ignition_injection/test_odd_firing_engine.cpp \ tests/ignition_injection/test_three_cylinder.cpp \ tests/util/test_base.cpp \ + tests/util/test_engine_configuration.cpp \ + tests/util/test_engine_state.cpp \ tests/ac/ac_test_base.cpp \ tests/ac/ac_pressure_test.cpp \ tests/launch/launch_test_base.cpp \ - tests/launch/test_target_skip_ratio.cpp \ + tests/launch/test_launch_target_skip_ratio.cpp \ tests/launch/test_rpm_condition.cpp \ tests/launch/test_retard_threshold_rpm.cpp \ tests/launch/test_ignition_angle_advance.cpp \ tests/launch/test_spark_skip_ratio.cpp \ tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp \ + tests/shift_torque_reduction/flat_shift_condition_test_base.cpp \ tests/shift_torque_reduction/test_shift_torque_reduction_trigger_pin_state.cpp \ tests/shift_torque_reduction/test_shift_torque_reduction_time_condition.cpp \ tests/shift_torque_reduction/test_shift_torque_reduction_rpm_condition.cpp \ tests/shift_torque_reduction/test_shift_torque_reduction_app_condition.cpp \ tests/shift_torque_reduction/test_shift_torque_reduction_flat_shift_condition.cpp \ + tests/shift_torque_reduction/test_shift_torque_reduction_spark_skip_ratio.cpp \ tests/test_fft.cpp \ tests/lua/test_lua_basic.cpp \ tests/lua/test_lookup.cpp \ diff --git a/unit_tests/tests/util/test_base.cpp b/unit_tests/tests/util/test_base.cpp index 6354769eca..d80f465786 100644 --- a/unit_tests/tests/util/test_base.cpp +++ b/unit_tests/tests/util/test_base.cpp @@ -14,6 +14,14 @@ void TestBase::TearDown() { eth.reset(); } +TestEngineConfiguration& TestBase::getTestEngineConfiguration() { + return TestEngineConfiguration::getInstance(); +} + +TestEngineState& TestBase::getTestEngineState() { + return TestEngineState::getInstance(); +} + void TestBase::periodicFastCallback() { // run the ignition math engine->periodicFastCallback(); diff --git a/unit_tests/tests/util/test_base.h b/unit_tests/tests/util/test_base.h index 64d1fcb420..2681a3b208 100644 --- a/unit_tests/tests/util/test_base.h +++ b/unit_tests/tests/util/test_base.h @@ -4,12 +4,17 @@ #pragma once +#include "test_engine_configuration.h" +#include "test_engine_state.h" class TestBase : public testing::Test { protected: void SetUp() override; void TearDown() override; + TestEngineConfiguration& getTestEngineConfiguration(); + TestEngineState& getTestEngineState(); + void periodicFastCallback(); void periodicSlowCallback(); diff --git a/unit_tests/tests/util/test_engine_configuration.cpp b/unit_tests/tests/util/test_engine_configuration.cpp new file mode 100644 index 0000000000..010ffcc35e --- /dev/null +++ b/unit_tests/tests/util/test_engine_configuration.cpp @@ -0,0 +1,223 @@ +// +// Created by kifir on 11/4/24. +// + +#include "pch.h" + +#include "test_engine_configuration.h" + +#include "engine_configuration_defaults.h" + +TestEngineConfiguration& TestEngineConfiguration::getInstance() { + return instance; +} + +void TestEngineConfiguration::configureLaunchControlEnabled(const std::optional launchControlEnabled) { + if (launchControlEnabled.has_value()) { + engineConfiguration->launchControlEnabled = launchControlEnabled.value(); + } else { + ASSERT_FALSE(engineConfiguration->launchControlEnabled); // check default value + } +} + +void TestEngineConfiguration::configureLaunchRpm(const std::optional launchRpm) { + if (launchRpm.has_value()) { + engineConfiguration->launchRpm = launchRpm.value(); + } else { + ASSERT_EQ(engineConfiguration->launchRpm, 0); // check default value + } +} + +void TestEngineConfiguration::configureLaunchRpmWindow(const std::optional launchRpmWindow) { + if (launchRpmWindow.has_value()) { + engineConfiguration->launchRpmWindow = launchRpmWindow.value(); + } else { + ASSERT_EQ(engineConfiguration->launchRpmWindow, 0); // check default value + } +} + +void TestEngineConfiguration::configureLaunchCorrectionsEndRpm(const std::optional launchCorrectionsEndRpm) { + if (launchCorrectionsEndRpm.has_value()) { + engineConfiguration->launchCorrectionsEndRpm = launchCorrectionsEndRpm.value(); + } else { + ASSERT_EQ(engineConfiguration->launchCorrectionsEndRpm, 0); // check default value + } +} + +void TestEngineConfiguration::configureIgnitionRetardEnable(std::optional ignitionRetardEnable) { + if (ignitionRetardEnable.has_value()) { + engineConfiguration->enableLaunchRetard = ignitionRetardEnable.value(); + } else { + ASSERT_FALSE(engineConfiguration->enableLaunchRetard); // check default value + } +} + +void TestEngineConfiguration::configureIgnitionRetard(std::optional ignitionRetard) { + if (ignitionRetard.has_value()) { + engineConfiguration->launchTimingRetard = ignitionRetard.value(); + } else { + ASSERT_EQ(engineConfiguration->launchTimingRetard, 0); // check default value + } +} + +void TestEngineConfiguration::configureSmoothRetardMode(std::optional smoothRetardMode) { + if (smoothRetardMode.has_value()) { + engineConfiguration->launchSmoothRetard = smoothRetardMode.value(); + } else { + ASSERT_FALSE(engineConfiguration->launchSmoothRetard); // check default value + } +} + +void TestEngineConfiguration::configureEnableIgnitionCut(const std::optional enableIgnitionCut) { + if (enableIgnitionCut.has_value()) { + engineConfiguration->launchSparkCutEnable = enableIgnitionCut.value(); + } else { + ASSERT_FALSE(engineConfiguration->launchSparkCutEnable); // check default value + } +} + +void TestEngineConfiguration::configureInitialIgnitionCutPercent(const std::optional initialIgnitionCutPercent) { + if (initialIgnitionCutPercent.has_value()) { + engineConfiguration->initialIgnitionCutPercent = initialIgnitionCutPercent.value(); + } else { + ASSERT_EQ(engineConfiguration->initialIgnitionCutPercent, 0); // check default value + } +} + +void TestEngineConfiguration::configureFinalIgnitionCutPercentBeforeLaunch( + const std::optional finalIgnitionCutPercentBeforeLaunch +) { + if (finalIgnitionCutPercentBeforeLaunch.has_value()) { + engineConfiguration->finalIgnitionCutPercentBeforeLaunch = finalIgnitionCutPercentBeforeLaunch.value(); + } else { + ASSERT_EQ(engineConfiguration->finalIgnitionCutPercentBeforeLaunch, 0); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionEnabled(const std::optional torqueReductionEnabled) { + if (torqueReductionEnabled.has_value()) { + engineConfiguration->torqueReductionEnabled = torqueReductionEnabled.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionEnabled, + engine_configuration_defaults::ENABLE_SHIFT_TORQUE_REDUCTION + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionActivationMode( + const std::optional activationMode +) { + if (activationMode.has_value()) { + engineConfiguration->torqueReductionActivationMode = activationMode.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionActivationMode, + engine_configuration_defaults::TORQUE_REDUCTION_ACTIVATION_MODE + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionButton(const std::optional pin) { + if (pin.has_value()) { + engineConfiguration->torqueReductionTriggerPin = pin.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionTriggerPin, + engine_configuration_defaults::TORQUE_REDUCTION_TRIGGER_PIN + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionButtonInverted(const std::optional pinInverted) { + if (pinInverted.has_value()) { + engineConfiguration->torqueReductionTriggerPinInverted = pinInverted.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionTriggerPinInverted, + engine_configuration_defaults::TORQUE_REDUCTION_TRIGGER_PIN_INVERTED + ); // check default value + } +} + +void TestEngineConfiguration::configureLaunchActivatePin(const std::optional pin) { + if (pin.has_value()) { + engineConfiguration->launchActivatePin = pin.value(); + } else { + ASSERT_EQ( + engineConfiguration->launchActivatePin, + engine_configuration_defaults::LAUNCH_ACTIVATE_PIN + ); // check default value + } +} + +void TestEngineConfiguration::configureLaunchActivateInverted(const std::optional pinInverted) { + if (pinInverted.has_value()) { + engineConfiguration->launchActivateInverted = pinInverted.value(); + } else { + ASSERT_EQ( + engineConfiguration->launchActivateInverted, + engine_configuration_defaults::LAUNCH_ACTIVATE_PIN_INVERTED + ); // check default value + } +} + +void TestEngineConfiguration::configureLimitTorqueReductionTime(std::optional limitTorqueReductionTime) { + if (limitTorqueReductionTime.has_value()) { + engineConfiguration->limitTorqueReductionTime = limitTorqueReductionTime.value(); + } else { + ASSERT_EQ( + engineConfiguration->limitTorqueReductionTime, + engine_configuration_defaults::LIMIT_TORQUE_REDUCTION_TIME + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionTime(std::optional timeout) { + if (timeout.has_value()) { + engineConfiguration->torqueReductionTime = timeout.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionTime, + engine_configuration_defaults::TORQUE_REDUCTION_TIME + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionArmingRpm(const std::optional armingRpm) { + if (armingRpm.has_value()) { + engineConfiguration->torqueReductionArmingRpm = armingRpm.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionArmingRpm, + engine_configuration_defaults::TORQUE_REDUCTION_ARMING_RPM + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionArmingApp(const std::optional armingApp) { + if (armingApp.has_value()) { + engineConfiguration->torqueReductionArmingApp = armingApp.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionArmingApp, + engine_configuration_defaults::TORQUE_REDUCTION_ARMING_APP + ); // check default value + } +} + +void TestEngineConfiguration::configureTorqueReductionIgnitionCut(const std::optional ignitionCut) { + if (ignitionCut.has_value()) { + engineConfiguration->torqueReductionIgnitionCut = ignitionCut.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionIgnitionCut, + engine_configuration_defaults::TORQUE_REDUCTION_IGNITION_CUT + ); // check default value + } +} + +TestEngineConfiguration::TestEngineConfiguration() { +} + +TestEngineConfiguration TestEngineConfiguration::instance; \ No newline at end of file diff --git a/unit_tests/tests/util/test_engine_configuration.h b/unit_tests/tests/util/test_engine_configuration.h new file mode 100644 index 0000000000..6638d24aab --- /dev/null +++ b/unit_tests/tests/util/test_engine_configuration.h @@ -0,0 +1,43 @@ +// +// Created by kifir on 11/4/24. +// + +#pragma once + +#include "pch.h" + +class TestEngineConfiguration { +public: + static TestEngineConfiguration& getInstance(); + + // Launch Control Settings + void configureLaunchControlEnabled(std::optional launchControlEnabled); + + void configureLaunchRpm(std::optional launchRpm); + void configureLaunchRpmWindow(std::optional launchRpmWindow); + void configureLaunchCorrectionsEndRpm(std::optional launchCorrectionsEndRpm); + + void configureIgnitionRetardEnable(std::optional ignitionRetardEnable); + void configureIgnitionRetard(std::optional ignitionRetard); + void configureSmoothRetardMode(std::optional smoothRetardMode); + + void configureEnableIgnitionCut(std::optional enableIgnitionCut); + void configureInitialIgnitionCutPercent(std::optional initialIgnitionCut); + void configureFinalIgnitionCutPercentBeforeLaunch(std::optional finalIgnitionCutBeforeLaunch); + + // Shift Torque Reduction (Flat Shift) + void configureTorqueReductionEnabled(std::optional isTorqueReductionEnabled); + void configureTorqueReductionActivationMode(std::optional activationMode); + void configureTorqueReductionButton(std::optional pin); + void configureTorqueReductionButtonInverted(std::optional pinInverted); + void configureLaunchActivatePin(std::optional pin); + void configureLaunchActivateInverted(std::optional pinInverted); + void configureLimitTorqueReductionTime(std::optional limitTorqueReductionTime); + void configureTorqueReductionTime(std::optional timeout); + void configureTorqueReductionArmingRpm(std::optional armingRpm); + void configureTorqueReductionArmingApp(std::optional armingApp); + void configureTorqueReductionIgnitionCut(std::optional ignitionCut); +private: + TestEngineConfiguration(); + static TestEngineConfiguration instance; +}; diff --git a/unit_tests/tests/util/test_engine_state.cpp b/unit_tests/tests/util/test_engine_state.cpp new file mode 100644 index 0000000000..04dd261755 --- /dev/null +++ b/unit_tests/tests/util/test_engine_state.cpp @@ -0,0 +1,24 @@ +// +// Created by kifir on 11/4/24. +// + +#include "pch.h" + +#include "test_engine_state.h" + +TestEngineState& TestEngineState::getInstance() { + return instance; +} + +void TestEngineState::setLuaSoftSparkSkip(const float value) { + engine->engineState.luaSoftSparkSkip = value; +} + +void TestEngineState::setLuaHardSparkSkip(const float value) { + engine->engineState.luaHardSparkSkip = value; +} + +TestEngineState::TestEngineState() { +} + +TestEngineState TestEngineState::instance; \ No newline at end of file diff --git a/unit_tests/tests/util/test_engine_state.h b/unit_tests/tests/util/test_engine_state.h new file mode 100644 index 0000000000..8e7da2c47f --- /dev/null +++ b/unit_tests/tests/util/test_engine_state.h @@ -0,0 +1,16 @@ +// +// Created by kifir on 11/4/24. +// + +#pragma once + +class TestEngineState { +public: + static TestEngineState& getInstance(); + + void setLuaSoftSparkSkip(float value); + void setLuaHardSparkSkip(float value); +private: + TestEngineState(); + static TestEngineState instance; +};