Skip to content

Instantly share code, notes, and snippets.

@ChipCE
Last active November 23, 2024 12:55
Show Gist options
  • Save ChipCE/95fdbd3c2f3a064397f9610f915f7d02 to your computer and use it in GitHub Desktop.
Save ChipCE/95fdbd3c2f3a064397f9610f915f7d02 to your computer and use it in GitHub Desktop.
Klipper bed mesh on print area only macro install guide

READ THIS FIRST

Adaptive bed mesh is merged into klipper master branch. You can use this feature without this custom macro. Official klipper adaptive bed mesh

Klipper mesh on print area only install guide

What this macro do

  • This macro will dynamically changing the bed mesh area based on the size of the parts will be printed. The fw will only probe on the area that the part will be printed (plus mesh_area_offset value)

Setup guide

  • (1) Add the following macro to your printer config, this will replace the default BED_MESH_CALIBRATE command.
[gcode_macro BED_MESH_CALIBRATE]
rename_existing: BED_MESH_CALIBRATE_BASE
; gcode parameters
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
; the clearance between print area and probe area 
variable_mesh_area_offset : 5.0
; number of sample per probe point
variable_probe_samples : 2
; minimum probe count
variable_min_probe_count : 4
; scale up the probe count, should be 1.0 ~ < variable_max_probe_count/variable_min_probe_count
variable_probe_count_scale_factor : 1.0
; enable preference index
variable_enable_reference_index : False
gcode:
    {% if params.AREA_START and params.AREA_END %}
        {% set bedMeshConfig = printer["configfile"].config["bed_mesh"] %}
        {% set safe_min_x = bedMeshConfig.mesh_min.split(",")[0]|float %}
        {% set safe_min_y = bedMeshConfig.mesh_min.split(",")[1]|float %}
        {% set safe_max_x = bedMeshConfig.mesh_max.split(",")[0]|float %}
        {% set safe_max_y = bedMeshConfig.mesh_max.split(",")[1]|float %}

        {% set area_min_x = params.AREA_START.split(",")[0]|float %}
	{% set area_min_y = params.AREA_START.split(",")[1]|float %}
	{% set area_max_x = params.AREA_END.split(",")[0]|float %}
	{% set area_max_y = params.AREA_END.split(",")[1]|float %}

	{% if bedMeshConfig.probe_count.split(",")|length == 2 %}
            {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
            {% set meshPointY = bedMeshConfig.probe_count.split(",")[1]|int %}
        {% else %}
            {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
            {% set meshPointY = bedMeshConfig.probe_count.split(",")[0]|int %}
        {% endif %}

	{% set meshMaxPointX = meshPointX %}
	{% set meshMaxPointY = meshPointY %}


        {% if (area_min_x < area_max_x) and (area_min_y < area_max_y) %}
            {% if area_min_x - mesh_area_offset >=  safe_min_x %}
                {% set area_min_x = area_min_x - mesh_area_offset %}
            {% else %}
                {% set area_min_x = safe_min_x %}
            {% endif %}

            {% if area_min_y - mesh_area_offset >=  safe_min_y %}
                {% set area_min_y = area_min_y - mesh_area_offset %}
            {% else %}
                {% set area_min_y = safe_min_y %}
            {% endif %}

            {% if area_max_x + mesh_area_offset <=  safe_max_x %}
                {% set area_max_x = area_max_x + mesh_area_offset %}
            {% else %}
                {% set area_max_x = safe_max_x %}
            {% endif %}

            {% if area_max_y + mesh_area_offset <=  safe_max_y %}
                {% set area_max_y = area_max_y + mesh_area_offset %}
            {% else %}
                {% set area_max_y = safe_max_y %}
            {% endif %}

            {% set meshPointX = (meshPointX * (area_max_x - area_min_x) / (safe_max_x - safe_min_x) * probe_count_scale_factor)|round(0)|int %}
            {% if meshPointX < min_probe_count %}
                {% set meshPointX = min_probe_count %}
            {% endif %}
	    {% if meshPointX > meshMaxPointX %}
                {% set meshPointX = meshMaxPointX %}
            {% endif %}

            {% set meshPointY = (meshPointY * (area_max_y -area_min_y ) / (safe_max_y - safe_min_y) * probe_count_scale_factor )|round(0)|int %}
            {% if meshPointY < min_probe_count %}
                {% set meshPointY = min_probe_count %}
            {% endif %}
	    {% if meshPointY > meshMaxPointY %}
                {% set meshPointY = meshMaxPointY %}
            {% endif %}

            {% set algorithm = "bicubic" %}
	    {% if "algorithm" in bedMeshConfig %}
	        {% set algorithm = bedMeshConfig.algorithm %}
            {% endif %}
            {% if meshPointX >=7 or meshPointY >=7 %}
                {% set algorithm = "bicubic" %}
            {% endif %}

            {% if enable_reference_index %}
                {% set referenceIndex = (meshPointX * meshPointY / 2 - 1 )|round(0)|int %}
                BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm} relative_reference_index={referenceIndex}
            {% else %}
                BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm}
            {% endif %}
        {% else %}
            BED_MESH_CALIBRATE_BASE
        {% endif %}
    {% else %}
        BED_MESH_CALIBRATE_BASE
    {% endif %}
  • (2) Go to slicer setting and replace the old bed mesh gcode the following command.

