clean up, set Boolean value to flags and inlining 0 padding
This commit is contained in:
parent
becc97f6c7
commit
d74063a650
1 changed files with 35 additions and 75 deletions
110
Main.py
110
Main.py
|
|
@ -30,28 +30,20 @@ while True:
|
|||
# pause
|
||||
time.sleep(.014)
|
||||
# Read the first message
|
||||
v_message = 0
|
||||
while v_message != 1:
|
||||
v_message = True
|
||||
while v_message:
|
||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
v_message = 1
|
||||
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: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
|
||||
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
|
||||
|
|
@ -64,34 +56,20 @@ while True:
|
|||
# Wait 18 ms
|
||||
time.sleep(.014)
|
||||
# Read the next message
|
||||
v_message = 0
|
||||
while v_message != 1:
|
||||
v_message = True
|
||||
while v_message:
|
||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
v_message = 1
|
||||
v_message = False
|
||||
# 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
|
||||
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
|
||||
# Append them to the list
|
||||
|
|
@ -113,29 +91,21 @@ while True:
|
|||
#PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD)
|
||||
|
||||
#time.sleep(.050)
|
||||
p_message = 0
|
||||
while p_message != 1:
|
||||
p_message = True
|
||||
while p_message:
|
||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||
MSG = MSG[1]
|
||||
if MSG.ID == 475:
|
||||
p_message = 1
|
||||
p_message = False
|
||||
|
||||
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
|
||||
voltage_byte1 = "{0:08b}".format(MSG.DATA[2])
|
||||
voltage_byte2 = "{0:08b}".format(MSG.DATA[3])
|
||||
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
|
||||
current_byte1 = "{0:08b}".format(MSG.DATA[0])
|
||||
current_byte2 = "{0:08b}".format(MSG.DATA[1])
|
||||
pack_current_string = current_byte1 + current_byte2
|
||||
pack_current_string = pack_current_string[0:10]
|
||||
if pack_current_string[0] == 1:
|
||||
|
|
@ -158,39 +128,29 @@ while True:
|
|||
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.request_group1)
|
||||
time.sleep(.014)
|
||||
p_message = 0
|
||||
while p_message != 1:
|
||||
p_message = True
|
||||
while p_message:
|
||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||
MSG = MSG[1]
|
||||
if MSG.ID == 1979:
|
||||
p_message = 1
|
||||
p_message = False
|
||||
for i in range(0, 4):
|
||||
PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line)
|
||||
time.sleep(.014)
|
||||
p_message = 0
|
||||
while p_message != 1:
|
||||
p_message = True
|
||||
while p_message:
|
||||
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
|
||||
p_message = False
|
||||
health_byte1 = "{0:08b}".format(MSG.DATA[2])
|
||||
health_byte2 = "{0:08b}".format(MSG.DATA[3])
|
||||
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_byte1 = "{0:08b}".format(MSG.DATA[5])
|
||||
charge_byte2 = "{0:08b}".format(MSG.DATA[6])
|
||||
charge_byte3 = "{0:08b}".format(MSG.DATA[7])
|
||||
charge_string = charge_byte1 + charge_byte2 + charge_byte3
|
||||
pack_charge = float(int(charge_string, 2))/10000
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue