Skip to content

Commit cb5ccc1

Browse files
author
Brendan Whitfield
committed
organized sensors by mode
1 parent d48697c commit cb5ccc1

3 files changed

Lines changed: 115 additions & 69 deletions

File tree

‎obd/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
from obd import OBD
3-
from obd_sensors import sensors
4-
from obd_utils import scanSerial
3+
from commands import sensors, specials, commands
4+
from utils import scanSerial

‎obd/commands.py‎

Lines changed: 97 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,25 @@
2929

3030

3131
class OBDCommand():
32-
def __init__(self, sensorname, desc, cmd, pid, returnBytes, decoder):
32+
def __init__(self, sensorname, desc, mode, pid, pid, returnBytes, decoder):
3333
self.sensorname = sensorname
3434
self.desc = desc
35-
self.cmd = cmd
35+
self.mode = mode
36+
self.pid = pid
3637
self.bytes = returnBytes # number of bytes expected in return
3738
self.decode = decoder
3839

40+
def clone(self):
41+
return OBDCommand(self.sensorname,
42+
self.desc,
43+
self.mode,
44+
self.pid,
45+
self.bytes,
46+
self.decode)
47+
48+
def getCommand(self):
49+
return self.mode + self.pid
50+
3951
def compute(result):
4052
if "NODATA" in result:
4153
return ""
@@ -50,53 +62,86 @@ def compute(result):
5062

5163

