########################################################################### # # 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 ro_pump.py # # @author (last) Dara Navaei # @date (last) 02-Jan-2023 # @author (original) Sean # @date (original) 14-Apr-2020 # ############################################################################ import struct from enum import unique from logging import Logger from .constants import RESET, NO_RESET from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish, DialinEnum from ..utils.checks import check_broadcast_interval_override_ms from ..utils.conversions import integer_to_bytearray, float_to_bytearray @unique class ROPumpStates(DialinEnum): RO_PUMP_OFF_STATE = 0 RO_PUMP_RAMP_UP_TO_TARGET_FLOW_STATE = 1 RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE = 2 RO_PUMP_CONTROL_TO_MAX_PRESSURE_STATE = 3 RO_PUMP_OPEN_LOOP_STATE = 4 class DGROPump(AbstractSubSystem): """ DGROPump Dialysate Generator (DG) Dialin API sub-class for RO pump related commands. """ def __init__(self, can_interface, logger: Logger): """ DGROPump constructor """ 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_RO_PUMP_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_ro_pump_sync) self.target_pressure_psi = 0.0 self.pwm_duty_cycle_pct = 0.0 self.ro_pump_state = 0 self.target_flow_lpm = 0.0 self.feedback_duty_cycle_pct = 0.0 def get_target_pressure(self): """ Gets the target pressure @return: the target pressure (PSI) """ return self.target_pressure_psi def get_target_flow_rate(self): """ Gets the target flow rate (lpm) @return: The measured flow rate (float) """ return self.target_flow_lpm def get_pwm_duty_cycle_pct(self): """ Gets the PWM duty cycle pct @return: The PWM duty cycle pct (float) """ return self.pwm_duty_cycle_pct def get_ro_pump_state(self): """ Gets the RO pump state @return: The state of the RO pump """ return self.ro_pump_state @publish(["target_pressure_psi", "pwm_duty_cycle_pct", "ro_pump_state", "target_flow_lpm", "feedback_duty_cycle_pct"]) def _handler_ro_pump_sync(self, message): """ Handles published ro pump data messages. RO pump data are captured for reference. @param message: published RO pump data message @return: None """ self.target_pressure_psi = struct.unpack(' int: """ Constructs and sends the set RO pump duty cycle message Constraints: Must be logged into DG. @param duty: integer - 1 percentage for duty cycle between 0 and 100 @return: 1 if successful, zero otherwise """ dc = float_to_bytearray(duty/100) payload = dc message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_RO_PUMP_DUTY_CYCLE_OVERRIDE.value, payload=payload) self.logger.debug("RO pump duty cycle set") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug( "RO pump duty cycle set to " + str(duty) + " %" + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_set_ro_flow_rate(self, flow: float) -> int: """ Constructs and sends the RO rate set command. Constraints: Must be logged into DG. @param flow: float - flow rate (in L/min) to set @return: 1 if successful, zero otherwise """ flo = float_to_bytearray(flow) payload = flo message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_SET_RO_PUMP_TARGET_FLOW.value, payload=payload) self.logger.debug("Set RO pump target flow rate") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: str_res = str(flo) self.logger.debug( "Target RO flow rate set to " + str_res + " L/min: " + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_ro_pump_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the RO pump set point override command Constraints: Must be logged into DG. Given interval must be non-zero and a multiple of the DG general task interval (50 ms). @param ms: integer - time interval (in ms) to broadcast RO pump data @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ if not check_broadcast_interval_override_ms(ms): return False rst = integer_to_bytearray(reset) mis = integer_to_bytearray(ms) payload = rst + mis message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_RO_PUMP_SEND_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("Override RO pump data broadcast interval") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: # self.logger.debug(received_message) if reset == RESET: str_res = "reset back to normal" else: str_res = str(mis) self.logger.debug( "RO pump data broadcast interval overridden to " + str_res + " ms: " + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_ro_pump_measured_feedback_pwm_override(self, pwm: float, reset: int = NO_RESET) -> int: """ Constructs and sends the feedback pwm override command. Constraints: Must be logged into DG. Given RPM must be within 0.0 <= PWM <= 0.99 @param pwm: (float) pwm to override with @param reset: (int) 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) pwm = float_to_bytearray(pwm) payload = rst + pwm message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_RO_FEEDBACK_VOLTAGE_OVERRIDE.value, payload=payload) self.logger.debug("Override RO Pump measured Feedback PWM.") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "back to normal" else: str_res = str(pwm) self.logger.debug( "Feedback PWM has been overridden " + str_res + " % " + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False