clean up, set Boolean value to flags and inlining 0 padding

This commit is contained in:
Yeongdo Park 2020-10-06 14:45:22 -07:00
parent becc97f6c7
commit d74063a650

110
Main.py
View file

@ -30,28 +30,20 @@ while True:
# pause # pause
time.sleep(.014) time.sleep(.014)
# Read the first message # Read the first message
v_message = 0 v_message = True
while v_message != 1: while v_message:
MSG = PCAN.Read(PCAN_USBBUS1) MSG = PCAN.Read(PCAN_USBBUS1)
MSG = MSG[1] MSG = MSG[1]
if MSG.ID == 1979: if MSG.ID == 1979:
v_message = 1 v_message = False
# MSG[1] is the data we are requesting # 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 # 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 # 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]) s4 = "{0:08b}".format(MSG.DATA[4])
while len(s4) != 8: s5 = "{0:08b}".format(MSG.DATA[5])
s4 = "0" + s4 s6 = "{0:08b}".format(MSG.DATA[6])
s5 = "{0:b}".format(MSG.DATA[5]) s7 = "{0:08b}".format(MSG.DATA[7])
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 # Concatenate all the strings
s = s4 + s5 + s6 + s7 s = s4 + s5 + s6 + s7
# Add them to the list of voltage messages # Add them to the list of voltage messages
@ -64,34 +56,20 @@ while True:
# Wait 18 ms # Wait 18 ms
time.sleep(.014) time.sleep(.014)
# Read the next message # Read the next message
v_message = 0 v_message = True
while v_message != 1: while v_message:
MSG = PCAN.Read(PCAN_USBBUS1) MSG = PCAN.Read(PCAN_USBBUS1)
MSG = MSG[1] MSG = MSG[1]
if MSG.ID == 1979: if MSG.ID == 1979:
v_message = 1 v_message = False
# For the remaining voltages, the cell voltage data is contained in bytes 1 - 7 # For the remaining voltages, the cell voltage data is contained in bytes 1 - 7
s1 = "{0:b}".format(int(MSG.DATA[1])) s1 = "{0:08b}".format(int(MSG.DATA[1]))
while len(s1) != 8: s2 = "{0:08b}".format(int(MSG.DATA[2]))
s1 = "0" + s1 s3 = "{0:08b}".format(MSG.DATA[3])
s2 = "{0:b}".format(int(MSG.DATA[2])) s4 = "{0:08b}".format(MSG.DATA[4])
while len(s2) != 8: s5 = "{0:08b}".format(MSG.DATA[5])
s2 = "0" + s2 s6 = "{0:08b}".format(MSG.DATA[6])
s3 = "{0:b}".format(MSG.DATA[3]) s7 = "{0:08b}".format(MSG.DATA[7])
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 # Concatenate byte strings
s = s1 + s2 + s3 + s4 + s5 + s6 + s7 s = s1 + s2 + s3 + s4 + s5 + s6 + s7
# Append them to the list # Append them to the list
@ -113,29 +91,21 @@ while True:
#PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD) #PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD)
#time.sleep(.050) #time.sleep(.050)
p_message = 0 p_message = True
while p_message != 1: while p_message:
MSG = PCAN.Read(PCAN_USBBUS1) MSG = PCAN.Read(PCAN_USBBUS1)
MSG = MSG[1] MSG = MSG[1]
if MSG.ID == 475: if MSG.ID == 475:
p_message = 1 p_message = False
voltage_byte1 = "{0:b}".format(MSG.DATA[2]) voltage_byte1 = "{0:08b}".format(MSG.DATA[2])
while len(voltage_byte1) != 8: voltage_byte2 = "{0:08b}".format(MSG.DATA[3])
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 = voltage_byte1 + voltage_byte2
pack_voltage_string = pack_voltage_string[0:9] pack_voltage_string = pack_voltage_string[0:9]
pack_voltage = float(int(pack_voltage_string, 2)) pack_voltage = float(int(pack_voltage_string, 2))
current_byte1 = "{0:b}".format(MSG.DATA[0]) current_byte1 = "{0:08b}".format(MSG.DATA[0])
while len(current_byte1) != 8: current_byte2 = "{0:08b}".format(MSG.DATA[1])
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 = current_byte1 + current_byte2
pack_current_string = pack_current_string[0:10] pack_current_string = pack_current_string[0:10]
if pack_current_string[0] == 1: if pack_current_string[0] == 1:
@ -158,39 +128,29 @@ while True:
PCAN.Write(PCAN_USBBUS1, Messages.request_group1) PCAN.Write(PCAN_USBBUS1, Messages.request_group1)
time.sleep(.014) time.sleep(.014)
p_message = 0 p_message = True
while p_message != 1: while p_message:
MSG = PCAN.Read(PCAN_USBBUS1) MSG = PCAN.Read(PCAN_USBBUS1)
MSG = MSG[1] MSG = MSG[1]
if MSG.ID == 1979: if MSG.ID == 1979:
p_message = 1 p_message = False
for i in range(0, 4): for i in range(0, 4):
PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line) PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line)
time.sleep(.014) time.sleep(.014)
p_message = 0 p_message = True
while p_message != 1: while p_message:
MSG = PCAN.Read(PCAN_USBBUS1) MSG = PCAN.Read(PCAN_USBBUS1)
MSG = MSG[1] MSG = MSG[1]
if MSG.ID == 1979: if MSG.ID == 1979:
p_message = 1 p_message = False
health_byte1 = "{0:b}".format(MSG.DATA[2]) health_byte1 = "{0:08b}".format(MSG.DATA[2])
health_byte2 = "{0:b}".format(MSG.DATA[3]) health_byte2 = "{0:08b}".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_string = health_byte1 + health_byte2
pack_health = float(int(pack_health_string, 2))/100 pack_health = float(int(pack_health_string, 2))/100
charge_byte1 = "{0:b}".format(MSG.DATA[5]) charge_byte1 = "{0:08b}".format(MSG.DATA[5])
charge_byte2 = "{0:b}".format(MSG.DATA[6]) charge_byte2 = "{0:08b}".format(MSG.DATA[6])
charge_byte3 = "{0:b}".format(MSG.DATA[7]) charge_byte3 = "{0:08b}".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 charge_string = charge_byte1 + charge_byte2 + charge_byte3
pack_charge = float(int(charge_string, 2))/10000 pack_charge = float(int(charge_string, 2))/10000