forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_knock.cpp
More file actions
198 lines (145 loc) · 5.23 KB
/
Copy pathtest_knock.cpp
File metadata and controls
198 lines (145 loc) · 5.23 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
#include "pch.h"
#include "knock_logic.h"
struct MockKnockController : public KnockControllerBase {
float getKnockThreshold() const override {
// Knock threshold of 20dBv
return 20;
}
float getMaximumRetard() const override {
// Maximum 8 degrees retarded
return 8;
}
};
TEST(Knock, frequencyApproximation) {
// that's first harmonic while default is knockDetectionUseDoubleFrequency for second harmonic
ASSERT_NEAR(7.3456, bore2frequency(78/*mm*/), EPS2D);
}
TEST(Knock, Retards) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
// Aggression of 10%
engineConfiguration->knockRetardAggression = 10;
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
setArrayValues(engine->engineState.timingAdvance, 0.0f);
MockKnockController dut;
dut.onFastCallback();
// No retard unless we knock
ASSERT_FLOAT_EQ(dut.getKnockRetard(), 0);
// Need to call onFastCallback once to update thresholds from the Mock
dut.onFastCallback();
// Send some weak knocks, should yield no response
for (size_t i = 0; i < 10; i++) {
dut.onKnockSenseCompleted(0, 10, 0);
}
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 0);
// Send a strong knock!
dut.onKnockSenseCompleted(0, 30, 0);
// Should retard 10% of the distance between current timing (0) and "minimum" (-20)
// dist = 0 - (-20) = 20. 10% of 20 = 2.
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 2);
// Send tons of strong knocks, make sure we don't go over the configured limit
for (size_t i = 0; i < 100; i++) {
dut.onKnockSenseCompleted(0, 30, 0);
}
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 8);
}
TEST(Knock, Reapply) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
setArrayValues(engine->engineState.timingAdvance, 0.0f);
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
MockKnockController dut;
dut.onFastCallback();
// Aggression of 10%
engineConfiguration->knockRetardAggression = 10;
// Apply 1 degree/second
engineConfiguration->knockRetardReapplyRate = 1;
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
// Send a strong knock!
dut.onKnockSenseCompleted(0, 30, 0);
// Should retard 10% of the distance between current timing (0) and "minimum" (-20)
// dist = 0 - (-20) = 20. 10% of 20 = 2.
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 2);
constexpr auto fastPeriodSec = FAST_CALLBACK_PERIOD_MS / 1000.0f;
// call the fast callback, should reapply 1 degree * callback period
dut.onFastCallback();
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 2 - 1.0f * fastPeriodSec);
// 10 updates total
for (size_t i = 0; i < 9; i++) {
dut.onFastCallback();
}
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 2 - 10 * 1.0f * fastPeriodSec);
// Spend a long time without knock
for (size_t i = 0; i < 1000; i++) {
dut.onFastCallback();
}
// Should have no knock retard
EXPECT_FLOAT_EQ(dut.getKnockRetard(), 0);
}
TEST(Knock, FuelTrim) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
setArrayValues(engine->engineState.timingAdvance, 0.0f);
// Aggression of 10%
engineConfiguration->knockFuelTrimAggression = 10;
engineConfiguration->knockFuelTrim = 30;
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
MockKnockController dut;
dut.onFastCallback();
// No trim unless we knock
ASSERT_FLOAT_EQ(dut.getFuelTrimMultiplier(), 1.0);
// Need to call onFastCallback once to update thresholds from the Mock
dut.onFastCallback();
// Send some weak knocks, should yield no response
for (size_t i = 0; i < 10; i++) {
dut.onKnockSenseCompleted(0, 10, 0);
}
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), 1.0);
// Send a strong knock!
dut.onKnockSenseCompleted(0, 30, 0);
// 30% * 10% = 3% = 0.03
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), 1.03);
// Send tons of strong knocks, make sure we don't go over the configured limit
for (size_t i = 0; i < 100; i++) {
dut.onKnockSenseCompleted(0, 30, 0);
}
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), 1.3);
}
TEST(Knock, FuelTrimReapply) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
setArrayValues(engine->engineState.timingAdvance, 0.0f);
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
MockKnockController dut;
dut.onFastCallback();
// Aggression of 100%
engineConfiguration->knockFuelTrimAggression = 10;
// Apply 1%/second
engineConfiguration->knockFuelTrimReapplyRate = 1;
// fuel trim 30%
engineConfiguration->knockFuelTrim = 30;
// disable suppress for test
engineConfiguration->knockSuppressMinTps = 0;
// Send a strong knock!
dut.onKnockSenseCompleted(0, 30, 0);
// 100% trim
float trim = 1.03;
// Should retard 100% of the distance between current timing and "maximum"
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), trim);
constexpr auto fastPeriodSec = FAST_CALLBACK_PERIOD_MS / 1000.0f;
// call the fast callback, should reapply 1% trim * callback period
dut.onFastCallback();
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), trim - 0.01f * fastPeriodSec);
// 10 updates total
for (size_t i = 0; i < 9; i++) {
dut.onFastCallback();
}
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), trim - 10 * 0.01f * fastPeriodSec);
// Spend a long time without knock
for (size_t i = 0; i < 2000; i++) {
dut.onFastCallback();
}
// Should have no knock retard
EXPECT_FLOAT_EQ(dut.getFuelTrimMultiplier(), 1.0);
}