Prusa Slicer
BED_MESH_CALIBRATE AREA_START={first_layer_print_min[0]},{first_layer_print_min[1]} AREA_END={first_layer_print_max[0]},{first_layer_print_max[1]}

Ideal maker
BED_MESH_CALIBRATE AREA_START={print_pos_min_x},{print_pos_min_y} AREA_END={print_pos_max_x},{print_pos_max_y}

Cura slicer
BED_MESH_CALIBRATE AREA_START=%MINX%,%MINY% AREA_END=%MAXX%,%MAXY%

*(Cura slicer plugin) To make the macro to work in Cura slicer, you need to install the post process plugin click here to download (based on frankbags's script)
- In cura menu Help -> Show configuration folder.
- Copy the python script from the above link in to scripts folder.
- Restart Cura
- In cura menu Extensions -> Post processing -> Modify G-Code and select Klipper print area mesh

For user using single command START_PRINT

  • (*) If you use single command start gcode like START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0} .You might need to add parameter parsing to BED_MESH_CALIBRATE inside START_PRINT. This is an example, also check my macro sample
[gcode_macro START_PRINT]
variable_parameter_EXTRUDER_TEMP: 190
variable_parameter_BED_TEMP: 60
; gcode parameters for area bed mesh
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
gcode:
    # preheat, homing, etc
    BED_MESH_CALIBRATE AREA_START={params.AREA_START|default("0,0")} AREA_END={params.AREA_END|default("0,0")}
    # the rest of your start macro here

And you will need to change Slicer start gcode to this

  • Cura: START_PRINT EXTRUDER_TEMP={material_print_temperature_layer_0} BED_TEMP={material_bed_temperature_layer_0} AREA_START=%MINX%,%MINY% AREA_END=%MAXX%,%MAXY%
  • Prusa slicer: START_PRINT EXTRUDER_TEMP=[first_layer_temperature] BED_TEMP=[first_layer_bed_temperature] AREA_START={first_layer_print_min[0]},{first_layer_print_min[1]} AREA_END={first_layer_print_max[0]},{first_layer_print_max[1]}

change logs

  • 2022/07/21
    • Added force lagrange algorithm for mesh with lower than 3 points
    • Added enable_reference_index config flag

The code does not work?

  • I will try to help if possible, you will have to post ALL of your configs, your slicer start gcode and your sliced gcode.
  • If I'm cannot or too slow to respose, our discord members might help you. Check our discord here
@locki-cz
Copy link

locki-cz commented Dec 20, 2022

there are some bugs in macro, i used diferent one, much better ... but still have problem with first layer ... then just turned it of because floating z endstop with bed mesh when it is not complete.... no time to install TAP ... try this one:

https://github.com/Frix-x/klipper-voron-V2/blob/main/macros/calibration/adaptive_bed_mesh.cfg

and i will use this with tap and it have random purge ...

https://github.com/kyleisah/Klipper-Adaptive-Meshing-Purging

@Skennd
Copy link

Skennd commented Dec 21, 2022

I'll have to check that out once I get a coreXY set up. I'm on an ender 3 so I've been avoiding the nice voron and ratOS resources. I feel like it would take more work to adapt than this was.
If you don't mind be asking, what's buggy about this one?

@MrLowe72
Copy link

MrLowe72 commented Jan 1, 2023

Hi I think I know the answer but could do with this being confirmed, after installing the macro and the bed mesh being calibrated for the print i now get this when the probe is completed

Move out of range: 203.175 361.000 7.600 [14.000]

This is an Ender 3 S1 Pro with a bed of 220 x 220, why is it calculating a 203 x 361.

thanks

@ChipCE
Copy link
Author

ChipCE commented Jan 2, 2023

If the bed mesh is completed then the error showup, the problem is more likely from your start gcode. Just my random guess, maybe the G0 or G1 is called when the printer in relative mode. You can try to add G90 line right after bed meah command called.

@rstudner
Copy link

rstudner commented Jan 7, 2023

Question - so I have this working (I think). But wanted a bit of clarity.

If I go into Fluidd/Mainsail "Tune" to view the Mesh, it shows it near a corner and not "in the center of the bed" though I of course watched it probe perfectly in the center of my bed around the "bound" of the print as desired
Screenshot 2023-01-07 at 8 36 38 AM

@frankbags
Copy link

are the X and Y coordinates correct? that's all that matters, the visual representation will be cut off after the furthest probe points.

@rstudner
Copy link

rstudner commented Jan 9, 2023

Anyone know in superslicer what the custom G code would be? I can't seem to find the right variable:

START_PRINT BED_TEMP=[first_layer_bed_temperature] EXTRUDER_TEMP=[first_layer_temperature] AREA_START=[first_layer_print_min[0]],[first_layer_print_min[1]] AREA_END=[first_layer_print_max[0]],[first_layer_print_max[1]]

