MCU crash (Timer too close) when executing macro

Basic Information:

Printer Model: Sovol SV01
MCU / Printerboard: SKR Mini E3 v2
Host / SBC: Pi Zero 2
klippy.log

Hi, I’m having an issue where the MCU seems to crash when executing a specific macro.

[gcode_macro START_PRINT]
description: Code to execute at print start
variable_step: 0 ; Declare variable 'step' and set to 0
gcode:
  {% set BED_TEMP = params.BED_TEMP|default(70)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|float %}
  M140 S{BED_TEMP} ; Start heating bed but don't wait
  G90 ; Absolute positioning
  G28 ; Home all
  BED_MESH_PROFILE LOAD="default"
  {% for step in range(999) %} ; Loop to check bed temp
    CHECK_BED_TEMP BED_TEMP={BED_TEMP} EXTRUDER_TEMP={EXTRUDER_TEMP}
  {% endfor %}

[gcode_macro CHECK_BED_TEMP]
gcode:
  {% if printer.heater_bed.temperature < params.BED_TEMP|float * 0.75 %} ; Check if bed is above 75% of set value
    G4 P3000 ; Wait specified time
  {% else %}
    M109 S{params.EXTRUDER_TEMP|float} ; Heat extruder & wait
    M190 S{params.BED_TEMP|float} ; Wait for bed to heat if not already
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=step VALUE=999 ; Exit loop
  {% endif %}

This is a macro to start heating the bed before homing, then once the bed temp reaches 75% of set temp the extruder heats. It seems to mostly work. It starts heating the bed and enters the for loop to check & wait. After the bed passes 75%, the extruder heats up so I know it’s getting to the M109 command. Once the extruder reaches the set temp the machine just sits there for a while then the mcu crashes. Is there something wrong with how I’m passing the step variable back to close the for loop?

Log attached

Thanks

klippy.log (1.9 MB)

Just checking a hunch here, Is this happening when you’re starting a print? Or if you run the macro by itself does it still happen?

When your bed and Extruder will reach desired temps, your script will pump
M109 and M190 commands
more than 900 times.

That step variable reset command - will not work as you expected.
Read more about Template-expansion and how Jinja is working here.

In general Macros are working in 2 separate step process Rendering and Execution.

Oh hell, Gaolst is right I didn’t even notice that.

There is a command specifically for those to avoid this whole thing.

https://www.klipper3d.org/G-Codes.html#temperature_wait

Oh man. I totally missed that command haha. I’ve probably spent 5 hours on this macro. I think I just wanted to get the logic working lol. Here’s what I have now, easy peasy:

[gcode_macro START_PRINT]
description: Code to execute at print start
gcode:
  {% set BED_TEMP = params.BED_TEMP|default(70)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|float %}
  M140 S{BED_TEMP} ; Start heating bed but don't wait
  G90 ; Absolute positioning
  G28 ; Home all
  BED_MESH_PROFILE LOAD="default"
  TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={BED_TEMP * 0.75}
  M109 S{EXTRUDER_TEMP} ; Heat extruder & wait
  M190 S{BED_TEMP} ; Wait for bed to heat if not already

Thanks for posting that, it works great.

Out of curiousity, could you guys try to explain what I’m doing wrong with the step variable? I don’t have a very firm grasp on for loops.

Thanks!

You’re essentially just spamming Klipper to death with

IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??
IS IT READY YET??

x 900

Edit: For more detail, and at least as far as I under jinja, there is no delay in your loop. It just spams the 999 messages all at once as quick as it can.

Right. Macros aren’t executed like pure python code. The jinja templates are first evaluated and then translated into the code that gets executed. So when PRINT_START is evaluated, it gets to the for loop and sees that it’s supposed to repeat that command 999 times so it queues up the same command to run 999 times in a row, with no regard for what that command is or what it does.

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