TimeLapse.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. # Created by Wayne Porter
  4. from ..Script import Script
  5. class TimeLapse(Script):
  6. def __init__(self):
  7. super().__init__()
  8. def getSettingDataString(self):
  9. return """{
  10. "name": "Time Lapse",
  11. "key": "TimeLapse",
  12. "metadata": {},
  13. "version": 2,
  14. "settings":
  15. {
  16. "trigger_command":
  17. {
  18. "label": "Trigger camera command",
  19. "description": "G-code command used to trigger camera.",
  20. "type": "str",
  21. "default_value": "M240"
  22. },
  23. "pause_length":
  24. {
  25. "label": "Pause length",
  26. "description": "How long to wait (in ms) after camera was triggered.",
  27. "type": "int",
  28. "default_value": 700,
  29. "minimum_value": 0,
  30. "unit": "ms"
  31. },
  32. "park_print_head":
  33. {
  34. "label": "Park Print Head",
  35. "description": "Park the print head out of the way. Assumes absolute positioning.",
  36. "type": "bool",
  37. "default_value": true
  38. },
  39. "head_park_x":
  40. {
  41. "label": "Park Print Head X",
  42. "description": "What X location does the head move to for photo.",
  43. "unit": "mm",
  44. "type": "float",
  45. "default_value": 0,
  46. "enabled": "park_print_head"
  47. },
  48. "head_park_y":
  49. {
  50. "label": "Park Print Head Y",
  51. "description": "What Y location does the head move to for photo.",
  52. "unit": "mm",
  53. "type": "float",
  54. "default_value": 190,
  55. "enabled": "park_print_head"
  56. },
  57. "park_feed_rate":
  58. {
  59. "label": "Park Feed Rate",
  60. "description": "How fast does the head move to the park coordinates.",
  61. "unit": "mm/s",
  62. "type": "float",
  63. "default_value": 9000,
  64. "enabled": "park_print_head"
  65. },
  66. "retract":
  67. {
  68. "label": "Retraction Distance",
  69. "description": "Filament retraction distance for camera trigger.",
  70. "unit": "mm",
  71. "type": "int",
  72. "default_value": 0
  73. },
  74. "zhop":
  75. {
  76. "label": "Z-Hop Height When Parking",
  77. "description": "Z-hop length before parking",
  78. "unit": "mm",
  79. "type": "float",
  80. "default_value": 0
  81. }
  82. }
  83. }"""
  84. def execute(self, data):
  85. feed_rate = self.getSettingValueByKey("park_feed_rate")
  86. park_print_head = self.getSettingValueByKey("park_print_head")
  87. x_park = self.getSettingValueByKey("head_park_x")
  88. y_park = self.getSettingValueByKey("head_park_y")
  89. trigger_command = self.getSettingValueByKey("trigger_command")
  90. pause_length = self.getSettingValueByKey("pause_length")
  91. retract = int(self.getSettingValueByKey("retract"))
  92. zhop = self.getSettingValueByKey("zhop")
  93. gcode_to_append = ";TimeLapse Begin\n"
  94. last_x = 0
  95. last_y = 0
  96. last_z = 0
  97. if park_print_head:
  98. gcode_to_append += self.putValue(G=1, F=feed_rate,
  99. X=x_park, Y=y_park) + " ;Park print head\n"
  100. gcode_to_append += self.putValue(M=400) + " ;Wait for moves to finish\n"
  101. gcode_to_append += trigger_command + " ;Snap Photo\n"
  102. gcode_to_append += self.putValue(G=4, P=pause_length) + " ;Wait for camera\n"
  103. for idx, layer in enumerate(data):
  104. for line in layer.split("\n"):
  105. if self.getValue(line, "G") in {0, 1}: # Track X,Y,Z location.
  106. last_x = self.getValue(line, "X", last_x)
  107. last_y = self.getValue(line, "Y", last_y)
  108. last_z = self.getValue(line, "Z", last_z)
  109. # Check that a layer is being printed
  110. lines = layer.split("\n")
  111. for line in lines:
  112. if ";LAYER:" in line:
  113. if retract != 0: # Retract the filament so no stringing happens
  114. layer += self.putValue(M=83) + " ;Extrude Relative\n"
  115. layer += self.putValue(G=1, E=-retract, F=3000) + " ;Retract filament\n"
  116. layer += self.putValue(M=82) + " ;Extrude Absolute\n"
  117. layer += self.putValue(M=400) + " ;Wait for moves to finish\n" # Wait to fully retract before hopping
  118. if zhop != 0:
  119. layer += self.putValue(G=1, Z=last_z+zhop, F=3000) + " ;Z-Hop\n"
  120. layer += gcode_to_append
  121. if zhop != 0:
  122. layer += self.putValue(G=0, X=last_x, Y=last_y, Z=last_z) + "; Restore position \n"
  123. else:
  124. layer += self.putValue(G=0, X=last_x, Y=last_y) + "; Restore position \n"
  125. if retract != 0:
  126. layer += self.putValue(M=400) + " ;Wait for moves to finish\n"
  127. layer += self.putValue(M=83) + " ;Extrude Relative\n"
  128. layer += self.putValue(G=1, E=retract, F=3000) + " ;Retract filament\n"
  129. layer += self.putValue(M=82) + " ;Extrude Absolute\n"
  130. data[idx] = layer
  131. break
  132. return data