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

Conditional gcode help required - New Problem

Scheduled Pinned Locked Moved
Gcode meta commands
6
45
1.9k
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.
  • undefined
    deckingman @A Former User
    last edited by deckingman 18 Jun 2020, 08:26

    @bearer Thanks very much for putting the time into this, and also thanks for the lessons.

    Having slept on it and thought about it some more, I can foresee a few scenarios where having a single "toggle" macro could be problematic. I'm often "tinkering" and even my home Z will pre-heat the nozzle. So it is likely that sooner or later, the active temperature might be other than zero when the macro is called but the intention is to set the tools to print temperature. If the active temperature is non zero, then the tool(s) will be set to zero which would be the opposite of what is intended. For that reason I think it will be safer to use two separate macros, one which will explicitly set the temperatures to print temperature, and the other to set them to zero.

    But thanks again for showing me how to do it. I'm sure that knowledge will be useful in the future.

    And thanks also to @OwenD for your contribution.

    Edit - I've changed the title to show (in an unconventional way) that this is solved.

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

    1 Reply Last reply Reply Quote 0
    • undefined
      arhi @deckingman
      last edited by 18 Jun 2020, 08:35

      @deckingman said in Conditional gcode help required:

      With a multi-input mixing hot end, one defines tools (colours) by using combinations of extruders and then setting mixing ratios for those extruders. But having a large number of tools defined at once, takes up too much space on the web interface. I haven't yet worked out the best way to deal with that but one option would be to have a "palette" of 50 tools but only use a selection of that full palette defined at any one time.

      I think that when version with support for variables come up this should be easier. In theory (we'll see whent he variable support is out) you could have something like a config variable that you set in the config.g header and then you have each tool defined in it's separate .g file and then according to that variable you call the specific number of g-code files... and since it all works in runtime with RRF you could have your slicer startup file do something like:

      $bt={bed_temperature}                ; copy bed temp from slicer to $bt variable
      $ht={hotend0_temperature}            ; copy hotend temp from slicer to $ht variable
      $htsb={hotend0_standbytemperature}   ; set standby temperature for hotend to $htsb
      
      $tool_combination=7                  ; Use specific set of configured tools
      M98 P"start_print.g"                 ; call start_print.g macro
      
      

      and then in start_print.g you have something like

      if $tool_combination == 0
        M98 P"configure_tool_1.g"
        M98 P"configure_tool_4.g"
        M98 P"configure_tool_5.g"
      else if $tool_combination == 1
        M98 P"configure_tool_1.g"
        M98 P"configure_tool_2.g"
        M98 P"configure_tool_3.g"
      else if $tool_combination == 2
        M98 P"configure_tool_4.g"
        M98 P"configure_tool_8.g"
      else if $tool_combination == 3
        M98 P"configure_tool_4.g"
        M98 P"configure_tool_5.g"
        M98 P"configure_tool_6.g"
        M98 P"configure_tool_7.g"
      ...
      else if $tool_combination == 30
        M98 P"configure_tool_14.g"
        M98 P"configure_tool_15.g"
        M98 P"configure_tool_20.g"
      else
        abort "You did not set valid tool_combination value"
      
      M200 D0 ; do not use volumetric extrusion
      M83 ; relative extruder
      
      ; heat up
      
      ; heat up bed first
      M140 S{$bt}
      M190 S{$bt}
      
      ; heat up extruder
      while iterations < #tools
        if tools[iterations] != null
          if tools[iterations].active[0] > 0
            echo "Turning off tool T", iterations
            G10 P{iterations} S0 R0
          else
            echo "Turning on tool T", iterations
            G10 P{iterations} S{$ht} R{$htsb}
      
      ; do not home if already homed
      if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed
        G28
      
      M98 P"wipe.g" ; do the wipe 
      G29S1 ; use MESH compensation
      M913 X100 Y100 Z100 ; all currents to 100%
      M290 R0 S0 ; reset baby steps
      ; reset speed overrides
      M221 S100
      M220 S100 
      
      ; Dynamic Acceleration Adjustment
      ; M593 Fxxx
      
      ; Pressure Advance
      M572 D0 S0.03
      
      
      ; ...
      
      

      in theory, this start_print.g can be something you call before number of other operations as it will reconfigure your tools based on the input variable ...

      in any case, variables should come with 3.2 and if I understood dc42 properly they are now testing new batch of toolboard they got from the supplier so question is when will this come around and how will the variables work... but I believe you understand the basic idea

      undefined 1 Reply Last reply 18 Jun 2020, 08:57 Reply Quote 0
      • undefined
        deckingman @arhi
        last edited by 18 Jun 2020, 08:57

        @arhi Yes, I had been thinking along those lines myself. Essentially taking the tool definitions out of config.g and putting them in one or more macros.

        For info, I already use pre-and post print macros which take care of the everything from heating the bed to homing and purging (as well as setting tool temperatures which will now be done by calling this new macro). That's mostly because it gives me better control over the order that things happen. For example, I pre-heat the bed to around 40 and wait. Once it reaches 40 I set the bed to (say) 55 but don't wait and commence homing and all the other stuff. The net result is that homing, heating tools, and purging all happen within the time it takes for the bed to reach temperature, but not too soon before to minimise the time that the hot end is at print temperature before printing starts.

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

        1 Reply Last reply Reply Quote 0
        • undefined
          OwenD @A Former User
          last edited by 18 Jun 2020, 09:09

          @bearer said in Conditional gcode help required - SOLVED:

          edit looking back there was something amiss with tools[#tools-1] so worked out well to go with your approach and a single loop avoiding tools[#tools-1]

          My bad.
          Should have been tools[(#tools-1)]

          The equation must be in braces

          ? 1 Reply Last reply 18 Jun 2020, 09:23 Reply Quote 0
          • ?
            A Former User @OwenD
            last edited by A Former User 18 Jun 2020, 09:23

            @OwenD said in Conditional gcode help required - SOLVED:

            braces

            dammit, i only tried curly braces. noted 👍

            edit: interesting:

            6/18/2020, 11:43:48 AM	M409 K"tools[19]"
            {"key":"tools[19]","flags":"","result":{"active":[0],"axes":[[0],[1]],"extruders":[0,1,2,3,4],"fans":[0],"filamentExtruder":-1,"heaters":[1],"mix":[10.00,0,0,0,0],"name":"All6","number":19,"offsets":[0,0,0],"offsetsProbed":0,"retraction":{"extraRestart":0,"length":2.0,"speed":16.7,"unretractSpeed":16.7,"zHop":0},"standby":[0],"state":"off"}}
            6/18/2020, 11:43:35 AM	M409 K"tools[(#tools-1)]"
            {"key":"tools[(#tools-1)]","flags":"","result":null}
            6/18/2020, 11:39:30 AM	echo (#tools-1)
            19
            6/18/2020, 11:39:15 AM	echo {(#tools-1)}
            6/18/2020, 11:39:04 AM	echo {#tools-1}
            6/18/2020, 11:38:56 AM	echo {#tools}
            20
            
            1 Reply Last reply Reply Quote 0
            • undefined
              deckingman
              last edited by 27 Jun 2020, 11:25

              I now have a new problem which is frying my brain somewhat. I have this macro to set the tool temps to zero at the end of a print:

              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

              It is correctly indented - but this forum removes the indents when I do a copy and paste.

              This macro works like a charm if I simply run it. But when I put inside my "Post-Print" macro I get a bunch of errors - "Error G10: 'iterations' used when not inside a loop". And of course the temperatures don't get changed. But I can run that macro "stand alone", immediately afterwards and it'll work just fine.

              Here is my Post-Print Macro

              ;End print gcode

              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

              What is really frying my brain is that I do something very similar in a Pre-Print macro which calls another almost identical macro to set the tool temps to 210, and that works as expected.

              For the sake of completeness, here is a "Pre-Print" macro which works as expected

              ; Pre-print gcode 45deg bed, 210 hot end

              M291 P"Heating bed to 40 degC " R"Pre-Print PLA" S1 T10

              M190 S40; start heating bed and wait for it to get to 40

              M291 P"Homing all axes" R"Pre-Print PLA" S1 T10

              M140 S45; Now start to heat bed to 45 but don't wait
              M98 P"0:/macros/PrePrintHome.g" ; home all axes (at high temp)

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

              G1 X80 Y363 F9000 ; move to rear
              M83;

              M291 P"Waiting for heaters to reach active temperatures" R"Pre-Print Macro" S1 T10

              M116 ; wait for all temps including hot end

              M291 P"Wiping nozzle" R"Pre-Print PLA" S1 T5
              M98 P"0:/macros/Nozzle wipe"

              So, I can't fathom why calling a macro to set the tool temps works from within a "pre-print" macro and also "stand alone" but calling it from within my post-print macro brings up errors.

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

              undefined 1 Reply Last reply 27 Jun 2020, 11:53 Reply Quote 0
              • dc42undefined
                dc42 administrators
                last edited by 27 Jun 2020, 11:44

                @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
                • undefined
                  arhi @deckingman
                  last edited by 27 Jun 2020, 11:53

                  @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

                  ? 1 Reply Last reply 27 Jun 2020, 12:25 Reply Quote 0
                  • undefined
                    arhi
                    last edited by 27 Jun 2020, 11:58

                    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
                    • undefined
                      deckingman
                      last edited by 27 Jun 2020, 12:10

                      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 @arhi
                        last edited by A Former User 27 Jun 2020, 12:25

                        @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
                        • undefined
                          deckingman
                          last edited by 27 Jun 2020, 17:38

                          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 27 Jun 2020, 17:40

                            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

                            undefined 1 Reply Last reply 27 Jun 2020, 18:07 Reply Quote 0
                            • undefined
                              deckingman @dc42
                              last edited by 27 Jun 2020, 18:07

                              @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
                              • undefined
                                deckingman
                                last edited by 2 Jul 2020, 20:12

                                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

                                undefined 1 Reply Last reply 2 Jul 2020, 20:24 Reply Quote 0
                                • undefined
                                  OwenD @deckingman
                                  last edited by OwenD 7 Feb 2020, 20:25 2 Jul 2020, 20:24

                                  @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

                                  undefined 1 Reply Last reply 2 Jul 2020, 20:34 Reply Quote 0
                                  • undefined
                                    deckingman @OwenD
                                    last edited by deckingman 7 Feb 2020, 20:34 2 Jul 2020, 20:34

                                    @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 3 Jul 2020, 15:34

                                      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

                                      undefined 1 Reply Last reply 3 Jul 2020, 17:26 Reply Quote 0
                                      • undefined
                                        deckingman @dc42
                                        last edited by deckingman 7 Mar 2020, 17:30 3 Jul 2020, 17:26

                                        @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
                                          last edited by 3 Jul 2020, 17:30

                                          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

                                          undefined 1 Reply Last reply 3 Jul 2020, 17:58 Reply Quote 0
                                          • First post
                                            Last post
                                          Unless otherwise noted, all forum content is licensed under CC-BY-SA