5264
# note, the SENSOR NAME field will be used as the dict key for that sensor
53-
# if no sensor name is given, it will be treated as a special command
54-
commands = [
55-
# sensor name description cmd bytes decoder
56-
OBDCommand("PIDS" , "Supported PIDs" , "0100" , 4, noop ),
57-
OBDCommand("STATUS" , "Status since DTCs cleared" , "0101" , 4, noop ),
58-
OBDCommand("FREEZE_DTC" , "Freeze DTC" , "0102" , 2, noop ),
59-
OBDCommand("FUEL_STATUS" , "Fuel System Status" , "0103" , 2, noop ),
60-
OBDCommand("LOAD" , "Calculated Engine Load" , "0104" , 1, percent ),
61-
OBDCommand("COOLANT_TEMP" , "Engine Coolant Temperature" , "0105" , 1, temp ),
62-
OBDCommand("SHORT_FUEL_TRIM_1" , "Short Term Fuel Trim - Bank 1" , "0106" , 1, percent_centered ),
63-
OBDCommand("Long_FUEL_TRIM_1" , "Long Term Fuel Trim - Bank 1" , "0107" , 1, percent_centered ),
64-
OBDCommand("SHORT_FUEL_TRIM_2" , "Short Term Fuel Trim - Bank 2" , "0108" , 1, percent_centered ),
65-
OBDCommand("LONG_FUEL_TRIM_2" , "Long Term Fuel Trim - Bank 2" , "0109" , 1, percent_centered ),
66-
OBDCommand("FUEL_PRESSURE" , "Fuel Pressure" , "010A" , 1, fuel_pressure ),
67-
OBDCommand("INTAKE_PRESSURE" , "Intake Manifold Pressure" , "010B" , 1, intake_pressure ),
68-
OBDCommand("RPM" , "Engine RPM" , "010C" , 2, rpm ),
69-
OBDCommand("SPEED" , "Vehicle Speed" , "010D" , 1, speed ),
70-
OBDCommand("TIMING_ADVANCE" , "Timing Advance" , "010E" , 1, timing_advance ),
71-
OBDCommand("INTAKE_TEMP" , "Intake Air Temp" , "010F" , 1, temp ),
72-
OBDCommand("MAF" , "Air Flow Rate (MAF)" , "0110" , 2, maf ),
73-
OBDCommand("THROTTLE" , "Throttle Position" , "0111" , 1, percent ),
74-
OBDCommand("AIR_STATUS" , "Secondary Air Status" , "0112" , 1, noop ),
75-
OBDCommand("O2_SENSORS" , "O2 Sensors Present" , "0113" , 1, noop ),
76-
OBDCommand("O2_B1S1" , "O2: Bank 1 - Sensor 1" , "0114" , 2, sensor_voltage ),
77-
OBDCommand("O2_B1S2" , "O2: Bank 1 - Sensor 2" , "0115" , 2, sensor_voltage ),
78-
OBDCommand("O2_B1S3" , "O2: Bank 1 - Sensor 3" , "0116" , 2, sensor_voltage ),
79-
OBDCommand("O2_B1S4" , "O2: Bank 1 - Sensor 4" , "0117" , 2, sensor_voltage ),
80-
OBDCommand("O2_B2S1" , "O2: Bank 2 - Sensor 1" , "0118" , 2, sensor_voltage ),
81-
OBDCommand("O2_B2S2" , "O2: Bank 2 - Sensor 2" , "0119" , 2, sensor_voltage ),
82-
OBDCommand("O2_B2S3" , "O2: Bank 2 - Sensor 3" , "011A" , 2, sensor_voltage ),
83-
OBDCommand("O2_B2S4" , "O2: Bank 2 - Sensor 4" , "011B" , 2, sensor_voltage ),
84-
OBDCommand("OBD_STANDARDS" , "OBD Standards Compliance" , "011C" , 1, noop ),
85-
OBDCommand("O2_SENSORS_ALT" , "O2 Sensors Present (alternate)" , "011D" , 1, noop ),
86-
OBDCommand("AUX_INPUT_STATUS" , "Auxiliary input status" , "011E" , 1, noop ),
87-
OBDCommand("RUN_TIME" , "Engine Run Time" , "011F" , 2, sec_to_min ),
88-
#OBDCommand("RUN_TIME_MIL" , "Engine Run Time MIL" , "014D" , sec_to_min ),
89-
90-
91-
# DTC handling
92-
# sensor name description cmd bytes decoder
93-
OBDCommand("GET_DTC" , "Get DTCs" , "03" , 0, noop ),
94-
OBDCommand("CLEAR_DTC" , "Clear DTCs" , "04" , 0, noop ),
95-
OBDCommand("GET_FREEZE_DTC" , "Get Freeze DTCs" , "07" , 0, noop ),
65+
66+
__mode1__ = [
67+
# mode 1
68+
# sensor name description mode cmd bytes decoder
69+
OBDCommand("PIDS" , "Supported PIDs" , "01", "00" , 4, noop ),
70+
OBDCommand("STATUS" , "Status since DTCs cleared" , "01", "01" , 4, noop ),
71+
OBDCommand("FREEZE_DTC" , "Freeze DTC" , "01", "02" , 2, noop ),
72+
OBDCommand("FUEL_STATUS" , "Fuel System Status" , "01", "03" , 2, noop ),
73+
OBDCommand("LOAD" , "Calculated Engine Load" , "01", "04" , 1, percent ),
74+
OBDCommand("COOLANT_TEMP" , "Engine Coolant Temperature" , "01", "05" , 1, temp ),
75+
OBDCommand("SHORT_FUEL_TRIM_1" , "Short Term Fuel Trim - Bank 1" , "01", "06" , 1, percent_centered ),
76+
OBDCommand("Long_FUEL_TRIM_1" , "Long Term Fuel Trim - Bank 1" , "01", "07" , 1, percent_centered ),
77+
OBDCommand("SHORT_FUEL_TRIM_2" , "Short Term Fuel Trim - Bank 2" , "01", "08" , 1, percent_centered ),
78+
OBDCommand("LONG_FUEL_TRIM_2" , "Long Term Fuel Trim - Bank 2" , "01", "09" , 1, percent_centered ),
79+
OBDCommand("FUEL_PRESSURE" , "Fuel Pressure" , "01", "0A" , 1, fuel_pressure ),
80+
OBDCommand("INTAKE_PRESSURE" , "Intake Manifold Pressure" , "01", "0B" , 1, intake_pressure ),
81+
OBDCommand("RPM" , "Engine RPM" , "01", "0C" , 2, rpm ),
82+
OBDCommand("SPEED" , "Vehicle Speed" , "01", "0D" , 1, speed ),
83+
OBDCommand("TIMING_ADVANCE" , "Timing Advance" , "01", "0E" , 1, timing_advance ),
84+
OBDCommand("INTAKE_TEMP" , "Intake Air Temp" , "01", "0F" , 1, temp ),
85+
OBDCommand("MAF" , "Air Flow Rate (MAF)" , "01", "10" , 2, maf ),
86+
OBDCommand("THROTTLE" , "Throttle Position" , "01", "11" , 1, percent ),
87+
OBDCommand("AIR_STATUS" , "Secondary Air Status" , "01", "12" , 1, noop ),
88+
OBDCommand("O2_SENSORS" , "O2 Sensors Present" , "01", "13" , 1, noop ),
89+
OBDCommand("O2_B1S1" , "O2: Bank 1 - Sensor 1" , "01", "14" , 2, sensor_voltage ),
90+
OBDCommand("O2_B1S2" , "O2: Bank 1 - Sensor 2" , "01", "15" , 2, sensor_voltage ),
91+
OBDCommand("O2_B1S3" , "O2: Bank 1 - Sensor 3" , "01", "16" , 2, sensor_voltage ),
92+
OBDCommand("O2_B1S4" , "O2: Bank 1 - Sensor 4" , "01", "17" , 2, sensor_voltage ),
93+
OBDCommand("O2_B2S1" , "O2: Bank 2 - Sensor 1" , "01", "18" , 2, sensor_voltage ),
94+
OBDCommand("O2_B2S2" , "O2: Bank 2 - Sensor 2" , "01", "19" , 2, sensor_voltage ),
95+
OBDCommand("O2_B2S3" , "O2: Bank 2 - Sensor 3" , "01", "1A" , 2, sensor_voltage ),
96+
OBDCommand("O2_B2S4" , "O2: Bank 2 - Sensor 4" , "01", "1B" , 2, sensor_voltage ),
97+
OBDCommand("OBD_STANDARDS" , "OBD Standards Compliance" , "01", "1C" , 1, noop ),
98+
OBDCommand("O2_SENSORS_ALT" , "O2 Sensors Present (alternate)" , "01", "1D" , 1, noop ),
99+
OBDCommand("AUX_INPUT_STATUS" , "Auxiliary input status" , "01", "1E" , 1, noop ),
100+
OBDCommand("RUN_TIME" , "Engine Run Time" , "01", "1F" , 2, sec_to_min ),
101+
#OBDCommand("RUN_TIME_MIL" , "Engine Run Time MIL" , "01", "4D" , sec_to_min ),
102+
]
103+
104+
# mode 2 is the same as mode 1, but returns values from when the DTC occured
105+
__mode2__ = []
106+
for c in __mode1__:
107+
c = c.clone()
108+
c.mode = "02"
109+
c.name = "DTC_" + c.name
110+
c.desc = "DTC " + c.desc
111+
__mode2__.append(c)
112+
113+
114+
__mode3__ = [
115+
# sensor name description mode cmd bytes decoder
116+
OBDCommand("GET_DTC" , "Get DTCs" , "03", "" , 0, noop ),
117+
]
118+
119+
__mode4__ = [
120+
# sensor name description mode cmd bytes decoder
121+
OBDCommand("CLEAR_DTC" , "Clear DTCs" , "04", "" , 0, noop ),
122+
]
123+
124+
__mode7__ [
125+
# sensor name description mode cmd bytes decoder
126+
OBDCommand("GET_FREEZE_DTC" , "Get Freeze DTCs" , "07", "" , 0, noop ),
96127
]
97128

98129

99130

131+
132+
# assemble the commands by mode
133+
commands = [
134+
[],
135+
__mode1__,
136+
__mode2__,
137+
__mode3__,
138+
__mode4__,
139+
[],
140+
[],
141+
__mode7__
142+
]
143+
144+
100145
class sensors():
101146
pass
102147

@@ -105,9 +150,10 @@ class specials():
105150

106151

107152
# allow sensor commands to be accessed by name
108-
for command in commands:
109-
# if the command has no decoder, it is considered a special
110-
if command.decode == noop:
111-
specials.__dict__[command.sensorname] = command
112-
else:
113-
sensors.__dict__[command.sensorname] = command
153+
for m in commands:
154+
for c in m:
155+
# if the command has no decoder, it is considered a special
156+
if c.decode == noop:
157+
specials.__dict__[c.sensorname] = c
158+
else:
159+
sensors.__dict__[c.sensorname] = c

‎obd/obd.py‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import time
44

5-
from obd_port import State
6-
from obd_port import OBDPort
7-
from obd_sensors import sensors
5+
from port import OBDPort, State
6+
from commands import sensors, specials, commands
87
from obd_utils import scanSerial
98

109

@@ -14,7 +13,7 @@ class OBD():
1413

1514
def __init__(self, portstr=None):
1615
self.port = None
17-
self.supportedSensors = []
16+
self.supportedCommands = []
1817

1918
# initialize by connecting and loading sensors
2019
if self.connect(portstr):
@@ -51,35 +50,36 @@ def get_port_name(self):
5150
def load_sensors(self):
5251
""" queries for available sensors, and compiles lists of indices and sensor objects """
5352

54-
self.supportedSensors = []
53+
self.supportedCommands = []
5554

5655
# Find supported sensors - by getting PIDs from OBD (sensor zero)
5756
# its a string of binary 01010101010101
5857
# 1 means the sensor is supported
59-
supported = self.valueOf(sensors.PIDS)
58+
supported = self.sendCommand(commands[1][0]) # mode 01, command 00
6059

61-
count = min(len(supported), len(sensors.by_PID))
60+
count = min(len(supported), len(commands[1]))
6261

6362
# loop through PIDs binary
6463
for i in range(count):
6564
if supported[i] == "1":
66-
sensor = sensors.by_PID[i]
67-
sensor.supported = True
68-
self.supportedSensors.append(sensor)
65+
c = commands[1][i]
66+
c.supported = True
67+
self.supportedCommands.append(c)
6968

7069

71-
def printSensors(self):
72-
for sensor in self.supportedSensors:
73-
print str(sensor)
70+
def printCommands(self):
71+
for c in self.supportedCommands:
72+
print str(c)
7473

75-
def hasSensor(self, sensor):
76-
return sensor.supported
74+
def hasCommand(self, c):
75+
return c.supported
7776

78-
def valueOf(self, sensor):
77+
def sendCommand(self, command):
7978
return self.port.get_sensor_value(sensor)
8079

8180

8281

82+
8383
if __name__ == "__main__":
8484

8585
o = OBD()

0 commit comments

Comments
 (0)