Bug in Macro for loop?

I have written a macro (attached below) to test my printing speed. However, in the for-loop, the variables in the G-code are not being output correctly. For example, there is no extrusion happening. Does anyone know why this might be happening? The fixed values seem to work fine.

# TEST_PRINT_SPEED SPEED=100 ACC=3000 TEMP_NOZZLE=200 TEMP_BED=50 DIA_FILAMENT=1.75 DIA_NOZZLE=0.6 LAYER_HEIGHT=0.3 CALIBRATE=1

# example: TEST_PRINT_SPEED SPEED=100 ACC=3000 TEMP_NOZZLE=200 TEMP_BED=50 DIA_FILAMENT=1.75 DIA_NOZZLE=0.4 LAYER_HEIGHT=0.2 CALIBRATE=1
[gcode_macro TEST_PRINT_SPEED]
gcode:
    # define params
    {% set SPEED = params.SPEED|default(100)|int %}
    {% set ACC = params.ACC|default(3000)|int %}
    {% set TEMP_NOZZLE = params.TEMP_NOZZLE|default(200)|int %}
    {% set TEMP_BED = params.TEMP_BED|default(50)|int %}
    {% set DIA_FILAMENT = params.DIA_FILAMENT|default(1.75)|float %}
    {% set DIA_NOZZLE = params.DIA_NOZZLE|default(0.4)|float %}
    {% set LAYER_HEIGHT = params.LAYER_HEIGHT|default(0.2)|float %}
    {% set CALIBRATE = params.CALIBRATE|default(1)|int %}

    {% set LAYER_WIDTH = DIA_NOZZLE * 1.2|float %}
    {% set DISTANCE = 100|int %}

    # calculate extrusion lengths
    {% set extrusion_area = (LAYER_WIDTH - LAYER_HEIGHT) * LAYER_HEIGHT + 3.14159 * (LAYER_HEIGHT / 2) ** 2 %}
    {% set filament_length = (extrusion_area * DISTANCE * 4 / (3.14159 * DIA_FILAMENT ** 2))|round(5) %}
    {% set filament_prime_length = (extrusion_area * 50 * 4 / (3.14159 * DIA_FILAMENT ** 2))|round(5) %}

    # calculate and display volumetric flowrate
    {% set extrusion_volume  = extrusion_area * DISTANCE %}
    {% set time_for_extrusion = DISTANCE / SPEED  %}
    {% set volumetric_flow_rate = extrusion_volume / time_for_extrusion  %}
    { action_respond_info("Volumetric E: %f mm³/s" % volumetric_flow_rate) }


    # set acceleration
    M204 S{ACC}

    # homing
    M107
    G90
    G28
    G1 Z50

    # preheat
    M140 S{TEMP_BED}
    M104 S130
    M109 S130
    M190 S{TEMP_BED}

    {% if CALIBRATE == 1 %}
        BED_MESH_CALIBRATE
    {% endif %}

    # go to start position
    G1 X0 Y50 F{SPEED*60}
    M109 S{TEMP_NOZZLE}
    G1 Z{LAYER_HEIGHT}

    # primeline
    G1 E3 F500
    G1 X50 E{filament_prime_length} F500

    # print loop
    {% for i in range(5) %}
         # set first layer speed
        {% if i == 0 %}
            {% set layer_speed = 60 %}
        {% else %}
            {% set layer_speed = SPEED %}
        {% endif %}

        { action_respond_info("Layer Speed %f" % layer_speed) }
        { action_respond_info("%f" % DIA_FILAMENT) }
        { action_respond_info("%f" % LAYER_HEIGHT) }
        { action_respond_info("%f" % LAYER_WIDTH) }
        { action_respond_info("%f" % filament_length) }


        # move and extrude
        G1 X{DISTANCE + 50} E{filament_length} F{layer_speed*60}
        G1 Y{DISTANCE + 50} E{filament_length} F{layer_speed*60}
        G1 X50 E{filament_length} F{layer_speed*60}
        G1 Y50 E{filament_length} F{layer_speed*60}
        G1 Z{LAYER_HEIGHT} F10000

        # enable fan after first layer
        {% if i == 0 %}
            M106 S255
        {% endif %}
    {% endfor %}

    # end position
    G1 Z50 X0 Y100 F10000
    M106 S0

print_speed.txt (3.0 KB)

As far as I know the macros are evaluated once before they actually run.
This means changing variables in the meantime won’t be noticed/processed.
There are some posts around here that might overcome that issue but I don’t have them at hand.

