simple_conveyor.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.timed_phase_enum import TimedPhaseEnum
  8. from fdir.mixins import TurnedOffMixin
  9. class ConveyorPhase(TimedPhaseEnum):
  10. """ Steps in order, along with their timings """
  11. IDLE = ('IDLE', INFINITY)
  12. RUNNING = ('RUNNING', 2.368)
  13. @dataclass
  14. class ConveyorState:
  15. delta_t: float = INFINITY
  16. phase: ConveyorPhase = ConveyorPhase.IDLE
  17. workpiece: Workpiece | None = None
  18. visual_update_pending: bool = False
  19. class SimpleConveyor(AtomicDEVS):
  20. """ A simple conveyor belt which moves a workpiece across its belt and detects when it has reached the end """
  21. def __init__(self, name: str):
  22. super(SimpleConveyor, self).__init__(name)
  23. self.inp = self.addInPort("inp")
  24. self.out = self.addOutPort("out")
  25. self.mqtt_in = self.addInPort("mqtt_in") # to receive visual update requests
  26. self.mqtt_out = self.addOutPort("mqtt_out")
  27. self.state = ConveyorState()
  28. def change_phase(self, new_phase: ConveyorPhase):
  29. """ Wrapper for changing the phase and time associated with it, helps with logging """
  30. self.state.phase = new_phase
  31. self.state.delta_t = new_phase.timing
  32. logger.trace(f"{type(self).__name__} '{self.name}' phase changed to {new_phase}")
  33. self.state.visual_update_pending = True
  34. def get_visual_update_data(self) -> MqttMessage:
  35. """ Get visual update data for the animation, contains the action taken and the duration of that action left """
  36. message = MqttMessage()
  37. message.topic = "visualization/conveyor"
  38. duration = self.state.delta_t
  39. if duration == INFINITY: duration = None
  40. message.payload = {
  41. "action": self.state.phase.value,
  42. "duration": duration, # should be equal to the timing of the phase
  43. "workpiece": self.state.workpiece.to_dict() if self.state.workpiece else None,
  44. }
  45. return message
  46. def extTransition(self, inputs):
  47. if self.mqtt_in in inputs:
  48. msg = inputs[self.mqtt_in][0]
  49. if msg.topic == "simulation/ctrl/all" and msg.payload.get("action") == "refresh":
  50. self.state.visual_update_pending = True
  51. return self.state
  52. if self.inp in inputs:
  53. new_workpiece = inputs[self.inp][0]
  54. logger.trace(f"{type(self).__name__} '{self.name}' received: {new_workpiece}")
  55. self.state.workpiece = new_workpiece
  56. self.change_phase(ConveyorPhase.RUNNING)
  57. return self.state # important, return state
  58. def timeAdvance(self):
  59. if self.state.visual_update_pending:
  60. return 0.0
  61. return self.state.delta_t
  62. def outputFnc(self):
  63. if self.state.visual_update_pending:
  64. return {self.mqtt_out: [self.get_visual_update_data()]}
  65. logger.trace(f"{type(self).__name__} '{self.name}' outputs: {self.state.workpiece}")
  66. return {self.out: [self.state.workpiece]}
  67. def intTransition(self):
  68. if self.state.visual_update_pending:
  69. self.state.visual_update_pending = False
  70. return self.state
  71. # We have just output the workpiece, so mark it as empty again
  72. self.state.workpiece = None
  73. self.change_phase(ConveyorPhase.IDLE)
  74. return self.state
  75. class FaultyConveyor(TurnedOffMixin, SimpleConveyor):
  76. """ A conveyor that is turned off and does nothing """
  77. pass