# Rat Rig V-core 3 Klipper Config
# Documentation: https://os.ratrig.com

# The first thing you'll need to do is go through this file and comment out / uncomment
# the files and/or settings you need.
# You'll be able to print just fine with this config as it is, but it is recommended
# that you follow these steps to properly calibrate your printer:
# 0) Sanity check and PID Tuning: https://www.klipper3d.org/Config_checks.html
# 1) Pressure Advance: https://www.klipper3d.org/Pressure_Advance.html
# 2) Skew Correction: https://www.klipper3d.org/Skew_Correction.html
# 3) Resonance Compensation: https://www.klipper3d.org/Resonance_Compensation.html

# Read more about klipper here: https://www.klipper3d.org/Overview.html

#############################################################################################################
### CONTROL BOARD
### Pick the board you have installed and wired in your printer.
#############################################################################################################
#[include RatOS/boards/btt-skr-pro-12/config.cfg]
[include RatOS/boards/btt-octopus-11/config.cfg]
#[include RatOS/boards/btt-octopus-11-407/config.cfg]
#[include RatOS/boards/btt-octopus-pro-446/config.cfg]
#[include RatOS/boards/btt-octopus-pro-429/config.cfg]
#[include RatOS/boards/btt-octopus-pro-h723/config.cfg]
#[include RatOS/boards/btt-manta-m8p/config.cfg]
#[include RatOS/boards/fysetc-spider/config.cfg]

#############################################################################################################
### BASE SETUP
#############################################################################################################
[include RatOS/printers/v-core-3/v-core-3.cfg]
[include RatOS/homing.cfg]
[include RatOS/macros.cfg]
[include RatOS/shell-macros.cfg]
[include RatOS/printers/v-core-3/macros.cfg]

#############################################################################################################
### STEPPER MOTORS, DRIVERS & SPEED LIMITS
### Pick the drivers and stepper motors you're using. See the RatOS documentation for custom combinations.
#############################################################################################################
[include RatOS/printers/v-core-3/steppers.cfg]

# UNCOOLED TMC 2209 + LDO-42STH48-2504AC
[include RatOS/printers/v-core-3/tmc2209.cfg]
[include RatOS/printers/v-core-3/speed-limits-basic.cfg]
[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.1a-x.cfg]
[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.1a-y.cfg]
[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.1a-z.cfg]
[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.1a-z1.cfg]
[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.1a-z2.cfg]

# COOLED TMC 2209 + LDO-42STH48-2504AC
# This increases motor torque, positional accuracy and speed limits.
# don't enable this before your printer is fully configured and you have a fan blowing on your stepper drivers.
#[include RatOS/printers/v-core-3/speed-limits-performance.cfg]
#[include RatOS/printers/v-core-3/tmc2209-performance.cfg]
#[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.6a-x.cfg]
#[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.6a-y.cfg]
#[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.6a-z.cfg]
#[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.6a-z1.cfg]
#[include RatOS/steppers/ldo/42sth48-2504ac/2209/24v-1.6a-z2.cfg]

# STEALTH MODE (Enables stealthchop and limits velocity and acceleration)
# NOTE: You still need to include one of the above stepper motor definitions.
# NOTE: This will make your printer quiter but less accurate, it's an inherent side effect of stealthchop.
#[include RatOS/printers/v-core-3/speed-limits-stealth.cfg]
#[include RatOS/printers/v-core-3/tmc2209-stealth.cfg]

#############################################################################################################
### PHYSICAL DIMENSIONS
### Pick your printer size
#############################################################################################################
# Remove the # from your printer size below.
# Similarly add a # in front of [include RatOS/printers/v-core-3/300.cfg] if you have a bigger machine.
#[include RatOS/printers/v-core-3/200.cfg]
[include RatOS/printers/v-core-3/300.cfg]
#[include RatOS/printers/v-core-3/400.cfg]
#[include RatOS/printers/v-core-3/500.cfg]

#############################################################################################################
### HOMING
### Pick your probe and endstops
#############################################################################################################
# BL Touch
[include RatOS/z-probe/bltouch.cfg]

# Inductive/Capacitive probe
#[include RatOS/z-probe/probe.cfg]

# Euclid probe (please read the RatOS documentation for instructions)
#[include RatOS/z-probe/euclid.cfg]

# Klicky probe (please read the RatOS documentation for instructions)
#[include RatOS/z-probe/klicky/klicky.cfg]
#[include RatOS/z-probe/klicky/unklicky.cfg]

