Update TimeLapse.py

Update TimeLapse.py

Update to include a pause before the image is snapped.
This commit is contained in:
GregValiant 2024-08-22 12:47:15 -04:00
parent ee65d93fb6
commit ffc21403a1

View file

@ -1,18 +1,19 @@
# Copyright (c) 2020 Ultimaker B.V.
# Modified 5/15/2023 - Greg Valiant (Greg Foresi)
# Created by Wayne Porter
# Re-write April 15, 2024 by GregValiant (Greg Foresi)
# Changes:
# Added insertion frequency
# Added support for Relative Extrusion mode
# Added support for Firmware Retraction
# Changed Retract to a boolean and when True use the regular Cura retract settings.
# Adjusted for use with Relative Extrusion
# Changed Retract to a boolean and when true use the regular Cura retract settings.
# Use the regular Cura settings for Travel Speed and Speed_Z instead of asking.
# Added code to prevent retracts when parking if the filament was already retracted.
# Added code to check the E location to prevent retracts if the filament was already retracted.
# Added 'Pause before image' per LemanRus
from ..Script import Script
from UM.Application import Application
from UM.Logger import Logger
class TimeLapse(Script):
def __init__(self):
super().__init__()
def getSettingDataString(self):
return """{
@ -25,7 +26,7 @@ class TimeLapse(Script):
"trigger_command":
{
"label": "Camera Trigger Command",
"description": "G-code command used to trigger the camera. The command will be inserted at the ends of layers.",
"description": "G-code command used to trigger the camera. The setting box will take any command and parameters.",
"type": "str",
"default_value": "M240"
},
@ -45,12 +46,21 @@ class TimeLapse(Script):
"every_100th": "Every 100th"},
"default_value": "every_layer"
},
"anti_shake_length":
{
"label": "Pause before image",
"description": "How long to wait (in ms) before capturing the image. This is to allow the printer to 'settle down' after movement. To disable set this to '0'.",
"type": "int",
"default_value": 0,
"minimum_value": 0,
"unit": "ms "
},
"pause_length":
{
"label": "Wait for Camera",
"description": "How long to wait (in ms) after the camera was triggered before returning to the print.",
"label": "Pause after image",
"description": "How long to wait (in ms) after camera was triggered.",
"type": "int",
"default_value": 700,
"default_value": 500,
"minimum_value": 0,
"unit": "ms "
},
@ -64,7 +74,7 @@ class TimeLapse(Script):
"head_park_x":
{
"label": "Park Print Head X",
"description": "What X location does the head move to for the photo.",
"description": "What X location does the head move to for photo.",
"unit": "mm",
"type": "float",
"default_value": 0,
@ -73,7 +83,7 @@ class TimeLapse(Script):
"head_park_y":
{
"label": "Park Print Head Y",
"description": "What Y location does the head move to for the photo.",
"description": "What Y location does the head move to for photo.",
"unit": "mm ",
"type": "float",
"default_value": 0,
@ -82,7 +92,7 @@ class TimeLapse(Script):
"retract":
{
"label": "Retract when required",
"description": "Retract if there is not already a retraction. If unchecked then there will be no retraction even if there is none in the gcode. If retractions are not enabled in Cura there won't be a retraction regardless of this setting.",
"description": "Retract if there isn't already a retraction. If unchecked then there will be no retraction even if there is none in the gcode. If retractions are not enabled in Cura there won't be a retraction. regardless of this setting.",
"type": "bool",
"default_value": true
},
@ -98,7 +108,7 @@ class TimeLapse(Script):
"ensure_final_image":
{
"label": "Ensure Final Image",
"description": "Depending on how the layer numbers work out with the 'How Often' frequency there might not be an image taken at the end of the last layer. This will ensure that one is taken. There is no parking because the 'End Gcode' occurs immediately after.",
"description": "Depending on how the layer numbers work out with the 'How Often' frequency there might not be an image taken at the end of the last layer. This will ensure that one is taken. There is no parking as the Ending Gcode comes right up.",
"type": "bool",
"default_value": false
}
@ -106,20 +116,19 @@ class TimeLapse(Script):
}"""
def execute(self, data):
# Get the settings and initialize some variables
mycura = Application.getInstance().getGlobalContainerStack()
relative_extrusion = bool(mycura.getProperty("relative_extrusion", "value"))
extruder = mycura.extruderList
retract_speed = int(extruder[0].getProperty("retraction_speed", "value"))*60
retract_dist = int(extruder[0].getProperty("retraction_amount", "value"))
retract_dist = round(float(extruder[0].getProperty("retraction_amount", "value")), 2)
retract_enabled = bool(extruder[0].getProperty("retraction_enable", "value"))
firmware_retraction = bool(mycura.getProperty("machine_firmware_retract", "value"))
firmware_retract = bool(mycura.getProperty("machine_firmware_retract", "value"))
speed_z = int(extruder[0].getProperty("speed_z_hop", "value"))*60
if relative_extrusion:
rel_cmd = 83
else:
rel_cmd = 82
trav_speed = int(extruder[0].getProperty("speed_travel", "value"))*60
travel_speed = int(extruder[0].getProperty("speed_travel", "value"))*60
park_print_head = self.getSettingValueByKey("park_print_head")
x_park = self.getSettingValueByKey("head_park_x")
y_park = self.getSettingValueByKey("head_park_y")
@ -137,10 +146,13 @@ class TimeLapse(Script):
is_retracted = False
gcode_to_append = ""
if park_print_head:
gcode_to_append += f"G1 F{trav_speed} X{x_park} Y{y_park} ;Park print head\n"
gcode_to_append += f"G0 F{travel_speed} X{x_park} Y{y_park} ;Park print head\n"
gcode_to_append += "M400 ;Wait for moves to finish\n"
anti_shake_length = self.getSettingValueByKey("anti_shake_length")
if anti_shake_length > 0:
gcode_to_append += f"G4 P{anti_shake_length} ;Wait for printer to settle down\n"
gcode_to_append += trigger_command + " ;Snap the Image\n"
gcode_to_append += f"G4 P{pause_length} ;Wait for camera\n"
gcode_to_append += f"G4 P{pause_length} ;Wait for camera to finish\n"
match when_to_insert:
case "every_layer":
step_freq = 1
@ -187,38 +199,40 @@ class TimeLapse(Script):
else:
is_retracted = False
prev_e = last_e
if self.getValue(line, "G") in {10, 11}:
if line.startswith("G10"):
if firmware_retract and self.getValue(line, "G") in {10, 11}:
if self.getValue(line, "G") == 10:
is_retracted = True
if line.startswith("G11"):
last_e = float(prev_e) - float(retract_dist)
if self.getValue(line, "G") == 11:
is_retracted = False
# Insert the code----------------------------------------------------
last_e = float(prev_e) + float(retract_dist)
prev_e = last_e
lines = layer.split("\n")
# Insert the code----------------------------------------------------
camera_code = ""
for line in lines:
if ";LAYER:" in line:
# Retract unless already retracted
if retract and not is_retracted and retract_enabled:
if retract and not is_retracted and retract_enabled: # Retract unless already retracted
camera_code += ";TYPE:CUSTOM-----------------TimeLapse Begin\n"
camera_code += "M83 ;Extrude Relative\n"
if not firmware_retraction:
if not firmware_retract:
camera_code += f"G1 F{retract_speed} E-{retract_dist} ;Retract filament\n"
else:
camera_code += "G10 ;Retract filament\n"
else:
camera_code += ";TYPE:CUSTOM-----------------TimeLapse Begin\n"
if zhop != 0:
camera_code += f"G1 F{speed_z} Z{last_z + zhop} ;Z-Hop\n"
camera_code += f"G1 F{speed_z} Z{round(last_z + zhop,2)} ;Z-Hop\n"
camera_code += gcode_to_append
camera_code += self.putValue(G=0, F=trav_speed, X=last_x, Y=last_y) + " ;Restore XY position \n"
camera_code += f"G0 F{travel_speed} X{last_x} Y{last_y} ;Restore XY position\n"
if zhop != 0:
camera_code += self.putValue(G=0, F=speed_z, Z=last_z) + " ;Restore Z position \n"
camera_code += f"G0 F{speed_z} Z{last_z} ;Restore Z position\n"
if retract and not is_retracted and retract_enabled:
if not firmware_retraction:
camera_code += self.putValue(G=1, E=retract_dist, F=retract_speed) + " ;Un-Retract filament\n"
if not firmware_retract:
camera_code += f"G1 F{retract_speed} E{retract_dist} ;Un-Retract filament\n"
else:
camera_code += "G11 ;Un-Retract filament\n"
camera_code += self.putValue(M=rel_cmd) + " ;Extrude Mode\n"
camera_code += f"M{rel_cmd} ;Extrude Mode\n"
camera_code += f";{'-' * 28}TimeLapse End"
# Format the camera code to be inserted
temp_lines = camera_code.split("\n")
@ -229,8 +243,8 @@ class TimeLapse(Script):
lines.insert(len(lines) - 2, temp_lines)
data[num] = "\n".join(lines)
break
except:
pass
except Exception as e:
Logger.log("w", "TimeLapse Error: " + repr(e))
# Take a final image if there was no camera shot at the end of the last layer.
if "TimeLapse Begin" not in data[len(data) - (3 if retract_enabled else 2)] and ensure_final_image:
data[len(data)-1] = "M400 ; Wait for all moves to finish\n" + trigger_command + " ;Snap the final Image\n" + f"G4 P{pause_length} ;Wait for camera\n" + data[len(data)-1]