From becc97f6c70ea205ddcaf18757d27dadbb1e2d0e Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Tue, 6 Oct 2020 13:23:08 -0700 Subject: [PATCH] from the laptop --- Main.py | 141 ++++++++++++++++++++++++++++++++++++++++++++----- Messages.py | 26 +++++++++ ProcessList.py | 6 +-- 3 files changed, 158 insertions(+), 15 deletions(-) diff --git a/Main.py b/Main.py index 51da418..3c04d3d 100644 --- a/Main.py +++ b/Main.py @@ -3,31 +3,41 @@ import time from ProcessList import process_list from PCANBasic import * from PCANBasic import TPCANTimestamp, TPCANMsg -import matplotlib.pyplot as plt -import numpy as np +#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) +#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: - print("new") + # 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) + #time.sleep(10) # Send message to request cell voltage data PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit) # pause - time.sleep(.018) + time.sleep(.014) # Read the first message - MSG = PCAN.Read(PCAN_USBBUS1) + 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] + #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]) @@ -52,10 +62,14 @@ while True: # Request a new messages PCAN.Write(PCAN_USBBUS1, Messages.rCellV) # Wait 18 ms - time.sleep(.18) + time.sleep(.014) # Read the next message - MSG = PCAN.Read(PCAN_USBBUS1) - MSG = MSG[1] + 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: @@ -83,4 +97,107 @@ while True: # Append them to the list v.append(s) - vv = process_list(v) \ No newline at end of file + + 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) diff --git a/Messages.py b/Messages.py index 8dc5100..0d32e69 100644 --- a/Messages.py +++ b/Messages.py @@ -27,3 +27,29 @@ rCellV.DATA[4] = 0xff rCellV.DATA[5] = 0xff rCellV.DATA[6] = 0xff rCellV.DATA[7] = 0xff + +request_group1 = TPCANMsg() +request_group1.ID = 0x79b +request_group1.MSGTYPE = 0x00 +request_group1.LEN = 8 +request_group1.DATA[0] = 0x02 +request_group1.DATA[1] = 0x21 +request_group1.DATA[2] = 0x01 +request_group1.DATA[3] = 0xff +request_group1.DATA[4] = 0xff +request_group1.DATA[5] = 0xff +request_group1.DATA[6] = 0xff +request_group1.DATA[7] = 0xff + +request_additional_line = TPCANMsg() +request_additional_line.ID = 0x79b +request_additional_line.MSGTYPE = 0x00 +request_additional_line.LEN = 8 +request_additional_line.DATA[0] = 0x30 +request_additional_line.DATA[1] = 0x01 +request_additional_line.DATA[2] = 0x00 +request_additional_line.DATA[3] = 0xff +request_additional_line.DATA[4] = 0xff +request_additional_line.DATA[5] = 0xff +request_additional_line.DATA[6] = 0xff +request_additional_line.DATA[7] = 0xff diff --git a/ProcessList.py b/ProcessList.py index 9e9efed..b5d5576 100644 --- a/ProcessList.py +++ b/ProcessList.py @@ -11,11 +11,11 @@ def process_list(v): v_string = v_string + temp # Create an empty list to store the voltage of each cell voltages = [0.0] * 97 - for i in range(0, 97): + for i in range(0, 96): # Each cell has a 16 bit value. Extract the next 16 bits and convert it into a float voltages[i] = float(int(v_string[(i*16):(i*16+16)], 2)) # Divide by 1000 to get the final value - voltages[i] = voltages[i]/10000 + voltages[i] = voltages[i]/1000 # Print the voltage of the cell to the console - print("Cell " + str(i+1) + " = %.3f" % voltages[i]) + #print("Cell " + str(i+1) + " = %.3f" % voltages[i]) return voltages