# Physical endstop on X
[include RatOS/printers/v-core-3/physical-endstop-x.cfg]
# Physical endstop on Y
[include RatOS/printers/v-core-3/physical-endstop-y.cfg]

# Sensorless homing (Beware: this requires manual tinkering and does not work if your x/y stepper drivers
# have clipped DIAG pins). It is strongly encouraged to use physical endstops if you're a beginner.
# If you still wish to proceed, copy config/templates/sensorless-homing-tmc2209.cfg to the root directory and
# remove the # from the line below, then tune the settings in it, until you have reliable homing.
#[include sensorless-homing-tmc2209.cfg]

# If you want to only run sensorless homing on a single axis, copy the corresponding file from config/templates
# to the root directory and uncomment it below. Then tweak the settings in it until you have reliable homing.
#[include sensorless-x-homing-tmc2209.cfg]
#[include sensorless-y-homing-tmc2209.cfg]

#############################################################################################################
### INPUT SHAPER
### Enable/disable input shaper calibration
#############################################################################################################
# Uncomment this next line if you have an ADXL345 connected to your control board
#[include RatOS/printers/v-core-3/input-shaper.cfg]


#############################################################################################################
### TOOLHEAD
### Pick your extruder and hotend
#############################################################################################################
# Extruder
[include RatOS/extruders/bmg.cfg]
#[include RatOS/extruders/lgx.cfg]
#[include RatOS/extruders/lgx-lite.cfg]
#[include RatOS/extruders/orbiter.cfg]
#[include RatOS/extruders/orbiter-1004.cfg] # Use this with the LDO-36STH20-1004AHG motor
#[include RatOS/extruders/hemera.cfg]
#[include RatOS/extruders/titan.cfg]

# Hotend
#[include RatOS/hotends/v6.cfg]
#[include RatOS/hotends/revo.cfg]
#[include RatOS/hotends/dragonfly.cfg]
#[include RatOS/hotends/rapido.cfg]
#[include RatOS/hotends/copperhead.cfg]
[include RatOS/hotends/mosquito.cfg]
#[include RatOS/hotends/mosquito-magnum.cfg]
#[include RatOS/hotends/dragon-standard-flow.cfg]
#[include RatOS/hotends/dragon-high-flow.cfg]


#############################################################################################################
### FANS
### If your board has 4 pin fan headers and you want to use them, you can enable them here.
### NOTE: If none are uncommented, the default 2pin fan headers will be used.
#############################################################################################################
# Part cooling
#[include RatOS/4pin-fans/part-cooling-fan-25khz.cfg]
#[include RatOS/4pin-fans/part-cooling-fan-100hz.cfg]
[fan]
pin: !fan_part_cooling_pin
cycle_time: 0.00004

# Hotend / Toolhead cooling
#[include RatOS/4pin-fans/toolhead-fan-25khz.cfg]
#[include RatOS/4pin-fans/toolhead-fan-100hz.cfg]

# Control board cooling
#[include RatOS/4pin-fans/controller-fan-25khz.cfg]
#[include RatOS/4pin-fans/controller-fan-100hz.cfg]


#############################################################################################################
### MACRO CONFIGURATION
### Configure the behavior of RatOS macros
#############################################################################################################
[gcode_macro RatOS]
# Use absolute extrusion mode
# Set to True to use relative extrusion mode
variable_relative_extrusion: True
# Wait for extruder to reach 150 so an inductive probe (if present) is at a predictable temp.
# Also allows the bed heat to spread a little, and softens any plastic that might be stuck to the nozzle.
# Set to False to disable
variable_preheat_extruder: False
# Calibrate the bed mesh in the START_PRINT macro.
# Set to false to skip BED_MESH_CALIBRATE, it will still load the BED_MESH
# with the name "ratos", be sure to save your bed_mesh profile with that name.
# or override the _START_PRINT_BED_MESH macro to implement your own mesh handling logic.
variable_calibrate_bed_mesh: False
variable_bed_mesh_profile: "Bed-Mesh"
# Print a prime line or blob at the end of the START_PRINT macro
# set to "primeline" or "primeblob", or False to disable nozzle_priming.
variable_nozzle_priming: "primeline"
variable_nozzle_prime_start_x: 298
variable_nozzle_prime_start_y: "min"
variable_nozzle_prime_direction: "auto"
# Park in the back when waiting for the extruder to heat up
# set to "front" to park in the front, or "center" to park in the center.
variable_start_print_park_in: "front"
# Height to park it when waiting for extruder to heat.
variable_start_print_park_z_height: 50
# Skew profile to load before starting the print
# uncomment this to use your calibrated skew correction profile.
#variable_skew_profile: "my_skew_profile"
# Park in the back after the print has ended or was cancelled.
# set to "front" to park in the front, or "center" to park in the center.
variable_end_print_park_in: "back"
# Park in the back when the print is paused.
# set to "front" to park in the front, or "center" to park in the center.
variable_pause_print_park_in: "back"
# Set the speed for travel moves in RatOS Macros in mm/s.
variable_macro_travel_speed: 300


