From 333fc6f83df197f25d61ec04e3ab4cdbaf74fa22 Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Thu, 6 Aug 2020 15:04:09 -0700 Subject: [PATCH] Parsing module data and bms profile --- BmsApp.py | 108 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 41 deletions(-) diff --git a/BmsApp.py b/BmsApp.py index f105499..6b98715 100644 --- a/BmsApp.py +++ b/BmsApp.py @@ -11,6 +11,9 @@ import cantools db = cantools.database.load_file('res/EBUS_Host.dbc') +nModule = 22 +nCell = 12 + from BmsView import BmsMonitorFrame def parse_can_id (arbitration_id): @@ -52,15 +55,20 @@ def findCategory (imsg, isignal): return "moduledata" else: - print("undefined category ", hex(imsg)) + # print("undefined category ", hex(imsg)) return "undefined" cat_propgrid = ["bmsprofile", "packprofile", "packdata", "packalarms",] +tohex = "{:#04X}-".format + class BmsMonitorView(BmsMonitorFrame): + def __init__(self, *args, **kwds): BmsMonitorFrame.__init__(self, *args, **kwds) + self.battery_module_state = [{} for i in range(nModule)] + self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) @@ -73,7 +81,6 @@ class BmsMonitorView(BmsMonitorFrame): # bms_model = wx.propgrid.StringProperty(label="Model", name="bms_model", value="B3DWER32EW") # self.property_grid_page_info.AppendIn("packdata", bms_model) - tohex = "{:#04X}-".format for msg in db.messages: tag, id, type = parse_can_id(msg.frame_id) @@ -85,10 +92,7 @@ class BmsMonitorView(BmsMonitorFrame): for isig, signal in enumerate(msg.signals): plabel = tohex(type)+signal.name - if signal.is_float: - prop = wx.propgrid.FloatProperty(label=plabel) - else: - prop = wx.propgrid.IntProperty(label=plabel) + prop = wx.propgrid.StringProperty(label=plabel) prop.SetHelpString(signal.comment) cat = findCategory(msg.frame_id, isig) @@ -122,53 +126,75 @@ class BmsMonitorView(BmsMonitorFrame): event.Skip() + def __update_bm_state(self, dmsg): + imodule = dmsg['BatS_Cell_ModuleNo'] + for k in dmsg: + self.battery_module_state[imodule][k] = dmsg[k] + + def GetModuleVoltage(self, module_dict): + keyString='BatS_Cell{}_V' + return [module_dict.get(keyString.format(x), -1) for x in range(nCell)] + + def GetModuleBalState(self, module_dict): + keyString='BatS_Cell{}_BalState' + return [module_dict.get(keyString.format(x), -1) for x in range(nCell)] + def OnTimer(self, event): # print("Read CAN Message!") msg = self.bus.recv(timeout=1.) - mtag, mid, mtype = parse_can_id(msg.frame_id) - if 0x07 <= mtype <= 0x0b: - try: - dmsg = self.candb.decode_message(msg.arbitration_id, msg.data) - except KeyError as key: - print(key, " does not exist") - else: - print(hex(mtag), hex(mid), hex(mtype), msg.data, dmsg) - self.__update_bm_state(dmsg) + fullid = msg.arbitration_id + mtag, mid, mtype = parse_can_id(msg.arbitration_id) + dbmsg = db.get_message_by_frame_id(msg.arbitration_id) - for k in self.battery_module_state[0]: - if self.property_grid_1.GetProperty(k): - self.property_grid_1.SetPropertyValue(k, self.battery_module_state[0][k]) - else: - self.property_grid_1.Append(wx.propgrid.StringProperty(label=k, value=str(self.battery_module_state[0][k]))) - - master_id = int(self.choice_1.GetStringSelection()) + master_id = int(self.choice_1.GetStringSelection(), 16) if mid == master_id: - - if findCategory(msg.frame_id, 0) == "bmsprofile": - prop = wx.propgrid.StringProperty(label=msg.name) - self.property_grid_page_info.AppendIn("bmsprofile", prop) - - for isig, signal in enumerate(msg.signals): - plabel = tohex(type) + signal.name - if signal.is_float: - prop = wx.propgrid.FloatProperty(label=plabel) + if findCategory(fullid, 0) == "moduledata": + try: + dmsg = db.decode_message(msg.arbitration_id, msg.data) + except KeyError as key: + print(key, " does not exist") else: - prop = wx.propgrid.IntProperty(label=plabel) - prop.SetHelpString(signal.comment) + self.__update_bm_state(dmsg) - cat = findCategory(msg.frame_id, isig) + elif findCategory(fullid, 0) == "bmsprofile": + try: + print ("bmsprofile message raw", (dbmsg.name), msg.data, msg.data.decode()) + except UnicodeDecodeError: + print ("bmsprofile message raw", (dbmsg.name), msg.data, int.from_bytes(msg.data, byteorder="big")) - if cat in cat_propgrid: - self.property_grid_page_info.AppendIn(cat, prop) + if self.property_grid_page_info.GetProperty(dbmsg.name): + self.property_grid_page_info.SetPropertyValue(dbmsg.name, msg.data.decode()) + else: + dmsg = db.decode_message(msg.arbitration_id, msg.data) + for isig, signal in enumerate(dbmsg.signals): + plabel = tohex(mtype) + signal.name + cat = findCategory(fullid, isig) + if cat in cat_propgrid: + if self.property_grid_page_info.GetProperty(plabel): + # print (plabel, dmsg[signal.name]) + value = float(dmsg[signal.name]) if signal.is_float else dmsg[signal.name] + self.property_grid_page_info.SetPropertyValue(plabel, value) + else: + pass + # print (plabel, " is Not Available") + + ''' + if tag == 0x14180 and id == 0x2 and 0x00 <= type <= 0x06: # BMS Profile + self.property_grid_page_info.AppendIn("bmsprofile", prop) + + if tag == 0x14180 and id == 0x2 and 0x10 <= type <= 0x20: # Pack Profile and Data + self.property_grid_page_info.AppendIn("packdata", prop) ''' - if tag == 0x14180 and id == 0x2 and 0x00 <= type <= 0x06: # BMS Profile - self.property_grid_page_info.AppendIn("bmsprofile", prop) - if tag == 0x14180 and id == 0x2 and 0x10 <= type <= 0x20: # Pack Profile and Data - self.property_grid_page_info.AppendIn("packdata", prop) - ''' + i_module = self.spin_ctrl_1.GetValue() + module_dict = self.battery_module_state[i_module] + voltages = self.GetModuleVoltage(module_dict) + bal_states = self.GetModuleBalState(module_dict) + for i in range(nCell): + self.grid_1.SetCellValue(i, 0, str(voltages[i])) + self.grid_1.SetCellValue(i, 2, str(bal_states[i])) event.Skip()