Index: dialin/common/msg_ids.py =================================================================== diff -u -r6ff50b4ae97f1d8071f1d8fdafae60f6fd704b9d -ra1d640c70267e6e5e2ad81fc23bc5ca91847b61d --- dialin/common/msg_ids.py (.../msg_ids.py) (revision 6ff50b4ae97f1d8071f1d8fdafae60f6fd704b9d) +++ dialin/common/msg_ids.py (.../msg_ids.py) (revision a1d640c70267e6e5e2ad81fc23bc5ca91847b61d) @@ -481,6 +481,9 @@ MSG_ID_DG_SEND_TEST_CONFIGURATION = 0xA068 MSG_ID_DG_RESET_ALL_TEST_CONFIGURATIONS = 0xA069 MSG_ID_DG_DIALIN_CHECK_IN = 0xA06A + MSG_ID_DG_GET_LOAD_CELLS_TARE_VALUES = 0xA06B + MSG_ID_DG_SET_LOAD_CELLS_TARE_VALUES = 0xA06C + MSG_ID_DG_SEND_LOAD_CELLS_TARE_VALUES_TO_DIALIN = 0xA06D MSG_ID_HD_DEBUG_EVENT = 0xFFF1 MSG_ID_DG_DEBUG_EVENT = 0xFFF2 Index: dialin/dg/load_cells.py =================================================================== diff -u -r9bc00e997e91dab8b404aa877b02ae3d4100d417 -ra1d640c70267e6e5e2ad81fc23bc5ca91847b61d --- dialin/dg/load_cells.py (.../load_cells.py) (revision 9bc00e997e91dab8b404aa877b02ae3d4100d417) +++ dialin/dg/load_cells.py (.../load_cells.py) (revision a1d640c70267e6e5e2ad81fc23bc5ca91847b61d) @@ -15,15 +15,27 @@ ############################################################################ import struct from logging import Logger +from enum import unique + 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 +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, bytearray_to_float +@unique +class DGLoadCellNames(DialinEnum): + + LOAD_CELL_RESERVOIR_1_PRIMARY = 0 # Primary load cell for reservoir 1 + LOAD_CELL_RESERVOIR_1_BACKUP = 1 # Backup load cell for reservoir 1 + LOAD_CELL_RESERVOIR_2_PRIMARY = 2 # Primary load cell for reservoir 2 + LOAD_CELL_RESERVOIR_2_BACKUP = 3 # Backup load cell for reservoir 2 + NUM_OF_LOAD_CELLS = 4 # Number of load cell sensors + + class DGLoadCells(AbstractSubSystem): """ DGLoadCells @@ -54,11 +66,21 @@ msg_id = MsgIds.MSG_ID_LOAD_CELL_READINGS.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_load_cells_sync) + msg_id = MsgIds.MSG_ID_DG_SEND_LOAD_CELLS_TARE_VALUES_TO_DIALIN.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_load_cells_tare_values_sync) + self.load_cell_A1 = 0.0 self.load_cell_A2 = 0.0 self.load_cell_B1 = 0.0 self.load_cell_B2 = 0.0 + self.load_cells_tare_values = dict() + self.dg_load_cells_tare_values_timestamp = 0.0 + for cell in DGLoadCellNames.__members__: + if 'NUM_OF_LOAD_CELLS' not in cell: + self.load_cells_tare_values[cell] = 0.0 + def get_load_cells(self): """ Gets the current load cell weights @@ -67,6 +89,24 @@ """ return [self.load_cell_A1, self.load_cell_A2, self.load_cell_B1, self.load_cell_B2] + @publish(["load_cells_tare_values"]) + def _handler_load_cells_tare_values_sync(self, message, timestamp=0.0): + """ + Handles sent load cells tare value messages. + + @param message: published load cell data message + @return: None + """ + payload = message['message'] + index = MsgFieldPositions.START_POS_FIELD_1 + + for cell in DGLoadCellNames.__members__: + if 'NUM_OF_LOAD_CELLS' not in cell: + tare_value, index = bytearray_to_float(payload, index, False) + self.load_cells_tare_values[cell] = tare_value + + self.dg_load_cells_tare_values_timestamp = timestamp + @publish(["dg_load_cell_timestamp","load_cell_A1", "load_cell_A2", "load_cell_B1", "load_cell_B2"]) def _handler_load_cells_sync(self, message, timestamp=0.0): """ @@ -171,3 +211,34 @@ else: self.logger.debug("Timeout!!!!") return False + + def cmd_set_load_cells_tare_values(self): + """ + Constructs and sends the load cells tare values command + Constraints: + Must be logged into DG. + + @return: 1 if successful, zero otherwise + """ + payload = 0.0 + + for cell in DGLoadCellNames.__members__: + tare = self.load_cells_tare_values[cell] + payload += float_to_bytearray(tare) + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, + message_id=MsgIds.MSG_ID_DG_SET_LOAD_CELLS_TARE_VALUES.value, + payload=payload) + # Send message + received_message = self.can_interface.send(message) + + self.logger.debug("Setting load cells tare values") + + # 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 +