#############################################################################################################
### PRINTER CONFIGURATION
### Customize the defaults to your specific build
#############################################################################################################
[stepper_x]
dir_pin: x_dir_pin # Add ! in front of pin name to reverse X stepper direction
rotation_distance: 40 # 40 for 20 tooth 2GT pulleys, 32 for 16 tooth 2GT pulleys

[stepper_y]
dir_pin: y_dir_pin # Add ! in front of pin name to reverse Y stepper direction
rotation_distance: 40 # 40 for 20 tooth 2GT pulleys, 32 for 16 tooth 2GT pulleys

[stepper_z]
dir_pin: !z0_dir_pin # Add ! in front of pin name to reverse Z stepper direction
rotation_distance: 4 # 4 for TR8*4 lead screws

[stepper_z1]
dir_pin: !z1_dir_pin # Add ! in front of pin name to reverse Z1 direction
rotation_distance: 4 # 4 for TR8*4 lead screws

[stepper_z2]
dir_pin: !z2_dir_pin # Add ! in front of pin name to reverse Z2 direction
rotation_distance: 4 # 4 for TR8*4 lead screws

# BLTouch configuration
[bltouch]
#z_offset: 2.5 # Adjust this to fit your setup

# Inductive probe configuration
#[probe]
#z_offset: 0.0 # Adjust this to fit your setup
#pin: ^probe_pin # For NPN NC probes such as the Super Pinda / Vinda / SupCR / Decoprobe probes.
#pin: ^!probe_pin # NPN NO (refer to the specs on your probe)
#pin: probe_pin # PNP NO (refer to the specs on your probe)
#pin: !probe_pin # PNP NC (refer to the specs on your probe)

[extruder]
# Check https://www.klipper3d.org/Pressure_Advance.html for pressure advance tuning.
pressure_advance: 0.05
nozzle_diameter: 0.8 # Remember to change this if you change nozzle diameter.
dir_pin: !e_dir_pin # Remove ! in front of pin name to reverse extruder direction
#control: pid
#pid_kp: 28.413
#pid_ki: 1.334
#pid_kd: 151.300

[heater_bed]
#control: pid
#pid_Kp: 22.2
#pid_Ki: 1.08
#pid_Kd: 114

# ADXL345 resonance testing configuration
#[resonance_tester]
#probe_points:
#    100,100,20 # 200mm printer
#    150,150,20 # 300mm printer
#    200,200,20 # 400mm printer
#    250,250,20 # 500mm printer

#############################################################################################################
### TOOLBOARD
### If you have a toolboard installed, select it here
#############################################################################################################
#[include RatOS/boards/mellow-fly-sht-42/toolboard-config.cfg]
#[include RatOS/boards/mellow-fly-sht-36/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb42-10/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb36-10/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb42-11/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb36-11/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb42-12/toolboard-config.cfg]
#[include RatOS/boards/btt-ebb36-12/toolboard-config.cfg]

# If you have a toolboard installed, uncomment the following
#[include RatOS/boards/toolboard.cfg]

# Bltouch
#[include RatOS/toolboard/bltouch.cfg]

# Probe
#[probe]
#pin: ^toolboard:probe_pin # For NPN NC probes such as the SuperPinda / Vinda / SupCR / Decoprobe probes.
#pin: ^!toolboard:probe_pin # NPN NO (refer to the specs on your probe)
#pin: toolboard:probe_pin # PNP NO (refer to the specs on your probe)
#pin: !toolboard:probe_pin # PNP NC (refer to the specs on your probe)

# If you need to change the direction of your extruder, you can do it here.
#[extruder]
#dir_pin: !toolboard:e_dir_pin

# If you have your x endstop connected to your toolboard, uncomment these next two lines.
#[stepper_x]
#endstop_pin: toolboard:x_endstop_pin

# To use the toolboard's ADXL345 for resonance testing (input shaper calibration) uncomment the following
#[include RatOS/toolboard/input-shaper.cfg]


#############################################################################################################
### USER OVERRIDES
### Anything custom you want to add, or RatOS configuration you want to override, do it here.
#############################################################################################################

# Increase size of bed a little. Based on 300.cfg
[stepper_x]
position_max: 303
position_endstop: 0

[stepper_y]
position_max: 305
position_endstop: 303

# Probe overrides
[probe]
speed: 15
lift_speed: 15

# Tilt overrides
[z_tilt]
speed: 300
#   The speed (in mm/s) of non-probing moves during the calibration. The default is 50

# changes to machine limits
[printer]
kinematics: corexy
max_velocity: 300
max_accel: 3000
max_accel_to_decel: 750
max_z_velocity: 15
max_z_accel: 20

# Input shaper resonance compensation
[input_shaper]
shaper_freq_x: 40  # frequency for the X mark of the test model
shaper_freq_y: 30  # frequency for the Y mark of the test model
shaper_type: mzv

# Retraction settings (from BMG extruder settings)
[firmware_retraction]
retract_speed: 50
unretract_extra_length: 0
unretract_speed: 50

# Retraction settings (from Mosquito)
[firmware_retraction]
retract_length: .75


# This changes the Prime Line 
# VVVVVVVVVVVVVVVVVVVVVVVVVVVV
[gcode_macro PRIME_LINE]
description: Prints a primeline, used internally, if configured, as part of the START_PRINT macro.
gcode:
  SAVE_GCODE_STATE NAME=prime_line_state
  # speed changed from original 60% to give a slow prime ... CHANGE
  {% set speed = printer["gcode_macro RatOS"].macro_travel_speed|float * 30 %}
  {% set z_speed = printer["gcode_macro RatOS"].macro_z_speed|float * 60 %}
  {% if printer["gcode_macro RatOS"].nozzle_prime_start_x|lower == 'min' %}
    {% set x_start = 5 %}
  {% elif printer["gcode_macro RatOS"].nozzle_prime_start_x|lower == 'max' %}
    {% set x_start = printer.toolhead.axis_maximum.x - 5 %}
  {% else %}
    {% set x_start = printer["gcode_macro RatOS"].nozzle_prime_start_x|float %}
  {% endif %}
  {% if printer["gcode_macro RatOS"].nozzle_prime_start_y|lower == 'min' %}
    {% set y_start = 5 %}
    {% set y_factor = 1 %}
  {% elif printer["gcode_macro RatOS"].nozzle_prime_start_y|lower == 'max' %}
    {% set y_start = printer.toolhead.axis_maximum.y - 5 %}
    {% set y_factor = -1 %}
  {% else %}
    {% set y_start = printer["gcode_macro RatOS"].nozzle_prime_start_y|float %}
    {% if printer["gcode_macro RatOS"].nozzle_prime_start_y|float < printer.toolhead.axis_maximum.y / 2 %}
      {% set y_factor = 1 %}
    {% else %}
      {% set y_factor = -1 %}
    {% endif %}
  {% endif %}
  {% if printer["gcode_macro RatOS"].nozzle_prime_direction|lower == 'forwards' %}
    {% set y_factor = 1 %}
  {% elif printer["gcode_macro RatOS"].nozzle_prime_direction|lower == 'backwards' %}
    {% set y_factor = -1 %}
  {% endif %}
  {% set z = printer["gcode_macro RatOS"].start_print_park_z_height|float %}
  # Absolute positioning
  G90 
  # Absolute extrusion
  M82
  # Final Z home
  G1 X150 Y150 F8000
  G28 Z
  M117 Priming nozzle with prime line..
  RESPOND MSG="Priming nozzle with prime line.."
  # move to position along the edge of the bed
  G1 X200.0 Y1 Z0.8 F8000
  # Set extruder position
  G92 E0.0 
  # Prime line part 1
  G1 X150.0 E20.0 F600
  # Prime line part 1
  G1 X120 E30 Z0.4 F600
  # Retract
  G1 X115 E29.0 F600
  # Reset extruder
  G92 E0.0 
  RESTORE_GCODE_STATE NAME=prime_line_state

