stop.g using conditional G code



  • I'm slowly trying to use conditional G code is as many areas as possible.
    Currently I'm working on stop.g

    At present I only have one extruder, so in the example I know which heater is going to need to be checked to determine if I can do a retract, but that's poor coding from a portability stand point.

    I'm struggling to find how to check which tools and associated heaters are active so I can avoid hard coded references.
    so instead of

    if {state.currentTool!=-1} ; check if any tools are active
    	G91 ; relative positioning
    	if heat.heaters[1].state!="Off" && heat.heaters[1].current > heat.coldRetractTemperature
       	G1 E-2 F300 ; retract the filament a bit before lifting the nozzle to release some of the pressure
       	M291 P"Retracted 2mm" R"Retracting" S0 T5
    	else
       	M291 P{"Not retracted...  Heater off or below extrude temp  " ^ heat.heaters[1].current ^ " : " ^ heat.coldRetractTemperature ^ "."} R"Retract" S0 T5
       G4 S5 ; wait for popup
    

    I'd like to return either the currently active heater(s) in an array which can be passed either in a loop or a single statement.
    There doesn't seem to be a "maxHeaters" or similar in the object model that would allow me to do it in a loop.

    For interest here is the entire stop.g as it currently stands.
    There is some code that could be combined, but for testing purposes I have separated it even though I'm checking the same thing a couple of times.

    ; stop.g
    ; called when M0 (Stop) is run (e.g. when a print from SD card is cancelled)
    ;
    ; generated by RepRapFirmware Configuration Tool v2.1.4 on Sat Jan 04 2020 09:46:45 GMT+1000 (Australian Eastern Standard Time)
    
    ; 
    if {state.currentTool!=-1} ; check if any tools are active
    	G91 ; relative positioning
    	if (heat.heaters[1].state!="off") && (heat.heaters[1].current > heat.coldRetractTemperature)
    		G1 E-2 F300 ; retract the filament a bit before lifting the nozzle to release some of the pressure
    		M291 P"Retracted 2mm" R"Retracting" S0 T5
    		G4 S5 ; wait for popup
    	else
    		M291 P{"Not retracted...  Heater off or below extrude temp  " ^ heat.heaters[1].current ^ " : " ^ heat.coldRetractTemperature ^ "."} R"Retract" S0 T5
    		G4 S5 ; wait for popup
    else
    	M291 P"No active tool" R"Check tools" S0 T2
    
    if {!move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed} ; check if the machine is homed
    	M291 P"Insufficient axis homed.  Cannot retract or park" R"Parking" S0 T5
    else
    	G90 ; absolute positioning
    	if {(move.axes[2].machinePosition) < (move.axes[2].max - 10)} ; check if there's sufficient space to raise head
    		M291 P{"Raising head to...  " ^ move.axes[2].machinePosition+5}  R"Raising head" S0 T5
    		G1 Z+5 F9000 ; move Z up a bit
    	else
    		M291 P{"Cannot raise head- insufficient space  " ^ move.axes[2].machinePosition ^ " : " ^ (move.axes[2].max - 10) ^ "."} R"Raising head" S0 T5
    		G4 S5 ; wait for popup to display
    	G1 X10 Y150 ; parks X head pushes bed out to front so you can pull part
    	M400 ; wait for current moves to finish
    
    if {state.currentTool!=-1}
    	G10 S0 R0 ; Set active and standby temps to zero
    	T-1 P0 ; if any tools selected, deselect all
    else
    	T0 ; Select a tool
    	M116; wait for any temps to stabilise
    	G10 S0 R0 ; Set active and standby temps to zero to ensure nothing is on standby
    	T-1 ; deselect all tools
    M140 S0 ; heated bed heater off 
    M106 P0 S0 ; part fan off
    M84 ; steppers off
    G90 ; absolute positioning
    
    M98 P"0:/macros/songs/itchyscratchy.g" ; play finish tune
    


  • @OwenD said in stop.g using conditional G code:

    {1}

    Not sure why these show in the code windows above. "{1}"
    They're not in my macro


  • administrators

    You can do something this (assuming either 0 or 1 heaters per tool):

    if state.currentTool >= 0
      if #tools[state.currentTool].heaters > 0 & heat.heaters[tools[state.currentTool].heaters[0]].current > heat.coldRetractTemperature
        G1 E-2 F300
    


  • Aaahhh!
    Missed that # operator.
    Frustrating coming from another language at times.

    Link for anyone else
    https://duet3d.dozuki.com/Wiki/GCode_Meta_Commands#Section_Unary_prefix_operators


Log in to reply