Conditional Code Based on temperature
-
So, I've just finished building a Jubilee Tool Changer with 4 tools. On my single nozzle machines, I usually have a prime and retract command to pull and push the filament through the Revo nozzle to prevent oozing when it starts and stops. I'd like to do something similar with my Jubilee Tool Changer printer.
It normally goes something like this for retract
M83 ; Relative extrusion mode G1 E-18 F800 ; retract 18mm filament
and then this for priming.
M83 ; Relative extrusion mode G1 E18 F800 ; prime 18mm filament
I have the tfreeX.g, tpreX.g, tpostX.g files for each tool to dock and undock, but experimenting with PrusaSlicer trying to make it do it through start code and tool change code doesn't work well if I don't start with T0.
So, is there any way to check the tool is active, at active temperature, and currently printing a file within my tfreeX.g for a retract something like:
; tfree0.g ; Runs at the start of a toolchange if the current tool is tool-0. ; Note: tool offsets are applied at this point unless we preempt commands with G53! G91 ; Relative Mode. G1 Z2 ; Pop Z up slightly so we don't crash while traveling over the usable bed region. G90 ; Absolute Mode. G53 G0 X11 Y5 F12000 ; Rapid to the back of the post. Stay away from the tool rack so we don't collide with tools. ; ; This position must be chosen such that the most protruding y face of the current tool ; ; (while on the carriage) does not collide with the most protruding y face of any parked tool. if job.file.fileName!=null if heat.heaters[1].current = heat.heaters[1].active M83 ; Relative extrusion mode G1 E-18 F800 ; retract 18mm filament M591 D0 S0 ; Disable filament sensor G53 G1 Y-40 F6000 ; Controlled move to the park position with tool-1. (park_x, park_y) M98 P"/macros/tool_unlock.g" ; Unlock the tool G53 G1 Y-5 F6000 ; Retract the pin.
And then something like this for initial tool pickup and prime
; tpost0.g ; called after firmware thinks Tool0 is selected ; Note: tool offsets are applied at this point! ; Note that commands preempted with G53 will NOT apply the tool offset. M116 P0 ; Wait for set temperatures to be reached M302 P0 ; Prevent Cold Extrudes, just in case temp setpoints are at 0 G90 ; Ensure the machine is in absolute mode before issuing movements. G53 G1 Y-40 F6000 ; Move to the pickup position with tool-0. M98 P"/macros/tool_lock.g" ; Lock the tool if job.file.fileName!=null if heat.heaters[1].current = heat.heaters[1].active M83 ; Relative extrusion mode G1 E18 F800 ; prime 18mm filament G53 G1 Y-5 F6000 ; Move off the parking posts M591 D0 S1 ; Enable filament sensor G1 R2 Z0 ; Restore prior Z position before tool change was initiated. ; Note: tool tip position is automatically saved to slot 2 upon the start of a tool change. ; Restore Z first so we don't crash the tool on retraction. G1 R0 Y0 ; Retract tool by restoring Y position next now accounting for new tool offset. ; Restoring Y next ensures the tool is fully removed from parking post. G1 R0 X0 ; Restore X position now accounting for new tool offset. M106 R2 ; restore print cooling fan speed
Is there a way to check the tool is at or close to active temperatures? And that those temperatures aren't below the cold extrusion? Something like what M116 does, but in an if statement? I know I could do this without the if statements, but I'd rather avoid the warning messages when I'm manually changing tools not in a print or not at temperature. That's partly why I enable/disable the filament sensor for the tool change.
-
-
So, after reading through some of the object model documentation, I think I'm going to try it with this section tonight and see if I get the desired behavior. I just wish there was an easier way to duplicate a similar behavior to M116 and check the temp is above cold extrusion.
; state.currentTool - Number of the currently selected tool or -1 if none is selected ; tools[].heaters[] - List of associated heaters (indices) ; heat.heaters[].state == "active" ; heat.heaters[].active - Active temperature of the heater (in C) ; heat.heaters[].current - Current temperature of the heater (in C) ; heat.coldExtrudeTemperature - Minimum required temperature for extrusion moves (in C) ; heat.coldRetractTemperature - Minimum required temperature for retraction moves (in C) var tool = state.currentTool; var heater = tools[tool].heaters[0]; var minPrintTemp = max(heat.coldExtrudeTemperature, heat.coldRetractTemperature); var curPrintTempTol = 5; var curTemp = heat.heaters[heater].current; var minActiveTemp = heat.heaters[heater].active - curPrintTempTol; var isActive = heat.heaters[heater].state == "active"; if state.status == "processing" if curTemp > minActiveTemp && curTemp > minPrintTemp && isActive M83 ; Relative extrusion mode G1 E-18 F800 ; retract 18mm filament
But, one question regarding the use of var. If I define a var in a macro such as the tool change macro. Since it is only local to my tool change, I can basically use the same var/code in all my tool change macros. If that's true, then the above code will become it's own macro that'll becalled from the tool change macros to reduce the amount of copied code.
-
@PDBeal yes,
var
is local to the macro (technically block-local,var
within an if/while statement is not available outside of that; var at the beginning of a macro is available for all of the macro since that's the logical block).global
would be used for globally / cross macro variables, which also can be useful at times.See https://docs.duet3d.com/en/User_manual/Reference/Gcode_meta_commands#variables for official docs.
-
@PDBeal said in Conditional Code Based on temperature:
I just wish there was an easier way to duplicate a similar behavior to M116 and check the temp is above cold extrusion.
I'm not exactly sure what it is that you want to achieve but I use a while loop in my "pre-print" macro instead of M116. This sort of thing
while sensors.analog[1].lastReading < (some number, variable or expression) ........ ........ G4 S5
In that example "sensors.analog[1]" is the hot end heater so essentially the loop will check the sensor value and if it's less that "something" (in my case a global variable representing the hot end set point for a particular filament) , wait 5 seconds then check again and repeat until the sensor is at or above that "something".
Does that help?
-
@deckingman said in Conditional Code Based on temperature:
Not specifically. I was looking for something that would return true if M116 P0 was satisified that I could then check with a minimum setpoint for extrusion.I have what I want working now, although having to proceed all defined variables with "var." was interesting. I saw that in the documentation, but it didn't click till I tried to run my gcode files.
In the end, this is what I came up with if anyone else needs something like this in the future and it works for my intended purposes without interfering with manual tool changes.
tretract.g
; tretract.g ; This will retract filament from the heatzone on tool change if printing and at temperature ; ; Variable details ;------------------------------------------------------------------------------------ ; state.currentTool - Number of the currently selected tool or -1 if none is selected ; tools[].heaters[] - List of associated heaters (indices) ; heat.heaters[].state == "active" ; heat.heaters[].active - Active temperature of the heater (in C) ; heat.heaters[].current - Current temperature of the heater (in C) ; heat.coldExtrudeTemperature - Minimum required temperature for extrusion moves (in C) ; heat.coldRetractTemperature - Minimum required temperature for retraction moves (in C) M116 P0 ; Wait for set temperatures to be reached M302 P0 ; Prevent Cold Extrudes, just in case temp setpoints are at 0 var tool = state.currentTool; var heater = tools[var.tool].heaters[0]; var minPrintTemp = max(heat.coldExtrudeTemperature, heat.coldRetractTemperature); var curPrintTempTol = 5; var curTemp = heat.heaters[var.heater].current; var minActiveTemp = heat.heaters[var.heater].active - var.curPrintTempTol; var isActive = heat.heaters[var.heater].state == "active"; if state.status == "processing" if var.curTemp > var.minActiveTemp && var.curTemp > var.minPrintTemp && var.isActive M83 ; Relative extrusion mode G1 E-18 F800 ; retract 18mm filament
tprime.g
; tprime.g ; This will prime the heatzone on tool change if printing and at temperature ; ; Variable details ;------------------------------------------------------------------------------------ ; state.currentTool - Number of the currently selected tool or -1 if none is selected ; tools[].heaters[] - List of associated heaters (indices) ; heat.heaters[].state == "active" ; heat.heaters[].active - Active temperature of the heater (in C) ; heat.heaters[].current - Current temperature of the heater (in C) ; heat.coldExtrudeTemperature - Minimum required temperature for extrusion moves (in C) ; heat.coldRetractTemperature - Minimum required temperature for retraction moves (in C) M116 P0 ; Wait for set temperatures to be reached M302 P0 ; Prevent Cold Extrudes, just in case temp setpoints are at 0 var tool = state.currentTool; var heater = tools[var.tool].heaters[0]; var minPrintTemp = max(heat.coldExtrudeTemperature, heat.coldRetractTemperature); var curPrintTempTol = 5; var curTemp = heat.heaters[var.heater].current; var minActiveTemp = heat.heaters[var.heater].active - var.curPrintTempTol; var isActive = heat.heaters[var.heater].state == "active"; if state.status == "processing" if var.curTemp > var.minActiveTemp && var.curTemp > var.minPrintTemp && var.isActive M83 ; Relative extrusion mode G1 E20 F800 ; prime 20mm filament
And I just use this in my postX.g files
; tpost0.g ; called after firmware thinks Tool0 is selected ; Note: tool offsets are applied at this point! ; Note that commands preempted with G53 will NOT apply the tool offset. M116 P0 ; Wait for set temperatures to be reached M302 P0 ; Prevent Cold Extrudes, just in case temp setpoints are at 0 G90 ; Ensure the machine is in absolute mode before issuing movements. G53 G1 Y-40 F6000 ; Move to the pickup position with tool-0. M98 P"/macros/tool_lock.g" ; Lock the tool M98 P"/sys/tprime.g" ; prime filament into the heatzone G53 G1 Y-5 F6000 ; Move off the parking posts M591 D0 S1 ; Enable filament sensor G1 R2 Z0 ; Restore prior Z position before tool change was initiated. ; Note: tool tip position is automatically saved to slot 2 upon the start of a tool change. ; Restore Z first so we don't crash the tool on retraction. G1 R0 Y0 ; Retract tool by restoring Y position next now accounting for new tool offset. ; Restoring Y next ensures the tool is fully removed from parking post. G1 R0 X0 ; Restore X position now accounting for new tool offset. M106 R2 ; restore print cooling fan speed
And this in my tfreeX.g files
; tfree0.g ; Runs at the start of a toolchange if the current tool is tool-0. ; Note: tool offsets are applied at this point unless we preempt commands with G53! G91 ; Relative Mode. G1 Z2 ; Pop Z up slightly so we don't crash while traveling over the usable bed region. G90 ; Absolute Mode. G53 G0 X11 Y5 F12000 ; Rapid to the back of the post. Stay away from the tool rack so we don't collide with tools. ; ; This position must be chosen such that the most protruding y face of the current tool ; ; (while on the carriage) does not collide with the most protruding y face of any parked tool. M98 P"/sys/tretract.g" ; retract filament out of heatzone M591 D0 S0 ; Disable filament sensor G53 G1 Y-40 F6000 ; Controlled move to the park position with tool-1. (park_x, park_y) M98 P"/macros/tool_unlock.g" ; Unlock the tool G53 G1 Y-5 F6000 ; Retract the pin.
-