PrinterOutputDevice.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. from UM.OutputDevice.OutputDevice import OutputDevice
  2. from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
  3. from enum import IntEnum # For the connection state tracking.
  4. from UM.Logger import Logger
  5. from UM.Signal import signalemitter
  6. ## Printer output device adds extra interface options on top of output device.
  7. #
  8. # The assumption is made the printer is a FDM printer.
  9. #
  10. # Note that a number of settings are marked as "final". This is because decorators
  11. # are not inherited by children. To fix this we use the private counter part of those
  12. # functions to actually have the implementation.
  13. #
  14. # For all other uses it should be used in the same way as a "regular" OutputDevice.
  15. @signalemitter
  16. class PrinterOutputDevice(QObject, OutputDevice):
  17. def __init__(self, device_id, parent = None):
  18. super().__init__(device_id = device_id, parent = parent)
  19. self._target_bed_temperature = 0
  20. self._bed_temperature = 0
  21. self._num_extruders = 1
  22. self._hotend_temperatures = [0] * self._num_extruders
  23. self._target_hotend_temperatures = [0] * self._num_extruders
  24. self._progress = 0
  25. self._head_x = 0
  26. self._head_y = 0
  27. self._head_z = 0
  28. self._connection_state = ConnectionState.closed
  29. self._time_elapsed = 0
  30. self._time_total = 0
  31. def requestWrite(self, node, file_name = None, filter_by_machine = False):
  32. raise NotImplementedError("requestWrite needs to be implemented")
  33. ## Signals
  34. # Signal to be emitted when bed temp is changed
  35. bedTemperatureChanged = pyqtSignal()
  36. # Signal to be emitted when target bed temp is changed
  37. targetBedTemperatureChanged = pyqtSignal()
  38. # Signal when the progress is changed (usually when this output device is printing / sending lots of data)
  39. progressChanged = pyqtSignal()
  40. # Signal to be emitted when hotend temp is changed
  41. hotendTemperaturesChanged = pyqtSignal()
  42. # Signal to be emitted when target hotend temp is changed
  43. targetHotendTemperaturesChanged = pyqtSignal()
  44. # Signal to be emitted when head position is changed (x,y,z)
  45. headPositionChanged = pyqtSignal()
  46. # Signal that is emitted every time connection state is changed.
  47. # it also sends it's own device_id (for convenience sake)
  48. connectionStateChanged = pyqtSignal(str)
  49. timeElapsedChanged = pyqtSignal()
  50. timeTotalChanged = pyqtSignal()
  51. ## Get the bed temperature of the bed (if any)
  52. # This function is "final" (do not re-implement)
  53. # /sa _getBedTemperature implementation function
  54. @pyqtProperty(float, notify = bedTemperatureChanged)
  55. def bedTemperature(self):
  56. return self._bed_temperature
  57. ## Set the (target) bed temperature
  58. # This function is "final" (do not re-implement)
  59. # /param temperature new target temperature of the bed (in deg C)
  60. # /sa _setTargetBedTemperature implementation function
  61. @pyqtSlot(int)
  62. def setTargetBedTemperature(self, temperature):
  63. self._setTargetBedTemperature(temperature)
  64. self._target_bed_temperature = temperature
  65. self.targetBedTemperatureChanged.emit()
  66. ## Time the print has been printing.
  67. # Note that timeTotal - timeElapsed should give time remaining.
  68. @pyqtProperty(float, notify = timeElapsedChanged)
  69. def timeElapsed(self):
  70. return self._time_elapsed
  71. ## Total time of the print
  72. # Note that timeTotal - timeElapsed should give time remaining.
  73. @pyqtProperty(float, notify=timeTotalChanged)
  74. def timeTotal(self):
  75. return self._time_total
  76. @pyqtSlot(float)
  77. def setTimeTotal(self, new_total):
  78. if self._time_total != new_total:
  79. self._time_total = new_total
  80. self.timeTotalChanged.emit()
  81. @pyqtSlot(float)
  82. def setTimeElapsed(self, time_elapsed):
  83. if self._time_elapsed != time_elapsed:
  84. self._time_elapsed = time_elapsed
  85. self.timeElapsedChanged.emit()
  86. ## Home the head of the connected printer
  87. # This function is "final" (do not re-implement)
  88. # /sa _homeHead implementation function
  89. @pyqtSlot()
  90. def homeHead(self):
  91. self._homeHead()
  92. ## Home the head of the connected printer
  93. # This is an implementation function and should be overriden by children.
  94. def _homeHead(self):
  95. Logger.log("w", "_homeHead is not implemented by this output device")
  96. ## Home the bed of the connected printer
  97. # This function is "final" (do not re-implement)
  98. # /sa _homeBed implementation function
  99. @pyqtSlot()
  100. def homeBed(self):
  101. self._homeBed()
  102. ## Home the bed of the connected printer
  103. # This is an implementation function and should be overriden by children.
  104. # /sa homeBed
  105. def _homeBed(self):
  106. Logger.log("w", "_homeBed is not implemented by this output device")
  107. ## Protected setter for the bed temperature of the connected printer (if any).
  108. # /parameter temperature Temperature bed needs to go to (in deg celsius)
  109. # /sa setTargetBedTemperature
  110. def _setTargetBedTemperature(self, temperature):
  111. Logger.log("w", "_setTargetBedTemperature is not implemented by this output device")
  112. ## Protected setter for the current bed temperature.
  113. # This simply sets the bed temperature, but ensures that a signal is emitted.
  114. # /param temperature temperature of the bed.
  115. def _setBedTemperature(self, temperature):
  116. self._bed_temperature = temperature
  117. self.bedTemperatureChanged.emit()
  118. ## Get the target bed temperature if connected printer (if any)
  119. @pyqtProperty(int, notify = targetBedTemperatureChanged)
  120. def targetBedTemperature(self):
  121. return self._target_bed_temperature
  122. ## Set the (target) hotend temperature
  123. # This function is "final" (do not re-implement)
  124. # /param index the index of the hotend that needs to change temperature
  125. # /param temperature The temperature it needs to change to (in deg celsius).
  126. # /sa _setTargetHotendTemperature implementation function
  127. @pyqtSlot(int, int)
  128. def setTargetHotendTemperature(self, index, temperature):
  129. self._setTargetHotendTemperature(index, temperature)
  130. self._target_hotend_temperatures[index] = temperature
  131. self.targetHotendTemperaturesChanged.emit()
  132. ## Implementation function of setTargetHotendTemperature.
  133. # /param index Index of the hotend to set the temperature of
  134. # /param temperature Temperature to set the hotend to (in deg C)
  135. # /sa setTargetHotendTemperature
  136. def _setTargetHotendTemperature(self, index, temperature):
  137. Logger.log("w", "_setTargetHotendTemperature is not implemented by this output device")
  138. @pyqtProperty("QVariantList", notify = targetHotendTemperaturesChanged)
  139. def targetHotendTemperatures(self):
  140. return self._target_hotend_temperatures
  141. @pyqtProperty("QVariantList", notify = hotendTemperaturesChanged)
  142. def hotendTemperatures(self):
  143. return self._hotend_temperatures
  144. ## Protected setter for the current hotend temperature.
  145. # This simply sets the hotend temperature, but ensures that a signal is emitted.
  146. # /param index Index of the hotend
  147. # /param temperature temperature of the hotend (in deg C)
  148. def _setHotendTemperature(self, index, temperature):
  149. self._hotend_temperatures[index] = temperature
  150. self.hotendTemperaturesChanged.emit()
  151. ## Attempt to establish connection
  152. def connect(self):
  153. raise NotImplementedError("connect needs to be implemented")
  154. ## Attempt to close the connection
  155. def close(self):
  156. raise NotImplementedError("close needs to be implemented")
  157. @pyqtProperty(bool, notify = connectionStateChanged)
  158. def connectionState(self):
  159. return self._connection_state
  160. ## Set the connection state of this output device.
  161. # /param connection_state ConnectionState enum.
  162. def setConnectionState(self, connection_state):
  163. self._connection_state = connection_state
  164. self.connectionStateChanged.emit(self._id)
  165. ## Ensure that close gets called when object is destroyed
  166. def __del__(self):
  167. self.close()
  168. ## Get the x position of the head.
  169. # This function is "final" (do not re-implement)
  170. @pyqtProperty(float, notify = headPositionChanged)
  171. def headX(self):
  172. return self._head_x
  173. ## Get the y position of the head.
  174. # This function is "final" (do not re-implement)
  175. @pyqtProperty(float, notify = headPositionChanged)
  176. def headY(self):
  177. return self._head_y
  178. ## Get the z position of the head.
  179. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  180. # This function is "final" (do not re-implement)
  181. @pyqtProperty(float, notify = headPositionChanged)
  182. def headZ(self):
  183. return self._head_z
  184. ## Update the saved position of the head
  185. # This function should be called when a new position for the head is recieved.
  186. def _updateHeadPosition(self, x, y ,z):
  187. position_changed = False
  188. if self._head_x != x:
  189. self._head_x = x
  190. position_changed = True
  191. if self._head_y != y:
  192. self._head_y = y
  193. position_changed = True
  194. if self._head_z != z:
  195. self._head_z = z
  196. position_changed = True
  197. if position_changed:
  198. self.headPositionChanged.emit()
  199. ## Set the position of the head.
  200. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  201. # This function is "final" (do not re-implement)
  202. # /param x new x location of the head.
  203. # /param y new y location of the head.
  204. # /param z new z location of the head.
  205. # /param speed Speed by which it needs to move (in mm/minute)
  206. # /sa _setHeadPosition implementation function
  207. @pyqtSlot("long", "long", "long")
  208. @pyqtSlot("long", "long", "long", "long")
  209. def setHeadPosition(self, x, y, z, speed = 3000):
  210. self._setHeadPosition(x, y , z, speed)
  211. ## Set the X position of the head.
  212. # This function is "final" (do not re-implement)
  213. # /param x x position head needs to move to.
  214. # /param speed Speed by which it needs to move (in mm/minute)
  215. # /sa _setHeadx implementation function
  216. @pyqtSlot("long")
  217. @pyqtSlot("long", "long")
  218. def setHeadX(self, x, speed = 3000):
  219. self._setHeadX(x, speed)
  220. ## Set the Y position of the head.
  221. # This function is "final" (do not re-implement)
  222. # /param y y position head needs to move to.
  223. # /param speed Speed by which it needs to move (in mm/minute)
  224. # /sa _setHeadY implementation function
  225. @pyqtSlot("long")
  226. @pyqtSlot("long", "long")
  227. def setHeadY(self, y, speed = 3000):
  228. self._setHeadY(y, speed)
  229. ## Set the Z position of the head.
  230. # In some machines it's actually the bed that moves. For convenience sake we simply see it all as head movements.
  231. # This function is "final" (do not re-implement)
  232. # /param z z position head needs to move to.
  233. # /param speed Speed by which it needs to move (in mm/minute)
  234. # /sa _setHeadZ implementation function
  235. @pyqtSlot("long")
  236. @pyqtSlot("long", "long")
  237. def setHeadZ(self, z, speed = 3000):
  238. self._setHeadY(z, speed)
  239. ## Move the head of the printer.
  240. # Note that this is a relative move. If you want to move the head to a specific position you can use
  241. # setHeadPosition
  242. # This function is "final" (do not re-implement)
  243. # /param x distance in x to move
  244. # /param y distance in y to move
  245. # /param z distance in z to move
  246. # /param speed Speed by which it needs to move (in mm/minute)
  247. # /sa _moveHead implementation function
  248. @pyqtSlot("long", "long", "long")
  249. @pyqtSlot("long", "long", "long", "long")
  250. def moveHead(self, x = 0, y = 0, z = 0, speed = 3000):
  251. self._moveHead(x, y, z, speed)
  252. ## Implementation function of moveHead.
  253. # /param x distance in x to move
  254. # /param y distance in y to move
  255. # /param z distance in z to move
  256. # /param speed Speed by which it needs to move (in mm/minute)
  257. # /sa moveHead
  258. def _moveHead(self, x, y, z, speed):
  259. Logger.log("w", "_moveHead is not implemented by this output device")
  260. ## Implementation function of setHeadPosition.
  261. # /param x new x location of the head.
  262. # /param y new y location of the head.
  263. # /param z new z location of the head.
  264. # /param speed Speed by which it needs to move (in mm/minute)
  265. # /sa setHeadPosition
  266. def _setHeadPosition(self, x, y, z, speed):
  267. Logger.log("w", "_setHeadPosition is not implemented by this output device")
  268. ## Implementation function of setHeadX.
  269. # /param x new x location of the head.
  270. # /param speed Speed by which it needs to move (in mm/minute)
  271. # /sa setHeadX
  272. def _setHeadX(self, x, speed):
  273. Logger.log("w", "_setHeadX is not implemented by this output device")
  274. ## Implementation function of setHeadY.
  275. # /param y new y location of the head.
  276. # /param speed Speed by which it needs to move (in mm/minute)
  277. # /sa _setHeadY
  278. def _setHeadY(self, y, speed):
  279. Logger.log("w", "_setHeadY is not implemented by this output device")
  280. ## Implementation function of setHeadZ.
  281. # /param z new z location of the head.
  282. # /param speed Speed by which it needs to move (in mm/minute)
  283. # /sa _setHeadZ
  284. def _setHeadZ(self, z, speed):
  285. Logger.log("w", "_setHeadZ is not implemented by this output device")
  286. ## Get the progress of any currently active process.
  287. # This function is "final" (do not re-implement)
  288. # /sa _getProgress
  289. # /returns float progress of the process. -1 indicates that there is no process.
  290. @pyqtProperty(float, notify = progressChanged)
  291. def progress(self):
  292. return self._progress
  293. ## Set the progress of any currently active process
  294. # /param progress Progress of the process.
  295. def setProgress(self, progress):
  296. if self._progress != progress:
  297. self._progress = progress
  298. self.progressChanged.emit()
  299. ## The current processing state of the backend.
  300. class ConnectionState(IntEnum):
  301. closed = 0
  302. connecting = 1
  303. connected = 2
  304. busy = 3
  305. error = 4