From 3ddaf455e303926559d78c0b67dbdf7dbe36a806 Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Wed, 30 Sep 2020 12:34:54 -0700 Subject: [PATCH] from email --- Main.py | 85 ++++++++++++++++++++++++++++++++++++++----------- Messages.py | 10 +++--- ProcessList.py | 49 ++++++++++++---------------- plotVoltages.py | 2 +- 4 files changed, 93 insertions(+), 53 deletions(-) diff --git a/Main.py b/Main.py index 1c2a46b..51da418 100644 --- a/Main.py +++ b/Main.py @@ -1,37 +1,86 @@ import Messages import time -from ProcessList import processList +from ProcessList import process_list from PCANBasic import * from PCANBasic import TPCANTimestamp, TPCANMsg +import matplotlib.pyplot as plt +import numpy as np -PCAN = PCANBasic() # type: PCANBasic +# 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) + +# Infinite loop runs until program is closed while True: print("new") + # Create empty list for cell voltage message strings + # Each entry in the list is a message containing the voltage information v = [] - time.sleep(.005) + # 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) - time.sleep(.01) + # pause + time.sleep(.018) + # Read the first message MSG = PCAN.Read(PCAN_USBBUS1) + # MSG[1] is the data we are requesting MSG = MSG[1] - print(MSG.ID) - s = "%s%s%s%s" % (MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) - v.append(int(s)) - print(v) + # 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 i in range(0, 28): - print("*************** i = #%s " % i) - time.sleep(.001) + # 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) - time.sleep(.01) + # Wait 18 ms + time.sleep(.18) + # Read the next message MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] - print(MSG.ID) - s = s + "%s%s%s%s%s%s%s" % (MSG.DATA[1], MSG.DATA[2], MSG.DATA[3], MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) - v.append(int(s)) + # 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) - print(v) - print(len(v)) - process_list(v) \ No newline at end of file + vv = process_list(v) \ No newline at end of file diff --git a/Messages.py b/Messages.py index ed7f15d..8dc5100 100644 --- a/Messages.py +++ b/Messages.py @@ -8,11 +8,11 @@ rCellVInit.LEN = 8 rCellVInit.DATA[0] = 0x02 rCellVInit.DATA[1] = 0x21 rCellVInit.DATA[2] = 0x02 -rCellVInit.DATA[3] = 0x00 -rCellVInit.DATA[4] = 0x00 -rCellVInit.DATA[5] = 0x00 -rCellVInit.DATA[6] = 0x00 -rCellVInit.DATA[7] = 0x00 +rCellVInit.DATA[3] = 0xff +rCellVInit.DATA[4] = 0xff +rCellVInit.DATA[5] = 0xff +rCellVInit.DATA[6] = 0xff +rCellVInit.DATA[7] = 0xff rCellV = TPCANMsg() diff --git a/ProcessList.py b/ProcessList.py index ce4d4d2..9e9efed 100644 --- a/ProcessList.py +++ b/ProcessList.py @@ -1,30 +1,21 @@ -from math import ceil - - +# This is a function to process the list of cell voltage data def process_list(v): - voltages = [] - voltages[0] = v[0] & 0x00000000ffff0000 - voltages[1] = v[1] & 0x000000000000ffff - for i in range(1, 27): - if i % 2 == 1: - v1 = (v[i] & 0x00ffff0000000000) >> 40 - v2 = (v[i] & 0x000000ffff000000) >> 20 - v3 = (v[i] & 0x0000000000ffff00) >> 8 - vh = v[i] & 0x00000000000000ff - voltages[ceil(2 + 3.5 * (i - 1))] = v1 - voltages[ceil(2 + 3.5 * (i - 1) + 1)] = v2 - voltages[ceil(2 + 3.5 * (i - 1) + 2)] = v3 - voltages[ceil(2 + 3.5 * (i - 1) + 3)] = vh << 8 - elif i % 2 == 0: - vh = (v[i] & 0x00ff000000000000) >> 48 - v1 = (v[i] & 0x0000ffff00000000) >> 28 - v2 = (v[i] & 0x00000000ffff0000) >> 16 - v3 = v[i] & 0x000000000000ffff - voltages[ceil(2 + 3.5 * (i - 1) - 1)] = voltages[ceil(2 + 3.5 * (i -1) - 1)] & vh - voltages[ceil(2 + 3.5 * (i - 1))] = v1 - voltages[ceil(2 + 3.5 * (i - 1) + 1)] = v2 - voltages[ceil(2 + 3.5 * (i - 1) + 2)] = v3 - voltages[96] = voltages[96] & ((v[28] & 0x00ff000000000000) >> 48) - for i in range(0, 95): - print("Cell " + str(i + 1) + "= %.2f" % (voltages[i])) - return + # Instantiate an empty string to store all of the voltage data + # Creating one long string alleviates the problem of having 1/2 a voltage on each message + v_string = "" + # Iterate through the 29 entries of the list + for i in range(0, 29): + # Create a temporary variable and store the next string + temp = v[i] + # Concatenate the temporary string to the end of the long string + 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): + # 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 + # Print the voltage of the cell to the console + print("Cell " + str(i+1) + " = %.3f" % voltages[i]) + return voltages diff --git a/plotVoltages.py b/plotVoltages.py index 30aea4f..8966991 100644 --- a/plotVoltages.py +++ b/plotVoltages.py @@ -21,4 +21,4 @@ plotVoltages(x) time.sleep(3) plt.clf() x = [4,3,2,1] -plotVoltages(x) +vv = plotVoltages(x)