[include KNCM_settings.cfg]

[skew_correction]

#[probe]
#pin:^PA11
#x_offset: -24.25
#y_offset: 20.45
#z_offset = 0.0
#speed: 6.0
#samples: 3
#samples_result: median 
#sample_retract_dist: 3.0
#samples_tolerance: 0.025
#samples_tolerance_retries: 3

#[resonance_tester]
#accel_chip_x: adxl345 x
#accel_chip_y: adxl345 y
#max_smoothing: 0.15
#probe_points:
#    162.5, 162.5, 20

[stepper_z]
endstop_pin: probe:z_virtual_endstop
homing_retract_dist: 0

[mcu cartographer] # See https://www.klipper3d.org/Config_Reference.html#mcu
# Remove either the serial or canbus_uuid line dependant on your setup.
serial: /dev/serial/by-id/usb-Cartographer_614e_0A0002000643303459323220-if00 
#canbus_uuid: <blank>
#restart_method: command # Not needed for probes using CAN. 

[cartographer]
mcu: cartographer
x_offset: 0 # This is the offset of your probe from the nozzle tip, to the centre of the coil.
y_offset: 22 # This is the offset of your probe from the nozzle tip, to the centre of the coil.
verbose: no # For extra output

[bed_mesh]
zero_reference_position: 165, 165 # The centre of your bed. 
speed: 300
horizontal_move_z: 3
mesh_min: 10, 21 # The first probed coordinate, nearest to the origin. This coordinate is relative to the probe's location.
mesh_max: 300.75, 315.45 # The probed coordinate farthest from the origin. This is not necessarily the last point probed, as the probing process occurs in a zig-zag fashion. As with mesh_min, this coordinate is relative to the probe's location.
probe_count: 20, 20
adaptive_margin: 10
mesh_pps: 0,0 # Disable interpolation - mesh is probably dense enough

[temperature_sensor cartographer]
sensor_type: temperature_mcu
sensor_mcu: cartographer
min_temp: 5
max_temp: 105

[adxl345]
# Select 1 of the cs_pin below depending on the probe you have. 
# Having the wrong one selected will cause an Endless Bootloop
# Cartographer V4 has int2 connected to the probes PA1 pin
cs_pin: cartographer:PA3 # For Cartographer V3
#cs_pin: cartographer:PA0 # For Cartographer V4
spi_bus: spi1

[resonance_tester]
accel_chip: adxl345
probe_points:
    165, 165, 20 # Fill out the blank with x and y coordinates where you want to run the resonance test.

#[scanner]
#serial: /dev/serial/by-id/usb-Cartographer_614e_0A0002000643303459323220-if00            
#   Offsets are measured from the centre of your coil, to the tip of your nozzle 
#   on a level axis. It is vital that this is accurate. 
#x_offset: 0                          
#    adjust for your cartographers offset from nozzle to middle of coil
#y_offset: 22                         
#    adjust for your cartographers offset from nozzle to middle of coil
##
#backlash_comp: 0.01346
#   Backlash compensation distance for removing Z backlash before measuring
#   the sensor response.
#sensor: cartographer
#    this must be set as cartographer unless using IDM etc.
#sensor_alt: carto
#    alternate name to call commands. CARTO_TOUCH etc      
#mesh_runs: 2
#    Number of passes to make during mesh scan.


[filament_switch_sensor filament_sensor]
pause_on_runout: True
#insert_gcode:
#    M117 Insert Detected
runout_gcode:
    {action_respond_info("FILAMENT SENSOR TRIGGERED")}
    M117 Runout Detected
##   The minimum amount of time in seconds to delay between events.
##   Events triggered during this time period will be silently
##   ignored. The default is 3 seconds.
event_delay: 5.0
##   The amount of time to delay, in seconds, between the pause command
##   dispatch and execution of the runout_gcode. It may be useful to
##   increase this delay if OctoPrint exhibits strange pause behavior.
##   Default is 0.5 seconds.
pause_delay: 1.5
switch_pin: PA12

