Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Yet more conditional gcode help required

    Scheduled Pinned Locked Moved
    Gcode meta commands
    6
    36
    2.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dc42undefined
      dc42 administrators
      last edited by

      See https://duet3d.dozuki.com/Wiki/Gcode#Section_M581_RepRapFirmware_3_01RC2_and_later. The P parameter accepts multiple GpIn pin numbers.

      Duet WiFi hardware designer and firmware engineer
      Please do not ask me for Duet support via PM or email, use the forum
      http://www.escher3d.com, https://miscsolutions.wordpress.com

      1 Reply Last reply Reply Quote 0
      • deckingmanundefined
        deckingman
        last edited by

        OK, I think I've sussed it but would appreciate a sanity check.

        Config.g now has this......

        M950 J5 C"^0.io5.in" ; main board(0) io5, joystick left 
        M950 J6 C"^0.io6.in" ; main board(0) io6, joystick right
        M950 J7 C"^0.io7.in" ; main board(0) io7, joystick forward 
        M950 J8 C"^0.io8.in" ; main board(0) io8, joystick backwards
        M581 P5:6:7:8 T5 R0 S0 ;Run trigger5 macro - (joystick left,right, forward or backwards)
        ; *** Note to self - change R0 to R2 after upgrading to firmware 3.2 ** 
        

        .....and the macro looks like this.....

        ; ******Trigger 5 - Joystick left,right, forward or backwards ********
        ; IO Pin to switch connections J5=left, J6=Right, J7=Forward, J8=Backward
        
        
        G91; Set relative
        M400; wait for any moves to finish
        G1 F6000 ; set feedrate to 100mm/sec
        while sensors.gpIn[5].value=0 | sensors.gpIn[6].value=0 | sensors.gpIn[7].value=0 | sensors.gpIn[8].value=0
        	if sensors.gpIn[5].value=0 ; joystick left
        		G1 X-1 U-1 A-1
        	elif sensors.gpIn[6].value=0 ; joystick right	
        		G1 X1 U1 A1
        	elif sensors.gpIn[7].value=0 ; joystick forwards	
        		G1 Y-1 V-1 B-1
        	elif sensors.gpIn[8].value=0 ; joystick backwards	
        		G1 Y1 V1 B1
        	elif sensors.gpIn[5].value=0 & sensors.gpIn[7]; joystick left and forward	
        		G1 X-1 U-1 A-1 Y-1 V-1 B-1
        	elif sensors.gpIn[5].value=0 & sensors.gpIn[8]; joystick left and backwards	
        		G1 X-1 U-1 A-1 Y1 V1 B1
        	elif sensors.gpIn[6].value=0 & sensors.gpIn[7]; joystick right and forward	
        		G1 X1 U1 A1 Y-1 V-1 B-1
        	elif sensors.gpIn[6].value=0 & sensors.gpIn[8]; joystick right and backward	
        		G1 X1 U1 A1 Y1 V1 B1
        

        I'm not sure about the "while". What I'm trying to say is while any one of those 4 pins is being pulled low by a switch do the following.

        After that, I've used 8 "if" or "elif" statements to define what happens with the various switch combinations that are possible. Is that the way to do it?

        Ian
        https://somei3deas.wordpress.com/
        https://www.youtube.com/@deckingman

        OwenDundefined dc42undefined 2 Replies Last reply Reply Quote 0
        • OwenDundefined
          OwenD @deckingman
          last edited by

          @deckingman You'll probably need to move the dual input directions in front of the uni-input directions.
          Otherwise they won't execute using elif because each combination move will have previously equated to true in the single input section.

          deckingmanundefined 1 Reply Last reply Reply Quote 0
          • deckingmanundefined
            deckingman @OwenD
            last edited by

            @OwenD said in Yet more conditional gcode help required:

            @deckingman You'll probably need to move the dual input directions in front of the uni-input directions.
            Otherwise they won't execute using elif because each combination move will have previously equated to true in the single input section.

            Ahh - good point. Does the rest of it look reasonable?

            Ian
            https://somei3deas.wordpress.com/
            https://www.youtube.com/@deckingman

            OwenDundefined 1 Reply Last reply Reply Quote 0
            • OwenDundefined
              OwenD @deckingman
              last edited by

              @deckingman said in Yet more conditional gcode help required:

              Ahh - good point. Does the rest of it look reasonable?

              Looks reasonably sane.
              You may need an M400 in each elif.
              I'm not sure what the cycle time will be so you might experience an accumulation of moves by the time you take your hand off the joystick

              deckingmanundefined 1 Reply Last reply Reply Quote 0
              • deckingmanundefined
                deckingman @OwenD
                last edited by

                @OwenD said in Yet more conditional gcode help required:

                @deckingman said in Yet more conditional gcode help required:

                Ahh - good point. Does the rest of it look reasonable?

                Looks reasonably sane.

                That's unusual for me - I spend most of my time doing crazy stuff 🙂

                You may need an M400 in each elif.

                Good point - I'll plaster a few more M400s around

                I'm not sure what the cycle time will be so you might experience an accumulation of moves by the time you take your hand off the joystick

                Yes I had considered that - hence the 1mm "nudge move" length to start with. Guess I could either shorten that to (say) 0.1mm and/or scatter a few G4 commands around.

                Ian
                https://somei3deas.wordpress.com/
                https://www.youtube.com/@deckingman

                1 Reply Last reply Reply Quote 0
                • taconiteundefined
                  taconite
                  last edited by

                  One suggestion: At the first 4 if statements I would add an "AND" with the negated statements of the last 4 if statements. Otherwise when you push the joystick to left forward the ifs "left", "forward" and "left forward" will be executed.

                  Or wait I think what I just read is only the case if using if instead of elif ...

                  Custom ANET A8
                  Custom Delta: D-PATCH (Delta Printer with Automatic Tool CHanging) https://forum.duet3d.com/topic/16082/d-patch?_=1596131234754

                  All I do here is under this license: CC BY-NC-SA

                  1 Reply Last reply Reply Quote 0
                  • dc42undefined
                    dc42 administrators @deckingman
                    last edited by dc42

                    That's the right approach, however the if..elif statements are not right. Try something like this:

                        if sensors.gpIn[5].value=0           ; if left button down
                            if sensors.gpIn[7].value=0       ; if joystick left and forward	
                    	    G1 ...
                    	elif sensors.gpIn[8].value=0     ; if joystick left and backwards	
                    	    G1 ...
                            else                             ; just left
                    	    G1 ... 
                        elif sensors.gpIn[6].value=0         ; right button down
                    	if sensors.gpIn[7].value=0       ; joystick right and forwards	
                    	    G1 ...
                            elif sensors.gpIn[8].value=0     ; joystick right and backwards	
                    	    G1 ...
                            else                             ; just right
                    	    G1 X1 U1 A1
                        elif sensors.gpIn[7].value=0         ; if just forwards
                    	G1 ...
                        elif sensors.gpIn[8].value=0         ; if just backwards
                    	G1 ...
                    

                    You could also add an "else break" at the end, then you wouldn't need to test all the buttons in the while-part.

                    Duet WiFi hardware designer and firmware engineer
                    Please do not ask me for Duet support via PM or email, use the forum
                    http://www.escher3d.com, https://miscsolutions.wordpress.com

                    deckingmanundefined OwenDundefined 2 Replies Last reply Reply Quote 0
                    • deckingmanundefined
                      deckingman @dc42
                      last edited by

                      @dc42 Thanks. That looks good. From my very limited knowledge, I can see that would be "conflict free" compared to what I had.

                      @all. Thanks for the input everyone - much appreciated. It'll be a couple of days or so before I get time to get the hardware and wiring sorted. Then I'll either report back with a successful outcome, or I'll be asking for more help........

                      Ian
                      https://somei3deas.wordpress.com/
                      https://www.youtube.com/@deckingman

                      1 Reply Last reply Reply Quote 1
                      • OwenDundefined
                        OwenD @dc42
                        last edited by

                        @dc42 said in Yet more conditional gcode help required:

                        That's the right approach, however the if..elif statements are not right. Try something like this:

                        You could also add an "else break" at the end, then you wouldn't need to test all the buttons in the while-part.

                        If he removes the while test of all pins, wouldn't the macro only trigger once (when a switch signal rose/fell)
                        Or do you just mean use "while true" instead (hence the break statement )
                        I'm always reluctant to use potentially infinite loops.

                        1 Reply Last reply Reply Quote 0
                        • dc42undefined
                          dc42 administrators
                          last edited by dc42

                          Yes I meant use "while true". Or if you don't like infinite loops, "while iterations < 1000" or similar.

                          Duet WiFi hardware designer and firmware engineer
                          Please do not ask me for Duet support via PM or email, use the forum
                          http://www.escher3d.com, https://miscsolutions.wordpress.com

                          OwenDundefined 1 Reply Last reply Reply Quote 0
                          • OwenDundefined
                            OwenD @dc42
                            last edited by OwenD

                            @dc42 Thanks for clarification.
                            I might have locked up a few machines over the years with my clumsy work when using loops.
                            I realise it's a valid method for those that think things through more than me before they press "executive" edit: "execute" (Stupid iPhone!!!) 😆

                            1 Reply Last reply Reply Quote 0
                            • deckingmanundefined
                              deckingman
                              last edited by

                              Err, in view of the above and as a further sanity check on my understanding, here is the full macro that I intend to start with ........

                              G91; Set relative
                              M400; wait for any moves to finish
                              G1 F6000 ; set feedrate to 100mm/sec
                              
                              while sensors.gpIn[5].value=0 | sensors.gpIn[6].value=0 | sensors.gpIn[7].value=0 | sensors.gpIn[8].value=0
                              	
                              	if sensors.gpIn[5].value=0; joystick left 	
                              		
                              		if sensors.gpIn[7].value=0 ; joystick left and forwards 
                              			M400 
                              			G1 X-1 U-1 A-1 Y-1 V-1 B-1
                              			
                              		elif sensors.gpIn[8]; joystick left and backwards	
                              			M400
                              			G1 X-1 U-1 A-1 Y1 V1 B1
                              		
                              		else ; joystick left only
                              			M400
                              			G1 X-1 U-1 A-1
                              			
                              	elif sensors.gpIn[6].value=0; joystick right 	
                              		
                              		if sensors.gpIn[7].value=0 ; joystick right and forwards 
                              			M400
                              			G1 X1 U1 A1 Y-1 V-1 B-1
                              		
                              			
                              		elif sensors.gpIn[8]; joystick right and backward	
                              			M400
                              			G1 X1 U1 A1 Y1 V1 B1
                              			
                              		else ; joystick right only
                              			M400
                              			G1 X1 U1 A1
                              	
                              	elif sensors.gpIn[7].value=0 ; joystick forwards only	
                              		M400
                              		G1 Y-1 V-1 B-1
                              		
                              	elif sensors.gpIn[8].value=0 ; joystick backward only
                              		M400
                              		G1 Y1 V1 B1
                              		
                              	else break
                              

                              Is that about right?

                              Ian
                              https://somei3deas.wordpress.com/
                              https://www.youtube.com/@deckingman

                              1 Reply Last reply Reply Quote 0
                              • OwenDundefined
                                OwenD
                                last edited by OwenD

                                To clarify my comments.

                                A "while" statement will loop if the condition test is true.
                                In your case by testing all those inputs.

                                If you put

                                while true
                                  G1 X1
                                

                                Then you have an endless loop as true always equals true, so you're going to crash.
                                Therefore you must ensure that you put a test in somewhere the calls "break" to get you out of the loop.

                                In the suggested code above that test is satisfied when none of the IF statements resolve to true, so break is called.

                                1 Reply Last reply Reply Quote 0
                                • deckingmanundefined
                                  deckingman
                                  last edited by

                                  OK, so that's all wired up and working. It does what I hoped it would and it respects the axis limits too (of course it does - I never doubted it would 🙂 ).

                                  But I'd like to add an embellishment if possible. I've used 1mm as a movement length which is great for reasonably precise positioning but naturally, it's a bit slow if I want to travel the full 400mm or so of axis travel. Increasing the step size to (say) 5mm is about right but then I lose precision. So what I'd like to do is a bit like how one sets the time on some digital clocks. That is to say, the longer one holds the button, the faster the time changes.

                                  So I'd like to alter the code so that if I hold the joystick in one position, it will move in 1mm increments but after (say) 5 iterations, the move length will increase to (say) 5mm). Ideally I'd like to do something a bit more sophisticated still, like keep increasing the move length the longer the joystick is held in one position but I'll settle for just a binary change after say 5 iterations of the same move.

                                  Here is the code that I have now (without the errors that I had in some of the elif commands when I last posted it 🙂 )

                                  G91; Set relative
                                  M400; wait for any moves to finish
                                  G1 F6000 ; set feedrate to 100mm/sec
                                  M118 S"Trigger 5 - Joystick"
                                  while sensors.gpIn[5].value=0 | sensors.gpIn[6].value=0 | sensors.gpIn[7].value=0 | sensors.gpIn[8].value=0
                                  	
                                  	if sensors.gpIn[5].value=0; joystick left 	
                                  		
                                  		if sensors.gpIn[7].value=0 ; joystick left and forwards 
                                  			M400 
                                  			G1 X-1 U-1 A-1 Y-1 V-1 B-1
                                  			echo "Left and forward"
                                  			
                                  		elif sensors.gpIn[8].value=0; joystick left and backwards	
                                  			M400
                                  			G1 X-1 U-1 A-1 Y1 V1 B1
                                  			echo "Left and backwards"
                                  			
                                  		else ; joystick left only
                                  			M400
                                  			G1 X-1 U-1 A-1
                                  			echo "Left"
                                  			
                                  	elif sensors.gpIn[6].value=0; joystick right 	
                                  		
                                  		if sensors.gpIn[7].value=0 ; joystick right and forwards 
                                  			M400
                                  			G1 X1 U1 A1 Y-1 V-1 B-1
                                  			echo "Right and forward"
                                  			
                                  		elif sensors.gpIn[8].value=0; joystick right and backward	
                                  			M400
                                  			G1 X1 U1 A1 Y1 V1 B1
                                  			echo "Right and backwards"
                                  			
                                  		else ; joystick right only
                                  			M400
                                  			G1 X1 U1 A1
                                  			echo "Right"
                                  	
                                  	elif sensors.gpIn[7].value=0 ; joystick forwards only	
                                  		M400
                                  		G1 Y-1 V-1 B-1
                                  		echo "Forward"
                                  		
                                  	elif sensors.gpIn[8].value=0 ; joystick backward only
                                  		M400
                                  		G1 Y1 V1 B1
                                  		echo "Backwards"
                                  		
                                  	else break
                                  

                                  Ian
                                  https://somei3deas.wordpress.com/
                                  https://www.youtube.com/@deckingman

                                  1 Reply Last reply Reply Quote 1
                                  • dc42undefined
                                    dc42 administrators
                                    last edited by

                                    You could use the built-in 'iterations' variable to adjust the movement amount and/or speed.

                                    Duet WiFi hardware designer and firmware engineer
                                    Please do not ask me for Duet support via PM or email, use the forum
                                    http://www.escher3d.com, https://miscsolutions.wordpress.com

                                    deckingmanundefined 1 Reply Last reply Reply Quote 0
                                    • deckingmanundefined
                                      deckingman @dc42
                                      last edited by deckingman

                                      @dc42 said in Yet more conditional gcode help required:

                                      You could use the built-in 'iterations' variable to adjust the movement amount and/or speed.

                                      Thanks. Would I be right in assuming the "iterations" is a counter which increments by 1 every time the while loop runs?

                                      Scratch that - found it.

                                      Ian
                                      https://somei3deas.wordpress.com/
                                      https://www.youtube.com/@deckingman

                                      1 Reply Last reply Reply Quote 0
                                      • deckingmanundefined
                                        deckingman
                                        last edited by

                                        That's all working now although the code is a bit "clunky".

                                        I'm guessing that once variables are introduced, I ought to be able to do something like ....

                                        MoveAmount= 0.5+(iterations/5) 
                                        G1 X MoveAmount..............
                                        

                                        .......or some such. Anyway, this is what I've ended up with which may not be elegant but works.

                                        
                                        G91; Set relative
                                        M400; wait for any moves to finish
                                        G1 F3000 ; set feedrate to 100mm/sec
                                        M118 S"Trigger 5 - Joystick"
                                        while sensors.gpIn[5].value=0 | sensors.gpIn[6].value=0 | sensors.gpIn[7].value=0 | sensors.gpIn[8].value=0
                                        	
                                        	if sensors.gpIn[5].value=0; joystick left 	
                                        		
                                        		if sensors.gpIn[7].value=0 ; joystick left and forwards 
                                        			M400 
                                        			if iterations<3
                                        				G1 X-0.5 U-0.5 A-0.5 Y-0.5 V-0.5 B-0.5
                                        			elif iterations<5
                                        				G1 X-1 U-1 A-1 Y-1 V-1 B-1
                                        			elif iterations<7
                                        				G1 X-2 U-2 A-2 Y-2 V-2 B-2
                                        			elif iterations<9
                                        				G1 X-5 U-5 A-5 Y-5 V-5 B-5	
                                        			elif iterations<11
                                        				G1 X-10 U-10 A-10 Y-10 V-10 B-10
                                        			else 
                                        				G1 X-20 U-20 A-20 Y-20 V-20 B-20	
                                        			
                                        			
                                        		elif sensors.gpIn[8].value=0; joystick left and backwards	
                                        			M400
                                        			if iterations<3
                                        				G1 X-0.5 U-0.5 A-0.5 Y0.5 V0.5 B0.5
                                        			elif iterations<5
                                        				G1 X-1 U-1 A-1 Y1 V1 B1
                                        			elif iterations<7
                                        				G1 X-2 U-2 A-2 Y2 V2 B2
                                        			elif iterations<9
                                        				G1 X-5 U-5 A-5 Y5 V5 B5	
                                        			elif iterations<11
                                        				G1 X-10 U-10 A-10 Y10 V10 B10
                                        			else 
                                        				G1 X-20 U-20 A-20 Y20 V20 B20
                                        			
                                        			
                                        		else ; joystick left only
                                        			M400
                                        			if iterations<3
                                        				G1 X-0.5 U-0.5 A-0.5 
                                        			elif iterations<5
                                        				G1 X-1 U-1 A-1 
                                        			elif iterations<7
                                        				G1 X-2 U-2 A-2 
                                        			elif iterations<9
                                        				G1 X-5 U-5 A-5 
                                        			elif iterations<11
                                        				G1 X-10 U-10 A-10 
                                        			else 
                                        				G1 X-20 U-20 A-20
                                        			
                                        						
                                        	elif sensors.gpIn[6].value=0; joystick right 	
                                        		
                                        		if sensors.gpIn[7].value=0 ; joystick right and forwards 
                                        			M400
                                        			if iterations<3
                                        				G1 X0.5 U0.5 A0.5 Y-0.5 V-0.5 B-0.5
                                        			elif iterations<5
                                        				G1 X1 U1 A1 Y-1 V-1 B-1
                                        			elif iterations<7
                                        				G1 X2 U2 A2 Y-2 V-2 B-2
                                        			elif iterations<9
                                        				G1 X5 U5 A5 Y-5 V-5 B-5	
                                        			elif iterations<11
                                        				G1 X10 U10 A10 Y-10 V-10 B-10
                                        			else 
                                        				G1 X20 U20 A20 Y-20 V-20 B-20
                                        			
                                        			
                                        		elif sensors.gpIn[8].value=0; joystick right and backward	
                                        			M400
                                        			if iterations<3
                                        				G1 X0.5 U0.5 A0.5 Y0.5 V0.5 B0.5
                                        			elif iterations<5
                                        				G1 X1 U1 A1 Y1 V1 B1
                                        			elif iterations<7
                                        				G1 X2 U2 A2 Y2 V2 B2
                                        			elif iterations<9
                                        				G1 X5 U5 A5 Y5 V5 B5	
                                        			elif iterations<11
                                        				G1 X10 U10 A10 Y10 V10 B10
                                        			else 
                                        				G1 X20 U20 A20 Y20 V20 B20
                                        			
                                        			
                                        		else ; joystick right only
                                        			M400
                                        			if iterations<3
                                        				G1 X0.5 U0.5 A0.5
                                        			elif iterations<5
                                        				G1 X1 U1 A1 
                                        			elif iterations<7
                                        				G1 X2 U2 A2 
                                        			elif iterations<9
                                        				G1 X5 U5 A5 
                                        			elif iterations<11
                                        				G1 X10 U10 A10 
                                        			else 
                                        				G1 X20 U20 A20 
                                        			
                                        	
                                        	elif sensors.gpIn[7].value=0 ; joystick forwards only	
                                        		M400
                                        		if iterations<3
                                        				G1 Y-0.5 V-0.5 B-0.5
                                        			elif iterations<5
                                        				G1 Y-1 V-1 B-1
                                        			elif iterations<7
                                        				G1 Y-2 V-2 B-2
                                        			elif iterations<9
                                        				G1 Y-5 V-5 B-5	
                                        			elif iterations<11
                                        				G1 Y-10 V-10 B-10
                                        			else 
                                        				G1 Y-20 V-20 B-20
                                        				
                                        	elif sensors.gpIn[8].value=0 ; joystick backward only
                                        		M400
                                        		if iterations<3
                                        				G1 Y0.5 V0.5 B0.5
                                        			elif iterations<5
                                        				G1 Y1 V1 B1
                                        			elif iterations<7
                                        				G1 Y2 V2 B2
                                        			elif iterations<9
                                        				G1 Y5 V5 B5	
                                        			elif iterations<11
                                        				G1 Y10 V10 B10
                                        			else 
                                        				G1 Y20 V20 B20
                                        		
                                        		
                                        	else break
                                        	
                                        

                                        Ian
                                        https://somei3deas.wordpress.com/
                                        https://www.youtube.com/@deckingman

                                        1 Reply Last reply Reply Quote 0
                                        • dc42undefined
                                          dc42 administrators
                                          last edited by dc42

                                          Another way is to use your original code but replace all the XYUVABC coordinates in the G1 commands by calculated values, something like this:

                                          G1 X{0.5+0.1 * iterations} Y{-(0.5+0.1 * iterations)} ...

                                          Duet WiFi hardware designer and firmware engineer
                                          Please do not ask me for Duet support via PM or email, use the forum
                                          http://www.escher3d.com, https://miscsolutions.wordpress.com

                                          deckingmanundefined 1 Reply Last reply Reply Quote 0
                                          • deckingmanundefined
                                            deckingman @dc42
                                            last edited by

                                            @dc42 said in Yet more conditional gcode help required:

                                            Another way is to use your original code but replace all the XYUVABC coordinates in the G1 commands by calculated values, something like this:

                                            G1 X{0.5+0.1 * iterations} Y{-(0.5+0.1 * iterations)} ...

                                            Ahhh - that looks good! I tried something similar but it didn't work - because I used plain braces rather than curly ones. I'll give that a go..............

                                            Ian
                                            https://somei3deas.wordpress.com/
                                            https://www.youtube.com/@deckingman

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Unless otherwise noted, all forum content is licensed under CC-BY-SA