simple_conveyor.py 3.1 KB

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