# This file contains common pin mappings for ELEGOO
# boards. To use this config, the firmware should be compiled for the
# stm32f407. When running "make menuconfig", select the 48KiB
# bootloader, and enable "Serial for communication" and select the "on USART1 PA10/PA9"
# See docs/Config_Reference.md for a description of parameters.

[include plr.cfg]

[mcu]
# The hardware use USART1 PA10/PA9 connect to RK3328
serial: /dev/ttyS0
restart_method: command

[stepper_x]
step_pin:PC14
dir_pin:PC13
enable_pin:!PC15
microsteps: 16
rotation_distance: 40
full_steps_per_rotation:200  #set to 400 for 0.9 degree stepper
endstop_pin:PC0
position_min: -8  
position_endstop:-8
position_max: 235
homing_speed:50
homing_retract_dist:5
homing_positive_dir:false
step_pulse_duration:0.000002

[stepper_y]
step_pin:PB4
dir_pin:PB3
enable_pin:!PB5
microsteps:16
rotation_distance: 40
full_steps_per_rotation:200  #set to 400 for 0.9 degree stepper
endstop_pin:PB8
position_min: 0
position_endstop:0 
position_max:235
homing_speed:50
homing_retract_dist:5
homing_positive_dir:false
step_pulse_duration:0.000002


[stepper_z]
step_pin:PC10
dir_pin:!PA15
enable_pin: !PC11
microsteps: 16
rotation_distance: 8
full_steps_per_rotation: 200
endstop_pin:probe:z_virtual_endstop ## PB12 for Z-max; endstop have'!' is NO

##  Z-position of nozzle (in mm) to z-endstop trigger point relative to print surface (Z0)
##  (+) value = endstop above Z0, (-) value = endstop below
##	Increasing position_endstop brings nozzle closer to the bed
##  After you run Z_ENDSTOP_CALIBRATE, position_endstop will be stored at the very end of your config
#position_endstop:-5
position_max: 268
position_min: -5 
homing_speed: 8
second_homing_speed: 3

[extruder]
step_pin:PA5
dir_pin:!PA6
enable_pin:!PA4
microsteps:16
rotation_distance: 31.4	#Bondtech 5mm Drive Gears
gear_ratio: 52:10
full_steps_per_rotation: 200	#200 for 1.8 degree, 400 for 0.9 degree
nozzle_diameter: 0.400
filament_diameter: 1.750
min_temp: 0
max_temp: 330
heater_pin: PA7
sensor_type:NTC 100K MGB18-104F39050L32
sensor_pin: PA1
max_power: 1
control :pid
pid_kp : 26.213 
pid_ki : 1.304
pid_kd : 131.721 
pressure_advance: 0.022
pressure_advance_smooth_time: 0.05
instantaneous_corner_velocity: 10.000
max_extrude_only_distance: 150
max_extrude_only_velocity:5000
max_extrude_only_accel:2000
step_pulse_duration:0.000002
max_extrude_cross_section: 50 

[verify_heater extruder]
max_error: 120
check_gain_time:120
hysteresis: 10
heating_gain: 2

[heater_bed]
heater_pin:PB10
sensor_type: NTC 100K MGB18-104F39050L32
sensor_pin: PA0
max_power: 1.0
control = watermark
min_temp: 0
max_temp: 200


[verify_heater heater_bed]
max_error: 120
check_gain_time:120
hysteresis: 10
heating_gain: 1


#fan for printed model FAN0
[fan]
pin: PC9

#fan for hotend FAN1
[heater_fan fan1]
pin: PA8
shutdown_speed: 1

[printer]
kinematics:cartesian
max_velocity: 500
max_accel: 5000
max_accel_to_decel: 4000
max_z_velocity: 20
max_z_accel: 200
square_corner_velocity: 5.0

[input_shaper]
shaper_freq_x: 66.66    
shaper_freq_y: 42.05   
shaper_type_x: mzv
shaper_type_y: ei

####################################################################
#	Homing and Gantry Adjustment Routines
#####################################################################
[idle_timeout]
timeout: 36000

[safe_z_home]
home_xy_position: 141,98 
speed: 100
z_hop: 10                 
z_hop_speed: 5


[gcode_macro G29]
gcode:
      BED_MESH_CLEAR
      G28
      BED_MESH_CALIBRATE
      G0 X117 Y126 Z5 F3000
      G0 Z0 F300

#####################################################################
# 	Probe
#####################################################################

[probe]
pin:^PA11
x_offset: -24.25
y_offset: 20.45
z_offset: 0.0
speed: 10.0
samples: 2
samples_result: median
sample_retract_dist: 3.0
samples_tolerance: 0.1
samples_tolerance_retries: 1

[bed_mesh]
speed:120                
horizontal_move_z:10     
mesh_min:10,21           
mesh_max:210,211         
probe_count:11,11          
algorithm:bicubic
bicubic_tension:0.2
mesh_pps: 4, 4 
fade_start: 1.0
fade_end: 10.0   


#####################################################################
# LED Control
#####################################################################
[output_pin caselight]
pin: PB7
pwm: false
shutdown_value:0
value:0.0

[output_pin caselight1]
pin: PC7
pwm: false
shutdown_value:0
value:0.0

[gcode_macro FLASHLIGHT_ON]
description: Turn on Hotend LEDs
gcode:
  SET_PIN PIN=caselight VALUE=1


[gcode_macro FLASHLIGHT_OFF]
description: Turn off Hotend LEDs
gcode:
  SET_PIN PIN=caselight VALUE=0


[gcode_macro MODLELIGHT_ON]
description: Turn on Logo LEDs
gcode:
  SET_PIN PIN=caselight1 VALUE=1


[gcode_macro MODLELIGHT_OFF]
description: Turn off Logo LEDs
gcode:
  SET_PIN PIN=caselight1 VALUE=0

[filament_switch_sensor fila]
pause_on_runout: True
runout_gcode:
            SET_FILAMENT_SENSOR SENSOR=fila ENABLE=1
event_delay: 3.0
pause_delay: 0.5
switch_pin: PA12

########################################
# TMC UART configuration
########################################

[tmc2209 stepper_x]
uart_pin: PB9
run_current: 1.2
hold_current: 0.8
interpolate: True
stealthchop_threshold:99999

[tmc2209 stepper_y]
uart_pin: PD2
run_current: 1.2
hold_current: 0.8
interpolate: True
stealthchop_threshold:99999

[tmc2209 stepper_z]
uart_pin: PC5
run_current: 0.8
hold_current: 0.8
interpolate: True
stealthchop_threshold: 120

[tmc2209 extruder]
uart_pin: PC4
run_current: 0.9
hold_current: 0.5
interpolate: True
stealthchop_threshold: 400

[mcu rpi]
serial: /tmp/klipper_host_mcu

[adxl345]
cs_pin: rpi:None
spi_bus: spidev0.2

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


[force_move]
enable_force_move : true

[virtual_sdcard]
path: ~/gcode_files


[gcode_macro PRINT_START]         
gcode:
    SAVE_VARIABLE VARIABLE=was_interrupted VALUE=True
	  G92 E0
    BED_MESH_CLEAR                                           
	  G90             
    BED_MESH_PROFILE LOAD=default   
    CLEAR_PAUSE
    M117 Printing                   
    
[gcode_macro PRINT_END]
gcode:
    SAVE_VARIABLE VARIABLE=was_interrupted VALUE=False
    RUN_SHELL_COMMAND CMD=clear_plr
    clear_last_file
    {% set RUN_VELOCITY = printer.configfile.settings['printer'].max_velocity|float %}
    {% set RUN_ACCEL    = printer.configfile.settings['printer'].max_accel|float %}
    {% set RUN_DECEL    = printer.configfile.settings['printer'].max_accel_to_decel|float %}
    SET_VELOCITY_LIMIT VELOCITY={RUN_VELOCITY} ACCEL={RUN_ACCEL} ACCEL_TO_DECEL={RUN_DECEL}

[pause_resume]


[gcode_macro CANCEL_PRINT]
rename_existing: BASE_CANCEL_PRINT
gcode:
    PRINT_END
    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout} 
    SDCARD_RESET_FILE
    M400 							            ; wait for buffer to clear
    G92 E0 							          ; zero the extruder
    G1 E-10.0 F1200 				      ; retract filament
    G91 							            ; relative positioning
    TURN_OFF_HEATERS
    M107 							            ; turn off fan
    G1 Z2 F3000 					        ; move nozzle up 2mm
    G90 							            ; absolute positioning
    G0 X0 Y225 F3600 				      ; park nozzle at rear
    M84


[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
gcode:
    M400 
    {% set z = params.Z|default(10)|int %}                                                   
    {% set e = params.E|default(2.5) %} 
    SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE={z}                             
    SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=etemp VALUE={printer['extruder'].target}                                              
    SAVE_GCODE_STATE NAME=PAUSE                                                                  
    M25                                                                              
    {% if (printer.gcode_move.position.z + z) < printer.toolhead.axis_maximum.z %}       
      G91
	    M83
	    G1 E-{e} F2100
      G1 Z{z} F900                                                                     
    {% else %}
      SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE=0
    {% endif %}
    SAVE_GCODE_STATE NAME=PAUSEPARK
    G90                                                                                  
    G1 X0 Y220 F6000
	  G1 E{e} F2100	
    SET_IDLE_TIMEOUT TIMEOUT=86400                                                       


[gcode_macro RESUME]
rename_existing: BASE_RESUME
variable_zhop: 0
variable_etemp: 0
gcode:
    {% set e = params.E|default(2.5)|int %}                                          
    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}
    G91                                                                               
    M83
	  G1 E-{e} F900		
    RESTORE_GCODE_STATE NAME=PAUSEPARK MOVE=1 MOVE_SPEED=100                     
    {% if printer[printer.toolhead.extruder].temperature >= printer.configfile.settings.extruder.min_extrude_temp %}                                                
      G1 Z{zhop * -1} F900
	    G1 E{e+0.5} F900	  
    {% else %}                      
      G1 Z{zhop * -1} F900                                                     
    {% endif %}
    M400
    RESTORE_GCODE_STATE NAME=PAUSE MOVE=1 MOVE_SPEED=60 
    M24
 

[gcode_macro M109]
rename_existing: M99109
gcode:
    #Parameters
    {% set s = params.S|float %}
    M104 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %}  
    {% if s != 0 %}
        TEMPERATURE_WAIT SENSOR=extruder MINIMUM={s-4} MAXIMUM={s+20}   
    {% endif %}
    
   
[display_status]


[gcode_macro M84]
rename_existing:M84.1
gcode:
      SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
      SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
      SET_STEPPER_ENABLE STEPPER=stepper_z enable=0
      SET_STEPPER_ENABLE STEPPER=extruder  enable=0
      SET_KINEMATIC_POSITION

[delayed_gcode KINEMATIC_POSITION]
initial_duration:0.2
gcode:
      SET_KINEMATIC_POSITION X=110
      SET_KINEMATIC_POSITION Y=110
      SET_KINEMATIC_POSITION Z=0

[gcode_macro M205]
description: Sets square corner velocity.
  Usage: M205 [X<velocity>] [Y<velocity>]
gcode:
  {% if 'X' in params or 'Y' in params %}
    SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY="{
      (params.X|default(0)|float, params.Y|default(0)|float)|min}"
  {% else %}
    SET_VELOCITY_LIMIT
  {% endif %}


[gcode_macro M203]
description: Sets maximum velocity.
  Usage: M203 [X<velocity>] [Y<velocity>]
gcode:
  {% if 'X' in params or 'Y' in params %}
    {% set velocity = (params.X|default(params.Y)|float,
                       params.Y|default(params.X)|float)|min %}
    SET_VELOCITY_LIMIT VELOCITY="{velocity}"
  {% else %}
    SET_VELOCITY_LIMIT
  {% endif %}
  
[gcode_arcs]



####################################################################
#	分区加热相关配置
#####################################################################
[heater_generic heater_bed1]
gcode_id:M105
#使用M105查询温度时使用的ID。
#必须提供此参数。
heater_pin:PC8
max_power:1.0
sensor_type: NTC 100K MGB18-104F39050L32
sensor_pin:PC2
control = watermark
#control = pid
#pid_Kp  = 70.591 
#pid_Ki  = 1.055 
#pid_Kd  = 1180.632
min_temp:0
max_temp:200

[verify_heater heater_bed1]    # 加热块温度容差配置
max_error: 600                 # 最大误差
check_gain_time:120            # 容差时间
hysteresis: 10                 # 容差温度
heating_gain: 1                # 加热增益

[gcode_macro M191]
gcode:
      {% set s = params.S|float %}
      {% if params.S is defined %}
        SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={params.S|int}
        SET_HEATER_TEMPERATURE HEATER=heater_bed1 TARGET={params.S|int}
        TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={s-4} MAXIMUM={s+10} 
      {% endif %}
	  
[gcode_macro M190]
rename_existing: M99190
gcode:
       {% set s = params.S|float %}
       {% if params.S is defined %}
        SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={params.S|int}
        SET_HEATER_TEMPERATURE HEATER=heater_bed1 TARGET={params.S|int}
        TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={s-4} MAXIMUM={s+10}
       {% endif %}  
	   
[gcode_macro M140]
rename_existing: M99140
gcode:
       {% set s = params.S|float %}
       {% if params.S is defined %}
        SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={params.S|int}
        SET_HEATER_TEMPERATURE HEATER=heater_bed1 TARGET={params.S|int}
       {% endif %} 

[gcode_macro M141]
gcode:
       {% set s = params.S|float %}
       {% if params.S is defined %}
        SET_HEATER_TEMPERATURE HEATER=heater_bed1 TARGET={params.S|int}
       {% endif %} 
###########################分区加热---END---#########################################