# This is to modify Start print sequence
# Preheat extruder BEFORE bed has reached temperature .. CHANGE
# VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
[gcode_macro START_PRINT]
description: Start print procedure, use this in your Slicer.
gcode:
  CLEAR_PAUSE
  {% if printer["gcode_macro RatOS"].force_absolute_position|lower == 'true' %}
  G90
  {% endif %}
  SAVE_GCODE_STATE NAME=start_print_state
  # Set units to mm
  G21
  # Absolute positioning
  G90 
  # Set extruder to absolute mode
  M82
  {% if printer["gcode_macro RatOS"].z_probe|lower == 'stowable' %}
  STOWABLE_PROBE_BEGIN_BATCH
  {% endif %}
   # Start bed heat up. This wasn't here before .... CHANGE
  M140 S{params.BED_TEMP|default(printer.heater_bed.target, true) }
  # Start nozzle heat up to 150C. This is a ... CHANGE
  M104 S180
  # Home if needed
  MAYBE_HOME
  M117 Heating bed...
  RESPOND MSG="Heating bed..."
  # Run the customizable "AFTER_HEATING_BED" macro.
  _START_PRINT_AFTER_HEATING_BED
  # Run the customizable "BED_MESH" macro
  _START_PRINT_BED_MESH  
  {% if printer["gcode_macro RatOS"].z_probe|lower == 'stowable' %}
  STOWABLE_PROBE_END_BATCH
  {% endif %}
  # Start heating extruder
  M104 S{params.EXTRUDER_TEMP|default(printer.extruder.target, true) }
  # Run the customizable "PARK" macro
  _START_PRINT_PARK
  # Wait for extruder to heat up
  M117 Heating Extruder...
  RESPOND MSG="Heating Extruder..."
  M109 S{params.EXTRUDER_TEMP|default(printer.extruder.target, true) }
  # Wait for bed to heat up. This has been moved to later so levelling and extruder heatup could start. CHANGE
  M190 S{params.BED_TEMP|default(printer.heater_bed.target, true) }
  # Run the customizable "AFTER_HEATING_EXTRUDER" macro.
  _START_PRINT_AFTER_HEATING_EXTRUDER
  M117 Printing...
  RESPOND MSG="Printing..."
  RESTORE_GCODE_STATE NAME=start_print_state
  # Set extrusion mode based on user configuration
  {% if printer["gcode_macro RatOS"].relative_extrusion|lower == 'true' %}
    M83
  {% else %}
    M82
  {% endif %}
  G92 E0

#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [bltouch]
#*# z_offset = 2.620
#*#
#*# [extruder]
#*# control = pid
#*# pid_kp = 24.239
#*# pid_ki = 1.293
#*# pid_kd = 113.621
#*#
#*# [heater_bed]
#*# control = pid
#*# pid_kp = 60.370
#*# pid_ki = 1.417
#*# pid_kd = 642.944
#*#
#*# [bed_mesh Ratos]
#*# version = 1
#*# points =
#*# 	0.108437, 0.084062, 0.074062, 0.108750, 0.139062, 0.140312, 0.109375
#*# 	0.087500, 0.112812, 0.080000, 0.097812, 0.108437, 0.053437, 0.055625
#*# 	0.062500, 0.098125, 0.077812, 0.039062, 0.059062, -0.000625, 0.016250
#*# 	0.046250, 0.057500, 0.044687, -0.011563, 0.001875, 0.016562, 0.009062
#*# 	0.049687, 0.057500, 0.005000, 0.003437, 0.019375, 0.051875, 0.020937
#*# 	0.065937, 0.054687, 0.042500, 0.088125, 0.085312, 0.083750, 0.111875
#*# 	0.125000, 0.079687, 0.052187, 0.113125, 0.184375, 0.188437, 0.130625
#*# x_count = 7
#*# y_count = 7
#*# mesh_x_pps = 2
#*# mesh_y_pps = 2
#*# algo = bicubic
#*# tension = 0.2
#*# min_x = 20.0
#*# max_x = 264.98
#*# min_y = 20.0
#*# max_y = 260.0
#*#
#*# [bed_mesh Bed-Mesh]
#*# version = 1
#*# points =
#*# 	0.108437, 0.084062, 0.074062, 0.108750, 0.139062, 0.140312, 0.109375
#*# 	0.087500, 0.112812, 0.080000, 0.097812, 0.108437, 0.053437, 0.055625
#*# 	0.062500, 0.098125, 0.077812, 0.039062, 0.059062, -0.000625, 0.016250
#*# 	0.046250, 0.057500, 0.044687, -0.011563, 0.001875, 0.016562, 0.009062
#*# 	0.049687, 0.057500, 0.005000, 0.003437, 0.019375, 0.051875, 0.020937
#*# 	0.065937, 0.054687, 0.042500, 0.088125, 0.085312, 0.083750, 0.111875
#*# 	0.125000, 0.079687, 0.052187, 0.113125, 0.184375, 0.188437, 0.130625
#*# x_count = 7
#*# y_count = 7
#*# mesh_x_pps = 2
#*# mesh_y_pps = 2
#*# algo = bicubic
#*# tension = 0.2
#*# min_x = 20.0
#*# max_x = 264.98
#*# min_y = 20.0
#*# max_y = 260.0