UMOCheckupMachineAction.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. from cura.MachineAction import MachineAction
  2. from cura.PrinterOutputDevice import PrinterOutputDevice
  3. from UM.Application import Application
  4. from PyQt5.QtCore import pyqtSlot, pyqtSignal, pyqtProperty
  5. from UM.Logger import Logger
  6. from UM.i18n import i18nCatalog
  7. catalog = i18nCatalog("cura")
  8. ## Action to check up if the self-built UMO was done correctly.
  9. class UMOCheckupMachineAction(MachineAction):
  10. def __init__(self):
  11. super().__init__("UMOCheckup", catalog.i18nc("@action", "Checkup"))
  12. self._qml_url = "UMOCheckupMachineAction.qml"
  13. self._hotend_target_temp = 180
  14. self._bed_target_temp = 60
  15. self._output_device = None
  16. self._bed_test_completed = False
  17. self._hotend_test_completed = False
  18. # Endstop tests
  19. self._x_min_endstop_test_completed = False
  20. self._y_min_endstop_test_completed = False
  21. self._z_min_endstop_test_completed = False
  22. self._check_started = False
  23. Application.getInstance().getOutputDeviceManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
  24. onBedTestCompleted = pyqtSignal()
  25. onHotendTestCompleted = pyqtSignal()
  26. onXMinEndstopTestCompleted = pyqtSignal()
  27. onYMinEndstopTestCompleted = pyqtSignal()
  28. onZMinEndstopTestCompleted = pyqtSignal()
  29. bedTemperatureChanged = pyqtSignal()
  30. hotendTemperatureChanged = pyqtSignal()
  31. def _onOutputDevicesChanged(self):
  32. # Check if this action was started, but no output device was found the first time.
  33. # If so, re-try now that an output device has been added/removed.
  34. if self._output_device is None and self._check_started:
  35. self.startCheck()
  36. def _getPrinterOutputDevices(self):
  37. return [printer_output_device for printer_output_device in
  38. Application.getInstance().getOutputDeviceManager().getOutputDevices() if
  39. isinstance(printer_output_device, PrinterOutputDevice)]
  40. def _reset(self):
  41. if self._output_device:
  42. self._output_device.bedTemperatureChanged.disconnect(self.bedTemperatureChanged)
  43. self._output_device.hotendTemperaturesChanged.disconnect(self.hotendTemperatureChanged)
  44. self._output_device.bedTemperatureChanged.disconnect(self._onBedTemperatureChanged)
  45. self._output_device.hotendTemperaturesChanged.disconnect(self._onHotendTemperatureChanged)
  46. self._output_device.endstopStateChanged.disconnect(self._onEndstopStateChanged)
  47. try:
  48. self._output_device.stopPollEndstop()
  49. except AttributeError as e: # Connection is probably not a USB connection. Something went pretty wrong if this happens.
  50. Logger.log("e", "An exception occurred while stopping end stop polling: %s" % str(e))
  51. self._output_device = None
  52. self._check_started = False
  53. self.checkStartedChanged.emit()
  54. # Ensure everything is reset (and right signals are emitted again)
  55. self._bed_test_completed = False
  56. self.onBedTestCompleted.emit()
  57. self._hotend_test_completed = False
  58. self.onHotendTestCompleted.emit()
  59. self._x_min_endstop_test_completed = False
  60. self.onXMinEndstopTestCompleted.emit()
  61. self._y_min_endstop_test_completed = False
  62. self.onYMinEndstopTestCompleted.emit()
  63. self._z_min_endstop_test_completed = False
  64. self.onZMinEndstopTestCompleted.emit()
  65. self.heatedBedChanged.emit()
  66. @pyqtProperty(bool, notify = onBedTestCompleted)
  67. def bedTestCompleted(self):
  68. return self._bed_test_completed
  69. @pyqtProperty(bool, notify = onHotendTestCompleted)
  70. def hotendTestCompleted(self):
  71. return self._hotend_test_completed
  72. @pyqtProperty(bool, notify = onXMinEndstopTestCompleted)
  73. def xMinEndstopTestCompleted(self):
  74. return self._x_min_endstop_test_completed
  75. @pyqtProperty(bool, notify=onYMinEndstopTestCompleted)
  76. def yMinEndstopTestCompleted(self):
  77. return self._y_min_endstop_test_completed
  78. @pyqtProperty(bool, notify=onZMinEndstopTestCompleted)
  79. def zMinEndstopTestCompleted(self):
  80. return self._z_min_endstop_test_completed
  81. @pyqtProperty(float, notify = bedTemperatureChanged)
  82. def bedTemperature(self):
  83. if not self._output_device:
  84. return 0
  85. return self._output_device.bedTemperature
  86. @pyqtProperty(float, notify=hotendTemperatureChanged)
  87. def hotendTemperature(self):
  88. if not self._output_device:
  89. return 0
  90. return self._output_device.hotendTemperatures[0]
  91. def _onHotendTemperatureChanged(self):
  92. if not self._output_device:
  93. return
  94. if not self._hotend_test_completed:
  95. if self._output_device.hotendTemperatures[0] + 10 > self._hotend_target_temp and self._output_device.hotendTemperatures[0] - 10 < self._hotend_target_temp:
  96. self._hotend_test_completed = True
  97. self.onHotendTestCompleted.emit()
  98. def _onBedTemperatureChanged(self):
  99. if not self._output_device:
  100. return
  101. if not self._bed_test_completed:
  102. if self._output_device.bedTemperature + 5 > self._bed_target_temp and self._output_device.bedTemperature - 5 < self._bed_target_temp:
  103. self._bed_test_completed = True
  104. self.onBedTestCompleted.emit()
  105. def _onEndstopStateChanged(self, switch_type, state):
  106. if state:
  107. if switch_type == "x_min":
  108. self._x_min_endstop_test_completed = True
  109. self.onXMinEndstopTestCompleted.emit()
  110. elif switch_type == "y_min":
  111. self._y_min_endstop_test_completed = True
  112. self.onYMinEndstopTestCompleted.emit()
  113. elif switch_type == "z_min":
  114. self._z_min_endstop_test_completed = True
  115. self.onZMinEndstopTestCompleted.emit()
  116. checkStartedChanged = pyqtSignal()
  117. @pyqtProperty(bool, notify = checkStartedChanged)
  118. def checkStarted(self):
  119. return self._check_started
  120. @pyqtSlot()
  121. def startCheck(self):
  122. self._check_started = True
  123. self.checkStartedChanged.emit()
  124. output_devices = self._getPrinterOutputDevices()
  125. if output_devices:
  126. self._output_device = output_devices[0]
  127. try:
  128. self._output_device.sendCommand("M18") # Turn off all motors so the user can move the axes
  129. self._output_device.startPollEndstop()
  130. self._output_device.bedTemperatureChanged.connect(self.bedTemperatureChanged)
  131. self._output_device.hotendTemperaturesChanged.connect(self.hotendTemperatureChanged)
  132. self._output_device.bedTemperatureChanged.connect(self._onBedTemperatureChanged)
  133. self._output_device.hotendTemperaturesChanged.connect(self._onHotendTemperatureChanged)
  134. self._output_device.endstopStateChanged.connect(self._onEndstopStateChanged)
  135. except AttributeError as e: # Connection is probably not a USB connection. Something went pretty wrong if this happens.
  136. Logger.log("e", "An exception occurred while starting end stop polling: %s" % str(e))
  137. @pyqtSlot()
  138. def cooldownHotend(self):
  139. if self._output_device is not None:
  140. self._output_device.setTargetHotendTemperature(0, 0)
  141. @pyqtSlot()
  142. def cooldownBed(self):
  143. if self._output_device is not None:
  144. self._output_device.setTargetBedTemperature(0)
  145. @pyqtSlot()
  146. def heatupHotend(self):
  147. if self._output_device is not None:
  148. self._output_device.setTargetHotendTemperature(0, self._hotend_target_temp)
  149. @pyqtSlot()
  150. def heatupBed(self):
  151. if self._output_device is not None:
  152. self._output_device.setTargetBedTemperature(self._bed_target_temp)
  153. heatedBedChanged = pyqtSignal()
  154. @pyqtProperty(bool, notify = heatedBedChanged)
  155. def hasHeatedBed(self):
  156. global_container_stack = Application.getInstance().getGlobalContainerStack()
  157. return global_container_stack.getProperty("machine_heated_bed", "value")