Index: leahi_dialin/common/msg_ids.py =================================================================== diff -u -rc911f3b6e212249136baf0e45b614a2f5be2b190 -rf9dd375900151a5daafe3b687187187645d5f1ab --- leahi_dialin/common/msg_ids.py (.../msg_ids.py) (revision c911f3b6e212249136baf0e45b614a2f5be2b190) +++ leahi_dialin/common/msg_ids.py (.../msg_ids.py) (revision f9dd375900151a5daafe3b687187187645d5f1ab) @@ -77,6 +77,9 @@ MSG_ID_RO_FLOW_DATA = 0x36 MSG_ID_RO_CONDUCTIVITY_DATA = 0x37 MSG_ID_DD_RO_START_STOP_CMD_REQUEST = 0x38 + MSG_ID_RO_TEMPERATURE_DATA = 0x39 + MSG_ID_RO_HEATER_DATA = 0x3A + MSG_ID_DD_PISTON_PUMP_CONTROL_DATA = 0x3B MSG_ID_TESTER_LOGIN_REQUEST = 0x8000 @@ -178,6 +181,9 @@ MSG_ID_DD_DIAL_DELIVERY_IN_PROGRESS_OVERRIDE_REQUEST = 0xA033 MSG_ID_DD_DIAL_DELIVERY_GOOD_TO_DELIVER_OVERRIDE_REQUEST = 0xA034 MSG_ID_DD_HEATERS_TARGET_TEMPERATURE_OVERRIDE_REQUEST = 0xA035 + MSG_ID_DD_BC_VALVE_STATES_OVERRIDE_REQUEST = 0xA036 + MSG_ID_DD_PISTON_PUMP_DATA_PUBLISH_OVERRIDE_REQUEST = 0xA037 + MSG_ID_DD_PISTON_PUMP_START_STOP_OVERRIDE_REQUEST = 0xA038 MSG_ID_TD_DEBUG_EVENT = 0xFFF1 MSG_ID_DD_DEBUG_EVENT = 0xFFF2 Index: leahi_dialin/dd/modules/piston_pump.py =================================================================== diff -u --- leahi_dialin/dd/modules/piston_pump.py (revision 0) +++ leahi_dialin/dd/modules/piston_pump.py (revision f9dd375900151a5daafe3b687187187645d5f1ab) @@ -0,0 +1,164 @@ +########################################################################### +# +# 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 piston_pumps.py +# +# @author (last) Micahel Garthwaite +# @date (last) 07-Mar-2023 +# @author (original) Micahel Garthwaite +# @date (original) 29-Oct-2020 +# +############################################################################ +import struct +from enum import unique +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 DDPistonPumps(AbstractSubSystem): + """ + PistonPumps + + Dialysate Delivery (DD) Dialin API sub-class for piston pumps 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.dd_sync_broadcast_ch_id + msg_id = MsgIds.MSG_ID_DD_PISTON_PUMP_CONTROL_DATA.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_piston_pumps_sync) + + self.dd_piston_pump_timestamp = 0.0 + self.acid_control_state = 0 + self.bicarb_control_state = 0 + self.uf_control_state = 0 + self.acid_sw_counter = 0 + self.bicarb_sw_counter = 0 + self.uf_sw_counter = 0 + self.acid_cycle_counter = 0 + self.bicarb_cycle_counter = 0 + self.uf_cycle_counter = 0 + + @publish(["dd_piston_pump_timestamp", + "acid_control_state", "bicarb_control_state", "uf_control_state", + "acid_sw_counter", "bicarb_sw_counter", "uf_sw_counter", + "acid_cycle_counter", "bicarb_cycle_counter", "uf_cycle_counter"]) + def _handler_piston_pumps_sync(self, message, timestamp=0.0): + """ + Handles published piston pumps' data messages. Piston pumps' data are captured + for reference. + + @param message: published piston pumps' data message + @return: None + """ + self.acid_control_state = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] + self.bicarb_control_state = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] + self.uf_control_state = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] + self.acid_sw_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] + self.bicarb_sw_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] + self.uf_sw_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] + self.acid_cycle_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] + self.bicarb_cycle_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] + self.uf_cycle_counter = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] + self.dd_piston_pump_timestamp = timestamp + + def cmd_piston_pump_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: + """ + Constructs and sends the piston pump data broadcast interval override command + Constraints: + Must be logged into DD. + Given interval must be non-zero and a multiple of the DD 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 + + reset_byte_array = integer_to_bytearray(reset) + ms_byte_array = integer_to_bytearray(ms) + payload = reset_byte_array + ms_byte_array + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, + message_id=MsgIds.MSG_ID_DD_PISTON_PUMP_DATA_PUBLISH_OVERRIDE_REQUEST.value, + payload=payload) + + self.logger.debug("override DD piston pump data broadcast interval") + + # 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.error("Timeout!!!!") + return False + + def cmd_piston_set_start_stop(self, pump_id: int, command: int, count: int, speed: float, volume: float ) -> int: + """ + Constructs and sends the piston pump start stop command + + @param pump_id: unsigned int - concentrate pump ID + @param command: int - value to command the concentrate pump + @param count: int - vale to set the cycle count to + @param speed: float - ml/min to set the speed to + @param volume: float - the volume to set to + @return: 1 if successful, zero otherwise + + """ + + pmp = integer_to_bytearray(pump_id) + cmd = integer_to_bytearray(command) + cyc = integer_to_bytearray(count) + spd = float_to_bytearray(speed) + vlm = float_to_bytearray(volume) + payload = pmp + cmd + cyc + spd + vlm + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, + message_id=MsgIds.MSG_ID_DD_PISTON_PUMP_START_STOP_OVERRIDE_REQUEST.value, + payload=payload) + + self.logger.debug("setting " + str(spd) + " - for pump: " + str(pump_id)) + + # 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.error("Timeout!!!!") + return False \ No newline at end of file Index: leahi_dialin/dd/modules/valves.py =================================================================== diff -u -r9a915d188a0e77cc68e9d5a91de66bacbd1da96f -rf9dd375900151a5daafe3b687187187645d5f1ab --- leahi_dialin/dd/modules/valves.py (.../valves.py) (revision 9a915d188a0e77cc68e9d5a91de66bacbd1da96f) +++ leahi_dialin/dd/modules/valves.py (.../valves.py) (revision f9dd375900151a5daafe3b687187187645d5f1ab) @@ -255,36 +255,36 @@ # Extract each valve state from U16 valves states using bit-masking self.valve_state_VHO["state"] = self._binary_to_valve_state(vst[0] & 1) self.valve_state_VTD["state"] = self._binary_to_valve_state(vst[0] & 2) - self.valve_state_VHB["state"] = self._binary_to_valve_state(vst[0] & 8) - self.valve_state_VRP["state"] = self._binary_to_valve_state(vst[0] & 16) - self.valve_state_VDR["state"] = self._binary_to_valve_state(vst[0] & 32) - self.valve_state_VDB2["state"] = self._binary_to_valve_state(vst[0] & 64) - self.valve_state_VP1["state"] = self._binary_to_valve_state(vst[0] & 128) - self.valve_state_VPT["state"] = self._binary_to_valve_state(vst[0] & 256) - self.valve_state_VDB1["state"] = self._binary_to_valve_state(vst[0] & 512) - self.valve_state_VDI["state"] = self._binary_to_valve_state(vst[0] & 1024) - self.valve_state_VDO["state"] = self._binary_to_valve_state(vst[0] & 2048) - self.valve_state_VP2["state"] = self._binary_to_valve_state(vst[0] & 4096) - self.valve_state_VHI["state"] = self._binary_to_valve_state(vst[0] & 8192) - self.valve_state_VWI["state"] = self._binary_to_valve_state(vst[0] & 16384) - self.valve_state_rsvrd1["state"] = self._binary_to_valve_state(vst[0] & 32768) - self.valve_state_rsvrd2["state"] = self._binary_to_valve_state(vst[0] & 65536) + self.valve_state_VHB["state"] = self._binary_to_valve_state(vst[0] & 4) + self.valve_state_VRP["state"] = self._binary_to_valve_state(vst[0] & 8) + self.valve_state_VDR["state"] = self._binary_to_valve_state(vst[0] & 16) + self.valve_state_VDB2["state"] = self._binary_to_valve_state(vst[0] & 32) + self.valve_state_VP1["state"] = self._binary_to_valve_state(vst[0] & 64) + self.valve_state_VPT["state"] = self._binary_to_valve_state(vst[0] & 128) + self.valve_state_VDB1["state"] = self._binary_to_valve_state(vst[0] & 256) + self.valve_state_VDI["state"] = self._binary_to_valve_state(vst[0] & 512) + self.valve_state_VDO["state"] = self._binary_to_valve_state(vst[0] & 1024) + self.valve_state_VP2["state"] = self._binary_to_valve_state(vst[0] & 2048) + self.valve_state_VHI["state"] = self._binary_to_valve_state(vst[0] & 4096) + self.valve_state_VWI["state"] = self._binary_to_valve_state(vst[0] & 8192) + self.valve_state_rsvrd1["state"] = self._binary_to_valve_state(vst[0] & 16384) + self.valve_state_rsvrd2["state"] = self._binary_to_valve_state(vst[0] & 32768) bcv = struct.unpack('B', bytearray(message['message'][self.START_POS_BCV_VALVES_STATES:self.END_POS_BCV_VALVES_STATES])) self.valve_state_BCV1["state"] = self._binary_to_valve_state(bcv[0] & 1) self.valve_state_BCV2["state"] = self._binary_to_valve_state(bcv[0] & 2) - self.valve_state_BCV3["state"] = self._binary_to_valve_state(bcv[0] & 8) - self.valve_state_BCV7["state"] = self._binary_to_valve_state(bcv[0] & 16) - self.valve_state_BCV5["state"] = self._binary_to_valve_state(bcv[0] & 32) - self.valve_state_BCV6["state"] = self._binary_to_valve_state(bcv[0] & 64) - self.valve_state_BCV4["state"] = self._binary_to_valve_state(bcv[0] & 128) - self.valve_state_BCV8["state"] = self._binary_to_valve_state(bcv[0] & 256) + self.valve_state_BCV3["state"] = self._binary_to_valve_state(bcv[0] & 4) + self.valve_state_BCV7["state"] = self._binary_to_valve_state(bcv[0] & 8) + self.valve_state_BCV5["state"] = self._binary_to_valve_state(bcv[0] & 16) + self.valve_state_BCV6["state"] = self._binary_to_valve_state(bcv[0] & 32) + self.valve_state_BCV4["state"] = self._binary_to_valve_state(bcv[0] & 64) + self.valve_state_BCV8["state"] = self._binary_to_valve_state(bcv[0] & 128) ufi = struct.unpack('B', bytearray(message['message'][self.START_POS_UFI_VALVES_STATES:self.END_POS_UFI_VALVES_STATES])) self.valve_state_UFI1["state"] = self._binary_to_valve_state(ufi[0] & 1) self.valve_state_UFI2["state"] = self._binary_to_valve_state(ufi[0] & 2) - self.valve_state_UFI3["state"] = self._binary_to_valve_state(ufi[0] & 8) - self.valve_state_UFI4["state"] = self._binary_to_valve_state(ufi[0] & 16) + self.valve_state_UFI3["state"] = self._binary_to_valve_state(ufi[0] & 4) + self.valve_state_UFI4["state"] = self._binary_to_valve_state(ufi[0] & 8) start = self.END_POS_UFI_VALVES_STATES end = start + 1 @@ -366,6 +366,61 @@ self.logger.debug("Timeout!!!!") return False + def cmd_bc_valves_override(self, bcv1: bool, bcv2: bool, bcv3: bool, bcv4: bool, bcv5: bool, bcv6: bool, bcv7: bool, bcv8: bool) -> int: + """ + Constructs and sends the balancing chamber valve states override command. + Constraints: + Must be logged into DD. + Given valve ID must be one of the valve IDs listed below. + + @param bcv1: bool - valve state for BCV1 (true=open, false=closed) + @param bcv2: bool - valve state for BCV2 (true=open, false=closed) + @param bcv3: bool - valve state for BCV3 (true=open, false=closed) + @param bcv4: bool - valve state for BCV4 (true=open, false=closed) + @param bcv5: bool - valve state for BCV5 (true=open, false=closed) + @param bcv6: bool - valve state for BCV6 (true=open, false=closed) + @param bcv7: bool - valve state for BCV7 (true=open, false=closed) + @param bcv8: bool - valve state for BCV8 (true=open, false=closed) + @return: 1 if successful, zero otherwise + """ + + valve = 0 + if bcv1: + valve = valve | 0x1 + if bcv2: + valve = valve | 0x2 + if bcv3: + valve = valve | 0x4 + if bcv7: + valve = valve | 0x8 + if bcv5: + valve = valve | 0x10 + if bcv6: + valve = valve | 0x20 + if bcv4: + valve = valve | 0x40 + if bcv8: + valve = valve | 0x80 + + payload = integer_to_bytearray(valve) + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, + message_id=MsgIds.MSG_ID_DD_BC_VALVE_STATES_OVERRIDE_REQUEST.value, + payload=payload) + + self.logger.debug("Override balancing chamber valve states") + + # 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_valve_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the valve state override command. Index: leahi_dialin/dd/proxies/td_proxy.py =================================================================== diff -u -r78814f5e551360517b569f81dc2a7455f15ac2bc -rf9dd375900151a5daafe3b687187187645d5f1ab --- leahi_dialin/dd/proxies/td_proxy.py (.../td_proxy.py) (revision 78814f5e551360517b569f81dc2a7455f15ac2bc) +++ leahi_dialin/dd/proxies/td_proxy.py (.../td_proxy.py) (revision f9dd375900151a5daafe3b687187187645d5f1ab) @@ -92,7 +92,8 @@ byp = integer_to_bytearray(bypass) acd = integer_to_bytearray(acid) bic = integer_to_bytearray(bicarb) - payload = bic +acd + byp + temp + uf + dial + stt + #payload = bic +acd + byp + temp + uf + dial + stt + payload = stt + dial + uf + temp + byp + acd + bic message = DenaliMessage.build_message(channel_id=DenaliChannels.td_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_GEN_DIALYSATE_REQUEST_DATA.value, payload=payload)