doesn't like first_layer_print_min[0] etc.

@ChipCE
Copy link
Author

ChipCE commented Jan 10, 2023

@rstudner The web UI only draw the area you probed. Does the web UI display the exact mesh position?

what slicer are you using?

@petitxef
Copy link

@rstudner I have this code on start g-code on superslicer:

START_PRINT AREA_START_X={first_layer_print_min[0]} AREA_START_Y={first_layer_print_min[1]} AREA_END_X={first_layer_print_max[0]} AREA_END_Y={first_layer_print_max[1]}

@0RedSky0
Copy link

0RedSky0 commented Jan 17, 2023

I have a Shutdown at the start, can someone help me:

12:45
Internal error on command:"BED_MESH_CALIBRATE"
12:45
Internal error on command:"BED_MESH_CALIBRATE_BASE"
12:45
Klipper state: Shutdown

Log:
Generating new points...
bed_mesh: generated points
Index | Tool Adjusted | Probe
0 | (89.0, 138.2) | (67.0, 128.7)
1 | (105.8, 138.2) | (83.8, 128.7)
2 | (122.7, 138.2) | (100.7, 128.7)
3 | (139.6, 138.2) | (117.6, 128.7)
4 | (156.5, 138.2) | (134.5, 128.7)
5 | (156.5, 155.1) | (134.5, 145.6)
6 | (139.6, 155.1) | (117.6, 145.6)
7 | (122.7, 155.1) | (100.7, 145.6)
8 | (105.8, 155.1) | (83.8, 145.6)
9 | (89.0, 155.1) | (67.0, 145.6)
10 | (89.0, 172.0) | (67.0, 162.5)
11 | (105.8, 172.0) | (83.8, 162.5)
12 | (122.7, 172.0) | (100.7, 162.5)
13 | (139.6, 172.0) | (117.6, 162.5)
14 | (156.5, 172.0) | (134.5, 162.5)
15 | (156.5, 188.9) | (134.5, 179.4)
16 | (139.6, 188.9) | (117.6, 179.4)
17 | (122.7, 188.9) | (100.7, 179.4)
18 | (105.8, 188.9) | (83.8, 179.4)
19 | (89.0, 188.9) | (67.0, 179.4)
20 | (89.0, 205.7) | (67.0, 196.2)
21 | (105.8, 205.7) | (83.8, 196.2)
22 | (122.7, 205.7) | (100.7, 196.2)
23 | (139.6, 205.7) | (117.6, 196.2)
24 | (156.5, 205.7) | (134.5, 196.2)
Internal error on command:"BED_MESH_CALIBRATE_BASE"
Traceback (most recent call last):
File "/home/pi/klipper/klippy/gcode.py", line 198, in _process_commands
handler(gcmd)
File "/home/pi/klipper/klippy/gcode.py", line 135, in
func = lambda params: origfunc(self._get_extended_params(params))
File "/home/pi/klipper/klippy/gcode.py", line 135, in
func = lambda params: origfunc(self._get_extended_params(params))
File "/home/pi/klipper/klippy/extras/bed_mesh.py", line 607, in cmd_BED_MESH_CALIBRATE
self.update_config(gcmd)
File "/home/pi/klipper/klippy/extras/bed_mesh.py", line 576, in update_config
self.print_generated_points(gcmd.respond_info)
File "/home/pi/klipper/klippy/extras/bed_mesh.py", line 406, in print_generated_points
% (rri, self.points[rri][0], self.points[rri][1]))
IndexError: list index out of range
Transition to shutdown state: Internal error on command:"BED_MESH_CALIBRATE_BASE"

@ChipCE
Copy link
Author

ChipCE commented Jan 17, 2023

@0RedSky0 I will need ALL of you config and the sliced gcode to debug. Can you post it here plz?

@0RedSky0
Copy link

[include mainsail.cfg]

VzBoT############

#######################

[printer]
kinematics: corexy
max_velocity: 800
max_accel: 6000
max_accel_to_decel: 6000
max_z_velocity: 35
max_z_accel: 120
square_corner_velocity: 12

[mcu]
serial:/dev/serial/by-id/usb-Klipper_stm32h743xx_300039000951303339323537-if00

[mcu rpi]
serial: /tmp/klipper_host_mcu

[adxl345]
cs_pin: rpi:None

[resonance_tester]
accel_chip: adxl345
probe_points:
165, 165, 20 # an example

########################################

EXP1 / EXP2 (display) pins

########################################

[board_pins]
aliases:
# EXP1 header
EXP1_1=PC5, EXP1_3=PB1, EXP1_5=PE9, EXP1_7=PE11, EXP1_9=,
EXP1_2=PB0, EXP1_4=PE8, EXP1_6=PE10, EXP1_8=PE12, EXP1_10=<5V>,
# EXP2 header
EXP2_1=PA6, EXP2_3=PE7, EXP2_5=PB2, EXP2_7=PC4, EXP2_9=,
EXP2_2=PA5, EXP2_4=PA4, EXP2_6=PA7, EXP2_8=, EXP2_10=

