| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Created by kifir on 7/31/24. | |||
| 3 | // | |||
| 4 | ||||
| 5 | #include "pch.h" | |||
| 6 | ||||
| 7 | #include "boost_test_base.h" | |||
| 8 | ||||
| 9 | namespace { | |||
| 10 | class ClosedLoopAddersTest : public BoostTestBase { | |||
| 11 | protected: | |||
| 12 | static constexpr float TEST_BOOST_CONTROL_TARGET = 14.0f; | |||
| 13 | static constexpr float TEST_CLT_BOOST_ADDER_BINS[BOOST_CURVE_SIZE] = { 4.8f, 9.7f, 19.6f, 39.5f, 79.4f }; | |||
| 14 | static constexpr float TEST_CLT_BOOST_ADDER[BOOST_CURVE_SIZE] = { 1.8f, 1.3f, 0.9f, 0.6f, 0.4f }; | |||
| 15 | static constexpr float TEST_IAT_BOOST_ADDER_BINS[BOOST_CURVE_SIZE] = { 2.7f, 7.5f, 17.4f, 37.3f, 77.2f }; | |||
| 16 | static constexpr float TEST_IAT_BOOST_ADDER[BOOST_CURVE_SIZE] = { 0.5f, 0.4f, 0.6f, 0.9f, 1.3f }; | |||
| 17 | ||||
| 18 | virtual void SetUp() override; | |||
| 19 | ||||
| 20 | void initTestCltBoostAdder(); | |||
| 21 | void initTestIatBoostAdder(); | |||
| 22 | void initLuaTargetCorrections(const float luaTargetMult, const int luaTargetAdd); | |||
| 23 | static std::optional<float> getTestCltBoostBin(const int index); | |||
| 24 | static std::optional<float> getTestIatBoostBin(const int index); | |||
| 25 | ||||
| 26 | void checkClosedLoopSetPoint( | |||
| 27 | std::function<std::optional<float>(int)> cltExtractorByIndex, | |||
| 28 | std::function<std::optional<float>(int)> iatExtractorByIndex, | |||
| 29 | std::function<float(int)> expectedClosedLoopExtractorByIndex | |||
| 30 | ); | |||
| 31 | ||||
| 32 | void checkClosedLoopSetPoint( | |||
| 33 | const std::optional<float> clt, | |||
| 34 | const std::optional<float> iat, | |||
| 35 | const float expectedClosedLoop | |||
| 36 | ); | |||
| 37 | }; | |||
| 38 | ||||
| 39 | 8 | void ClosedLoopAddersTest::SetUp() { | ||
| 40 | 8 | BoostTestBase::SetUp(); | ||
| 41 | ||||
| 42 | 8 | engineConfiguration->boostType = CLOSED_LOOP; | ||
| 43 | ||||
| 44 | 8 | setTable(config->boostTableClosedLoop, TEST_BOOST_CONTROL_TARGET); | ||
| 45 | ||||
| 46 | 8 | Sensor::setMockValue(SensorType::DriverThrottleIntent, 11.2f); | ||
| 47 | 8 | } | ||
| 48 | ||||
| 49 | 5 | void ClosedLoopAddersTest::initTestCltBoostAdder() { | ||
| 50 | 5 | initTestBoostCurve( | ||
| 51 | TEST_CLT_BOOST_ADDER_BINS, | |||
| 52 | 5 | config->cltBoostAdderBins, | ||
| 53 | TEST_CLT_BOOST_ADDER, | |||
| 54 | 5 | config->cltBoostAdder | ||
| 55 | ); | |||
| 56 | 5 | } | ||
| 57 | ||||
| 58 | 5 | void ClosedLoopAddersTest::initTestIatBoostAdder() { | ||
| 59 | 5 | initTestBoostCurve( | ||
| 60 | TEST_IAT_BOOST_ADDER_BINS, | |||
| 61 | 5 | config->iatBoostAdderBins, | ||
| 62 | TEST_IAT_BOOST_ADDER, | |||
| 63 | 5 | config->iatBoostAdder | ||
| 64 | ); | |||
| 65 | 5 | } | ||
| 66 | ||||
| 67 | 1 | void ClosedLoopAddersTest::initLuaTargetCorrections(const float luaTargetMult, const int luaTargetAdd) { | ||
| 68 | 1 | getBoostController().luaTargetMult = luaTargetMult; | ||
| 69 |
1/1✓ Branch 3 taken 1 time.
|
1 | getBoostController().luaTargetAdd = luaTargetAdd; | |
| 70 | 1 | } | ||
| 71 | ||||
| 72 | 25 | std::optional<float> ClosedLoopAddersTest::getTestCltBoostBin(const int index) { | ||
| 73 | 25 | return { TEST_CLT_BOOST_ADDER_BINS[index] }; | ||
| 74 | } | |||
| 75 | ||||
| 76 | 25 | std::optional<float> ClosedLoopAddersTest::getTestIatBoostBin(const int index) { | ||
| 77 | 25 | return { TEST_IAT_BOOST_ADDER_BINS[index] }; | ||
| 78 | } | |||
| 79 | ||||
| 80 | 6 | void ClosedLoopAddersTest::checkClosedLoopSetPoint( | ||
| 81 | std::function<std::optional<float>(int)> cltExtractorByIndex, | |||
| 82 | std::function<std::optional<float>(int)> iatExtractorByIndex, | |||
| 83 | std::function<float(int)> expectedClosedLoopExtractorByIndex | |||
| 84 | ) { | |||
| 85 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
|
2/2✓ Decision 'true' taken 30 times.
✓ Decision 'false' taken 6 times.
|
36 | for (int i = 0; i< BOOST_CURVE_SIZE; i++) { |
| 86 | 30 | checkClosedLoopSetPoint( | ||
| 87 | cltExtractorByIndex(i), | |||
| 88 | iatExtractorByIndex(i), | |||
| 89 | expectedClosedLoopExtractorByIndex(i) | |||
| 90 | ); | |||
| 91 | } | |||
| 92 | 6 | } | ||
| 93 | ||||
| 94 | 32 | void ClosedLoopAddersTest::checkClosedLoopSetPoint( | ||
| 95 | const std::optional<float> clt, | |||
| 96 | const std::optional<float> iat, | |||
| 97 | const float expectedSetPoint | |||
| 98 | ) { | |||
| 99 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 7 times.
|
2/2✓ Decision 'true' taken 25 times.
✓ Decision 'false' taken 7 times.
|
32 | if (clt.has_value()) { |
| 100 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | Sensor::setMockValue(SensorType::Clt, clt.value()); | |
| 101 | } | |||
| 102 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 7 times.
|
2/2✓ Decision 'true' taken 25 times.
✓ Decision 'false' taken 7 times.
|
32 | if (iat.has_value()) { |
| 103 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | Sensor::setMockValue(SensorType::Iat, iat.value()); | |
| 104 | } | |||
| 105 |
2/2✓ Branch 2 taken 32 times.
✓ Branch 5 taken 32 times.
|
32 | const expected<percent_t> setPoint = getBoostController().getSetpoint(); | |
| 106 |
1/10✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✗ Branch 25 not taken.
✗ Branch 30 not taken.
✗ Branch 34 not taken.
✗ Branch 37 not taken.
|
32 | EXPECT_TRUE(setPoint.Valid) << "clt: " << clt.value_or(-1) << ", iat: " << iat.value_or(-1) ; | |
| 107 |
2/6✓ Branch 2 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 9 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
|
32 | ASSERT_NEAR(setPoint.Value, expectedSetPoint, EPS5D) | |
| 108 |
1/6✗ Branch 1 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 16 not taken.
✓ Branch 24 taken 32 times.
✗ Branch 25 not taken.
|
32 | << "clt: " << clt.value_or(-1) << ", iat: " << iat.value_or(-1) ; | |
| 109 | } | |||
| 110 | ||||
| 111 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithDefaultCurves) { | ||
| 112 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 time.
|
2/2✓ Decision 'true' taken 5 times.
✓ Decision 'false' taken 1 time.
|
6 | for (int i = 0; i < BOOST_CURVE_SIZE; i++) { |
| 113 |
2/8✓ Branch 4 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 23 not taken.
✗ Branch 26 not taken.
|
5 | EXPECT_EQ(config->cltBoostAdder[i], 0.0f) << "index: " << i; // check default adder | |
| 114 |
2/8✓ Branch 4 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 23 not taken.
✗ Branch 26 not taken.
|
5 | EXPECT_EQ(config->iatBoostAdder[i], 0.0f) << "index: " << i; // check default adder | |
| 115 | } | |||
| 116 | ||||
| 117 |
1/1✓ Branch 5 taken 1 time.
|
1 | checkClosedLoopSetPoint({}, {}, TEST_BOOST_CONTROL_TARGET); | |
| 118 | 1 | } | ||
| 119 | ||||
| 120 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithUninitializedCurves) { | ||
| 121 | // Emulate configuration created with old version of TunerStudio, that doesn't support CLT and IAT boost curves: | |||
| 122 | 1 | setArrayValues(config->cltBoostAdder, 0.0f); | ||
| 123 | 1 | setArrayValues(config->cltBoostAdderBins, 0.0f); | ||
| 124 | 1 | setArrayValues(config->iatBoostAdder, 0.0f); | ||
| 125 | 1 | setArrayValues(config->iatBoostAdderBins, 0.0f); | ||
| 126 | ||||
| 127 |
1/1✓ Branch 5 taken 1 time.
|
1 | checkClosedLoopSetPoint({}, {}, TEST_BOOST_CONTROL_TARGET); | |
| 128 | 1 | } | ||
| 129 | ||||
| 130 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithCltAdder) { | ||
| 131 | 1 | initTestCltBoostAdder(); | ||
| 132 | ||||
| 133 |
1/1✓ Branch 8 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 134 | ClosedLoopAddersTest::getTestCltBoostBin, | |||
| 135 | ClosedLoopAddersTest::getTestIatBoostBin, | |||
| 136 | 1 | [](const int i) -> float { return TEST_BOOST_CONTROL_TARGET + TEST_CLT_BOOST_ADDER[i]; } | ||
| 137 | ); | |||
| 138 | 1 | } | ||
| 139 | ||||
| 140 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithIatAdder) { | ||
| 141 | 1 | initTestIatBoostAdder(); | ||
| 142 | ||||
| 143 |
1/1✓ Branch 8 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 144 | ClosedLoopAddersTest::getTestCltBoostBin, | |||
| 145 | ClosedLoopAddersTest::getTestIatBoostBin, | |||
| 146 | 1 | [](const int i) -> float { return TEST_BOOST_CONTROL_TARGET + TEST_IAT_BOOST_ADDER[i]; } | ||
| 147 | ); | |||
| 148 | 1 | } | ||
| 149 | ||||
| 150 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithBothCltAndIatAdders) { | ||
| 151 | 1 | initTestCltBoostAdder(); | ||
| 152 | 1 | initTestIatBoostAdder(); | ||
| 153 | ||||
| 154 |
1/1✓ Branch 8 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 155 | ClosedLoopAddersTest::getTestCltBoostBin, | |||
| 156 | ClosedLoopAddersTest::getTestIatBoostBin, | |||
| 157 | 1 | [](const int i) -> float { | ||
| 158 | return TEST_BOOST_CONTROL_TARGET + TEST_CLT_BOOST_ADDER[i] + TEST_IAT_BOOST_ADDER[i]; | |||
| 159 | } | |||
| 160 | ); | |||
| 161 | 1 | } | ||
| 162 | ||||
| 163 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithBothCltAndIatCorrectionWithMissedIatSensor) { | ||
| 164 | 1 | initTestCltBoostAdder(); | ||
| 165 | 1 | initTestIatBoostAdder(); | ||
| 166 | ||||
| 167 |
2/2✓ Branch 5 taken 1 time.
✓ Branch 10 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 168 | ClosedLoopAddersTest::getTestCltBoostBin, | |||
| 169 | emptyValue, | |||
| 170 | 1 | [](const int i) -> float { return TEST_BOOST_CONTROL_TARGET + TEST_CLT_BOOST_ADDER[i]; } | ||
| 171 | ); | |||
| 172 | 1 | } | ||
| 173 | ||||
| 174 | 4 | TEST_F(ClosedLoopAddersTest, closedLoopWithBothCltAndIatCorrectionWithMissedCltSensor) { | ||
| 175 | 1 | initTestCltBoostAdder(); | ||
| 176 | 1 | initTestIatBoostAdder(); | ||
| 177 | ||||
| 178 |
2/2✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 179 | emptyValue, | |||
| 180 | ClosedLoopAddersTest::getTestIatBoostBin, | |||
| 181 | 1 | [](const int i) -> float { return TEST_BOOST_CONTROL_TARGET + TEST_IAT_BOOST_ADDER[i]; } | ||
| 182 | ); | |||
| 183 | 1 | } | ||
| 184 | ||||
| 185 | 4 | TEST_F(ClosedLoopAddersTest, closeLoopWithBothCltAndIatCorrectionAndLuaCorrections) { | ||
| 186 | 1 | initTestCltBoostAdder(); | ||
| 187 | 1 | initTestIatBoostAdder(); | ||
| 188 | ||||
| 189 | 1 | constexpr float TEST_LUA_TARGET_MULT = 1.2f; | ||
| 190 | 1 | constexpr int TEST_LUA_TARGET_ADD = 3; | ||
| 191 | 1 | initLuaTargetCorrections(TEST_LUA_TARGET_MULT, TEST_LUA_TARGET_ADD); | ||
| 192 | ||||
| 193 |
1/1✓ Branch 8 taken 1 time.
|
2 | checkClosedLoopSetPoint( | |
| 194 | getTestCltBoostBin, | |||
| 195 | getTestIatBoostBin, | |||
| 196 | 1 | [](const int i) -> float { | ||
| 197 | return TEST_BOOST_CONTROL_TARGET * TEST_LUA_TARGET_MULT + static_cast<float>(TEST_LUA_TARGET_ADD) | |||
| 198 | + TEST_CLT_BOOST_ADDER[i] + TEST_IAT_BOOST_ADDER[i]; | |||
| 199 | } | |||
| 200 | ); | |||
| 201 | 1 | } | ||
| 202 | } | |||
| 203 |