# This file contains common pin mappings for MKS Robin Nano (v1.2.004)
# boards.
# When running "make menuconfig" you have to:
# - enable "extra low-level configuration setup",
# - select the 28KiB bootloader,
# - select serial (on USART3 PB11/PB10) communication
# - set "GPIO pins to set at micro-controller startup" to "!PC6,!PD13"

# Note that the "make flash" command does not work with the Sapphire
# Pro. After running "make", run the following command:
#   ./scripts/update_mks_robin.py out/klipper.bin out/Robin_nano35.bin
# Copy the file 	 to an SD card and then restart the
# printer with that SD card.

# See docs/Config_Reference.md for a description of parameters.

######################################################
#################   Wanhao D12-300   #################
######################################################

[mcu]
serial: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0 #By default /dev/ttyUSB0
restart_method: command

#[mcu pico]
#serial: /dev/serial/by-id/usb-Klipper_rp2040_E6616407E350BD26-if00

#[adxl345]
#spi_bus: spi0a
#cs_pin: pico:gpio1

#[resonance_tester]
#accel_chip: adxl345
#probe_points:
#    115,115,20 # center of the bed, 20mm de hauteur

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

[input_shaper]
shaper_freq_x: 84.0
shaper_type_x: mzv #suggested max_accel <= 20800 mm/sec^2
shaper_freq_y: 42.4
shaper_type_y: mzv #suggested max_accel <= 5300 mm/sec^2

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

[stepper_x]
step_pin: PE3
dir_pin: PE2
enable_pin: !PE4
microsteps: 16
#M92 X80.97 Y80.63 Z402.41
rotation_distance: 39.52081017660862
endstop_pin: !PA15
position_endstop: 0
position_max: 230
homing_speed: 50

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

[stepper_y]
step_pin: PE0
dir_pin: !PB9
enable_pin: !PE1
microsteps: 16
#M92 X80.97 Y80.63 Z402.41
rotation_distance: 39.68746124271363
endstop_pin: !PA12
position_endstop: 0
position_max: 230
homing_speed: 50

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

[stepper_z]
step_pin: PB5
dir_pin: !PB4
enable_pin: !PB8
microsteps: 16
#M92 X80.97 Y80.63 Z402.41
rotation_distance: 7.952088665788623
#endstop_pin: !PA11  # Uncomment if NO BLtouch used
endstop_pin:probe:z_virtual_endstop
#position_endstop: 0 # Uncomment if NO BLtouch used
position_min: -2.0
position_max: 210
homing_speed = 10
homing_retract_dist = 5
second_homing_speed = 3

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

[extruder]
step_pin: PD6
dir_pin: !PD3
enable_pin: !PB3
#M92 T0 E407.50
microsteps: 16
rotation_distance: 4.67
full_steps_per_rotation: 200
max_extrude_only_velocity: 60
#rotation_distance: 7.852760736196319
min_extrude_temp: 150 # The minimum temperature (in Celsius) at which extruder move
nozzle_diameter: 0.400
filament_diameter: 1.750
heater_pin: PC3
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC1
min_temp: 5
max_temp: 250

#pressure_advance: 0 # For Input Shaper test
#pressure_advance: 0.060
#pressure_advance_smooth_time: 0.040

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

# Secondary extruder
#[extruder1]
#step_pin: PA6
#dir_pin: !PA1
#enable_pin: !PA3
#M92 T1 E417.81
#microsteps: 16
#rotation_distance: 7.658983748593858
#nozzle_diameter: 0.400
#filament_diameter: 1.750
#heater_pin: PC3
#sensor_type: EPCOS 100K B57560G104F
#sensor_pin: PC1
#min_temp: 5
#max_temp: 250

#pressure_advance: 0 # For Input Shaper test
#pressure_advance: 0.060
#pressure_advance_smooth_time: 0.040

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

[heater_bed]
heater_pin: PA0
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC0
min_temp: 5
max_temp: 100

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

[fan]
pin: PB1

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

[printer]
kinematics: cartesian
max_velocity: 300
max_accel: 5000
max_accel_to_decel: 4000
max_z_velocity: 10
max_z_accel: 100

