implement shift torque reduction trigger pin state monitoring #5608

This commit is contained in:
kifir 2024-10-02 17:00:39 +03:00 committed by kifir23917
parent 95c61f56ee
commit 1e8e31b178
9 changed files with 426 additions and 0 deletions

View File

@ -8,6 +8,7 @@ CONTROLLERS_ALGO_SRC_CPP = $(PROJECT_DIR)/controllers/algo/advance_map.cpp \
$(PROJECT_DIR)/controllers/algo/accel_enrichment.cpp \
$(PROJECT_DIR)/controllers/algo/wall_fuel.cpp \
$(PROJECT_DIR)/controllers/algo/launch_control.cpp \
$(PROJECT_DIR)/controllers/algo/shift_torque_reduction_controller.cpp \
$(PROJECT_DIR)/controllers/algo/antilag_system.cpp \
$(PROJECT_DIR)/controllers/algo/dynoview.cpp \
$(PROJECT_DIR)/controllers/algo/runtime_state.cpp \

View File

@ -173,6 +173,7 @@ void EngineState::periodicFastCallback() {
#if EFI_LAUNCH_CONTROL
engine->launchController.update();
engine->shiftTorqueReductionController.update();
#endif //EFI_LAUNCH_CONTROL
float l_ignitionLoad = getIgnitionLoad();

View File

@ -10,4 +10,14 @@ namespace engine_configuration_defaults {
constexpr uint16_t MIN_AC_PRESSURE = 100;
constexpr uint16_t MAX_AC_PRESSURE = 300;
constexpr float AC_PRESSURE_ENABLE_HYST = 10.0f;
/* Shift Torque Reduction (Flat Shift): */
constexpr bool ENABLE_SHIFT_TORQUE_REDUCTION = false;
constexpr torqueReductionActivationMode_e TORQUE_REDUCTION_ACTIVATION_MODE = TORQUE_REDUCTION_BUTTON;
constexpr switch_input_pin_e TORQUE_REDUCTION_TRIGGER_PIN = Gpio::Unassigned;
constexpr bool TORQUE_REDUCTION_TRIGGER_PIN_INVERTED = false;
/* Launch Control: */
constexpr switch_input_pin_e LAUNCH_ACTIVATE_PIN = Gpio::Unassigned;
constexpr bool LAUNCH_ACTIVATE_PIN_INVERTED = false;
}

View File

@ -4,4 +4,52 @@
#include "pch.h"
#if EFI_LAUNCH_CONTROL
#include "shift_torque_reduction_controller.h"
#include "boost_control.h"
#include "launch_control.h"
#include "advance_map.h"
#include "engine_state.h"
#include "advance_map.h"
#include "tinymt32.h"
void ShiftTorqueReductionController::update() {
if (engineConfiguration->torqueReductionEnabled) {
updateTriggerPinState();
}
}
void ShiftTorqueReductionController::updateTriggerPinState() {
switch (engineConfiguration->torqueReductionActivationMode) {
case TORQUE_REDUCTION_BUTTON: {
updateTriggerPinState(
engineConfiguration->torqueReductionTriggerPin,
engineConfiguration->torqueReductionTriggerPinInverted
);
break;
}
case LAUNCH_BUTTON: {
updateTriggerPinState(
engineConfiguration->launchActivatePin,
engineConfiguration->launchActivateInverted
);
break;
}
default: {
break; // we shouldn't be here!
}
}
}
void ShiftTorqueReductionController::updateTriggerPinState(const switch_input_pin_e pin, const bool isPinInverted) {
#if !EFI_SIMULATOR
isTorqueReductionTriggerPinValid = isBrainPinValid(pin);
if (isTorqueReductionTriggerPinValid) {
torqueReductionTriggerPinState = isPinInverted ^ efiReadPin(pin);
} else {
torqueReductionTriggerPinState = false;
}
#endif // !EFI_SIMULATOR
}
#endif // EFI_LAUNCH_CONTROL

View File

@ -7,4 +7,9 @@
#include "shift_torque_reduction_state_generated.h"
class ShiftTorqueReductionController : public shift_torque_reduction_state_s {
public:
void update();
private:
void updateTriggerPinState();
void updateTriggerPinState(switch_input_pin_e pin, bool isPinInverted);
};

View File

@ -0,0 +1,125 @@
//
// Created by kifir on 9/30/24.
//
#include "pch.h"
#include "shift_torque_reduction_test_base.h"
#include "engine_configuration_defaults.h"
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionEnabled(
const std::optional<bool> value
) {
m_isTorqueReductionEnabled = value;
return *this;
}
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTorqueReductionActivationMode(
const std::optional<torqueReductionActivationMode_e> value
) {
m_torqueReductionActivationMode = value;
return *this;
}
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setTriggerPin(
const std::optional<switch_input_pin_e> value
) {
m_torqueReductionTriggerPin = value;
return *this;
}
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setPinInverted(const std::optional<bool> value) {
m_pinInverted = value;
return *this;
}
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setLaunchActivatePin(
const std::optional<switch_input_pin_e> value
) {
m_launchActivatePin = value;
return *this;
}
ShiftTorqueReductionTestConfig ShiftTorqueReductionTestConfig::setLaunchActivateInverted(
const std::optional<bool> value
) {
m_launchActivateInverted = value;
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());
}
void ShiftTorqueReductionTestBase::configureTorqueReductionEnabled(const std::optional<bool> 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<torqueReductionActivationMode_e> 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 ShiftTorqueReductionTestBase::configureTorqueReductionButton(const std::optional<switch_input_pin_e> 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::configureTorqueReductionButtonInverted(const std::optional<bool> 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<switch_input_pin_e> 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<bool> pinInverted) {
if (pinInverted.has_value()) {
engineConfiguration->launchActivateInverted = pinInverted.value();
} else {
ASSERT_EQ(
engineConfiguration->launchActivateInverted,
engine_configuration_defaults::LAUNCH_ACTIVATE_PIN_INVERTED
); // check default value
}
}

View File

@ -0,0 +1,49 @@
//
// Created by kifir on 9/30/24.
//
#pragma once
#include "util/test_base.h"
class ShiftTorqueReductionTestConfig {
public:
std::optional<bool> getTorqueReductionEnabled() const { return m_isTorqueReductionEnabled; }
std::optional<torqueReductionActivationMode_e> getTorqueReductionActivationMode() const {
return m_torqueReductionActivationMode;
}
std::optional<switch_input_pin_e> getTriggerPin() const { return m_torqueReductionTriggerPin; }
std::optional<bool> getPinInverted() const { return m_pinInverted; }
std::optional<switch_input_pin_e> getLaunchActivatePin() const { return m_launchActivatePin; }
std::optional<bool> getLaunchActivateInverted() const { return m_launchActivateInverted; }
// We do not core about performance in tests, but we want to use builder-like style, so setters return new instance
// of configuration:
ShiftTorqueReductionTestConfig setTorqueReductionEnabled(std::optional<bool> value);
ShiftTorqueReductionTestConfig setTorqueReductionActivationMode(
std::optional<torqueReductionActivationMode_e> value
);
ShiftTorqueReductionTestConfig setTriggerPin(std::optional<switch_input_pin_e> value);
ShiftTorqueReductionTestConfig setPinInverted(std::optional<bool> value);
ShiftTorqueReductionTestConfig setLaunchActivatePin(std::optional<switch_input_pin_e> value);
ShiftTorqueReductionTestConfig setLaunchActivateInverted(std::optional<bool> value);
private:
std::optional<bool> m_isTorqueReductionEnabled;
std::optional<torqueReductionActivationMode_e> m_torqueReductionActivationMode;
std::optional<switch_input_pin_e> m_torqueReductionTriggerPin;
std::optional<bool> m_pinInverted;
std::optional<switch_input_pin_e> m_launchActivatePin;
std::optional<bool> m_launchActivateInverted;
};
class ShiftTorqueReductionTestBase : public TestBase {
protected:
void setUpTestConfig(const ShiftTorqueReductionTestConfig& config);
private:
void configureTorqueReductionEnabled(std::optional<bool> isTorqueReductionEnabled);
void configureTorqueReductionActivationMode(std::optional<torqueReductionActivationMode_e> activationMode);
void configureTorqueReductionButton(std::optional<switch_input_pin_e> pin);
void configureTorqueReductionButtonInverted(std::optional<bool> pinInverted);
void configureLaunchActivatePin(std::optional<switch_input_pin_e> pin);
void configureLaunchActivateInverted(std::optional<bool> pinInverted);
};

View File

@ -0,0 +1,185 @@
//
// Created by kifir on 9/30/24.
//
#include "pch.h"
#include "shift_torque_reduction_test_base.h"
namespace {
constexpr switch_input_pin_e TEST_TORQUE_REDUCTION_BUTTON_PIN = Gpio::F15;
constexpr switch_input_pin_e TEST_LAUNCH_BUTTON_PIN = Gpio::G15;
struct ShiftTorqueReductionTriggerPinTestData {
const std::string context;
const bool isTorqueReductionTriggerPinValid;
const bool torqueReductionTriggerPinState;
};
class ShiftTorqueReductionTriggerPinTest : public ShiftTorqueReductionTestBase {
protected:
void checkShiftTorqueReductionState(const ShiftTorqueReductionTriggerPinTestData& expected);
void checkShiftTorqueReductionStateAfterPeriodicFastCallback(
const ShiftTorqueReductionTriggerPinTestData& expected
);
void checkShiftTorqueReductionStateWithPinState(
switch_input_pin_e triggerPin,
bool pinState,
const ShiftTorqueReductionTriggerPinTestData& expected
);
};
void ShiftTorqueReductionTriggerPinTest::checkShiftTorqueReductionState(
const ShiftTorqueReductionTriggerPinTestData& expected
) {
EXPECT_EQ(
engine->shiftTorqueReductionController.isTorqueReductionTriggerPinValid,
expected.isTorqueReductionTriggerPinValid
) << expected.context;
EXPECT_EQ(
engine->shiftTorqueReductionController.torqueReductionTriggerPinState,
expected.torqueReductionTriggerPinState
) << expected.context;
}
void ShiftTorqueReductionTriggerPinTest::checkShiftTorqueReductionStateAfterPeriodicFastCallback(
const ShiftTorqueReductionTriggerPinTestData& expected
) {
periodicFastCallback();
checkShiftTorqueReductionState(expected);
}
void ShiftTorqueReductionTriggerPinTest::checkShiftTorqueReductionStateWithPinState(
const switch_input_pin_e triggerPin,
const bool pinState,
const ShiftTorqueReductionTriggerPinTestData& expected
) {
setMockState(triggerPin, pinState);
checkShiftTorqueReductionStateAfterPeriodicFastCallback(expected);
}
TEST_F(ShiftTorqueReductionTriggerPinTest, checkDefaultConfiguration) {
setUpTestConfig(ShiftTorqueReductionTestConfig());
checkShiftTorqueReductionStateAfterPeriodicFastCallback({ "Default trigger pin state", false, false });
}
TEST_F(ShiftTorqueReductionTriggerPinTest, checkTorqueReductionTriggerPinSwitch) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setLaunchActivatePin(TEST_LAUNCH_BUTTON_PIN)
);
checkShiftTorqueReductionStateAfterPeriodicFastCallback({ "Default trigger pin state", true, false });
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
true,
{ "Torque reduction trigger pin is on", true, true }
);
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
false,
{ "Torque reduction trigger pin is off", true, false }
);
// Check that launch button pin switching does not switch shift torque reduction trigger pin:
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
true,
{ "Launch activate pin is on", true, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
false,
{ "Launch activate pin is off", true, false }
);
}
TEST_F(ShiftTorqueReductionTriggerPinTest, checkTorqueReductionTriggerInvertedPinSwitch) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setPinInverted(true)
);
checkShiftTorqueReductionStateAfterPeriodicFastCallback({ "Default trigger pin state", true, true });
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
true,
{ "Torque reduction trigger pin is on", true, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
false,
{ "Torque reduction trigger pin is off", true, true }
);
}
TEST_F(ShiftTorqueReductionTriggerPinTest, checkLaunchActivatePinSwitch) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(true)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::LAUNCH_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setLaunchActivatePin(TEST_LAUNCH_BUTTON_PIN)
);
checkShiftTorqueReductionStateAfterPeriodicFastCallback({ "Default trigger pin state", true, false });
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
true,
{ "Launch activate pin is on", true, true }
);
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
false,
{ "Launch activate pin is off", true, false }
);
// Check that launch button pin switching does not switch shift torque reduction trigger pin:
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
true,
{ "Torque reduction trigger pin is on", true, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
false,
{ "Torque reduction trigger pin is off", true, false }
);
}
TEST_F(ShiftTorqueReductionTriggerPinTest, checkTorqueReductionTriggerPinSwitchWithDisabledTorqueReduction) {
setUpTestConfig(ShiftTorqueReductionTestConfig()
.setTorqueReductionEnabled(false)
.setTorqueReductionActivationMode(torqueReductionActivationMode_e::TORQUE_REDUCTION_BUTTON)
.setTriggerPin(TEST_TORQUE_REDUCTION_BUTTON_PIN)
.setLaunchActivatePin(TEST_LAUNCH_BUTTON_PIN)
);
checkShiftTorqueReductionStateAfterPeriodicFastCallback({ "Default trigger pin state", false, false });
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
true,
{ "Torque reduction trigger pin is on", false, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_TORQUE_REDUCTION_BUTTON_PIN,
false,
{ "Torque reduction trigger pin is off", false, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
true,
{ "Launch activate pin is on", false, false }
);
checkShiftTorqueReductionStateWithPinState(
TEST_LAUNCH_BUTTON_PIN,
false,
{ "Launch activate pin is off", false, false }
);
}
}

View File

@ -57,6 +57,8 @@ TESTS_SRC_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/test_shift_torque_reduction_trigger_pin_state.cpp \
tests/lua/test_lua_basic.cpp \
tests/lua/test_lookup.cpp \
tests/lua/test_lua_e38.cpp \