#   _____             __ _                       _   _                
#  / ____|           / _(_)                     | | (_)                
# | |     ___  _ __ | |_ _  __ _ _   _ _ __ __ _| |_ _  ___  _ __  ___ 
# | |    / _ \| '_ \|  _| |/ _` | | | | '__/ _` | __| |/ _ \| '_ \/ __|
# | |___| (_) | | | | | | | (_| | |_| | | | (_| | |_| | (_) | | | \__ \
#  \_____\___/|_| |_|_| |_|\__, |\__,_|_|  \__,_|\__|_|\___/|_| |_|___/
#                           __/ |                                      
#                          |___/    
#
# Klipper Configurations - Flsun V400
# Version 4.0
#
# Guislain Cyril


########################################
# Firmware Settings
########################################

# MKS Robin Nano V2.0

# When running "make menuconfig"
#
# [*] Enable extra low-level configuration options
#     Micro-controller Architecture (STMicroelectronics STM32)  --->
#     Processor model (STM32F103)  --->
# [ ] Only 10KiB of RAM (for rare stm32f103x6 variant)  (NEW)
# [ ] Disable SWD at startup (for GigaDevice stm32f103 clones)  (NEW)
#     Bootloader offset (28KiB bootloader)  --->
#     Clock Reference (8 MHz crystal)  --->
#     Communication interface (Serial (on USART3 PB11/PB10))  --->
# (250000) Baud rate for serial port
# ()  GPIO pins to set at micro-controller startup
#
# Note that the "make flash" command does not work with MKS Robin
# boards. After running "make", run the following command:
#   ./scripts/update_mks_robin.py out/klipper.bin out/Robin_nano35.bin
# Copy the file out/Robin_nano35.bin to an microSD card, 
# insert it in the printer and restart it.


########################################
# Editable Settings
########################################

# Notes: Some settings can be enabled or disabled by removing or adding the '#' symbol
#
# PID (pid_Kp, pid_Ki, pid_Kd) --> [extruder] and [heater_bed] sections
# E-Steps Extruder (rotation_distance) --> [extruder] section --> <rotation_distance> = <full_steps_per_rotation> * <microsteps> / <steps_per_mm>
# Pressure Advance (pressure_advance) --> [extruder] section -- See: https://www.klipper3d.org/Pressure_Advance.html
# Firmware Retraction --> [firmware_retraction] section -- Requires "Klipper Settings Plugin" for Cura -- See: https://github.com/jjgraphix/KlipperSettingsPlugin
# ADXL345 function for resonance testing --> Enable/Disable [include adxl345_pico.cfg] or [include adxl345_fysetc.cfg] -- Configuration in [input_shaper] section -- See: https://www.klipper3d.org/Measuring_Resonances.html
# NeoPixels macros --> Enable/Disable [include neopixels.cfg]
# Timelapse function --> Enable/Disable [include timelapse.cfg]


########################################
# Included Files
########################################

[include macros.cfg]
#[include adxl345_pico.cfg]  #Enable if you want to use ADXL with Rapsberry Pi Pico
#[include adxl345_fysetc.cfg]  #Enable if you want to use ADXL with Fysetc Portable Input Shaper
#[include timelapse.cfg]  #Enable if you want to use Timelapse
#[include neopixels.cfg]  #Enable if you want to use some Neopixels macros

########################################
# Enable Exclude Objects
########################################
[exclude_object]

########################################
# Printer Settings
########################################

[printer]
kinematics: delta
max_velocity: 600
max_accel: 8000
minimum_cruise_ratio: 0.5
square_corner_velocity: 5
max_z_velocity: 100
max_z_accel: 1500
minimum_z_position: -25
print_radius: 152
delta_radius: 152


########################################
# X Stepper Motor & Driver Settings
########################################

[stepper_a]
step_pin: PE3
dir_pin: PE2
enable_pin: !PE4
microsteps: 64  # If you encounter an error while measuring the resonances. Replace the microsteps to 16 just for the duration of the test.
rotation_distance: 40
endstop_pin: ^PA15
homing_speed: 50
homing_retract_dist: 5.0
homing_retract_speed: 10
angle: 210
position_endstop: 415.0
arm_length = 345.0

[tmc2209 stepper_a]
uart_pin: PD5
run_current: 1.2
interpolate: False
stealthchop_threshold: 0

[autotune_tmc stepper_a]  # Enable if Klipper TMC Autotune is used
motor: flsun-v400-42
tuning_goal: silent


########################################
# Y Stepper Motor & Driver Settings
########################################

[stepper_b]
step_pin: PE0
dir_pin: PB9
enable_pin: !PE1
microsteps: 64  # If you encounter an error while measuring the resonances. Replace the microsteps to 16 just for the duration of the test.
rotation_distance: 40
endstop_pin: ^PA12
angle: 330