See the sample-lcd.cfg file for definitions of common LCD displays.

########################
########################

[stepper_x]
step_pin: PA15
dir_pin: PA8
enable_pin: !PD1
microsteps: 16
rotation_distance: 40
endstop_pin: ^PC1
position_endstop: 0
position_min: 0
position_max: 328
homing_speed: 100
full_steps_per_rotation: 200
homing_retract_dist: 0
homing_positive_dir: false
step_pulse_duration: 0.000001

[tmc5160 stepper_x]
cs_pin: PD0
spi_software_miso_pin: PE15
spi_software_mosi_pin: PE13
spi_software_sclk_pin: PE14
run_current: 1.100
#hold_current: 1.200
diag1_pin: PC3
sense_resistor: 0.075

########################

[stepper_y]
step_pin: PD4
dir_pin: PD3
enable_pin: !PD6
microsteps: 16
rotation_distance: 40
endstop_pin: ^PC3
position_endstop: 0
position_min: 0
position_max: 325
homing_speed: 100
full_steps_per_rotation: 200
homing_retract_dist: 0
homing_positive_dir: false
step_pulse_duration: 0.000001

[tmc5160 stepper_y]
cs_pin: PD5
spi_software_miso_pin: PE15
spi_software_mosi_pin: PE13
spi_software_sclk_pin: PE14
run_current: 1.100
#hold_current: 1.000
diag1_pin: PC1
sense_resistor: 0.075

########################

[stepper_z]
step_pin: PE2
dir_pin: PE3
enable_pin: !PE0
microsteps: 16
rotation_distance: 3.985
endstop_pin: probe:z_virtual_endstop
position_min: -3
position_max: 320
full_steps_per_rotation: 200
homing_retract_dist: 5.0
homing_positive_dir: false
homing_speed: 5.0
second_homing_speed: 2.5
step_pulse_duration: 0.000001

[tmc5160 stepper_z]
cs_pin: PE1
spi_software_miso_pin: PE15
spi_software_mosi_pin: PE13
spi_software_sclk_pin: PE14
run_current: 1.5
#hold_current: 0.600
diag1_pin: PC0
sense_resistor: 0.075

[stepper_z1]
step_pin: PD11
dir_pin: PD10
enable_pin: !PD13
microsteps: 16
rotation_distance: 3.985
full_steps_per_rotation: 200
step_pulse_duration: 0.000001

[tmc5160 stepper_z1]
cs_pin: PD12
spi_software_miso_pin: PE15
spi_software_mosi_pin: PE13
spi_software_sclk_pin: PE14
run_current: 1.5
#hold_current: 0.600
diag1_pin: PA0
sense_resistor: 0.075

########################

[extruder]

Vz-HextrudORT

step_pin: PD15
dir_pin: PD14
enable_pin: !PC7
microsteps: 16
rotation_distance: 5.586
nozzle_diameter: 0.600
#nozzle_diameter: 0.400
filament_diameter: 1.750
full_steps_per_rotation: 200
heater_pin: PB3
sensor_type: ATC Semitec 104NT-4-R025H42G
sensor_pin: PA2
control: pid
pid_kp = 24.525
pid_ki = 2.555
pid_kd = 58.860
min_temp: 0
max_temp: 305
max_extrude_only_distance: 500.0
max_extrude_cross_section: 300
min_extrude_temp: 0
#pressure_advance: 0.02
#pressure_advance_smooth_time: 0.03
step_pulse_duration: 0.000001

[tmc5160 extruder]
cs_pin: PC6
spi_software_miso_pin: PE15
spi_software_mosi_pin: PE13
spi_software_sclk_pin: PE14
run_current: 0.6
#hold_current:0.300
diag1_pin: PC1
sense_resistor: 0.075

########################
########################

[heater_bed]
heater_pin: PD7
sensor_type: Generic 3950
sensor_pin: PA1
min_temp: 0
max_temp: 130
control: pid
pid_kp = 60.859
pid_ki = 3.145
pid_kd = 294.406

########################

FANS

########################

[fan]
##Print Cooling Fan - FAN1 Connector
pin: PB6
max_power: 1
kick_start_time: 0.5
off_below: 0.10

[heater_fan hotend_fan]
##Hotend Fan - FAN0 Connector
pin: PB7
max_power: 1.0
kick_start_time: 0.5
heater: extruder
heater_temp: 50.0

[fan_generic Exhaust_fan]

exhaust fan - FAN2

pin: PB5
max_power: 1
shutdown_speed: 0
kick_start_time: 0.1
off_below: 0.10

########################

PAUSE_RESUME

########################

[pause_resume]
recover_velocity: 350

########################

SCREWSADJUST

########################

[screws_tilt_adjust]
screw1: 40, 35
screw1_name: front left screw
screw2: 328, 35
screw2_name: front right screw
screw3: 328, 318
screw3_name: rear right screw
screw4: 40, 318
screw4_name: rear left screw
horizontal_move_z: 12
speed: 350
screw_thread: CW-M3

########################

PROBE

########################

