diff --git a/BmsApp.py b/BmsApp.py index 61c7543..c6f80f8 100644 --- a/BmsApp.py +++ b/BmsApp.py @@ -13,19 +13,19 @@ from enum import Enum import can import cantools +from BmsView import BmsMonitorFrame + db = cantools.database.load_file('res/EBUS_Host.dbc') nModule = 22 nCell = 12 -from BmsView import BmsMonitorFrame - def parse_can_id(arbitration_id): - mtag = (arbitration_id & 0xfffff000) >> 12 + mprefix = (arbitration_id & 0xfffff000) >> 12 mid = (arbitration_id & 0xf00) >> 8 mtype = arbitration_id & 0xff - return mtag, mid, mtype + return mprefix, mid, mtype BMS_MSG_CAT = Enum( @@ -40,44 +40,51 @@ BMS_MSG_CAT = Enum( ) -def findCategory(imsg, isignal): - tag, id, type = parse_can_id(imsg) +def find_category(imsg, isignal): + msg_prefix, master_id, msg_type = parse_can_id(imsg) - if tag != 0x14180: + if msg_prefix != 0x14180: return BMS_MSG_CAT.ERROR - if 0x00 <= type <= 0x06: + if 0x00 <= msg_type <= 0x06: return BMS_MSG_CAT.BMS_PROFILE - elif 0x07 <= type <= 0x0B: + elif 0x07 <= msg_type <= 0x0B: return BMS_MSG_CAT.MODULE_DATA - elif type == 0x10: + elif msg_type == 0x10: if isignal < 4: return BMS_MSG_CAT.PACK_PROFILE else: return BMS_MSG_CAT.PACK_ALARM - elif type == 0x11: + elif msg_type == 0x11: return BMS_MSG_CAT.PACK_PROFILE - elif type == 0x14: + elif msg_type == 0x14: return BMS_MSG_CAT.PACK_DATA - elif type == 0x15: + elif msg_type == 0x15: if isignal < 3: return BMS_MSG_CAT.PACK_DATA else: return BMS_MSG_CAT.PACK_ALARM - elif 0x16 <= type <= 0x20: + elif 0x16 <= msg_type <= 0x20: return BMS_MSG_CAT.PACK_DATA else: return BMS_MSG_CAT.UNDEFINED -cat_propgrid = [ +pgrid_category = [ BMS_MSG_CAT.BMS_PROFILE, BMS_MSG_CAT.PACK_PROFILE, BMS_MSG_CAT.PACK_ALARM, BMS_MSG_CAT.PACK_DATA ] -tohex = "{:#04X}-".format +str_hex = "{:#04X}-".format + +class BmsListener (can.Listener): + def __init__(self, parent): + self.parent = parent + + def on_message_received(self, msg): + print("{:#X}".format(msg.arbitration_id)) class Controller: @@ -85,12 +92,27 @@ class Controller: self.view = v self.model = m - pub.subscribe(lambda: "dummy function", "dummy message") + pub.subscribe(self.model.start_can, "model.start") + pub.subscribe(self.model.stop_can, "model.stop") class Model: def __init__(self): self.battery_module_state = [{} for i in range(nModule)] + self.bus = None + self.notifier = None + self.listen_bms = can.Printer() + #self.listen_bms = BmsListener(self) + + def start_can(self): + self.bus = can.Bus() + self.notifier = can.Notifier(self.bus, [self.listen_bms]) + + def stop_can(self): + self.notifier.stop() + self.bus.shutdown() + self.notifier = None + self.bus = None def process_message(self, msg): pass @@ -108,7 +130,7 @@ class BmsMonitorView(BmsMonitorFrame): self.property_grid_page_info = self.property_grid_1.AddPage("Info") - for c in cat_propgrid: + for c in pgrid_category: cat_name = c.name cat = wx.propgrid.PropertyCategory(label=cat_name) self.property_grid_page_info.Append(cat) @@ -117,46 +139,39 @@ class BmsMonitorView(BmsMonitorFrame): # self.property_grid_page_info.AppendIn("packdata", bms_model) for msg in db.messages: - tag, id, type = parse_can_id(msg.frame_id) + prefix, id, type = parse_can_id(msg.frame_id) if id == 0x02: - if findCategory(msg.frame_id, 0) == BMS_MSG_CAT.BMS_PROFILE: + if find_category(msg.frame_id, 0) == BMS_MSG_CAT.BMS_PROFILE: prop = wx.propgrid.StringProperty(label=msg.name) self.property_grid_page_info.AppendIn(BMS_MSG_CAT.BMS_PROFILE.name, prop) for isig, signal in enumerate(msg.signals): - plabel = tohex(type) + signal.name + plabel = str_hex(type) + signal.name prop = wx.propgrid.StringProperty(label=plabel) prop.SetHelpString(signal.comment) - cat = findCategory(msg.frame_id, isig) + cat = find_category(msg.frame_id, isig) - if cat in cat_propgrid: + if cat in pgrid_category: self.property_grid_page_info.AppendIn(cat.name, prop) - ''' - if tag == 0x14180 and id == 0x2 and 0x00 <= type <= 0x06: # BMS Profile - self.property_grid_page_info.AppendIn(BMS_MSG_CAT.BMS_PROFILE, prop) - - if tag == 0x14180 and id == 0x2 and 0x10 <= type <= 0x20: # Pack Profile and Data - self.property_grid_page_info.AppendIn("packdata", prop) - ''' def OnToggle(self, event): btnLabel = self.button_1.GetLabel() if self.button_1.Value: - print("starting timer...") - self.bus = can.Bus() + # self.bus = can.Bus() - self.timer.Start(10) + # self.timer.Start(10) + pub.sendMessage ("model.start") self.button_1.SetLabel("Stop") else: - print("timer stopped!") - self.bus.shutdown() - self.bus = None + # self.bus.shutdown() + # self.bus = None - self.timer.Stop() + # self.timer.Stop() + pub.sendMessage ("model.stop") self.button_1.SetLabel("Start") event.Skip() @@ -178,13 +193,13 @@ class BmsMonitorView(BmsMonitorFrame): # print("Read CAN Message!") msg = self.bus.recv(timeout=1.) full_id = msg.arbitration_id - mtag, mid, mtype = parse_can_id(msg.arbitration_id) + msg_prefix, master_id, msg_type = parse_can_id(msg.arbitration_id) db_msg = db.get_message_by_frame_id(msg.arbitration_id) - master_id = int(self.choice_1.GetStringSelection(), 16) + id_chosen = int(self.choice_1.GetStringSelection(), 16) - if mid == master_id: - if findCategory(full_id, 0) == BMS_MSG_CAT.MODULE_DATA: + if master_id == id_chosen: + if find_category(full_id, 0) == BMS_MSG_CAT.MODULE_DATA: try: decoded = db.decode_message(msg.arbitration_id, msg.data) except KeyError as key: @@ -192,7 +207,7 @@ class BmsMonitorView(BmsMonitorFrame): else: self.__update_bm_state(decoded) - elif findCategory(full_id, 0) == BMS_MSG_CAT.BMS_PROFILE: + elif find_category(full_id, 0) == BMS_MSG_CAT.BMS_PROFILE: try: print("bmsprofile message raw", db_msg.name, msg.data, msg.data.decode()) except UnicodeDecodeError: @@ -203,10 +218,10 @@ class BmsMonitorView(BmsMonitorFrame): else: decoded = db.decode_message(msg.arbitration_id, msg.data) for isig, signal in enumerate(db_msg.signals): - plabel = tohex(mtype) + signal.name - cat = findCategory(full_id, isig) + plabel = str_hex(msg_type) + signal.name + cat = find_category(full_id, isig) - if cat in cat_propgrid: + if cat in pgrid_category: if self.property_grid_page_info.GetProperty(plabel): # print (plabel, decoded[signal.name]) value = float(decoded[signal.name]) if signal.is_float else decoded[signal.name] @@ -215,11 +230,6 @@ class BmsMonitorView(BmsMonitorFrame): pass # print (plabel, " is Not Available") - ''' - 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)