Conditional gcode help required - New Problem
-
@OwenD, @bearer .
Brilliant! Thanks guys, I'll give it go.Refreshing to know that I wasn't too far away.
I'll likely iterate through all 50 possibilities (the maximum number that can be defined) because it is conceivable that I might have a "n" tools but that they might not be consecutively numbered. 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. So it's conceivable that one might have say 3 tools in config.g but they would be numbered 3 11 and 32 or some such.
I feel more macros and conditional gcode coming on...............
-
@deckingman said in Conditional gcode help required:
I'll likely iterate through all 50 possibilities
then just swap the
while iterations < #tools
forwhile iterations < 50
one thing that had me puzzled is, same temps for all tools?
I feel more macros and conditional gcode coming on...............
hihi .. adicting
-
@bearer said in Conditional gcode help required:
one thing that had me puzzled is, same temps for all tools?
Yes - mixing hot end = multiple inputs but a single (shared) heater for all tools. Which is also why active and standby temperatures are also the same.
Edit. My MK1 design did use individual heaters for each filament input, plus a shared heater for the mixing chamber and nozzle, but that was a bit too ambitious (and complex). I might look at resurrecting it at some time in the future but right now, I have my hands pretty full just trying to make the more simplified version work.
-
OK, macro uploaded and tested - mostly OK but a little bit "flaky".
I had 11 tools defined - 0 to 10
With iterations set to <50, when I run the macro the active and standby temperatures all get set as expected. But I get an error in the console "Error in file macro line 4 column 6 : meta command : array index out of bounds. Sometimes that error say line 4 and other times it says line 3 but always column 6.
Long story short but I get that error if the number of iterations is set to a value which is greater than the highest tool number. That is to say, if I change the first line to be "while iterations < 11" it will set the temperature for tools 1 to 10 and not give an error. If I change the tool definition for tool 10 and call it tool 19 the I can use "while iterations < 20" and it will work without error. But if I use iterations <21 with tool 19 as one of the defined tools, I get the error.
Which seems to me like the array isn't a fixed size of 50 (0 to 49) but has a variable size which is determined by the highest tool number (if that makes any sense). With tools 0 to 9 plus tool 19 defined, I didn't get any errors for tools 11 to 18 (which don't exist) which is fine, but if the macro tries to set a value for tool 20 then I get the error.
Not sure if that makes any sense but basically, it works and I don't suppose the error does any harm. But in an ideal world, it'd be good if I didn't get the error. Is there such a thing as the highest tool number in the object model which could be used to set the iterations (or some other way of using the array size).
-
@deckingman do you still have
if tools[iterations] != null
? Because that is what should check if the array index has a value. Perhaps that is itself what is causing the error, I've never used the object model/conditional gcode. -
@bot said in Conditional gcode help required:
@deckingman do you still have
if tools[iterations] != null
? Because that is what should check if the array index has a value. Perhaps that is itself what is causing the error, I've never used the object model/conditional gcode.Yes - and that bit works.
Like I said, if I define tools 0 to 9 plus tool 19, then there are no errors for the non-existent tools 10 to 18 providing I set the "while iterations < 20". Only when I set that "while iterations" value to something higher than the highest defined tool number do I get the error.
If I redefine tool 19 back to it's original value of "10", then I have to use "while iterations <11" otherwise I get the array out of bounds error.
-
@deckingman said in Conditional gcode help required:
array index out of bounds
probably my bad; I added the echo statement above the if statement so it will generate errors for non existent tools, moving it down help.
anyways, could you put up a sample config/macro for the tools to play with?
-
@bearer The echo seems to work fine - it shows which tools have had their temperatures set and no others.
The macro is exactly as you wrote except I use a number instead of your "#tools". Here is a cut and paste of the tools section in my config.g - sorry, it's a bit long. The M568 are historic and no longer necessary, hence commented out.
M563 P0 S"Black" D0:1:2:3:4:5 H1 ; Define tool 0 (drive 3 not used)
G10 P0 X0 Y0 ; Set tool 0 axis offsets
G10 P0 R0 S0 ; Set initial tool 0 active and standby temperatures to 0C
;M568 P0 S1 ; Enable mixing for tool 0
M567 P0 E1.00:0.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 0
M563 P1 S"Red" D0:1:2:3:4:5 H1 ; Define tool 1
G10 P1 X0 Y0 ; Set tool 1 axis offsets
G10 P1 R0 S0 ; Set initial tool 1 active and standby temperatures to 0C
;M568 P1 S1 ; Enable mixing for tool 1
M567 P1 E0.00:1.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 1
M563 P2 S"Yellow" D0:1:2:3:4:5 H1 ; Define tool 2
G10 P2 X0 Y0 ; Set tool 2 axis offsets
G10 P2 R0 S0 ; Set initial tool 2 active and standby temperatures to 0C
;M568 P2 S1 ; Enable mixing for tool 2
M567 P2 E0.00:0.00:1.00:0.00:0.00:0.00 ; Set mixing ratios for tool 2
M563 P3 S"Clear" D0:1:2:3:4:5 H1 ; Define tool 3
G10 P3 X0 Y0 ; Set tool 3 axis offsets
G10 P3 R0 S0 ; Set initial tool 3 active and standby temperatures to 0C
;M568 P3 S1 ; Enable mixing for tool 3
M567 P3 E0.00:0.00:0.00:1.00:0.00:0.00 ; Set mixing ratios for tool 3
M563 P4 S"Blue" D0:1:2:3:4:5 H1 ; Define tool 4
G10 P4 X0 Y0 ; Set tool 4 axis offsets
G10 P4 R0 S0 ; Set initial tool 4 active and standby temperatures to 0C
;M568 P4 S1 ; Enable mixing for tool 4
M567 P4 E0.00:0.00:0.00:0.00:1.00:0.00 ; Set mixing ratios for tool 4
M563 P5 S"White" D0:1:2:3:4:5 H1 ; Define tool 5
G10 P5 X0 Y0 ; Set tool 5 axis offsets
G10 P5 R0 S0 ; Set initial tool 5 active and standby temperatures to 0C
M567 P5 E0.00:0.00:0.00:0.00:0.00:1.00 ; Set mixing ratios for tool 5
M563 P6 S"Orange" D0:1:2:3:4:5 H1 ; Define tool 6
G10 P6 X0 Y0 ; Set tool 6 axis offsets
G10 P6 R0 S0 ; Set initial tool 6 active and standby temperatures to 0C
M567 P6 E0.00:0.50:0.50:0.00:0.00:0.00 ; Set mixing ratios for tool 6
M563 P7 S"Purple" D0:1:2:3:4:5 H1 ; Define tool 7
G10 P7 X0 Y0 ; Set tool 7 axis offsets
G10 P7 R0 S0 ; Set initial tool 7 active and standby temperatures to 0C
M567 P7 E0.00:0.50:0.00:0.00:0.50:0.00 ; Set mixing ratios for tool 7
M563 P8 S"Green" D0:1:2:3:4:5 H1 ; Define tool 8
G10 P8 X0 Y0 ; Set tool 8 axis offsets
G10 P8 R0 S0 ; Set initial tool 8 active and standby temperatures to 0C
M567 P8 E0.00:0.00:0.50:0.00:0.50:0.00 ; Set mixing ratios for tool 8
M563 P9 S"Pink" D0:1:2:3:4:5 H1 ; Define tool 9
G10 P9 X0 Y0 ; Set tool 9 axis offsets
G10 P9 R0 S0 ; Set initial tool 9 active and standby temperatures to 0C
M567 P9 E0.00:0.50:0.00:0.00:0.00:0.50 ; Set mixing ratios for tool 9
M563 P10 S"All6" D0:1:2:3:4:5 H1 ; Define tool 10
G10 P10 X0 Y0 ; Set tool 10 axis offsets
G10 P10 R0 S0 ; Set initial tool 10 active and standby temperatures to 0C
M567 P10 E0.16:0.17:0.17:0.17:0.17:0.16 ; Set mixing ratios for tool 10 -
@deckingman said in Conditional gcode help required:
- sorry, it's a bit long
no worries; point was to save me re-create it all from scratc:)
(and compulsory thats what she said)Anyways, I did up two macro files for testing, and seems #tools evaluates to the highest tool number (i.e. you had 10 tools, and I skipped T3, #tools still evaluates to 10). and correctly the echo command doesn't access the array so it will not create an index out of bounds error regardless of it being below the if statement or not, still its tidier inside.
so if you'te getting index errors it might be a tool thats not properly defined?
Can you run
M409
and post for the setup that generates the error?edit: output is the NOT same with
while iterations < 29
or any other fixed number - conclusion making a tool with a gap, creates null tools to fill the gap, but testing a tool past the last tool results in a index error. use #tools or another test for valid tools.6/17/2020, 8:28:31 PM M98 P"0:/macros/mktools.g" 6/17/2020, 8:28:41 PM M98 P"0:/macros/tools.g" Running toosl.g Setting temp for tool T 0 Setting temp for tool T 1 Setting temp for tool T 2 Setting temp for tool T 4 Setting temp for tool T 5 Setting temp for tool T 6 Setting temp for tool T 7 Setting temp for tool T 8 Setting temp for tool T 9 Setting temp for tool T 19 done
;/macros/tool.g echo "Running toosl.g" while iterations < #tools if tools[iterations] != null echo "Setting temp for tool T", iterations G10 P{iterations} S1 R0 ; - set it's active and standby temperature echo "done"
;/macros/mktools.g M569 P0 S1 R-1 ;drivers are bust.. M569 P1 S1 R-1 ;drivers are bust.. M569 P2 S1 R-1 ;drivers are bust.. M569 P3 S1 R-1 ;drivers are bust.. M569 P4 S1 R-1 ;drivers are bust.. M584 E0:1:2:3:4 ;t0 M563 P0 S"Black" D0:1:2:3:4 H1 G10 P0 X0 Y0 ; Set tool 0 axis offsets G10 P0 R0 S0 ; Set initial tool 0 active and standby temperatures to 0C M567 P0 E0.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 0 ;t1 M563 P1 S"Red" D0:1:2:3:4 H1 ; Define tool 1 G10 P1 X0 Y0 ; Set tool 1 axis offsets G10 P1 R0 S0 ; Set initial tool 1 active and standby temperatures to 0C ;M568 P1 S1 ; Enable mixing for tool 1 M567 P1 E1.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 1 ;t2 M563 P2 S"Yellow" D0:1:2:3:4 H1 ; Define tool 2 G10 P2 X0 Y0 ; Set tool 2 axis offsets G10 P2 R0 S0 ; Set initial tool 2 active and standby temperatures to 0C ;M568 P2 S1 ; Enable mixing for tool 2 M567 P2 E2.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 2 ;t3 - skip t3 to create gap in test data ;M563 P3 S"Clear" D0:1:2:3:4 H1 ; Define tool 3 ;G10 P3 X0 Y0 ; Set tool 3 axis offsets ;G10 P3 R0 S0 ; Set initial tool 3 active and standby temperatures to 0C ;M568 P3 S1 ; Enable mixing for tool 3 M567 P3 E3.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 3 ;t4 M563 P4 S"Blue" D0:1:2:3:4 H1 ; Define tool 4 G10 P4 X0 Y0 ; Set tool 4 axis offsets G10 P4 R0 S0 ; Set initial tool 4 active and standby temperatures to 0C ;M568 P4 S1 ; Enable mixing for tool 4 M567 P4 E4.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 4 ;t5 M563 P5 S"White" D0:1:2:3:4 H1 ; Define tool 5 G10 P5 X0 Y0 ; Set tool 5 axis offsets G10 P5 R0 S0 ; Set initial tool 5 active and standby temperatures to 0C M567 P5 E5.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 5 ;t6 M563 P6 S"Orange" D0:1:2:3:4 H1 ; Define tool 6 G10 P6 X0 Y0 ; Set tool 6 axis offsets G10 P6 R0 S0 ; Set initial tool 6 active and standby temperatures to 0C M567 P6 E6.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 6 ;t7 M563 P7 S"Purple" D0:1:2:3:4 H1 ; Define tool 7 G10 P7 X0 Y0 ; Set tool 7 axis offsets G10 P7 R0 S0 ; Set initial tool 7 active and standby temperatures to 0C M567 P7 E7.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 7 ;t8 M563 P8 S"Green" D0:1:2:3:4 H1 ; Define tool 8 G10 P8 X0 Y0 ; Set tool 8 axis offsets G10 P8 R0 S0 ; Set initial tool 8 active and standby temperatures to 0C M567 P8 E8.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 8 ;t9 M563 P9 S"Pink" D0:1:2:3:4 H1 ; Define tool 9 G10 P9 X0 Y0 ; Set tool 9 axis offsets G10 P9 R0 S0 ; Set initial tool 9 active and standby temperatures to 0C M567 P9 E9.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 9 ;t10 - shifted to 19 M563 P19 S"All6" D0:1:2:3:4 H1 ; Define tool 10 G10 P19 X0 Y0 ; Set tool 10 axis offsets G10 P19 R0 S0 ; Set initial tool 10 active and standby temperatures to 0C M567 P19 E10.00:0.00:0.00:0.00:0.00 ; Set mixing ratios for tool 19
-
@bearer Brilliant!
As you say "#tools" seems to evaluate to the highest tool number which is just what I want.
So now I can define any number of tools (up to the limit of 50), and use any tool numbers including non-sequential ones, and the macro will always set the active and standby temperatures for all of the defined tools - and without throwing up any errors.
Which means I just need a single macro for each filament type (PLA, PET-G, ABS etc) and it will work regardless of whether I'm using a single input hot end, a 3 colour Diamond hot end, a 5 colour Diamond, or my own 6 colour (if I ever get it working).
Many thanks indeed!
-
@deckingman said in Conditional gcode help required:
Many thanks indeed!
No worries, looking forward to seeing what you come up with next
-
@bearer I'm one step ahead of you and already thinking about it.
At the end of every print, I run another macro which sets the active and standby temperatures to 0 (I could use -273.something to turn them off but 0 will do). So it occurs to me that if I had another "if" statement, I could use the same macro for both pre-print and post print.
Something like
if the tool set temperature (G10 S or R parameter) >0, set it to 0 else set it to (say) 210.How would I do that?
-
Can I use something like this
After the echo "setting temp for tool"
if G10 P{iterations} S >0
(indented) G10 P{iterations} S210 R210
(not indented) G10 P{iterations} S0 R0Does that work as an "else"?
-
@deckingman said in Conditional gcode help required:
Does that work as an "else"?
no else needs to be explicit; without it it will revert the changes on every run immediately as the last line will always execute.
you're on the right track, but have to access the object model instead of the G10 g-code.
something like
if tools[#tools].activeTemp > 0 ;set the temp to 0 else ;set the temp to 210
I'm not sure about the correct object model node, but will try to poke it some more later, stepping out for a bit now that the sun is no longer scorching everything in an instant.. (and deliberately testing the last tool as it will be defined, the first might not)
-
@bearer
if tools[#tools].activeTemp > 0should be
if tools[#tools-1].activeTemp > 0
Tools array starts at zero, whereas number of tools starts at 1
-
to poke the object model you can user
M409
it will print out the top level object model in a flat JSON string, use something like https://jsonformatter.org/ to make it more readable.M409
shows the tool object, and you can dig further withM409 K"tools"
which return an array of tool objects,M409 K"tools[19]"
will return the last tool object. (The returned JSON string will have the actual object model inside the result object - for forM409 K"tools[19]"
you should haveresult.active[0]
for the active temp, but the object model will not use theresult
as part of the path, but it will need thetools[19]
as part of the path giving youtools[19].active[0]
. (why active is an single element array idk so this may break in wonderful and unexpected ways)I now see your thinking was to look at each individual tool inside the loop, but my thinking was to look at the last tool and have two loops - subtle difference. Not sure if either is better, depends a little on wheather or not all tools will be in the same state.
taking
if G10 P{iterations} S >0 G10 P{iterations} S210 R210 G10 P{iterations} S0 R0
and inserting into
;/macros/tool.g echo "Running toosl.g" while iterations < #tools if tools[iterations] != null echo "Setting temp for tool T", iterations G10 P{iterations} S1 R0 ; - set it's active and standby temperature echo "done"
you get
;/macros/toggletools.g - toggles stand by temps for all tools. echo "Running toggletools.g" 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} S210 R210 echo "done"
edit looking back there was something amiss with
tools[#tools-1]
so worked out well to go with your approach and a single loop avoidingtools[#tools-1]
-
@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.
-
@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
-
@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.
-
@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