implement time condition monitoring #5608

This commit is contained in:
kifir 2024-10-02 20:54:35 +03:00 committed by kifir23917
parent 1e8e31b178
commit 953d085904
8 changed files with 168 additions and 0 deletions

View File

@ -16,6 +16,7 @@ namespace engine_configuration_defaults {
constexpr torqueReductionActivationMode_e TORQUE_REDUCTION_ACTIVATION_MODE = TORQUE_REDUCTION_BUTTON; constexpr torqueReductionActivationMode_e TORQUE_REDUCTION_ACTIVATION_MODE = TORQUE_REDUCTION_BUTTON;
constexpr switch_input_pin_e TORQUE_REDUCTION_TRIGGER_PIN = Gpio::Unassigned; constexpr switch_input_pin_e TORQUE_REDUCTION_TRIGGER_PIN = Gpio::Unassigned;
constexpr bool TORQUE_REDUCTION_TRIGGER_PIN_INVERTED = false; constexpr bool TORQUE_REDUCTION_TRIGGER_PIN_INVERTED = false;
constexpr float TORQUE_REDUCTION_TIME = 0.0f;
/* Launch Control: */ /* Launch Control: */
constexpr switch_input_pin_e LAUNCH_ACTIVATE_PIN = Gpio::Unassigned; constexpr switch_input_pin_e LAUNCH_ACTIVATE_PIN = Gpio::Unassigned;

View File

@ -16,6 +16,7 @@
void ShiftTorqueReductionController::update() { void ShiftTorqueReductionController::update() {
if (engineConfiguration->torqueReductionEnabled) { if (engineConfiguration->torqueReductionEnabled) {
updateTriggerPinState(); updateTriggerPinState();
updateTimeConditionSatisfied();
} }
} }
@ -45,11 +46,22 @@ void ShiftTorqueReductionController::updateTriggerPinState(const switch_input_pi
#if !EFI_SIMULATOR #if !EFI_SIMULATOR
isTorqueReductionTriggerPinValid = isBrainPinValid(pin); isTorqueReductionTriggerPinValid = isBrainPinValid(pin);
if (isTorqueReductionTriggerPinValid) { if (isTorqueReductionTriggerPinValid) {
const bool previousTorqueReductionTriggerPinState = torqueReductionTriggerPinState;
torqueReductionTriggerPinState = isPinInverted ^ efiReadPin(pin); torqueReductionTriggerPinState = isPinInverted ^ efiReadPin(pin);
if (!previousTorqueReductionTriggerPinState && torqueReductionTriggerPinState) {
m_pinTriggeredTimer.reset();
}
} else { } else {
torqueReductionTriggerPinState = false; torqueReductionTriggerPinState = false;
} }
#endif // !EFI_SIMULATOR #endif // !EFI_SIMULATOR
} }
void ShiftTorqueReductionController::updateTimeConditionSatisfied() {
isTimeConditionSatisfied = torqueReductionTriggerPinState
? (0.0f < engineConfiguration->torqueReductionTime)
&& !m_pinTriggeredTimer.hasElapsedMs(engineConfiguration->torqueReductionTime)
: false;
}
#endif // EFI_LAUNCH_CONTROL #endif // EFI_LAUNCH_CONTROL

View File

@ -12,4 +12,8 @@ public:
private: private:
void updateTriggerPinState(); void updateTriggerPinState();
void updateTriggerPinState(switch_input_pin_e pin, bool isPinInverted); void updateTriggerPinState(switch_input_pin_e pin, bool isPinInverted);
void updateTimeConditionSatisfied();
Timer m_pinTriggeredTimer;
}; };

View File

@ -2,5 +2,6 @@ struct_no_prefix shift_torque_reduction_state_s
bit isTorqueReductionTriggerPinValid bit isTorqueReductionTriggerPinValid
bit torqueReductionTriggerPinState bit torqueReductionTriggerPinState
bit isTimeConditionSatisfied
end_struct end_struct

View File

