How to make a G1 command with a variable as the extrusion and feed rate?

As the title says I need a command that allows me to control the extrusion and feed rate at the same time based on a variable. I don’t know the syntax though.

Currently I have

{% set linear_speed = params.LIN|default(200)|float %}
{% set extrude_rate = (20/linear_speed) %} # keeps extrusion rate constant
G1 Y180 E0.1 F{linear_speed}

when I need

G1 Y180 E{extrude_rate} F{linear_speed}

but this results in the F{linear_speed} not being seen.

Another question is the extrude rate even effected by the speed “F” at all? I assumed it does but if not please let me know.

Really I just need to keep the overall extrusion rate constant while having the ability to adjust the linear_speed as a parameter. Any help would be appreciated. Thank you.

I don’t know, but I’m afraid with such things you throw all slicer calculations over board.

Are you following a certain purpose?

I have a spinning shaft I’m printing on that moves back and forth to make a cylindrical mesh stent. Think of a Chinese finger trap.

I’m not using a slicer at all and am doing the programming myself. I adjust the linear speed to see what properties of the stent changes. However, if the extrusion rate is dependent upon this linear speed, “F” value, then I need to be able to adjust the “E” in order to keep the extrusion rate constant.

Thus I have this dilemma of changing the linear speed, which changes the extrusion rate because they are based on one another. I can keep the extrusion rate constant if it changes as a function of the linear speed.

1 Like

It would help to see your entire macro and an example of how you’re calling it. In the code snippet you posted the ‘extrude_rate’ variable is calculated but never used for anything.

Down in the macro with the for loop section is where I need extrude rate to be constant no matter what I change the linear_speed to. However, if I have the command written as:

G1 Y180 E{extrude_rate} F{linear_speed}

instead of:

G1 Y180 E0.1 F{linear_speed}

I get an error and it doesn’t allow me to have a variable with the E value but with just the F value it works fine. I could go in and change it every single time but then I have to restart the machine and reheat everything and it takes a lot of time.

Heres the full macro:

[gcode_macro LIN_X]

variable_extruder_temp: 150 #Celcius
variable_shaft_speed: 63.32 #rpm
variable_numpass: 7         #number of back and forth passes
variable_length: 10         #length of stent in mm

gcode:
     #variable linear speed, need to be able to change this
     {% set linear_speed = params.LIN|default(200)|float %}
     #keeps extrusion rate constant based on the linear speed
     {% set extrude_rate = (20/linear_speed) %}
     #code for calculating how long to turn the spindle on
     {% set move = ((120 * length * numpass / linear_speed + 26) * shaft_speed %} 

# Homes printer
     M117 Homing...
     G28

# Gets extruder ready
     M117 Heating up extruder...
     M109 S{extruder_temp}
     G92 E0

# Sets coordinate systems for xyz and extruder
     G90
     M83

# Moves to initial position
     G1 X153.3 Y160 Z27.3 F3000

# Turns spindle on
     MANUAL_STEPPER STEPPER=shaft ENABLE=1 SET_POSITION=0 MOVE={move} SPEED={shaft_speed} SYNC = 0

# Initial contact with spindle
     G1 162.5 E0.8 F20
     G1 Z29 E0.1 F100

# Turns fan on for cooling
     SET_FAN_SPEED FAN=shaftfan SPEED=0.5

# Moves back and forth 10mm for n number of passes
     {% for i in range(numpass) %}

          # this is where I want to be able to control both the extrude rate 
          # and linear speed in order to keep it constant. I am changing 
          # linear speed everytime, but want extrude_rate to change 
          # as linear speed changes

          G1 Y180 E{extrude_rate} F{linear_speed}
          G1 Y168.5 E{extrude_rate} F{linear_speed}

     {% endfor %}

# Homes itself and turns off extrusion and fan. Stent is finished
     G92 E0
     G1 Y160 F{linear_speed}
     G4 P1000
     G1 X0 Y0 F3000
     G1 Z0.5 F3000
     SET_FAN_SPEED FAN=shaftfan SPEED=0
     M117 Finished!
     

Nevermind, I’m dumb and it works just fine. My monkey brain assumed that the “F” turning red in the printer.cfg was a bad thing. It seems to just be able for the user to differentiate from the “E” to the “F” because they were normally both the same color, until you add a variable to the “E”. This makes no sense unless you actually have this code pulled up in the Klipper code editor. Thanks to anyone who replied.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.