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

    Meta commands

    Scheduled Pinned Locked Moved
    Gcode meta commands
    4
    31
    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.
    • jens55undefined
      jens55
      last edited by

      Thanks for that but I still don't quite understand. From what I understand (yeah, very little), the 'continue' statement does go back to the top like you said but the block that has the 'continue' statement is in the loop of 'if iteration < 10.' So in my thinking, if iterations gets to 10, that 'if' block fails and the next executed statement should be 'abort' .... ????

      DanS79undefined alankilianundefined 2 Replies Last reply Reply Quote 0
      • DanS79undefined
        DanS79 @jens55
        last edited by DanS79

        @jens55

        It's just a guess, but since no commands are being run when the if statement fails interations might not get incremented (maybe a bug).

        As a developer I'm not sure i like the concept of an iterations variable.

        the documentation says this about it.
        iterations - The number of completed iterations of the innermost loop

        If you have nested looping structures it could get difficult to follow the code.

        if i have all the syntax correct, something like this is more standard in the software development world.

        var cnt = 0
        while cnt < 10
            set var.cnt = cnt + 1
            m118 s"hello world"
            g4 s2
        
        
        
        jens55undefined 1 Reply Last reply Reply Quote 0
        • alankilianundefined
          alankilian @jens55
          last edited by alankilian

          @jens55 said in Meta commands:

          Thanks for that but I still don't quite understand. From what I understand (yeah, very little), the 'continue' statement does go back to the top like you said but the block that has the 'continue' statement is in the loop of 'if iteration < 10.' So in my thinking, if iterations gets to 10, that 'if' block fails and the next executed statement should be 'abort' .... ????

          You can take that 'continue' statement out completely and see that the macro runs just the same.

          Every time the interpreter gets to the end of the loop it will jump back to the top of the loop.

          So when the 'if' statement is not true, the interpreter does go to the next 'statement'. If there are no more statements in the loop, so it goes back to the top.

          This might be a better way to do the loop and make it more understandable to you.

          while iterations < 10
              m118 s"hello world"
              g4 s2
          

          Or this might make it even MORE clear (I tested this one!)

          m118 s"Starting the loop"
          while iterations < 10
                m118 s"hello world"
                g4 s2
          m118 s"Done with the loop"
          
          8/12/2021, 2:27:26 PM 	Done with the loop
          8/12/2021, 2:27:24 PM 	hello world
          8/12/2021, 2:27:22 PM 	hello world
          8/12/2021, 2:27:20 PM 	hello world
          8/12/2021, 2:27:18 PM 	hello world
          8/12/2021, 2:27:16 PM 	hello world
          8/12/2021, 2:27:14 PM 	hello world
          8/12/2021, 2:27:12 PM 	hello world
          8/12/2021, 2:27:10 PM 	hello world
          8/12/2021, 2:27:08 PM 	hello world
          8/12/2021, 2:27:06 PM 	M98 P"0:/macros/000_test"
          Starting the loop
          hello world
          
          1 Reply Last reply Reply Quote 1
          • jens55undefined
            jens55 @DanS79
            last edited by

            @dans79, agreed, that is the way I am used to program a loop and yes, nested loops will be a nightmare.

            alankilianundefined 1 Reply Last reply Reply Quote 0
            • alankilianundefined
              alankilian @jens55
              last edited by alankilian

              @jens55 said in Meta commands:

              nested loops will be a nightmare.

              Why?

              Each loop gets its own iterator (which is what iterators ARE) so everything works out.

              m118 s"Starting the loop"
              while iterations < 3
                    m118 s"hello world first"
                    while iterations < 3
                      m118 s"hello world second"
                      while iterations < 3
                        m118 s"hello world third"
              m118 s"Done with the loop"
              
              8/12/2021, 2:57:50 PM 	M98 P"0:/macros/000_test"
              Starting the loop
              hello world first
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              hello world first
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              hello world first
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              hello world second
              hello world third
              hello world third
              hello world third
              Done with the loop
              
              DanS79undefined 1 Reply Last reply Reply Quote 0
              • DanS79undefined
                DanS79 @alankilian
                last edited by

                @alankilian said in Meta commands:

                @jens55 said in Meta commands:

                nested loops will be a nightmare.

                Why?

                Each loop gets its own iterator (which is what iterators ARE) so everything works out.

                They are all called iterations.

                If use want to use multiple of them to set a variable, the code would be very hard to read (assuming scoping even allows you to access all of them).

                The following would be real confusing to follow if you replaced a, b, and c with iterations .

                var x = 0
                var a = 0
                var b = 0
                var c = 0
                while a < 10
                    set var.b = 0
                    while b < 20
                        set var.c = 0
                        while c < 40
                            set var.x = (a * b) - c
                            set var.c = c + 1
                         set var.b = b + 1
                    set var.a = a + 1
                
                alankilianundefined 1 Reply Last reply Reply Quote 0
                • alankilianundefined
                  alankilian @DanS79
                  last edited by

                  @dans79 Yeah, there are a thousand wrong ways to use a programming feature for every right way to use them.

                  🙂

                  1 Reply Last reply Reply Quote 0
                  • jens55undefined
                    jens55
                    last edited by

                    I think we are drifting away from the part that I am having problems with which is why the particular construct I was using was having issues. Yes, there is a way to change things but WHY is it needed.

                    Here is a different way of going:

                    This works:

                    while iterations < 10
                      m118 s{"hello world " ^ iterations}
                      g4 s2
                      continue
                    

                    This gives:
                    Error: in file macro line 6 column 11: meta command: 'continue' was not inside a loop

                    while iterations < 10
                      m118 s{"hello world " ^ iterations}
                    ;  m118 s{"hello world " ^ iterations}
                      g4 s2
                      continue
                    

                    The commented out line seems to dump the 'continue' statement outside of the loop and seems to me to be a bug in interpretation.

                    alankilianundefined 2 Replies Last reply Reply Quote 0
                    • alankilianundefined
                      alankilian @jens55
                      last edited by

                      @jens55 You don't need to add a 'continue' statement at the end of a loop.

                      jens55undefined 1 Reply Last reply Reply Quote 0
                      • alankilianundefined
                        alankilian @jens55
                        last edited by

                        @jens55 said in Meta commands:

                        The commented out line seems to dump the 'continue' statement outside of the loop and seems to me to be a bug in interpretation.

                        You need to be very particular with the left-side whitespace to keep things working.

                        The interpreter is going to look at how many tab (or space)?) characters you have at the beginning of every line to know where the end of the loops are.

                        Your commented-out line starts with a semicolon in column one which tells the interperter that's the end of your while loop.

                        So, the 'continue' statement is not outside of the loop.

                        1 Reply Last reply Reply Quote 0
                        • jens55undefined
                          jens55 @alankilian
                          last edited by

                          @alankilian, I realize that .... but what I am getting at is that something is fishy about how this is interpreted. While I don't need the 'continue', there should be nothing barring me from using it. It isn't inherently wrong.

                          alankilianundefined 1 Reply Last reply Reply Quote 0
                          • alankilianundefined
                            alankilian @jens55
                            last edited by alankilian

                            @jens55 That semicolon in column-1 causes the interpeter to end the while loop.

                            This works:

                            while iterations < 10
                              m118 s{"hello world " ^ iterations}
                              ;  m118 s{"hello world " ^ iterations}
                              g4 s2
                              continue
                            
                            jens55undefined 1 Reply Last reply Reply Quote 0
                            • jens55undefined
                              jens55 @alankilian
                              last edited by

                              @alankilian, I will test that but the semicolon is there to comment out the line and should not cause this reaction.

                              alankilianundefined 2 Replies Last reply Reply Quote 0
                              • alankilianundefined
                                alankilian @jens55
                                last edited by

                                @jens55 This works to keep the whitespace:

                                while iterations < 10
                                  m118 s{"hello world " ^ iterations}
                                  ;  m118 s{"hello world " ^ iterations}
                                  g4 s2
                                  continue
                                
                                jens55undefined 1 Reply Last reply Reply Quote 0
                                • jens55undefined
                                  jens55 @alankilian
                                  last edited by

                                  @alankilian
                                  @dc42
                                  Ahhhh, I need the spaces before the comment .... ok but that s a bug in the interpreter

                                  alankilianundefined 1 Reply Last reply Reply Quote 0
                                  • alankilianundefined
                                    alankilian @jens55
                                    last edited by

                                    This post is deleted!
                                    1 Reply Last reply Reply Quote 0
                                    • alankilianundefined
                                      alankilian @jens55
                                      last edited by

                                      @jens55 said in Meta commands:

                                      Ahhhh, I need the spaces before the comment .... ok but that s a bug in the interpreter

                                      From the documentation:

                                      The body ends just before the first line that is not indented.

                                      So, it could be argued that the interpreter is working fine.

                                      It's not up to me to decide.

                                      I'm happy you're getting going on your project again.

                                      And I learned some things from this discussion, so thank you for that also.

                                      1 Reply Last reply Reply Quote 0
                                      • jens55undefined
                                        jens55
                                        last edited by jens55

                                        @dc42
                                        Here is another example of strange behaviour:

                                        while true
                                          if iterations < 10
                                            m118 s"hello world"
                                            g4 s2
                                            continue
                                        break
                                        

                                        This executes but leaves the printer in a 'busy' state
                                        Without the 'break' command it does the same
                                        If I move the 'break' command to the right by two spaces, everything finishes ok - it breaks out of the loop
                                        Point is, there is no such thing as an 'endif' to clearly identify where the 'if' statement ends. IMHO there should be (optional maybe) an 'endif' for readability but in any case, the 'break' should break out of the loop no matter what.

                                        Edit: On re-reading this, there is a flaw in my thinking here, and unless the 'break' is indented two spaces over, the 'while' loop has no exit. Interesting note here is that the iteration variable apparently isn't reset until after exiting the 'while' loop because it only prints 'hello world' for one 'if' loop.
                                        That to me seems rather odd ....

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

                                          @jens55
                                          Nothing strange about it.
                                          Using "while true" creates an infinite loop
                                          This is a dangerous practice for the uninitiated as you must ensure that at some point you have a conditional that triggers a break command.
                                          You have not done so. Therefore the loop continues.
                                          Once it gets to iteration 10 it just stops sending the echo.

                                          You should have put

                                          'if iterations < 10
                                               Echo "blah blah"
                                          else
                                                break
                                          

                                          Your continue command is useless as it's not part of the while construct.
                                          The point of continue is to skip an iteration.

                                          'if iterations < 10
                                               if iterations = 5
                                                      continue 
                                               Echo "blah blah" ; won't happen on #5
                                          else
                                                break  ;  required to end loop
                                          

                                          It's safer to use
                                          while iterations < 10
                                          blah blah

                                          EDIT
                                          Sorry. Doing this on my phone so I could not see your code
                                          Your BREK is outside the whole loop, but the comments stand

                                          jens55undefined 1 Reply Last reply Reply Quote 1
                                          • jens55undefined
                                            jens55
                                            last edited by

                                            Here is another thing that strikes me as odd:

                                            while true
                                              if iterations < 10
                                                m118 s"hello world"
                                                g4 s2
                                                continue
                                              if iterations < 11
                                                m118 s"hello north america"
                                            	g4 s2
                                            	continue
                                              break
                                            

                                            This code will execute the first 'if' for 10 times but will not reset 'iterations' as I would have expected and because of that will only execute the second 'if' loop once.

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