diff --git a/obd/UnitsAndScaling.py b/obd/UnitsAndScaling.py index b49650b1..e5236156 100644 --- a/obd/UnitsAndScaling.py +++ b/obd/UnitsAndScaling.py @@ -134,6 +134,8 @@ def __call__(self, _bytes): 0x3F: UAS(False, 0.01, Unit.liter), 0x40: UAS(False, 1, Unit.ppm), 0x41: UAS(False, 0.01, Unit.microampere), + 0x42: UAS(False, 0.1, Unit.kilometer), + 0x43: UAS(False, 1, Unit.newton * Unit.meter), # signed ----------------------------------------- 0x81: UAS(True, 1, Unit.count), diff --git a/obd/commands.py b/obd/commands.py index 9162f1c8..79a53380 100644 --- a/obd/commands.py +++ b/obd/commands.py @@ -150,6 +150,24 @@ OBDCommand("FUEL_INJECT_TIMING" , "Fuel injection timing" , b"015D", 4, inject_timing, ECU.ENGINE, True), OBDCommand("FUEL_RATE" , "Engine fuel rate" , b"015E", 4, fuel_rate, ECU.ENGINE, True), OBDCommand("EMISSION_REQ" , "Designed emission requirements" , b"015F", 3, drop, ECU.ENGINE, True), + + # name description cmd bytes decoder ECU fast + OBDCommand("PIDS_D" , "Supported PIDs [61-80]" , b"0160", 6, pid, ECU.ENGINE, True), + OBDCommand("DEMAND_PERCENT_TORQUE" , "Driver's demand engine - percent torque" , b"0161", 3, percent_torque, ECU.ENGINE, True), + OBDCommand("ACTUAL_PERCENT_TORQUE" , "Actual engine - percent torque" , b"0162", 3, percent_torque, ECU.ENGINE, True), + OBDCommand("REF_TORQUE" , "Engine reference torque" , b"0163", 4, uas(0x43), ECU.ENGINE, True), + OBDCommand("PERCENT_TORQUE_DATA" , "Engine percent torque data" , b"0164", 4, drop, ECU.ENGINE, True), + + # name description cmd bytes decoder ECU fast + OBDCommand("PIDS_E" , "Supported PIDs [81-A0]" , b"0180", 6, pid, ECU.ENGINE, True), + OBDCommand("THROTTLE_POS_G" , "Throttle position G" , b"018D", 3, percent, ECU.ENGINE, True), + OBDCommand("FRICTION_PERCENT_TORQUE" , "Engine Friction - Percent Torque" , b"018E", 3, percent_torque, ECU.ENGINE, True), + + # name description cmd bytes decoder ECU fast + OBDCommand("PIDS_F" , "Supported PIDs [A1-C0]" , b"01A0", 6, pid, ECU.ENGINE, True), + OBDCommand("ODOMETER" , "Distance traveled" , b"01A6", 3, uas(0x42), ECU.ENGINE, True), + + ] # mode 2 is the same as mode 1, but returns values from when the DTC occured diff --git a/obd/decoders.py b/obd/decoders.py index fffca50a..9c03c434 100644 --- a/obd/decoders.py +++ b/obd/decoders.py @@ -115,6 +115,13 @@ def percent_centered(messages): return v * Unit.percent +def percent_torque(messages): # to be tested ~omar + d = messages[0].data[2:] # implement this to decode pid 64 as well. If $64 not needed, probably this function can be replaced with an entry in the UAS dict. + v = d[0] + v = v - 125 + return v * Unit.percent + + # -40 to 215 C def temp(messages): d = messages[0].data[2:] diff --git a/obd/protocols/protocol_can.py b/obd/protocols/protocol_can.py index ee2c3528..33dc13ea 100644 --- a/obd/protocols/protocol_can.py +++ b/obd/protocols/protocol_can.py @@ -263,8 +263,11 @@ def parse_message(self, message): # [] # 43 03 11 11 22 22 33 33 # [DTC] [DTC] [DTC] + try: + num_dtc_bytes = message.data[1] * 2 # each DTC is 2 bytes + except: + num_dtc_bytes = 0 - num_dtc_bytes = message.data[1] * 2 # each DTC is 2 bytes message.data = message.data[:(num_dtc_bytes + 2)] # add 2 to account for mode/DTC_count bytes return True diff --git a/obd2.py b/obd2.py new file mode 100644 index 00000000..0b9c389a --- /dev/null +++ b/obd2.py @@ -0,0 +1,54 @@ +import serial +import obd +import ast +import sys +import time +import argparse +import traceback +from logging import raiseExceptions +sys.path.append('/opt/iotistic-mnvr/pyscripts/') +from network_utils import run +from db_util import * + +parser = argparse.ArgumentParser() +# parser.add_argument("-r", "--request", type=str, help="requested data") +args = parser.parse_args() + + +def connect_to_OBD(): + while True: + try: + serialport = '/dev/ttyUSB0' if 'Future' in run('udevadm info /dev/ttyUSB0') else '/dev/ttyUSB4' + connection = obd.OBD(serialport) + return connection + except: + traceback.print_exc() + continue + +def init_obd_db(): + obd_db = Database('/home/mnvr/Data/obd.db') # GPS_DB SETUP + obd_db.create_table("obd_data") + obd_db.empty_table("obd_data") + return obd_db + +if __name__=='__main__': + connection = connect_to_OBD() + obd_db = init_obd_db() + time.sleep(0.5) + while True: + try: + res = connection.query(obd.commands['GET_DTC']) + response = ast.literal_eval(str(res)) + print(f'insert into obd_data (candtc) VALUES ("{response}")') + # print(response) + if (str(response)== 'None'): + raise Exception('OBD Connection is not available.') + obd_db.send_query(f'insert into obd_data (candtc) VALUES ("{response}")') + time.sleep(2) + + except: + traceback.print_exc() + connection.close() + time.sleep(1) + connection = connect_to_OBD() + continue \ No newline at end of file