PrinterOutputDevice.py 15 KB

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