gcode_macro: Enable use of Python's math module
Jinja2's native math functionality is fairly limited.
Python's math module includes many functions that are useful for calculations related to 3D printing.
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
@JamesH1978 Thanks for pointing that out. I've updated the commit message and rebased the commit on top of master.
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}
I've stopped using macros for my custom behavior and no longer need this change, myself. It works just fine and I could see it being useful for some people. I'll leave it up to the maintainers to decide if this PR should be merged or closed.
Thank you for your contribution to Klipper. Unfortunately, a reviewer has not assigned themselves to this GitHub Pull Request. All Pull Requests are reviewed before merging, and a reviewer will need to volunteer. Further information is available at: https://www.klipper3d.org/CONTRIBUTING.html
There are some steps that you can take now:
- Perform a self-review of your Pull Request by following the steps at: https://www.klipper3d.org/CONTRIBUTING.html#what-to-expect-in-a-review If you have completed a self-review, be sure to state the results of that self-review explicitly in the Pull Request comments. A reviewer is more likely to participate if the bulk of a review has already been completed.
- Consider opening a topic on the Klipper Discourse server to discuss this work. The Discourse server is a good place to discuss development ideas and to engage users interested in testing. Reviewers are more likely to prioritize Pull Requests with an active community of users.
- Consider helping out reviewers by reviewing other Klipper Pull Requests. Taking the time to perform a careful and detailed review of others work is appreciated. Regular contributors are more likely to prioritize the contributions of other regular contributors.
Unfortunately, if a reviewer does not assign themselves to this GitHub Pull Request then it will be automatically closed. If this happens, then it is a good idea to move further discussion to the Klipper Discourse server. Reviewers can reach out on that forum to let you know if they are interested and when they are available.
Best regards, ~ Your friendly GitIssueBot
PS: I'm just an automated script, not a human being.