From 2688750cafa80debe54001c0a701b63883cc539e Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Thu, 10 Dec 2020 11:12:08 -0800 Subject: [PATCH] Take output filename as cmdline argument, align cell voltage index, added timestamp --- Main.py | 14 ++++++++++++-- WriteToFile.py | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Main.py b/Main.py index d74458c..65f1d93 100644 --- a/Main.py +++ b/Main.py @@ -1,5 +1,7 @@ import Messages import time +import datetime +import argparse from ProcessList import process_list from WriteToFile import report from PCANBasic import * @@ -7,14 +9,20 @@ from PCANBasic import TPCANTimestamp, TPCANMsg #import matplotlib.pyplot as plt #import numpy as np +parser = argparse.ArgumentParser() +parser.add_argument("-o", "--output", help="log file name", required=True) + +args = parser.parse_args() + id_response = 0x7BB id_periodic = 0x1DB +filename = args.output # input("Please Enter a File Name: ") + # Initialize PCAN device and set filter for incoming messages with ID id_response PCAN = PCANBasic() res = PCAN.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K) #PCAN.FilterMessages(PCAN_USBBUS1, id_response, id_response, PCAN_MESSAGE_STANDARD) -filename = input("Please Enter a File Name: ") # Infinite loop runs until program is closed @@ -115,7 +123,9 @@ while True: 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) + tnow = datetime.datetime.now() + + report_string = report(vv, pack_voltage, pack_current, pack_health, pack_charge, tnow.ctime()) print (report_string) with open(filename, "a") as f: f.write(report_string) diff --git a/WriteToFile.py b/WriteToFile.py index 27c1443..f2a80a7 100644 --- a/WriteToFile.py +++ b/WriteToFile.py @@ -1,11 +1,12 @@ -def report (vv, pack_voltage, pack_current, pack_health, pack_charge): +def report (vv, pack_voltage, pack_current, pack_health, pack_charge, timestamp): return ( "\n".join([ - "Cell {} = {:.3f}\t\t\tCell {} = {:.3f}".format(str(i+1), vv[i], str(i+49), vv[i+48]) + "Cell {:2d} = {:.3f}\t\t\tCell {:2d} = {:.3f}".format((i+1), vv[i], (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) + + "\nTime = {}" .format(timestamp) + "\n")