TimeLapse.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  67. }"""
  68. def execute(self, data):
  69. feed_rate = self.getSettingValueByKey("park_feed_rate")
  70. park_print_head = self.getSettingValueByKey("park_print_head")
  71. x_park = self.getSettingValueByKey("head_park_x")
  72. y_park = self.getSettingValueByKey("head_park_y")
  73. trigger_command = self.getSettingValueByKey("trigger_command")
  74. pause_length = self.getSettingValueByKey("pause_length")
  75. gcode_to_append = ";TimeLapse Begin\n"
  76. last_x = 0
  77. last_y = 0
  78. if park_print_head:
  79. gcode_to_append += self.putValue(G=1, F=feed_rate,
  80. X=x_park, Y=y_park) + " ;Park print head\n"
  81. gcode_to_append += self.putValue(M=400) + " ;Wait for moves to finish\n"
  82. gcode_to_append += trigger_command + " ;Snap Photo\n"
  83. gcode_to_append += self.putValue(G=4, P=pause_length) + " ;Wait for camera\n"
  84. for idx, layer in enumerate(data):
  85. for line in layer.split("\n"):
  86. if self.getValue(line, "G") in {0, 1}: # Track X,Y location.
  87. last_x = self.getValue(line, "X", last_x)
  88. last_y = self.getValue(line, "Y", last_y)
  89. # Check that a layer is being printed
  90. lines = layer.split("\n")
  91. for line in lines:
  92. if ";LAYER:" in line:
  93. layer += gcode_to_append
  94. layer += "G0 X%s Y%s\n" % (last_x, last_y)
  95. data[idx] = layer
  96. break
  97. return data