[bltouch]
sensor_pin: ^PC13
control_pin: PE5
x_offset: -22
y_offset: -9.5
#z_offset: 3.350

#[safe_z_home]
#home_xy_position: 187, 175
#speed: 400
#z_hop: 7
#z_hop_speed: 30

[homing_override]
set_position_z: 0
#set_position_x: 0
#set_position_y: 0
axes: xyz
gcode:
G90
G0 Z5 F600
G28 X0 Y0
G0 X187 Y175 F9000
G28 Z0
G0 Z5 F500
Z_TILT_ADJUST
G0 X187 Y175 F9000
G28 Z0
G0 Z5 F500

########################

MeshBed

########################

[z_tilt]
z_positions: 383, 160
-50, 160
points: 328, 170
35, 170
speed: 400
horizontal_move_z: 7
retries: 20
retry_tolerance: 0.0020

########################

MeshBed

########################

[bed_mesh]
speed: 400
horizontal_move_z: 7
mesh_min: 22, 10
mesh_max: 300, 310
probe_count: 21, 21
algorithm: bicubic
relative_reference_index: 220

[gcode_macro BED_MESH_CALIBRATE]
rename_existing: BED_MESH_CALIBRATE_BASE
; gcode parameters
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
; the clearance between print area and probe area
variable_mesh_area_offset : 10.0
; number of sample per probe point
variable_probe_samples : 2
; minimum probe count
variable_min_probe_count : 3
; scale up the probe count, should be 1.0 ~ < variable_max_probe_count/variable_min_probe_count
variable_probe_count_scale_factor : 1.0
; enable preference index
variable_enable_reference_index : False
gcode:
{% if params.AREA_START and params.AREA_END %}
{% set bedMeshConfig = printer["configfile"].config["bed_mesh"] %}
{% set safe_min_x = bedMeshConfig.mesh_min.split(",")[0]|float %}
{% set safe_min_y = bedMeshConfig.mesh_min.split(",")[1]|float %}
{% set safe_max_x = bedMeshConfig.mesh_max.split(",")[0]|float %}
{% set safe_max_y = bedMeshConfig.mesh_max.split(",")[1]|float %}

    {% set area_min_x = params.AREA_START.split(",")[0]|float %}
{% set area_min_y = params.AREA_START.split(",")[1]|float %}
{% set area_max_x = params.AREA_END.split(",")[0]|float %}
{% set area_max_y = params.AREA_END.split(",")[1]|float %}

{% if bedMeshConfig.probe_count.split(",")|length == 2 %}
        {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
        {% set meshPointY = bedMeshConfig.probe_count.split(",")[1]|int %}
    {% else %}
        {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
        {% set meshPointY = bedMeshConfig.probe_count.split(",")[0]|int %}
    {% endif %}

{% set meshMaxPointX = meshPointX %}
{% set meshMaxPointY = meshPointY %}


    {% if (area_min_x < area_max_x) and (area_min_y < area_max_y) %}
        {% if area_min_x - mesh_area_offset >=  safe_min_x %}
            {% set area_min_x = area_min_x - mesh_area_offset %}
        {% else %}
            {% set area_min_x = safe_min_x %}
        {% endif %}

        {% if area_min_y - mesh_area_offset >=  safe_min_y %}
            {% set area_min_y = area_min_y - mesh_area_offset %}
        {% else %}
            {% set area_min_y = safe_min_y %}
        {% endif %}

        {% if area_max_x + mesh_area_offset <=  safe_max_x %}
            {% set area_max_x = area_max_x + mesh_area_offset %}
        {% else %}
            {% set area_max_x = safe_max_x %}
        {% endif %}

        {% if area_max_y + mesh_area_offset <=  safe_max_y %}
            {% set area_max_y = area_max_y + mesh_area_offset %}
        {% else %}
            {% set area_max_y = safe_max_y %}
        {% endif %}

        {% set meshPointX = (meshPointX * (area_max_x - area_min_x) / (safe_max_x - safe_min_x) * probe_count_scale_factor)|round(0)|int %}
        {% if meshPointX < min_probe_count %}
            {% set meshPointX = min_probe_count %}
        {% endif %}
    {% if meshPointX > meshMaxPointX %}
            {% set meshPointX = meshMaxPointX %}
        {% endif %}

        {% set meshPointY = (meshPointY * (area_max_y -area_min_y ) / (safe_max_y - safe_min_y) * probe_count_scale_factor )|round(0)|int %}
        {% if meshPointY < min_probe_count %}
            {% set meshPointY = min_probe_count %}
        {% endif %}
    {% if meshPointY > meshMaxPointY %}
            {% set meshPointY = meshMaxPointY %}
        {% endif %}

        {% set algorithm = "lagrange" %}
        {% if "algorithm" in bedMeshConfig and meshPointX >=4 and meshPointY >=4 %}
            {% set algorithm = bedMeshConfig.algorithm %}
        {% endif %}

        {% if enable_reference_index %}
            {% set referenceIndex = (meshPointX * meshPointY / 2 - 1 )|round(0)|int %}
            BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm} relative_reference_index={referenceIndex}
        {% else %}
            BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm}
        {% endif %}
    {% else %}
        BED_MESH_CALIBRATE_BASE
    {% endif %}
{% else %}
    BED_MESH_CALIBRATE_BASE
{% endif %}

