-
Notifications
You must be signed in to change notification settings - Fork 25
/
MAP_EFI_V2.ino
417 lines (360 loc) · 13.6 KB
/
MAP_EFI_V2.ino
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
// EFI controller based on MAP and Points ONLY, V2
// This version includes a complete AFR mapping table.
// Thresholded rate-of-change-of-MAP is also used to emulate delta-TPS.
// (C) 2021 John Patterson Consulting, LLC
// You are free to modify and redistribute this code without restriction.
// ----------------- Program constants -----------------:
// Pin numbers:
const int pointsPin = 3;
const int MAPPin = 14;
const int injectorPin = 6;
const int LEDPin = 13;
// Timing constants:
const unsigned long injectorUpdateInterval = 50000;
const unsigned long interruptDebounce = 3000;
const unsigned long primeTime = 300;
const unsigned long pumpTime = 0;
const int serialReportModulo = 10;
const int RPMrunningAverageDepth = 5;
const unsigned long blinkTime = 250;
// Engine-related constants:
const int pulsesPerRevolution = 2;
const double displacement_Liters = 1.6;
const double injector_MaxGramsPerMinute = 260.0; // Will depend on your injector and fuel pressure
const double stallRPM = 300.0;
const double Pascals_Per_ADC_Unit = 122.1;
const int vacuum_ADC_value = 102;
const double min_DC = 0.07; // Minimum duty cycle for idling
const double vacuum_ROC_Multiplier = 0.0005; // Add or remove AFR proportional to the rate of change of MAP
const double vacuum_ROC_Cut_In = 51710; // MAP pressure over which vacuum ROC will be applied
//Fuel AFR selector (gas or E51):
//const double fuel_comp = 1.0; // Gas
const double fuel_comp = 0.70; // E51
//// All 14.7:
//// AFR mapping:
//// PSI: 0-1.5 1.5-3 3-4.5 4.5-6 6-7.5 7.5-9 9-10.5 10.5-12 12-13.5 13.5-15
//double AFR_500RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_1000RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_1500RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_2000RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_2500RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_3000RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_3500RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_4000RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_4500RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//double AFR_5000RPM[10] = {14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700, 14.700};
//// Richer at high load only:
//// AFR mapping:
//// PSI: 0-1.5 1.5-3 3-4.5 4.5-6 6-7.5 7.5-9 9-10.5 10.5-12 12-13.5 13.5-15
//double AFR_500RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_1000RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_1500RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_2000RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_2500RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_3000RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_3500RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_4000RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_4500RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
//double AFR_5000RPM[10] = {14.500, 14.500, 14.000, 13.700, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
// Lean coasting:
// AFR mapping:
// PSI: 0-1.5 1.5-3 3-4.5 4.5-6 6-7.5 7.5-9 9-10.5 10.5-12 12-13.5 13.5-15
double AFR_500RPM[10] = {13.500, 13.500, 13.500, 13.500, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_1000RPM[10] = {13.500, 13.500, 13.500, 13.500, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_1500RPM[10] = {14.000, 14.000, 14.000, 14.000, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_2000RPM[10] = {14.000, 14.000, 14.000, 14.000, 13.500, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_2500RPM[10] = {15.000, 14.000, 14.000, 14.000, 13.700, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_3000RPM[10] = {16.500, 15.500, 14.000, 14.000, 14.000, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_3500RPM[10] = {17.000, 16.500, 15.000, 14.000, 14.000, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_4000RPM[10] = {18.000, 17.500, 16.000, 15.000, 14.000, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_4500RPM[10] = {19.000, 18.500, 17.000, 16.000, 15.000, 13.200, 13.000, 12.700, 12.500, 12.000};
double AFR_5000RPM[10] = {19.500, 18.500, 17.000, 16.000, 15.000, 13.200, 13.000, 12.700, 12.500, 12.000};
// Physical parameters:
const double airTempKelvin = 298;
const double airMolarMass = 28.9;
const double R_Liter_Pascal_Kelvin = 8314.0;
// ----------------- Global Variables -----------------:
unsigned long updateLastMicros = 0;
unsigned long pointsLastMicros = 0;
unsigned long pointsDiff = 100000000;
bool stalled = 0;
int serialReportCount = 0;
double RPMrunningAverage[RPMrunningAverageDepth];
int RPMrunningAverageIndex = 0;
unsigned long blinkLastMillis = 0;
bool blinkState = 0;
unsigned long lastMAP = 0;
// ----------------- Functions -----------------:
// ISR triggered when the points open:
void points()
{
//Determine if interrupt is valid based on time since last interrupt:
if((micros() - pointsLastMicros) > interruptDebounce)
{
// Compute difference in time from last points opening:
pointsDiff = micros() - pointsLastMicros;
// Reset points timer:
pointsLastMicros = micros();
}
}
// Function to obtain AFR target:
double AFR(double pressurePa, double speedRPM)
{
// Determine index of MAP pressure in AFR table to use:
int MAP_index = 0;
if(pressurePa < 10342.1)
{
MAP_index = 0;
}
else if(pressurePa < 20684.3)
{
MAP_index = 1;
}
else if(pressurePa < 31026.4)
{
MAP_index = 2;
}
else if(pressurePa < 41368.5)
{
MAP_index = 3;
}
else if(pressurePa < 51710.7)
{
MAP_index = 4;
}
else if(pressurePa < 62052.8)
{
MAP_index = 5;
}
else if(pressurePa < 72394.95)
{
MAP_index = 6;
}
else if(pressurePa < 82737.1)
{
MAP_index = 7;
}
else if(pressurePa < 93079.22)
{
MAP_index = 8;
}
else
{
MAP_index = 9;
}
// Determine RPM range to use and return relevant AFR:
if(speedRPM < 500)
{
return AFR_500RPM[MAP_index];
}
else if(speedRPM < 1000)
{
return AFR_1000RPM[MAP_index];
}
else if(speedRPM < 1500)
{
return AFR_1500RPM[MAP_index];
}
else if(speedRPM < 2000)
{
return AFR_2000RPM[MAP_index];
}
else if(speedRPM < 2500)
{
return AFR_2500RPM[MAP_index];
}
else if(speedRPM < 3000)
{
return AFR_3000RPM[MAP_index];
}
else if(speedRPM < 3500)
{
return AFR_3500RPM[MAP_index];
}
else if(speedRPM < 4000)
{
return AFR_4000RPM[MAP_index];
}
else if(speedRPM < 4500)
{
return AFR_4500RPM[MAP_index];
}
else
{
return AFR_5000RPM[MAP_index];
}
}
// Function to compute RPM based on time since last points opening:
double RPM()
{
// If pointsDiff is longer than the time since points() was last called,
// just use pointsDiff to compute the RPM:
if(pointsDiff > (micros() - pointsLastMicros))
{
// RPM = (Pulses/sec)/(Pulses/Revolution)*(60 sec/min)
RPMrunningAverage[RPMrunningAverageIndex] = (1000000.0/pointsDiff)/pulsesPerRevolution*60.0;
}
// If it has been longer than pointsDiff since points() was last called,
// use the elapsed time to compute the RPM:
else
{
// RPM = (Pulses/sec)/(Pulses/Revolution)*(60 sec/min)
RPMrunningAverage[RPMrunningAverageIndex] = (1000000.0/(micros() - pointsLastMicros))/pulsesPerRevolution*60.0;
}
//Increment running average index, reset if needed:
RPMrunningAverageIndex++;
if(RPMrunningAverageIndex >= RPMrunningAverageDepth)
{
RPMrunningAverageIndex = 0;
}
// Compute running average sum:
double sum = 0;
for(int i = 0; i < RPMrunningAverageDepth; i++)
{
sum = sum + RPMrunningAverage[i];
}
// Return running average of RPM:
return sum/RPMrunningAverageDepth;
}
// Function to measure the Manifold Absolute Pressure (MAP):
double MAP()
{
double estimatedMAP = (analogRead(MAPPin) - vacuum_ADC_value)*Pascals_Per_ADC_Unit;
// Ensure only positive MAP values are returned:
if(estimatedMAP < 0)
{
estimatedMAP = 0;
}
return estimatedMAP;
}
// Function to report human-readable stats to the serial port:
void reportStats(double RPM_report, double MAP_report, double DC_report)
{
// Report engine RPM:
Serial.print("Engine RPM: ");
Serial.println(RPM_report);
Serial.print("Manifold Absolute Pressure: ");
Serial.print("kPa: ");
Serial.print(MAP_report/1000.0);
Serial.print(" PSI: ");
Serial.println(MAP_report/6894.76);
Serial.print("Injector Duty Cycle: ");
Serial.print(DC_report*100.0);
Serial.println("%");
Serial.println(" ");
}
// Function to compute the required fuel injector duty cycle:
double getInjectorDC()
{
//Get measurements:
double RPM_value = RPM();
double MAP_value = MAP();
// Compute the volume flow rate of air (L/min) into the engine
// (Otto cycle with 2 revolutions per intake stroke assumed):
double volFlowRate = displacement_Liters*RPM_value/2.0;
// Compute the density of air (g/L) using the Ideal Gas Law (PV=nRT):
// Density = mass/volume = n(airMolarMass)/V = P(airMolarMass)/(RT)
double airDensity = MAP_value*airMolarMass/(R_Liter_Pascal_Kelvin*airTempKelvin);
// Compute the mass air flow rate into the engine:
double massAirFlow = volFlowRate*airDensity;
// Get AFR:
// Adjust the AFR based on the rate of change of the MAP level:
double vacROC = (double)MAP_value - (double)lastMAP;
lastMAP = MAP_value;
// Look up AFR in table:
double AFR_target = AFR(RPM_value,MAP_value);
// If MAP is above threshold, apply rate-of-change factor:
if(MAP_value > vacuum_ROC_Cut_In)
{
AFR_target = AFR_target - vacuum_ROC_Multiplier*vacROC;
}
// Otherwise, just use AFR from the table directly.
// Apply fuel compensator (for E51, E85, etc.)
AFR_target = fuel_comp*AFR_target;
// Compute the required mass fuel flow rate into the engine:
double massFuelFlow = massAirFlow/AFR_target;
// Compute the duty cycle for the fuel injector
// (based on the maximum fuel delivery rate):
double estimated_DC = massFuelFlow/injector_MaxGramsPerMinute;
// Ensure that the duty cycle does not exceed 1 or fall below the minimum duty cycle value:
if(estimated_DC > 1.0)
{
estimated_DC = 1.0;
}
if(estimated_DC < min_DC)
{
estimated_DC = min_DC;
}
// Report human-readable stats every serialReportModulo iterations:
if(serialReportCount < serialReportModulo)
{
serialReportCount++;
}
else
{
serialReportCount = 0;
reportStats(RPM_value, MAP_value, estimated_DC);
}
return estimated_DC;
}
// ----------------- Main Program -----------------:
void setup() {
// Set pin modes:
pinMode(injectorPin, OUTPUT);
pinMode(pointsPin, INPUT);
pinMode(LEDPin, OUTPUT);
// Attach interrupt for detecting points pulses:
attachInterrupt(digitalPinToInterrupt(pointsPin), points, RISING);
//Start Serial port:
Serial.begin(115200);
Serial.println("EFI controller based on MAP and Points ONLY");
Serial.println("(C) 2020 John Patterson Consulting, LLC");
Serial.println(" ");
//Wait for fuel pump to build pressure:
Serial.println("Waiting for fuel pump...");
delay(pumpTime);
Serial.println(" ");
// Prime engine with some fuel before starting:
Serial.println("Priming intake...");
digitalWrite(injectorPin, HIGH);
delay(primeTime);
digitalWrite(injectorPin, LOW);
Serial.println("Priming complete. Ready to crank!");
Serial.println(" ");
}
void loop() {
//Determine if the engine is currently stalled:
stalled = (RPM() < stallRPM);
// If engine is running, keep LED on and steady:
if(!stalled)
{
digitalWrite(LEDPin, HIGH);
}
// If engine is stalled, blink LED:
else if(millis() - blinkLastMillis > blinkTime)
{
blinkLastMillis = millis();
blinkState = !blinkState;
digitalWrite(LEDPin, blinkState);
}
// Pulse the injector if it is time to do so:
if((micros() - updateLastMicros) > injectorUpdateInterval)
{
// Reset injector update timer:
updateLastMicros = micros();
// Compute fuel injector duty cycle:
double computed_DC = getInjectorDC();
// Only dispense fuel if engine is running:
if(!stalled)
{
// Open injector for amount of time specified by duty cycle:
unsigned long injectorMicros = micros();
while((micros() - injectorMicros) < (unsigned long)(injectorUpdateInterval*computed_DC))
{
digitalWrite(injectorPin, HIGH);
}
//Close injector:
digitalWrite(injectorPin, LOW);
}
}
}