Found the problem and it was my fault, extruder moves are not relatives so the extruder wont extrude more…

heres the working macro

# Examples: 
# TEST_PRINT_SPEED
# TEST_PRINT_SPEED SPEED=100 ACC=3000
# TEST_PRINT_SPEED SPEED=100 ACC=3000 TEMP_NOZZLE=200 TEMP_BED=50 DIA_FILAMENT=1.75 DIA_NOZZLE=0.4 LAYER_HEIGHT=0.2 FLOW_RATE=1.1 CALIBRATE=1
[gcode_macro TEST_PRINT_SPEED]
gcode:
    # user inputs, u also can set defaults here 
    {% set SPEED = params.SPEED|default(100)|int %}
    {% set ACC = params.ACC|default(3000)|int %}
    {% set TEMP_NOZZLE = params.TEMP_NOZZLE|default(200)|int %}
    {% set TEMP_BED = params.TEMP_BED|default(50)|int %}
    {% set DIA_FILAMENT = params.DIA_FILAMENT|default(1.75)|float %}
    {% set DIA_NOZZLE = params.DIA_NOZZLE|default(0.6)|float %}
    {% set LAYER_HEIGHT = params.LAYER_HEIGHT|default(0.3)|float %}
    {% set FLOW_RATE = params.FLOW_RATE|default(1)|float %}
    {% set CALIBRATE = params.CALIBRATE|default(0)|int %}

    # system vars, can be changed here
    {% set LAYER_WIDTH = DIA_NOZZLE * 1.2|float %}
    {% set DISTANCE = 100|int %}
    {% set LAYERS = 10|int %}
    {% set FIRST_LAYER_SPEED = 60|int %}

    # calculate extrusion lengths
    {% set extrusion_area = (LAYER_WIDTH - LAYER_HEIGHT) * LAYER_HEIGHT + 3.14159 * (LAYER_HEIGHT / 2) ** 2 %}
    {% set filament_length = (extrusion_area * DISTANCE * 4 / (3.14159 * DIA_FILAMENT ** 2)) * FLOW_RATE|round(5) %}
    {% set filament_prime_length = (extrusion_area * 50 * 4 / (3.14159 * DIA_FILAMENT ** 2)) * FLOW_RATE|round(5) %}

    # set acceleration
    M204 S{ACC}

    # homing
    M107
    G90
    G28
    G1 Z50

    # preheat
    M140 S{TEMP_BED}
    M104 S130
    M109 S130
    M190 S{TEMP_BED}

    {% if CALIBRATE == 1 %}
        BED_MESH_CALIBRATE
    {% endif %}

    # calculate and display settings
    {% set extrusion_volume  = extrusion_area * DISTANCE %}
    {% set time_for_extrusion = DISTANCE / SPEED  %}
    {% set volumetric_flow_rate = extrusion_volume / time_for_extrusion  %}
    { action_respond_info("testing printspeed: %f mm/s" % SPEED) }
    { action_respond_info("volumetric E: %f mm3/s" % volumetric_flow_rate) }
    { action_respond_info("layer width should be: %f mm" % LAYER_WIDTH) }

    # go to start position
    G1 X0 Y50 F{SPEED*60}
    M109 S{TEMP_NOZZLE}
    G1 Z{LAYER_HEIGHT}

    # primeline
    G92 E0
    G1 E5 F500
    G92 E0
    G1 X50 E{filament_prime_length} F500
    G92 E0

    # print loop
    {% for i in range(LAYERS) %}
         # set first layer speed
        {% if i == 0 %}
            {% set layer_speed = FIRST_LAYER_SPEED %}
        {% else %}
            {% set layer_speed = SPEED %}
        {% endif %}

        # move and extrude
        G1 X{DISTANCE + 50} E{filament_length * 1} F{layer_speed*60}
        G1 Y{DISTANCE + 50} E{filament_length * 2} F{layer_speed*60}
        G1 X50 E{filament_length * 3} F{layer_speed*60}
        G1 Y50 E{filament_length * 4} F{layer_speed*60}

        # move up in realtive
        G91
        G1 Z{LAYER_HEIGHT} F10000
        G90

        #reset extrudeer
        G92 E0

        # enable fan after first layer
        {% if i == 0 %}
            M106 S255
        {% endif %}
    {% endfor %}

    # end position
    G1 Z50 X0 Y100 F10000
    M106 S0
``
1 Like

Keep in mind with such things that macros cant be aborted/cancelled.

i use the e stop for that ^^

The sledge hammer. But it works :smiley:

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