########################
######## MACROS ########
########################

########################

Z TILT ADJUST

########################

[gcode_macro Z_TILT]
gcode:
Z_TILT_ADJUST

########################

START_PRINT

########################

[gcode_macro START_PRINT]
gcode:

Use Mesh Profile

#BED_MESH_PROFILE LOAD="achtzig"
#BED_MESH_PROFILE LOAD="neunzig"
#BED_MESH_PROFILE LOAD="neunzigrau"
#####Princore PA12 CF#####
#SET_PRESSURE_ADVANCE ADVANCE=0.03
#SET_PRESSURE_ADVANCE EXTRUDER=extruder SMOOTH_TIME=0.03

########################

END_PRINT

########################

[gcode_macro END_PRINT]
variable_machine_depth: 300
variable_machine_width: 300
gcode:
# Turn off bed, extruder, and fan
M140 S0
M104 S0
M106 S0
# Relative positionning
G91
# Retract and raise Z
G1 Z0.2 E-2 F2400
# Wipe out
G1 X5 Y5 F3000
# Raise Z more
G1 Z10
# Absolute positionning
G90
# Present print
G1 X{machine_width} Y{machine_depth}
# Disable steppers
M84
BED_MESH_CLEAR
# Print message on LCD
M117 Druck beendet!

## <---------------------- SAVE_CONFIG ---------------------->
#
# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
##
#
# [bltouch]
#*# z_offset = 3.55

@0RedSky0
Copy link

0RedSky0 commented Jan 17, 2023

;FLAVOR:Marlin
;TIME:383
;Filament used: 1.28256m
;Layer height: 0.2
;MINX:148.375
;MINY:148.375
;MINZ:0.2
;MAXX:176.625
;MAXY:176.625
;MAXZ:10.4
;POSTPROCESSED
;Generated with Cura_SteamEngine 5.2.1
M140 S90
M105
M190 S90
M104 S265
M105
M109 S265
M82 ;absolute extrusion mode
SET_GCODE_OFFSET Z=0.0
G28
BED_MESH_CALIBRATE AREA_START=148.375,148.375 AREA_END=176.625,176.625
;Purge Line
G1 X3.1 Y20 F14000.0
G1 Z5 F3000
G1 Z0.15 F300
G92 E0
G1 Z0.25 F2000.0
G1 X3.1 Y200.0 Z0.25 F1500.0 E15
G1 X3.5 Y200.0 Z0.25 F5000.0
G92 E0
G92 E0
G92 E0
G1 F3000 E-1
;LAYER_COUNT:53
;LAYER:0
M107
G0 F21000 X149.736 Y149.494 Z0.2

@ChipCE
Copy link
Author

ChipCE commented Jan 17, 2023

Hi,
Because you have reference_index, try to change the variable_enable_reference_index : False to variable_enable_reference_index : True

@0RedSky0
Copy link

Thank you for your quick help. It works perfectly now!

@rstudner
Copy link

@rstudner I have this code on start g-code on superslicer:

START_PRINT AREA_START_X={first_layer_print_min[0]} AREA_START_Y={first_layer_print_min[1]} AREA_END_X={first_layer_print_max[0]} AREA_END_Y={first_layer_print_max[1]}

Thanks!

I ended up with this m'self:

START_PRINT BED_TEMP=[first_layer_bed_temperature] EXTRUDER_TEMP=[first_layer_temperature]
AREA_START={first_layer_print_min[0]},{first_layer_print_min[1]} AREA_START={first_layer_print_max[0]},{first_layer_print_max[1]}

@Lekkz
Copy link

Lekkz commented Jan 27, 2023

please help, I sometimes get an error and I haven't figured out the reason why? the frustrating thing is that it only happens sometimes. although I do understand what the error is saying I don't understand why is happening.
2023-01-27 11_14_26
2023-01-27 11_13_28
``[gcode_macro BED_MESH_CALIBRATE]
rename_existing: BED_MESH_CALIBRATE_BASE
; gcode parameters
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
; the clearance between print area and probe area
variable_mesh_area_offset : 2.0
; number of sample per probe point
variable_probe_samples : 1
; minimum probe count
variable_min_probe_count : 3
; scale up the probe count, should be 1.0 ~ < variable_max_probe_count/variable_min_probe_count
variable_probe_count_scale_factor : 1.0
; enable preference index
variable_enable_reference_index : False
gcode:
{% if params.AREA_START and params.AREA_END %}
{% set bedMeshConfig = printer["configfile"].config["bed_mesh"] %}
{% set safe_min_x = bedMeshConfig.mesh_min.split(",")[0]|float %}
{% set safe_min_y = bedMeshConfig.mesh_min.split(",")[1]|float %}
{% set safe_max_x = bedMeshConfig.mesh_max.split(",")[0]|float %}
{% set safe_max_y = bedMeshConfig.mesh_max.split(",")[1]|float %}

    {% set area_min_x = params.AREA_START.split(",")[0]|float %}
{% set area_min_y = params.AREA_START.split(",")[1]|float %}
{% set area_max_x = params.AREA_END.split(",")[0]|float %}
{% set area_max_y = params.AREA_END.split(",")[1]|float %}

