Last active
September 16, 2015 05:53
-
-
Save taylor224/f69b895f7fe01fe3f354 to your computer and use it in GitHub Desktop.
ECU Data Recording System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import obd | |
import json | |
import time | |
import os | |
import sys | |
from obd import OBDCommand | |
from obd.utils import unhex | |
def rpm(_hex): | |
v = unhex(_hex) # helper function to convert hex to int | |
v = v / 4.0 | |
return (v, obd.Unit.RPM) | |
def speed(_hex): | |
v = unhex(_hex) | |
return (v, obd.Unit.KPH) | |
commFastRPM = OBDCommand("RPM", "Engine RPM", "01", "0C1", 2, rpm) | |
commFastSpeed = OBDCommand("SPEED", "Speed", "01", "0D1", 1, speed) | |
print '=============================================' | |
print 'OBD Vehicle ECU Data Recording System' | |
print '' | |
print 'version 0.0.1' | |
print '=============================================' | |
print 'System Initializing...' | |
obd.debug.console = False | |
dataindex = 0 | |
for i in range(100): | |
if not os.path.exists('data' + str(i) + '.json'): | |
print 'Data File Location : data' + str(i) + '.json' | |
dataindex = i | |
break | |
if i is 100: | |
print 'Data File is FULL. Please Back up and Delete Data Files' | |
print 'System Halt' | |
sys.exit(1) | |
f = open('data' + str(dataindex) + '.json', 'w') | |
print 'Connecting to Vehicle ECU' | |
#connection = obd.OBD() | |
#ports = obd.scanSerial() | |
#print ports | |
#connection = obd.OBD('/dev/rfcomm0') | |
# For Async Connection | |
connection = obd.Async('/dev/rfcomm0') | |
connection.watch(commFastSpeed, force=True) | |
connection.watch(commFastRPM, force=True) | |
#connection.watch(obd.commands.RUN_TIME) | |
connection.start() | |
if connection.is_connected(): | |
print 'Link SUCCESSED with Vehicle ECU' | |
else: | |
print 'Link FAIL with Vehicle ECU' | |
print 'FATAL ERROR' | |
print 'System Halt' | |
sys.exit(1) | |
print 'ECU Connection Initializing...' | |
time.sleep(1) | |
print '' | |
print '==================================================' | |
print 'Data from ECU' | |
print '' | |
print '- Engine -' | |
print 'RPM : ' + str(connection.query(obd.commands.RPM).value) | |
print 'Throttle Position : ' + str(connection.query(obd.commands.THROTTLE_POS).value) | |
print 'Uptime : ' + str(connection.query(obd.commands.RUN_TIME).value) | |
print 'Fuel Type : ' + str(connection.query(obd.commands.FUEL_TYPE).value) | |
print '- Device -' | |
print 'Accel Pedal Position : ' + str(connection.query(obd.commands.RELATIVE_ACCEL_POS).value) | |
print 'Fuel System Status : ' + str(connection.query(obd.commands.FUEL_STATUS).value) | |
print 'Status : ' + str(connection.query(obd.commands.STATUS).value) | |
print '==================================================' | |
print '' | |
print '' | |
print 'Recording Start in 7 Second' | |
print 'test' | |
print connection.query(commFastRPM).value | |
print 'test2' | |
print connection.query(commFastRPM).value | |
print 'test3' | |
print connection.query(commFastRPM).value | |
time.sleep(7) | |
starttime = time.time() | |
prevspeed = 0 | |
prevrpm = 0 | |
print 'Recording Start' | |
try: | |
while(True): | |
nowtime = time.time() | |
difftime = nowtime - starttime | |
speeddata = connection.query(commFastSpeed).value | |
rpmdata = connection.query(commFastRPM).value | |
#runtimedata = connection.query(obd.commands.RUN_TIME).value | |
#accelposdata = connection.query(obd.commands.RELATIVE_ACCEL_POS).value | |
if speeddata == prevspeed and rpmdata == prevrpm: | |
continue | |
print speeddata | |
f.write(str(difftime) + '=' + str(speeddata) + '=' + str(rpmdata) + '\n') | |
print 'time : ' + str(difftime) + '\nSpeed : ' + str(speeddata) + '\nRPM : ' + str(rpmdata) + '\n' | |
prevspeed = speeddata | |
prevrpm = rpmdata | |
time.sleep(0.001) | |
except (KeyboardInterrupt, SystemExit): | |
print 'System Shutdown Requested' | |
print 'Saving Data File' | |
f.close() | |
print 'Data File Saved' | |
print 'System Halt' | |
sys.exit(0) | |
except Exception, e: | |
print 'FATAL ERROR OCCURRED - ' + str(e) | |
print 'Saving Data File' | |
f.close() | |
print 'Data File Saved' | |
print 'System Halt' | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Data