2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2020-08-17 02:22:25 -07:00
|
|
|
#include "injector_model.h"
|
|
|
|
|
2021-07-07 20:46:44 -07:00
|
|
|
using ::testing::_;
|
2020-08-17 02:22:25 -07:00
|
|
|
using ::testing::StrictMock;
|
|
|
|
|
|
|
|
class MockInjectorModel : public InjectorModelBase {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD(floatms_t, getDeadtime, (), (const, override));
|
2021-12-26 09:59:53 -08:00
|
|
|
MOCK_METHOD(float, getInjectorMassFlowRate, (), (override));
|
|
|
|
MOCK_METHOD(float, getInjectorFlowRatio, (), (override));
|
2020-11-10 20:11:22 -08:00
|
|
|
MOCK_METHOD(expected<float>, getAbsoluteRailPressure, (), (const, override));
|
2021-07-07 20:46:44 -07:00
|
|
|
MOCK_METHOD(float, correctShortPulse, (float baseDuration), (const, override));
|
2020-08-17 02:22:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(InjectorModel, Prepare) {
|
|
|
|
StrictMock<MockInjectorModel> dut;
|
|
|
|
|
|
|
|
EXPECT_CALL(dut, getDeadtime());
|
|
|
|
EXPECT_CALL(dut, getInjectorMassFlowRate());
|
|
|
|
|
|
|
|
dut.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InjectorModel, getInjectionDuration) {
|
|
|
|
StrictMock<MockInjectorModel> dut;
|
|
|
|
|
|
|
|
EXPECT_CALL(dut, getDeadtime())
|
2021-07-07 20:46:44 -07:00
|
|
|
.WillOnce(Return(2.0f));
|
2020-08-17 02:22:25 -07:00
|
|
|
EXPECT_CALL(dut, getInjectorMassFlowRate())
|
2021-07-07 20:46:44 -07:00
|
|
|
.WillOnce(Return(4.8f)); // 400cc/min
|
|
|
|
EXPECT_CALL(dut, correctShortPulse(_))
|
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly([](float b) { return b; });
|
2020-08-17 02:22:25 -07:00
|
|
|
|
|
|
|
dut.prepare();
|
|
|
|
|
|
|
|
EXPECT_NEAR(dut.getInjectionDuration(0.01f), 10 / 4.8f + 2.0f, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.getInjectionDuration(0.02f), 20 / 4.8f + 2.0f, EPS4D);
|
|
|
|
}
|
|
|
|
|
2021-07-07 20:46:44 -07:00
|
|
|
TEST(InjectorModel, getInjectionDurationNonlinear) {
|
|
|
|
StrictMock<MockInjectorModel> dut;
|
|
|
|
|
|
|
|
EXPECT_CALL(dut, getDeadtime())
|
|
|
|
.WillOnce(Return(2.0f));
|
|
|
|
EXPECT_CALL(dut, getInjectorMassFlowRate())
|
|
|
|
.WillOnce(Return(4.8f)); // 400cc/min
|
|
|
|
|
|
|
|
// Dummy nonlinearity correction: just doubles the pulse
|
|
|
|
EXPECT_CALL(dut, correctShortPulse(_))
|
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly([](float b) { return 2 * b; });
|
|
|
|
|
|
|
|
dut.prepare();
|
|
|
|
|
|
|
|
EXPECT_NEAR(dut.getInjectionDuration(0.01f), 2 * 10 / 4.8f + 2.0f, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.getInjectionDuration(0.02f), 2 * 20 / 4.8f + 2.0f, EPS4D);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InjectorModel, nonlinearPolynomial) {
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2021-07-07 20:46:44 -07:00
|
|
|
InjectorModel dut;
|
|
|
|
|
2022-04-28 05:16:02 -07:00
|
|
|
engineConfiguration->applyNonlinearBelowPulse = 10;
|
2021-07-07 20:46:44 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->injectorCorrectionPolynomial[i] = i + 1;
|
2021-07-07 20:46:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// expect return of the original value, plus polynomial f(x)
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(-3), -3 + -13532, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(-2), -2 + -711, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(-1), -1 + -4, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(0), 0 + 1, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(1), 1 + 36, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(2), 2 + 1793, EPS4D);
|
|
|
|
EXPECT_NEAR(dut.correctInjectionPolynomial(3), 3 + 24604, EPS4D);
|
2022-04-28 05:16:02 -07:00
|
|
|
|
|
|
|
// Check that the disable threshold works
|
|
|
|
EXPECT_NE(dut.correctInjectionPolynomial(9.9f), 9.9f);
|
|
|
|
EXPECT_EQ(dut.correctInjectionPolynomial(10.1f), 10.1f);
|
2021-07-07 20:46:44 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 02:22:25 -07:00
|
|
|
TEST(InjectorModel, Deadtime) {
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-08-17 02:22:25 -07:00
|
|
|
|
|
|
|
// Some test data in the injector correction table
|
|
|
|
for (size_t i = 0; i < efi::size(engineConfiguration->injector.battLagCorr); i++) {
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->injector.battLagCorr[i] = 2 * i;
|
|
|
|
engineConfiguration->injector.battLagCorrBins[i] = i;
|
2020-08-17 02:22:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
InjectorModel dut;
|
|
|
|
|
2021-03-11 20:07:18 -08:00
|
|
|
Sensor::setMockValue(SensorType::BatteryVoltage, 3);
|
2020-08-17 02:22:25 -07:00
|
|
|
EXPECT_EQ(dut.getDeadtime(), 6);
|
|
|
|
|
2021-03-11 20:07:18 -08:00
|
|
|
Sensor::setMockValue(SensorType::BatteryVoltage, 7);
|
2020-08-17 02:22:25 -07:00
|
|
|
EXPECT_EQ(dut.getDeadtime(), 14);
|
|
|
|
}
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
struct TesterGetFlowRate : public InjectorModel {
|
2021-12-26 09:59:53 -08:00
|
|
|
MOCK_METHOD(float, getInjectorFlowRatio, (), (override));
|
2020-11-10 20:11:22 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TesterGetRailPressure : public InjectorModel {
|
|
|
|
MOCK_METHOD(expected<float>, getAbsoluteRailPressure, (), (const, override));
|
|
|
|
};
|
|
|
|
|
|
|
|
class FlowRateFixture : public ::testing::TestWithParam<float> {
|
|
|
|
};
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
InjectorModel,
|
|
|
|
FlowRateFixture,
|
|
|
|
::testing::Values(0.1f, 0.5f, 1.0f, 2.0f, 10.0f)
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_P(FlowRateFixture, FlowRateRatio) {
|
|
|
|
float flowRatio = GetParam();
|
|
|
|
|
|
|
|
StrictMock<TesterGetFlowRate> dut;
|
|
|
|
EXPECT_CALL(dut, getInjectorFlowRatio()).WillOnce(Return(flowRatio));
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
engineConfiguration->injector.flow = 500;
|
|
|
|
|
|
|
|
// 500 cc/min = 6g/s
|
|
|
|
float expectedFlow = flowRatio * 6.0f;
|
|
|
|
|
|
|
|
// Check that flow is adjusted correctly
|
|
|
|
EXPECT_FLOAT_EQ(expectedFlow, dut.getInjectorMassFlowRate());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(FlowRateFixture, PressureRatio) {
|
|
|
|
float pressureRatio = GetParam();
|
|
|
|
// Flow ratio should be the sqrt of pressure ratio
|
|
|
|
float expectedFlowRatio = sqrtf(pressureRatio);
|
|
|
|
float fakeMap = 35.0f;
|
|
|
|
|
|
|
|
StrictMock<TesterGetRailPressure> dut;
|
|
|
|
EXPECT_CALL(dut, getAbsoluteRailPressure()).WillOnce(Return(400 * pressureRatio + fakeMap));
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
// Use injector compensation
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_SensedRailPressure;
|
|
|
|
|
|
|
|
// Reference pressure is 400kPa
|
|
|
|
engineConfiguration->fuelReferencePressure = 400.0f;
|
|
|
|
|
|
|
|
// MAP sensor always reads 35 kpa
|
2021-01-02 16:13:10 -08:00
|
|
|
Sensor::setMockValue(SensorType::Map, fakeMap);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
// Should return the expected ratio
|
|
|
|
EXPECT_FLOAT_EQ(expectedFlowRatio, dut.getInjectorFlowRatio());
|
|
|
|
}
|
|
|
|
|
2021-03-12 20:32:41 -08:00
|
|
|
TEST(InjectorModel, NegativePressureDelta) {
|
|
|
|
StrictMock<TesterGetRailPressure> dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2021-03-12 20:32:41 -08:00
|
|
|
|
|
|
|
// Use injector compensation
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_SensedRailPressure;
|
|
|
|
|
|
|
|
// Reference pressure is 400kPa
|
|
|
|
engineConfiguration->fuelReferencePressure = 400.0f;
|
|
|
|
|
|
|
|
EXPECT_CALL(dut, getAbsoluteRailPressure()).WillOnce(Return(50));
|
|
|
|
// MAP sensor reads more pressure than fuel rail
|
|
|
|
Sensor::setMockValue(SensorType::Map, 100);
|
|
|
|
|
|
|
|
// Flow ratio defaults to 1.0 in this case
|
|
|
|
EXPECT_FLOAT_EQ(1.0f, dut.getInjectorFlowRatio());
|
|
|
|
}
|
|
|
|
|
2020-11-10 20:11:22 -08:00
|
|
|
TEST(InjectorModel, VariableInjectorFlowModeNone) {
|
|
|
|
StrictMock<TesterGetRailPressure> dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_None;
|
|
|
|
|
|
|
|
// This shoudn't call getAbsoluteRailPressure, it should just return 1.0
|
|
|
|
EXPECT_FLOAT_EQ(1, dut.getInjectorFlowRatio());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InjectorModel, RailPressureFixed) {
|
|
|
|
InjectorModel dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
// Reference pressure is 350kpa
|
|
|
|
engineConfiguration->fuelReferencePressure = 350;
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_FixedRailPressure;
|
|
|
|
|
|
|
|
// Should be reference pressure + 1 atm
|
|
|
|
EXPECT_FLOAT_EQ(101.325f + 350.0f, dut.getAbsoluteRailPressure().value_or(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InjectorModel, RailPressureSensed) {
|
|
|
|
InjectorModel dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
// Reference pressure is 350kpa
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_SensedRailPressure;
|
|
|
|
|
|
|
|
// Should just return rail sensor value
|
|
|
|
Sensor::setMockValue(SensorType::FuelPressureInjector, 100);
|
|
|
|
EXPECT_FLOAT_EQ(100, dut.getAbsoluteRailPressure().value_or(-1));
|
|
|
|
Sensor::setMockValue(SensorType::FuelPressureInjector, 200);
|
|
|
|
EXPECT_FLOAT_EQ(200, dut.getAbsoluteRailPressure().value_or(-1));
|
|
|
|
Sensor::setMockValue(SensorType::FuelPressureInjector, 300);
|
|
|
|
EXPECT_FLOAT_EQ(300, dut.getAbsoluteRailPressure().value_or(-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InjectorModel, FailedPressureSensor) {
|
|
|
|
InjectorModel dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
// Reference pressure is 350kpa
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_SensedRailPressure;
|
|
|
|
|
|
|
|
// Sensor is broken!
|
2021-03-12 20:32:41 -08:00
|
|
|
// We have to register a broken sensor because the fuel pressure comp system
|
|
|
|
// has different logic for missing sensor
|
|
|
|
MockSensor ms(SensorType::FuelPressureInjector);
|
|
|
|
ms.invalidate();
|
|
|
|
ms.Register();
|
2020-11-10 20:11:22 -08:00
|
|
|
|
|
|
|
EXPECT_EQ(1.0f, dut.getInjectorFlowRatio());
|
|
|
|
}
|
2021-03-12 20:32:41 -08:00
|
|
|
|
|
|
|
TEST(InjectorModel, MissingPressureSensor) {
|
|
|
|
InjectorModel dut;
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2021-03-12 20:32:41 -08:00
|
|
|
|
|
|
|
// Reference pressure is 350kpa
|
|
|
|
engineConfiguration->injectorCompensationMode = ICM_SensedRailPressure;
|
|
|
|
|
|
|
|
// Sensor is missing!
|
|
|
|
Sensor::resetMockValue(SensorType::FuelPressureInjector);
|
|
|
|
|
|
|
|
// Missing sensor should trigger a fatal as it's a misconfiguration
|
|
|
|
EXPECT_FATAL_ERROR(dut.getInjectorFlowRatio());
|
|
|
|
}
|