Index: leahi_dialin/td/modules/air_pump.py =================================================================== diff -u -rcd27cd828b37e7e0c8a77b63594456f308887ae8 -r1f2bf6d939eb4033dbedb7d7005494cc12fccbc6 --- leahi_dialin/td/modules/air_pump.py (.../air_pump.py) (revision cd27cd828b37e7e0c8a77b63594456f308887ae8) +++ leahi_dialin/td/modules/air_pump.py (.../air_pump.py) (revision 1f2bf6d939eb4033dbedb7d7005494cc12fccbc6) @@ -8,22 +8,25 @@ # @file air_pump.py # # @author (last) Zoltan Miskolci -# @date (last) 08-Jan-2026 +# @date (last) 05-May-2026 # @author (original) Micahel Garthwaite # @date (original) 01-Nov-2024 # ############################################################################ -import struct +# Module imports from logging import Logger +# Project imports from leahi_dialin.common.constants import NO_RESET -from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions +from leahi_dialin.common.generic_defs import DataTypes +from leahi_dialin.common.msg_defs import MsgIds from leahi_dialin.common.override_templates import cmd_generic_broadcast_interval_override, cmd_generic_override -from leahi_dialin.common.td_defs import td_enum_repository -from leahi_dialin.protocols.CAN import DenaliChannels -from leahi_dialin.utils.base import AbstractSubSystem, publish -from leahi_dialin.utils.conversions import integer_to_bytearray +from leahi_dialin.common import td_enum_repository +from leahi_dialin.protocols.CAN import CanMessenger, CanChannels +from leahi_dialin.utils.abstract_classes import AbstractSubSystem +from leahi_dialin.utils.base import publish +from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray MAX_AIR_PUMP_POWER_LEVEL = 255 #Maximum air pump power allowed @@ -34,30 +37,35 @@ Treatment Delivery (TD) Dialin API sub-class for air pump related commands. """ - def __init__(self, can_interface, logger: Logger): + def __init__(self, can_interface: CanMessenger, logger: Logger): """ - @param can_interface: Denali Can Messenger object + @param can_interface: 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 - self.msg_id_td_air_pump_data = MsgIds.MSG_ID_TD_AIR_PUMP_DATA.value - self.can_interface.register_receiving_publication_function(channel_id, self.msg_id_td_air_pump_data, - self._handler_air_pump_sync) + self.can_interface.register_receiving_publication_function(channel_id = CanChannels.td_sync_broadcast_ch_id, + message_id = MsgIds.MSG_ID_TD_AIR_PUMP_DATA.value, + function = self._handler_air_pump_sync) + self.td_air_pump_timestamp = 0.0 #: The timestamp of the last message + + #: The Air Trap Level Sensors data in dictionary format self.td_air_pump = { td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name: { td_enum_repository.TDAirPumpAttributes.STATE.name: 0, - td_enum_repository.TDAirPumpAttributes.POWER.name: 0 + td_enum_repository.TDAirPumpAttributes.DUTY_CYCLE.name: 0, + td_enum_repository.TDAirPumpAttributes.RPM.name: 0, + # TODO remove after FPGA air pump speed validation + td_enum_repository.TDAirPumpAttributes.FPGA_RPM.name: 0, + # TODO remove after FPGA air pump speed validation + td_enum_repository.TDAirPumpAttributes.SCALAR_POWER.name: 0 } } - self.td_air_pump_timestamp = 0.0 - @publish(["msg_id_td_air_pump_data", "td_air_pump", "td_air_pump_timestamp"]) def _handler_air_pump_sync(self, message, timestamp=0.0): """ @@ -66,13 +74,18 @@ @param message: published air pump data message as: air pump state @return: None """ - aps = struct.unpack('i', bytearray( - message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) - app = struct.unpack('i', bytearray( - message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) - - self.td_air_pump[td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name][td_enum_repository.TDAirPumpAttributes.STATE.name] = aps[0] - self.td_air_pump[td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name][td_enum_repository.TDAirPumpAttributes.POWER.name] = app[0] + sensor_list =[] + sensor_list.append((td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name, td_enum_repository.TDAirPumpAttributes.STATE.name, DataTypes.U32)) + sensor_list.append((td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name, td_enum_repository.TDAirPumpAttributes.DUTY_CYCLE.name, DataTypes.F32)) + sensor_list.append((td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name, td_enum_repository.TDAirPumpAttributes.RPM.name, DataTypes.U32)) + # TODO remove after FPGA air pump speed validation + sensor_list.append((td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name, td_enum_repository.TDAirPumpAttributes.FPGA_RPM.name, DataTypes.U32)) + # TODO remove after FPGA air pump speed validation + sensor_list.append((td_enum_repository.TDAirPumpNames.H12_AIR_PUMP.name, td_enum_repository.TDAirPumpAttributes.SCALAR_POWER.name, DataTypes.U32)) + + self.process_into_dict(dict_to_update = self.td_air_pump, + decoder_list = sensor_list, + message = message) self.td_air_pump_timestamp = timestamp @@ -90,7 +103,7 @@ return cmd_generic_broadcast_interval_override( ms = ms, reset = reset, - channel_id = DenaliChannels.dialin_to_td_ch_id, + channel_id = CanChannels.dialin_to_td_ch_id, msg_id = MsgIds.MSG_ID_TD_AIR_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST, module_name = 'TD Air Pump', logger = self.logger, @@ -111,17 +124,16 @@ if dutyCycle < 0 or dutyCycle > 100: return 0 - power = int((dutyCycle/100) * MAX_AIR_PUMP_POWER_LEVEL) sts = integer_to_bytearray(state) - pwr = integer_to_bytearray(power) + pwr = float_to_bytearray(dutyCycle) payload = sts + pwr state_name = 'start with power' if state == 2 else 'stopped' - power_value = f'{str(power)}' if state == 2 else '' + power_value = f'{str(dutyCycle)}' if state == 2 else '' return cmd_generic_override( payload = payload, reset = NO_RESET, - channel_id = DenaliChannels.dialin_to_td_ch_id, + channel_id = CanChannels.dialin_to_td_ch_id, msg_id = MsgIds.MSG_ID_TD_AIR_PUMP_SET_STATE_REQUEST, entity_name = f'TD Air Pump to {state_name}', override_text = f'{power_value}',