Inlining loops and merge output formatting
This commit is contained in:
parent
d74063a650
commit
05c5019b2f
3 changed files with 34 additions and 63 deletions
72
Main.py
72
Main.py
|
|
@ -1,6 +1,7 @@
|
|||
import Messages
|
||||
import time
|
||||
from ProcessList import process_list
|
||||
from WriteToFile import report
|
||||
from PCANBasic import *
|
||||
from PCANBasic import TPCANTimestamp, TPCANMsg
|
||||
#import matplotlib.pyplot as plt
|
||||
|
|
@ -25,34 +26,15 @@ while True:
|
|||
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 = True
|
||||
while v_message:
|
||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
v_message = False
|
||||
# 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:08b}".format(MSG.DATA[4])
|
||||
s5 = "{0:08b}".format(MSG.DATA[5])
|
||||
s6 = "{0:08b}".format(MSG.DATA[6])
|
||||
s7 = "{0:08b}".format(MSG.DATA[7])
|
||||
# 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)
|
||||
for i in range(0, 29):
|
||||
if i == 0 :
|
||||
# Send message to request cell voltage data
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit)
|
||||
else:
|
||||
# Request a new messages
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.rCellV)
|
||||
# Wait 18 ms
|
||||
time.sleep(.014)
|
||||
# Read the next message
|
||||
|
|
@ -63,27 +45,12 @@ while True:
|
|||
if MSG.ID == 1979:
|
||||
v_message = False
|
||||
# For the remaining voltages, the cell voltage data is contained in bytes 1 - 7
|
||||
s1 = "{0:08b}".format(int(MSG.DATA[1]))
|
||||
s2 = "{0:08b}".format(int(MSG.DATA[2]))
|
||||
s3 = "{0:08b}".format(MSG.DATA[3])
|
||||
s4 = "{0:08b}".format(MSG.DATA[4])
|
||||
s5 = "{0:08b}".format(MSG.DATA[5])
|
||||
s6 = "{0:08b}".format(MSG.DATA[6])
|
||||
s7 = "{0:08b}".format(MSG.DATA[7])
|
||||
# Concatenate byte strings
|
||||
s = s1 + s2 + s3 + s4 + s5 + s6 + s7
|
||||
s = "".join(map("{0:08b}".format, MSG.DATA[1:8]))
|
||||
# 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)
|
||||
|
|
@ -115,17 +82,12 @@ while True:
|
|||
|
||||
#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)
|
||||
|
||||
group1_msg = []
|
||||
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.request_group1)
|
||||
time.sleep(.014)
|
||||
p_message = True
|
||||
|
|
@ -134,6 +96,7 @@ while True:
|
|||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
p_message = False
|
||||
group1_msg.append(MSG)
|
||||
for i in range(0, 4):
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line)
|
||||
time.sleep(.014)
|
||||
|
|
@ -143,6 +106,7 @@ while True:
|
|||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
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
|
||||
|
|
@ -154,10 +118,10 @@ while True:
|
|||
charge_string = charge_byte1 + charge_byte2 + charge_byte3
|
||||
pack_charge = float(int(charge_string, 2))/10000
|
||||
|
||||
print("State of Health = %.2f %%" % pack_health)
|
||||
report_string = report(vv, pack_voltage, pack_current, pack_health, pack_charge)
|
||||
print (report_string)
|
||||
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)
|
||||
f.write(report_string)
|
||||
f.write('\n')
|
||||
|
||||
time.sleep(10)
|
||||
|
|
|
|||
|
|
@ -1,21 +1,17 @@
|
|||
# This is a function to process the list of cell voltage data
|
||||
def process_list(v):
|
||||
# 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
|
||||
# Discard first 3 bytes of the first message since it has only 2 voltages
|
||||
v_string = "".join(v[0:29])[3*8:]
|
||||
|
||||
# Create an empty list to store the voltage of each cell
|
||||
voltages = [0.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]/1000
|
||||
voltages[i] = voltages[i]/1000.
|
||||
# Print the voltage of the cell to the console
|
||||
#print("Cell " + str(i+1) + " = %.3f" % voltages[i])
|
||||
return voltages
|
||||
|
|
|
|||
11
WriteToFile.py
Normal file
11
WriteToFile.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def report (vv, pack_voltage, pack_current, pack_health, pack_charge):
|
||||
return (
|
||||
"\n".join([
|
||||
"Cell {} = {:.3f}\t\t\tCell {} = {:.3f}".format(str(i+1), vv[i], str(i+49), vv[i+48])
|
||||
for i in range(48)]) +
|
||||
"\nCell Voltage Sum = {:.2f}".format(sum(vv)) +
|
||||
"\nPack Voltage = {:.2f} V" .format(pack_voltage) +
|
||||
"\nPack Current = {:.2f} A" .format(pack_current) +
|
||||
"\nState of Health = {:.2f} %" .format(pack_health) +
|
||||
"\nState of Charge = {:.2f} %" .format(pack_charge) +
|
||||
"\n")
|
||||
Loading…
Add table
Reference in a new issue