{% if bedMeshConfig.probe_count.split(",")|length == 2 %}
        {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
        {% set meshPointY = bedMeshConfig.probe_count.split(",")[1]|int %}
    {% else %}
        {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
        {% set meshPointY = bedMeshConfig.probe_count.split(",")[0]|int %}
    {% endif %}

{% set meshMaxPointX = meshPointX %}
{% set meshMaxPointY = meshPointY %}


    {% if (area_min_x < area_max_x) and (area_min_y < area_max_y) %}
        {% if area_min_x - mesh_area_offset >=  safe_min_x %}
            {% set area_min_x = area_min_x - mesh_area_offset %}
        {% else %}
            {% set area_min_x = safe_min_x %}
        {% endif %}

        {% if area_min_y - mesh_area_offset >=  safe_min_y %}
            {% set area_min_y = area_min_y - mesh_area_offset %}
        {% else %}
            {% set area_min_y = safe_min_y %}
        {% endif %}

        {% if area_max_x + mesh_area_offset <=  safe_max_x %}
            {% set area_max_x = area_max_x + mesh_area_offset %}
        {% else %}
            {% set area_max_x = safe_max_x %}
        {% endif %}

        {% if area_max_y + mesh_area_offset <=  safe_max_y %}
            {% set area_max_y = area_max_y + mesh_area_offset %}
        {% else %}
            {% set area_max_y = safe_max_y %}
        {% endif %}

        {% set meshPointX = (meshPointX * (area_max_x - area_min_x) / (safe_max_x - safe_min_x) * probe_count_scale_factor)|round(0)|int %}
        {% if meshPointX < min_probe_count %}
            {% set meshPointX = min_probe_count %}
        {% endif %}
    {% if meshPointX > meshMaxPointX %}
            {% set meshPointX = meshMaxPointX %}
        {% endif %}

        {% set meshPointY = (meshPointY * (area_max_y -area_min_y ) / (safe_max_y - safe_min_y) * probe_count_scale_factor )|round(0)|int %}
        {% if meshPointY < min_probe_count %}
            {% set meshPointY = min_probe_count %}
        {% endif %}
    {% if meshPointY > meshMaxPointY %}
            {% set meshPointY = meshMaxPointY %}
        {% endif %}

        {% set algorithm = "lagrange" %}
        {% if "algorithm" in bedMeshConfig and meshPointX >=4 and meshPointY >=4 %}
            {% set algorithm = bedMeshConfig.algorithm %}
        {% endif %}

        {% if enable_reference_index %}
            {% set referenceIndex = (meshPointX * meshPointY / 2 - 1 )|round(0)|int %}
            BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm} relative_reference_index={referenceIndex}
        {% else %}
            BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm}
        {% endif %}
    {% else %}
        BED_MESH_CALIBRATE_BASE
    {% endif %}
{% else %}
    BED_MESH_CALIBRATE_BASE
{% endif %}

``[gcode_macro START_PRINT]
variable_parameter_EXTRUDER_TEMP: 235
variable_parameter_BED_TEMP: 50
; gcode parameters for area bed mesh
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
gcode:
{% set BED_TEMP = params.BED_TEMP|default(50)|float %}
{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(235)|float %}
M104 S{EXTRUDER_TEMP} T0
M140 S{BED_TEMP}
G28
# preheat, homing, etc
BED_MESH_CALIBRATE AREA_START={params.AREA_START|default("0,0")} AREA_END={params.AREA_END|default("0,0")}
# the rest of your start macro here
M117 Waiting for temperature
# Start bed heating and continue
#M140 S{BED_TEMP}
{% if printer.heater_bed.temperature < params.BED_TEMP|float0.95 %}
M190 S{params.BED_TEMP|float
0.95} # wait till 0.85 of bed temp is reached, then continue
{% endif %}
#M140 S{EXTRUDER_TEMP}
#M190 S{BED_TEMP}
#M109 S{EXTRUDER_TEMP}
M190 S{BED_TEMP}
M109 S{EXTRUDER_TEMP} T0
PRIME_LINE
M117 Printing...

[gcode_macro END_PRINT]
gcode:
M117 Done printing :)
# move z down
G91
G1 Z10 F3000
G1 E-5
G28 X Y
#disable hotend and heated bed
M104 S0
M140 S0
# disable steppers
M84
BED_MESH_CLEAR

@ChipCE
Copy link
Author

ChipCE commented Jan 27, 2023

@Lekkz the mesh is completed so your macro setting is good.
Here is the problem: Unlike full size bed mesh witch the last probe position always in the bed corner, bed mesh area more likely end at center of the bed where the printed part are. You have to modify your PRIME_LINE, move the toolhead to the prime line start position before calling the extrude gcodes.

@Lekkz
Copy link

