from email
This commit is contained in:
parent
a0a55c2ae3
commit
3ddaf455e3
4 changed files with 93 additions and 53 deletions
85
Main.py
85
Main.py
|
|
@ -1,37 +1,86 @@
|
||||||
import Messages
|
import Messages
|
||||||
import time
|
import time
|
||||||
from ProcessList import processList
|
from ProcessList import process_list
|
||||||
from PCANBasic import *
|
from PCANBasic import *
|
||||||
from PCANBasic import TPCANTimestamp, TPCANMsg
|
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)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
# Infinite loop runs until program is closed
|
||||||
while True:
|
while True:
|
||||||
print("new")
|
print("new")
|
||||||
|
# Create empty list for cell voltage message strings
|
||||||
|
# Each entry in the list is a message containing the voltage information
|
||||||
v = []
|
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)
|
PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit)
|
||||||
time.sleep(.01)
|
# pause
|
||||||
|
time.sleep(.018)
|
||||||
|
# Read the first message
|
||||||
MSG = PCAN.Read(PCAN_USBBUS1)
|
MSG = PCAN.Read(PCAN_USBBUS1)
|
||||||
|
# MSG[1] is the data we are requesting
|
||||||
MSG = MSG[1]
|
MSG = MSG[1]
|
||||||
print(MSG.ID)
|
# Bytes 4, 5, 6, and 7 store the first two cell voltages
|
||||||
s = "%s%s%s%s" % (MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7])
|
# For each byte we need to create a binary string and make sure they are 8 bits long
|
||||||
v.append(int(s))
|
s4 = "{0:b}".format(MSG.DATA[4])
|
||||||
print(v)
|
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):
|
# For cell voltages we need to request and read 28 additional messages. Each message contains 3 1/2 cell voltages.
|
||||||
print("*************** i = #%s " % i)
|
for i in range(1, 29):
|
||||||
time.sleep(.001)
|
# Request a new messages
|
||||||
PCAN.Write(PCAN_USBBUS1, Messages.rCellV)
|
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 = PCAN.Read(PCAN_USBBUS1)
|
||||||
MSG = MSG[1]
|
MSG = MSG[1]
|
||||||
print(MSG.ID)
|
# For the remaining voltages, the cell voltage data is contained in bytes 1 - 7
|
||||||
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])
|
s1 = "{0:b}".format(int(MSG.DATA[1]))
|
||||||
v.append(int(s))
|
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)
|
vv = process_list(v)
|
||||||
print(len(v))
|
|
||||||
process_list(v)
|
|
||||||
10
Messages.py
10
Messages.py
|
|
@ -8,11 +8,11 @@ rCellVInit.LEN = 8
|
||||||
rCellVInit.DATA[0] = 0x02
|
rCellVInit.DATA[0] = 0x02
|
||||||
rCellVInit.DATA[1] = 0x21
|
rCellVInit.DATA[1] = 0x21
|
||||||
rCellVInit.DATA[2] = 0x02
|
rCellVInit.DATA[2] = 0x02
|
||||||
rCellVInit.DATA[3] = 0x00
|
rCellVInit.DATA[3] = 0xff
|
||||||
rCellVInit.DATA[4] = 0x00
|
rCellVInit.DATA[4] = 0xff
|
||||||
rCellVInit.DATA[5] = 0x00
|
rCellVInit.DATA[5] = 0xff
|
||||||
rCellVInit.DATA[6] = 0x00
|
rCellVInit.DATA[6] = 0xff
|
||||||
rCellVInit.DATA[7] = 0x00
|
rCellVInit.DATA[7] = 0xff
|
||||||
|
|
||||||
|
|
||||||
rCellV = TPCANMsg()
|
rCellV = TPCANMsg()
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,21 @@
|
||||||
from math import ceil
|
# This is a function to process the list of cell voltage data
|
||||||
|
|
||||||
|
|
||||||
def process_list(v):
|
def process_list(v):
|
||||||
voltages = []
|
# Instantiate an empty string to store all of the voltage data
|
||||||
voltages[0] = v[0] & 0x00000000ffff0000
|
# Creating one long string alleviates the problem of having 1/2 a voltage on each message
|
||||||
voltages[1] = v[1] & 0x000000000000ffff
|
v_string = ""
|
||||||
for i in range(1, 27):
|
# Iterate through the 29 entries of the list
|
||||||
if i % 2 == 1:
|
for i in range(0, 29):
|
||||||
v1 = (v[i] & 0x00ffff0000000000) >> 40
|
# Create a temporary variable and store the next string
|
||||||
v2 = (v[i] & 0x000000ffff000000) >> 20
|
temp = v[i]
|
||||||
v3 = (v[i] & 0x0000000000ffff00) >> 8
|
# Concatenate the temporary string to the end of the long string
|
||||||
vh = v[i] & 0x00000000000000ff
|
v_string = v_string + temp
|
||||||
voltages[ceil(2 + 3.5 * (i - 1))] = v1
|
# Create an empty list to store the voltage of each cell
|
||||||
voltages[ceil(2 + 3.5 * (i - 1) + 1)] = v2
|
voltages = [0.0] * 97
|
||||||
voltages[ceil(2 + 3.5 * (i - 1) + 2)] = v3
|
for i in range(0, 97):
|
||||||
voltages[ceil(2 + 3.5 * (i - 1) + 3)] = vh << 8
|
# Each cell has a 16 bit value. Extract the next 16 bits and convert it into a float
|
||||||
elif i % 2 == 0:
|
voltages[i] = float(int(v_string[(i*16):(i*16+16)], 2))
|
||||||
vh = (v[i] & 0x00ff000000000000) >> 48
|
# Divide by 1000 to get the final value
|
||||||
v1 = (v[i] & 0x0000ffff00000000) >> 28
|
voltages[i] = voltages[i]/10000
|
||||||
v2 = (v[i] & 0x00000000ffff0000) >> 16
|
# Print the voltage of the cell to the console
|
||||||
v3 = v[i] & 0x000000000000ffff
|
print("Cell " + str(i+1) + " = %.3f" % voltages[i])
|
||||||
voltages[ceil(2 + 3.5 * (i - 1) - 1)] = voltages[ceil(2 + 3.5 * (i -1) - 1)] & vh
|
return voltages
|
||||||
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
|
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,4 @@ plotVoltages(x)
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
plt.clf()
|
plt.clf()
|
||||||
x = [4,3,2,1]
|
x = [4,3,2,1]
|
||||||
plotVoltages(x)
|
vv = plotVoltages(x)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue