From 9d86da59e11a752530fe80a9618f96859e5a29bc Mon Sep 17 00:00:00 2001 From: kifir Date: Tue, 5 Nov 2024 21:07:08 +0200 Subject: [PATCH] implement test for `Torque Reduction Ignition Retard(deg)` parameter #5608 --- .../algo/engine_configuration_defaults.h | 1 + .../flat_shift_condition_test_base.cpp | 8 ++- .../flat_shift_condition_test_base.h | 3 +- .../shift_torque_reduction_test_base.cpp | 10 ++- .../shift_torque_reduction_test_base.h | 3 + ...t_shift_torque_reduction_angle_advance.cpp | 61 +++++++++++++++++++ ...hift_torque_reduction_spark_skip_ratio.cpp | 2 +- unit_tests/tests/tests.mk | 1 + .../tests/util/test_engine_configuration.cpp | 11 ++++ .../tests/util/test_engine_configuration.h | 1 + 10 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp diff --git a/firmware/controllers/algo/engine_configuration_defaults.h b/firmware/controllers/algo/engine_configuration_defaults.h index e19c965f78..be01589170 100644 --- a/firmware/controllers/algo/engine_configuration_defaults.h +++ b/firmware/controllers/algo/engine_configuration_defaults.h @@ -21,6 +21,7 @@ namespace engine_configuration_defaults { constexpr float TORQUE_REDUCTION_ARMING_RPM = 0.0f; constexpr float TORQUE_REDUCTION_ARMING_APP = 0.0f; constexpr int8_t TORQUE_REDUCTION_IGNITION_CUT = 0; + constexpr float TORQUE_REDUCTION_IGNITION_RETARD = 0.0f; /* Launch Control: */ constexpr switch_input_pin_e LAUNCH_ACTIVATE_PIN = Gpio::Unassigned; 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 index 585701e0ce..cd746297d4 100644 --- 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 @@ -6,8 +6,11 @@ #include "flat_shift_condition_test_base.h" -FlatShiftConditionTestBase::FlatShiftConditionTestBase(const int8_t torqueReductionIgnitionCut) -: m_torqueReductionIgnitionCut(torqueReductionIgnitionCut) { +FlatShiftConditionTestBase::FlatShiftConditionTestBase( + const int8_t torqueReductionIgnitionCut, + const float torqueReductionIgnitionRetard +) : m_torqueReductionIgnitionCut(torqueReductionIgnitionCut), + m_torqueReductionIgnitionRetard(torqueReductionIgnitionRetard) { } void FlatShiftConditionTestBase::SetUp() { @@ -19,6 +22,7 @@ void FlatShiftConditionTestBase::SetUp() { .setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN) .setLimitTorqueReductionTime(false) .setTorqueReductionIgnitionCut(m_torqueReductionIgnitionCut) + .setTorqueReductionIgnitionRetard(m_torqueReductionIgnitionRetard) ); } 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 index 9fed7ebfd8..58bd5c61e2 100644 --- 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 @@ -11,7 +11,7 @@ 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); + FlatShiftConditionTestBase(int8_t torqueReductionIgnitionCut, float m_torqueReductionIgnitionRetard); void SetUp() override; @@ -19,4 +19,5 @@ protected: void unsatisfyFlatShiftCondition(); private: const int8_t m_torqueReductionIgnitionCut; + const float m_torqueReductionIgnitionRetard; }; \ 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 7c500966a8..660af44202 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 @@ -69,12 +69,19 @@ ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductio } ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionIgnitionCut( - std::optional value + const std::optional value ) { m_torqueReductionIgnitionCut = value; return *this; } +ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionIgnitionRetard( + const std::optional value +) { + m_torqueReductionIgnitionRetard = value; + return *this; +} + void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTestConfig& config) { getTestEngineConfiguration().configureTorqueReductionEnabled(config.getTorqueReductionEnabled()); getTestEngineConfiguration().configureTorqueReductionActivationMode(config.getTorqueReductionActivationMode()); @@ -87,4 +94,5 @@ void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTes getTestEngineConfiguration().configureTorqueReductionArmingRpm(config.getTorqueReductionArmingRpm()); getTestEngineConfiguration().configureTorqueReductionArmingApp(config.getTorqueReductionArmingApp()); getTestEngineConfiguration().configureTorqueReductionIgnitionCut(config.getTorqueReductionIgnitionCut()); + getTestEngineConfiguration().configureTorqueReductionIgnitionRetard(config.getTorqueReductionIgnitionRetard()); } 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 9af8d86cb7..53e12d8bbd 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 @@ -21,6 +21,7 @@ public: std::optional getTorqueReductionArmingRpm() const { return m_torqueReductionArmingRpm; } std::optional getTorqueReductionArmingApp() const { return m_torqueReductionArmingApp; } std::optional getTorqueReductionIgnitionCut() const { return m_torqueReductionIgnitionCut; } + std::optional getTorqueReductionIgnitionRetard() const { return m_torqueReductionIgnitionRetard; } // We do not core about performance in tests, but we want to use builder-like style, so setters return new instance // of configuration: @@ -37,6 +38,7 @@ public: ShiftTorqueReductionTestConfig setTorqueReductionArmingRpm(std::optional value); ShiftTorqueReductionTestConfig setTorqueReductionArmingApp(std::optional value); ShiftTorqueReductionTestConfig setTorqueReductionIgnitionCut(std::optional value); + ShiftTorqueReductionTestConfig setTorqueReductionIgnitionRetard(std::optional value); private: std::optional m_isTorqueReductionEnabled; std::optional m_torqueReductionActivationMode; @@ -49,6 +51,7 @@ private: std::optional m_torqueReductionArmingRpm; std::optional m_torqueReductionArmingApp; std::optional m_torqueReductionIgnitionCut; + std::optional m_torqueReductionIgnitionRetard; }; class ShiftTorqueReductionTestBase : public TestBase { diff --git a/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp new file mode 100644 index 0000000000..1b84e12ec0 --- /dev/null +++ b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp @@ -0,0 +1,61 @@ +// +// Created by kifir on 11/5/24. +// + +#include "pch.h" + +#include "flat_shift_condition_test_base.h" + +constexpr float TEST_IGNITION_RETARD = 17.4; +constexpr float TEST_TORQUE_REDUCTION_IGNITION_RETARD = 23.0f; + +namespace { + class ShiftTorqueReductionAngleAdvanceTest : public FlatShiftConditionTestBase { + protected: + ShiftTorqueReductionAngleAdvanceTest(); + + void SetUp() override; + + void checkBaseIgnitionAdvance(const char* context, float expectedAdvance); + private: + void configureTestIgnitionTable(); + }; + + ShiftTorqueReductionAngleAdvanceTest::ShiftTorqueReductionAngleAdvanceTest() + : FlatShiftConditionTestBase(0, TEST_TORQUE_REDUCTION_IGNITION_RETARD) { + } + + void ShiftTorqueReductionAngleAdvanceTest::SetUp() { + FlatShiftConditionTestBase::SetUp(); + configureTestIgnitionTable(); + } + + void ShiftTorqueReductionAngleAdvanceTest::checkBaseIgnitionAdvance( + const char* const context, + const float expectedAdvance + ) { + periodicFastCallback(); + EXPECT_NEAR(expectedAdvance, engine->ignitionState.baseIgnitionAdvance, EPS5D) << context; + } + + void ShiftTorqueReductionAngleAdvanceTest::configureTestIgnitionTable() { + IgnitionTable testIgnitionTable; + for (IgnitionTable::size_type loadIdx = 0; loadIdx < testIgnitionTable.size(); loadIdx++) { + const IgnitionTable::value_type& rpms = testIgnitionTable[loadIdx]; + for (IgnitionTable::size_type rpmIdx = 0; rpmIdx < rpms.size(); rpmIdx++) { + testIgnitionTable[loadIdx][rpmIdx] = TEST_IGNITION_RETARD; + } + }; + getTestPersistentConfiguration().setIgnitionTable(testIgnitionTable); + } + + TEST_F(ShiftTorqueReductionAngleAdvanceTest, checkAngleAdvance) { + checkBaseIgnitionAdvance("Initial state", TEST_IGNITION_RETARD); + + satisfyFlatShiftCondition(); + checkBaseIgnitionAdvance("Flat Shift condition is satisfied", TEST_TORQUE_REDUCTION_IGNITION_RETARD); + + unsatisfyFlatShiftCondition(); + checkBaseIgnitionAdvance("Flat Shift condition is not satisfied", TEST_IGNITION_RETARD); + } +} \ No newline at end of file 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 index f92f04fffb..853a2e3728 100644 --- 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 @@ -25,7 +25,7 @@ namespace { }; ShiftTorqueReductionSparkSkipRatioTest::ShiftTorqueReductionSparkSkipRatioTest() - : FlatShiftConditionTestBase(TEST_TORQUE_REDUCTION_IGNITION_CUT) { + : FlatShiftConditionTestBase(TEST_TORQUE_REDUCTION_IGNITION_CUT, 0.0f) { } void ShiftTorqueReductionSparkSkipRatioTest::SetUp() { diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index ff6a10ba63..f0e33bddca 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -68,6 +68,7 @@ TESTS_SRC_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/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp \ tests/test_fft.cpp \ tests/lua/test_lua_basic.cpp \ tests/lua/test_lookup.cpp \ diff --git a/unit_tests/tests/util/test_engine_configuration.cpp b/unit_tests/tests/util/test_engine_configuration.cpp index e40b96e073..fd2a97d804 100644 --- a/unit_tests/tests/util/test_engine_configuration.cpp +++ b/unit_tests/tests/util/test_engine_configuration.cpp @@ -241,6 +241,17 @@ void TestEngineConfiguration::configureTorqueReductionIgnitionCut(const std::opt } } +void TestEngineConfiguration::configureTorqueReductionIgnitionRetard(const std::optional ignitionRetard) { + if (ignitionRetard.has_value()) { + engineConfiguration->torqueReductionIgnitionRetard = ignitionRetard.value(); + } else { + ASSERT_EQ( + engineConfiguration->torqueReductionIgnitionRetard, + engine_configuration_defaults::TORQUE_REDUCTION_IGNITION_RETARD + ); // check default value + } +} + TestEngineConfiguration::TestEngineConfiguration() { } diff --git a/unit_tests/tests/util/test_engine_configuration.h b/unit_tests/tests/util/test_engine_configuration.h index 93904a3390..cefa101e3e 100644 --- a/unit_tests/tests/util/test_engine_configuration.h +++ b/unit_tests/tests/util/test_engine_configuration.h @@ -40,6 +40,7 @@ public: void configureTorqueReductionArmingRpm(std::optional armingRpm); void configureTorqueReductionArmingApp(std::optional armingApp); void configureTorqueReductionIgnitionCut(std::optional ignitionCut); + void configureTorqueReductionIgnitionRetard(std::optional ignitionRetard); private: TestEngineConfiguration(); static TestEngineConfiguration instance;