@ -47,6 +47,13 @@ ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setLaunchActivate
return *this; return *this;
} }
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionTime(
const std::optional<float> value
) {
m_torqueReductionTime = value;
return *this;
}
void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTestConfig& config) { void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTestConfig& config) {
configureTorqueReductionEnabled(config.getTorqueReductionEnabled()); configureTorqueReductionEnabled(config.getTorqueReductionEnabled());
configureTorqueReductionActivationMode(config.getTorqueReductionActivationMode()); configureTorqueReductionActivationMode(config.getTorqueReductionActivationMode());
@ -54,6 +61,7 @@ void ShiftTorqueReductionTestBase::setUpTestConfig(const ShiftTorqueReductionTes
configureTorqueReductionButtonInverted(config.getPinInverted()); configureTorqueReductionButtonInverted(config.getPinInverted());
configureLaunchActivatePin(config.getLaunchActivatePin()); configureLaunchActivatePin(config.getLaunchActivatePin());
configureLaunchActivateInverted(config.getLaunchActivateInverted()); configureLaunchActivateInverted(config.getLaunchActivateInverted());
configureTorqueReductionTime(config.getTorqueReductionTime());
} }
void ShiftTorqueReductionTestBase::configureTorqueReductionEnabled(const std::optional<bool> torqueReductionEnabled) { void ShiftTorqueReductionTestBase::configureTorqueReductionEnabled(const std::optional<bool> torqueReductionEnabled) {
@ -122,4 +130,15 @@ void ShiftTorqueReductionTestBase::configureLaunchActivateInverted(const std::op
engine_configuration_defaults::LAUNCH_ACTIVATE_PIN_INVERTED engine_configuration_defaults::LAUNCH_ACTIVATE_PIN_INVERTED
); // check default value ); // check default value
} }
}
void ShiftTorqueReductionTestBase::configureTorqueReductionTime(std::optional<float> timeout) {
if (timeout.has_value()) {
engineConfiguration->torqueReductionTime = timeout.value();
} else {
ASSERT_EQ(
engineConfiguration->torqueReductionTime,
engine_configuration_defaults::TORQUE_REDUCTION_TIME
); // check default value
}
} }

View File

@ -16,6 +16,7 @@ public:
std::optional<bool> getPinInverted() const { return m_pinInverted; } std::optional<bool> getPinInverted() const { return m_pinInverted; }
std::optional<switch_input_pin_e> getLaunchActivatePin() const { return m_launchActivatePin; } std::optional<switch_input_pin_e> getLaunchActivatePin() const { return m_launchActivatePin; }
std::optional<bool> getLaunchActivateInverted() const { return m_launchActivateInverted; } std::optional<bool> getLaunchActivateInverted() const { return m_launchActivateInverted; }
std::optional<float> getTorqueReductionTime() const { return m_torqueReductionTime; }
// We do not core about performance in tests, but we want to use builder-like style, so setters return new instance // We do not core about performance in tests, but we want to use builder-like style, so setters return new instance
// of configuration: // of configuration:
@ -27,6 +28,7 @@ public:
ShiftTorqueReductionTestConfig setPinInverted(std::optional<bool> value); ShiftTorqueReductionTestConfig setPinInverted(std::optional<bool> value);
ShiftTorqueReductionTestConfig setLaunchActivatePin(std::optional<switch_input_pin_e> value); ShiftTorqueReductionTestConfig setLaunchActivatePin(std::optional<switch_input_pin_e> value);
ShiftTorqueReductionTestConfig setLaunchActivateInverted(std::optional<bool> value); ShiftTorqueReductionTestConfig setLaunchActivateInverted(std::optional<bool> value);
ShiftTorqueReductionTestConfig setTorqueReductionTime(std::optional<float> value);
private: private:
std::optional<bool> m_isTorqueReductionEnabled; std::optional<bool> m_isTorqueReductionEnabled;
std::optional<torqueReductionActivationMode_e> m_torqueReductionActivationMode; std::optional<torqueReductionActivationMode_e> m_torqueReductionActivationMode;
@ -34,6 +36,7 @@ private:
std::optional<bool> m_pinInverted; std::optional<bool> m_pinInverted;
std::optional<switch_input_pin_e> m_launchActivatePin; std::optional<switch_input_pin_e> m_launchActivatePin;
std::optional<bool> m_launchActivateInverted; std::optional<bool> m_launchActivateInverted;
std::optional<float> m_torqueReductionTime;
}; };
class ShiftTorqueReductionTestBase : public TestBase { class ShiftTorqueReductionTestBase : public TestBase {
@ -46,4 +49,5 @@ private:
void configureTorqueReductionButtonInverted(std::optional<bool> pinInverted); void configureTorqueReductionButtonInverted(std::optional<bool> pinInverted);
void configureLaunchActivatePin(std::optional<switch_input_pin_e> pin); void configureLaunchActivatePin(std::optional<switch_input_pin_e> pin);
void configureLaunchActivateInverted(std::optional<bool> pinInverted); void configureLaunchActivateInverted(std::optional<bool> pinInverted);
void configureTorqueReductionTime(std::optional<float> timeout);
}; };

View File

@ -0,0 +1,126 @@
//
// Created by kifir on 10/2/24.
//
#include "pch.h"
#include "shift_torque_reduction_test_base.h"
namespace {
constexpr switch_input_pin_e TEST_TORQUE_REDUCTION_BUTTON_PIN = Gpio::F13;
constexpr float TEST_TORQUE_REDUCTION_TIME = 239.17;
constexpr float IMMEDIATELY = 0.0f;
class ShiftTorqueReductionTimeConditionTest : public ShiftTorqueReductionTestBase {
protected:
void waitAndCheckTimeCondition(
float timeoutInMs,
bool expectedTriggerPinState,
bool expectedTimeCondition,
const char* context
);
};
void ShiftTorqueReductionTimeConditionTest::waitAndCheckTimeCondition(
const float timeoutInMs,
const bool expectedTriggerPinState,
const bool expectedTimeCondition,
const char* const context
) {
if (timeoutInMs > IMMEDIATELY) {
advanceTimeUs(MS2US(timeoutInMs));
}
periodicFastCallback();
EXPECT_EQ(
engine->shiftTorqueReductionController.torqueReductionTriggerPinState,
expectedTriggerPinState
) << context;
EXPECT_EQ(
engine->shiftTorqueReductionController.isTimeConditionSatisfied,
expectedTimeCondition
) << context;
}
TEST_F(ShiftTorqueReductionTimeConditionTest, checkExpiration) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setTorqueReductionTime(TEST_TORQUE_REDUCTION_TIME)
);
waitAndCheckTimeCondition(IMMEDIATELY, false, false, "Initial state");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, true, "Pin has just been actvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME, true, true, "Before timeout exriration");
waitAndCheckTimeCondition(EPS3D, true, false, "After timeout expiration");
}
TEST_F(ShiftTorqueReductionTimeConditionTest, checkDeactivation) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setTorqueReductionTime(TEST_TORQUE_REDUCTION_TIME)
);
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, true, "Pin has just been actvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME / 2, true, true, "Before pin deactivation");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, false);
waitAndCheckTimeCondition(IMMEDIATELY, false, false, "Pin has just been deactvated");
}
TEST_F(ShiftTorqueReductionTimeConditionTest, checkReactivation) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setTorqueReductionTime(TEST_TORQUE_REDUCTION_TIME)
);
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, true, "Pin has just been actvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME / 3, true, true, "Before pin deactivation");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, false);
waitAndCheckTimeCondition(IMMEDIATELY, false, false, "Pin has just been deactvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME / 3, false, false, "Pin is still deactvated");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, true, "Pin has just been reactivated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME, true, true, "Before timeout expiration");
waitAndCheckTimeCondition(EPS3D, true, false, "After timeout expiration");
}
TEST_F(ShiftTorqueReductionTimeConditionTest, checkTimeConditionIsNeverSatisfiedWithZeroTorqueReductionTime) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setTorqueReductionTime(IMMEDIATELY)
);
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, false, "Pin has just been actvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME / 3, true, false, "Before pin deactivation");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, false);
waitAndCheckTimeCondition(IMMEDIATELY, false, false, "Pin has just been deactvated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME / 3, false, false, "Pin is still deactvated");
setMockState(TEST_TORQUE_REDUCTION_BUTTON_PIN, true);
waitAndCheckTimeCondition(IMMEDIATELY, true, false, "Pin has just been reactivated");
waitAndCheckTimeCondition(TEST_TORQUE_REDUCTION_TIME, true, false, "Before timeout expiration");
waitAndCheckTimeCondition(EPS3D, true, false, "After timeout expiration");
}
}

View File

@ -59,6 +59,7 @@ TESTS_SRC_CPP = \
tests/launch/test_spark_skip_ratio.cpp \ tests/launch/test_spark_skip_ratio.cpp \
tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp \ tests/shift_torque_reduction/shift_torque_reduction_test_base.cpp \
tests/shift_torque_reduction/test_shift_torque_reduction_trigger_pin_state.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/lua/test_lua_basic.cpp \ tests/lua/test_lua_basic.cpp \
tests/lua/test_lookup.cpp \ tests/lua/test_lookup.cpp \
tests/lua/test_lua_e38.cpp \ tests/lua/test_lua_e38.cpp \