[include mainsail.cfg]

[mcu]
##	Obtain mcu value by "ls -l /dev/serial/by-id/" 
serial: /dev/serial/by-id/usb-Klipper_rp2040_E16044B34B343525-if00
#serial: /dev/ttyAMA0
restart_method: command

[stepper_x]
step_pin: gpio3
dir_pin: !gpio2
enable_pin: !gpio4
rotation_distance: 40
microsteps: 16
endstop_pin: ^!gpio11
# endstop_pin: tmc2209_stepper_x:virtual_endstop
# 
position_endstop: 120
position_max: 120
homing_speed: 50
homing_retract_dist: 5
homing_positive_dir: true

[tmc2209 stepper_x]
uart_pin: gpio9
tx_pin: gpio8
# MS2/AD1=0,MS1/AD0=1
uart_address: 1   
interpolate: True
run_current: 0.8
hold_current: 0.6
sense_resistor: 0.220
stealthchop_threshold: 999999
# diag_pin: ^gpio11
# driver_SGTHRS: 100

[stepper_y]
step_pin: gpio16
dir_pin: !gpio15
enable_pin: !gpio14
rotation_distance: 40
microsteps: 16
endstop_pin: ^!gpio12
# endstop_pin: tmc2209_stepper_y:virtual_endstop
position_endstop: 120
position_max: 120
homing_speed: 50
homing_retract_dist: 5

[tmc2209 stepper_y]
uart_pin: gpio9
tx_pin: gpio8
# MS2/AD1=1,MS1/AD0=0
uart_address: 2
run_current: 0.8
hold_current: 0.6
sense_resistor: 0.220
stealthchop_threshold: 999999
#diag_pin: ^gpio12
#driver_SGTHRS: 100

[stepper_z]
step_pin: gpio19
dir_pin: !gpio18
enable_pin: !gpio17
rotation_distance: 8 #for T8x8 lead screw
microsteps: 16
rotation_distance: 8
endstop_pin: ^!gpio13
position_endstop: 0
#endstop_pin: probe:z_virtual_endstop
position_min: -1.0
position_max: 250
homing_speed: 12
second_homing_speed: 3.0
homing_retract_dist: 3.0

[tmc2209 stepper_z]
uart_pin: gpio9
tx_pin: gpio8
# MS2/AD1=1,MS1/AD0=1
uart_address: 3
run_current: 0.580
hold_current: 0.500
sense_resistor: 0.220
stealthchop_threshold: 999999

[extruder]
step_pin: gpio6
dir_pin: !gpio5
enable_pin: !gpio7
full_steps_per_rotation: 200    # 1.8 degree motor
rotation_distance: 22.23        # See calibrating rotation_distance on extruders doc
gear_ratio: 50:10               # For Mini Afterburner
microsteps: 16
rotation_distance: 33.500
nozzle_diameter: 0.4
filament_diameter: 1.75
heater_pin: gpio24
sensor_type: EPCOS 100K B57560G104F
sensor_pin: gpio28
control: pid
pid_Kp: 22.2
pid_Ki: 1.08
pid_Kd: 114
min_temp: 0
max_temp: 300
min_extrude_temp: 170
max_extrude_only_distance: 780.0
max_extrude_cross_section:2
pressure_advance: 0.0   # See tuning pressure advance doc
pressure_advance_smooth_time: 0.040

[tmc2209 extruder]
uart_pin: gpio9
tx_pin: gpio8
# MS2/AD1=0,MS1/AD0=0
uart_address: 0
interpolate: True
run_current: 0.650
hold_current: 0.500
sense_resistor: 0.220
stealthchop_threshold: 999999

[filament_switch_sensor runout_sensor]
switch_pin: ^gpio10
pause_on_runout: True

# [filament_motion_sensor smart_sensor]
# switch_pin: ^gpio10
# detection_length: 2.5

[heater_bed]
heater_pin: gpio23
sensor_type: EPCOS 100K B57560G104F
sensor_pin: gpio26
control: pid
pid_Kp: 54.027
pid_Ki: 0.770
pid_Kd: 948.182
min_temp: 0
max_temp: 130

[printer]
kinematics: corexy
max_velocity: 500
max_accel: 3000
max_z_velocity: 25
max_z_accel: 30
square_corner_velocity: 6.0

[heater_fan hotend_fan]
pin: gpio21
heater: extruder
heater_temp: 50.0

[fan]
pin: gpio20

#[heater_fan controller_fan]
#pin: gpio22
#heater: heater_bed
#heater_temp: 45.0

[temperature_sensor chamber]
sensor_type: EPCOS 100K B57560G104F
sensor_pin: gpio27
#min_temp:
#max_temp:

#[probe]
##	Inductive Probe - If you use this section , please comment the [bltouch] section
##	This probe is not used for Z height, only Quad Gantry Leveling
##	In Z+ position
##	If your probe is NC instead of NO, add change pin to ^PA3
#pin: ^gpio25
#x_offset: 0
#y_offset: 25.0
#z_offset: 0
#speed: 10.0
#samples: 3
#samples_result: median
#sample_retract_dist: 3.0
#samples_tolerance: 0.006
#samples_tolerance_retries: 3

#[bltouch]
#sensor_pin: gpio25
#control_pin: gpio13 # Reuse z endstop pin

#[neopixel board_rgb]
#pin: gpio29
#chain_count: 1
#color_order: GRB
#initial_RED: 0.3
#initial_GREEN: 0.3
#initial_BLUE: 0.3

[idle_timeout]
timeout: 1800

[safe_z_home]
home_xy_position: 120,120
speed: 50.0
z_hop: 5


# Tool to help adjust bed leveling screws. One may define a
# [bed_screws] config section to enable a BED_SCREWS_ADJUST g-code
# command.
[bed_screws]
screw1: 65,5
screw1_name: front screw
screw2: 10,110
screw2_name: back left
screw3: 120,110
screw3_name: back right


[gcode_macro PRINT_START]
#   Use PRINT_START for the slicer starting script - please customize for your slicer of choice
gcode:
    G28                            ; home all axes
    G1 Z20 F3000                   ; move nozzle away from bed
   
[gcode_macro PRINT_END]
#   Use PRINT_END for the slicer ending script - please customize for your slicer of choice
gcode:

    M400                           ; wait for buffer to clear
    G92 E0                         ; zero the extruder
    G1 E-4.0 F3600                 ; retract filament
    G91                            ; relative positioning

    #   Get Boundaries
    {% set max_x = printer.configfile.config["stepper_x"]["position_max"]|float %}
    {% set max_y = printer.configfile.config["stepper_y"]["position_max"]|float %}
    {% set max_z = printer.configfile.config["stepper_z"]["position_max"]|float %}

    #   Check end position to determine safe direction to move
    {% if printer.toolhead.position.x < (max_x - 20) %}
        {% set x_safe = 20.0 %}
    {% else %}
        {% set x_safe = -20.0 %}
    {% endif %}

    {% if printer.toolhead.position.y < (max_y - 20) %}
        {% set y_safe = 20.0 %}
    {% else %}
        {% set y_safe = -20.0 %}
    {% endif %}

    {% if printer.toolhead.position.z < (max_z - 2) %}
        {% set z_safe = 2.0 %}
    {% else %}
        {% set z_safe = max_z - printer.toolhead.position.z %}
    {% endif %}

    G0 Z{z_safe} F3600    ; move nozzle up
    G0 X{x_safe} Y{y_safe} F20000    ; move nozzle to remove stringing
    TURN_OFF_HEATERS
    M107                           ; turn off fan
    G90                            ; absolute positioning
    G0 X60 Y{max_y} F3600          ; park nozzle at rear
	
[gcode_macro LOAD_FILAMENT]
gcode:
   M83                            ; set extruder to relative
   G1 E30 F300                    ; load
   G1 E15 F150                    ; prime nozzle with filament
   M82                            ; set extruder to absolute
    
[gcode_macro UNLOAD_FILAMENT]
gcode:
   M83                            ; set extruder to relative
   G1 E10 F300                    ; extrude a little to soften tip
   G1 E-40 F1800                  ; retract some, but not too much or it will jam
   M82                            ; set extruder to absolute

#[include v0_display.cfg]
#[include bedScrewMenu.cfg]