121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
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
|
|
#import numpy as np
|
|
|
|
# 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)
|
|
filename = input("Please Enter a File Name: ")
|
|
|
|
|
|
# Infinite loop runs until program is closed
|
|
while True:
|
|
# Filters messages so we only get cell voltage data
|
|
PCAN.Reset(PCAN_USBBUS1)
|
|
PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD)
|
|
PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD)
|
|
|
|
# Create empty list for cell voltage message strings
|
|
# Each entry in the list is a message containing the voltage information
|
|
v = []
|
|
# wait 10 seconds to read the cell voltages again
|
|
#time.sleep(10)
|
|
|
|
# For cell voltages we need to request and read 28 additional messages. Each message contains 3 1/2 cell voltages.
|
|
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
|
|
v_message = True
|
|
while v_message:
|
|
MSG = PCAN.Read(PCAN_USBBUS1)
|
|
MSG = MSG[1]
|
|
if MSG.ID == 1979:
|
|
v_message = False
|
|
# For the remaining voltages, the cell voltage data is contained in bytes 1 - 7
|
|
# Concatenate byte strings
|
|
s = "".join(map("{0:08b}".format, MSG.DATA[1:8]))
|
|
# Append them to the list
|
|
v.append(s)
|
|
|
|
vv = process_list(v)
|
|
|
|
# Filter messages for pack voltage and current
|
|
#PCAN.Reset(PCAN_USBBUS1)
|
|
#time.sleep(.005)
|
|
#PCAN.FilterMessages(PCAN_USBBUS1, 475, 475, PCAN_MESSAGE_STANDARD)
|
|
|
|
#time.sleep(.050)
|
|
p_message = True
|
|
while p_message:
|
|
MSG = PCAN.Read(PCAN_USBBUS1)
|
|
MSG = MSG[1]
|
|
if MSG.ID == 475:
|
|
p_message = False
|
|
|
|
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: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:
|
|
pack_current = ~int(pack_current_string, 2) + 1
|
|
else:
|
|
pack_current = float(int(pack_current_string, 2))
|
|
|
|
#pack_current = pack_current/2
|
|
|
|
#PCAN.Reset(PCAN_USBBUS1)
|
|
#time.sleep(.005)
|
|
#PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD)
|
|
|
|
group1_msg = []
|
|
for i in range(0, 5):
|
|
if i == 0:
|
|
PCAN.Write(PCAN_USBBUS1, Messages.request_group1)
|
|
else:
|
|
PCAN.Write(PCAN_USBBUS1, Messages.request_additional_line)
|
|
time.sleep(.014)
|
|
p_message = True
|
|
while p_message:
|
|
MSG = PCAN.Read(PCAN_USBBUS1)
|
|
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
|
|
pack_health = float(int(pack_health_string, 2))/100
|
|
|
|
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
|
|
|
|
report_string = report(vv, pack_voltage, pack_current, pack_health, pack_charge)
|
|
print (report_string)
|
|
with open(filename, "a") as f:
|
|
f.write(report_string)
|
|
f.write('\n')
|
|
|
|
time.sleep(10)
|