Skip to content

Instantly share code, notes, and snippets.

@der-pw
Last active February 3, 2024 08:43
Show Gist options
  • Save der-pw/554f089bb44f37ef77035c2474076a82 to your computer and use it in GitHub Desktop.
Save der-pw/554f089bb44f37ef77035c2474076a82 to your computer and use it in GitHub Desktop.
extended M106 Macro
[gcode_macro M106]
description: Custom fan control feature that allows slicers like Orca or S3D to adopt bambu lab-specific auxiliary fan control to define the speed of the auxiliary cooling fan, or exhaust fan
rename_existing: M106.1
gcode:
## Original macro can be found somewhere in the Orca Discord. Thank you, dude!
## this has some changes, so the order of the parameters S and P does not matter and the fan control value is specified as float 0.0 - 1.0
## Define the device names of the controlled fans here. If you're not running a specific type of fan, remove it and the macro will have no effect
## Define the mapping between the bambu lab fans and the fan_generic fans defined
## These are the names you gave to the fans in your printer.cfg
## NOTE: ensure P0 is left as "fan". This is important as you cannot set the
## standard part cooling fan Using the set_fan_speed command.
## This is accounted for in the macro and handled appropriately.
{% set fan_map = ({
"P0": "fan",
"P1": "BENTO_mini",
"P2": "aux_fan1",
})
%}
## Divide the params given into an array split on a singular whitespace
{% set split_params = rawparams.split(" ") %}
## if we've had more than one paramater passed define requested fan and speed
## if one paramater is passed, assume were asking for the part cooling fan
{% if split_params|length == 1 %}
{% set requested_fan = "P0" %}
{% set fan_speed = rawparams %}
{% else %}
{% set value_1, value_2 = split_params %}
## Check order in which the parameters were entered and set the values ​​at the correct position
{% if 'P' in value_1 %}
{% set requested_fan = value_1 %}
{% set fan_speed = value_2 %}
{% else %}
{% set requested_fan = value_2 %}
{% set fan_speed = value_1 %}
{% endif %}
{% endif %}
## check to ensure the requested fan is mapped
## this allows us to safely ignore commands for fans were not using
{% if requested_fan in fan_map %}
## define the target fan device as per the above map
{% set target_fan = fan_map[requested_fan] %}
# If the target fan is the part cooling fan, just use the standard M106.
{% if target_fan == "fan" %}
M106.1 {fan_speed}
{% else %}
## strip the leading S from the fan speed
{% if fan_speed[0:1] == "S" %}
{% set fan_speed = fan_speed[1:] %}
{% endif %}
## Convert the 0-255 fan percentage to a percentage out of 100
## and round it to an int
{% set fan_percentage = (fan_speed|int / 255 * 100)|round(0) %}
## divide by 100 to get the value 0.0 - 1.0
{% set fan_klipperval = (fan_percentage|float / 100) %}
## Set the requested fan speed on the target fan
SET_FAN_SPEED fan={target_fan} speed={fan_klipperval}
## M118 SET_FAN_SPEED fan={target_fan} speed={fan_klipperval}
{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment