diff --git a/firmware/controllers/algo/shift_torque_reduction_controller.cpp b/firmware/controllers/algo/shift_torque_reduction_controller.cpp index 5bbb6b2e4f..24716eaaa4 100644 --- a/firmware/controllers/algo/shift_torque_reduction_controller.cpp +++ b/firmware/controllers/algo/shift_torque_reduction_controller.cpp @@ -19,6 +19,9 @@ void ShiftTorqueReductionController::update() { updateTimeConditionSatisfied(); updateRpmConditionSatisfied(); updateAppConditionSatisfied(); + + isFlatShiftConditionSatisfied = torqueReductionTriggerPinState && isTimeConditionSatisfied + && isRpmConditionSatisfied && isAppConditionSatisfied; } } diff --git a/firmware/controllers/algo/shift_torque_reduction_state.txt b/firmware/controllers/algo/shift_torque_reduction_state.txt index 1a9eb30d3b..b88ddecd17 100644 --- a/firmware/controllers/algo/shift_torque_reduction_state.txt +++ b/firmware/controllers/algo/shift_torque_reduction_state.txt @@ -5,5 +5,6 @@ bit torqueReductionTriggerPinState bit isTimeConditionSatisfied bit isRpmConditionSatisfied bit isAppConditionSatisfied +bit isFlatShiftConditionSatisfied end_struct diff --git a/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_flat_shift_condition.cpp b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_flat_shift_condition.cpp new file mode 100644 index 0000000000..6de50d375c --- /dev/null +++ b/unit_tests/tests/shift_torque_reduction/test_shift_torque_reduction_flat_shift_condition.cpp @@ -0,0 +1,112 @@ +// +// Created by kifir on 10/4/24. +// + +#include "pch.h" + +#include "shift_torque_reduction_test_base.h" + +namespace { + class ShiftTorqueReductionFlatShiftConditionTest : public ShiftTorqueReductionTestBase { + protected: + static constexpr switch_input_pin_e TEST_TORQUE_REDUCTION_BUTTON_PIN = Gpio::E13; + static constexpr float TEST_TORQUE_REDUCTION_TIME = 123.45f; + static constexpr float TEST_TORQUE_REDUCTION_ARMING_RPM = 678.90f; + static constexpr float TEST_TORQUE_REDUCTION_ARMING_APP = 12.3f; + static constexpr float IMMEDIATELY = 0.0f; + + void SetUp() override; + + void triggerPin(); + void satisfyRpmCondition(); + void satisfyAppCondition(); + + void waitAndCheckFlatShiftCondition(float timeoutInMs, bool expectedFlatShiftCondition, const char* context); + }; + + void ShiftTorqueReductionFlatShiftConditionTest::SetUp() { + TestBase::SetUp(); + setUpTestConfig(ShiftTorqueReductionTestConfig() + .setTorqueReductionEnabled(true) + .setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON) + .setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN) + .setTorqueReductionTime(TEST_TORQUE_REDUCTION_TIME) + .setTorqueReductionArmingRpm(TEST_TORQUE_REDUCTION_ARMING_RPM) + .setTorqueReductionArmingApp(TEST_TORQUE_REDUCTION_ARMING_APP) + ); + } + + void ShiftTorqueReductionFlatShiftConditionTest::triggerPin() { + setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true); + periodicFastCallback(); + EXPECT_TRUE(engine->shiftTorqueReductionController.isTorqueReductionTriggerPinValid); + EXPECT_TRUE(engine->shiftTorqueReductionController.torqueReductionTriggerPinState); + EXPECT_TRUE(engine->shiftTorqueReductionController.isTimeConditionSatisfied); + } + + void ShiftTorqueReductionFlatShiftConditionTest::satisfyRpmCondition() { + updateRpm(TEST_TORQUE_REDUCTION_ARMING_RPM); + EXPECT_TRUE(engine->shiftTorqueReductionController.isRpmConditionSatisfied); + } + + void ShiftTorqueReductionFlatShiftConditionTest::satisfyAppCondition() { + updateApp(TEST_TORQUE_REDUCTION_ARMING_APP); + EXPECT_TRUE(engine->shiftTorqueReductionController.isAppConditionSatisfied); + } + + void ShiftTorqueReductionFlatShiftConditionTest::waitAndCheckFlatShiftCondition( + const float timeoutInMs, + const bool expectedFlatShiftCondition, + const char* const context + ) { + advanceTimeUs(MS2US(timeoutInMs)); + periodicFastCallback(); + EXPECT_EQ(engine->shiftTorqueReductionController.isFlatShiftConditionSatisfied, expectedFlatShiftCondition) + << context; + } + + TEST_F(ShiftTorqueReductionFlatShiftConditionTest, checkSatisfiedFlatShiftCondition) { + triggerPin(); + satisfyRpmCondition(); + satisfyAppCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, true, "App condition is satisfied"); + } + + TEST_F(ShiftTorqueReductionFlatShiftConditionTest, checkWithoutTriggeredPin) { + //triggerPin(); + satisfyRpmCondition(); + satisfyAppCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Without triggered pin"); + } + + TEST_F(ShiftTorqueReductionFlatShiftConditionTest, checkWithoutRpmCondition) { + triggerPin(); + //satisfyRpmCondition(); + satisfyAppCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Without rpm condition"); + } + + TEST_F(ShiftTorqueReductionFlatShiftConditionTest, checkWithoutAppCondition) { + triggerPin(); + satisfyRpmCondition(); + //satisfyAppCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Without app condition"); + } + + TEST_F(ShiftTorqueReductionFlatShiftConditionTest, checkSatisfiedFlatShiftConditionExpiration) { + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Default"); + + triggerPin(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Time condition is satisfied"); + + satisfyRpmCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, false, "Rpm condition is satisfied"); + + satisfyAppCondition(); + waitAndCheckFlatShiftCondition(IMMEDIATELY, true, "App condition is satisfied"); + + waitAndCheckFlatShiftCondition(TEST_TORQUE_REDUCTION_TIME, true, "Flat shift condition is still satisfied"); + + waitAndCheckFlatShiftCondition(EPS3D, false, "Flat shift condition is expired"); + } +} \ No newline at end of file diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index cc96afe318..2e29a6e0cf 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -62,6 +62,7 @@ TESTS_SRC_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/lua/test_lua_basic.cpp \ tests/lua/test_lookup.cpp \ tests/lua/test_lua_e38.cpp \