nissan-leaf-reader/Main.py

128 lines
4.1 KiB
Python

import time
import datetime
import argparse
from argparse import ArgumentParser
import PycanMessages as Messages
from ProcessList import process_list
from WriteToFile import report
import can
parser: ArgumentParser = argparse.ArgumentParser()
parser.add_argument("-c", "--channel",
help=('The serial device to open. '
'For example "/dev/ttyS1" or "/dev/ttyUSB0" on Linux '
'or "COM1" on Windows systems.'),
required=True)
parser.add_argument("-o", "--output", help="log file name", required=True)
args = parser.parse_args()
can.rc['interface'] = 'seeedstudio'
can.rc['channel'] = args.channel
can.rc['bitrate'] = 500000
id_response = 0x7BB
id_periodic = 0x1DB
id_mask11 = 0x7FF
voltage_filters = [
{"can_id": id_response, "can_mask": id_mask11, "extended": False},
{"can_id": id_periodic, "can_mask": id_mask11, "extended": False},
]
filename = args.output # input("Please Enter a File Name: ")
# Initialize CAN device and set filter for incoming messages with ID id_response
bus = can.Bus()
bus.set_filters(voltage_filters)
# Infinite loop runs until program is closed
while True:
# Create empty list for cell voltage message strings
# Each entry in the list is a message containing the voltage information
v = []
# For cell voltages we need to request and read 28 additional messages. Each message contains 3 1/2 cell voltages.
for i in range(0, 29):
if i == 0:
# Send message to request cell voltage data
bus.send(Messages.rCellVInit)
else:
# Request a new messages
bus.send(Messages.rCellV)
# Wait 18 ms
time.sleep(.014)
# Read the next message
v_message = True
while v_message:
MSG = bus.recv()
if MSG.arbitration_id == id_response:
v_message = False
# For the remaining voltages, the cell voltage data is contained in bytes 1 - 7
# Concatenate byte strings
s = "".join(map("{0:08b}".format, MSG.data))
# Append them to the list
v.append(s)
vv = process_list(v)
p_message = True
while p_message:
MSG = bus.recv()
if MSG.arbitration_id == id_periodic:
p_message = False
voltage_byte1 = "{0:08b}".format(MSG.data[2])
voltage_byte2 = "{0:08b}".format(MSG.data[3])
pack_voltage_string = voltage_byte1 + voltage_byte2
pack_voltage_string = pack_voltage_string[0:9]
pack_voltage = float(int(pack_voltage_string, 2))
current_byte1 = "{0:08b}".format(MSG.data[0])
current_byte2 = "{0:08b}".format(MSG.data[1])
pack_current_string = current_byte1 + current_byte2
pack_current_string = pack_current_string[0:10]
if pack_current_string[0] == 1:
pack_current = ~int(pack_current_string, 2) + 1
else:
pack_current = float(int(pack_current_string, 2))
group1_msg = []
for i in range(0, 5):
if i == 0:
bus.send(Messages.request_group1)
else:
bus.send(Messages.request_additional_line)
time.sleep(.014)
p_message = True
while p_message:
MSG = bus.recv()
if MSG.arbitration_id == id_response:
p_message = False
group1_msg.append(MSG)
health_byte1 = "{0:08b}".format(MSG.data[2])
health_byte2 = "{0:08b}".format(MSG.data[3])
pack_health_string = health_byte1 + health_byte2
pack_health = float(int(pack_health_string, 2))/100
charge_byte1 = "{0:08b}".format(MSG.data[5])
charge_byte2 = "{0:08b}".format(MSG.data[6])
charge_byte3 = "{0:08b}".format(MSG.data[7])
charge_string = charge_byte1 + charge_byte2 + charge_byte3
pack_charge = float(int(charge_string, 2))/10000
tnow = datetime.datetime.now()
report_string = report(vv, pack_voltage, pack_current, pack_health, pack_charge, tnow.ctime())
print(report_string)
with open(filename, "a") as f:
f.write(report_string)
f.write('\n')
# wait 10 seconds to read the cell voltages again
time.sleep(10)