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 @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
                                      • jens55undefined
                                        jens55 @OwenD
                                        last edited by

                                        @owend said in Meta commands:

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

                                        To skip an iteration ???? Where did you see this in the documentation ???
                                        I look at 'continue' as a command that strictly exists as a means to improve readability of the code since the command itself seems to do diddly squat and can be left out.

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

                                          @jens55 said in Meta commands:

                                          @owend said in Meta commands:

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

                                          To skip an iteration ???? Where did you see this in the documentation ???

                                          'continue' jumps back to the BEGINNING of a loop.

                                          So if you have the following story: (COMPLETELY useless, but trying to illuminate how 'continue' is used)

                                          while iterations < 10
                                            m118 s{"This is iteration " ^ iterations}
                                            if iterations > 5
                                              continue  ; Don't do this stuff after the first 5 iterations
                                            m118 s{"Perform this action on " ^ iterations}
                                          
                                          This is iteration 0
                                          Perform this action on 0
                                          This is iteration 1
                                          Perform this action on 1
                                          This is iteration 2
                                          Perform this action on 2
                                          This is iteration 3
                                          Perform this action on 3
                                          This is iteration 4
                                          Perform this action on 4
                                          This is iteration 5
                                          Perform this action on 5
                                          This is iteration 6
                                          This is iteration 7
                                          This is iteration 8
                                          This is iteration 9
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • alankilianundefined
                                            alankilian @jens55
                                            last edited by

                                            @jens55 said in Meta commands:

                                            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.

                                            Why do you think 'iterations' will get reset to zero?
                                            'iterations' counts the number of times through the loop.

                                            Your 'if' statements will not affect the iteration count at all.

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