PrinterOutputDevice.py 17 KB

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