[filament_motion_sensor filament_motion]
switch_pin: PC1
detection_length: 8.00 # Supports down to 2.88
extruder: extruder
pause_on_runout: False
##   The minimum amount of time in seconds to delay between events.
##   Events triggered during this time period will be silently
##   ignored. The default is 3 seconds.
event_delay: 5.0
##   The amount of time to delay, in seconds, between the pause command
##   dispatch and execution of the runout_gcode. It may be useful to
##   increase this delay if OctoPrint exhibits strange pause behavior.
##   Default is 0.5 seconds.
pause_delay: 2.0
runout_gcode:
    {action_respond_info("FILAMENT MOTION TRIGGERED")}
    M117 Runout Detected


## ---------------------------------------------------------------------------- ##
## PRINT_START MACRO                                                            ##
## ---------------------------------------------------------------------------- ##

## Start print gcode defined here. In slicer, use only the following lines for start gcode:
## in slicer, use only the following lines for start gcode:
## PrusaSlicer: ## PRINT_START EXTRUDER_TEMP=[first_layer_temperature] BED_TEMP=[first_layer_bed_temperature]
## OrcaSlicer: ## PRINT_START EXTRUDER_TEMP=[nozzle_temperature_initial_layer] BED_TEMP=[bed_temperature_initial_layer_single]
## SuperSlicer: ## PRINT_START EXTRUDER_TEMP={first_layer_temperature[initial_extruder]+extruder_temperature_offset[initial_extruder]} BED_TEMP=[first_layer_bed_temperature]
## Cura: ## PRINT_START EXTRUDER_TEMP={material_print_temperature_layer_0} BED_TEMP={material_bed_temperature_layer_0}

[gcode_macro PRINT_START_NOCCI]
gcode:
    #--- Setup & Reset ---
    M117 Setting up parameters...
    RESPOND MSG="Starting Print Sequence..."
    
    SET_GCODE_OFFSET Z=0                    ; ⭐ NEW: Reset Z-Offset for Cartographer
    G92 E0
    G90                                     ; Absolute positioning
    BED_MESH_CLEAR
    Frame_Light_ON
    Part_Light_ON
    
    #--- Home with Cartographer ---
    CG28                                    ; ⭐ CHANGE: G28 → CG28 (Open4Neptune smart home)
    CARTOGRAPHER_TOUCH_HOME                 ; ⭐ NEW: Precise Z0 measurement with Cartographer
    
    #--- Parameters ---
    {% set BED_TEMP = params.BED_TEMP|default(60)|float %}
    {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|float %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default("PLA")|upper %}
    
    #--- Pre-Heat ---
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET=160  ; No-drip pre-heat
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={BED_TEMP}
    
    #--- Wait for Bed & Mesh ---
    {% set MIN_TEMP = BED_TEMP - (BED_TEMP * 0.15) %}
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={MIN_TEMP}
    BED_MESH_CALIBRATE ADAPTIVE=1
    
    #--- Nozzle Clean (KNCM) ---
    CLEAN_NOZZLE FILAMENT_TYPE={FILAMENT_TYPE}
    
    #--- Final Heat ---
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET={EXTRUDER_TEMP}
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={EXTRUDER_TEMP-2} MAXIMUM={EXTRUDER_TEMP+2}   ; ⭐ TIGHTER: ±2°C
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={BED_TEMP-2} MAXIMUM={BED_TEMP+2}           ; ⭐ TIGHTER: ±2°C
    
    #--- Prime & Start ---
    SMART_PARK
    SET_FILAMENT_SENSOR SENSOR=filament_sensor ENABLE=1
    SET_FILAMENT_SENSOR SENSOR=filament_motion ENABLE=0
    LINE_PURGE
    G92 E0
    G1 Z2.0 F3000
    
    M117 Printer goes brrbrrr...:)
    RESPOND MSG="Print starting..."

## ---------------------------------------------------------------------------- ##
## PRINT_END MACRO                                                              ##
## ---------------------------------------------------------------------------- ##

