-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobd-ii-logger.py
executable file
·257 lines (174 loc) · 7.55 KB
/
obd-ii-logger.py
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
#!/usr/bin/python3
#
# ---=== simple data logging for OBD-II sensor PIDs ===---
#
# This file is part of https://github.com/ogressel/obd-ii-logger
# Copyright (c) 2021 by Oliver Gressel <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
from h5py import File
from time import time, sleep
from datetime import datetime, timedelta
import obd
from obd import OBD, OBDCommand
from obd.protocols import ECU
from timeloop import Timeloop
# --- utility functions -------------------------------------------------------
_float = lambda val: 0. if val=="" else float(val)
Signed = lambda val: (val & 0xff) - (1<<8) if val >= (1<<7) else val
# --- OBD-II sensor class (matching TorquePro-style CSV file) -----------------
class obd_sensors:
name = "" # long name
nm = "" # short name
pid = 0 # mode + PID
eqn = "" # algebraic expression for unpacking values
minv = 0 # smallest possible value
maxv = 0 # biggest possible value
unit = "" # physical unit descriptor
hdr = 0 # OBD-II header
tms = 0 # time series of data
nbt = 0 # number of bytes returned
cmd = 0 # query command object
dset = 0 # h5py dataset
# --- constructor
def __init__(self, value = []):
self.name = value[0]
self.nm = value[1].replace(' ', '_')
self.pid = bytes(value[2],encoding='ascii')
self.eqn = value[3]
self.minv = _float(value[4])
self.maxv = _float(value[5])
self.unit = value[6]
self.hdr = bytes(value[7],encoding='ascii')
self.tms = []
self.dset = 0
self.nbt=0
if "A" in self.eqn: self.nbt=1
if "B" in self.eqn: self.nbt=2
if "C" in self.eqn: self.nbt=3
if "D" in self.eqn: self.nbt=4
self.cmd = OBDCommand( self.nm, # name
self.name, # description
self.pid, # command
0, # number of return bytes to expect
decode_pid, # decoding function
ECU.UNKNOWN, # (opt) ECU filter
True, # (opt) "01" may be added for speed
self.hdr ) # (opt) custom PID header
# --- print object values
def __repr__(self):
return \
"OBD sensor object:" \
" name='%s', nm='%s', pid='%s', eqn='%s'," \
" minv='%g', maxv='%g', unit='%s', hdr='%s'\n" \
% ( self.name, self.nm, hex(self.pid), self.eqn,
self.minv, self.maxv, self.unit, hex(self.hdr) )
# --- callback method for data accumulation
def accumulate(self,result):
self.tms.append( [ time()-start_time, result.value ] )
# --- routine for appending HDF5 datasets -------------------------------------
tloop = Timeloop()
@tloop.job(interval=timedelta(seconds=60))
def append_hdf5_file_every_60s():
for pid,sensor in my_obd_sensors.items():
if(len(sensor.tms)==0):
print("DEBUG:: skipping empty time series '%s'" % sensor.nm)
continue
else:
print("DEBUG:: saving time series '%s' with %d elements"
% (sensor.nm, len(sensor.tms)))
print("DEBUG:: values are %s" % sensor.tms)
# --- write data to HDF5 file
if not any( val==None for (_,val) in sensor.tms ):
if not sensor.nm in f_id.keys(): # create dataset
sensor.dset = f_id.create_dataset( sensor.nm,
data=sensor.tms,
dtype='f8',
maxshape=(None,2) )
else: # append to dataset
new = len(sensor.tms)
sensor.dset.resize(sensor.dset.shape[0]+new, axis=0)
sensor.dset[-new:] = sensor.tms
# --- flush file and purge data
print("DEBUG:: dataset shape is ", sensor.dset.shape)
f_id.flush()
sensor.tms = []
# --- callback function for decoding messages ---------------------------------
def decode_pid(messages):
""" generic decoder function for OBD-II messages """
data = messages[0].frames[0].raw # operate on a single message/frame
pid = bytearray(data[5:11],encoding='ascii');
pid[0] -= 4 # remove acknowledgement flag
pid = bytes(pid) # make hashable
print("DEBUG:: decoding message '%s' of type %s, assuming pid=%s"
% (data,type(data), pid))
# lookup expression
try:
sensor = my_obd_sensors[pid]
print("DEBUG:: found PID %s ('%s') with eqn='%s'"
% (sensor.pid,sensor.nm,sensor.eqn))
except KeyError:
print("WARN:: failed to lookup pid=%s" % pid)
return -1
A = int(data[11:13],16) if "A" in sensor.eqn else 0
B = int(data[13:15],16) if "B" in sensor.eqn else 0
print("DEBUG:: fetching values A=%s, B=%s" % (A,B))
result = float( eval( sensor.eqn ) )
print("DEBUG:: evaluated %s = %g" % (sensor.eqn, result))
return result
# --- import the CSV file -----------------------------------------------------
if( len(sys.argv)!=2 ):
print("INFO:: usage is '%s <pid-file.csv>'" % sys.argv[0] )
sys.exit(-1)
my_obd_sensors = {}
with open(sys.argv[1], 'r') as f:
for num,line in enumerate(f):
if (num>0):
sensor = obd_sensors( (line.strip()).split(",") )
if "[" in sensor.eqn:
print("WARN:: skipping compound expression '%s'" % sensor.eqn)
continue
if len(sensor.pid)==4:
print("WARN:: skipping standard PID %s" % sensor.pid)
continue
if sensor.name=="":
print("WARN:: skipping empty entry")
continue
if sensor.pid in my_obd_sensors:
print("WARN:: skipping duplicate PID %s" % sensor.pid)
continue
my_obd_sensors[sensor.pid] = sensor
# --- open HDF5 file ----------------------------------------------------------
f_nm = (datetime.now()).strftime("sensor-readings-%Y.%m.%d-%H:%M:%S.h5")
f_id = File( f_nm, "a" )
# --- main event loop ---------------------------------------------------------
start_time = time()
connection = obd.Async()
connection.unwatch_all()
for pid,sensor in my_obd_sensors.items():
print( "INFO:: adding PID=%s with command '%s', eqn='%s'" %
(sensor.pid, sensor.cmd, sensor.eqn) )
connection.supported_commands.add(sensor.cmd)
connection.watch(sensor.cmd, callback=sensor.accumulate)
tloop.start(block=False) # append HDF5 data
connection.start()
while True:
print('.', end='', flush=True); sleep(0.5)
print()
connection.stop()
connection.unwatch_all()
# --- close the HDF5 data file ------------------------------------------------
f_id.close()
# -----------------------------------------------------------------------------