Invitation to share your conditional GCode scripts



  • I was looking for a Thermostatic Fan trigger and was pointed to a post about daemon.g. Based RRF3.1-RC9 + SBC I now have my fan control for my power tied in.

    This conditional logic is really powerful.. thanks for all the hard work.

    daemon.g

    M98 P"0:/macros/Special Scripts/Check Power Supply"
    
    G4 S10 ; wait 10 seconds
    

    Special Scripts/Check Power Supply

    if fans[3].actualValue > 0.0 && sensors.analog[3].lastReading < 25
        ;echo "fan running and temp less than 25c... stop fan"
        M106 P3 S0.0
    elif fans[3].actualValue < 0.5 && sensors.analog[3].lastReading > 35
        ;echo "fan stopped and temp greater than 35c... start fan"
        M106 P3 S1.0
    


  • Another example using daemon.g

    I'm in the process of adding some extra safety checks.
    This will check the heater temps when idle and initiate action such as shutting off all power, activating an alarm (probably requires a latching relay)
    As RRF checks this when printing there's no need to run this section of the code unless idle.

    ; daemon.g
    ; check temps every 10 seconds when not printing (i.e. Idle) and shut down if exceed max temps.  RRF checks when printing, but not when idle.
    ; not all commands are required.  e.g. shutting off power by dropping a relay negates need for M112, M81 - un-comment or adjust as required for your configuration
    ; assumes that the thermistor for each heater is monitor # zero as there is no way to do nested loops using "iterations" as identifier in both loops until variables are available in the object model
    ; no checking done for null values! - 
    
    if state.status="idle"
    	while iterations < #heat.heaters ; 
    		;if heat.heaters[iterations].current > heat.heaters[iterations].monitors[0].limit ; un-comment when ready to go live
    		if heat.heaters[iterations].current > 50 ; uncomment for testing, comment out when going live
    			M291 P"Heater over max temp" R"Temp Error!" S1 T0 ; Display message for testing
    			;M112 ; Do emergency stop
    			;M81  ; Turn PS_ON (ATX) off
    			;M42 P5 S1 ; Set GPIO 5 (output) high to activate alarm relay. GPIO pin must be configured using M950
    			;M42 P6 S0 ; Set GPIO 6 (output) low to cut power. GPIO pin must be configured using M950
    
    ;if sensors.gpIn[6].value=1 ; check state of connected smoke detector - GPIO pin must be configured using M950
    	;M291 P"Smoke detector active!" R"I'm on fire!" S1 T0 ; Display Nikki Lauder message for testing
    	;M112 ; Do emergency stop
    	;M81  ; Turn PS_ON (ATX) off
    	;M42 P5 S1 ; Set GPIO 5 (output) high to activate alarm relay
    	;M42 P6 S0 ; Set GPIO 6 (output) low to cut power	
    		
    G4 S10 ; wait 10 seconds before next check	(adjust S parameter as you see fit)
    


  • @OwenD Please can I make a suggestion, at the beginning, issue a

    M290 R0 S0
    

    to reset baby stepping before you try to start performing jogging Z and trigger heights.

    Just this minute made that mistake!

    Paul



  • @PaulHew said in Invitation to share your conditional GCode scripts:

    @OwenD Please can I make a suggestion, at the beginning, issue a

    M290 R0 S0
    

    to reset baby stepping before you try to start performing jogging Z and trigger heights.

    Just this minute made that mistake!

    Paul

    Good idea!
    So let it be written...



  • Code to check if all axes are homed and optionally home them, or cancel macro.
    It iterates across ALL axes making it more portable code (for those with >3 axis)
    This saves manually listing them in an if || (or) statement such as

    if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed
    

    I imagine this would be useful for tool changing printers where you might be doing something that may or may not require homing.
    For the rest of us, I'd just run G28 if any are not homed.

    ;first check if any axes are not homed and optionally home them 
    while iterations < #move.axes
    	if !move.axes[iterations].homed ; check if each axis is homed
    		M291 P{"Axis " ^ move.axes[iterations].letter ^ " is not homed.  Home now?"} R"Axis un-homed" S3 ; continue or abort?
    		M98 P{"0:/sys/home" ^ (move.axes[iterations].letter) ^ ".g"} ; home axis if answer is OK - G28 doesn't accept object variable so run macro
    		
    ; rest of code here
    


  • Here is what I use to choose machine type at startup depending on what tool is connected.
    I include a resistor connected to the sensor pin inside the tool connector. This gives a fixed temperature reading and lets the machine choose its type depending on what is connected at startup.

    M308 S23 P"e2temp" Y"thermistor" T100000 B3950 A"ModeSelector"   ;setup a thermistor input as config selection sensor 
    
    
    ;wait for the sensor to start returning meaningful values (don't know if there is a better approach for this) 
    while sensors.analog[23].lastReading==0||sensors.analog[23].lastReading==2000
      echo
    
    ;print the value detected (useful when adding a new mode)
    ;M117 ModeSelector value:{sensors.analog[23].lastReading}
    
    ;depending on sensor reading choose a system folder
    if sensors.analog[23].lastReading==-273.1
      M117 No Attachment Detected.
    elif sensors.analog[23].lastReading<155
      M117 Going to be a CNC.
      M505 P"CNC"
    else 
      M117 Going to be a 3D printer.
      M505 P"Printer"
    
    ;load config.g from the chosen system folder
    M98 P"config.g"
    


  • That’s pretty cool. I’m considering my using a serial line with some tiny85 chips that I have laying around to do the same thing. For now I write out a script that I try to call from config.g that just sets up the last executed tool script.

    Setup 3 to 1

    M28 "0:/sys/lasttool.g"
    M98 P"0:/macros/Config Scripts/Setup 3 to 1 Hotend"
    M29
    

    I have a script that uses conditional code to clear out tools, fans, and heaters as well as de-energizing my relays (stepper bank switch).

    Change Toolhead

    ;echo "check temperature"
    while iterations < #sensors.analog
    	if sensors.analog[iterations] != null && sensors.analog[iterations].lastReading > 32 && sensors.analog[iterations].name != "PS"
    		if sensors.analog[iterations].name != "Bed"
    			abort "Must wait for tool head to cool down!!!"
    
    ;echo "set relays"
    M18 E ; Turn off Steppers for extruders
    G4 P100
    M42 P0 S0 ; Ensure relay 0 is off
    M42 P1 S0 ; Ensure relay 1 is off
    M42 P2 S0 ; Ensure relay 2 is off
    G4 P100
    
    ;echo "remove all tools"
    while iterations < #tools
    	;echo "Removing Tool ", tools[iterations].name, "T", iterations
    	M563 P{iterations} D-1 H-1
    
    ;echo "disable heaters"
    while iterations < #heat.heaters
    	if heat.bedHeaters[0] != iterations
    		;echo "Removing Temp Sensor and heater", sensors.analog[heat.heaters[iterations].sensor].name, "T", heat.heaters[iterations].sensor, "H", iterations
    		M308 S{heat.heaters[iterations].sensor} P"nil" Y"nil"
    		M950 H{iterations} C"nil"
    
    ;echo "remove fans"
    while iterations < #fans
    	if fans[iterations] != null && fans[iterations ].name != "Power Supply"
    		;echo "Removing Fan", fans[iterations].name, "F", iterations
    		M950 F{iterations} C"nil"
    
    M30 "0:/sys/lasttool.g"
    


  • Once reading I²C bus is implemented (it is maybe already done in current release, I didn't check yet), using a I²C EEPROM can be a way to identify a device; this is often used in consumer products (Gopro does or did that for their accessories).



  • @fma said in Invitation to share your conditional GCode scripts:

    Once reading I²C bus is implemented (it is maybe already done in current release, I didn't check yet), using a I²C EEPROM can be a way to identify a device; this is often used in consumer products (Gopro does or did that for their accessories).

    It also opens the possibility to store calibration data etc in the tool independent of the machine, ref IEEE1451-ish. Although Duet3d avoided using I2C for their fillament sensor (unlike Prusa) as the bus isn't intended for long runs. Maybe we'll see 1-wire or something in the future?



  • Well, the filement sensor needs continuous readings, which is not the case for calibration or so.


Log in to reply