mpo_table_saw.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from data_models.mqtt_message import MqttMessage
  2. from pypdevs.DEVS import AtomicDEVS
  3. from pypdevs.infinity import INFINITY
  4. from dataclasses import dataclass
  5. from loguru import logger
  6. from data_models.workpiece import Workpiece
  7. from utils.get_timestamp import get_timestamp
  8. from utils.timed_phase_enum import TimedPhaseEnum
  9. from fdir.mixins import TurnedOffMixin
  10. class MPOTableSawPhase(TimedPhaseEnum):
  11. """ Steps in order, along with their timings """
  12. IDLE = ('IDLE', INFINITY)
  13. ROTATE_TO_FRONT = ('ROTATE_TO_FRONT', 2.697) # rotate table to face the gripper
  14. AWAIT_WP = ('AWAIT_WP', INFINITY) # await workpiece delivery by gripper
  15. ROTATE_TO_BACK = ('ROTATE_TO_BACK', 1.791) # rotate table to face saw (at back)
  16. SAW_RIGHT = ('SAW_RIGHT', 3.5)
  17. SAW_LEFT = ('SAW_LEFT', 3.5)
  18. ROTATE_TO_SIDE = ('ROTATE_TO_SIDE', 0.904) # turn table towards conveyor (to the right side)
  19. EJECT_WP = ('EJECT_WP', 0.630) # eject/push workpiece on conveyor
  20. @dataclass
  21. class MPOTableSawState:
  22. delta_t: float = INFINITY
  23. phase: MPOTableSawPhase = MPOTableSawPhase.IDLE
  24. workpiece: Workpiece | None = None
  25. status_requested: bool = True # if true, publish status
  26. visual_update_pending: bool = False
  27. def get_mpo_produced_msg() -> MqttMessage:
  28. """ Get the 'MPO_PRODUCED' mqtt message """
  29. message = MqttMessage()
  30. message.topic = "fl/mpo/ack"
  31. message.payload = {
  32. "code": 2,
  33. "ts": get_timestamp(),
  34. }
  35. return message
  36. class MPOTableSaw(AtomicDEVS):
  37. """ MPO rotating table and table saw """
  38. def __init__(self, name: str):
  39. super(MPOTableSaw, self).__init__(name)
  40. self.gripper_in = self.addInPort("gripper_in") # vacuum gripper which brings us workpiece
  41. self.gripper_out = self.addOutPort("gripper_out") # to acknowledge table turning to the gripper
  42. self.conveyor_out = self.addOutPort("conveyor_out") # conveyor belt output after we're done
  43. self.mqtt_in = self.addInPort("mqtt_in")
  44. self.mqtt_out = self.addOutPort("mqtt_out")
  45. self.state = MPOTableSawState()
  46. def change_phase(self, new_phase: MPOTableSawPhase):
  47. """ Wrapper for changing the phase and time associated with it, helps with logging """
  48. self.state.phase = new_phase
  49. self.state.delta_t = new_phase.timing
  50. logger.trace(f"{type(self).__name__} '{self.name}' phase changed to {new_phase}")
  51. self.state.visual_update_pending = True
  52. def get_visual_update_data(self) -> MqttMessage:
  53. """ Get visual update data for the animation, contains the action taken and the duration of that action left """
  54. message = MqttMessage()
  55. message.topic = "visualization/mpo/table_saw"
  56. duration = self.state.delta_t
  57. if duration == INFINITY: duration = None
  58. message.payload = {
  59. "action": self.state.phase.value,
  60. "duration": duration, # should be equal to the timing of the phase
  61. "workpiece": self.state.workpiece.to_dict() if self.state.workpiece else None,
  62. }
  63. return message
  64. def get_status_message(self, use_next_phase=True) -> MqttMessage:
  65. phase = self.state.phase.next() if use_next_phase else self.state.phase
  66. active = 0 if (phase == MPOTableSawPhase.IDLE) else 1
  67. code = 2 if active == 1 else 1
  68. message = MqttMessage()
  69. message.topic = "f/i/state/mpo"
  70. message.payload = {
  71. "active": active,
  72. "code": code,
  73. "description": '',
  74. "station": 'mpo',
  75. "ts": get_timestamp(),
  76. }
  77. return message
  78. def extTransition(self, inputs):
  79. self.state.delta_t -= self.elapsed
  80. if self.mqtt_in in inputs:
  81. msg = inputs[self.mqtt_in][0]
  82. if msg.topic == "simulation/ctrl/all" and msg.payload.get("action") == "refresh":
  83. self.state.status_requested = True
  84. self.state.visual_update_pending = True
  85. return self.state
  86. elif not (self.state.phase == MPOTableSawPhase.IDLE or self.state.phase == MPOTableSawPhase.AWAIT_WP):
  87. logger.error(f"{type(self).__name__} '{self.name}' received input while not expecting: {inputs}")
  88. return self.state
  89. if self.gripper_in in inputs:
  90. # 2 cases: instruction to turn, or workpiece
  91. if self.state.phase == MPOTableSawPhase.IDLE:
  92. self.change_phase(MPOTableSawPhase.ROTATE_TO_FRONT)
  93. elif self.state.phase == MPOTableSawPhase.AWAIT_WP:
  94. wp = inputs[self.gripper_in][0]
  95. self.state.workpiece = wp
  96. self.change_phase(MPOTableSawPhase.ROTATE_TO_BACK)
  97. else:
  98. logger.error(f"{type(self).__name__} '{self.name}' received gripper input while not expecting: {inputs}")
  99. return self.state # important, return state
  100. def timeAdvance(self):
  101. if self.state.visual_update_pending or self.state.status_requested:
  102. return 0.0
  103. return self.state.delta_t
  104. def outputFnc(self):
  105. if self.state.visual_update_pending:
  106. return {self.mqtt_out: [self.get_visual_update_data()]}
  107. if self.state.status_requested:
  108. return {self.mqtt_out: [self.get_status_message(use_next_phase=False)]}
  109. if self.state.phase == MPOTableSawPhase.ROTATE_TO_FRONT:
  110. return {self.gripper_out: {}, self.mqtt_out: [self.get_status_message()]} # acknowledge rotation
  111. elif self.state.phase == MPOTableSawPhase.EJECT_WP:
  112. # output wp, and publish 'mpo produced' message to mqtt
  113. return {self.conveyor_out: [self.state.workpiece], self.mqtt_out: [get_mpo_produced_msg(), self.get_status_message()]}
  114. return {self.mqtt_out: [self.get_status_message()]}
  115. def intTransition(self):
  116. if self.state.visual_update_pending:
  117. self.state.visual_update_pending = False
  118. return self.state
  119. if self.state.status_requested:
  120. self.state.status_requested = False
  121. return self.state
  122. if self.state.phase == MPOTableSawPhase.EJECT_WP:
  123. self.state.workpiece = None # remove the workpiece we just output to the conveyor
  124. # Transition to next state
  125. self.change_phase(self.state.phase.next())
  126. return self.state
  127. class FaultyMPOTableSaw(TurnedOffMixin, MPOTableSaw):
  128. """ Faulty MPO rotating table and table saw, turned off and does nothing """
  129. pass