Index: leahi_dialin/common/msg_ids.py =================================================================== diff -u -r416746b17a13438e6a97a2ab5321d908ab7f6619 -r8c823851a48f72a6ac2a26cc691063d214288231 --- leahi_dialin/common/msg_ids.py (.../msg_ids.py) (revision 416746b17a13438e6a97a2ab5321d908ab7f6619) +++ leahi_dialin/common/msg_ids.py (.../msg_ids.py) (revision 8c823851a48f72a6ac2a26cc691063d214288231) @@ -97,6 +97,8 @@ MSG_ID_DD_PRE_GEN_DIALYSATE_STATE_DATA = 0x4A MSG_ID_DD_POST_GEN_DIALYSATE_STATE_DATA = 0x4B MSG_ID_DD_PRE_GEN_DIALYSATE_REQUEST_DATA = 0x4C + MSG_ID_RO_PRE_GEN_WATER_MODE_DATA = 0x4D + MSG_ID_TD_EJECTOR_DATA = 0x4E MSG_ID_DD_PISTON_PUMP_CONTROL_DATA = 0xF0 @@ -147,8 +149,11 @@ MSG_ID_TD_TMP_PRESSURE_OVERRIDE_REQUEST = 0x802C MSG_ID_TD_REQ_CURRENT_TREATMENT_PARAMETERS = 0x802D MSG_ID_TD_RSP_CURRENT_TREATMENT_PARAMETERS = 0x802E - MSG_ID_TD_OP_MODE_PUBLISH_INTERVAL_OVERRIDE_REQUEST = 0x802F - MSG_ID_TD_OP_MODE_OVERRIDE_REQUEST = 0x8030 + MSG_ID_TD_SET_TREATMENT_PARAMETER = 0x802F + MSG_ID_TD_OP_MODE_PUBLISH_INTERVAL_OVERRIDE_REQUEST = 0x8030 + MSG_ID_TD_OP_MODE_OVERRIDE_REQUEST = 0x8031 + MSG_ID_TD_EJECTOR_MOTOR_SET_SPEED_REQUEST = 0x8032 + MSG_ID_TD_EJECTOR_COMMAND = 0x8033 MSG_ID_DD_TESTER_LOGIN_REQUEST = 0xA000 MSG_ID_DD_SOFTWARE_RESET_REQUEST = 0xA001 Index: leahi_dialin/td/modules/ejector.py =================================================================== diff -u --- leahi_dialin/td/modules/ejector.py (revision 0) +++ leahi_dialin/td/modules/ejector.py (revision 8c823851a48f72a6ac2a26cc691063d214288231) @@ -0,0 +1,176 @@ +########################################################################### +# +# Copyright (c) 2022-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 ejector.py +# +# @author (last) Micahel Garthwaite +# @date (last) 10-Mar-2023 +# @author (original) Micahel Garthwaite +# @date (original) 01-Nov-2024 +# +############################################################################ + +import struct +from logging import Logger + +from .constants import RESET, NO_RESET +from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions +from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels +from leahi_dialin.utils.base import AbstractSubSystem, publish +from leahi_dialin.utils.checks import check_broadcast_interval_override_ms +from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray + + +class TDEjector(AbstractSubSystem): + """ + TDEjector + + Treatment Delivery (TD) Dialin API sub-class for ejector motor related commands. + """ + + def __init__(self, can_interface, logger: Logger): + """ + + @param can_interface: Denali Can Messenger object + """ + super().__init__() + self.can_interface = can_interface + self.logger = logger + + if self.can_interface is not None: + channel_id = DenaliChannels.td_sync_broadcast_ch_id + msg_id = MsgIds.MSG_ID_TD_EJECTOR_DATA.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_ejector_sync) + + + self.h5_state = 0 + self.h5_set_speed = 0.0 + self.td_ejector_timestamp = 0 + + @publish(["td_ejector_timestamp", "h5_state", "h5_set_speed"]) + def _handler_ejector_sync(self, message, timestamp=0.0): + """ + Handles published ejector data messages. + + @param message: published ejector data message as: ejector state + @return: None + """ + self.h12_air_pump_state = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) + self.h12_air_pump_power = struct.unpack('f', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) + + self.td_air_pump_timestamp = timestamp + + def cmd_ejector_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: + """ + Constructs and sends the ejector data broadcast interval override command + Constraints: + Must be logged into TD. + Given interval must be non-zero and a multiple of the TD 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_td_ch_id, + message_id=MsgIds.MSG_ID_TD_AIR_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, + payload=payload) + + self.logger.debug("override TD ejector data broadcast interval") + + # 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(ms) + " ms: " + self.logger.debug("Ejector data broadcast interval overridden to " + 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 + + def cmd_ejector_request_command(self, cmd: int) -> int: + """ + Constructs and sends the ejector request command. + EJECTOR_OPERATION_HOME = 0, ///< Ejector home operation + EJECTOR_OPERATION_RETRACT, ///< Ejector retract operation + EJECTOR_OPERATION_EXTEND, ///< Ejector extend operation + NUM_OF_EJECTOR_OPERATIONS, ///< Number of ejector operations + + Constraints: + Must be logged into TD. + @param cmd: integer - state value to set the ejector + + @return: 1 if successful, zero otherwise + """ + + idx = integer_to_bytearray(cmd) + payload = idx + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, + message_id=MsgIds.MSG_ID_TD_EJECTOR_COMMAND.value, + payload=payload) + + self.logger.debug("setting ejector state to" + str(cmd)) + + # Send message + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False + + def cmd_ejector_set_motor_speed_request(self, speed: int) -> int: + """ + Constructs and sends the ejector set speed command. + + Constraints: + Must be logged into TD. + @param speed: float - value to set the motor speed + + @return: 1 if successful, zero otherwise + """ + + spd = float_to_bytearray(speed) + payload = spd + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, + message_id=MsgIds.MSG_ID_TD_EJECTOR_MOTOR_SET_SPEED_REQUEST.value, + payload=payload) + + self.logger.debug("setting ejector set speed to" + str(spd)) + + # Send message + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False \ No newline at end of file Index: leahi_dialin/td/modules/treatment.py =================================================================== diff -u -r0c1da102269169d5b63f91e146488b20eaa883a7 -r8c823851a48f72a6ac2a26cc691063d214288231 --- leahi_dialin/td/modules/treatment.py (.../treatment.py) (revision 0c1da102269169d5b63f91e146488b20eaa883a7) +++ leahi_dialin/td/modules/treatment.py (.../treatment.py) (revision 8c823851a48f72a6ac2a26cc691063d214288231) @@ -15,15 +15,45 @@ ############################################################################ import struct from logging import Logger +from enum import unique from .constants import RESET, NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels -from leahi_dialin.utils.base import AbstractSubSystem, publish +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 +@unique +class TDTreatmentParameters(DialinEnum): + TREATMENT_PARAM_BLOOD_FLOW = 0 # Blood flow rate (in mL/min) + TREATMENT_PARAM_FIRST_UINT = TREATMENT_PARAM_BLOOD_FLOW # First unsigned integer treatment parameter + TREATMENT_PARAM_DIALYSATE_FLOW = 1 # Dialysate flow rate (in mL/min) + TREATMENT_PARAM_TREATMENT_DURATION = 2 # Treatment duration (in minutes) + TREATMENT_PARAM_SALINE_BOLUS_VOLUME = 3 # Saline bolus volume (in mL) + TREATMENT_PARAM_HEPARIN_STOP_TIME = 4 # Heparin stop time (in minutes) + TREATMENT_PARAM_HEPARIN_TYPE = 5 # Heparin type (enum) + TREATMENT_PARAM_ACID_CONCENTRATE = 6 # Acid concentrate type (enum) + TREATMENT_PARAM_BICARB_CONCENTRATE = 7 # Bicarbonate concentrate type (enum) + TREATMENT_PARAM_DIALYZER_TYPE = 8 # Dialysate type (enum) + TREATMENT_PARAM_BP_MEAS_INTERVAL = 9 # Blood pressure measurement interval (in minutes) + TREATMENT_PARAM_RINSEBACK_FLOW_RATE = 10 # Rinseback flow rate (in mL/min) + TREATMENT_PARAM_RINSEBACK_VOLUME = 11 # Rinseback volume (in mL) + TREATMENT_PARAM_LAST_UINT = TREATMENT_PARAM_RINSEBACK_VOLUME # Last unsigned integer treatment parameter + TREATMENT_PARAM_ART_PRES_LIMIT_WINDOW = 12 # Arterial pressure alarm limit window (in mmHg) + TREATMENT_PARAM_FIRST_INT = TREATMENT_PARAM_ART_PRES_LIMIT_WINDOW # First integer treatment parameter + TREATMENT_PARAM_VEN_PRES_LIMIT_WINDOW = 13 # Venous pressure alarm limit window (in mmHg) + TREATMENT_PARAM_VEN_PRES_LIMIT_ASYMMETRIC = 14 # Venous pressure alarm limit asymmetric (in mmHg) + TREATMENT_PARAM_TMP_PRES_LIMIT_WINDOW = 15 # TMP alarm limit window (in mmHg) + TREATMENT_PARAM_LAST_INT = TREATMENT_PARAM_TMP_PRES_LIMIT_WINDOW # Last integer treatment parameter + TREATMENT_PARAM_DIALYSATE_TEMPERATURE = 16 # Dialysate temperature (in degC) + TREATMENT_PARAM_FIRST_F32 = TREATMENT_PARAM_DIALYSATE_TEMPERATURE # First floating point treatment parameter + TREATMENT_PARAM_HEPARIN_DISPENSE_RATE = 17 # Heparin dispense rate (in mL/hr) + TREATMENT_PARAM_HEPARIN_BOLUS_VOLUME = 18 # Heparin bolus volume (in mL) + TREATMENT_PARAM_UF_VOLUME = 19 # Ultrafiltration volume (in liters) - provided separately by UI + NUM_OF_TREATMENT_PARAMS = 20 # Total number of treatment parameters + class TDTreatment(AbstractSubSystem): """ Treatment Delivery (TD) Dialin API sub-class for treatment related commands. @@ -55,6 +85,9 @@ msg_id = MsgIds.MSG_ID_TD_TREATMENT_STATE_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_treatment_state_sync) + msg_id = MsgIds.MSG_ID_TD_RSP_CURRENT_TREATMENT_PARAMETERS.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_resp_treatment_parameters_sync) # Treatment param ranges data self.min_tx_time = 0 @@ -96,6 +129,29 @@ self.tx_end_state = 0 self.tx_state_timestamp = 0 + # Treatment Parameters Data. Most recent response. + self.blood_flow_rate = 0 + self.dialysate_flow_rate = 0 + self.tx_duration = 0 + self.saline_bolus_volume = 0 + self.hep_stop_time = 0 + self.hep_time = 0 + self.acid_con = 0 + self.bicarb_con = 0 + self.dialyzer_type = 0 + self.bp_interval = 0 + self.rb_flow_rate = 0 + self.rb_volume = 0 + self.art_pressure_window = 0 + self.venous_pressure_window = 0 + self.venous_asymm_window = 0 + self.tmp_limit_window = 0 + self.dialysate_temp = 0.0 + self.hep_dispense_rate = 0.0 + self.hep_bolus_vol = 0.0 + self.uf_vol = 0.0 + self.tx_param_req_timestamp = 0 + @publish(["min_tx_time","max_tx_time","min_uf_volume","max_uf_volume", "min_dial_rate","max_dial_rate","tx_params_timestamp"]) def _handler_treatment_param_ranges_sync(self, message, timestamp=0.0): @@ -210,3 +266,97 @@ message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.tx_state_timestamp = timestamp + + @publish(["blood_flow_rate", "dialysate_flow_rate", "tx_duration", "saline_bolus_volume", + "hep_stop_time", "hep_time", "acid_con", "bicarb_con", + "dialyzer_type", "bp_interval", "rb_flow_rate", "rb_volume", + "art_pressure_window","venous_pressure_window","venous_asymm_window","tmp_limit_window", + "dialysate_temp", "hep_dispense_rate", "hep_bolus_vol", "uf_vol"]) + def _handler_resp_treatment_parameters_sync(self, message, timestamp=0.0): + """ + Handles published treatment parameter response data messages. + + @param message: published treatment parameter response data message + @return: none + """ + + self.blood_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] + self.dialysate_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] + self.tx_duration = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] + self.saline_bolus_volume = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] + self.hep_stop_time = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] + self.hep_time = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] + self.acid_con = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] + self.bicarb_con = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] + self.dialyzer_type = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] + self.bp_interval = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] + self.rb_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] + self.rb_volume = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0] + self.art_pressure_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_13:MsgFieldPositions.END_POS_FIELD_13]))[0] + self.venous_pressure_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_14:MsgFieldPositions.END_POS_FIELD_14]))[0] + self.venous_asymm_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_15:MsgFieldPositions.END_POS_FIELD_15]))[0] + self.tmp_limit_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_16:MsgFieldPositions.END_POS_FIELD_16]))[0] + self.dialysate_temp = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_17:MsgFieldPositions.END_POS_FIELD_17]))[0] + self.hep_dispense_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_18:MsgFieldPositions.END_POS_FIELD_18]))[0] + self.hep_bolus_vol = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_19:MsgFieldPositions.END_POS_FIELD_19]))[0] + self.uf_vol = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_20:MsgFieldPositions.END_POS_FIELD_20]))[0] + + self.tx_param_req_timestamp = timestamp + + def cmd_set_treatment_parameter(self, tx_param_value, tx_param_id: int = 0 ): + """ + + Constraints: + Must be logged into TD. + @param tx_param_value: varied - value to set the treatment parameter + @param tx_param_id: integer - the param id of the treatment paramter + + @return: 1 if successful, zero otherwise + """ + idx = integer_to_bytearray(tx_param_id) + + if tx_param_id <= TDTreatmentParameters.TREATMENT_PARAM_LAST_UINT.value: + tpv = integer_to_bytearray(tx_param_value) + + elif tx_param_id >= TDTreatmentParameters.TREATMENT_PARAM_FIRST_F32.value: + tpv = float_to_bytearray(tx_param_value) + + else: + tpv = integer_to_bytearray(tx_param_value) + + payload = idx + tpv + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, + message_id=MsgIds.MSG_ID_TD_SET_TREATMENT_PARAMETER.value, + payload=payload) + + # Send message + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False \ No newline at end of file Index: leahi_dialin/td/treatment_delivery.py =================================================================== diff -u -rd3f64754a55d930463ad518acc34931e9a3867f1 -r8c823851a48f72a6ac2a26cc691063d214288231 --- leahi_dialin/td/treatment_delivery.py (.../treatment_delivery.py) (revision d3f64754a55d930463ad518acc34931e9a3867f1) +++ leahi_dialin/td/treatment_delivery.py (.../treatment_delivery.py) (revision 8c823851a48f72a6ac2a26cc691063d214288231) @@ -22,6 +22,7 @@ from .modules.bubble_detector import TDBubbleDectector from .modules.buttons import TDButtons from .modules.constants import NO_RESET, RESET +from .modules.ejector import TDEjector from .modules.pressure_sensors import TDPressureSensors from .modules.switches import TDSwitches from .modules.treatment import TDTreatment @@ -122,6 +123,7 @@ self.blood_flow = TDBloodFlow(self.can_interface, self.logger) self.bubbles = TDBubbleDectector(self.can_interface, self.logger) self.buttons = TDButtons(self.can_interface, self.logger) + self.ejector = TDEjector(self.can_interface, self.logger) self.pressure_sensors = TDPressureSensors(self.can_interface, self.logger) self.switches = TDSwitches(self.can_interface, self.logger) self.treatment = TDTreatment(self.can_interface, self.logger)