How to do arithmetic inside a condition?
 
Notifications
Clear all

How to do arithmetic inside a condition?  

  RSS
Chris Hill
(@chris-hill)
Estimable Member
How to do arithmetic inside a condition?

 

Hi

I'm new to G-code, and am struggling with the syntax for what should be an easy task.  I'd like to change the flow rate for the first layer only.  I've managed to do this with some custom G-code before a layer change in the printer settings:

M221 S{if layer_num==0}75{else}100{endif} ; reduce flow for first layer only

But since this is a printer setting, not a filament setting, I'd like to replace '100' with whatever is the extrusion multiplier for the current filament.  Unsurprisingly, the variable I need is called extrusion_multiplier, and I can modify the G-code to:

M221 S{if layer_num==0}75{else}[extrusion_multiplier]{endif} ; reduce flow for first layer only 

However, the value I need in the G-code should be a percentage, and the extrusion_multiplier variable is a fraction.  Easy - just multiply it by 100.  But whatever syntax I try - square brackets, curly brackets, round brackets - I can't seem to get a calculated value to appear in the G-code.  At best, the output contains a literal representation of the condition, e.g.

M221 S{if layer_num==0}75{else}(100 * [extrusion_multiplier]){endif} ; reduce flow for first layer only 

gives

M221 S(100 * 0.95) ; reduce flow for first layer only 

Can someone help me with the syntax to turn 'S(100 * 0.95)' to 'S95'?

Thanks

Posted : 21/12/2021 11:18 am
Neophyl
(@neophyl)
Illustrious Member
RE: How to do arithmetic inside a condition?

Its

M221 S{if layer_num==0}75{else}{100 * extrusion_multiplier[0]}{endif} ; reduce flow for first layer only 

Filaments and extruder values are stored in an array, even when only one is in use.  So you have to use extrusion_multiplier[0].  I tried this in 2.4rc with my filament set to an EM of 0.97 and the output gcode was M221 S97 ; reduce flow for first layer only on other layers.

However this does result in a M221 line on every layer change so I'm not sure its optimised for reduced gcode.

Posted : 21/12/2021 1:28 pm
Chris Hill
(@chris-hill)
Estimable Member
Topic starter answered:
RE: How to do arithmetic inside a condition?

That's perfect, thanks Neophyl.

You're right about the code appearing on every layer change.  I optimised it by using:

{if layer_num==0}M221 S75{endif}
{if layer_num==1}M221 S{100 * extrusion_multiplier[0]}{endif} 

So now it sets a bespoke value (75%) on the first layer then resets it to the extrusion multiplier on the second layer.  That value persists for all the other layers.

Posted : 21/12/2021 1:48 pm
Neophyl
(@neophyl)
Illustrious Member
RE: How to do arithmetic inside a condition?

That sounds much better from a gcode perspective.  Anyway glad you have it working now.

Posted : 21/12/2021 2:20 pm
FastEddie
(@fasteddie)
Member
RE: How to do arithmetic inside a condition?

I apologize for reviving this old thread, but this solution resolves a nagging problem I have had with ASA filament, and works beautifully. The issue I see here is that, in klipper, the extrusion factor is a percentage of the extrusion multiplier passed to klipper from the slicer...ie, if my extrusion multiplier in the slicer is 96.5%, the klipper extrusion factor should be 100% of the 96.5%...otherwise, you will be taking a percentage of the EM percentage, not 100% of the value passed. In my view, the code should be:

{if layer_num==0}M221 S75{endif}
{if layer_num==1}M221 S{100}{endif}
Posted : 04/08/2023 4:24 am
Share: