-
Notifications
You must be signed in to change notification settings - Fork 22
/
OBD2_mod.cpp
900 lines (776 loc) · 25.2 KB
/
OBD2_mod.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <math.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#else
#define PROGMEM
#endif
#include "CANmod.h"
#include "OBD2_mod.h"
const char PID_NAME_0x00[] PROGMEM = "PIDs supported [01 - 20]";
const char PID_NAME_0x01[] PROGMEM = "Monitor status since DTCs cleared";
const char PID_NAME_0x02[] PROGMEM = "Freeze DTC";
const char PID_NAME_0x03[] PROGMEM = "Fuel system status";
const char PID_NAME_0x04[] PROGMEM = "Calculated engine load";
const char PID_NAME_0x05[] PROGMEM = "Engine coolant temperature";
const char PID_NAME_0x06[] PROGMEM = "Short term fuel trim — Bank 1";
const char PID_NAME_0x07[] PROGMEM = "Long term fuel trim — Bank 1";
const char PID_NAME_0x08[] PROGMEM = "Short term fuel trim — Bank 2";
const char PID_NAME_0x09[] PROGMEM = "Long term fuel trim — Bank 2";
const char PID_NAME_0x0a[] PROGMEM = "Fuel pressure";
const char PID_NAME_0x0b[] PROGMEM = "Intake manifold absolute pressure";
const char PID_NAME_0x0c[] PROGMEM = "Engine RPM";
const char PID_NAME_0x0d[] PROGMEM = "Vehicle speed";
const char PID_NAME_0x0e[] PROGMEM = "Timing advance";
const char PID_NAME_0x0f[] PROGMEM = "Intake air temperature";
const char PID_NAME_0x10[] PROGMEM = "MAF air flow rate";
const char PID_NAME_0x11[] PROGMEM = "Throttle position";
const char PID_NAME_0x12[] PROGMEM = "Commanded secondary air status";
const char PID_NAME_0x13[] PROGMEM = "Oxygen sensors present (in 2 banks)";
const char PID_NAME_0x14[] PROGMEM = "Oxygen Sensor 1 - Short term fuel trim";
const char PID_NAME_0x15[] PROGMEM = "Oxygen Sensor 2 - Short term fuel trim";
const char PID_NAME_0x16[] PROGMEM = "Oxygen Sensor 3 - Short term fuel trim";
const char PID_NAME_0x17[] PROGMEM = "Oxygen Sensor 4 - Short term fuel trim";
const char PID_NAME_0x18[] PROGMEM = "Oxygen Sensor 5 - Short term fuel trim";
const char PID_NAME_0x19[] PROGMEM = "Oxygen Sensor 6 - Short term fuel trim";
const char PID_NAME_0x1a[] PROGMEM = "Oxygen Sensor 7 - Short term fuel trim";
const char PID_NAME_0x1b[] PROGMEM = "Oxygen Sensor 8 - Short term fuel trim";
const char PID_NAME_0x1c[] PROGMEM = "OBD standards this vehicle conforms to";
const char PID_NAME_0x1d[] PROGMEM = "Oxygen sensors present (in 4 banks)";
const char PID_NAME_0x1e[] PROGMEM = "Auxiliary input status";
const char PID_NAME_0x1f[] PROGMEM = "Run time since engine start";
const char PID_NAME_0x20[] PROGMEM = "PIDs supported [21 - 40]";
const char PID_NAME_0x21[] PROGMEM = "Distance traveled with malfunction indicator lamp (MIL) on";
const char PID_NAME_0x22[] PROGMEM = "Fuel Rail Pressure (relative to manifold vacuum)";
const char PID_NAME_0x23[] PROGMEM = "Fuel Rail Gauge Pressure (diesel, or gasoline direct injection)";
const char PID_NAME_0x24[] PROGMEM = "Oxygen Sensor 1 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x25[] PROGMEM = "Oxygen Sensor 2 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x26[] PROGMEM = "Oxygen Sensor 3 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x27[] PROGMEM = "Oxygen Sensor 4 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x28[] PROGMEM = "Oxygen Sensor 5 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x29[] PROGMEM = "Oxygen Sensor 6 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x2a[] PROGMEM = "Oxygen Sensor 7 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x2b[] PROGMEM = "Oxygen Sensor 8 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x2c[] PROGMEM = "Commanded EGR";
const char PID_NAME_0x2d[] PROGMEM = "EGR Error";
const char PID_NAME_0x2e[] PROGMEM = "Commanded evaporative purge";
const char PID_NAME_0x2f[] PROGMEM = "Fuel Tank Level Input";
const char PID_NAME_0x30[] PROGMEM = "Warm-ups since codes cleared";
const char PID_NAME_0x31[] PROGMEM = "Distance traveled since codes cleared";
const char PID_NAME_0x32[] PROGMEM = "Evap. System Vapor Pressure";
const char PID_NAME_0x33[] PROGMEM = "Absolute Barometric Pressure";
const char PID_NAME_0x34[] PROGMEM = "Oxygen Sensor 1 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x35[] PROGMEM = "Oxygen Sensor 2 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x36[] PROGMEM = "Oxygen Sensor 3 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x37[] PROGMEM = "Oxygen Sensor 4 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x38[] PROGMEM = "Oxygen Sensor 5 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x39[] PROGMEM = "Oxygen Sensor 6 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x3a[] PROGMEM = "Oxygen Sensor 7 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x3b[] PROGMEM = "Oxygen Sensor 8 - Fuel–Air Equivalence Ratio";
const char PID_NAME_0x3c[] PROGMEM = "Catalyst Temperature: Bank 1, Sensor 1";
const char PID_NAME_0x3d[] PROGMEM = "Catalyst Temperature: Bank 2, Sensor 1";
const char PID_NAME_0x3e[] PROGMEM = "Catalyst Temperature: Bank 1, Sensor 2";
const char PID_NAME_0x3f[] PROGMEM = "Catalyst Temperature: Bank 2, Sensor 2";
const char PID_NAME_0x40[] PROGMEM = "PIDs supported [41 - 60]";
const char PID_NAME_0x41[] PROGMEM = "Monitor status this drive cycle";
const char PID_NAME_0x42[] PROGMEM = "Control module voltage";
const char PID_NAME_0x43[] PROGMEM = "Absolute load value";
const char PID_NAME_0x44[] PROGMEM = "Fuel–Air commanded equivalence ratio";
const char PID_NAME_0x45[] PROGMEM = "Relative throttle position";
const char PID_NAME_0x46[] PROGMEM = "Ambient air temperature";
const char PID_NAME_0x47[] PROGMEM = "Absolute throttle position B";
const char PID_NAME_0x48[] PROGMEM = "Absolute throttle position C";
const char PID_NAME_0x49[] PROGMEM = "Absolute throttle position D";
const char PID_NAME_0x4a[] PROGMEM = "Absolute throttle position E";
const char PID_NAME_0x4b[] PROGMEM = "Absolute throttle position F";
const char PID_NAME_0x4c[] PROGMEM = "Commanded throttle actuator";
const char PID_NAME_0x4d[] PROGMEM = "Time run with MIL on";
const char PID_NAME_0x4e[] PROGMEM = "Time since trouble codes cleared";
const char PID_NAME_0x4f[] PROGMEM = "Maximum value for Fuel–Air equivalence ratio, oxygen sensor voltage, oxygen sensor current, and intake manifold absolute pressure";
const char PID_NAME_0x50[] PROGMEM = "Maximum value for air flow rate from mass air flow sensor";
const char PID_NAME_0x51[] PROGMEM = "Fuel Type";
const char PID_NAME_0x52[] PROGMEM = "Ethanol fuel percentage";
const char PID_NAME_0x53[] PROGMEM = "Absolute Evap system Vapor Pressure";
const char PID_NAME_0x54[] PROGMEM = "Evap system vapor pressure";
const char PID_NAME_0x55[] PROGMEM = "Short term secondary oxygen sensor trim";
const char PID_NAME_0x56[] PROGMEM = "Long term secondary oxygen sensor trim";
const char PID_NAME_0x57[] PROGMEM = "Short term secondary oxygen sensor trim";
const char PID_NAME_0x58[] PROGMEM = "Long term secondary oxygen sensor trim";
const char PID_NAME_0x59[] PROGMEM = "Fuel rail absolute pressure";
const char PID_NAME_0x5a[] PROGMEM = "Relative accelerator pedal position";
const char PID_NAME_0x5b[] PROGMEM = "Hybrid battery pack remaining life";
const char PID_NAME_0x5c[] PROGMEM = "Engine oil temperature";
const char PID_NAME_0x5d[] PROGMEM = "Fuel injection timing";
const char PID_NAME_0x5e[] PROGMEM = "Engine fuel rate";
const char PID_NAME_0x5f[] PROGMEM = "Emission requirements to which vehicle is designed";
const char PID_NAME_0x202[] PROGMEM = "rpm speed throttle";
const char PID_NAME_0x215[] PROGMEM = "All wheel speed";
const char* const PID_NAME_MAPPER[] PROGMEM = {
PID_NAME_0x00,
PID_NAME_0x01,
PID_NAME_0x02,
PID_NAME_0x03,
PID_NAME_0x04,
PID_NAME_0x05,
PID_NAME_0x06,
PID_NAME_0x07,
PID_NAME_0x08,
PID_NAME_0x09,
PID_NAME_0x0a,
PID_NAME_0x0b,
PID_NAME_0x0c,
PID_NAME_0x0d,
PID_NAME_0x0e,
PID_NAME_0x0f,
PID_NAME_0x10,
PID_NAME_0x11,
PID_NAME_0x12,
PID_NAME_0x13,
PID_NAME_0x14,
PID_NAME_0x15,
PID_NAME_0x16,
PID_NAME_0x17,
PID_NAME_0x18,
PID_NAME_0x19,
PID_NAME_0x1a,
PID_NAME_0x1b,
PID_NAME_0x1c,
PID_NAME_0x1d,
PID_NAME_0x1e,
PID_NAME_0x1f,
PID_NAME_0x20,
PID_NAME_0x21,
PID_NAME_0x22,
PID_NAME_0x23,
PID_NAME_0x24,
PID_NAME_0x25,
PID_NAME_0x26,
PID_NAME_0x27,
PID_NAME_0x28,
PID_NAME_0x29,
PID_NAME_0x2a,
PID_NAME_0x2b,
PID_NAME_0x2c,
PID_NAME_0x2d,
PID_NAME_0x2e,
PID_NAME_0x2f,
PID_NAME_0x30,
PID_NAME_0x31,
PID_NAME_0x32,
PID_NAME_0x33,
PID_NAME_0x34,
PID_NAME_0x35,
PID_NAME_0x36,
PID_NAME_0x37,
PID_NAME_0x38,
PID_NAME_0x39,
PID_NAME_0x3a,
PID_NAME_0x3b,
PID_NAME_0x3c,
PID_NAME_0x3d,
PID_NAME_0x3e,
PID_NAME_0x3f,
PID_NAME_0x40,
PID_NAME_0x41,
PID_NAME_0x42,
PID_NAME_0x43,
PID_NAME_0x44,
PID_NAME_0x45,
PID_NAME_0x46,
PID_NAME_0x47,
PID_NAME_0x48,
PID_NAME_0x49,
PID_NAME_0x4a,
PID_NAME_0x4b,
PID_NAME_0x4c,
PID_NAME_0x4d,
PID_NAME_0x4e,
PID_NAME_0x4f,
PID_NAME_0x50,
PID_NAME_0x51,
PID_NAME_0x52,
PID_NAME_0x53,
PID_NAME_0x54,
PID_NAME_0x55,
PID_NAME_0x56,
PID_NAME_0x57,
PID_NAME_0x58,
PID_NAME_0x59,
PID_NAME_0x5a,
PID_NAME_0x5b,
PID_NAME_0x5c,
PID_NAME_0x5d,
PID_NAME_0x5e,
PID_NAME_0x5f,
PID_NAME_0x202,
PID_NAME_0x215,
};
const char PERCENTAGE[] PROGMEM = "%";
const char KPA[] PROGMEM = "kPa";
const char PA[] PROGMEM = "Pa";
const char RPM[] PROGMEM = "rpm";
const char KPH[] PROGMEM = "km/h";
const char DEGREES_BEFORE_TDC[] PROGMEM = "° before TDC";
const char GRAMS_PER_SECOND[] PROGMEM = "grams/sec";
const char SECONDS[] PROGMEM = "seconds";
const char RATIO[] PROGMEM = "ratio";
const char COUNT[] PROGMEM = "count";
const char KM[] PROGMEM = "km";
const char VOLTS[] PROGMEM = "V";
const char MINUTES[] PROGMEM = "minutes";
const char GPS[] PROGMEM = "g/s";
const char DEGREES[] PROGMEM = "°";
const char DEGREES_CELCIUS[] PROGMEM = "°C";
const char LPH[] PROGMEM = "L/h";
const char* const PID_UNIT_MAPPER[] PROGMEM = {
NULL,
NULL,
NULL,
NULL,
PERCENTAGE,
DEGREES_CELCIUS,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
KPA,
KPA,
RPM,
KPH,
DEGREES_BEFORE_TDC,
DEGREES_CELCIUS,
GRAMS_PER_SECOND,
PERCENTAGE,
NULL,
NULL,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
NULL,
NULL,
NULL,
SECONDS,
NULL,
KM,
KPA,
KPA,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
COUNT,
KM,
PA,
KPA,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
RATIO,
DEGREES_CELCIUS,
DEGREES_CELCIUS,
DEGREES_CELCIUS,
DEGREES_CELCIUS,
NULL,
NULL,
VOLTS,
PERCENTAGE,
RATIO,
PERCENTAGE,
DEGREES_CELCIUS,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
MINUTES,
MINUTES,
NULL,
GPS,
NULL,
PERCENTAGE,
KPA,
PA,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
PERCENTAGE,
KPA,
PERCENTAGE,
PERCENTAGE,
DEGREES_CELCIUS,
DEGREES,
LPH,
NULL,
NULL,
NULL,
};
OBD2Class::OBD2Class() :
_responseTimeout(OBD2_DEFAULT_TIMEOUT),
_lastPidResponseMillis(0)
{
memset(_supportedPids, 0x00, sizeof(_supportedPids));
}
OBD2Class::~OBD2Class()
{
}
int OBD2Class::begin()
{
if (!CANmod.begin(500E3)) {
return 0;
}
memset(_supportedPids, 0x00, sizeof(_supportedPids));
// first try standard addressing
_useExtendedAddressing = false;
CANmod.filter(0x7e8);
if (!supportedPidsRead()) {
// next try extended addressing
_useExtendedAddressing = true;
CANmod.filterExtended(0x18daf110);
if (!supportedPidsRead()) {
return 0;
}
}
return 1;
}
void OBD2Class::end()
{
CANmod.end();
}
bool OBD2Class::pidSupported(uint8_t pid)
{
if (pid == 0) {
return true;
}
return (_supportedPids[(pid - 1) / 0x20] & ((1UL << (31 - ((pid - 1) % 32))))) ? true : false;
}
bool OBD2Class::pidValueRaw(uint8_t pid)
{
switch(pid) {
case PIDS_SUPPORT_01_20: // raw
case MONITOR_STATUS_SINCE_DTCS_CLEARED: // raw
case FREEZE_DTC: // raw
case PIDS_SUPPORT_21_40: // raw
case PIDS_SUPPORT_41_60: // raw
case MONITOR_STATUS_THIS_DRIVE_CYCLE: // raw
case FUEL_SYSTEM_STATUS: // raw
case COMMANDED_SECONDARY_AIR_STATUS: // raw
case OBD_STANDARDS_THIS_VEHICLE_CONFORMS_TO: // raw
case OXYGEN_SENSORS_PRESENT_IN_2_BANKS: // raw
case OXYGEN_SENSORS_PRESENT_IN_4_BANKS: // raw
case AUXILIARY_INPUT_STATUS: // raw
case FUEL_TYPE: // raw
case EMISSION_REQUIREMENT_TO_WHICH_VEHICLE_IS_DESIGNED: // raw
return true;
default:
return (pid > 0x5f);
}
}
String OBD2Class::pidName(uint8_t pid)
{
if (pid > 0x5f) {
return "Unknown";
}
#ifdef __AVR__
const char* pgmName = pgm_read_ptr(&PID_NAME_MAPPER[pid]);
String name;
if (pgmName != NULL) {
while (char c = pgm_read_byte(pgmName++)) {
name += c;
}
}
return name;
#else
return PID_NAME_MAPPER[pid];
#endif
}
String OBD2Class::pidUnits(uint8_t pid)
{
if (pid > 0x5f) {
return "";
}
#ifdef __AVR__
const char* pgmUnits = pgm_read_ptr(&PID_UNIT_MAPPER[pid]);
String units;
if (pgmUnits != NULL) {
while (char c = pgm_read_byte(pgmUnits++)) {
units += c;
}
}
return units;
#else
return PID_UNIT_MAPPER[pid];
#endif
}
float OBD2Class::pidRead(uint8_t pid)
{
if (!pidSupported(pid)) {
return NAN;
}
#define A value[0]
#define B value[1]
#define C value[2]
#define D value[3]
uint8_t value[4];
if (!pidRead(0x01, pid, &value, sizeof(value))) {
return NAN;
}
switch (pid) {
default:
case PIDS_SUPPORT_01_20: // raw
case MONITOR_STATUS_SINCE_DTCS_CLEARED: // raw
case FREEZE_DTC: // raw
case PIDS_SUPPORT_21_40: // raw
case PIDS_SUPPORT_41_60: // raw
case MONITOR_STATUS_THIS_DRIVE_CYCLE: // raw
// NOTE: return value can lose precision!
return ((uint32_t)A << 24 | (uint32_t)B << 16 | (uint32_t)C << 8 | (uint32_t)D);
case FUEL_SYSTEM_STATUS: // raw
case RUN_TIME_SINCE_ENGINE_START:
case DISTANCE_TRAVELED_WITH_MIL_ON:
case DISTANCE_TRAVELED_SINCE_CODES_CLEARED:
case TIME_RUN_WITH_MIL_ON:
case TIME_SINCE_TROUBLE_CODES_CLEARED:
return (A * 256.0 + B);
case CALCULATED_ENGINE_LOAD:
case THROTTLE_POSITION:
case COMMANDED_EGR:
case COMMANDED_EVAPORATIVE_PURGE:
case FUEL_TANK_LEVEL_INPUT:
case RELATIVE_THROTTLE_POSITION:
case ABSOLUTE_THROTTLE_POSITION_B:
case ABSOLUTE_THROTTLE_POSITION_C:
case ABSOLUTE_THROTTLE_POSITION_D:
case ABSOLUTE_THROTTLE_POSITION_E:
case ABSOLUTE_THROTTLE_POSITION_F:
case COMMANDED_THROTTLE_ACTUATOR:
case ETHANOL_FUEL_PERCENTAGE:
case RELATIVE_ACCELERATOR_PEDAL_POSITTION:
case HYBRID_BATTERY_PACK_REMAINING_LIFE:
return (A / 2.55);
case COMMANDED_SECONDARY_AIR_STATUS: // raw
case OBD_STANDARDS_THIS_VEHICLE_CONFORMS_TO: // raw
case OXYGEN_SENSORS_PRESENT_IN_2_BANKS: // raw
case OXYGEN_SENSORS_PRESENT_IN_4_BANKS: // raw
case AUXILIARY_INPUT_STATUS: // raw
case FUEL_TYPE: // raw
case EMISSION_REQUIREMENT_TO_WHICH_VEHICLE_IS_DESIGNED: // raw
return (A);
case OXYGEN_SENSOR_1_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_2_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_3_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_4_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_5_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_6_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_7_SHORT_TERM_FUEL_TRIM:
case OXYGEN_SENSOR_8_SHORT_TERM_FUEL_TRIM:
return ((B / 1.28) - 100.0);
break;
case ENGINE_COOLANT_TEMPERATURE:
case AIR_INTAKE_TEMPERATURE:
case AMBIENT_AIR_TEMPERATURE:
case ENGINE_OIL_TEMPERATURE:
return (A - 40.0);
case SHORT_TERM_FUEL_TRIM_BANK_1:
case LONG_TERM_FUEL_TRIM_BANK_1:
case SHORT_TERM_FUEL_TRIM_BANK_2:
case LONG_TERM_FUEL_TRIM_BANK_2:
case EGR_ERROR:
return ((A / 1.28) - 100.0);
case FUEL_PRESSURE:
return (A * 3.0);
case INTAKE_MANIFOLD_ABSOLUTE_PRESSURE:
case VEHICLE_SPEED:
case WARM_UPS_SINCE_CODES_CLEARED:
case ABSOLULTE_BAROMETRIC_PRESSURE:
return (A);
case ENGINE_RPM:
return ((A * 256.0 + B) / 4.0);
case TIMING_ADVANCE:
return ((A / 2.0) - 64.0);
case MAF_AIR_FLOW_RATE:
return ((A * 256.0 + B) / 100.0);
case FUEL_RAIL_PRESSURE:
return ((A * 256.0 + B) * 0.079);
case FUEL_RAIL_GAUGE_PRESSURE:
case FUEL_RAIL_ABSOLUTE_PRESSURE:
return ((A * 256.0 + B) * 10.0);
case OXYGEN_SENSOR_1_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_2_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_3_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_4_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_5_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_6_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_7_FUEL_AIR_EQUIVALENCE_RATIO:
case OXYGEN_SENSOR_8_FUEL_AIR_EQUIVALENCE_RATIO:
case 0x34:
case 0x35:
case 0x36:
case 0x37:
case 0x38:
case 0x39:
case 0x3a:
case 0x3b:
return (((A * 256.0 + B) * 2.0) / 65536.0);
case EVAP_SYSTEM_VAPOR_PRESSURE:
return (((int16_t)(A * 256.0 + B)) / 4.0);
case CATALYST_TEMPERATURE_BANK_1_SENSOR_1:
case CATALYST_TEMPERATURE_BANK_2_SENSOR_1:
case CATALYST_TEMPERATURE_BANK_1_SENSOR_2:
case CATALYST_TEMPERATURE_BANK_2_SENSOR_2:
return (((A * 256.0 + B) / 10.0) - 40.0);
case CONTROL_MODULE_VOLTAGE:
return ((A * 256.0 + B) / 1000.0);
case ABSOLUTE_LOAD_VALUE:
return ((A * 256.0 + B) / 2.55);
case FUEL_AIR_COMMANDED_EQUIVALENCE_RATE:
return (2.0 * (A * 256.0 + B) / 65536.0);
case ABSOLUTE_EVAP_SYSTEM_VAPOR_PRESSURE:
return ((A * 256.0 + B) / 200.0);
case 0x54:
return ((A * 256.0 + B) - 32767.0);
case FUEL_INJECTION_TIMING:
return (((A * 256.0 + B) / 128.0) - 210.0);
case ENGINE_FUEL_RATE:
return ((A * 256.0 + B) / 20.0);
}
}
String OBD2Class::vinRead()
{
char vin[18];
memset(vin, 0x00, sizeof(vin));
if (!pidRead(0x09, 0x02, vin, 17)) {
// failed
return "";
}
return vin;
}
uint32_t OBD2Class::pidReadRaw(uint8_t pid)
{
if (!pidSupported(pid)) {
return 0;
}
#define A value[0]
#define B value[1]
#define C value[2]
#define D value[3]
uint8_t value[4];
if (!pidRead(0x01, pid, &value, sizeof(value))) {
return 0;
}
switch (pid) {
case COMMANDED_SECONDARY_AIR_STATUS:
case OBD_STANDARDS_THIS_VEHICLE_CONFORMS_TO:
case OXYGEN_SENSORS_PRESENT_IN_2_BANKS:
case OXYGEN_SENSORS_PRESENT_IN_4_BANKS:
case AUXILIARY_INPUT_STATUS:
case FUEL_TYPE:
case EMISSION_REQUIREMENT_TO_WHICH_VEHICLE_IS_DESIGNED:
return (A);
case FUEL_SYSTEM_STATUS:
return ((uint32_t)A << 8 | (uint32_t)B);
default:
return ((uint32_t)A << 24 | (uint32_t)B << 16 | (uint32_t)C << 8 | (uint32_t)D);
}
}
String OBD2Class::ecuNameRead()
{
char ecuName[21];
memset(ecuName, 0x00, sizeof(ecuName));
if (!pidRead(0x09, 0x0a, ecuName, 20)) {
// failed
return "";
}
return ecuName;
}
void OBD2Class::setTimeout(unsigned long timeout)
{
_responseTimeout = timeout;
}
int OBD2Class::supportedPidsRead()
{
for (int pid = 0x00; pid < 0xe0; pid += 0x20) {
uint8_t value[4];
if (pidRead(0x01, pid, value, sizeof(value)) != 4) {
return 0;
}
#define A value[0]
#define B value[1]
#define C value[2]
#define D value[3]
_supportedPids[pid / 0x20] = ((uint32_t)A << 24 | (uint32_t)B << 16 | (uint32_t)C << 8 | (uint32_t)D << 0);
if ((_supportedPids[pid / 0x20] & 0x00000001) == 0x00000000) {
// no more
break;
}
}
return 1;
}
uint32_t OBD2Class:: supPIDGET(uint8_t offset)
{
return(_supportedPids[offset]);
}
int OBD2Class::clearAllStoredDTC()
{
//Function clears stored Diagnostic Trouble Codes (DTC)
// make sure at least 60 ms have passed since the last response
unsigned long lastResponseDelta = millis() - _lastPidResponseMillis;
if (lastResponseDelta < 60) {
delay(60 - lastResponseDelta);
}
for (int retries = 10; retries > 0; retries--) {
if (_useExtendedAddressing) {
CANmod.beginExtendedPacket(0x18db33f1, 8);
} else {
CANmod.beginPacket(0x7df, 8);
}
CANmod.write(0x00); // number of additional bytes
CANmod.write(0x04); // Mode / Service 4, for clearing DTC
if (CANmod.endPacket()) {
// send success
break;
} else if (retries <= 1) {
return 0;
}
}
return 1;
}
int OBD2Class::pidRead(uint8_t mode, uint8_t pid, void* data, int length)
{
// make sure at least 60 ms have passed since the last response
unsigned long lastResponseDelta = millis() - _lastPidResponseMillis;
if (lastResponseDelta < 60) {
vTaskDelay(60 / portTICK_PERIOD_MS);
}
for (int retries = 10; retries > 0; retries--) {
if (_useExtendedAddressing) {
CANmod.beginExtendedPacket(0x18db33f1, 8);
} else {
CANmod.beginPacket(0x7df, 8);
}
CANmod.write(0x02); // number of additional bytes
CANmod.write(mode);
CANmod.write(pid);
if (CANmod.endPacket()) {
// send success
break;
} else if (retries <= 1) {
return 0;
}
vTaskDelay(1 / portTICK_PERIOD_MS);
}
bool splitResponse = (length > 5);
for (unsigned long start = millis(); (millis() - start) < _responseTimeout;) {
vTaskDelay(1 / portTICK_PERIOD_MS);
if (CANmod.parsePacket() != 0 &&
(splitResponse ? (CANmod.read() == 0x10 && CANmod.read()) : CANmod.read()) &&
(CANmod.read() == (mode | 0x40) && CANmod.read() == pid)) {
_lastPidResponseMillis = millis();
// got a response
if (!splitResponse) {
return CANmod.readBytes((uint8_t*)data, length);
}
int read = CANmod.readBytes((uint8_t*)data, 3);
for (int i = 0; read < length; i++) {
vTaskDelay(60 / portTICK_PERIOD_MS);
// send the request for the next chunk
if (_useExtendedAddressing) {
CANmod.beginExtendedPacket(0x18db33f1, 8);
} else {
CANmod.beginPacket(0x7df, 8);
}
CANmod.write(0x30);
CANmod.endPacket();
// wait for response
while (CANmod.parsePacket() == 0 ||
CANmod.read() != (0x21 + i))
vTaskDelay(5 / portTICK_PERIOD_MS); // correct sequence number
while (CANmod.available()) {
((uint8_t*)data)[read++] = CANmod.read();
}
}
_lastPidResponseMillis = millis();
return read;
}
}
return 0;
}
int OBD2Class::pidReadPUB(uint8_t mode, uint8_t pid, void* data, int length)
{
// make sure at least 60 ms have passed since the last response
unsigned long lastResponseDelta = millis() - _lastPidResponseMillis;
if (lastResponseDelta < 60) {
delay(60 - lastResponseDelta);
}
for (int retries = 10; retries > 0; retries--) {
if (_useExtendedAddressing) {
CANmod.beginExtendedPacket(0x18db33f1, 8);
} else {
CANmod.beginPacket(0x7df, 8);
}
CANmod.write(0x02); // number of additional bytes
CANmod.write(mode);
CANmod.write(pid);
if (CANmod.endPacket()) {
// send success
break;
} else if (retries <= 1) {
return 0;
}
vTaskDelay(1 / portTICK_PERIOD_MS);
}
bool splitResponse = (length > 5);
for (unsigned long start = millis(); (millis() - start) < _responseTimeout;) {
vTaskDelay(1 / portTICK_PERIOD_MS);
if (CANmod.parsePacket() != 0 &&
(splitResponse ? (CANmod.read() == 0x10 && CANmod.read()) : CANmod.read()) &&
(CANmod.read() == (mode | 0x40) && CANmod.read() == pid)) {
_lastPidResponseMillis = millis();
// got a response
if (!splitResponse) {
return CANmod.readBytes((uint8_t*)data, length);
}
int read = CANmod.readBytes((uint8_t*)data, 3);
for (int i = 0; read < length; i++) {
vTaskDelay(60 / portTICK_PERIOD_MS);
// send the request for the next chunk
if (_useExtendedAddressing) {
CANmod.beginExtendedPacket(0x18db33f1, 8);
} else {
CANmod.beginPacket(0x7df, 8);
}
CANmod.write(0x30);
CANmod.endPacket();
// wait for response
while (CANmod.parsePacket() == 0 ||
CANmod.read() != (0x21 + i)) // correct sequence number
vTaskDelay(5 / portTICK_PERIOD_MS);
while (CANmod.available()) {
((uint8_t*)data)[read++] = CANmod.read();
}
}
_lastPidResponseMillis = millis();
return read;
}
}
return 0;
}
OBD2Class OBD2mod;