Lekkz commented Jan 28, 2023

@ChipCE thanks for the response, this is what I have for my PRIME_LINE [gcode_macro PRIME_LINE] gcode: M117 Prime Line G92 E0 ;Reset Extruder # move z axis G1 Z2.0 F3000 ;Move Z Axis up # move to prime position G1 X10 Y30 Z0.28 F5000.0 ;Move to start position G1 X10 Y200.0 Z0.28 F1500.0 E15 ;Draw the first line G1 X15 Y200.0 Z0.28 F5000.0 ;Move to side a little G1 X15 Y50 Z0.28 F1500.0 E30 ;Draw the second line G92 E0 ;Reset Extruder G1 Z2.0 F3000 ;Move Z Axis up

@ChipCE
Copy link
Author

ChipCE commented Jan 29, 2023

@Lekkz I think add G90 command at the start of your prime line will fix that

@sgoph
Copy link

sgoph commented Feb 7, 2023

Can someone please explain what this section does:

{% set algorithm = "lagrange" %}
{% if "algorithm" in bedMeshConfig and meshPointX >=4 and meshPointY >=4 %}
{% set algorithm = bedMeshConfig.algorithm %}
{% endif %}

Is it setting the algorithm to lagrange if the mesh points are 4 or greater? Or.. Is it setting the algorithm to whatever is set in printer.cfg if the points are >=4?

If I wanted to increase the number of probe points calculated for any given print area, would I just increase the scale factor (ie 1.25, 1.5, 1.75)?

Thanks!

@ChipCE
Copy link
Author

ChipCE commented Feb 7, 2023

@sgoph It's not that well writen, but similar to this.

{% set algorithm = bedMeshConfig.algorithm %} {% if meshPointX <4 and meshPointY <4 %} {% set algorithm = "lagrange" %} {% endif %} Bicubic need 4 points or more so we will need to force change to lagrange if the meshPoints are under 4.

Yes. Increase the scale factor will do it.

@whi-tw
Copy link

whi-tw commented Feb 9, 2023

I'm working on a macro which enables per-object meshes if:

  1. 'Label Objects' is turned on in the slicer
  2. moonraker is set up with:
    [file_manager]
    enable_object_processing: True
    
  3. klipper is configured with [exclude_object]

This will make a single small mesh for every item on the bed and load it dynamically when the object starts printing:

https://github.com/whi-tw/klipper-adaptive-mesh/releases/tag/v0.0.2 (adaptive_bed_mesh.cfg in the Assets section)

This has the benefit of the meshes being very detailed, as they are only made on the area of each object - no wasted space.

If anyone wants to give it a go, please leave feedback on the repo with any issues.

@rstudner
Copy link

Minor help request from y'all experts... for some reason I can't seem to get this right.

My Ender bed isn't very flat (hah!)

So if I have it calculate a mesh for a smaller object in the center of the bed, when it moves fromt he post-purge line to the center, it'll scratch/drag my nozzle on the way over.

Trying to get it to move up 1mm, navigate to 'skirt/brim/whatever' start ,then lower back down 1mm

@ChipCE
Copy link
Author

ChipCE commented Feb 14, 2023

@rstudner This macro cannot be changed to fix your problem. But modify your start gcode can fix that. Post your start gcode here and i will try to look at it.

@andrewt27
Copy link

andrewt27 commented Feb 19, 2023

@ChipCE Hi, I have a fairly large distance between the nozzle and the sensor (57mm). With large print I get errors like this
!! Move out of range: -27.016 86.484 5.000 [0.000]
How to compensate it?

Edit:
[SOLVED] I have changed x min in config:
[bed_mesh]
...
mesh_min: 57, 10
...

@kllxdababy
Copy link

22:13
Internal error on command:"PRINT_START"
22:13
Internal error on command:"BED_MESH_CALIBRATE"
22:13
Internal error on command:"BED_MESH_CALIBRATE_BASE"

Getting this error after my voron does QGL and homes.

Here is my print start macro:
[gcode_macro PRINT_START]
variable_parameter_EXTRUDER_TEMP: 190
variable_parameter_BED_TEMP: 30

gcode parameters for area bed mesh

variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
gcode:
G32 ; home all axes
G90 ; absolute positioning
G1 Z20 F3000 ; move nozzle away from bed
BED_MESH_CALIBRATE AREA_START={params.AREA_START|default("0,0")} AREA_END={params.AREA_END|default("0,0")}

@Zephyrus67
Copy link

Where exactly did you get %MINX% etc. from in Cura slicer? in my Cura that does not pass anything but the %MINX% and similar.

I am already using the "START_PRINT BED_TEMP=110 EXTRUDER_TEMP=255" part but the definition of area does not seem to work

@Zephyrus67
Copy link

Sorry. I didnt see the part where the plugin is mentioned...got it working now.

@Zephyrus67
Copy link

Zephyrus67 commented Mar 3, 2023

But will you not need a "BED_MESH_PROFILE Load=default" in your printer.cfg to actually load the saved mesh? With normal BED_MESH_CALIBRATE you need to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment