Change hybrid treshold online

UPD:
TMC2208/2209/2225/2226 are slightly different from TMC5160

[gcode_macro HYBRID_TPWMTHRS]
gcode:
    {% set speed = params.SPEED|default(50)|int %} 
    {% set tmc_hz = 12000000 %}

    {% set x_rt = printer.configfile.settings.stepper_x.rotation_distance|float %}
    {% set y_rt = printer.configfile.settings.stepper_y.rotation_distance|float %} 
    {% set x_rs = printer.configfile.settings.stepper_x.full_steps_per_rotation|float %}
    {% set y_rs = printer.configfile.settings.stepper_y.full_steps_per_rotation|float %}

    # Comment/uncomment depends on default configuration for TMC, if you already enable tpwmthrs, for speed like 1mm/s you can leave it as is
    # Only for TMC22xx
    # SET_TMC_FIELD STEPPER=stepper_y FIELD=en_spreadCycle VALUE=0
    # SET_TMC_FIELD STEPPER=stepper_x FIELD=en_spreadCycle VALUE=0

    {% set x_tpwmthrs = (tmc_hz * x_rt/x_rs/256/speed)|int %}
    M118 X TPWMTHRS={x_tpwmthrs} ~ {speed} mm/s
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE={x_tpwmthrs} STEPPER=stepper_x

    {% set y_tpwmthrs = (tmc_hz * y_rt/y_rs/256/speed)|int %}
    M118 X TPWMTHRS={y_tpwmthrs} ~ {speed} mm/s
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE={y_tpwmthrs} STEPPER=stepper_y

And another 2 for enable/disable stealth/spread

[gcode_macro _descrease_velocity]
# initial_duration: 0.0
gcode:
    SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity / 5 | int}
    SET_VELOCITY_LIMIT ACCEL={printer.configfile.settings.printer.max_accel / 5 | int}
    SET_VELOCITY_LIMIT ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel / 5 | int}
    SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={printer.configfile.settings.printer.square_corner_velocity / 5 | int}

[delayed_gcode _increase_velocity]
initial_duration: 0.0
gcode:
    SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} 
    SET_VELOCITY_LIMIT ACCEL={printer.configfile.settings.printer.max_accel}
    SET_VELOCITY_LIMIT ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel}
    SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={printer.configfile.settings.printer.square_corner_velocity}

[delayed_gcode _en_spread_y]
initial_duration: 0.0
gcode:
    # TMC2209
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE=0 STEPPER=stepper_y
    SET_TMC_FIELD STEPPER=stepper_y FIELD=en_spreadCycle VALUE=1
    # TMC5160
    # SET_TMC_FIELD FIELD=TPWMTHRS VALUE=1048575 TEPPER=stepper_y
    
[delayed_gcode _en_spread_x]
initial_duration: 0.0
gcode:
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE=0 STEPPER=stepper_x
    SET_TMC_FIELD STEPPER=stepper_x FIELD=en_spreadCycle VALUE=1
    # TMC5160
    # SET_TMC_FIELD FIELD=TPWMTHRS VALUE=1048575 TEPPER=stepper_x

[delayed_gcode _dis_spread_y]
initial_duration: 0.0
gcode:
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE=0 STEPPER=stepper_y
    SET_TMC_FIELD STEPPER=stepper_y FIELD=en_spreadCycle VALUE=0
    # TMC5160
    # SET_TMC_FIELD FIELD=TPWMTHRS VALUE=1 STEPPER=stepper_y

[delayed_gcode _dis_spread_x]
initial_duration: 0.0
gcode:
    SET_TMC_FIELD FIELD=TPWMTHRS VALUE=0 STEPPER=stepper_x
    SET_TMC_FIELD STEPPER=stepper_x FIELD=en_spreadCycle VALUE=0
    # TMC5160
    # SET_TMC_FIELD FIELD=TPWMTHRS VALUE=1 STEPPER=stepper_x

[gcode_macro SpreadCycle]
gcode:
    _descrease_velocity
    UPDATE_DELAYED_GCODE ID=_en_spread_x DURATION=1
    UPDATE_DELAYED_GCODE ID=_en_spread_y DURATION=3
    UPDATE_DELAYED_GCODE ID=_increase_velocity DURATION=5

[delayed_gcode _stealth_velocity]
initial_duration: 0.0
gcode:
    SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity / 2} 
    SET_VELOCITY_LIMIT ACCEL={printer.configfile.settings.printer.max_accel / 2} 
    SET_VELOCITY_LIMIT ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel / 2} 
    SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={printer.configfile.settings.printer.square_corner_velocity / 2}

[gcode_macro StealthChop]
gcode:
    _descrease_velocity
    UPDATE_DELAYED_GCODE ID=_dis_spread_x DURATION=1
    UPDATE_DELAYED_GCODE ID=_dis_spread_y DURATION=3
    UPDATE_DELAYED_GCODE ID=_stealth_velocity DURATION=5
2 Likes