rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
firing_order.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3static const uint8_t order_1[] = {1};
4
5static const uint8_t order_1_2[] = {1, 2};
6
7static const uint8_t order_1_2_3[] = {1, 2, 3};
8static const uint8_t order_1_3_2[] = {1, 3, 2};
9// 4 cylinder
10
11static const uint8_t order_1_THEN_3_THEN_4_THEN2[] = { 1, 3, 4, 2 };
12static const uint8_t order_1_THEN_2_THEN_4_THEN3[] = { 1, 2, 4, 3 };
13static const uint8_t order_1_THEN_3_THEN_2_THEN4[] = { 1, 3, 2, 4 };
14static const uint8_t order_1_THEN_4_THEN_3_THEN2[] = { 1, 4, 3, 2 };
15
16// 5 cylinder
17static const uint8_t order_1_2_4_5_3[] = {1, 2, 4, 5, 3};
18
19// 6 cylinder
20static const uint8_t order_1_THEN_5_THEN_3_THEN_6_THEN_2_THEN_4[] = { 1, 5, 3, 6, 2, 4 };
21static const uint8_t order_1_THEN_4_THEN_2_THEN_5_THEN_3_THEN_6[] = { 1, 4, 2, 5, 3, 6 };
22static const uint8_t order_1_THEN_2_THEN_3_THEN_4_THEN_5_THEN_6[] = { 1, 2, 3, 4, 5, 6 };
23static const uint8_t order_1_6_3_2_5_4[] = {1, 6, 3, 2, 5, 4};
24static const uint8_t order_1_4_3_6_2_5[] = {1, 4, 3, 6, 2, 5};
25static const uint8_t order_1_6_2_4_3_5[] = {1, 6, 2, 4, 3, 5};
26static const uint8_t order_1_6_5_4_3_2[] = {1, 6, 5, 4, 3, 2};
27static const uint8_t order_1_4_5_2_3_6[] = {1, 4, 5, 2, 3, 6};
28
29// 8 cylinder
30static const uint8_t order_1_8_4_3_6_5_7_2[] = { 1, 8, 4, 3, 6, 5, 7, 2 };
31static const uint8_t order_1_8_7_2_6_5_4_3[] = { 1, 8, 7, 2, 6, 5, 4, 3 };
32static const uint8_t order_1_5_4_2_6_3_7_8[] = { 1, 5, 4, 2, 6, 3, 7, 8 };
33static const uint8_t order_1_2_7_8_4_5_6_3[] = { 1, 2, 7, 8, 4, 5, 6, 3 };
34static const uint8_t order_1_3_7_2_6_5_4_8[] = { 1, 3, 7, 2, 6, 5, 4, 8 };
35static const uint8_t order_1_2_3_4_5_6_7_8[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
36static const uint8_t order_1_5_4_8_6_3_7_2[] = { 1, 5, 4, 8, 6, 3, 7, 2 };
37static const uint8_t order_1_8_7_3_6_5_4_2[] = { 1, 8, 7, 3, 6, 5, 4, 2 };
38static const uint8_t order_1_5_4_8_3_7_2_6[] = { 1, 5, 4, 8, 3, 7, 2, 6 };
39static const uint8_t order_1_8_6_2_7_3_4_5[] = { 1, 8, 6, 2, 7, 3, 4, 5 };
40
41// 9 cylinder
42static const uint8_t order_1_2_3_4_5_6_7_8_9[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
43
44// 10 cylinder
45static const uint8_t order_1_10_9_4_3_6_5_8_7_2[] = {1, 10, 9, 4, 3, 6, 5, 8, 7, 2};
46static const uint8_t order_1_6_5_10_2_7_3_8_4_9[] = {1, 6, 5, 10, 2, 7, 3, 8, 4, 9};
47
48// 12 cyliner
49static const uint8_t order_1_7_5_11_3_9_6_12_2_8_4_10[] = {1, 7, 5, 11, 3, 9, 6, 12, 2, 8, 4, 10};
50static const uint8_t order_1_7_4_10_2_8_6_12_3_9_5_11[] = {1, 7, 4, 10, 2, 8, 6, 12, 3, 9, 5, 11};
51static const uint8_t order_1_12_5_8_3_10_6_7_2_11_4_9[] = {1, 12, 5, 8, 3, 10, 6, 7, 2, 11, 4, 9};
52static const uint8_t order_1_2_3_4_5_6_7_8_9_10_11_12[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
53
54// no comments
55static const uint8_t order_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10[] = {1, 14, 9, 4, 7, 12, 15, 6, 13, 8, 3, 16, 11, 2, 5, 10};
56
57static size_t getFiringOrderLength() {
58
60 case FO_1:
61 return 1;
62// 2 cylinder
63 case FO_1_2:
64 return 2;
65// 3 cylinder
66 case FO_1_2_3:
67 case FO_1_3_2:
68 return 3;
69// 4 cylinder
70 case FO_1_3_4_2:
71 case FO_1_2_4_3:
72 case FO_1_3_2_4:
73 case FO_1_4_3_2:
74 return 4;
75// 5 cylinder
76 case FO_1_2_4_5_3:
77 return 5;
78
79// 6 cylinder
80 case FO_1_5_3_6_2_4:
81 case FO_1_4_2_5_3_6:
82 case FO_1_2_3_4_5_6:
83 case FO_1_6_3_2_5_4:
84 case FO_1_4_3_6_2_5:
85 case FO_1_6_2_4_3_5:
86 case FO_1_6_5_4_3_2:
87 case FO_1_4_5_2_3_6:
88 return 6;
89
90// 8 cylinder
91 case FO_1_8_4_3_6_5_7_2:
92 case FO_1_8_7_2_6_5_4_3:
93 case FO_1_5_4_2_6_3_7_8:
94 case FO_1_2_7_8_4_5_6_3:
95 case FO_1_3_7_2_6_5_4_8:
96 case FO_1_2_3_4_5_6_7_8:
97 case FO_1_5_4_8_6_3_7_2:
98 case FO_1_8_7_3_6_5_4_2:
99 case FO_1_5_4_8_3_7_2_6:
100 case FO_1_8_6_2_7_3_4_5:
101 return 8;
102
103// 9 cylinder radial
104 case FO_1_2_3_4_5_6_7_8_9:
105 return 9;
106
107// 10 cylinder
108 case FO_1_10_9_4_3_6_5_8_7_2:
109 case FO_1_6_5_10_2_7_3_8_4_9:
110 return 10;
111
112// 12 cylinder
113 case FO_1_7_5_11_3_9_6_12_2_8_4_10:
114 case FO_1_7_4_10_2_8_6_12_3_9_5_11:
115 case FO_1_12_5_8_3_10_6_7_2_11_4_9:
116 case FO_1_2_3_4_5_6_7_8_9_10_11_12:
117 return 12;
118
119 case FO_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10:
120 return 16;
121
122 default:
124 }
125 return 1;
126}
127
128static const uint8_t* getFiringOrderTable() {
130 case FO_1:
131 return order_1;
132// 2 cylinder
133 case FO_1_2:
134 return order_1_2;
135// 3 cylinder
136 case FO_1_2_3:
137 return order_1_2_3;
138 case FO_1_3_2:
139 return order_1_3_2;
140// 4 cylinder
141 case FO_1_3_4_2:
143 case FO_1_2_4_3:
145 case FO_1_3_2_4:
147 case FO_1_4_3_2:
149// 5 cylinder
150 case FO_1_2_4_5_3:
151 return order_1_2_4_5_3;
152
153// 6 cylinder
154 case FO_1_5_3_6_2_4:
156 case FO_1_4_2_5_3_6:
158 case FO_1_2_3_4_5_6:
160 case FO_1_6_3_2_5_4:
161 return order_1_6_3_2_5_4;
162 case FO_1_4_3_6_2_5:
163 return order_1_4_3_6_2_5;
164 case FO_1_6_2_4_3_5:
165 return order_1_6_2_4_3_5;
166 case FO_1_6_5_4_3_2:
167 return order_1_6_5_4_3_2;
168 case FO_1_4_5_2_3_6:
169 return order_1_4_5_2_3_6;
170
171// 8 cylinder
172 case FO_1_8_4_3_6_5_7_2:
174 case FO_1_8_7_2_6_5_4_3:
176 case FO_1_5_4_2_6_3_7_8:
178 case FO_1_2_7_8_4_5_6_3:
180 case FO_1_3_7_2_6_5_4_8:
182 case FO_1_2_3_4_5_6_7_8:
184 case FO_1_5_4_8_6_3_7_2:
186 case FO_1_8_7_3_6_5_4_2:
188 case FO_1_5_4_8_3_7_2_6:
190 case FO_1_8_6_2_7_3_4_5:
192
193// 9 cylinder
194 case FO_1_2_3_4_5_6_7_8_9:
196
197
198// 10 cylinder
199 case FO_1_10_9_4_3_6_5_8_7_2:
201 case FO_1_6_5_10_2_7_3_8_4_9:
203
204// 12 cylinder
205 case FO_1_7_5_11_3_9_6_12_2_8_4_10:
207 case FO_1_7_4_10_2_8_6_12_3_9_5_11:
209 case FO_1_12_5_8_3_10_6_7_2_11_4_9:
211 case FO_1_2_3_4_5_6_7_8_9_10_11_12:
213
214// do not ask
215 case FO_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10:
217
218 default:
220 }
221
222 return NULL;
223}
224
225/**
226 * @param index from zero to cylindersCount - 1
227 * @return cylinderId from one to cylindersCount
228 */
229size_t getCylinderNumberAtIndex(size_t index) {
230 const size_t firingOrderLength = getFiringOrderLength();
231
232 if (firingOrderLength < 1 || firingOrderLength > MAX_CYLINDER_COUNT) {
233 firmwareError(ObdCode::CUSTOM_FIRING_LENGTH, "fol %d", firingOrderLength);
234 return 0;
235 }
236 if (engineConfiguration->cylindersCount != firingOrderLength) {
237 // May 2020 this somehow still happens with functional tests, maybe race condition?
238 firmwareError(ObdCode::CUSTOM_OBD_WRONG_FIRING_ORDER, "Wrong cyl count for firing order, expected %d cylinders, got %d", firingOrderLength, engineConfiguration->cylindersCount);
239 return 0;
240 }
241
242 if (index >= firingOrderLength) {
243 // May 2020 this somehow still happens with functional tests, maybe race condition?
244 warning(ObdCode::CUSTOM_ERR_6686, "firing order index %d", index);
245 return 0;
246 }
247
248 if (auto firingOrderTable = getFiringOrderTable()) {
249 return firingOrderTable[index] - 1;
250 } else {
251 // error already reported
252 return 0;
253 }
254}
255
256/**
257 * @param prevCylinderId from one to cylindersCount
258 * @return cylinderId from one to cylindersCount
259 */
260size_t getNextFiringCylinderId(size_t prevCylinderId) {
261 const size_t firingOrderLength = getFiringOrderLength();
262 auto firingOrderTable = getFiringOrderTable();
263
264 if (firingOrderTable) {
265 for (size_t i = 0; i < firingOrderLength; i++) {
266 if (firingOrderTable[i] == prevCylinderId) {
267 return firingOrderTable[(i + 1) % firingOrderLength];
268 }
269 }
270 }
271
272 return 1;
273}
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
void firmwareError(ObdCode code, const char *fmt,...)
static const uint8_t order_1_5_4_8_6_3_7_2[]
static const uint8_t order_1_THEN_3_THEN_4_THEN2[]
static const uint8_t order_1_4_5_2_3_6[]
static const uint8_t order_1_6_3_2_5_4[]
static const uint8_t order_1_THEN_4_THEN_2_THEN_5_THEN_3_THEN_6[]
static const uint8_t order_1_8_7_3_6_5_4_2[]
static const uint8_t order_1_THEN_4_THEN_3_THEN2[]
static const uint8_t order_1_THEN_2_THEN_4_THEN3[]
static const uint8_t order_1[]
static const uint8_t order_1_10_9_4_3_6_5_8_7_2[]
static const uint8_t order_1_5_4_8_3_7_2_6[]
size_t getNextFiringCylinderId(size_t prevCylinderId)
static const uint8_t order_1_2_3_4_5_6_7_8_9[]
static const uint8_t order_1_3_7_2_6_5_4_8[]
static const uint8_t order_1_6_2_4_3_5[]
static const uint8_t order_1_3_2[]
static const uint8_t order_1_2_3_4_5_6_7_8[]
static const uint8_t order_1_6_5_10_2_7_3_8_4_9[]
static const uint8_t order_1_5_4_2_6_3_7_8[]
static const uint8_t order_1_2_7_8_4_5_6_3[]
static const uint8_t order_1_4_3_6_2_5[]
static const uint8_t order_1_THEN_3_THEN_2_THEN4[]
static const uint8_t order_1_8_7_2_6_5_4_3[]
static const uint8_t order_1_8_6_2_7_3_4_5[]
static const uint8_t order_1_THEN_2_THEN_3_THEN_4_THEN_5_THEN_6[]
static const uint8_t order_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10[]
static const uint8_t order_1_2_3[]
static const uint8_t order_1_2_3_4_5_6_7_8_9_10_11_12[]
static const uint8_t order_1_6_5_4_3_2[]
static const uint8_t order_1_2_4_5_3[]
static const uint8_t * getFiringOrderTable()
static const uint8_t order_1_8_4_3_6_5_7_2[]
static const uint8_t order_1_THEN_5_THEN_3_THEN_6_THEN_2_THEN_4[]
static const uint8_t order_1_12_5_8_3_10_6_7_2_11_4_9[]
static const uint8_t order_1_7_4_10_2_8_6_12_3_9_5_11[]
static size_t getFiringOrderLength()
size_t getCylinderNumberAtIndex(size_t index)
static const uint8_t order_1_7_5_11_3_9_6_12_2_8_4_10[]
static const uint8_t order_1_2[]
@ CUSTOM_ERR_6686
@ CUSTOM_OBD_UNKNOWN_FIRING_ORDER
@ CUSTOM_FIRING_LENGTH
@ CUSTOM_OBD_WRONG_FIRING_ORDER