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

    Conditional gcode help required - New Problem

    Scheduled Pinned Locked Moved
    Gcode meta commands
    6
    45
    2.2k
    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

      @deckingman said in Conditional gcode help required - New Problem:

      while iterations < #tools
      if tools[iterations] != null
      echo "Setting temp for tool T", iterations
      G10 P{iterations} S0 R0 ; - set it's active and standby temperature

      If you use code escapes (three back-quote characters at the start and end of the block) then it will preserve the indentation:

      while iterations < #tools
        if tools[iterations] != null
        echo "Setting temp for tool T", iterations
        G10 P{iterations} S0 R0 ; - set it's active and standby temperature
      

      Please either post the contents of the non-working macro in this way, or attach it as a file

      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
      • arhiundefined
        arhi @deckingman
        last edited by

        @deckingman said in Conditional gcode help required - New Problem:

        forum removes the indents

        you can do it like this:

        while iterations < #tools
          if tools[iterations] != null
            echo "Setting temp for tool T", iterations
          G10 P{iterations} S0 R0 ; - set it's active and standby temperature
        

        or withot "type of the code" like this

        while iterations < #tools
          if tools[iterations] != null
            echo "Setting temp for tool T", iterations
          G10 P{iterations} S0 R0 ; - set it's active and standby temperature
        

        and if you want to quote it in-line you do it like this

        2c0ab180-fb7f-40e1-bdc3-807fddad2ed5-image.png

        A Former User? 1 Reply Last reply Reply Quote 0
        • arhiundefined
          arhi
          last edited by

          looking at the code it should work as expected. better upload files directly as this "space formatting" can be the source of the problem

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

            OK. Thanks all for the info about preserving indentations.

            By a process of elimination I have isolated what causes the error. It is the preceding Z move. That is is to say, if I comment out the two lines below so I have ..........

            G10 ; retract
            ;G91 ; set relative
            ;G1 Z5 F240 ; move bed down 5mm
            G90 ; back to absolute
            G1 X165 Y300 F9000 ; move to rear

            M98 P"0:/macros/ToolTemps/ToolTemps0.g" ; set the tool temperatures to 0

            ..............Then I get no errors and the macro runs just fine.

            Or if I just re-arrange things so that the macro runs before the Z move, it works fine too. i.e. I now have.................

            ;End print gcode

            G10 ; retract

            M98 P"0:/macros/ToolTemps/ToolTemps0.g" ; set the tool temperatures to 0

            G91 ; set relative
            G1 Z5 F240 ; move bed down 5mm
            G90 ; back to absolute
            G1 X165 Y300 F9000 ; move to rear

            M140 S0 ; turn off bed
            M106 S0 ; turn off part fan

            M291 P"Print finished" R"Post-Print Macro" S1 T10

            ........and this work fine. So it's either the preceding G91 switching to relative, or the G1 Z5 that breaks the conditional macro.

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

            1 Reply Last reply Reply Quote 1
            • A Former User?
              A Former User @arhi
              last edited by A Former User

              @arhi said in Conditional gcode help required - New Problem:

              and if you want to quote it in-line you do it like this

              you can even escape things with \ like `quote it in-line` becomes quote it in-line 🤓 and then you can go all inception to show \`quote it in-line\` and then you can... 😇

              edit: you can use double (as in two) quotes to avoid escaping single quotes (and three to escape two and so on..)
              ``single `quote` `` becomes single `quote`

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

                I spoke too soon. I just ran a print with the end gcode macro as per my last post above and received the errors "Error G10: 'iterations' used when not inside a loop". I ran that same post-print macro again, and it worked without errors on that second try.

                So it's seemingly random. Some times it runs without error, other times I get those errors.

                Here is the macro yet again:

                while iterations < #tools
                  if tools[iterations] != null
                    echo "Setting temp for tool T", iterations
                    G10 P{iterations} S0 R0 ; - set it's active and standby temperature
                

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

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

                  Can you confirm that you are running firmware 3.1.1 on the main board?

                  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 Conditional gcode help required - New Problem:

                    Can you confirm that you are running firmware 3.1.1 on the main board?

                    Yup

                    M122
                    === Diagnostics ===
                    RepRapFirmware for Duet 3 MB6HC version 3.1.1 running on Duet 3 MB6HC v0.6 or 1.0 (standalone mode)
                    
                    M122 B1
                    Diagnostics for board 1:
                    Board EXP3HC firmware 3.1.0 (2020-05-15b1)
                    

                    Don't know if it makes any difference but all the extruders are connected to expansion boards 1 and 2 and the hot end heater and thermistor are on expansion board 1.

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

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

                      Does nobody have any idea why that macro sometimes works and at other times throws up errors - "iterations used when not inside a loop". Interestingly, the other very similar macros which set the tool temperatures to high values and which are used at the start of a print always work. It's just this one which sets the temperature to zero at the end of a print.

                      Because it is erratic and unpredictable, I've had to go back to my old way of doing things and use a number of macros, each with a fixed but different number of G10 Pnn S0 R0 commands, rather than use the while loop. Otherwise, more often than not, the tools will all remain at print temperature when a print finishes which is highly undesirable.

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

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

                        @deckingman
                        If it works sometimes and not others I suspect an unhandled exception. I.e. the firmware isn't getting what it expected.
                        It may help in diagnosing if you add a delay after the echo and add an "else" section to notify when a null value is being returned by your if statement (also with an echo and delay)
                        I'd also do a "finished" section for good measure.

                        I suspect if you don't have sequential numbering on the tools something may be going awry that you aren't seeing.

                        Those changes should help determine which "iteration" is giving the grief

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

                          @OwenD Thanks -

                          I don't know if it helps but actually I get one error for every tool that exists. The tools are always sequentially numbered, so for example if I have 11 tools numbered from 0 to 10, then I get 11 error messages.

                          And as I said, running an almost identical macro at the start of a print to set the tool temperatures to (say) 210, always works. But I'll add the delays and an else section and see what happens.

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

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

                            Ian, does this issue only occur when you call that macro from within end.g, or can you reproduce it in any other way?

                            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 Conditional gcode help required - New Problem:

                              Ian, does this issue only occur when you call that macro from within end.g, or can you reproduce it in any other way?

                              Unfortunately, it isn't as clear cut as that. I've just spent the last couple of hours trying to provoke it to misbehave. It seems the only time it will misbehave is at the end of a print. But running the post print macro on it's own (which in turn calls the tool temps macro), does not make it misbehave.

                              To recap, the macro which provokes errors (at the end of a print) is this:

                                if tools[iterations] != null
                                  echo "Setting temp for tool T", iterations
                                  G10 P{iterations} S0 R0 ; - set it's active and standby temperature
                              

                              It is called from within this macro which is called "PostPrint.g":

                              
                              G10 ; retract
                              G91 ; set relative
                              G1 Z5 F240 ; move bed down 5mm
                              G90 ; back to absolute
                              G1 X165 Y300 F9000 ; move to rear
                              
                              M98 P"0:/macros/ToolTemps/ToolTemps0.g" ; set the tool temperatures to 0
                              
                              M140 S0 ; turn off bed
                              M106 S0 ; turn off part fan
                              
                              M291 P"Print finished" R"Post-Print Macro" S1 T10
                              

                              I can run that "PostPrint" macro any time and it runs without errors. Yet when it is called from the end of a print, I get the "iterations used when not inside a loop" errors (11 of them - 1 for each tool)

                              When I get the errors, I can send M98 P"PostPrint.g" and it will run without errors.

                              This is the end of a gcode file that I've just finished

                              G1 X107.377 Y142.526 E0.05564
                              G10 ; retract
                              M107
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ;end
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ; Filament-specific end gcode 
                              ;END gcode for filament
                              ;end
                              M98 P"0:/macros/PostPrint.g"
                              ; filament used = 17028.6mm (41.0cm3)
                              ; total filament cost = 0.0
                              .........lots more comments after this
                              

                              Could those filament specific comments that the slicer puts in before the M98 macro call screw things up?

                              I've tried running the last gcode commands in order - i.e. G10 followed by M107 followed by the M98 P... but that does not provoke the errors.

                              Weird or what?

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

                              1 Reply Last reply Reply Quote 0
                              • A Former User?
                                A Former User
                                last edited by

                                can you upload a sample print file along with the config? stuck in the countryside for a rainy weekend, never know, might find the time to poke it

                                deckingmanundefined 1 Reply Last reply Reply Quote 0
                                • deckingmanundefined
                                  deckingman @A Former User
                                  last edited by

                                  @bearer Thanks. I've sent you a chat message with shareable link to a folder that I have created on my Google Drive, which contains what I think are all the relevant files.

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

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

                                    OK. So by a process of elimination, I have found the reason (but not the cause) for the errors and a work around. I created a gcode file with simply the start part and the end part of a print file which provokes the problem. That is to say, with all the G1 moves removed. Running that did provoke the problem. Then I progressively removed various commands until I ended with simply this:

                                    M98 P"0:/macros/PrePrintPETG.g"
                                    
                                    ;G21 ; set units to millimeters
                                    ;G90 ; use absolute coordinates
                                    ;M83 ; use relative distances for e
                                    M107
                                    
                                    M98 P"0:/macros/PostPrint.g"
                                    

                                    ..........and that will provoke the problem when run as a print file. Yet running those commands "stand alone" via the console does not provoke the problem - it has to be done as a print from the sd card.

                                    The work around is to put M400 before that second "post print" macro (or indeed at the start of that macro before the call to the second "tool temps" macro).

                                    If that's how it has to be, that's fine by me, but I suggest it be documented somewhere so as to help others in the future.

                                    HTH

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

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

                                      Thanks, that will make it easier for me to track down.

                                      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 Conditional gcode help required - New Problem:

                                        Thanks, that will make it easier for me to track down.

                                        No worries. I have those macros in a folder on my google drive so I can send you a shareable link if you want to replicate things exactly.

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

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

                                          Thanks, I've reproduced the fault using those files, and I'm looking into it.

                                          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
                                          • dc42undefined
                                            dc42 administrators
                                            last edited by

                                            I've worked out what's going on. The G10 command would normally be synchronised to the movement queue. If there are movements not yet complete, it is put in the deferred execution queue. When it is retrieved from that queue, the "iterations" context has been lost.

                                            I think the solution will be to substitute for all { } parameters when copying the GCode command to the deferred execution queue.

                                            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
                                            • First post
                                              Last post
                                            Unless otherwise noted, all forum content is licensed under CC-BY-SA