###################################
 
[bltouch]
sensor_pin: ^PA11
control_pin: PA8
#M851 X-45.50 Y-5.00 Z-0.20
x_offset: -37.9
y_offset: -0.47
#x_offset: -43.0 #tête d'origine
#y_offset: -6.5  #tête d'origine
speed: 10.0
pin_move_time: 0.675
#   The amount of time (in seconds) to wait for the BLTouch pin to
#   move up or down. The default is 0.675 seconds.
lift_speed: 20 #80.0
#   Speed (in mm/s) of the Z axis when lifting the probe between
#   samples. The default is to use the same value as the 'speed'
#   parameter.
samples: 3
#   The number of times to probe each point. The probed z-values will
#   be averaged. The default is to probe 1 time.
samples_result: median
#   The calculation method when sampling more than once - either
#   "median" or "average". The default is average.
samples_tolerance: 0.015
#   The maximum Z distance (in mm) that a sample may differ from other
#   samples. If this tolerance is exceeded then either an error is
#   reported or the attempt is restarted (see
#   samples_tolerance_retries). The default is 0.100mm.
samples_tolerance_retries: 8
#   The number of times to retry if a sample is found that exceeds
#   samples_tolerance. On a retry, all current samples are discarded
#   and the probe attempt is restarted. If a valid set of samples are
#   not obtained in the given number of retries then an error is
#   reported. The default is zero which causes an error to be reported
#   on the first sample that exceeds samples_tolerance.
# set this to false if probe does not support it
stow_on_each_sample: False
#   If set to False, intermediate STOW/DEPLOY sequences on certain
#   operations that use the probe multiple times will be omitted.
sample_retract_dist: 0.8
#   The distance (in mm) to lift the toolhead between each sample (if
#   sampling more than once). The default is 2mm.
 
[bed_mesh]
speed: 180
horizontal_move_z: 8
mesh_min: 10,10
mesh_max: 177,213
probe_count: 5,5
fade_start: 1.0
fade_end: 10
mesh_pps: 2,2
algorithm = bicubic

[safe_z_home]
home_xy_position: 115,115
speed: 100.0
z_hop: 10.0
z_hop_speed: 10.0

########################
## Additional sensors ##
########################

[idle_timeout]
# A list of G-Code commands to execute on an idle timeout. See
# docs/Command_Templates.md for G-Code format. The default is to run
# "TURN_OFF_HEATERS" and "M84".
gcode:
  {% if printer.pause_resume.is_paused %} #Case of manual pause or filament runout
    M104 S0
  {% else %} #Every other idle reasons
    TURN_OFF_HEATERS
    M84
  {% endif %}
# Idle time (in seconds) to wait before running the above G-Code
# commands. The default is 600 seconds.
  timeout: 10 #Want the extruder to turn off fast
  
[pause_resume]
recover_velocity: 60

[filament_switch_sensor E0]
switch_pin: PA4
pause_on_runout: false #pause handled by macro
runout_gcode:
    M118 Filament Runout Detected
    M600
insert_gcode:
    M118 Filament Load Detected
#event_delay: 3.0
#pause_delay: 0.5

[filament_switch_sensor E1]
switch_pin: PE6
pause_on_runout: false #pause handled by macro
runout_gcode:
    M118 Filament Runout Detected
    M600
insert_gcode:
    M118 Filament Load Detected
#event_delay: 3.0
#pause_delay: 0.5

################
###  Macros  ###
################

[gcode_macro G29]
gcode:
 G28
 BED_MESH_CALIBRATE
 #SAVE_CONFIG
 G1 X0 Y0 Z5 F4000

