# This file contains common pin mappings for the BigTreeTech Octopus V1.
# To use this config, the firmware should be compiled for the STM32F446 with a "32KiB bootloader"
# Enable "extra low-level configuration options" and select the "12MHz crystal" as clock reference

# after running "make", copy the generated "klipper/out/klipper.bin" file to a
# file named "firmware.bin" on an SD card and then restart the OctoPus with that SD card.

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

## Voron Design VORON2 250/300/350mm BigTreeTech OctoPus V1 TMC2209 UART config

## *** THINGS TO CHANGE/CHECK: ***
## MCU paths                            [mcu] section
## Thermistor types                     [extruder] and [heater_bed] sections - See https://www.klipper3d.org/Config_Reference.html#common-thermistors for common thermistor types
## Z Endstop Switch location            [safe_z_home] section
## Homing end position                  [gcode_macro G32] section
## Z Endstop Switch  offset for Z0      [stepper_z] section
## Probe points                         [quad_gantry_level] section
## Min & Max gantry corner postions     [quad_gantry_level] section
## PID tune                             [extruder] and [heater_bed] sections
## Probe pin                            [probe] section
## Fine tune E steps                    [extruder] section

[mcu]
canbus_uuid: 970febd72165

[include ebb36.cfg]
[include klicky-probe.cfg]
[include klicky-quad-gantry-level.cfg]
[include fluidd.cfg]
[include stealthburner_leds.cfg]
[include martyn_macros.cfg]



[printer]
kinematics: corexy
max_velocity: 300  
max_accel: 3000             #Max 4000
max_z_velocity: 15          #Max 15 for 12V TMC Drivers, can increase for 24V
max_z_accel: 350
square_corner_velocity: 5.0

#####################################################################
#   X/Y Stepper Settings
#####################################################################

##  B Stepper - Left
##  Connected to MOTOR_0
##  Endstop connected to DIAG_0
[stepper_x]
step_pin: PF13
dir_pin: PF12
enable_pin: !PF14
rotation_distance: 40
microsteps: 32
full_steps_per_rotation:400  #set to 200 for 1.8 degree stepper
endstop_pin: PG6
position_min: 0
position_endstop: 345
position_max: 345
homing_speed: 25   #Max 100
homing_retract_dist: 5
homing_positive_dir: true

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_x]
uart_pin: PC4
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 0

##  A Stepper - Right
##  Connected to MOTOR_1
##  Endstop connected to DIAG_1
[stepper_y]
step_pin: PG0
dir_pin: PG1
enable_pin: !PF15
rotation_distance: 40
microsteps: 32
full_steps_per_rotation:400  #set to 200 for 1.8 degree stepper
endstop_pin: PG9
position_min: 0
position_endstop: 350
position_max: 350
homing_speed: 25  #Max 100
homing_retract_dist: 5
homing_positive_dir: true

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_y]
uart_pin: PD11
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 0
 
#####################################################################
#   Z Stepper Settings
#####################################################################

## Z0 Stepper - Front Left
##  Connected to MOTOR_2
##  Endstop connected to DIAG_2
[stepper_z]
step_pin: PF11
dir_pin: PG3
enable_pin: !PG5
rotation_distance: 40
gear_ratio: 80:16
microsteps: 32
endstop_pin: PG10
##  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: -0.5
position_max: 310
position_min: -5
homing_speed: 8
second_homing_speed: 3
homing_retract_dist: 3

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_z]
uart_pin: PC6
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 999999

##  Z1 Stepper - Rear Left
##  Connected to MOTOR_3
[stepper_z1]
step_pin: PG4
dir_pin: !PC1
enable_pin: !PA0
rotation_distance: 40
gear_ratio: 80:16
microsteps: 32

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_z1]
uart_pin: PC7
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 999999

##  Z2 Stepper - Rear Right
##  Connected to MOTOR_4
[stepper_z2]
step_pin: PF9
dir_pin: PF10
enable_pin: !PG2
rotation_distance: 40
gear_ratio: 80:16
microsteps: 32

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_z2]
uart_pin: PF2
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 999999

##  Z3 Stepper - Front Right
##  Connected to MOTOR_5
[stepper_z3]
step_pin: PC13
dir_pin: !PF0
enable_pin: !PF1
rotation_distance: 40
gear_ratio: 80:16
microsteps: 32

##  Make sure to update below for your relevant driver (2208 or 2209)
[tmc2209 stepper_z3]
uart_pin: PE4
interpolate: false
run_current: 0.8
sense_resistor: 0.110
stealthchop_threshold: 999999


#####################################################################
#   Bed Heater
#####################################################################


##  SSR Pin - HE1
##  Thermistor - TB
[heater_bed]
heater_pin: PA1
##  Validate the following thermistor type to make sure it is correct
##  Keenovo branded bed heaters should use Generic 3950
##  LDO branded bed heaters will have the sensor type labelled on the heater
## Check what thermistor type you have. See https://www.klipper3d.org/Config_Reference.html#common-thermistors for common thermistor types.
## Use "Generic 3950" for Keenovo heaters
sensor_type: ATC Semitec 104NT-4-R025H42G
sensor_pin: PF3
##  Adjust Max Power so your heater doesn't warp your bed. Rule of thumb is 0.4 watts / cm^2 .
max_power: 0.6
min_temp: 0
max_temp: 120
control: pid
pid_kp: 58.437
pid_ki: 2.347
pid_kd: 363.769



#####################################################################
#   Fan Control
#####################################################################



##  Controller fan - FAN2
[controller_fan controller_fan]
pin: PD12
kick_start_time: 0.5
heater: heater_bed
fan_speed: 0.5

##  Exhaust fan - FAN3
#[heater_fan exhaust_fan]
#pin: PD13
#max_power: 1.0
#shutdown_speed: 0.0
#kick_start_time: 5.0
#heater: heater_bed
#heater_temp: 60
#fan_speed: 1.0

#####################################################################
#   LED Control
#####################################################################

[output_pin caselight]
## Chamber Lighting - CNC_FAN5
pin: PD15
pwm:true
shutdown_value: 0
value:1
cycle_time: 0.01

#####################################################################
#   Additional Sensors
#####################################################################

[thermistor CMFB103F3950FANT]
temperature1: 0.0
resistance1: 32116.0
temperature2: 40.0
resistance2: 5309.0
temperature3: 80.0
resistance3: 1228.0

# [temperature_sensor chamber_temp]
# ## Chamber Temperature - T1
# sensor_type: CMFB103F3950FANT
# sensor_pin: PF5
# min_temp: 0
# max_temp: 100
# gcode_id: chamber_th

#####################################################################
#   Homing and Gantry Adjustment Routines
#####################################################################

[idle_timeout]
timeout: 1800

# Klicky probe replaces this!
# [safe_z_home]
# ##  XY Location of the Z Endstop Switch
# ##  Update -10,-10 to the XY coordinates of your endstop pin 
# ##  (such as 157,305) after going through Z Endstop Pin
# ##  Location Definition step.
# home_xy_position:232,350
# speed:100
# z_hop:10
# z_hop_speed:10


##  Use QUAD_GANTRY_LEVEL to level a gantry.
##  Min & Max gantry corners - measure from nozzle at MIN (0,0) and 
##  MAX (250, 250), (300,300), or (350,350) depending on your printer size
##  to respective belt positions
[quad_gantry_level]
gantry_corners:
   -60,-10
   410,420
#  Probe points
points:
   50,25
   50,275
   300,275
   300,25

speed: 100
horizontal_move_z: 10
retries: 5
retry_tolerance: 0.0075
max_adjust: 10

########################################
# EXP1 / EXP2 (display) pins
########################################

