Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcode_macro: Enable use of Python's math module #6843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iammattcoleman
Copy link

Jinja2's native math functionality is fairly limited.

Python's math module includes many functions that are useful for calculations related to 3D printing.

@JamesH1978
Copy link
Collaborator

Thankyou for submitting a PR,

Please be aware you need to sign off, as per point 3 in https://github.com/Klipper3d/klipper/blob/master/docs/CONTRIBUTING.md#what-to-expect-in-a-review

Thanks
James

Jinja2's native math functionality is fairly limited.

Python's math module includes many functions that are useful for
calculations related to 3D printing.

Signed-off-by: Matt Coleman <[email protected]>
@iammattcoleman
Copy link
Author

@JamesH1978 Thanks for pointing that out. I've updated the commit message and rebased the commit on top of master.

@iammattcoleman
Copy link
Author

iammattcoleman commented Mar 5, 2025

I'm using this in my _START_PRINT macro every time I run a print.

I measured warmup time for my printer's extruder and bed at various temperatures and then plotted the data. I then wrote some G-code that uses the trend line equations to get my printer's extruder and build plate to reach temperature at about the same time. The lines that set stage_two_threshold inside the conditional rely on this code change in order to use math.sqrt():

    {% set bed_temp = params.BED_TEMP|default(60)|float %}
    {% set extruder_temp = params.EXTRUDER_TEMP|default(220)|float %}
    {% set estimated_bed_preheat_time = 0.1607 * bed_temp**2 - 13.904 * bed_temp + 329.03 %}
    {% set estimated_extruder_preheat_time = 0.0022 * extruder_temp**2 + 0.0413 * extruder_temp + 18.567 %}
    {% if estimated_bed_preheat_time > estimated_extruder_preheat_time %}
      {% set first_tool = "heater_bed" %}
      {% set first_tool_target = bed_temp %}
      {% set second_tool = "extruder" %}
      {% set second_tool_target = extruder_temp %}
      {% set preheat_time_difference = estimated_bed_preheat_time - estimated_extruder_preheat_time %}
      {% set square = 160700 * preheat_time_difference - 4544817 %}
      {% set stage_two_threshold = (10 * (math.sqrt(square) + 6952) / 1607) | round(0) %}
    {% else %}
      {% set first_tool = "extruder" %}
      {% set first_tool_target = extruder_temp %}
      {% set second_tool = "heater_bed" %}
      {% set second_tool_target = bed_temp %}
      {% set preheat_time_difference = estimated_extruder_preheat_time - estimated_bed_preheat_time %}
      {% set square = 880000 * preheat_time_difference - 16168391 %}
      {% set stage_two_threshold = ((math.sqrt(square) - 413) / 44) | round(0) %}
    {% endif %}
    #RESPOND MSG="Estimated times: bed = {estimated_bed_preheat_time|round(1)}, extruder = {estimated_extruder_preheat_time|round(1)}. Begin heating {second_tool} when {first_tool} reaches {stage_two_threshold}°C."
    # Start heating the first tool.
    SET_HEATER_TEMPERATURE HEATER={first_tool} TARGET={first_tool_target}
    # Reset the Z offset.
    SET_GCODE_OFFSET Z=0.0
    # Home the printer.
    G28
    # Start heating the second tool after the first tool has reached the stage two threshold temperature.
    TEMPERATURE_WAIT SENSOR={first_tool} MINIMUM={stage_two_threshold}
    SET_HEATER_TEMPERATURE HEATER={second_tool} TARGET={second_tool_target}
    # Wait for the tools to reach temperature.
    TEMPERATURE_WAIT SENSOR={first_tool} MINIMUM={first_tool_target}
    TEMPERATURE_WAIT SENSOR={second_tool} MINIMUM={second_tool_target}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants