import Messages import time from ProcessList import process_list from PCANBasic import * from PCANBasic import TPCANTimestamp, TPCANMsg #import matplotlib.pyplot as plt #import numpy as np # Initialize PCAN device and set filter for incoming messages with ID 1979 PCAN = PCANBasic() res = PCAN.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K) #PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD) filename = input("Please Enter a File Name: ") # Infinite loop runs until program is closed while True: # Filters messages so we only get cell voltage data PCAN.Reset(PCAN_USBBUS1) PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD) PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD) # Create empty list for cell voltage message strings # Each entry in the list is a message containing the voltage information v = [] # wait 10 seconds to read the cell voltages again #time.sleep(10) # Send message to request cell voltage data PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit) # pause time.sleep(.014) # Read the first message v_message = 0 while v_message != 1: MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] if MSG.ID == 1979: v_message = 1 # MSG[1] is the data we are requesting #MSG = MSG[1] # Bytes 4, 5, 6, and 7 store the first two cell voltages # For each byte we need to create a binary string and make sure they are 8 bits long s4 = "{0:b}".format(MSG.DATA[4]) while len(s4) != 8: s4 = "0" + s4 s5 = "{0:b}".format(MSG.DATA[5]) while len(s5) != 8: s5 = "0" + s5 s6 = "{0:b}".format(MSG.DATA[6]) while len(s6) != 8: s6 = "0" + s6 s7 = "{0:b}".format(MSG.DATA[7]) while len(s7) != 8: s7 = "0" + s7 # Concatenate all the strings s = s4 + s5 + s6 + s7 # Add them to the list of voltage messages v.append(s) # For cell voltages we need to request and read 28 additional messages. Each message contains 3 1/2 cell voltages. for i in range(1, 29): # Request a new messages PCAN.Write(PCAN_USBBUS1, Messages.rCellV) # Wait 18 ms time.sleep(.014) # Read the next message v_message = 0 while v_message != 1: MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] if MSG.ID == 1979: v_message = 1 # For the remaining voltages, the cell voltage data is contained in bytes 1 - 7 s1 = "{0:b}".format(int(MSG.DATA[1])) while len(s1) != 8: s1 = "0" + s1 s2 = "{0:b}".format(int(MSG.DATA[2])) while len(s2) != 8: s2 = "0" + s2 s3 = "{0:b}".format(MSG.DATA[3]) while len(s3) != 8: s3 = "0" + s3 s4 = "{0:b}".format(MSG.DATA[4]) while len(s4) != 8: s4 = "0" + s4 s5 = "{0:b}".format(MSG.DATA[5]) while len(s5) != 8: s5 = "0" + s5 s6 = "{0:b}".format(MSG.DATA[6]) while len(s6) != 8: s6 = "0" + s6 s7 = "{0:b}".format(MSG.DATA[7]) while len(s7) != 8: s7 = "0" + s7 # Concatenate byte strings s = s1 + s2 + s3 + s4 + s5 + s6 + s7 # Append them to the list v.append(s) vv = process_list(v) for i in range(0, 48): print("Cell " + str(i+1) + " = %.3f" % vv[i] + "\t\t\tCell " + str(i + 49) + " = %.3f" % vv[i+48]) with open(filename, "a") as f: f.write("\nCell " + str(i+1) + " = %.3f" % vv[i] + "\t\t\tCell " + str(i + 49) + " = %.3f" % vv[i+48]) print("Cell Voltage Sum = %.2f" % sum(vv)) with open(filename, "a") as f: f.write("\nCell Voltage Sum = %.2f" % sum(vv)) # Filter messages for pack voltage and current #PCAN.Reset(PCAN_USBBUS1) #time.sleep(.005) #PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD) #time.sleep(.050) p_message = 0 while p_message != 1: MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] if MSG.ID == 475: p_message = 1 voltage_byte1 = "{0:b}".format(MSG.DATA[2]) while len(voltage_byte1) != 8: voltage_byte1 = "0" + voltage_byte1 voltage_byte2 = "{0:b}".format(MSG.DATA[3]) while len(voltage_byte2) != 8: voltage_byte2 = "0" + voltage_byte2 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:b}".format(MSG.DATA[0]) while len(current_byte1) != 8: current_byte1 = "0" + current_byte1 current_byte2 = "{0:b}".format(MSG.DATA[1]) while len(current_byte2) != 8: current_byte2 = "0" + current_byte2 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)) #pack_current = pack_current/2 print("Pack Voltage = %.2f V" % pack_voltage) with open(filename, "a") as f: f.write("\nPack Voltage = %.2f V" % pack_voltage) print("Pack Current = %.2f A" % pack_current) with open(filename, "a") as f: f.write("\nPack Current = %.2f A" % pack_current) #PCAN.Reset(PCAN_USBBUS1) #time.sleep(.005) #PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD) PCAN.Write(PCAN_USBBUS1, Messages.request_group1) time.sleep(.014) p_message = 0 while p_message != 1: MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] if MSG.ID == 1979: p_message = 1 for i in range(0, 4): PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line) time.sleep(.014) p_message = 0 while p_message != 1: MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] if MSG.ID == 1979: p_message = 1 health_byte1 = "{0:b}".format(MSG.DATA[2]) health_byte2 = "{0:b}".format(MSG.DATA[3]) while len(health_byte1) != 8: health_byte1 = "0" + health_byte1 while len(health_byte2) != 8: health_byte2 = "0" + health_byte2 pack_health_string = health_byte1 + health_byte2 pack_health = float(int(pack_health_string, 2))/100 charge_byte1 = "{0:b}".format(MSG.DATA[5]) charge_byte2 = "{0:b}".format(MSG.DATA[6]) charge_byte3 = "{0:b}".format(MSG.DATA[7]) while len(charge_byte1) != 8: charge_byte1 = "0" + charge_byte1 while len(charge_byte2) != 8: charge_byte2 = "0" + charge_byte2 while len(charge_byte3) != 8: charge_byte3 = "0" + charge_byte3 charge_string = charge_byte1 + charge_byte2 + charge_byte3 pack_charge = float(int(charge_string, 2))/10000 print("State of Health = %.2f %%" % pack_health) with open(filename, "a") as f: f.write("\nState of Health = %.2f %%" % pack_health) print("State of Charge = %.2f %%" % pack_charge) with open(filename, "a") as f: f.write("\nState of Charge = %.2f %%" % pack_charge) time.sleep(10)