## End print gcode defined here. In slicer, use only the following lines for start gcode:
## PrusaSlicer/OrcaSlicer/SuperSlicer: PRINT_END
## Cura: PRINT_END

#[gcode_macro PRINT_END_NOCCI]
#gcode:
#  M117 Turning off heaters and fans...					; broadcast message on LCD screen
#  RESPOND MSG="Shutdown heaters and fans..."			; broadcast message in console
#  M400													; wait for buffer to clear
#  TURN_OFF_HEATERS										; Turn off heaters
#  G92 E0												; reset extruder
#  G91													; relative positioning
#  G1 E-2 F2700											; retract a bit
#  G1 X5 Y5 F3000										; wipe out
#  G1 E-2 Z0.2 F1600										; retract and raise Z
#  M117 Presenting print...								; broadcast message on LCD screen
#  RESPOND MSG="Presenting print..."						; broadcast message in console
#  G1 Z10 F3000											; move nozzle up 10mm
#  G90													; absolute positioning
#  G1 X10 Y325											; park nozzle at rear, present print
#  M107													; turn off fan
#  #Frame_Light_OFF
#  Part_Light_OFF
#  # G1 Z20 F3000											; move nozzle up 20mm
#  # G0 X0 Y220 F3600										; park nozzle at rear
#  #SAVE_IF_SET											; SAVE_CONFIG if a mesh was probed in START_PRINT
#  M84													; disable motors
#  M117 Done... Have a nice day!!!						; broadcast message on LCD screen
#  RESPOND MSG="Done... Have a nice day!!!"				; broadcast message in console

#######################################################################
## Start Macro
#######################################################################

#######################################################################
## Clean Nozzle
#######################################################################
[gcode_macro CLEAN_NOZZLE_OLD]
gcode:
   {% set LOWERED_EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|int-30 %}
   {% set WIPES = params.WIPES|default(10)|int %}
   #Position the nozzle
   G90
   G0 X-10 Y20 Z10 F5000
   #Heat up nozzle to extruder temperature - 40°C
   {action_respond_info("Clean nozzle: Heating up to {}°C the do {} wipes.".format(
     (LOWERED_EXTRUDER_TEMP),
     (WIPES)
   ))}
   M109 S{LOWERED_EXTRUDER_TEMP}
   #Bring the nozzle down
   G0 Z4
   #Swing the nozzle
   {% for wipe in range(WIPES) %}
       G0 Y5 F3000
       G0 Y35 F3000
   {% endfor %}
   #Bring the nozzle up
   G0 X-10 Y20 Z10 F5000
   # Home to get the correct offset
#   G28

[gcode_macro CLEAN_NOZZLE_WHILE_PRINT]
gcode:
   {% set LOWERED_EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(240)|int-40 %}
   {% set WIPES = params.WIPES|default(10)|int %}
   #Position the nozzle
   G90
   G0 X-10 Y20 Z10 F5000
   #Heat up nozzle to extruder temperature - 40°C
   {action_respond_info("Clean nozzle: Heating up to {}°C the do {} wipes.".format(
     (LOWERED_EXTRUDER_TEMP),
     (WIPES)
   ))}
   M109 S{LOWERED_EXTRUDER_TEMP}
   #Bring the nozzle down
   G0 Z4
   #Swing the nozzle
   {% for wipe in range(WIPES) %}
       G0 Y5 F3000
       G0 Y35 F3000
   {% endfor %}
   #Bring the nozzle up
   G0 X-10 Y20 Z10 F5000
   # Home to get the correct offset
#   G28
#### CUSTOM END
#[gcode_macro update_git]
#gcode:
#    RUN_SHELL_COMMAND CMD=update_git_script

#[gcode_shell_command update_git_script]
#command: bash -c "bash $HOME/klipper-backup/script.sh"
#timeout: 90.0

[gcode_macro QUICK_TOUCH]
description: Führt eine schnelle Z-Messung per Touch durch
gcode:
    {% if printer.toolhead.homed_axes != "xyz" %}
        G28
    {% endif %}
    G1 Z10 F600
    CARTOGRAPHER_TOUCH


