Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | /** | |||
2 | * @file test_trigger_input_adc.cpp | |||
3 | * | |||
4 | * @date Jul 24, 2021 | |||
5 | * @author andreika <prometheus.pcb@gmail.com> | |||
6 | * @author Andrey Belomutskiy, (c) 2012-2023 | |||
7 | */ | |||
8 | ||||
9 | #include "pch.h" | |||
10 | #include "engine_test_helper.h" | |||
11 | #include "trigger_decoder.h" | |||
12 | #include "engine_math.h" | |||
13 | #include "allsensors.h" | |||
14 | #include "rpm_calculator.h" | |||
15 | #include "event_queue.h" | |||
16 | #include "trigger_central.h" | |||
17 | #include "main_trigger_callback.h" | |||
18 | #include "engine.h" | |||
19 | #include "advance_map.h" | |||
20 | #include "speed_density.h" | |||
21 | #include "fuel_math.h" | |||
22 | #include "spark_logic.h" | |||
23 | #include "trigger_universal.h" | |||
24 | ||||
25 | #include "trigger_input_adc.h" | |||
26 | #include "logicdata_csv_reader.h" | |||
27 | ||||
28 | ||||
29 | extern TriggerAdcDetector trigAdcState; | |||
30 | ||||
31 | static int triggerChangedRisingCnt = 0, triggerChangedFallingCnt = 0; | |||
32 | ||||
33 | ||||
34 | 1 | void setTriggerAdcMode(triggerAdcMode_t adcMode) { | ||
35 | 1 | trigAdcState.curAdcMode = adcMode; | ||
36 | 1 | } | ||
37 | ||||
38 | 1848 | void onTriggerChanged(efitick_t stamp, bool isPrimary, bool isRising) { | ||
39 |
2/2✓ Branch 0 taken 924 times.
✓ Branch 1 taken 924 times.
|
2/2✓ Decision 'true' taken 924 times.
✓ Decision 'false' taken 924 times.
|
1848 | if (isRising) |
40 | 924 | triggerChangedRisingCnt++; | ||
41 | else | |||
42 | 924 | triggerChangedFallingCnt++; | ||
43 | ||||
44 |
1/2✓ Branch 0 taken 1848 times.
✗ Branch 1 not taken.
|
1848 | hwHandleShaftSignal(isPrimary ? 0 : 1, isRising, stamp); | |
45 | 1848 | } | ||
46 | ||||
47 | 1 | static void simulateTrigger(EngineTestHelper ð, TriggerAdcDetector &trigAdcState, CsvReader &reader, float voltageDiv, float adcMaxVoltage) { | ||
48 |
2/4✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
|
1 | static const float Vil = 0.3f * adcMaxVoltage; | |
49 |
2/4✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
|
1 | static const float Vih = 0.7f * adcMaxVoltage; | |
50 | ||||
51 | 1 | efitimeus_t startUs = getTimeNowUs(); | ||
52 | ||||
53 | 1 | int prevLogicValue = -1; | ||
54 |
2/2✓ Branch 1 taken 60000 times.
✓ Branch 2 taken 1 time.
|
2/2✓ Decision 'true' taken 60000 times.
✓ Decision 'false' taken 1 time.
|
60001 | while (reader.haveMore()) { |
55 | 60000 | double value = 0; | ||
56 |
1/1✓ Branch 1 taken 60000 times.
|
60000 | double stamp = reader.readTimestampAndValues(&value); | |
57 | 60000 | efitimeus_t stampUs = (efitick_t)(stamp * 1'000'000) + startUs; | ||
58 |
1/1✓ Branch 1 taken 60000 times.
|
60000 | eth.setTimeAndInvokeEventsUs(stampUs); | |
59 | 60000 | efitick_t stampNt = US2NT(stampUs); | ||
60 | ||||
61 | // convert into mcu-adc voltage | |||
62 |
2/2✓ Branch 1 taken 60000 times.
✓ Branch 4 taken 60000 times.
|
60000 | value = minF(maxF(value / voltageDiv, 0), adcMaxVoltage); | |
63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60000 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 60000 times.
|
60000 | if (trigAdcState.curAdcMode == TRIGGER_ADC_EXTI) { |
64 | ✗ | int logicValue = 0; | ||
65 | // imitate Schmitt trigger input | |||
66 | ✗ | if (value < Vil || value > Vih) { | ||
67 | ✗ | logicValue = value > Vih; | ||
68 | // we need at least two values to detect an edge | |||
69 | ✗ | if (prevLogicValue != -1) { | ||
70 | // printf("--> DIGITAL %d %d\r\n", logicValue, prevLogicValue); | |||
71 | ||||
72 | ✗ | trigAdcState.digitalCallback(stampNt, true, logicValue > prevLogicValue ? true : false); | ||
73 | } | |||
74 | ✗ | prevLogicValue = logicValue; | ||
75 | } | |||
76 |
1/2✓ Branch 0 taken 60000 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 60000 times.
✗ Decision 'false' not taken.
|
60000 | } else if (trigAdcState.curAdcMode == TRIGGER_ADC_ADC) { |
77 | 60000 | triggerAdcSample_t sampleValue = value * ADC_MAX_VALUE / adcMaxVoltage; | ||
78 | ||||
79 | // printf("--> ANALOG %d\r\n", sampleValue); | |||
80 | ||||
81 |
1/1✓ Branch 1 taken 60000 times.
|
60000 | trigAdcState.analogCallback(stampNt, sampleValue); | |
82 | } | |||
83 | } | |||
84 | 1 | } | ||
85 | ||||
86 | 1 | static void testOnCsvData(const char *fileName, int finalRpm, int risingCnt, int fallingCnt, uint32_t errCnt) { | ||
87 |
1/1✓ Branch 2 taken 1 time.
|
1 | EngineTestHelper eth(engine_type_e::TEST_ENGINE); | |
88 | ||||
89 | 1 | engineConfiguration->ignitionMode = IM_WASTED_SPARK; | ||
90 | ||||
91 | 1 | engineConfiguration->adcVcc = 3.3f; | ||
92 | 1 | engineConfiguration->analogInputDividerCoefficient = 2.0f; | ||
93 | ||||
94 | // we'll test on 60-2 wheel | |||
95 |
1/1✓ Branch 1 taken 1 time.
|
1 | eth.setTriggerType(trigger_type_e::TT_TOOTHED_WHEEL_60_2); | |
96 | ||||
97 | // we generate the data that way | |||
98 | 1 | engineConfiguration->invertPrimaryTriggerSignal = true; | ||
99 | ||||
100 |
3/8✓ Branch 3 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 11 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✓ Branch 26 taken 1 time.
✗ Branch 27 not taken.
|
1 | ASSERT_EQ(0u, engine->triggerCentral.triggerState.totalTriggerErrorCounter); | |
101 |
4/11✓ Branch 3 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 27 not taken.
✗ Branch 30 not taken.
✓ Branch 37 taken 1 time.
✗ Branch 38 not taken.
|
1 | ASSERT_EQ(0, Sensor::getOrZero(SensorType::Rpm)) << "testTriggerInputAdc RPM #1 on " << fileName; | |
102 | ||||
103 |
1/1✓ Branch 1 taken 1 time.
|
1 | trigAdcState.init(); | |
104 | ||||
105 | // disable weak signal detector for this test | |||
106 |
1/1✓ Branch 1 taken 1 time.
|
1 | trigAdcState.setWeakSignal(false); | |
107 | ||||
108 |
1/1✓ Branch 1 taken 1 time.
|
1 | setTriggerAdcMode(TRIGGER_ADC_ADC); | |
109 | ||||
110 | // reset counters | |||
111 | 1 | triggerChangedRisingCnt = 0; triggerChangedFallingCnt = 0; | ||
112 | ||||
113 | // skip some time to avoid conflicts with ADC sampling time correction | |||
114 |
1/1✓ Branch 1 taken 1 time.
|
1 | eth.moveTimeForwardUs(NT2US(trigAdcState.stampCorrectionForAdc)); | |
115 | ||||
116 |
1/1✓ Branch 2 taken 1 time.
|
1 | CsvReader reader(1, 0); | |
117 | ||||
118 |
1/1✓ Branch 1 taken 1 time.
|
1 | reader.open(fileName); | |
119 |
1/1✓ Branch 1 taken 1 time.
|
1 | simulateTrigger(eth, trigAdcState, reader, 2.0f, 3.3f); | |
120 | ||||
121 |
3/8✓ Branch 2 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
|
1 | ASSERT_EQ(errCnt, engine->triggerCentral.triggerState.totalTriggerErrorCounter); | |
122 |
3/8✓ Branch 2 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
|
1 | ASSERT_EQ(risingCnt, triggerChangedRisingCnt); | |
123 |
3/8✓ Branch 2 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
|
1 | ASSERT_EQ(fallingCnt, triggerChangedFallingCnt); | |
124 |
4/11✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 time.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 23 not taken.
✗ Branch 26 not taken.
✓ Branch 33 taken 1 time.
✗ Branch 34 not taken.
|
1 | ASSERT_NEAR(finalRpm, Sensor::getOrZero(SensorType::Rpm), 0.5f) << "testTriggerInputAdc RPM #2 on " << fileName; | |
125 | 1 | } | ||
126 | ||||
127 | /* | |||
128 | TEST(big, testTriggerInputAdc1) { | |||
129 | printf("====================================================================================== testTriggerInputAdc 1\r\n"); | |||
130 | ||||
131 | testOnCsvData("tests/trigger/resources/trigger_adc_1.csv", 1524, 74, 73, 0); | |||
132 | } | |||
133 | ||||
134 | ||||
135 | ||||
136 | TEST(big, testTriggerInputAdc750) { | |||
137 | printf("====================================================================================== testTriggerInputAdc 750\r\n"); | |||
138 | ||||
139 | testOnCsvData("tests/trigger/resources/trigger_adc_750.csv", 750, 144, 144, 0); | |||
140 | } | |||
141 | ||||
142 | ||||
143 | ||||
144 | TEST(big, testTriggerInputAdc1000) { | |||
145 | printf("====================================================================================== testTriggerInputAdc 1000\r\n"); | |||
146 | ||||
147 | testOnCsvData("tests/trigger/resources/trigger_adc_1000.csv", 1000, 194, 194, 0); | |||
148 | } | |||
149 | ||||
150 | TEST(big, testTriggerInputAdcIncrDecr) { | |||
151 | printf("====================================================================================== testTriggerInputAdc 1000\r\n"); | |||
152 | ||||
153 | testOnCsvData("tests/trigger/resources/trigger_adc_incr.csv", 1330, 155, 154, 0); | |||
154 | testOnCsvData("tests/trigger/resources/trigger_adc_decr.csv", 419, 142, 141, 0); | |||
155 | } | |||
156 | */ | |||
157 | ||||
158 | 4 | TEST(big, testTriggerInputAdcReal) { | ||
159 | 1 | printf("====================================================================================== testTriggerInputAdc 1000\r\n"); | ||
160 | ||||
161 | // constant low RPM | |||
162 | 1 | testOnCsvData("tests/trigger/resources/trigger_adc_real1.csv", 322, 924, 924, 2); | ||
163 | // accelerate and switch from analog to digital mode | |||
164 | //testOnCsvData("tests/trigger/resources/trigger_adc_real2.csv", 1283, 2398, 2398, 29); | |||
165 | 1 | } | ||
166 | ||||
167 |