########################################################################### # # Copyright (c) 2020-2024 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 boost_pump.py # # @author (last) Micahel Garthwaite # @date (last) 17-Aug-2023 # @author (original) Peter Lucia # @date (original) 02-Apr-2020 # ############################################################################ import struct from logging import Logger from leahi_dialin.utils.base import DialinEnum from .constants import RESET, NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.common.fp_defs import BoostPumpNames, FPPumpAttributes from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels from leahi_dialin.utils.base import AbstractSubSystem, publish, DialinEnum from leahi_dialin.utils.checks import check_broadcast_interval_override_ms from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray class FPBoostPump(AbstractSubSystem): """ Dialin API sub-class for FP Boost pump controller related commands. """ def __init__(self, can_interface, logger: Logger): """ FPBoostPump constructor """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.fp_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_FP_BOOST_PUMP_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_pump_sync) self.boost_pump_timestamp = 0.0 self.boost_pump = { BoostPumpNames.P40_PUMP.name: { FPPumpAttributes.STATE.name: 0, FPPumpAttributes.DUTY_CYCLE.name: 0, FPPumpAttributes.FB_DUTY_CYCLE.name: 0, FPPumpAttributes.SPEED.name: 0.0, FPPumpAttributes.TARGET_PRES.name: 0.0, FPPumpAttributes.TARGET_FLOW.name: 0.0, FPPumpAttributes.TARGET_DUTY_CYCLE.name: 0.0, FPPumpAttributes.DUTY_CYCLE_PCT.name: 0.0, FPPumpAttributes.FB_DUTY_CYCLE_PCT.name: 0.0 } } @publish(["boost_pump_timestamp", "boost_pump" ]) def _handler_pump_sync(self, message, timestamp=0.0): """ Handles published FP ro pump data messages. FP ro pump data is captured for reference. @param message: published FP ro pump data message @return: none """ self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.STATE.name] = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.DUTY_CYCLE.name] = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.FB_DUTY_CYCLE.name] = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.SPEED.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.TARGET_PRESSURE.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.TARGET_FLOW.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.TARGET_DUTY_CYCLE.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.DUTY_CYCLE_PCT.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.boost_pump[BoostPumpNames.P40_PUMP.name][FPPumpAttributes.FB_DUTY_CYCLE_PCT.name] = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] self.boost_pump_timestamp = timestamp def cmd_boost_pump_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the FP boost pump broadcast interval override command Constraints: Must be logged into FP. Given interval must be non-zero and a multiple of the FP general task interval (50 ms). @param ms: integer - interval (in ms) to override with @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_fp_ch_id, message_id=MsgIds.MSG_ID_FP_BOOST_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override FP boost pump 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("broadcast overridden to " + str_res) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_boost_pump_target_pressure_override(self, pressure: float, reset: int = NO_RESET) -> int: """ Constructs and sends the target pressure override for the boost pump. This will drive the boost pump in a closed loop to the pressure. Constraints: Must be logged into FP. @param pressure: float - target pressure in PSI between 10 - 40 @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) prs = float_to_bytearray(pressure) payload = rst + prs message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_FP_BOOST_PUMP_TARGET_PRESSURE_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override boost pump target pressure") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal" else: str_res = str(pressure) self.logger.debug("boost target pressure overridden to " + str_res) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_boost_pump_target_flow_override(self, flow: int, reset: int = NO_RESET) -> int: """ Constructs and sends the target flow override for the boost pump. This will drive the boost pump in a closed loop to the flow. Constraints: Must be logged into FP. @param flow: float - target flow in mL/min between 0 - 1500 @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) flw = integer_to_bytearray(flow) payload = rst + flw message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_FP_BOOST_PUMP_TARGET_FLOW_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override BOOST pump target flow") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal" else: str_res = str(flow) self.logger.debug("BOOST target flow overridden to " + str_res) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_boost_pump_target_pwm_override(self, duty_cycle: float, reset: int = NO_RESET) -> int: """ Constructs and sends the target duty cycle override for the boost pump. This will drive the boost pump in an open loop to the pwm. Constraints: Must be logged into FP. @param pressure: float - target duty cycle between 0 - 0.90 @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) pwm = float_to_bytearray(duty_cycle) payload = rst + pwm message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_FP_BOOST_PUMP_TARGET_PWM_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override boost pump target 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 = "reset back to normal" else: str_res = str(duty_cycle) self.logger.debug("boost target pwm overridden to " + str_res) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_boost_pump_set_hard_stop(self) -> int: """ Constructs and sends the hard stop for the boost pump. This will stop the boost pump. Constraints: Must be logged into FP. @return: 1 if successful, zero otherwise """ message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_FP_BOOST_PUMP_STOP_REQUEST.value) self.logger.debug("hard stopping boost pump") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug(" boost pump stopped ") # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False