forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_limp.cpp
More file actions
463 lines (351 loc) · 13.9 KB
/
Copy pathtest_limp.cpp
File metadata and controls
463 lines (351 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "pch.h"
#include "fuel_math.h"
#include "limp_manager.h"
TEST(limp, testFatalError) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
LimpManager dut;
// Everything should work by default
ASSERT_TRUE(dut.allowElectronicThrottle());
ASSERT_TRUE(dut.allowIgnition());
ASSERT_TRUE(dut.allowInjection());
ASSERT_TRUE(dut.allowTriggerInput());
dut.fatalError();
// Fatal error should kill everything
EXPECT_FALSE(dut.allowElectronicThrottle());
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
EXPECT_FALSE(dut.allowTriggerInput());
}
TEST(limp, revLimit) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->rpmHardLimit = 2500;
engineConfiguration->cutFuelOnHardLimit = true;
engineConfiguration->cutSparkOnHardLimit = true;
LimpManager dut;
// Under rev limit, inj/ign allowed
dut.updateState(2000, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
// Over rev limit, no injection
dut.updateState(3000, 0);
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
// Now recover back to under limit
dut.updateState(2000, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
}
TEST(limp, revLimitCltBased) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->rpmHardLimit = 2500;
engineConfiguration->cutFuelOnHardLimit = true;
engineConfiguration->cutSparkOnHardLimit = true;
// Configure CLT-based rev limit curve
engineConfiguration->useCltBasedRpmLimit = true;
copyArray(config->cltRevLimitRpmBins, { 10, 20, 30, 40 });
copyArray(config->cltRevLimitRpm, { 1000, 2000, 3000, 4000 });
LimpManager dut;
// Check low temperature first
Sensor::setMockValue(SensorType::Clt, 10);
// Under rev limit, inj/ign allowed
dut.updateState(900, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
// Over rev limit, no injection
dut.updateState(1100, 0);
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
// Now recover back to under limit
dut.updateState(900, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
// Check middle temperature
Sensor::setMockValue(SensorType::Clt, 35);
// Under rev limit, inj/ign allowed
dut.updateState(3400, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
// Over rev limit, no injection
dut.updateState(3600, 0);
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
// Now recover back to under limit
dut.updateState(3400, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
}
TEST(limp, revHardLimitHyst) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->rpmHardLimit = 2500;
engineConfiguration->rpmHardLimitHyst = 200;
engineConfiguration->cutSparkOnHardLimit = true;
LimpManager dut;
// Under rev limit, inj/ign allowed
dut.updateState(2500, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
// Over rev limit, no injection or ignition
dut.updateState(2501, 0);
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
// Now set back inside the limit window - still not allowed
dut.updateState(2300, 0);
EXPECT_FALSE(dut.allowIgnition());
EXPECT_FALSE(dut.allowInjection());
// Now recover back to under lower limit
dut.updateState(2299, 0);
EXPECT_TRUE(dut.allowIgnition());
EXPECT_TRUE(dut.allowInjection());
}
TEST(limp, revSoftLimit) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->rpmHardLimit = 2500;
engineConfiguration->rpmHardLimitHyst = 200;
engineConfiguration->rpmSoftLimitTimingRetard = 10; // 10 deg
engineConfiguration->rpmSoftLimitFuelAdded = 20; // 20%
engineConfiguration->cutSparkOnHardLimit = true;
eth.engine.updateSlowSensors();
Sensor::setMockValue(SensorType::Clt, 36.605f);
Sensor::setMockValue(SensorType::Iat, 30.0f);
// this is 5ms base fuel with some default CLT/IAT corrections
static const float baseFuel = 5.0f;
static const float normalRunningFuel = 5.3679f;
// Under rev limit, no inj/ign corrections
Sensor::setMockValue(SensorType::Rpm, 2300);
eth.engine.periodicFastCallback();
EXPECT_FLOAT_EQ(0, getLimpManager()->getLimitingTimingRetard());
EXPECT_FLOAT_EQ((float)eth.engine.ignitionState.correctedIgnitionAdvance - (float)eth.engine.ignitionState.baseIgnitionAdvance, 0);
EXPECT_FLOAT_EQ(1, getLimpManager()->getLimitingFuelCorrection());
// this is normal injection mode, no limiting fuel corrections
ASSERT_NEAR(normalRunningFuel, getRunningFuel(baseFuel), EPS4D) << "base fuel";
// For upper rev limit, we expect maximum inj/ign corrections
Sensor::setMockValue(SensorType::Rpm, 2500);
eth.engine.periodicFastCallback();
EXPECT_FLOAT_EQ(10, getLimpManager()->getLimitingTimingRetard()); // 10 deg
EXPECT_FLOAT_EQ((float)eth.engine.ignitionState.correctedIgnitionAdvance - (float)eth.engine.ignitionState.baseIgnitionAdvance, -10.0f);
EXPECT_FLOAT_EQ(1.2f, getLimpManager()->getLimitingFuelCorrection()); // 20%
ASSERT_NEAR(normalRunningFuel * 1.2f, getRunningFuel(baseFuel), EPS4D) << "base fuel"; // 20%
// In the middle of the limit window, we expect 50% interpolated inj/ign corrections
Sensor::setMockValue(SensorType::Rpm, 2400);
eth.engine.periodicFastCallback();
EXPECT_FLOAT_EQ(5, getLimpManager()->getLimitingTimingRetard()); // 5 deg
EXPECT_FLOAT_EQ((float)eth.engine.ignitionState.correctedIgnitionAdvance - (float)eth.engine.ignitionState.baseIgnitionAdvance, -5.0f);
EXPECT_FLOAT_EQ(1.1f, getLimpManager()->getLimitingFuelCorrection()); // 10%
ASSERT_NEAR(normalRunningFuel * 1.1f, getRunningFuel(baseFuel), EPS4D) << "base fuel"; // 10%
}
TEST(limp, boostCut) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
// Cut above 100kPa
engineConfiguration->boostCutPressure = 100;
engineConfiguration->boostCutPressureHyst = 20;
LimpManager dut;
// Below threshold, injection allowed
Sensor::setMockValue(SensorType::Map, 80);
dut.updateState(1000, 0);
EXPECT_TRUE(dut.allowInjection());
// Above rising threshold, injection cut
Sensor::setMockValue(SensorType::Map, 105);
dut.updateState(1000, 0);
EXPECT_FALSE(dut.allowInjection());
// Below rising threshold, but should have hysteresis, so not cut yet
Sensor::setMockValue(SensorType::Map, 95);
dut.updateState(1000, 0);
EXPECT_FALSE(dut.allowInjection());
// Below falling threshold, fuel restored
Sensor::setMockValue(SensorType::Map, 79);
dut.updateState(1000, 0);
EXPECT_TRUE(dut.allowInjection());
// SPECIAL CASE: threshold of 0 means never boost cut
engineConfiguration->boostCutPressure = 0;
Sensor::setMockValue(SensorType::Map, 500);
dut.updateState(1000, 0);
EXPECT_TRUE(dut.allowInjection());
}
TEST(limp, boostCutUint8Overflow) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
// Cut above 1500kPa
engineConfiguration->boostCutPressure = 1500;
engineConfiguration->boostCutPressureHyst = 20;
LimpManager dut;
// Below threshold, injection allowed
Sensor::setMockValue(SensorType::Map, 80);
dut.updateState(1000, 0);
EXPECT_TRUE(dut.allowInjection());
// Above rising threshold, injection cut
Sensor::setMockValue(SensorType::Map, 1600);
dut.updateState(1000, 0);
EXPECT_FALSE(dut.allowInjection());
// Below rising threshold, but should have hysteresis, so not cut yet
Sensor::setMockValue(SensorType::Map, 1495);
dut.updateState(1000, 0);
EXPECT_FALSE(dut.allowInjection());
// Below falling threshold, fuel restored
Sensor::setMockValue(SensorType::Map, 79);
dut.updateState(1000, 0);
EXPECT_TRUE(dut.allowInjection());
}
TEST(limp, oilPressureStartupFailureCase) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->minOilPressureAfterStart = 200;
LimpManager dut;
// Low oil pressure!
Sensor::setMockValue(SensorType::OilPressure, 50);
// Start the engine
engine->rpmCalculator.setRpmValue(1000);
// update & check: injection should be allowed
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// 4.5 seconds later, should still be allowed (even though pressure is low)
advanceTimeUs(4.5e6);
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// 1 second later (5.5 since start), injection should cut
advanceTimeUs(1.0e6);
dut.updateState(1000, getTimeNowNt());
ASSERT_FALSE(dut.allowInjection());
// But then oil pressure arrives!
// Injection still isn't allowed, since now we're late.
Sensor::setMockValue(SensorType::OilPressure, 250);
dut.updateState(1000, getTimeNowNt());
ASSERT_FALSE(dut.allowInjection());
}
TEST(limp, oilPressureStartupEnableProtect) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
// Configure "After Start" protection
engineConfiguration->minOilPressureAfterStart = 200;
// explicitly set it to false.
engineConfiguration->enableOilPressureProtect = false;
LimpManager dut;
// Low oil pressure!
Sensor::setMockValue(SensorType::OilPressure, 50);
// Start the engine
engine->rpmCalculator.setRpmValue(1000);
// update & check: injection should be allowed
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
advanceTimeUs(5.5e6);
dut.updateState(1000, getTimeNowNt());
// It SHOULD stay TRUE because enableOilPressureProtect is false.
EXPECT_FALSE(dut.allowInjection());
}
TEST(limp, oilPressureStartupSuccessCase) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->enableOilPressureProtect = true;
engineConfiguration->minOilPressureAfterStart = 200;
LimpManager dut;
// Low oil pressure!
Sensor::setMockValue(SensorType::OilPressure, 50);
// Start the engine
engine->rpmCalculator.setRpmValue(1000);
// update & check: injection should be allowed
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// 4.5 seconds later, should still be allowed (even though pressure is low)
advanceTimeUs(4.5e6);
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// But then oil pressure arrives!
Sensor::setMockValue(SensorType::OilPressure, 250);
dut.updateState(1000, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
// 1 second later (5.5 since start), injection should be allowed since we saw pressure before the timeout
advanceTimeUs(1.0e6);
dut.updateState(1000, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
// Later, we lose oil pressure, but engine should stay running
advanceTimeUs(10e6);
Sensor::setMockValue(SensorType::OilPressure, 10);
dut.updateState(1000, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
}
TEST(limp, oilPressureRunning) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->enableOilPressureProtect = true;
engineConfiguration->minimumOilPressureTimeout = 1.0f;
setArrayValues(config->minimumOilPressureValues, 100);
LimpManager dut;
// Oil pressure starts OK
Sensor::setMockValue(SensorType::OilPressure, 110);
// Start the engine
engine->rpmCalculator.setRpmValue(1000);
// update & check: injection should be allowed
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// A long time later, everything should still be OK
advanceTimeUs(60e6);
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// Now oil pressure drops below threshold
Sensor::setMockValue(SensorType::OilPressure, 90);
// 0.9 second later, injection should continue as timeout isn't hit yet
advanceTimeUs(0.9e6);
dut.updateState(1000, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
// 0.2 second later (1.1s since low pressure starts), injection should cut
advanceTimeUs(1.0e6);
dut.updateState(1000, getTimeNowNt());
ASSERT_FALSE(dut.allowInjection());
// Oil pressure is restored, and fuel should be restored too
Sensor::setMockValue(SensorType::OilPressure, 110);
dut.updateState(1000, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
}
TEST(limp, oilPressureMaxLimit) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
config->maximumOilPressureValues[0] = 200; // kPa
config->maximumOilPressureValues[1] = 200;
config->maximumOilPressureValues[2] = 800;
config->maximumOilPressureValues[3] = 800;
setRpmTableBin(config->maximumOilPressureBins); // 700 800 3800 7000
engineConfiguration->maxOilPressureTimeout = 1.0f; // sec
LimpManager dut;
// Oil pressure starts OK
Sensor::setMockValue(SensorType::OilPressure, 300);
// Start the engine
float rpm = 4000;
engine->rpmCalculator.setRpmValue(rpm);
// update & check: injection should be allowed
dut.updateState(rpm, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// after 5 sec - still allowed
advanceTimeUs(5.0e6);
dut.updateState(rpm, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
// On lower RPMs oil pressure goes above threshold
rpm = 800;
engine->rpmCalculator.setRpmValue(rpm);
// 0.9 second later, injection should continue as timeout isn't hit yet
advanceTimeUs(0.9e6);
dut.updateState(rpm, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
// 0.2 second later (1.1s since non-valid pressure), injection should cut
advanceTimeUs(1.0e6);
dut.updateState(rpm, getTimeNowNt());
ASSERT_FALSE(dut.allowInjection());
// Oil pressure is restored, and fuel should be restored too
Sensor::setMockValue(SensorType::OilPressure, 110);
dut.updateState(rpm, getTimeNowNt());
ASSERT_TRUE(dut.allowInjection());
}
TEST(limp, gdiFuelCut) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
// configure GDI engine for tests:
engineConfiguration->hpfpCam = HPFP_CAM_NONE;
engineConfiguration->hpfpCamLobes = 4;
engineConfiguration->mc33_t_hold_tot = 3.0f;
engine->rpmCalculator.setRpmValue(1000);
// below limits:
engine->engineState.injectionDuration = 1.8;
LimpManager dut;
// update & check: injection should be allowed
dut.updateState(1000, getTimeNowNt());
EXPECT_TRUE(dut.allowInjection());
engine->engineState.injectionDuration = 3.1f;
// update & check: injection should cut
dut.updateState(1000, getTimeNowNt());
ASSERT_EQ(ClearReason::GdiLimits, dut.allowInjection().reason);
}
struct Mockhpfp : public MockHpfpController {
bool isHpfpActive;
angle_t m_deadangle;
};