[gcode_macro CALIBRATE_PROBE]
description: Startet die manuelle Z-Offset Kalibrierung
gcode:
    {% if printer.toolhead.homed_axes != "xyz" %}
        G28                                    # Homed, falls noch nicht geschehen
    {% endif %}
    G1 X{printer.toolhead.axis_maximum.x / 2} Y{printer.toolhead.axis_maximum.y / 2} Z10 F6000
    CARTOGRAPHER_CALIBRATE


[controller_fan heatbreak+mainboard_fan]
fan_speed: 0.40
idle_speed: 0.40
cycle_time: 0.00004

[tmc2209 stepper_x]
driver_SGTHRS: 110

[safe_z_home]
home_xy_position: 165,165
# Example home_xy_position: 175,175 - This would be for a 350 * 350mm bed. 
z_hop: 10



#[temperature_sensor cartographer]
#sensor_type: temperature_mcu
#sensor_mcu: cartographer
#min_temp: 5
#max_temp: 105

[bed_mesh]
zero_reference_position: 165.00,165.00

[stepper_z]
endstop_pin: probe:z_virtual_endstop # use cartographer as virtual endstop
homing_retract_dist: 0 # cartographer needs this to be set to 0


[bed_mesh]
zero_reference_position: 162.5 ,162.5   
#    set this to the middle of your bed
speed: 200
#    movement speed of toolhead during bed mesh
horizontal_move_z: 5
#    height of scanner during bed mesh scan
mesh_min: 35, 6
#    start point of bed mesh [X, Y]
mesh_max: 240, 198
#    end point of bed mesh [X, Y]
probe_count: 30, 30
algorithm: bicubic

[gcode_macro PROBE_CALIBRATE]
gcode:
    CARTOGRAPHER_CALIBRATE


#[mcu scanner]

#serial: //dev/serial/by-id/usb-Cartographer_614e_0A0002000643303459323220-if00
#    adjust to suit your scanner, if using usb change to serial

#[temperature_sensor Cartographer_MCU]
#sensor_type: temperature_mcu
#sensor_mcu: scanner
#min_temp: 0
#max_temp: 105


[force_move]
enable_force_move: True

#[adxl345]
#cs_pin: scanner:PA3
#spi_bus: spi1

[resonance_tester]
accel_chip: adxl345
probe_points:
    165.2, 165.2, 20

[screws_tilt_adjust]
screw1: 165.0,189.55
screw1_name: middle-rear bed mount (shim adjust)
screw2: 165.0,99.55
screw2_name: middle-front bed mount (shim adjust)
screw3: 32.50,277.05         
screw3_name: rear left screw
screw4: 32.50,144.55        
screw4_name: center left screw
screw5: 32.50,12.05            
screw5_name: front left screw
screw6: 288.5,12.05
screw6_name: front right screw
screw7: 288.5,144.55
screw7_name: center right screw
screw8: 288.5,277.05
screw8_name: rear right screw
horizontal_move_z: 5
speed: 150
screw_thread: CW-M4

[gcode_macro BABYZ_PLUS]
#   Babystep Up .05
gcode:
    SET_GCODE_OFFSET Z_ADJUST=0.005 MOVE=1

[gcode_macro BABYZ_MINUS]
#   Babystep Down .05
gcode:
    SET_GCODE_OFFSET Z_ADJUST=-0.005 MOVE=1



#############################################################################
#   OLD Probe Config
#############################################################################

#[probe]
#pin:^PA11
#x_offset: -24.25
#y_offset: 20.45
#z_offset = 1.660
#speed: 10.0
#samples: 2
#samples_result: average 
#sample_retract_dist: 3.0
#samples_tolerance: 0.025
#samples_tolerance_retries: 3

[skew_correction noccis_skew_profile]
xy_skew = 0.00100050075747056
xz_skew = 0.0
yz_skew = 0.0