[tmc2209 stepper_b]
uart_pin: PD7
run_current: 1.2
interpolate: False
stealthchop_threshold: 0

[autotune_tmc stepper_b]  # Enable if Klipper TMC Autotune is used
motor: flsun-v400-42
tuning_goal: silent


########################################
# Z Stepper Motor & Driver Settings
########################################

[stepper_c]
step_pin: PB5
dir_pin: PB4
enable_pin: !PB8
microsteps: 64  # If you encounter an error while measuring the resonances. Replace the microsteps to 16 just for the duration of the test.
rotation_distance: 40
endstop_pin: ^PC4
angle: 90

[tmc2209 stepper_c]
uart_pin: PD4
run_current: 1.2
interpolate: False
stealthchop_threshold: 0

[autotune_tmc stepper_c]  # Enable if Klipper TMC Autotune is used
motor: flsun-v400-42
tuning_goal: silent


########################################
# Extruder & Driver Settings
########################################

[extruder]
step_pin: PD6
dir_pin: !PD3
enable_pin: !PB3
microsteps: 16
rotation_distance: 4.5
nozzle_diameter: 0.400
filament_diameter: 1.750
heater_pin: PC3
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC1
min_temp: -5
max_temp: 315
max_extrude_cross_section: 50
max_extrude_only_distance: 800
#pressure_advance: 0.04
#pressure_advance_smooth_time: 0.040
control = pid
pid_kp = 17.501
pid_ki = 0.711
pid_kd = 107.630

[tmc2209 extruder]
uart_pin: PD9
run_current: 0.900
interpolate: True
stealthchop_threshold: 0

[autotune_tmc extruder]  # Enable if Klipper TMC Autotune is used
motor: flsun-v400-36
tuning_goal: auto


########################################
# Bed Settings
########################################

[heater_bed]
heater_pin: PA0
sensor_pin: PC0
sensor_type: EPCOS 100K B57560G104F
min_temp: -5
max_temp: 120
control = pid
pid_kp = 64.044
pid_ki = 3.812
pid_kd = 268.984


########################################
# Filament Sensor Settings
########################################

[filament_switch_sensor filament_sensor]
pause_on_runout: True
runout_gcode: M600
switch_pin: PA4


########################################
# Fans Settings
########################################

[fan]
pin: PB1

[heater_fan Hotend] 
pin: PB0
heater_temp: 50.0


########################################
# Probe Settings
########################################

[probe]
pin: !PA11
x_offset: 0
y_offset: 0
z_offset: 0
speed: 10
lift_speed: 50
samples: 8
samples_result: average
sample_retract_dist: 6
samples_tolerance: 0.02
samples_tolerance_retries: 7


########################################
# Delta Calibration & Mesh Settings
########################################

[delta_calibrate]
radius: 147
horizontal_move_z: 30
speed: 50

[bed_mesh]
speed: 50
horizontal_move_z: 30
mesh_radius: 147
mesh_origin: 0,0
mesh_pps: 2,2
round_probe_count: 13
algorithm: bicubic
bicubic_tension: 0.1
move_check_distance: 3
zero_reference_position: 0,0
fade_start: 1
fade_end: 30


########################################
# Temperature Controls
########################################

[verify_heater extruder]
max_error: 160
heating_gain: 1

[verify_heater heater_bed]
max_error: 120
heating_gain: 1


########################################
# Firmware Retraction Settings
########################################

[firmware_retraction]
retract_length: 0.7
retract_speed: 40
unretract_extra_length: 0.05
unretract_speed: 40


########################################
# Input Shaper Settings
########################################

[input_shaper]
shaper_freq_x: 38.6
shaper_type_x = mzv
shaper_freq_y: 45
shaper_type_y = ei


########################################
# G-Code Macros & Events
########################################

[idle_timeout]
timeout: 1800

[save_variables]
filename: ~/printer_data/config/variables.cfg

[gcode_arcs]
resolution: 0.1

[pause_resume]

[display_status]

[respond]

[exclude_object]

[virtual_sdcard]
path: ~/printer_data/gcodes

[endstop_phase stepper_a]
endstop_align_zero: False

[endstop_phase stepper_b]
endstop_align_zero: False

[endstop_phase stepper_c]
endstop_align_zero: False 


########################################
# MCU Settings
########################################

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

[temperature_sensor Speeder_Pad]
sensor_type: temperature_host
min_temp: 10
max_temp: 100

[temperature_sensor Motherboard]
sensor_type: temperature_mcu
min_temp: 0
max_temp: 100


########################################
# LED Settings
########################################

[output_pin LED_Hotend]
pin: PE12
pwm: False
value: 0

[output_pin LED_Logo]
pin: PD11
pwm: False
value: 1

#[neopixel NeoPixels]
#pin: PB2
#chain_count: 34
#color_order: GRB
#initial_RED: 1.0
#initial_GREEN: 1.0
#initial_BLUE: 1.0
