Query E4 state in object model



  • Well the title says what I think I need to do, but I can't work out how to do it (I did look at M409 as described here: https://forum.duet3d.com/topic/14878/conditional-gcode-expressions/2?_=1590053013630)

    My system:
    Duet2 Wifi, Duex5, PanelDue
    Latest RRF 3.1.1

    I have installed 4 momentary push buttons with a PanelDue, these connect GPIO1 - GPIO4 to earth when activated, I have assigned them the macro files trigger2.g - trigger4.g. I want one button to turn on/off some LED lighting, I have "on" working but currently need to use a second button to turn off, presumably this can be done with conditional Gcode but I can't see how to query the current state of the output E4?

    Display.jpg image url)

    ![64f73439-e002-4548-bfc5-58f6d0877f68-image.png](/assets/uploads/files/1590054764141-64f73439-e002-4548-bfc5-58f6d0877f68-image.png) code_text
    ```config.g
    : External trigger inputs
    ; these are the 4 push buttons on my front panel, 1 (top) - 4 (bottom)
    
    M950 J1 C"^duex.gp1"  				; define input pin 1 using GPIO1 and enable pullup
    M581 P1 T2 S0 R0 					; associate input pin 1 with trigger2.g (top switch)
    
    M950 J2 C"^duex.gp2"  				; define input pin 2 using GPIO2 and enable pullup
    M581 P2 T3 S0 R0 					; associate input pin 2 with trigger3.g
    
    M950 J3 C"^duex.gp3"  				; define input pin 3 using GPIO3 and enable pullup
    M581 P3 T4 S0 R0 					; associate input pin 3 with trigger4.g
    
    M950 J4 C"^duex.gp4"  				; define input pin 4 using GPIO4 and enable pullup
    M581 P4 T5 S0 R0 					; associate input pin 4 with trigger5.g (bottom switch)
    
    ; output ports
    M950 P0 C"duex.e4heat" 				; assign output pin 0 to E4 on the duex5 - enclosure lights
    ;M950 P1 C"!duex.pwm4"  				; assign output pin 1 to E5 on the duex5 - bed lights
    
    
    trigger5.g file
    ; Trigger number 0 causes an emergency stop as if M112 had been received. 
    ; Trigger number 1 causes the print to be paused as if M25 had been received. 
    ; Any trigger number # greater than 1 causes the macro file sys/trigger#.g to be executed. 
    
    ; need conditional Gcode here so I can select the value of Sx depending on the current state of E4
    M42 P0 S0					          ; turn on output pin 0 (base lights)
    M118 S"Switch 1 pressed"                                ; print message for debugging purposes


  • Last 2 lines of code should read as follows (I can't edit them because my update got flagged as spam!)

    M42 P0 S1					; turn on output pin 0 (base lights)
    M118 S"Switch 4 pressed"                                ; print message for debugging purposes


  • Try something like

    if {state.gpOut[0].pwm = 1}
    	echo "Pin 0 is high"
    elif {state.gpOut[0].pwm = 0}
    	echo "Pin 0 is low"
    else
    	echo {"Pin 0 value is" ^ state.gpOut[0].pwm}
    


  • Or something like this

    while iterations < #state.gpOut
    	if {state.gpOut[iterations]=null}
    		echo {"gpOut " ^ iterations ^ " is not assigned"}
    		G4 S1
    	else
    		echo {"gpOut " ^ iterations ^ " value is " ^ state.gpOut[iterations].pwm}
    		G4 S1
    


  • That worked, thanks.

    In case anyone else needs it, this is the working config:

    config.g

    : External trigger inputs
    ; these are the 4 push buttons on my front panel, 1 (top) - 4 (bottom)
    
    M950 J1 C"^duex.gp1"  				; define input pin 1 using GPIO1 and enable pullup
    M581 P1 T2 S0 R0 					; associate input pin 1 with trigger2.g (top switch)
    
    M950 J2 C"^duex.gp2"  				; define input pin 2 using GPIO2 and enable pullup
    M581 P2 T3 S0 R0 					; associate input pin 2 with trigger3.g
    
    M950 J3 C"^duex.gp3"  				; define input pin 3 using GPIO3 and enable pullup
    M581 P3 T4 S0 R0 					; associate input pin 3 with trigger4.g
    
    M950 J4 C"^duex.gp4"  				; define input pin 4 using GPIO4 and enable pullup
    M581 P4 T5 S0 R0 					; associate input pin 4 with trigger5.g (bottom switch)
    
    ; output ports
    M950 P0 C"duex.e4heat" 				; assign output pin 0 to E4 on the duex5 - enclosure lights
    ;M950 P1 C"duex.e5heat"  				; assign output pin 1 to E5 on the duex5 - bed lights
    

    trigger5 file

    ; Trigger number 0 causes an emergency stop as if M112 had been received. 
    ; Trigger number 1 causes the print to be paused as if M25 had been received. 
    ; Any trigger number # greater than 1 causes the macro file sys/trigger#.g to be executed. 
    				
    ; trigger5 runs if switch 4 pressed", turns the LED connected to E4 on/off
    ; checks state of output P0 and reverses the state
    if {state.gpOut[0].pwm = 1}
    	M42 P0 S0										; turn off output pin 0 
    elif {state.gpOut[0].pwm = 0}
    	M42 P0 S1										; turn on output pin 0 
    else
    	echo {"Pin 0 value is" ^ state.gpOut[0].pwm}
            M42 P0 S0

Log in to reply