[gcode_macro M600]
gcode:
    {% set X = params.X|default(50)|float %}
    {% set Y = params.Y|default(0)|float %}
    {% set Z = params.Z|default(10)|float %}
    SAVE_GCODE_STATE NAME=M600_state
    PAUSE
    G91
    G1 E-.8 F2700
    G1 Z{Z}
    G90
    G1 X{X} Y{Y} F3000
    G91
    G1 E-50 F1000
    RESTORE_GCODE_STATE NAME=M600_state

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: PAUSE_BASE
# change this if you need more or less extrusion
variable_extrude: 1.0
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  ##### set park positon for x and y #####
  # default is your max posion from your printer.cfg
  {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
  {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
  ##### calculate save lift position #####
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% if act_z < (max_z - 2.0) %}
      {% set z_safe = 2.0 %}
  {% else %}
      {% set z_safe = max_z - act_z %}
  {% endif %}
  ##### end of definitions #####
  PAUSE_BASE
  G91
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G1 E-{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    G1 Z{z_safe} F900
    G90
    G1 X{x_park} Y{y_park} F6000
  {% else %}
    {action_respond_info("Printer not homed")}
  {% endif %} 

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: RESUME_BASE
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  #### get VELOCITY parameter if specified ####
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  ##### end of definitions #####
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G91
    G1 E{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}  
  RESUME_BASE {get_params}

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: CANCEL_PRINT_BASE
gcode:
  TURN_OFF_HEATERS
  CANCEL_PRINT_BASE

######################################################################
# Filament Change
######################################################################

# M600: Filament Change. This macro will pause the printer, move the
# tool to the change position, and retract the filament 50mm. Adjust
# the retraction settings for your own extruder. After filament has
# been changed, the print can be resumed from its previous position
# with the "RESUME" gcode.

[gcode_macro M600]
gcode:
    {% set X = params.X|default(50)|float %}
    {% set Y = params.Y|default(0)|float %}
    {% set Z = params.Z|default(10)|float %}
    SAVE_GCODE_STATE NAME=M600_state
    PAUSE
    G91
    G1 E-.8 F2700
    G1 Z{Z}
    G90
    G1 X{X} Y{Y} F3000
    G91
    G1 E-50 F1000
    RESTORE_GCODE_STATE NAME=M600_state

[screws_tilt_adjust]
# This adds a screw tilt adjust command that probes certain points on the bed and recommends new bed screw positions (turn clockwise this much etc)
screw1: 55,55
screw1_name: Front left screw
screw2: 55,255
screw2_name: Rear left screw
screw3: 255,55
screw3_name: Front right screw
screw4: 255,255
screw4_name: Rear right screw
horizontal_move_z: 10
speed: 200
screw_thread: CW-M3

[bed_screws]
# This adds a bed screws adjust GCode, that moves the nozzle around for you so you can paper adjust.
screw1: 20,20
screw1_name: Front left screw
screw2: 20,290
screw2_name: Rear left screw
screw3: 290,290
screw3_name: Rear right screw
screw4: 290,20
screw4_name: Front right screw

#########################
## Requested by Fluidd ##
#########################

[virtual_sdcard]
path: ~/gcode_files

[display_status]

#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [heater_bed]
#*# control = pid
#*# pid_kp = 70.560
#*# pid_ki = 1.048
#*# pid_kd = 1188.052
#*#
#*# [extruder]
#*# control = pid
#*# pid_kp = 25.096
#*# pid_ki = 2.231
#*# pid_kd = 70.583
#*#
#*# [input_shaper]
#*#
#*# [bed_mesh default]
#*# version = 1
#*# points =
#*# 	  -0.278323, -0.370269, -0.285778, -0.231108, -0.288263
#*# 	  -0.136677, -0.139162, -0.054671, -0.074551, -0.141647
#*# 	  -0.009940, 0.029820, -0.029820, -0.037275, -0.047216
#*# 	  0.024850, -0.024850, 0.007455, 0.009940, -0.067096
#*# 	  -0.114311, 0.012425, -0.029820, 0.002485, -0.129221
#*# tension = 0.2
#*# min_x = 10.0
#*# algo = bicubic
#*# y_count = 5
#*# mesh_y_pps = 2
#*# min_y = 10.0
#*# x_count = 5
#*# max_y = 213.0
#*# mesh_x_pps = 2
#*# max_x = 177.0
#*#
#*# [bltouch]
#*# z_offset = 3.591