[board_pins]
aliases:
    # EXP1 header
    EXP1_1=PE8, EXP1_2=PE7,
    EXP1_3=PE9, EXP1_4=PE10,
    EXP1_5=PE12, EXP1_6=PE13,    # Slot in the socket on this side
    EXP1_7=PE14, EXP1_8=PE15,
    EXP1_9=<GND>, EXP1_10=<5V>,

    # EXP2 header
    EXP2_1=PA6, EXP2_2=PA5,
    EXP2_3=PB1, EXP2_4=PA4,
    EXP2_5=PB2, EXP2_6=PA7,      # Slot in the socket on this side
    EXP2_7=PC15, EXP2_8=<RST>,
    EXP2_9=<GND>, EXP2_10=<5V>

#####################################################################
#   Displays
#####################################################################

[display]
#  mini12864 LCD Display
lcd_type: uc1701
cs_pin: EXP1_3
a0_pin: EXP1_4
rst_pin: EXP1_5
encoder_pins: ^EXP2_5, ^EXP2_3
click_pin: ^!EXP1_2
contrast: 63
spi_software_miso_pin: EXP2_1
spi_software_mosi_pin: EXP2_6
spi_software_sclk_pin: EXP2_2

[neopixel btt_mini12864]
#  To control Neopixel RGB in mini12864 display
pin: EXP1_6
chain_count: 3
initial_RED: 0.1
initial_GREEN: 0.5
initial_BLUE: 0.0
color_order: RGB

##  Set RGB values on boot up for each Neopixel. 
##  Index 1 = display, Index 2 and 3 = Knob
[delayed_gcode setdisplayneopixel]
initial_duration: 1
gcode:
        SET_LED LED=btt_mini12864 RED=1 GREEN=1 BLUE=1 INDEX=1 TRANSMIT=0
        SET_LED LED=btt_mini12864 RED=1 GREEN=0 BLUE=0 INDEX=2 TRANSMIT=0
        SET_LED LED=btt_mini12864 RED=1 GREEN=0 BLUE=0 INDEX=3 

#--------------------------------------------------------------------


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

[gcode_macro G32]
gcode:
    SAVE_GCODE_STATE NAME=STATE_G32
    G90
    G28
    QUAD_GANTRY_LEVEL
    G28
    ##  Uncomment for for your size printer:
    #--------------------------------------------------------------------
    ##  Uncomment for 250mm build
    #G0 X125 Y125 Z30 F3600
    
    ##  Uncomment for 300 build
    #G0 X150 Y150 Z30 F3600
    
    ##  Uncomment for 350mm build
    G0 X175 Y175 Z30 F3600
    #--------------------------------------------------------------------
    RESTORE_GCODE_STATE NAME=STATE_G32


#   Use PRINT_END for the slicer ending script - please customise for your slicer of choice
[gcode_macro PRINT_END]
gcode:
    # safe anti-stringing move coords
    {% set th = printer.toolhead %}
    {% set x_safe = th.position.x + 20 * (1 if th.axis_maximum.x - th.position.x > 20 else -1) %}
    {% set y_safe = th.position.y + 20 * (1 if th.axis_maximum.y - th.position.y > 20 else -1) %}
    {% set z_safe = [th.position.z + 2, th.axis_maximum.z]|min %}
    
    SAVE_GCODE_STATE NAME=STATE_PRINT_END
    
    M400                           ; wait for buffer to clear
    G92 E0                         ; zero the extruder
    G1 E-5.0 F1800                 ; retract filament
    
    TURN_OFF_HEATERS
    
    G90                                      ; absolute positioning
    G0 X{x_safe} Y{y_safe} Z{z_safe} F20000  ; move nozzle to remove stringing
    G0 X{th.axis_maximum.x//2} Y{th.axis_maximum.y - 2} F3600  ; park nozzle at rear
    M107                                     ; turn off fan
    
    BED_MESH_CLEAR
    RESTORE_GCODE_STATE NAME=STATE_PRINT_END

[bed_mesh]
speed: 120
horizontal_move_z: 10
mesh_min: 35, 35
mesh_max: 320, 320
probe_count: 5, 5

#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [stepper_z]
#*# position_endstop = -0.805