########################################################################### # # Copyright (c) 2020-2023 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file reservoirs.py # # @author (last) Dara Navaei # @date (last) 03-Nov-2022 # @author (original) Sean # @date (original) 14-Apr-2020 # ############################################################################ import struct from logging import Logger from enum import unique from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish, DialinEnum from ..utils.conversions import integer_to_bytearray @unique class DGReservoirsNames(DialinEnum): # Reservoir IDs RESERVOIR1 = 0 RESERVOIR2 = 1 class DGReservoirs(AbstractSubSystem): """ DG interface containing reservoir related commands. """ def __init__(self, can_interface, logger: Logger): """ @param can_interface: The DenaliCANMessenger object """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DG_RESERVOIRS_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_reservoirs_sync) self.active_reservoir = 0 self.fill_to_vol_ml = 0 self.drain_to_vol_ml = 0 self.time_reservoir_cycle = 0 self.time_reservoir_fill_2_switch = 0 self.time_uf_decay = 0.0 self.temp_uf_fill = 0.0 self.temp_reservoir_use_actual = 0.0 self.temp_reservoir_end_fill = 0.0 self.temp_avg_fill = 0.0 self.temp_last_fill = 0.0 self.time_rsrvr_fill = 0.0 def get_active_reservoir(self): """ Gets the active reservoir RESERVOIR1 = 0 \n RESERVOIR2 = 1 \n @return: 0 or 1 """ return self.active_reservoir def get_fill_to_vol(self): """ Gets the fill to volume @return: The fill to volume (mL) """ return self.fill_to_vol_ml def get_drain_to_vol(self): """ Gets the drain to volume @return: The drain to volume (mL) """ return self.drain_to_vol_ml def cmd_switch_reservoirs(self, reservoir: int, use_last_trimmer_dc: int = 0) -> bool: """ Sends a command to the DG to switch reservoirs @param reservoir: (int) the new reservoir number @param use_last_trimmer_dc: (int) use last trimmer heater duty cycle (1) or not (0) @return: True if command sent, False if invalid reservoir provided """ if reservoir not in [0, 1]: return False r = integer_to_bytearray(reservoir) dc = integer_to_bytearray(use_last_trimmer_dc) payload = r + dc message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_SWITCH_RESERVOIR_CMD.value, payload=payload) self.logger.debug("Sending command to switch DG reservoir to {0} and use last trimmer heater" "duty cycle to {1}".format(reservoir, use_last_trimmer_dc)) self.can_interface.send(message, 0) return True def cmd_reservoir_tare(self, reservoir: int) -> bool: """ Sends a command to the DG to tare a given reservoir @param reservoir: (int) the ID of the reservoir to tare @return: True if command sent, False if reservoir ID invalid """ if reservoir not in [0, 1]: return False payload = integer_to_bytearray(reservoir) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_RESERVOIR_TARE_REQUEST.value, payload=payload) self.logger.debug("Sending command to tare DG reservoir {0}".format(reservoir)) self.can_interface.send(message, 0) return True @publish(["active_reservoir", "fill_to_vol_ml", "drain_to_vol_ml", "time_reservoir_cycle", "time_reservoir_fill_2_switch", "time_uf_decay", "temp_uf_fill", "temp_reservoir_use_actual", "temp_reservoir_end_fill", "temp_avg_fill", "temp_last_fill", "time_rsrvr_fill"]) def _handler_reservoirs_sync(self, message): """ Handles published reservoir data messages. Reservoir data are captured for reference. @param message: published reservoir data message @return: none """ self.active_reservoir = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.fill_to_vol_ml = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.drain_to_vol_ml = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.time_reservoir_cycle = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.time_reservoir_fill_2_switch = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.time_uf_decay = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.temp_uf_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.temp_reservoir_use_actual = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.temp_reservoir_end_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] self.temp_avg_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] self.temp_last_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] self.time_rsrvr_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0]