Skip to content

Instantly share code, notes, and snippets.

@alienboyxp
Last active April 15, 2024 21:27
Show Gist options
  • Save alienboyxp/194649a9314f8adfceb51ae8b2e80b7b to your computer and use it in GitHub Desktop.
Save alienboyxp/194649a9314f8adfceb51ae8b2e80b7b to your computer and use it in GitHub Desktop.
3Dwork.io - Klipper - Macro test_speed
# Current Version v_20240401
[gcode_macro TEST_KINEMATICS]
description: Test Printer Kinematics
gcode:
# home not homed axies
{% for axis in ['x', 'y', 'z'] %}
{% if axis not in printer.toolhead.homed_axes %}
G28 { axis }
{% endif %}
{% endfor %}
# move bed down 3 cm at max speed
G0 Z30 F1000
# move head to center of bed at max speed
G0 X{ printer.toolhead.axis_maximum.x / 2 } Y{ printer.toolhead.axis_maximum.y / 2 } F{ printer.toolhead.max_velocity * 60 }
# Vibrate max speed +/- 30% of min-min to max-max
{% for i in range(30) %}
G0 X{ ( printer.toolhead.axis_maximum.x / 2 ) * ( 1 + i / 100.0 ) } Y{ ( printer.toolhead.axis_maximum.y / 2 ) * ( 1 + i / 100.0 ) } F{ printer.toolhead.max_velocity * 60 }
G0 X{ ( printer.toolhead.axis_maximum.x / 2 ) * ( 1 - i / 100.0 ) } Y{ ( printer.toolhead.axis_maximum.y / 2 ) * ( 1 - i / 100.0 ) } F{ printer.toolhead.max_velocity * 60 }
{% endfor %}
# move head to center of bed at max speed
G0 X{ printer.toolhead.axis_maximum.x / 2 } Y{ printer.toolhead.axis_maximum.y / 2 } F{ printer.toolhead.max_velocity * 60 }
# Vibrate max speed +/- 30% of max-min to min-max
{% for i in range(30) %}
G0 X{ ( printer.toolhead.axis_maximum.x / 2 ) * ( 1 - i / 100.0 ) } Y{ ( printer.toolhead.axis_maximum.y / 2 ) * ( 1 + i / 100.0 ) } F{ printer.toolhead.max_velocity * 60 }
G0 X{ ( printer.toolhead.axis_maximum.x / 2 ) * ( 1 + i / 100.0 ) } Y{ ( printer.toolhead.axis_maximum.y / 2 ) * ( 1 - i / 100.0 ) } F{ printer.toolhead.max_velocity * 60 }
{% endfor %}
# move head to center of bed at max speed
G0 X{ printer.toolhead.axis_maximum.x / 2 } Y{ printer.toolhead.axis_maximum.y / 2 } F{ printer.toolhead.max_velocity * 60 }
# Current Version v_2024040201
### TEST SPEED MACRO LIST
#
# Home, get position, throw around toolhead, home again.
# If MCU stepper positions (first line in GET_POSITION) are greater than a full step different (your number of microsteps), then skipping occured.
# We only measure to a full step to accomodate for endstop variance.
# TEST_SPEED
# Example: TEST_SPEED SPEED=300 ACCEL=5000 CRUISE=0.5 ITERATIONS=10 BOUND=20 SMALLPATTERNSIZE=20
# TEST_SPEED_DELTA
# Example: TEST_SPEED_DELTA SPEED=300 ACCEL=5000 CRUISE=0.5 ITERATIONS=10 BOUND=20 SMALLPATTERNSIZE=20
###
[gcode_macro TEST_SPEED]
description: Test Speed macro using MINIMUM_CRUISE_RATIO
gcode:
# Speed
{% set speed = params.SPEED|default(printer.configfile.settings.printer.max_velocity)|int %}
{action_respond_info("Speed Value : %f" % (speed))}
# Iterations
{% set iterations = params.ITERATIONS|default(5)|int %}
{action_respond_info("Iterations Value : %f" % (iterations))}
# Acceleration
{% set accel = params.ACCEL|default(printer.configfile.settings.printer.max_accel)|int %}
{action_respond_info("Acceleration Value : %f" % (accel))}
# Minimum Cruise Ratio
{% set cruise = params.CRUISE|default(0.5)|float %}
{action_respond_info("Minimum Cruise Ratio : %f" % (cruise))}
# Bounding inset for large pattern (helps prevent slamming the toolhead into the sides after small skips, and helps to account for machines with imperfectly set dimensions)
{% set bound = params.BOUND|default(20)|int %}
{action_respond_info("Bound Value : %f" % (bound))}
# Size for small pattern box
{% set smallpatternsize = params.SMALLPATTERNSIZE|default(20)|int %}
{action_respond_info("Small Pattern Size Value : %f" % (smallpatternsize))}
# Large pattern
# Max positions, inset by BOUND
{% set x_min = printer.toolhead.axis_minimum.x + bound %}
{action_respond_info("x_min : %f" % (x_min))}
{% set x_max = printer.toolhead.axis_maximum.x - bound %}
{action_respond_info("x_max : %f" % (x_max))}
{% set y_min = printer.toolhead.axis_minimum.y + bound %}
{action_respond_info("y_min : %f" % (y_min))}
{% set y_max = printer.toolhead.axis_maximum.y - bound %}
{action_respond_info("y_max : %f" % (y_max))}
# Small pattern at center
# Find X/Y center point
{% set x_center = (printer.toolhead.axis_minimum.x|float + printer.toolhead.axis_maximum.x|float ) / 2 %}
{action_respond_info("x_center : %f" % (x_center))}
{% set y_center = (printer.toolhead.axis_minimum.y|float + printer.toolhead.axis_maximum.y|float ) / 2 %}
{action_respond_info("y_center : %f" % (y_center))}
# Set small pattern box around center point
{% set x_center_min = x_center - (smallpatternsize/2) %}
{action_respond_info("x_center_min : %f" % (x_center_min))}
{% set x_center_max = x_center + (smallpatternsize/2) %}
{action_respond_info("x_center_max : %f" % (x_center_max))}
{% set y_center_min = y_center - (smallpatternsize/2) %}
{action_respond_info("y_center_min : %f" % (y_center_min))}
{% set y_center_max = y_center + (smallpatternsize/2) %}
{action_respond_info("y_center_max : %f" % (y_center_max))}
# Save current gcode state (absolute/relative, etc)
SAVE_GCODE_STATE NAME=TEST_SPEED
# Output parameters to g-code terminal
{action_respond_info("TEST_SPEED: starting %d iterations at speed %d, accel %d, cruise %d" % (iterations, speed, accel, cruise))}
# Home and get position for comparison later:
G28
# QGL if not already QGLd (only if QGL section exists in config)
{% if printer.configfile.settings.quad_gantry_level %}
{% if printer.quad_gantry_level.applied == False %}
QUAD_GANTRY_LEVEL
G28 Z
{% endif %}
{% endif %}
# Move 50mm away from max position and home again (to help with hall effect endstop accuracy - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/24)
G90
G1 X{printer.toolhead.axis_maximum.x-50} Y{printer.toolhead.axis_maximum.y-50} F{30*60}
G28 X Y
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Go to starting position
G0 X{x_min} Y{y_min} Z{bound + 10} F{speed*60}
# Set new limits
SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} MINIMUM_CRUISE_RATIO={cruise}
{% for i in range(iterations) %}
# Large pattern
# Diagonals
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Box
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Small pattern
# Small diagonals
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
# Small box
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
{% endfor %}
# Restore max speed/accel/accel_to_decel to their configured values
SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} MINIMUM_CRUISE_RATIO={printer.configfile.settings.printer.minimum_cruise_ratio}
# Re-home and get position again for comparison:
G28
# Go to XY home positions (in case your homing override leaves it elsewhere)
G90
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Restore previous gcode state (absolute/relative, etc)
RESTORE_GCODE_STATE NAME=TEST_SPEED
# This variant is for delta printers.
# Home, get position, throw around toolhead, home again.
# If MCU stepper positions (first line in GET_POSITION) are greater than a full step different (your number of microsteps), then skipping occured.
# We only measure to a full step to accomodate for endstop variance.
# Example: TEST_SPEED_DELTA SPEED=300 ACCEL=5000 CRUISE=0.5 ITERATIONS=10
[gcode_macro TEST_SPEED_DELTA]
description: Test Speed Delta macro using MINIMUM_CRUISE_RATIO
gcode:
# Speed
{% set speed = params.SPEED|default(printer.configfile.settings.printer.max_velocity)|int %}
# Iterations
{% set iterations = params.ITERATIONS|default(5)|int %}
# Acceleration
{% set accel = params.ACCEL|default(printer.configfile.settings.printer.max_accel)|int %}
# Minimum Cruise Ratio
{% set accel = params.CRUISE|default(0.5)|int %}
{action_respond_info("Minimum Cruise Ratio : %f" % (cruise))}
# Bounding inset for large pattern (helps prevent slamming the toolhead into the sides after small skips, and helps to account for machines with imperfectly set dimensions)
{% set bound = params.BOUND|default(20)|int %}
# Size for small pattern box
{% set smallpatternsize = params.SMALLPATTERNSIZE|default(20)|int %}
# Large pattern
# Max positions, inset by BOUND
{% set radius_bound = printer.toolhead.axis_maximum.x - bound %}
{% set xy_max_pos = ((radius_bound ** 2) / 2) ** 0.5 %}
{% set x_min = xy_max_pos * -1 %}
{% set x_max = xy_max_pos %}
{% set y_min = xy_max_pos * -1 %}
{% set y_max = xy_max_pos %}
# Small pattern at center
# Set small pattern box around center point
{% set x_center_min = smallpatternsize / 2 * -1 %}
{% set x_center_max = smallpatternsize / 2 %}
{% set y_center_min = smallpatternsize / 2 * -1 %}
{% set y_center_max = smallpatternsize / 2 %}
# Save current gcode state (absolute/relative, etc)
SAVE_GCODE_STATE NAME=TEST_SPEED_DELTA
# Output parameters to g-code terminal
{action_respond_info("TEST_SPEED: starting %d iterations at speed %d, accel %d, cruise %d" % (iterations, speed, accel, cruise))}
# Home and get position for comparison later:
G28
G90
G4 P1000
GET_POSITION
# Go to starting position
G0 X{x_min} Y{y_min} Z{printer.toolhead.axis_maximum.z/2} F{speed*60}
# Set new limits
SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} MINIMUM_CRUISE_RATIO={cruise}
{% for i in range(iterations) %}
# Large pattern
# Diagonals
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Box
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Small pattern
# Small diagonals
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
# Small box
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
{% endfor %}
# Restore max speed/accel/accel_to_decel to their configured values
SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} MINIMUM_CRUISE_RATIO={printer.configfile.settings.printer.minimum_cruise_ratio}
# Re-home and get position again for comparison:
G28
G4 P1000
GET_POSITION
# Restore previous gcode state (absolute/relative, etc)
RESTORE_GCODE_STATE NAME=TEST_SPEED_DELTA
[gcode_macro TEST_SPEED_OLD]
gcode:
# Speed
{% set speed = params.SPEED|default(printer.configfile.settings.printer.max_velocity)|int %}
{action_respond_info("Speed Value : %f" % (speed))}
# Iterations
{% set iterations = params.ITERATIONS|default(5)|int %}
{action_respond_info("Iterations Value : %f" % (iterations))}
# Acceleration
{% set accel = params.ACCEL|default(printer.configfile.settings.printer.max_accel)|int %}
{action_respond_info("Acceleration Value : %f" % (accel))}
# Bounding inset for large pattern (helps prevent slamming the toolhead into the sides after small skips, and helps to account for machines with imperfectly set dimensions)
{% set bound = params.BOUND|default(20)|int %}
{action_respond_info("Bound Value : %f" % (bound))}
# Size for small pattern box
{% set smallpatternsize = params.SMALLPATTERNSIZE|default(20)|int %}
{action_respond_info("Small Pattern Size Value : %f" % (smallpatternsize))}
# Large pattern
# Max positions, inset by BOUND
{% set x_min = printer.toolhead.axis_minimum.x + bound %}
{action_respond_info("x_min : %f" % (x_min))}
{% set x_max = printer.toolhead.axis_maximum.x - bound %}
{action_respond_info("x_max : %f" % (x_max))}
{% set y_min = printer.toolhead.axis_minimum.y + bound %}
{action_respond_info("y_min : %f" % (y_min))}
{% set y_max = printer.toolhead.axis_maximum.y - bound %}
{action_respond_info("y_max : %f" % (y_max))}
# Small pattern at center
# Find X/Y center point
{% set x_center = (printer.toolhead.axis_minimum.x|float + printer.toolhead.axis_maximum.x|float ) / 2 %}
{action_respond_info("x_center : %f" % (x_center))}
{% set y_center = (printer.toolhead.axis_minimum.y|float + printer.toolhead.axis_maximum.y|float ) / 2 %}
{action_respond_info("y_center : %f" % (y_center))}
# Set small pattern box around center point
{% set x_center_min = x_center - (smallpatternsize/2) %}
{action_respond_info("x_center_min : %f" % (x_center_min))}
{% set x_center_max = x_center + (smallpatternsize/2) %}
{action_respond_info("x_center_max : %f" % (x_center_max))}
{% set y_center_min = y_center - (smallpatternsize/2) %}
{action_respond_info("y_center_min : %f" % (y_center_min))}
{% set y_center_max = y_center + (smallpatternsize/2) %}
{action_respond_info("y_center_max : %f" % (y_center_max))}
# Save current gcode state (absolute/relative, etc)
SAVE_GCODE_STATE NAME=TEST_SPEED
# Output parameters to g-code terminal
{action_respond_info("TEST_SPEED: starting %d iterations at speed %d, accel %d" % (iterations, speed, accel))}
# Home and get position for comparison later:
G28
# QGL if not already QGLd (only if QGL section exists in config)
{% if printer.configfile.settings.quad_gantry_level %}
{% if printer.quad_gantry_level.applied == False %}
QUAD_GANTRY_LEVEL
G28 Z
{% endif %}
{% endif %}
# Move 50mm away from max position and home again (to help with hall effect endstop accuracy - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/24)
G90
G1 X{printer.toolhead.axis_maximum.x-50} Y{printer.toolhead.axis_maximum.y-50} F{30*60}
G28 X Y
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Go to starting position
G0 X{x_min} Y{y_min} Z{bound + 10} F{speed*60}
# Set new limits
SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} ACCEL_TO_DECEL={accel / 2}
{% for i in range(iterations) %}
# Large pattern
# Diagonals
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Box
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Small pattern
# Small diagonals
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
# Small box
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
{% endfor %}
# Restore max speed/accel/accel_to_decel to their configured values
SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel}
# Re-home and get position again for comparison:
G28
# Go to XY home positions (in case your homing override leaves it elsewhere)
G90
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Restore previous gcode state (absolute/relative, etc)
RESTORE_GCODE_STATE NAME=TEST_SPEED
# This variant is for delta printers.
# Home, get position, throw around toolhead, home again.
# If MCU stepper positions (first line in GET_POSITION) are greater than a full step different (your number of microsteps), then skipping occured.
# We only measure to a full step to accomodate for endstop variance.
# Example: TEST_SPEED_DELTA SPEED=300 ACCEL=5000 ITERATIONS=10
[gcode_macro TEST_SPEED_DELTA_OLD]
gcode:
# Speed
{% set speed = params.SPEED|default(printer.configfile.settings.printer.max_velocity)|int %}
# Iterations
{% set iterations = params.ITERATIONS|default(5)|int %}
# Acceleration
{% set accel = params.ACCEL|default(printer.configfile.settings.printer.max_accel)|int %}
# Bounding inset for large pattern (helps prevent slamming the toolhead into the sides after small skips, and helps to account for machines with imperfectly set dimensions)
{% set bound = params.BOUND|default(20)|int %}
# Size for small pattern box
{% set smallpatternsize = params.SMALLPATTERNSIZE|default(20)|int %}
# Large pattern
# Max positions, inset by BOUND
{% set radius_bound = printer.toolhead.axis_maximum.x - bound %}
{% set xy_max_pos = ((radius_bound ** 2) / 2) ** 0.5 %}
{% set x_min = xy_max_pos * -1 %}
{% set x_max = xy_max_pos %}
{% set y_min = xy_max_pos * -1 %}
{% set y_max = xy_max_pos %}
# Small pattern at center
# Set small pattern box around center point
{% set x_center_min = smallpatternsize / 2 * -1 %}
{% set x_center_max = smallpatternsize / 2 %}
{% set y_center_min = smallpatternsize / 2 * -1 %}
{% set y_center_max = smallpatternsize / 2 %}
# Save current gcode state (absolute/relative, etc)
SAVE_GCODE_STATE NAME=TEST_SPEED_DELTA
# Output parameters to g-code terminal
{action_respond_info("TEST_SPEED_DELTA: starting %d iterations at speed %d, accel %d" % (iterations, speed, accel))}
# Home and get position for comparison later:
G28
G90
G4 P1000
GET_POSITION
# Go to starting position
G0 X{x_min} Y{y_min} Z{printer.toolhead.axis_maximum.z/2} F{speed*60}
# Set new limits
SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} ACCEL_TO_DECEL={accel / 2}
{% for i in range(iterations) %}
# Large pattern
# Diagonals
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Box
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Small pattern
# Small diagonals
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
# Small box
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
{% endfor %}
# Restore max speed/accel/accel_to_decel to their configured values
SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel}
# Re-home and get position again for comparison:
G28
G4 P1000
GET_POSITION
# Restore previous gcode state (absolute/relative, etc)
RESTORE_GCODE_STATE NAME=TEST_SPEED_DELTA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment