diff --git a/Fabrikator.txt b/Fabrikator.txt new file mode 100644 index 0000000..2b8cf75 --- /dev/null +++ b/Fabrikator.txt @@ -0,0 +1,31 @@ +1:46:29.193: echo:Settings Stored (614 bytes; crc 18498) +1:46:30.390: echo:V47 stored settings retrieved (614 bytes; crc 18498) +1:46:30.390: echo: G21 ; Units in mm +1:46:30.390: echo: M149 C ; Units in Celsius +1:46:30.390: echo:Filament settings: Disabled +1:46:30.391: echo: M200 D1.75 +1:46:30.391: echo: M200 D0 +1:46:30.392: echo:Steps per unit: +1:46:30.393: echo: M92 X80.00 Y40.00 Z1600.00 E95.00 +1:46:30.398: echo:Maximum feedrates (units/s): +1:46:30.398: echo: M203 X100.00 Y100.00 Z5.00 E25.00 +1:46:30.402: echo:Maximum Acceleration (units/s2): +1:46:30.402: echo: M201 X15000 Y3500 Z50 E5000 +1:46:30.406: echo:Acceleration (units/s2): P R T +1:46:30.406: echo: M204 P1500.00 R1000.00 T1500.00 +1:46:30.409: echo:Advanced: S T B X Z E +1:46:30.414: echo: M205 S0.00 T0.00 B20000 X10.00 Y10.00 Z0.30 E5.00 +1:46:30.415: echo:Home offset: +1:46:30.415: echo: M206 X0.00 Y0.00 Z0.00 +1:46:30.417: echo:Auto Bed Leveling: +1:46:30.417: echo: M420 S0 Z0.00 +1:46:30.418: echo:Material heatup parameters: +1:46:30.419: echo: M145 S0 H180 B70 F0 +1:46:30.420: echo: M145 S1 H240 B110 F0 +1:46:30.420: echo:PID settings: +1:46:30.421: echo: M301 P24.75 I2.21 D69.45 +1:46:30.426: echo: M304 P112.28 I9.32 D338.03 +1:46:30.426: echo:Z-Probe Offset (mm): +1:46:30.426: echo: M851 Z-0.40 +1:46:30.426: echo:Stepper driver current: +1:46:30.429: echo: M906 X 800 Y 950 Z 800 E0 800 \ No newline at end of file diff --git a/Marlin-bugfix-1.1.x-trigorilla/.gitattributes b/Marlin-bugfix-1.1.x-trigorilla/.gitattributes new file mode 100644 index 0000000..d511b4e --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/.gitattributes @@ -0,0 +1,20 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto + +# Files with Unix line endings +*.c text eol=lf +*.cpp text eol=lf +*.h text eol=lf +*.ino text eol=lf +*.py text eol=lf +*.sh text eol=lf +*.scad text eol=lf + +# Files with native line endings +# *.sln text + +# Binary files +*.png binary +*.jpg binary +*.fon binary + diff --git a/Marlin-bugfix-1.1.x-trigorilla/.github/issue_template.md b/Marlin-bugfix-1.1.x-trigorilla/.github/issue_template.md new file mode 100644 index 0000000..17f680d --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/.github/issue_template.md @@ -0,0 +1,36 @@ +Thank you for submitting your feedback to the Marlin project. +Please use one of the templates below to fill out this box. + +------------------------------------------------------------ +### Feature Request +Please put [FR] in the issue title: `[FR] Add-on that goes 'ping'` + +------------------------------------------------------------ +### Compile Error +When I compile with `FEATURE_X` I get an error: +``` +Paste_the_error_text_here +``` + +------------------------------------------------------------ +### Bug Report +- Description: --- +- Expected behaviour: --- +- Actual behaviour: --- +- Steps to reproduce: + - Do this + - Do that + +Attach a ZIP of `Configuration.h` and `Configuration_adv.h` by dropping here. + +------------------------------------------------------------ +### Bug Report Tips +- When troubleshooting, use `M502` followed by `M500` to reset EEPROM to defaults. +- Use `DEBUG_LEVELING_FEATURE` with `M111 S247` for detailed logging of homing/leveling. +- Format text with: **bold**, _italic_, `code`. +- Format C++ with three backticks, plus "cpp": +```cpp +void my_function(bool do_it) { + // Hold this spot +} +``` diff --git a/Marlin-bugfix-1.1.x-trigorilla/.gitignore b/Marlin-bugfix-1.1.x-trigorilla/.gitignore new file mode 100644 index 0000000..8c70538 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/.gitignore @@ -0,0 +1,141 @@ +# +# Marlin 3D Printer Firmware +# Copyright (C) 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] +# +# Based on Sprinter and grbl. +# Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# Our automatic versioning scheme generates the following file +# NEVER put it in the repository +_Version.h + +# +# OS +# +applet/ +*.DS_Store + + +# +# Misc +# +*~ +*.orig +*.rej +*.bak +*.idea +*.s +*.i +*.ii +*.swp +tags + +# +# C++ +# +# Compiled Object files +*.slo +*.lo +*.o +*.obj +*.ino.cpp + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + + +# +# C +# +# Object files +*.o +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su + +# PlatformIO files/dirs +.pio* +lib/readme.txt + +#Visual Studio +*.sln +*.vcxproj +*.vcxproj.filters +Marlin/Release/ +Marlin/Debug/ +Marlin/__vm/ +Marlin/.vs/ + +#VScode +.vscode + +#cmake +CMakeLists.txt +Marlin/CMakeLists.txt +CMakeListsPrivate.txt + +#CLion +cmake-build-* diff --git a/Marlin-bugfix-1.1.x-trigorilla/.travis.yml b/Marlin-bugfix-1.1.x-trigorilla/.travis.yml new file mode 100644 index 0000000..ad67320 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/.travis.yml @@ -0,0 +1,358 @@ +dist: trusty +sudo: true + # +language: c + # +notifications: + email: false + # +before_install: + # + # Fetch the tag information for the current branch + - git fetch origin --tags + # + # Publish the buildroot script folder + - chmod +x ${TRAVIS_BUILD_DIR}/buildroot/bin/* + - export PATH=${TRAVIS_BUILD_DIR}/buildroot/bin/:${PATH} + # + # Start fb X server + - "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16" + - sleep 3 + - export DISPLAY=:1.0 + # +install: + # + # Install arduino 1.6.10 + - wget http://downloads-02.arduino.cc/arduino-1.6.10-linux64.tar.xz + - tar xf arduino-1.6.10-linux64.tar.xz + - sudo mv arduino-1.6.10 /usr/local/share/arduino + - ln -s /usr/local/share/arduino/arduino ${TRAVIS_BUILD_DIR}/buildroot/bin/arduino + # + # Install: LiquidCrystal_I2C library + - git clone https://github.com/kiyoshigawa/LiquidCrystal_I2C.git + - mv LiquidCrystal_I2C/LiquidCrystal_I2C /usr/local/share/arduino/libraries/LiquidCrystal_I2C + # + # Install: LiquidTWI2 library + - git clone https://github.com/lincomatic/LiquidTWI2.git + - sudo mv LiquidTWI2 /usr/local/share/arduino/libraries/LiquidTWI2 + # + # Install: Monochrome Graphics Library for LCDs and OLEDs + - git clone https://github.com/olikraus/U8glib_Arduino.git + - sudo mv U8glib_Arduino /usr/local/share/arduino/libraries/U8glib + # + # Install: L6470 Stepper Motor Driver library + # - git clone https://github.com/ameyer/Arduino-L6470.git + # - sudo mv Arduino-L6470/L6470 /usr/local/share/arduino/libraries/L6470 + # + # Install: TMC26X Stepper Motor Controller library + # - git clone https://github.com/trinamic/TMC26XStepper.git + # - sudo mv TMC26XStepper /usr/local/share/arduino/libraries/TMC26XStepper + # + # Install: TMC2130 Stepper Motor Controller library + - git clone https://github.com/teemuatlut/TMC2130Stepper.git + - sudo mv TMC2130Stepper /usr/local/share/arduino/libraries/TMC2130Stepper + # + # Install: Adafruit Neopixel library + - git clone https://github.com/adafruit/Adafruit_NeoPixel.git + - sudo mv Adafruit_NeoPixel /usr/local/share/arduino/libraries/Adafruit_NeoPixel + # +before_script: + # + # Change current working directory to the build dir + - cd ${TRAVIS_BUILD_DIR} + # + # Generate custom version include + - generate_version_header_for_marlin ${TRAVIS_BUILD_DIR}/Marlin + - cat ${TRAVIS_BUILD_DIR}/Marlin/_Version.h + # +script: + # + # Backup Configuration.h, Configuration_adv.h, and pins_RAMPS.h + # + - cp Marlin/Configuration.h Marlin/Configuration.h.backup + - cp Marlin/Configuration_adv.h Marlin/Configuration_adv.h.backup + - cp Marlin/pins_RAMPS.h Marlin/pins_RAMPS.h.backup + # + # Build with the default configurations + # + - build_marlin + # + # Test 2 extruders (one MAX6675) and heated bed on basic RAMPS 1.4 + # Test a "Fix Mounted" Probe with Safe Homing, some arc options, + # linear bed leveling, M48, leveling debug, and firmware retraction. + # + - opt_set MOTHERBOARD BOARD_RAMPS_14_EEB + - opt_set EXTRUDERS 2 + - opt_set TEMP_SENSOR_0 -2 + - opt_set TEMP_SENSOR_1 1 + - opt_set TEMP_SENSOR_BED 1 + - opt_enable PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING ARC_P_CIRCLES CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS + - opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS + - opt_enable BLINKM PCA9632 RGB_LED NEOPIXEL_LED + - opt_enable AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE + - opt_enable_adv FWRETRACT MAX7219_DEBUG + - opt_set ABL_GRID_POINTS_X 16 + - opt_set ABL_GRID_POINTS_Y 16 + - opt_set_adv FANMUX0_PIN 53 + - build_marlin + # + # Test a probeless build of AUTO_BED_LEVELING_UBL + # + - restore_configs + - opt_enable AUTO_BED_LEVELING_UBL UBL_G26_MESH_EDITING ENABLE_LEVELING_FADE_HEIGHT EEPROM_SETTINGS G3D_PANEL + - opt_enable_adv CUSTOM_USER_MENUS I2C_POSITION_ENCODERS BABYSTEPPING + - build_marlin + # + # And with a probe... + # + - opt_enable FIX_MOUNTED_PROBE + - build_marlin + # + # Test a Sled Z Probe + # ...with AUTO_BED_LEVELING_LINEAR, DEBUG_LEVELING_FEATURE, EEPROM_SETTINGS, and EEPROM_CHITCHAT + # + - restore_configs + - opt_enable Z_PROBE_SLED AUTO_BED_LEVELING_LINEAR DEBUG_LEVELING_FEATURE EEPROM_SETTINGS EEPROM_CHITCHAT + - build_marlin + # + # Test a Servo Probe + # ...with AUTO_BED_LEVELING_3POINT, DEBUG_LEVELING_FEATURE, EEPROM_SETTINGS, EEPROM_CHITCHAT, EXTENDED_CAPABILITIES_REPORT, and AUTO_REPORT_TEMPERATURES + # + - restore_configs + - opt_enable NUM_SERVOS Z_ENDSTOP_SERVO_NR Z_SERVO_ANGLES DEACTIVATE_SERVOS_AFTER_MOVE + - opt_set NUM_SERVOS 1 + - opt_enable AUTO_BED_LEVELING_3POINT DEBUG_LEVELING_FEATURE EEPROM_SETTINGS EEPROM_CHITCHAT + - opt_enable_adv EXTENDED_CAPABILITIES_REPORT AUTO_REPORT_TEMPERATURES AUTOTEMP G38_PROBE_TARGET + - build_marlin + # + # Test MESH_BED_LEVELING feature, with LCD + # + - restore_configs + - opt_enable MESH_BED_LEVELING MESH_G28_REST_ORIGIN LCD_BED_LEVELING ULTIMAKERCONTROLLER + - build_marlin + # + # Test PROBE_MANUALLY feature, with LCD support, + # EEPROM_SETTINGS, EEPROM_CHITCHAT, M100_FREE_MEMORY_WATCHER, + # INCH_MODE_SUPPORT, TEMPERATURE_UNITS_SUPPORT + # + - restore_configs + - opt_set MOTHERBOARD BOARD_MINIRAMBO + - opt_enable PROBE_MANUALLY AUTO_BED_LEVELING_BILINEAR LCD_BED_LEVELING ULTIMAKERCONTROLLER + - opt_enable EEPROM_SETTINGS EEPROM_CHITCHAT M100_FREE_MEMORY_WATCHER M100_FREE_MEMORY_DUMPER M100_FREE_MEMORY_CORRUPTOR INCH_MODE_SUPPORT TEMPERATURE_UNITS_SUPPORT + - build_marlin + # + # Test 5 extruders on AZTEEG_X3_PRO (can use any board with >=5 extruders defined) + # Include a test for LIN_ADVANCE here also + # + - opt_set MOTHERBOARD BOARD_AZTEEG_X3_PRO + - opt_set EXTRUDERS 5 + - opt_set TEMP_SENSOR_1 1 + - opt_set TEMP_SENSOR_2 5 + - opt_set TEMP_SENSOR_3 20 + - opt_set TEMP_SENSOR_4 999 + - opt_set TEMP_SENSOR_BED 1 + - opt_enable_adv LIN_ADVANCE + - build_marlin + # + # Mixing Extruder with 5 steppers + # + - restore_configs + - opt_set MOTHERBOARD BOARD_AZTEEG_X3_PRO + - opt_enable MIXING_EXTRUDER + - opt_set MIXING_STEPPERS 5 + - build_marlin + # + # Test DUAL_X_CARRIAGE + # + - restore_configs + - opt_set MOTHERBOARD BOARD_RUMBA + - opt_set EXTRUDERS 2 + - opt_set TEMP_SENSOR_1 1 + - opt_enable USE_XMAX_PLUG + - opt_enable_adv DUAL_X_CARRIAGE + - build_marlin + # + # Test SPEAKER with BOARD_BQ_ZUM_MEGA_3D and BQ_LCD_SMART_CONTROLLER + # + - restore_configs + - opt_set MOTHERBOARD BOARD_BQ_ZUM_MEGA_3D + - opt_set LCD_FEEDBACK_FREQUENCY_DURATION_MS 10 + - opt_set LCD_FEEDBACK_FREQUENCY_HZ 100 + - opt_enable BQ_LCD_SMART_CONTROLLER SPEAKER + # + # Test SWITCHING_EXTRUDER + # + - restore_configs + - opt_set MOTHERBOARD BOARD_RUMBA + - opt_set EXTRUDERS 2 + - opt_enable NUM_SERVOS + - opt_set NUM_SERVOS 1 + - opt_set TEMP_SENSOR_1 1 + - opt_enable SWITCHING_EXTRUDER ULTIMAKERCONTROLLER + - build_marlin + # + # Test MINIRAMBO for PWM_MOTOR_CURRENT + # ULTIMAKERCONTROLLER, FILAMENT_LCD_DISPLAY, FILAMENT_WIDTH_SENSOR, + # PRINTCOUNTER, NOZZLE_PARK_FEATURE, NOZZLE_CLEAN_FEATURE, PCA9632, + # Z_DUAL_STEPPER_DRIVERS, Z_DUAL_ENDSTOPS, BEZIER_CURVE_SUPPORT, EXPERIMENTAL_I2CBUS, + # ADVANCED_PAUSE_FEATURE, PARK_HEAD_ON_PAUSE, LCD_INFO_MENU, + # + - restore_configs + - opt_enable ULTIMAKERCONTROLLER FILAMENT_LCD_DISPLAY FILAMENT_WIDTH_SENSOR SDSUPPORT + - opt_enable PRINTCOUNTER NOZZLE_PARK_FEATURE NOZZLE_CLEAN_FEATURE PCA9632 USE_XMAX_PLUG + - opt_enable_adv Z_DUAL_STEPPER_DRIVERS Z_DUAL_ENDSTOPS BEZIER_CURVE_SUPPORT EXPERIMENTAL_I2CBUS + - opt_set_adv I2C_SLAVE_ADDRESS 63 + - opt_enable_adv ADVANCED_PAUSE_FEATURE PARK_HEAD_ON_PAUSE LCD_INFO_MENU + - opt_add_adv Z2_MIN_PIN 2 + - build_marlin + # + # Enable COREXY + # + - restore_configs + - opt_enable COREXY + - build_marlin + # + # Enable COREYX (swapped) + # + #- restore_configs + #- opt_enable COREYX + #- build_marlin + # + # + ######## Other Standard LCD/Panels ############## + # + # ULTRA_LCD + # + - restore_configs + - opt_enable ULTRA_LCD + - build_marlin + # + # DOGLCD + # + - restore_configs + - opt_enable DOGLCD + - build_marlin + # + # MAKRPANEL + # Needs to use Melzi and Sanguino hardware + # + #- restore_configs + #- opt_enable MAKRPANEL + #- build_marlin + # + # REPRAP_DISCOUNT_SMART_CONTROLLER, SDSUPPORT, BABYSTEPPING, RIGIDBOARD_V2, and DAC_MOTOR_CURRENT_DEFAULT + # + - restore_configs + - opt_set MOTHERBOARD BOARD_RIGIDBOARD_V2 + - opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT BABYSTEPPING DAC_MOTOR_CURRENT_DEFAULT + - build_marlin + # + # G3D_PANEL with SDCARD_SORT_ALPHA and STATUS_MESSAGE_SCROLLING + # + - restore_configs + - opt_enable G3D_PANEL SDSUPPORT + - opt_enable_adv SDCARD_SORT_ALPHA STATUS_MESSAGE_SCROLLING SCROLL_LONG_FILENAMES + - opt_set_adv SDSORT_GCODE true + - opt_set_adv SDSORT_USES_RAM true + - opt_set_adv SDSORT_USES_STACK true + - opt_set_adv SDSORT_CACHE_NAMES true + - build_marlin + # + # REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER with SDCARD_SORT_ALPHA and STATUS_MESSAGE_SCROLLING + # + - restore_configs + - opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER SDSUPPORT + - opt_enable_adv SDCARD_SORT_ALPHA STATUS_MESSAGE_SCROLLING SCROLL_LONG_FILENAMES + - build_marlin + # + # REPRAPWORLD_KEYPAD + # + # Cant find configuration details to get it to compile + #- restore_configs + #- opt_enable ULTRA_LCD REPRAPWORLD_KEYPAD REPRAPWORLD_KEYPAD_MOVE_STEP + #- build_marlin + # + # RA_CONTROL_PANEL + # + - restore_configs + - opt_enable RA_CONTROL_PANEL PINS_DEBUGGING + - build_marlin + # + ######## I2C LCD/PANELS ############## + # + # !!!ATTENTION!!! + # Most I2C configurations are failing at the moment because they require + # a different Liquid Crystal library "LiquidTWI2". + # + # LCD_I2C_SAINSMART_YWROBOT + # + #- restore_configs + #- opt_enable LCD_I2C_SAINSMART_YWROBOT + #- build_marlin + # + # LCD_I2C_PANELOLU2 + # + #- restore_configs + #- opt_enable LCD_I2C_PANELOLU2 + #- build_marlin + # + # LCD_I2C_VIKI + # + #- restore_configs + #- opt_enable LCD_I2C_VIKI + #- build_marlin + # + # LCM1602 + # + - restore_configs + - opt_enable LCM1602 + - build_marlin + # + # + ######## Example Configurations ############## + # + # BQ Hephestos 2 + #- restore_configs + #- use_example_configs Hephestos_2 + #- build_marlin + # + # Delta Config (generic) + ABL bilinear + PROBE_MANUALLY + - use_example_configs delta/generic + - opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER DELTA_CALIBRATION_MENU AUTO_BED_LEVELING_BILINEAR PROBE_MANUALLY + - build_marlin + # + # Delta Config (generic) + UBL + ALLEN_KEY + OLED_PANEL_TINYBOY2 + EEPROM_SETTINGS + # + - use_example_configs delta/generic + - opt_disable DISABLE_MIN_ENDSTOPS + - opt_enable AUTO_BED_LEVELING_UBL Z_PROBE_ALLEN_KEY EEPROM_SETTINGS EEPROM_CHITCHAT OLED_PANEL_TINYBOY2 + - build_marlin + # + # Delta Config (FLSUN AC because it's complex) + # + - use_example_configs delta/FLSUN/auto_calibrate + - build_marlin + # + # Makibox Config need to check board type for Teensy++ 2.0 + # + #- use_example_configs makibox + #- build_marlin + # + # SCARA with TMC2130 + # + - use_example_configs SCARA + - opt_enable AUTO_BED_LEVELING_BILINEAR FIX_MOUNTED_PROBE USE_ZMIN_PLUG EEPROM_SETTINGS EEPROM_CHITCHAT ULTIMAKERCONTROLLER + - opt_enable_adv HAVE_TMC2130 X_IS_TMC2130 Y_IS_TMC2130 Z_IS_TMC2130 + - opt_enable_adv AUTOMATIC_CURRENT_CONTROL STEALTHCHOP HYBRID_THRESHOLD SENSORLESS_HOMING + - build_marlin + # + # tvrrug Config need to check board type for sanguino atmega644p + # + #- use_example_configs tvrrug/Round2 + #- build_marlin + # + # + ######## Board Types ############# + # + # To be added in nightly test branch + # diff --git a/Marlin-bugfix-1.1.x-trigorilla/LICENSE b/Marlin-bugfix-1.1.x-trigorilla/LICENSE new file mode 100644 index 0000000..f27031a --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/LICENSE @@ -0,0 +1,677 @@ + + + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. + diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals.h new file mode 100644 index 0000000..ff6c6b1 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals.h @@ -0,0 +1,27 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Conditionals.h + * OBSOLETE: Replaced by Conditionals_LCD.h and Conditionals_post.h + */ +#error "Old configurations? Please delete all #include lines from Configuration.h and Configuration_adv.h." diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_LCD.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_LCD.h new file mode 100644 index 0000000..71bb113 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_LCD.h @@ -0,0 +1,473 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Conditionals_LCD.h + * Conditionals that need to be set before Configuration_adv.h or pins.h + */ + +#ifndef CONDITIONALS_LCD_H // Get the LCD defines which are needed first +#define CONDITIONALS_LCD_H + + #define LCD_HAS_DIRECTIONAL_BUTTONS (BUTTON_EXISTS(UP) || BUTTON_EXISTS(DWN) || BUTTON_EXISTS(LFT) || BUTTON_EXISTS(RT)) + + #if ENABLED(CARTESIO_UI) + + #define DOGLCD + #define ULTIPANEL + #define DEFAULT_LCD_CONTRAST 90 + #define LCD_CONTRAST_MIN 60 + #define LCD_CONTRAST_MAX 140 + + #elif ENABLED(MAKRPANEL) + + #define U8GLIB_ST7565_64128N + + #elif ENABLED(ANET_KEYPAD_LCD) + + #define REPRAPWORLD_KEYPAD + #define REPRAPWORLD_KEYPAD_MOVE_STEP 10.0 + #define ADC_KEYPAD + #define ADC_KEY_NUM 8 + #define ULTIPANEL + + // this helps to implement ADC_KEYPAD menus + #define ENCODER_PULSES_PER_STEP 1 + #define ENCODER_STEPS_PER_MENU_ITEM 1 + #define ENCODER_FEEDRATE_DEADZONE 2 + #define REVERSE_MENU_DIRECTION + + #elif ENABLED(ANET_FULL_GRAPHICS_LCD) + + #define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER + + #elif ENABLED(BQ_LCD_SMART_CONTROLLER) + + #define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER + + #elif ENABLED(miniVIKI) || ENABLED(VIKI2) || ENABLED(ELB_FULL_GRAPHIC_CONTROLLER) + + #define ULTRA_LCD //general LCD support, also 16x2 + #define DOGLCD // Support for SPI LCD 128x64 (Controller ST7565R graphic Display Family) + #define ULTIMAKERCONTROLLER //as available from the Ultimaker online store. + + #if ENABLED(miniVIKI) + #define LCD_CONTRAST_MIN 75 + #define LCD_CONTRAST_MAX 115 + #define DEFAULT_LCD_CONTRAST 95 + #define U8GLIB_ST7565_64128N + #elif ENABLED(VIKI2) + #define LCD_CONTRAST_MIN 0 + #define LCD_CONTRAST_MAX 255 + #define DEFAULT_LCD_CONTRAST 140 + #define U8GLIB_ST7565_64128N + #elif ENABLED(ELB_FULL_GRAPHIC_CONTROLLER) + #define LCD_CONTRAST_MIN 90 + #define LCD_CONTRAST_MAX 130 + #define DEFAULT_LCD_CONTRAST 110 + #define U8GLIB_LM6059_AF + #define SD_DETECT_INVERTED + #endif + + #elif ENABLED(OLED_PANEL_TINYBOY2) + + #define U8GLIB_SSD1306 + #define ULTIPANEL + #define REVERSE_ENCODER_DIRECTION + #define REVERSE_MENU_DIRECTION + + #elif ENABLED(RA_CONTROL_PANEL) + + #define LCD_I2C_TYPE_PCA8574 + #define LCD_I2C_ADDRESS 0x27 // I2C Address of the port expander + #define ULTIPANEL + + #elif ENABLED(REPRAPWORLD_GRAPHICAL_LCD) + + #define DOGLCD + #define U8GLIB_ST7920 + #define ULTIPANEL + + #elif ENABLED(CR10_STOCKDISPLAY) + + #define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER + #ifndef ST7920_DELAY_1 + #define ST7920_DELAY_1 DELAY_2_NOP + #endif + #ifndef ST7920_DELAY_2 + #define ST7920_DELAY_2 DELAY_2_NOP + #endif + #ifndef ST7920_DELAY_3 + #define ST7920_DELAY_3 DELAY_2_NOP + #endif + + #elif ENABLED(MKS_12864OLED) + + #define REPRAP_DISCOUNT_SMART_CONTROLLER + #define U8GLIB_SH1106 + + #elif ENABLED(MKS_MINI_12864) + + #define MINIPANEL + + #endif + + #if ENABLED(MAKRPANEL) || ENABLED(MINIPANEL) + #define DOGLCD + #define ULTIPANEL + #define DEFAULT_LCD_CONTRAST 17 + #endif + + // Generic support for SSD1306 / SH1106 OLED based LCDs. + #if ENABLED(U8GLIB_SSD1306) || ENABLED(U8GLIB_SH1106) + #define ULTRA_LCD //general LCD support, also 16x2 + #define DOGLCD // Support for I2C LCD 128x64 (Controller SSD1306 / SH1106 graphic Display Family) + #endif + + #if ENABLED(PANEL_ONE) || ENABLED(U8GLIB_SH1106) + + #define ULTIMAKERCONTROLLER + + #elif ENABLED(MAKEBOARD_MINI_2_LINE_DISPLAY_1602) + + #define REPRAP_DISCOUNT_SMART_CONTROLLER + #define LCD_WIDTH 16 + #define LCD_HEIGHT 2 + + #endif + + #if ENABLED(REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER) || ENABLED(LCD_FOR_MELZI) || ENABLED(SILVER_GATE_GLCD_CONTROLLER) + #define DOGLCD + #define U8GLIB_ST7920 + #define REPRAP_DISCOUNT_SMART_CONTROLLER + #endif + + #if ENABLED(ULTIMAKERCONTROLLER) \ + || ENABLED(REPRAP_DISCOUNT_SMART_CONTROLLER) \ + || ENABLED(G3D_PANEL) \ + || ENABLED(RIGIDBOT_PANEL) + #define ULTIPANEL + #endif + + #if ENABLED(REPRAPWORLD_KEYPAD) + #define NEWPANEL + #if ENABLED(ULTIPANEL) && !defined(REPRAPWORLD_KEYPAD_MOVE_STEP) + #define REPRAPWORLD_KEYPAD_MOVE_STEP 1.0 + #endif + #endif + + /** + * I2C PANELS + */ + + #if ENABLED(LCD_I2C_SAINSMART_YWROBOT) + + // Note: This controller requires F.Malpartida's LiquidCrystal_I2C library + // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home + + #define LCD_I2C_TYPE_PCF8575 + #define LCD_I2C_ADDRESS 0x27 // I2C Address of the port expander + #define ULTIPANEL + + #elif ENABLED(LCD_I2C_PANELOLU2) + + // PANELOLU2 LCD with status LEDs, separate encoder and click inputs + + #define LCD_I2C_TYPE_MCP23017 + #define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander + #define LCD_USE_I2C_BUZZER //comment out to disable buzzer on LCD + #define ULTIPANEL + + #elif ENABLED(LCD_I2C_VIKI) + + /** + * Panucatt VIKI LCD with status LEDs, integrated click & L/R/U/P buttons, separate encoder inputs + * + * This uses the LiquidTWI2 library v1.2.3 or later ( https://github.com/lincomatic/LiquidTWI2 ) + * Make sure the LiquidTWI2 directory is placed in the Arduino or Sketchbook libraries subdirectory. + * Note: The pause/stop/resume LCD button pin should be connected to the Arduino + * BTN_ENC pin (or set BTN_ENC to -1 if not used) + */ + #define LCD_I2C_TYPE_MCP23017 + #define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander + #define LCD_USE_I2C_BUZZER //comment out to disable buzzer on LCD (requires LiquidTWI2 v1.2.3 or later) + #define ULTIPANEL + + #define ENCODER_FEEDRATE_DEADZONE 4 + + #define STD_ENCODER_PULSES_PER_STEP 1 + #define STD_ENCODER_STEPS_PER_MENU_ITEM 2 + + #elif ENABLED(G3D_PANEL) + + #define STD_ENCODER_PULSES_PER_STEP 2 + #define STD_ENCODER_STEPS_PER_MENU_ITEM 1 + + #elif ENABLED(miniVIKI) || ENABLED(VIKI2) \ + || ENABLED(ELB_FULL_GRAPHIC_CONTROLLER) \ + || ENABLED(OLED_PANEL_TINYBOY2) \ + || ENABLED(BQ_LCD_SMART_CONTROLLER) \ + || ENABLED(LCD_I2C_PANELOLU2) \ + || ENABLED(REPRAP_DISCOUNT_SMART_CONTROLLER) + #define STD_ENCODER_PULSES_PER_STEP 4 + #define STD_ENCODER_STEPS_PER_MENU_ITEM 1 + #endif + + #ifndef STD_ENCODER_PULSES_PER_STEP + #define STD_ENCODER_PULSES_PER_STEP 5 + #endif + #ifndef STD_ENCODER_STEPS_PER_MENU_ITEM + #define STD_ENCODER_STEPS_PER_MENU_ITEM 1 + #endif + #ifndef ENCODER_PULSES_PER_STEP + #define ENCODER_PULSES_PER_STEP STD_ENCODER_PULSES_PER_STEP + #endif + #ifndef ENCODER_STEPS_PER_MENU_ITEM + #define ENCODER_STEPS_PER_MENU_ITEM STD_ENCODER_STEPS_PER_MENU_ITEM + #endif + #ifndef ENCODER_FEEDRATE_DEADZONE + #define ENCODER_FEEDRATE_DEADZONE 6 + #endif + + // Shift register panels + // --------------------- + // 2 wire Non-latching LCD SR from: + // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/schematics#!shiftregister-connection + + #if ENABLED(SAV_3DLCD) + #define SR_LCD_2W_NL // Non latching 2 wire shift register + #define ULTIPANEL + #endif + + #if ENABLED(DOGLCD) // Change number of lines to match the DOG graphic display + #ifndef LCD_WIDTH + #define LCD_WIDTH 22 + #endif + #ifndef LCD_HEIGHT + #define LCD_HEIGHT 5 + #endif + #endif + + #if ENABLED(ULTIPANEL) + #define NEWPANEL // Disable this if you actually have no click-encoder panel + #define ULTRA_LCD + #ifndef LCD_WIDTH + #define LCD_WIDTH 20 + #endif + #ifndef LCD_HEIGHT + #define LCD_HEIGHT 4 + #endif + #elif ENABLED(ULTRA_LCD) // no panel but just LCD + #ifndef LCD_WIDTH + #define LCD_WIDTH 16 + #endif + #ifndef LCD_HEIGHT + #define LCD_HEIGHT 2 + #endif + #endif + + #if ENABLED(DOGLCD) + /* Custom characters defined in font dogm_font_data_Marlin_symbols.h / Marlin_symbols.fon */ + // \x00 intentionally skipped to avoid problems in strings + #define LCD_STR_REFRESH "\x01" + #define LCD_STR_FOLDER "\x02" + #define LCD_STR_ARROW_RIGHT "\x03" + #define LCD_STR_UPLEVEL "\x04" + #define LCD_STR_CLOCK "\x05" + #define LCD_STR_FEEDRATE "\x06" + #define LCD_STR_BEDTEMP "\x07" + #define LCD_STR_THERMOMETER "\x08" + #define LCD_STR_DEGREE "\x09" + + #define LCD_STR_SPECIAL_MAX '\x09' + // Maximum here is 0x1F because 0x20 is ' ' (space) and the normal charsets begin. + // Better stay below 0x10 because DISPLAY_CHARSET_HD44780_WESTERN begins here. + + // Symbol characters + #define LCD_STR_FILAM_DIA "\xf8" + #define LCD_STR_FILAM_MUL "\xa4" + #else + /* Custom characters defined in the first 8 characters of the LCD */ + #define LCD_BEDTEMP_CHAR 0x00 // Print only as a char. This will have 'unexpected' results when used in a string! + #define LCD_DEGREE_CHAR 0x01 + #define LCD_STR_THERMOMETER "\x02" // Still used with string concatenation + #define LCD_UPLEVEL_CHAR 0x03 + #define LCD_STR_REFRESH "\x04" + #define LCD_STR_FOLDER "\x05" + #define LCD_FEEDRATE_CHAR 0x06 + #define LCD_CLOCK_CHAR 0x07 + #define LCD_STR_ARROW_RIGHT ">" /* from the default character set */ + #endif + + /** + * Default LCD contrast for dogm-like LCD displays + */ + #if ENABLED(DOGLCD) + + #define HAS_LCD_CONTRAST ( \ + ENABLED(MAKRPANEL) \ + || ENABLED(CARTESIO_UI) \ + || ENABLED(VIKI2) \ + || ENABLED(miniVIKI) \ + || ENABLED(ELB_FULL_GRAPHIC_CONTROLLER) \ + ) + + #if HAS_LCD_CONTRAST + #ifndef LCD_CONTRAST_MIN + #define LCD_CONTRAST_MIN 0 + #endif + #ifndef LCD_CONTRAST_MAX + #define LCD_CONTRAST_MAX 63 + #endif + #ifndef DEFAULT_LCD_CONTRAST + #define DEFAULT_LCD_CONTRAST 32 + #endif + #endif + #endif + + // Boot screens + #if DISABLED(ULTRA_LCD) + #undef SHOW_BOOTSCREEN + #elif !defined(BOOTSCREEN_TIMEOUT) + #define BOOTSCREEN_TIMEOUT 2500 + #endif + + #define HAS_DEBUG_MENU ENABLED(LCD_PROGRESS_BAR_TEST) + + // MK2 Multiplexer forces SINGLENOZZLE to be enabled + #if ENABLED(MK2_MULTIPLEXER) + #define SINGLENOZZLE + #endif + + /** + * Extruders have some combination of stepper motors and hotends + * so we separate these concepts into the defines: + * + * EXTRUDERS - Number of Selectable Tools + * HOTENDS - Number of hotends, whether connected or separate + * E_STEPPERS - Number of actual E stepper motors + * E_MANUAL - Number of E steppers for LCD move options + * TOOL_E_INDEX - Index to use when getting/setting the tool state + * + */ + #if ENABLED(SINGLENOZZLE) || ENABLED(MIXING_EXTRUDER) // One hotend, one thermistor, no XY offset + #define HOTENDS 1 + #undef TEMP_SENSOR_1_AS_REDUNDANT + #undef HOTEND_OFFSET_X + #undef HOTEND_OFFSET_Y + #else // Two hotends + #define HOTENDS EXTRUDERS + #if ENABLED(SWITCHING_NOZZLE) && !defined(HOTEND_OFFSET_Z) + #define HOTEND_OFFSET_Z { 0 } + #endif + #endif + + #if ENABLED(SWITCHING_EXTRUDER) || ENABLED(MIXING_EXTRUDER) // Unified E axis + #if ENABLED(MIXING_EXTRUDER) + #define E_STEPPERS MIXING_STEPPERS + #else + #define E_STEPPERS 1 // One E stepper + #endif + #define E_MANUAL 1 + #define TOOL_E_INDEX 0 + #else + #define E_STEPPERS EXTRUDERS + #define E_MANUAL EXTRUDERS + #define TOOL_E_INDEX current_block->active_extruder + #endif + + /** + * DISTINCT_E_FACTORS affects how some E factors are accessed + */ + #if ENABLED(DISTINCT_E_FACTORS) && E_STEPPERS > 1 + #define XYZE_N (XYZ + E_STEPPERS) + #define E_AXIS_N (E_AXIS + extruder) + #else + #undef DISTINCT_E_FACTORS + #define XYZE_N XYZE + #define E_AXIS_N E_AXIS + #endif + + /** + * The BLTouch Probe emulates a servo probe + * and uses "special" angles for its state. + */ + #if ENABLED(BLTOUCH) + #ifndef Z_ENDSTOP_SERVO_NR + #define Z_ENDSTOP_SERVO_NR 0 + #endif + #ifndef NUM_SERVOS + #define NUM_SERVOS (Z_ENDSTOP_SERVO_NR + 1) + #endif + #undef DEACTIVATE_SERVOS_AFTER_MOVE + #if NUM_SERVOS == 1 + #undef SERVO_DELAY + #define SERVO_DELAY { 50 } + #endif + #ifndef BLTOUCH_DELAY + #define BLTOUCH_DELAY 375 + #endif + #undef Z_SERVO_ANGLES + #define Z_SERVO_ANGLES { BLTOUCH_DEPLOY, BLTOUCH_STOW } + + #define BLTOUCH_DEPLOY 10 + #define BLTOUCH_STOW 90 + #define BLTOUCH_SELFTEST 120 + #define BLTOUCH_RESET 160 + #define _TEST_BLTOUCH(P) (READ(P##_PIN) != P##_ENDSTOP_INVERTING) + + // Always disable probe pin inverting for BLTouch + #undef Z_MIN_PROBE_ENDSTOP_INVERTING + #define Z_MIN_PROBE_ENDSTOP_INVERTING false + + #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) + #undef Z_MIN_ENDSTOP_INVERTING + #define Z_MIN_ENDSTOP_INVERTING false + #define TEST_BLTOUCH() _TEST_BLTOUCH(Z_MIN) + #else + #define TEST_BLTOUCH() _TEST_BLTOUCH(Z_MIN_PROBE) + #endif + #endif + + /** + * Set a flag for a servo probe + */ + #define HAS_Z_SERVO_ENDSTOP (defined(Z_ENDSTOP_SERVO_NR) && Z_ENDSTOP_SERVO_NR >= 0) + + /** + * Set a flag for any enabled probe + */ + #define PROBE_SELECTED (ENABLED(PROBE_MANUALLY) || ENABLED(FIX_MOUNTED_PROBE) || ENABLED(Z_PROBE_ALLEN_KEY) || HAS_Z_SERVO_ENDSTOP || ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) + + /** + * Clear probe pin settings when no probe is selected + */ + #if !PROBE_SELECTED || ENABLED(PROBE_MANUALLY) + #undef Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN + #undef Z_MIN_PROBE_ENDSTOP + #endif + + #define HAS_SOFTWARE_ENDSTOPS (ENABLED(MIN_SOFTWARE_ENDSTOPS) || ENABLED(MAX_SOFTWARE_ENDSTOPS)) + #define HAS_RESUME_CONTINUE (ENABLED(NEWPANEL) || ENABLED(EMERGENCY_PARSER)) + #define HAS_COLOR_LEDS (ENABLED(BLINKM) || ENABLED(RGB_LED) || ENABLED(RGBW_LED) || ENABLED(PCA9632) || ENABLED(NEOPIXEL_LED)) + +#endif // CONDITIONALS_LCD_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_post.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_post.h new file mode 100644 index 0000000..7897066 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Conditionals_post.h @@ -0,0 +1,1082 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Conditionals_post.h + * Defines that depend on configuration but are not editable. + */ + +#ifndef CONDITIONALS_POST_H +#define CONDITIONALS_POST_H + + #define IS_SCARA (ENABLED(MORGAN_SCARA) || ENABLED(MAKERARM_SCARA)) + #define IS_KINEMATIC (ENABLED(DELTA) || IS_SCARA) + #define IS_CARTESIAN !IS_KINEMATIC + + /** + * Axis lengths and center + */ + #define X_MAX_LENGTH (X_MAX_POS - (X_MIN_POS)) + #define Y_MAX_LENGTH (Y_MAX_POS - (Y_MIN_POS)) + #define Z_MAX_LENGTH (Z_MAX_POS - (Z_MIN_POS)) + + // Defined only if the sanity-check is bypassed + #ifndef X_BED_SIZE + #define X_BED_SIZE X_MAX_LENGTH + #endif + #ifndef Y_BED_SIZE + #define Y_BED_SIZE Y_MAX_LENGTH + #endif + + // Require 0,0 bed center for Delta and SCARA + #if IS_KINEMATIC + #define BED_CENTER_AT_0_0 + #endif + + // Define center values for future use + #if ENABLED(BED_CENTER_AT_0_0) + #define X_CENTER 0 + #define Y_CENTER 0 + #else + #define X_CENTER ((X_BED_SIZE) / 2) + #define Y_CENTER ((Y_BED_SIZE) / 2) + #endif + #define Z_CENTER ((Z_MIN_POS + Z_MAX_POS) / 2) + + // Get the linear boundaries of the bed + #define X_MIN_BED (X_CENTER - (X_BED_SIZE) / 2) + #define X_MAX_BED (X_CENTER + (X_BED_SIZE) / 2) + #define Y_MIN_BED (Y_CENTER - (Y_BED_SIZE) / 2) + #define Y_MAX_BED (Y_CENTER + (Y_BED_SIZE) / 2) + + /** + * CoreXY, CoreXZ, and CoreYZ - and their reverse + */ + #define CORE_IS_XY (ENABLED(COREXY) || ENABLED(COREYX)) + #define CORE_IS_XZ (ENABLED(COREXZ) || ENABLED(COREZX)) + #define CORE_IS_YZ (ENABLED(COREYZ) || ENABLED(COREZY)) + #define IS_CORE (CORE_IS_XY || CORE_IS_XZ || CORE_IS_YZ) + #if IS_CORE + #if CORE_IS_XY + #define CORE_AXIS_1 A_AXIS + #define CORE_AXIS_2 B_AXIS + #define NORMAL_AXIS Z_AXIS + #elif CORE_IS_XZ + #define CORE_AXIS_1 A_AXIS + #define NORMAL_AXIS Y_AXIS + #define CORE_AXIS_2 C_AXIS + #elif CORE_IS_YZ + #define NORMAL_AXIS X_AXIS + #define CORE_AXIS_1 B_AXIS + #define CORE_AXIS_2 C_AXIS + #endif + #if (ENABLED(COREYX) || ENABLED(COREZX) || ENABLED(COREZY)) + #define CORESIGN(n) (-(n)) + #else + #define CORESIGN(n) (n) + #endif + #endif + + /** + * No adjustable bed on non-cartesians + */ + #if IS_KINEMATIC + #undef LEVEL_BED_CORNERS + #endif + + /** + * SCARA cannot use SLOWDOWN and requires QUICKHOME + */ + #if IS_SCARA + #undef SLOWDOWN + #define QUICK_HOME + #endif + + /** + * Set the home position based on settings or manual overrides + */ + #ifdef MANUAL_X_HOME_POS + #define X_HOME_POS MANUAL_X_HOME_POS + #elif ENABLED(BED_CENTER_AT_0_0) + #if ENABLED(DELTA) + #define X_HOME_POS 0 + #else + #define X_HOME_POS ((X_BED_SIZE) * (X_HOME_DIR) * 0.5) + #endif + #else + #if ENABLED(DELTA) + #define X_HOME_POS (X_MIN_POS + (X_BED_SIZE) * 0.5) + #else + #define X_HOME_POS (X_HOME_DIR < 0 ? X_MIN_POS : X_MAX_POS) + #endif + #endif + + #ifdef MANUAL_Y_HOME_POS + #define Y_HOME_POS MANUAL_Y_HOME_POS + #elif ENABLED(BED_CENTER_AT_0_0) + #if ENABLED(DELTA) + #define Y_HOME_POS 0 + #else + #define Y_HOME_POS ((Y_BED_SIZE) * (Y_HOME_DIR) * 0.5) + #endif + #else + #if ENABLED(DELTA) + #define Y_HOME_POS (Y_MIN_POS + (Y_BED_SIZE) * 0.5) + #else + #define Y_HOME_POS (Y_HOME_DIR < 0 ? Y_MIN_POS : Y_MAX_POS) + #endif + #endif + + #ifdef MANUAL_Z_HOME_POS + #define Z_HOME_POS MANUAL_Z_HOME_POS + #else + #define Z_HOME_POS (Z_HOME_DIR < 0 ? Z_MIN_POS : Z_MAX_POS) + #endif + + /** + * If DELTA_HEIGHT isn't defined use the old setting + */ + #if ENABLED(DELTA) && !defined(DELTA_HEIGHT) + #define DELTA_HEIGHT Z_HOME_POS + #endif + + /** + * Auto Bed Leveling and Z Probe Repeatability Test + */ + #define HOMING_Z_WITH_PROBE (HAS_BED_PROBE && Z_HOME_DIR < 0 && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)) + + /** + * Z Sled Probe requires Z_SAFE_HOMING + */ + #if ENABLED(Z_PROBE_SLED) + #define Z_SAFE_HOMING + #endif + + /** + * DELTA should ignore Z_SAFE_HOMING and SLOWDOWN + */ + #if ENABLED(DELTA) + #undef Z_SAFE_HOMING + #undef SLOWDOWN + #endif + + /** + * Safe Homing Options + */ + #if ENABLED(Z_SAFE_HOMING) + #ifndef Z_SAFE_HOMING_X_POINT + #define Z_SAFE_HOMING_X_POINT X_CENTER + #endif + #ifndef Z_SAFE_HOMING_Y_POINT + #define Z_SAFE_HOMING_Y_POINT Y_CENTER + #endif + #define X_TILT_FULCRUM Z_SAFE_HOMING_X_POINT + #define Y_TILT_FULCRUM Z_SAFE_HOMING_Y_POINT + #else + #define X_TILT_FULCRUM X_HOME_POS + #define Y_TILT_FULCRUM Y_HOME_POS + #endif + + /** + * Host keep alive + */ + #ifndef DEFAULT_KEEPALIVE_INTERVAL + #define DEFAULT_KEEPALIVE_INTERVAL 2 + #endif + + /** + * Provide a MAX_AUTORETRACT for older configs + */ + #if ENABLED(FWRETRACT) && !defined(MAX_AUTORETRACT) + #define MAX_AUTORETRACT 99 + #endif + + /** + * MAX_STEP_FREQUENCY differs for TOSHIBA + */ + #if ENABLED(CONFIG_STEPPERS_TOSHIBA) + #define MAX_STEP_FREQUENCY 10000 // Max step frequency for Toshiba Stepper Controllers + #else + #define MAX_STEP_FREQUENCY 40000 // Max step frequency for Ultimaker (5000 pps / half step) + #endif + + // MS1 MS2 Stepper Driver Microstepping mode table + #define MICROSTEP1 LOW,LOW + #define MICROSTEP2 HIGH,LOW + #define MICROSTEP4 LOW,HIGH + #define MICROSTEP8 HIGH,HIGH + #define MICROSTEP16 HIGH,HIGH + + /** + * Override here because this is set in Configuration_adv.h + */ + #if ENABLED(ULTIPANEL) && DISABLED(ELB_FULL_GRAPHIC_CONTROLLER) + #undef SD_DETECT_INVERTED + #endif + + /** + * Set defaults for missing (newer) options + */ + #ifndef DISABLE_INACTIVE_X + #define DISABLE_INACTIVE_X DISABLE_X + #endif + #ifndef DISABLE_INACTIVE_Y + #define DISABLE_INACTIVE_Y DISABLE_Y + #endif + #ifndef DISABLE_INACTIVE_Z + #define DISABLE_INACTIVE_Z DISABLE_Z + #endif + #ifndef DISABLE_INACTIVE_E + #define DISABLE_INACTIVE_E DISABLE_E + #endif + + // Power Signal Control Definitions + // By default use ATX definition + #ifndef POWER_SUPPLY + #define POWER_SUPPLY 1 + #endif + #if (POWER_SUPPLY == 1) // 1 = ATX + #define PS_ON_AWAKE LOW + #define PS_ON_ASLEEP HIGH + #elif (POWER_SUPPLY == 2) // 2 = X-Box 360 203W + #define PS_ON_AWAKE HIGH + #define PS_ON_ASLEEP LOW + #endif + #define HAS_POWER_SWITCH (POWER_SUPPLY > 0 && PIN_EXISTS(PS_ON)) + + /** + * Temp Sensor defines + */ + #if TEMP_SENSOR_0 == -3 + #define HEATER_0_USES_MAX6675 + #define MAX6675_IS_MAX31855 + #define MAX6675_TMIN -270 + #define MAX6675_TMAX 1800 + #elif TEMP_SENSOR_0 == -2 + #define HEATER_0_USES_MAX6675 + #define MAX6675_TMIN 0 + #define MAX6675_TMAX 1024 + #elif TEMP_SENSOR_0 == -1 + #define HEATER_0_USES_AD595 + #elif TEMP_SENSOR_0 == 0 + #undef HEATER_0_MINTEMP + #undef HEATER_0_MAXTEMP + #elif TEMP_SENSOR_0 > 0 + #define THERMISTORHEATER_0 TEMP_SENSOR_0 + #define HEATER_0_USES_THERMISTOR + #endif + + #if TEMP_SENSOR_1 <= -2 + #error "MAX6675 / MAX31855 Thermocouples not supported for TEMP_SENSOR_1" + #elif TEMP_SENSOR_1 == -1 + #define HEATER_1_USES_AD595 + #elif TEMP_SENSOR_1 == 0 + #undef HEATER_1_MINTEMP + #undef HEATER_1_MAXTEMP + #elif TEMP_SENSOR_1 > 0 + #define THERMISTORHEATER_1 TEMP_SENSOR_1 + #define HEATER_1_USES_THERMISTOR + #endif + + #if TEMP_SENSOR_2 <= -2 + #error "MAX6675 / MAX31855 Thermocouples not supported for TEMP_SENSOR_2" + #elif TEMP_SENSOR_2 == -1 + #define HEATER_2_USES_AD595 + #elif TEMP_SENSOR_2 == 0 + #undef HEATER_2_MINTEMP + #undef HEATER_2_MAXTEMP + #elif TEMP_SENSOR_2 > 0 + #define THERMISTORHEATER_2 TEMP_SENSOR_2 + #define HEATER_2_USES_THERMISTOR + #endif + + #if TEMP_SENSOR_3 <= -2 + #error "MAX6675 / MAX31855 Thermocouples not supported for TEMP_SENSOR_3" + #elif TEMP_SENSOR_3 == -1 + #define HEATER_3_USES_AD595 + #elif TEMP_SENSOR_3 == 0 + #undef HEATER_3_MINTEMP + #undef HEATER_3_MAXTEMP + #elif TEMP_SENSOR_3 > 0 + #define THERMISTORHEATER_3 TEMP_SENSOR_3 + #define HEATER_3_USES_THERMISTOR + #endif + + #if TEMP_SENSOR_4 <= -2 + #error "MAX6675 / MAX31855 Thermocouples not supported for TEMP_SENSOR_4" + #elif TEMP_SENSOR_4 == -1 + #define HEATER_4_USES_AD595 + #elif TEMP_SENSOR_4 == 0 + #undef HEATER_4_MINTEMP + #undef HEATER_4_MAXTEMP + #elif TEMP_SENSOR_4 > 0 + #define THERMISTORHEATER_4 TEMP_SENSOR_4 + #define HEATER_4_USES_THERMISTOR + #endif + + #if TEMP_SENSOR_BED <= -2 + #error "MAX6675 / MAX31855 Thermocouples not supported for TEMP_SENSOR_BED" + #elif TEMP_SENSOR_BED == -1 + #define BED_USES_AD595 + #elif TEMP_SENSOR_BED == 0 + #undef BED_MINTEMP + #undef BED_MAXTEMP + #elif TEMP_SENSOR_BED > 0 + #define THERMISTORBED TEMP_SENSOR_BED + #define BED_USES_THERMISTOR + #endif + + /** + * Flags for PID handling + */ + #define HAS_PID_HEATING (ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)) + #define HAS_PID_FOR_BOTH (ENABLED(PIDTEMP) && ENABLED(PIDTEMPBED)) + + /** + * Default hotend offsets, if not defined + */ + #if HOTENDS > 1 + #ifndef HOTEND_OFFSET_X + #define HOTEND_OFFSET_X { 0 } // X offsets for each extruder + #endif + #ifndef HOTEND_OFFSET_Y + #define HOTEND_OFFSET_Y { 0 } // Y offsets for each extruder + #endif + #if !defined(HOTEND_OFFSET_Z) && (ENABLED(DUAL_X_CARRIAGE) || ENABLED(SWITCHING_NOZZLE)) + #define HOTEND_OFFSET_Z { 0 } + #endif + #endif + + /** + * ARRAY_BY_EXTRUDERS based on EXTRUDERS + */ + #define ARRAY_BY_EXTRUDERS(...) ARRAY_N(EXTRUDERS, __VA_ARGS__) + #define ARRAY_BY_EXTRUDERS1(v1) ARRAY_BY_EXTRUDERS(v1, v1, v1, v1, v1, v1) + + /** + * ARRAY_BY_HOTENDS based on HOTENDS + */ + #define ARRAY_BY_HOTENDS(...) ARRAY_N(HOTENDS, __VA_ARGS__) + #define ARRAY_BY_HOTENDS1(v1) ARRAY_BY_HOTENDS(v1, v1, v1, v1, v1, v1) + + /** + * X_DUAL_ENDSTOPS endstop reassignment + */ + #if ENABLED(X_DUAL_ENDSTOPS) + #if X_HOME_DIR > 0 + #if X2_USE_ENDSTOP == _XMIN_ + #define X2_MAX_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define X2_MAX_PIN X_MIN_PIN + #elif X2_USE_ENDSTOP == _XMAX_ + #define X2_MAX_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define X2_MAX_PIN X_MAX_PIN + #elif X2_USE_ENDSTOP == _YMIN_ + #define X2_MAX_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define X2_MAX_PIN Y_MIN_PIN + #elif X2_USE_ENDSTOP == _YMAX_ + #define X2_MAX_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define X2_MAX_PIN Y_MAX_PIN + #elif X2_USE_ENDSTOP == _ZMIN_ + #define X2_MAX_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define X2_MAX_PIN Z_MIN_PIN + #elif X2_USE_ENDSTOP == _ZMAX_ + #define X2_MAX_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define X2_MAX_PIN Z_MAX_PIN + #else + #define X2_MAX_ENDSTOP_INVERTING false + #endif + #define X2_MIN_ENDSTOP_INVERTING false + #else + #if X2_USE_ENDSTOP == _XMIN_ + #define X2_MIN_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define X2_MIN_PIN X_MIN_PIN + #elif X2_USE_ENDSTOP == _XMAX_ + #define X2_MIN_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define X2_MIN_PIN X_MAX_PIN + #elif X2_USE_ENDSTOP == _YMIN_ + #define X2_MIN_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define X2_MIN_PIN Y_MIN_PIN + #elif X2_USE_ENDSTOP == _YMAX_ + #define X2_MIN_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define X2_MIN_PIN Y_MAX_PIN + #elif X2_USE_ENDSTOP == _ZMIN_ + #define X2_MIN_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define X2_MIN_PIN Z_MIN_PIN + #elif X2_USE_ENDSTOP == _ZMAX_ + #define X2_MIN_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define X2_MIN_PIN Z_MAX_PIN + #else + #define X2_MIN_ENDSTOP_INVERTING false + #endif + #define X2_MAX_ENDSTOP_INVERTING false + #endif + #endif + + // Is an endstop plug used for the X2 endstop? + #define IS_X2_ENDSTOP(A,M) (ENABLED(X_DUAL_ENDSTOPS) && X2_USE_ENDSTOP == _##A##M##_) + + /** + * Y_DUAL_ENDSTOPS endstop reassignment + */ + #if ENABLED(Y_DUAL_ENDSTOPS) + #if Y_HOME_DIR > 0 + #if Y2_USE_ENDSTOP == _XMIN_ + #define Y2_MAX_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define Y2_MAX_PIN X_MIN_PIN + #elif Y2_USE_ENDSTOP == _XMAX_ + #define Y2_MAX_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define Y2_MAX_PIN X_MAX_PIN + #elif Y2_USE_ENDSTOP == _YMIN_ + #define Y2_MAX_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define Y2_MAX_PIN Y_MIN_PIN + #elif Y2_USE_ENDSTOP == _YMAX_ + #define Y2_MAX_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define Y2_MAX_PIN Y_MAX_PIN + #elif Y2_USE_ENDSTOP == _ZMIN_ + #define Y2_MAX_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define Y2_MAX_PIN Z_MIN_PIN + #elif Y2_USE_ENDSTOP == _ZMAX_ + #define Y2_MAX_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define Y2_MAX_PIN Z_MAX_PIN + #else + #define Y2_MAX_ENDSTOP_INVERTING false + #endif + #define Y2_MIN_ENDSTOP_INVERTING false + #else + #if Y2_USE_ENDSTOP == _XMIN_ + #define Y2_MIN_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define Y2_MIN_PIN X_MIN_PIN + #elif Y2_USE_ENDSTOP == _XMAX_ + #define Y2_MIN_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define Y2_MIN_PIN X_MAX_PIN + #elif Y2_USE_ENDSTOP == _YMIN_ + #define Y2_MIN_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define Y2_MIN_PIN Y_MIN_PIN + #elif Y2_USE_ENDSTOP == _YMAX_ + #define Y2_MIN_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define Y2_MIN_PIN Y_MAX_PIN + #elif Y2_USE_ENDSTOP == _ZMIN_ + #define Y2_MIN_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define Y2_MIN_PIN Z_MIN_PIN + #elif Y2_USE_ENDSTOP == _ZMAX_ + #define Y2_MIN_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define Y2_MIN_PIN Z_MAX_PIN + #else + #define Y2_MIN_ENDSTOP_INVERTING false + #endif + #define Y2_MAX_ENDSTOP_INVERTING false + #endif + #endif + + // Is an endstop plug used for the Y2 endstop or the bed probe? + #define IS_Y2_ENDSTOP(A,M) (ENABLED(Y_DUAL_ENDSTOPS) && Y2_USE_ENDSTOP == _##A##M##_) + + /** + * Z_DUAL_ENDSTOPS endstop reassignment + */ + #if ENABLED(Z_DUAL_ENDSTOPS) + #if Z_HOME_DIR > 0 + #if Z2_USE_ENDSTOP == _XMIN_ + #define Z2_MAX_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define Z2_MAX_PIN X_MIN_PIN + #elif Z2_USE_ENDSTOP == _XMAX_ + #define Z2_MAX_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define Z2_MAX_PIN X_MAX_PIN + #elif Z2_USE_ENDSTOP == _YMIN_ + #define Z2_MAX_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define Z2_MAX_PIN Y_MIN_PIN + #elif Z2_USE_ENDSTOP == _YMAX_ + #define Z2_MAX_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define Z2_MAX_PIN Y_MAX_PIN + #elif Z2_USE_ENDSTOP == _ZMIN_ + #define Z2_MAX_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define Z2_MAX_PIN Z_MIN_PIN + #elif Z2_USE_ENDSTOP == _ZMAX_ + #define Z2_MAX_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define Z2_MAX_PIN Z_MAX_PIN + #else + #define Z2_MAX_ENDSTOP_INVERTING false + #endif + #define Z2_MIN_ENDSTOP_INVERTING false + #else + #if Z2_USE_ENDSTOP == _XMIN_ + #define Z2_MIN_ENDSTOP_INVERTING X_MIN_ENDSTOP_INVERTING + #define Z2_MIN_PIN X_MIN_PIN + #elif Z2_USE_ENDSTOP == _XMAX_ + #define Z2_MIN_ENDSTOP_INVERTING X_MAX_ENDSTOP_INVERTING + #define Z2_MIN_PIN X_MAX_PIN + #elif Z2_USE_ENDSTOP == _YMIN_ + #define Z2_MIN_ENDSTOP_INVERTING Y_MIN_ENDSTOP_INVERTING + #define Z2_MIN_PIN Y_MIN_PIN + #elif Z2_USE_ENDSTOP == _YMAX_ + #define Z2_MIN_ENDSTOP_INVERTING Y_MAX_ENDSTOP_INVERTING + #define Z2_MIN_PIN Y_MAX_PIN + #elif Z2_USE_ENDSTOP == _ZMIN_ + #define Z2_MIN_ENDSTOP_INVERTING Z_MIN_ENDSTOP_INVERTING + #define Z2_MIN_PIN Z_MIN_PIN + #elif Z2_USE_ENDSTOP == _ZMAX_ + #define Z2_MIN_ENDSTOP_INVERTING Z_MAX_ENDSTOP_INVERTING + #define Z2_MIN_PIN Z_MAX_PIN + #else + #define Z2_MIN_ENDSTOP_INVERTING false + #endif + #define Z2_MAX_ENDSTOP_INVERTING false + #endif + #endif + + // Is an endstop plug used for the Z2 endstop or the bed probe? + #define IS_Z2_OR_PROBE(A,M) ( \ + (ENABLED(Z_DUAL_ENDSTOPS) && Z2_USE_ENDSTOP == _##A##M##_) \ + || (ENABLED(Z_MIN_PROBE_ENDSTOP) && Z_MIN_PROBE_PIN == A##_##M##_PIN ) ) + + /** + * Set ENDSTOPPULLUPS for active endstop switches + */ + #if ENABLED(ENDSTOPPULLUPS) + #if ENABLED(USE_XMAX_PLUG) + #define ENDSTOPPULLUP_XMAX + #endif + #if ENABLED(USE_YMAX_PLUG) + #define ENDSTOPPULLUP_YMAX + #endif + #if ENABLED(USE_ZMAX_PLUG) + #define ENDSTOPPULLUP_ZMAX + #endif + #if ENABLED(USE_XMIN_PLUG) + #define ENDSTOPPULLUP_XMIN + #endif + #if ENABLED(USE_YMIN_PLUG) + #define ENDSTOPPULLUP_YMIN + #endif + #if ENABLED(USE_ZMIN_PLUG) + #define ENDSTOPPULLUP_ZMIN + #endif + #endif + + /** + * Shorthand for pin tests, used wherever needed + */ + + // Steppers + #define HAS_X_ENABLE (PIN_EXISTS(X_ENABLE)) + #define HAS_X_DIR (PIN_EXISTS(X_DIR)) + #define HAS_X_STEP (PIN_EXISTS(X_STEP)) + #define HAS_X_MICROSTEPS (PIN_EXISTS(X_MS1)) + + #define HAS_X2_ENABLE (PIN_EXISTS(X2_ENABLE)) + #define HAS_X2_DIR (PIN_EXISTS(X2_DIR)) + #define HAS_X2_STEP (PIN_EXISTS(X2_STEP)) + #define HAS_Y_MICROSTEPS (PIN_EXISTS(Y_MS1)) + + #define HAS_Y_ENABLE (PIN_EXISTS(Y_ENABLE)) + #define HAS_Y_DIR (PIN_EXISTS(Y_DIR)) + #define HAS_Y_STEP (PIN_EXISTS(Y_STEP)) + #define HAS_Z_MICROSTEPS (PIN_EXISTS(Z_MS1)) + + #define HAS_Y2_ENABLE (PIN_EXISTS(Y2_ENABLE)) + #define HAS_Y2_DIR (PIN_EXISTS(Y2_DIR)) + #define HAS_Y2_STEP (PIN_EXISTS(Y2_STEP)) + + #define HAS_Z_ENABLE (PIN_EXISTS(Z_ENABLE)) + #define HAS_Z_DIR (PIN_EXISTS(Z_DIR)) + #define HAS_Z_STEP (PIN_EXISTS(Z_STEP)) + + #define HAS_Z2_ENABLE (PIN_EXISTS(Z2_ENABLE)) + #define HAS_Z2_DIR (PIN_EXISTS(Z2_DIR)) + #define HAS_Z2_STEP (PIN_EXISTS(Z2_STEP)) + + // Extruder steppers and solenoids + #define HAS_E0_ENABLE (PIN_EXISTS(E0_ENABLE)) + #define HAS_E0_DIR (PIN_EXISTS(E0_DIR)) + #define HAS_E0_STEP (PIN_EXISTS(E0_STEP)) + #define HAS_E0_MICROSTEPS (PIN_EXISTS(E0_MS1)) + #define HAS_SOLENOID_0 (PIN_EXISTS(SOL0)) + + #define HAS_E1_ENABLE (PIN_EXISTS(E1_ENABLE)) + #define HAS_E1_DIR (PIN_EXISTS(E1_DIR)) + #define HAS_E1_STEP (PIN_EXISTS(E1_STEP)) + #define HAS_E1_MICROSTEPS (PIN_EXISTS(E1_MS1)) + #define HAS_SOLENOID_1 (PIN_EXISTS(SOL1)) + + #define HAS_E2_ENABLE (PIN_EXISTS(E2_ENABLE)) + #define HAS_E2_DIR (PIN_EXISTS(E2_DIR)) + #define HAS_E2_STEP (PIN_EXISTS(E2_STEP)) + #define HAS_E2_MICROSTEPS (PIN_EXISTS(E2_MS1)) + #define HAS_SOLENOID_2 (PIN_EXISTS(SOL2)) + + #define HAS_E3_ENABLE (PIN_EXISTS(E3_ENABLE)) + #define HAS_E3_DIR (PIN_EXISTS(E3_DIR)) + #define HAS_E3_STEP (PIN_EXISTS(E3_STEP)) + #define HAS_E3_MICROSTEPS (PIN_EXISTS(E3_MS1)) + #define HAS_SOLENOID_3 (PIN_EXISTS(SOL3)) + + #define HAS_E4_ENABLE (PIN_EXISTS(E4_ENABLE)) + #define HAS_E4_DIR (PIN_EXISTS(E4_DIR)) + #define HAS_E4_STEP (PIN_EXISTS(E4_STEP)) + #define HAS_E4_MICROSTEPS (PIN_EXISTS(E4_MS1)) + #define HAS_SOLENOID_4 (PIN_EXISTS(SOL4)) + + // Endstops and bed probe + #define HAS_X_MIN (PIN_EXISTS(X_MIN) && !IS_X2_ENDSTOP(X,MIN) && !IS_Y2_ENDSTOP(X,MIN) && !IS_Z2_OR_PROBE(X,MIN)) + #define HAS_X_MAX (PIN_EXISTS(X_MAX) && !IS_X2_ENDSTOP(X,MAX) && !IS_Y2_ENDSTOP(X,MAX) && !IS_Z2_OR_PROBE(X,MAX)) + #define HAS_Y_MIN (PIN_EXISTS(Y_MIN) && !IS_X2_ENDSTOP(Y,MIN) && !IS_Y2_ENDSTOP(Y,MIN) && !IS_Z2_OR_PROBE(Y,MIN)) + #define HAS_Y_MAX (PIN_EXISTS(Y_MAX) && !IS_X2_ENDSTOP(Y,MAX) && !IS_Y2_ENDSTOP(Y,MAX) && !IS_Z2_OR_PROBE(Y,MAX)) + #define HAS_Z_MIN (PIN_EXISTS(Z_MIN) && !IS_X2_ENDSTOP(Z,MIN) && !IS_Y2_ENDSTOP(Z,MIN) && !IS_Z2_OR_PROBE(Z,MIN)) + #define HAS_Z_MAX (PIN_EXISTS(Z_MAX) && !IS_X2_ENDSTOP(Z,MAX) && !IS_Y2_ENDSTOP(Z,MAX) && !IS_Z2_OR_PROBE(Z,MAX)) + #define HAS_X2_MIN (PIN_EXISTS(X2_MIN)) + #define HAS_X2_MAX (PIN_EXISTS(X2_MAX)) + #define HAS_Y2_MIN (PIN_EXISTS(Y2_MIN)) + #define HAS_Y2_MAX (PIN_EXISTS(Y2_MAX)) + #define HAS_Z2_MIN (PIN_EXISTS(Z2_MIN)) + #define HAS_Z2_MAX (PIN_EXISTS(Z2_MAX)) + #define HAS_Z_MIN_PROBE_PIN (PIN_EXISTS(Z_MIN_PROBE)) + + // Thermistors + #define HAS_TEMP_0 (PIN_EXISTS(TEMP_0) && TEMP_SENSOR_0 != 0 && TEMP_SENSOR_0 > -2) + #define HAS_TEMP_1 (PIN_EXISTS(TEMP_1) && TEMP_SENSOR_1 != 0 && TEMP_SENSOR_1 > -2) + #define HAS_TEMP_2 (PIN_EXISTS(TEMP_2) && TEMP_SENSOR_2 != 0 && TEMP_SENSOR_2 > -2) + #define HAS_TEMP_3 (PIN_EXISTS(TEMP_3) && TEMP_SENSOR_3 != 0 && TEMP_SENSOR_3 > -2) + #define HAS_TEMP_4 (PIN_EXISTS(TEMP_4) && TEMP_SENSOR_4 != 0 && TEMP_SENSOR_4 > -2) + #define HAS_TEMP_HOTEND (HAS_TEMP_0 || ENABLED(HEATER_0_USES_MAX6675)) + #define HAS_TEMP_BED (PIN_EXISTS(TEMP_BED) && TEMP_SENSOR_BED != 0 && TEMP_SENSOR_BED > -2) + + // Heaters + #define HAS_HEATER_0 (PIN_EXISTS(HEATER_0)) + #define HAS_HEATER_1 (PIN_EXISTS(HEATER_1)) + #define HAS_HEATER_2 (PIN_EXISTS(HEATER_2)) + #define HAS_HEATER_3 (PIN_EXISTS(HEATER_3)) + #define HAS_HEATER_4 (PIN_EXISTS(HEATER_4)) + #define HAS_HEATER_BED (PIN_EXISTS(HEATER_BED)) + + // Thermal protection + #define HAS_THERMALLY_PROTECTED_BED (ENABLED(THERMAL_PROTECTION_BED) && HAS_TEMP_BED && HAS_HEATER_BED) + #define WATCH_HOTENDS (ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0) + #define WATCH_THE_BED (HAS_THERMALLY_PROTECTED_BED && WATCH_BED_TEMP_PERIOD > 0) + + // Auto fans + #define HAS_AUTO_FAN_0 (PIN_EXISTS(E0_AUTO_FAN)) + #define HAS_AUTO_FAN_1 (HOTENDS > 1 && PIN_EXISTS(E1_AUTO_FAN)) + #define HAS_AUTO_FAN_2 (HOTENDS > 2 && PIN_EXISTS(E2_AUTO_FAN)) + #define HAS_AUTO_FAN_3 (HOTENDS > 3 && PIN_EXISTS(E3_AUTO_FAN)) + #define HAS_AUTO_FAN_4 (HOTENDS > 4 && PIN_EXISTS(E4_AUTO_FAN)) + #define HAS_AUTO_FAN (HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3) + #define AUTO_1_IS_0 (E1_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_2_IS_0 (E2_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_2_IS_1 (E2_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_3_IS_0 (E3_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_3_IS_1 (E3_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_3_IS_2 (E3_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_4_IS_0 (E4_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_4_IS_1 (E4_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_4_IS_2 (E4_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_4_IS_3 (E4_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) + + // Other fans + #define HAS_FAN0 (PIN_EXISTS(FAN)) + #define HAS_FAN1 (PIN_EXISTS(FAN1) && CONTROLLER_FAN_PIN != FAN1_PIN && E0_AUTO_FAN_PIN != FAN1_PIN && E1_AUTO_FAN_PIN != FAN1_PIN && E2_AUTO_FAN_PIN != FAN1_PIN && E3_AUTO_FAN_PIN != FAN1_PIN) + #define HAS_FAN2 (PIN_EXISTS(FAN2) && CONTROLLER_FAN_PIN != FAN2_PIN && E0_AUTO_FAN_PIN != FAN2_PIN && E1_AUTO_FAN_PIN != FAN2_PIN && E2_AUTO_FAN_PIN != FAN2_PIN && E3_AUTO_FAN_PIN != FAN2_PIN) + #define HAS_CONTROLLER_FAN (PIN_EXISTS(CONTROLLER_FAN)) + + // Servos + #define HAS_SERVOS (defined(NUM_SERVOS) && NUM_SERVOS > 0) + #define HAS_SERVO_0 (PIN_EXISTS(SERVO0)) + #define HAS_SERVO_1 (PIN_EXISTS(SERVO1)) + #define HAS_SERVO_2 (PIN_EXISTS(SERVO2)) + #define HAS_SERVO_3 (PIN_EXISTS(SERVO3)) + + // Sensors + #define HAS_FILAMENT_WIDTH_SENSOR (PIN_EXISTS(FILWIDTH)) + #define HAS_FIL_RUNOUT (PIN_EXISTS(FIL_RUNOUT)) + + // User Interface + #define HAS_HOME (PIN_EXISTS(HOME)) + #define HAS_KILL (PIN_EXISTS(KILL)) + #define HAS_SUICIDE (PIN_EXISTS(SUICIDE)) + #define HAS_PHOTOGRAPH (PIN_EXISTS(PHOTOGRAPH)) + #define HAS_BUZZER (PIN_EXISTS(BEEPER) || ENABLED(LCD_USE_I2C_BUZZER)) + #define HAS_CASE_LIGHT (PIN_EXISTS(CASE_LIGHT) && ENABLED(CASE_LIGHT_ENABLE)) + + // Digital control + #define HAS_MICROSTEPS (HAS_X_MICROSTEPS || HAS_Y_MICROSTEPS || HAS_Z_MICROSTEPS || HAS_E0_MICROSTEPS || HAS_E1_MICROSTEPS || HAS_E2_MICROSTEPS || HAS_E3_MICROSTEPS || HAS_E4_MICROSTEPS) + #define HAS_STEPPER_RESET (PIN_EXISTS(STEPPER_RESET)) + #define HAS_DIGIPOTSS (PIN_EXISTS(DIGIPOTSS)) + #define HAS_MOTOR_CURRENT_PWM (PIN_EXISTS(MOTOR_CURRENT_PWM_XY) || PIN_EXISTS(MOTOR_CURRENT_PWM_Z) || PIN_EXISTS(MOTOR_CURRENT_PWM_E)) + + /** + * This setting is also used by M109 when trying to calculate + * a ballpark safe margin to prevent wait-forever situation. + */ + #ifndef EXTRUDE_MINTEMP + #define EXTRUDE_MINTEMP 170 + #endif + + /** + * Helper Macros for heaters and extruder fan + */ + #define WRITE_HEATER_0P(v) WRITE(HEATER_0_PIN, v) + #if HOTENDS > 1 || ENABLED(HEATERS_PARALLEL) + #define WRITE_HEATER_1(v) WRITE(HEATER_1_PIN, v) + #if HOTENDS > 2 + #define WRITE_HEATER_2(v) WRITE(HEATER_2_PIN, v) + #if HOTENDS > 3 + #define WRITE_HEATER_3(v) WRITE(HEATER_3_PIN, v) + #if HOTENDS > 4 + #define WRITE_HEATER_4(v) WRITE(HEATER_4_PIN, v) + #endif // HOTENDS > 4 + #endif // HOTENDS > 3 + #endif // HOTENDS > 2 + #endif // HOTENDS > 1 + #if ENABLED(HEATERS_PARALLEL) + #define WRITE_HEATER_0(v) { WRITE_HEATER_0P(v); WRITE_HEATER_1(v); } + #else + #define WRITE_HEATER_0(v) WRITE_HEATER_0P(v) + #endif + + /** + * Heated bed requires settings + */ + #if HAS_HEATER_BED + #ifndef MAX_BED_POWER + #define MAX_BED_POWER 255 + #endif + #ifndef HEATER_BED_INVERTING + #define HEATER_BED_INVERTING false + #endif + #define WRITE_HEATER_BED(v) WRITE(HEATER_BED_PIN, (v) ^ HEATER_BED_INVERTING) + #endif + + /** + * Up to 3 PWM fans + */ + #if HAS_FAN2 + #define FAN_COUNT 3 + #elif HAS_FAN1 + #define FAN_COUNT 2 + #elif HAS_FAN0 + #define FAN_COUNT 1 + #else + #define FAN_COUNT 0 + #endif + + #if HAS_FAN0 + #define WRITE_FAN(v) WRITE(FAN_PIN, v) + #define WRITE_FAN0(v) WRITE_FAN(v) + #endif + #if HAS_FAN1 + #define WRITE_FAN1(v) WRITE(FAN1_PIN, v) + #endif + #if HAS_FAN2 + #define WRITE_FAN2(v) WRITE(FAN2_PIN, v) + #endif + #define WRITE_FAN_N(n, v) WRITE_FAN##n(v) + + /** + * Part Cooling fan multipliexer + */ + #define HAS_FANMUX PIN_EXISTS(FANMUX0) + + /** + * Servos and probes + */ + + #if HAS_SERVOS + #ifndef Z_ENDSTOP_SERVO_NR + #define Z_ENDSTOP_SERVO_NR -1 + #endif + #endif + + #define PROBE_PIN_CONFIGURED (HAS_Z_MIN_PROBE_PIN || (HAS_Z_MIN && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN))) + #define HAS_BED_PROBE (PROBE_SELECTED && PROBE_PIN_CONFIGURED && DISABLED(PROBE_MANUALLY)) + + #if ENABLED(Z_PROBE_ALLEN_KEY) + #define PROBE_IS_TRIGGERED_WHEN_STOWED_TEST + #endif + + /** + * Bed Probe dependencies + */ + #if HAS_BED_PROBE + #if ENABLED(ENDSTOPPULLUPS) && HAS_Z_MIN_PROBE_PIN + #define ENDSTOPPULLUP_ZMIN_PROBE + #endif + #ifndef Z_PROBE_OFFSET_RANGE_MIN + #define Z_PROBE_OFFSET_RANGE_MIN -20 + #endif + #ifndef Z_PROBE_OFFSET_RANGE_MAX + #define Z_PROBE_OFFSET_RANGE_MAX 20 + #endif + #ifndef XY_PROBE_SPEED + #ifdef HOMING_FEEDRATE_XY + #define XY_PROBE_SPEED HOMING_FEEDRATE_XY + #else + #define XY_PROBE_SPEED 4000 + #endif + #endif + #if Z_CLEARANCE_BETWEEN_PROBES > Z_CLEARANCE_DEPLOY_PROBE + #define _Z_CLEARANCE_DEPLOY_PROBE Z_CLEARANCE_BETWEEN_PROBES + #else + #define _Z_CLEARANCE_DEPLOY_PROBE Z_CLEARANCE_DEPLOY_PROBE + #endif + #else + #undef X_PROBE_OFFSET_FROM_EXTRUDER + #undef Y_PROBE_OFFSET_FROM_EXTRUDER + #undef Z_PROBE_OFFSET_FROM_EXTRUDER + #define X_PROBE_OFFSET_FROM_EXTRUDER 0 + #define Y_PROBE_OFFSET_FROM_EXTRUDER 0 + #define Z_PROBE_OFFSET_FROM_EXTRUDER 0 + #endif + + /** + * Heater & Fan Pausing + */ + #if FAN_COUNT == 0 + #undef PROBING_FANS_OFF + #endif + #define QUIET_PROBING (HAS_BED_PROBE && (ENABLED(PROBING_HEATERS_OFF) || ENABLED(PROBING_FANS_OFF) || DELAY_BEFORE_PROBING > 0)) + #define HEATER_IDLE_HANDLER (ENABLED(ADVANCED_PAUSE_FEATURE) || ENABLED(PROBING_HEATERS_OFF)) + + /** + * Only constrain Z on DELTA / SCARA machines + */ + #if IS_KINEMATIC + #undef MIN_SOFTWARE_ENDSTOP_X + #undef MIN_SOFTWARE_ENDSTOP_Y + #undef MAX_SOFTWARE_ENDSTOP_X + #undef MAX_SOFTWARE_ENDSTOP_Y + #endif + + /** + * Delta radius/rod trimmers/angle trimmers + */ + #if ENABLED(DELTA) + #ifndef DELTA_PROBEABLE_RADIUS + #define DELTA_PROBEABLE_RADIUS DELTA_PRINTABLE_RADIUS + #endif + #ifndef DELTA_CALIBRATION_RADIUS + #define DELTA_CALIBRATION_RADIUS DELTA_PRINTABLE_RADIUS - 10 + #endif + #ifndef DELTA_ENDSTOP_ADJ + #define DELTA_ENDSTOP_ADJ { 0, 0, 0 } + #endif + #ifndef DELTA_TOWER_ANGLE_TRIM + #define DELTA_TOWER_ANGLE_TRIM {0, 0, 0} + #endif + #ifndef DELTA_RADIUS_TRIM_TOWER + #define DELTA_RADIUS_TRIM_TOWER {0, 0, 0} + #endif + #ifndef DELTA_DIAGONAL_ROD_TRIM_TOWER + #define DELTA_DIAGONAL_ROD_TRIM_TOWER {0, 0, 0} + #endif + #endif + + /** + * Set granular options based on the specific type of leveling + */ + #define UBL_DELTA (ENABLED(AUTO_BED_LEVELING_UBL) && (ENABLED(DELTA) || ENABLED(UBL_GRANULAR_SEGMENTATION_FOR_CARTESIAN))) + #define ABL_PLANAR (ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(AUTO_BED_LEVELING_3POINT)) + #define ABL_GRID (ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(AUTO_BED_LEVELING_BILINEAR)) + #define OLDSCHOOL_ABL (ABL_PLANAR || ABL_GRID) + #define HAS_ABL (OLDSCHOOL_ABL || ENABLED(AUTO_BED_LEVELING_UBL)) + #define HAS_LEVELING (HAS_ABL || ENABLED(MESH_BED_LEVELING)) + #define HAS_AUTOLEVEL (HAS_ABL && DISABLED(PROBE_MANUALLY)) + #define HAS_MESH (ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(MESH_BED_LEVELING)) + #define PLANNER_LEVELING (OLDSCHOOL_ABL || ENABLED(MESH_BED_LEVELING) || UBL_DELTA) + #define HAS_PROBING_PROCEDURE (HAS_ABL || ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST)) + #if HAS_PROBING_PROCEDURE + #define PROBE_BED_WIDTH abs(RIGHT_PROBE_BED_POSITION - (LEFT_PROBE_BED_POSITION)) + #define PROBE_BED_HEIGHT abs(BACK_PROBE_BED_POSITION - (FRONT_PROBE_BED_POSITION)) + #endif + + /** + * Bed Probing rectangular bounds + * These can be further constrained in code for Delta and SCARA + */ + #if ENABLED(DELTA) + // Probing points may be verified at compile time within the radius + // using static_assert(HYPOT2(X2-X1,Y2-Y1)<=sq(DELTA_PRINTABLE_RADIUS),"bad probe point!") + // so that may be added to SanityCheck.h in the future. + #define MIN_PROBE_X (X_CENTER - (DELTA_PROBEABLE_RADIUS)) + #define MIN_PROBE_Y (Y_CENTER - (DELTA_PROBEABLE_RADIUS)) + #define MAX_PROBE_X (X_CENTER + DELTA_PROBEABLE_RADIUS) + #define MAX_PROBE_Y (Y_CENTER + DELTA_PROBEABLE_RADIUS) + #elif IS_SCARA + #define SCARA_PRINTABLE_RADIUS (SCARA_LINKAGE_1 + SCARA_LINKAGE_2) + #define MIN_PROBE_X (X_CENTER - (SCARA_PRINTABLE_RADIUS)) + #define MIN_PROBE_Y (Y_CENTER - (SCARA_PRINTABLE_RADIUS)) + #define MAX_PROBE_X (X_CENTER + SCARA_PRINTABLE_RADIUS) + #define MAX_PROBE_Y (Y_CENTER + SCARA_PRINTABLE_RADIUS) + #else + // Boundaries for Cartesian probing based on bed limits + #define MIN_PROBE_X (max(X_MIN_BED, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER)) + #define MIN_PROBE_Y (max(Y_MIN_BED, Y_MIN_POS + Y_PROBE_OFFSET_FROM_EXTRUDER)) + #define MAX_PROBE_X (min(X_MAX_BED, X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER)) + #define MAX_PROBE_Y (min(Y_MAX_BED, Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER)) + #endif + + /** + * Default mesh area is an area with an inset margin on the print area. + */ + #if ENABLED(MESH_BED_LEVELING) || ENABLED(AUTO_BED_LEVELING_UBL) + #if IS_KINEMATIC + // Probing points may be verified at compile time within the radius + // using static_assert(HYPOT2(X2-X1,Y2-Y1)<=sq(DELTA_PRINTABLE_RADIUS),"bad probe point!") + // so that may be added to SanityCheck.h in the future. + #define _MESH_MIN_X (MIN_PROBE_X + MESH_INSET) + #define _MESH_MIN_Y (MIN_PROBE_Y + MESH_INSET) + #define _MESH_MAX_X (MAX_PROBE_X - (MESH_INSET)) + #define _MESH_MAX_Y (MAX_PROBE_Y - (MESH_INSET)) + #else + // Boundaries for Cartesian probing based on set limits + #define _MESH_MIN_X (max(X_MIN_BED + MESH_INSET, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER)) + #define _MESH_MIN_Y (max(Y_MIN_BED + MESH_INSET, Y_MIN_POS + Y_PROBE_OFFSET_FROM_EXTRUDER)) + #define _MESH_MAX_X (min(X_MAX_BED - (MESH_INSET), X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER)) + #define _MESH_MAX_Y (min(Y_MAX_BED - (MESH_INSET), Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER)) + #endif + /** + * These may be overridden in Configuration if a smaller area is wanted + */ + #if ENABLED(MESH_BED_LEVELING) || ENABLED(AUTO_BED_LEVELING_UBL) + #ifndef MESH_MIN_X + #define MESH_MIN_X _MESH_MIN_X + #endif + #ifndef MESH_MIN_Y + #define MESH_MIN_Y _MESH_MIN_Y + #endif + #ifndef MESH_MAX_X + #define MESH_MAX_X _MESH_MAX_X + #endif + #ifndef MESH_MAX_Y + #define MESH_MAX_Y _MESH_MAX_Y + #endif + #endif + #endif // MESH_BED_LEVELING || AUTO_BED_LEVELING_UBL + + /** + * Buzzer/Speaker + */ + #if ENABLED(LCD_USE_I2C_BUZZER) + #ifndef LCD_FEEDBACK_FREQUENCY_HZ + #define LCD_FEEDBACK_FREQUENCY_HZ 1000 + #endif + #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS + #define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100 + #endif + #else + #ifndef LCD_FEEDBACK_FREQUENCY_HZ + #define LCD_FEEDBACK_FREQUENCY_HZ 5000 + #endif + #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS + #define LCD_FEEDBACK_FREQUENCY_DURATION_MS 2 + #endif + #endif + + /** + * VIKI2, miniVIKI, and AZSMZ_12864 require DOGLCD_SCK and DOGLCD_MOSI to be defined. + */ + #if ENABLED(VIKI2) || ENABLED(miniVIKI) || ENABLED(AZSMZ_12864) + #ifndef DOGLCD_SCK + #define DOGLCD_SCK SCK_PIN + #endif + #ifndef DOGLCD_MOSI + #define DOGLCD_MOSI MOSI_PIN + #endif + #endif + + /** + * Z_HOMING_HEIGHT / Z_CLEARANCE_BETWEEN_PROBES + */ + #ifndef Z_HOMING_HEIGHT + #ifndef Z_CLEARANCE_BETWEEN_PROBES + #define Z_HOMING_HEIGHT 0 + #else + #define Z_HOMING_HEIGHT Z_CLEARANCE_BETWEEN_PROBES + #endif + #endif + #ifndef Z_CLEARANCE_BETWEEN_PROBES + #define Z_CLEARANCE_BETWEEN_PROBES Z_HOMING_HEIGHT + #endif + #if Z_CLEARANCE_BETWEEN_PROBES > Z_HOMING_HEIGHT + #define MANUAL_PROBE_HEIGHT Z_CLEARANCE_BETWEEN_PROBES + #else + #define MANUAL_PROBE_HEIGHT Z_HOMING_HEIGHT + #endif + + // Stepper pulse duration, in cycles + #define STEP_PULSE_CYCLES ((MINIMUM_STEPPER_PULSE) * CYCLES_PER_MICROSECOND) + + #if ENABLED(SDCARD_SORT_ALPHA) + #define HAS_FOLDER_SORTING (FOLDER_SORTING || ENABLED(SDSORT_GCODE)) + #endif + + // Updated G92 behavior shifts the workspace + #define HAS_POSITION_SHIFT DISABLED(NO_WORKSPACE_OFFSETS) + // The home offset also shifts the coordinate space + #define HAS_HOME_OFFSET (DISABLED(NO_WORKSPACE_OFFSETS) && DISABLED(DELTA)) + // Either offset yields extra calculations on all moves + #define HAS_WORKSPACE_OFFSET (HAS_POSITION_SHIFT || HAS_HOME_OFFSET) + // M206 doesn't apply to DELTA + #define HAS_M206_COMMAND (HAS_HOME_OFFSET && DISABLED(DELTA)) + + // LCD timeout to status screen default is 15s + #ifndef LCD_TIMEOUT_TO_STATUS + #define LCD_TIMEOUT_TO_STATUS 15000 + #endif + + /** + * DELTA_SEGMENT_MIN_LENGTH for UBL_DELTA + */ + #if UBL_DELTA + #ifndef DELTA_SEGMENT_MIN_LENGTH + #if IS_SCARA + #define DELTA_SEGMENT_MIN_LENGTH 0.25 // SCARA minimum segment size is 0.25mm + #elif ENABLED(DELTA) + #define DELTA_SEGMENT_MIN_LENGTH 0.10 // mm (still subject to DELTA_SEGMENTS_PER_SECOND) + #else // CARTESIAN + #define DELTA_SEGMENT_MIN_LENGTH 1.00 // mm (similar to G2/G3 arc segmentation) + #endif + #endif + #endif + + // Shorthand + #define GRID_MAX_POINTS ((GRID_MAX_POINTS_X) * (GRID_MAX_POINTS_Y)) + + // Add commands that need sub-codes to this list + #define USE_GCODE_SUBCODES ENABLED(G38_PROBE_TARGET) || ENABLED(CNC_COORDINATE_SYSTEMS) + + // Parking Extruder + #if ENABLED(PARKING_EXTRUDER) + #ifndef PARKING_EXTRUDER_GRAB_DISTANCE + #define PARKING_EXTRUDER_GRAB_DISTANCE 0 + #endif + #ifndef PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE + #define PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE HIGH + #endif + #endif + + // Number of VFAT entries used. Each entry has 13 UTF-16 characters + #if ENABLED(SCROLL_LONG_FILENAMES) + #define MAX_VFAT_ENTRIES (5) + #else + #define MAX_VFAT_ENTRIES (2) + #endif + +#endif // CONDITIONALS_POST_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration.h new file mode 100644 index 0000000..8ff710a --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration.h @@ -0,0 +1,1849 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Configuration.h + * + * Basic settings such as: + * + * - Type of electronics + * - Type of temperature sensor + * - Printer geometry + * - Endstop configuration + * - LCD controller + * - Extra features + * + * Advanced settings can be found in Configuration_adv.h + * + */ +#ifndef CONFIGURATION_H +#define CONFIGURATION_H +#define CONFIGURATION_H_VERSION 010107 + +//=========================================================================== +//============================= Getting Started ============================= +//=========================================================================== + +/** + * Here are some standard links for getting your machine calibrated: + * + * http://reprap.org/wiki/Calibration + * http://youtu.be/wAL9d7FgInk + * http://calculator.josefprusa.cz + * http://reprap.org/wiki/Triffid_Hunter%27s_Calibration_Guide + * http://www.thingiverse.com/thing:5573 + * https://sites.google.com/site/repraplogphase/calibration-of-your-reprap + * http://www.thingiverse.com/thing:298812 + */ + +//=========================================================================== +//============================= DELTA Printer =============================== +//=========================================================================== +// For a Delta printer start with one of the configuration files in the +// example_configurations/delta directory and customize for your machine. +// + +//=========================================================================== +//============================= SCARA Printer =============================== +//=========================================================================== +// For a SCARA printer start with the configuration files in +// example_configurations/SCARA and customize for your machine. +// + +// @section info + +// User-specified version info of this build to display in [Pronterface, etc] terminal window during +// startup. Implementation of an idea by Prof Braino to inform user that any changes made to this +// build by the user have been successfully uploaded into firmware. +#define STRING_CONFIG_H_AUTHOR "(none, default config)" // Who made the changes. +#define SHOW_BOOTSCREEN +#define STRING_SPLASH_LINE1 SHORT_BUILD_VERSION // will be shown during bootup in line 1 +#define STRING_SPLASH_LINE2 WEBSITE_URL // will be shown during bootup in line 2 + +// +// *** VENDORS PLEASE READ ***************************************************** +// +// Marlin now allow you to have a vendor boot image to be displayed on machine +// start. When SHOW_CUSTOM_BOOTSCREEN is defined Marlin will first show your +// custom boot image and then the default Marlin boot image is shown. +// +// We suggest for you to take advantage of this new feature and keep the Marlin +// boot image unmodified. For an example have a look at the bq Hephestos 2 +// example configuration folder. +// +//#define SHOW_CUSTOM_BOOTSCREEN +// @section machine + +/** + * Select which serial port on the board will be used for communication with the host. + * This allows the connection of wireless adapters (for instance) to non-default port pins. + * Serial port 0 is always used by the Arduino bootloader regardless of this setting. + * + * :[0, 1, 2, 3, 4, 5, 6, 7] + */ +#define SERIAL_PORT 0 + +/** + * This setting determines the communication speed of the printer. + * + * 250000 works in most cases, but you might try a lower speed if + * you commonly experience drop-outs during host printing. + * You may try up to 1000000 to speed up SD file transfer. + * + * :[2400, 9600, 19200, 38400, 57600, 115200, 250000, 500000, 1000000] + */ +#define BAUDRATE 250000 + +// Enable the Bluetooth serial interface on AT90USB devices +//#define BLUETOOTH + +// The following define selects which electronics board you have. +// Please choose the name from boards.h that matches your setup +#ifndef MOTHERBOARD + #define MOTHERBOARD 39 +#endif + +// Optional custom name for your RepStrap or other custom machine +// Displayed in the LCD "Ready" message +#define CUSTOM_MACHINE_NAME "Anet A8" + +// Define this to set a unique identifier for this printer, (Used by some programs to differentiate between machines) +// You can use an online service to generate a random UUID. (eg http://www.uuidgenerator.net/version4) +#define MACHINE_UUID "372a2b24-a5e9-46f7-ae58-788dea10b05c" + +// @section extruder + +// This defines the number of extruders +// :[1, 2, 3, 4, 5] +#define EXTRUDERS 1 + +// For Cyclops or any "multi-extruder" that shares a single nozzle. +//#define SINGLENOZZLE + +/** + * Průša MK2 Single Nozzle Multi-Material Multiplexer, and variants. + * + * This device allows one stepper driver on a control board to drive + * two to eight stepper motors, one at a time, in a manner suitable + * for extruders. + * + * This option only allows the multiplexer to switch on tool-change. + * Additional options to configure custom E moves are pending. + */ +//#define MK2_MULTIPLEXER +#if ENABLED(MK2_MULTIPLEXER) + // Override the default DIO selector pins here, if needed. + // Some pins files may provide defaults for these pins. + //#define E_MUX0_PIN 40 // Always Required + //#define E_MUX1_PIN 42 // Needed for 3 to 8 steppers + //#define E_MUX2_PIN 44 // Needed for 5 to 8 steppers +#endif + +// A dual extruder that uses a single stepper motor +//#define SWITCHING_EXTRUDER +#if ENABLED(SWITCHING_EXTRUDER) + #define SWITCHING_EXTRUDER_SERVO_NR 0 + #define SWITCHING_EXTRUDER_SERVO_ANGLES { 0, 90 } // Angles for E0, E1[, E2, E3] + #if EXTRUDERS > 3 + #define SWITCHING_EXTRUDER_E23_SERVO_NR 1 + #endif +#endif + +// A dual-nozzle that uses a servomotor to raise/lower one of the nozzles +//#define SWITCHING_NOZZLE +#if ENABLED(SWITCHING_NOZZLE) + #define SWITCHING_NOZZLE_SERVO_NR 0 + #define SWITCHING_NOZZLE_SERVO_ANGLES { 0, 90 } // Angles for E0, E1 + //#define HOTEND_OFFSET_Z { 0.0, 0.0 } +#endif + +/** + * Two separate X-carriages with extruders that connect to a moving part + * via a magnetic docking mechanism. Requires SOL1_PIN and SOL2_PIN. + */ +//#define PARKING_EXTRUDER +#if ENABLED(PARKING_EXTRUDER) + #define PARKING_EXTRUDER_SOLENOIDS_INVERT // If enabled, the solenoid is NOT magnetized with applied voltage + #define PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE LOW // LOW or HIGH pin signal energizes the coil + #define PARKING_EXTRUDER_SOLENOIDS_DELAY 250 // Delay (ms) for magnetic field. No delay if 0 or not defined. + #define PARKING_EXTRUDER_PARKING_X { -78, 184 } // X positions for parking the extruders + #define PARKING_EXTRUDER_GRAB_DISTANCE 1 // mm to move beyond the parking point to grab the extruder + #define PARKING_EXTRUDER_SECURITY_RAISE 5 // Z-raise before parking + #define HOTEND_OFFSET_Z { 0.0, 1.3 } // Z-offsets of the two hotends. The first must be 0. +#endif + +/** + * "Mixing Extruder" + * - Adds a new code, M165, to set the current mix factors. + * - Extends the stepping routines to move multiple steppers in proportion to the mix. + * - Optional support for Repetier Firmware M163, M164, and virtual extruder. + * - This implementation supports only a single extruder. + * - Enable DIRECT_MIXING_IN_G1 for Pia Taubert's reference implementation + */ +//#define MIXING_EXTRUDER +#if ENABLED(MIXING_EXTRUDER) + #define MIXING_STEPPERS 2 // Number of steppers in your mixing extruder + #define MIXING_VIRTUAL_TOOLS 16 // Use the Virtual Tool method with M163 and M164 + //#define DIRECT_MIXING_IN_G1 // Allow ABCDHI mix factors in G1 movement commands +#endif + +// Offset of the extruders (uncomment if using more than one and relying on firmware to position when changing). +// The offset has to be X=0, Y=0 for the extruder 0 hotend (default extruder). +// For the other hotends it is their distance from the extruder 0 hotend. +//#define HOTEND_OFFSET_X {0.0, 20.00} // (in mm) for each extruder, offset of the hotend on the X axis +//#define HOTEND_OFFSET_Y {0.0, 5.00} // (in mm) for each extruder, offset of the hotend on the Y axis + +// @section machine + +/** + * Select your power supply here. Use 0 if you haven't connected the PS_ON_PIN + * + * 0 = No Power Switch + * 1 = ATX + * 2 = X-Box 360 203Watts (the blue wire connected to PS_ON and the red wire to VCC) + * + * :{ 0:'No power switch', 1:'ATX', 2:'X-Box 360' } + */ +#define POWER_SUPPLY 0 + +#if POWER_SUPPLY > 0 + // Enable this option to leave the PSU off at startup. + // Power to steppers and heaters will need to be turned on with M80. + //#define PS_DEFAULT_OFF +#endif + +// @section temperature + +//=========================================================================== +//============================= Thermal Settings ============================ +//=========================================================================== + +/** + * --NORMAL IS 4.7kohm PULLUP!-- 1kohm pullup can be used on hotend sensor, using correct resistor and table + * + * Temperature sensors available: + * + * -3 : thermocouple with MAX31855 (only for sensor 0) + * -2 : thermocouple with MAX6675 (only for sensor 0) + * -1 : thermocouple with AD595 + * 0 : not used + * 1 : 100k thermistor - best choice for EPCOS 100k (4.7k pullup) + * 2 : 200k thermistor - ATC Semitec 204GT-2 (4.7k pullup) + * 3 : Mendel-parts thermistor (4.7k pullup) + * 4 : 10k thermistor !! do not use it for a hotend. It gives bad resolution at high temp. !! + * 5 : 100K thermistor - ATC Semitec 104GT-2 (Used in ParCan & J-Head) (4.7k pullup) + * 6 : 100k EPCOS - Not as accurate as table 1 (created using a fluke thermocouple) (4.7k pullup) + * 7 : 100k Honeywell thermistor 135-104LAG-J01 (4.7k pullup) + * 71 : 100k Honeywell thermistor 135-104LAF-J01 (4.7k pullup) + * 8 : 100k 0603 SMD Vishay NTCS0603E3104FXT (4.7k pullup) + * 9 : 100k GE Sensing AL03006-58.2K-97-G1 (4.7k pullup) + * 10 : 100k RS thermistor 198-961 (4.7k pullup) + * 11 : 100k beta 3950 1% thermistor (4.7k pullup) + * 12 : 100k 0603 SMD Vishay NTCS0603E3104FXT (4.7k pullup) (calibrated for Makibox hot bed) + * 13 : 100k Hisens 3950 1% up to 300°C for hotend "Simple ONE " & "Hotend "All In ONE" + * 20 : the PT100 circuit found in the Ultimainboard V2.x + * 60 : 100k Maker's Tool Works Kapton Bed Thermistor beta=3950 + * 66 : 4.7M High Temperature thermistor from Dyze Design + * 70 : the 100K thermistor found in the bq Hephestos 2 + * 75 : 100k Generic Silicon Heat Pad with NTC 100K MGB18-104F39050L32 thermistor + * + * 1k ohm pullup tables - This is atypical, and requires changing out the 4.7k pullup for 1k. + * (but gives greater accuracy and more stable PID) + * 51 : 100k thermistor - EPCOS (1k pullup) + * 52 : 200k thermistor - ATC Semitec 204GT-2 (1k pullup) + * 55 : 100k thermistor - ATC Semitec 104GT-2 (Used in ParCan & J-Head) (1k pullup) + * + * 1047 : Pt1000 with 4k7 pullup + * 1010 : Pt1000 with 1k pullup (non standard) + * 147 : Pt100 with 4k7 pullup + * 110 : Pt100 with 1k pullup (non standard) + * + * Use these for Testing or Development purposes. NEVER for production machine. + * 998 : Dummy Table that ALWAYS reads 25°C or the temperature defined below. + * 999 : Dummy Table that ALWAYS reads 100°C or the temperature defined below. + * + * :{ '0': "Not used", '1':"100k / 4.7k - EPCOS", '2':"200k / 4.7k - ATC Semitec 204GT-2", '3':"Mendel-parts / 4.7k", '4':"10k !! do not use for a hotend. Bad resolution at high temp. !!", '5':"100K / 4.7k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '6':"100k / 4.7k EPCOS - Not as accurate as Table 1", '7':"100k / 4.7k Honeywell 135-104LAG-J01", '8':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT", '9':"100k / 4.7k GE Sensing AL03006-58.2K-97-G1", '10':"100k / 4.7k RS 198-961", '11':"100k / 4.7k beta 3950 1%", '12':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT (calibrated for Makibox hot bed)", '13':"100k Hisens 3950 1% up to 300°C for hotend 'Simple ONE ' & hotend 'All In ONE'", '20':"PT100 (Ultimainboard V2.x)", '51':"100k / 1k - EPCOS", '52':"200k / 1k - ATC Semitec 204GT-2", '55':"100k / 1k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '60':"100k Maker's Tool Works Kapton Bed Thermistor beta=3950", '66':"Dyze Design 4.7M High Temperature thermistor", '70':"the 100K thermistor found in the bq Hephestos 2", '71':"100k / 4.7k Honeywell 135-104LAF-J01", '147':"Pt100 / 4.7k", '1047':"Pt1000 / 4.7k", '110':"Pt100 / 1k (non-standard)", '1010':"Pt1000 / 1k (non standard)", '-3':"Thermocouple + MAX31855 (only for sensor 0)", '-2':"Thermocouple + MAX6675 (only for sensor 0)", '-1':"Thermocouple + AD595",'998':"Dummy 1", '999':"Dummy 2" } + */ +#define TEMP_SENSOR_0 5 +#define TEMP_SENSOR_1 0 +#define TEMP_SENSOR_2 0 +#define TEMP_SENSOR_3 0 +#define TEMP_SENSOR_4 0 +#define TEMP_SENSOR_BED 0 + +// Dummy thermistor constant temperature readings, for use with 998 and 999 +#define DUMMY_THERMISTOR_998_VALUE 25 +#define DUMMY_THERMISTOR_999_VALUE 100 + +// Use temp sensor 1 as a redundant sensor with sensor 0. If the readings +// from the two sensors differ too much the print will be aborted. +//#define TEMP_SENSOR_1_AS_REDUNDANT +#define MAX_REDUNDANT_TEMP_SENSOR_DIFF 5 + +// Extruder temperature must be close to target for this long before M109 returns success +#define TEMP_RESIDENCY_TIME 10 // (seconds) +#define TEMP_HYSTERESIS 3 // (degC) range of +/- temperatures considered "close" to the target one +#define TEMP_WINDOW 1 // (degC) Window around target to start the residency timer x degC early. + +// Bed temperature must be close to target for this long before M190 returns success +#define TEMP_BED_RESIDENCY_TIME 0 // (seconds) +#define TEMP_BED_HYSTERESIS 3 // (degC) range of +/- temperatures considered "close" to the target one +#define TEMP_BED_WINDOW 1 // (degC) Window around target to start the residency timer x degC early. + +// The minimal temperature defines the temperature below which the heater will not be enabled It is used +// to check that the wiring to the thermistor is not broken. +// Otherwise this would lead to the heater being powered on all the time. +#define HEATER_0_MINTEMP 5 +#define HEATER_1_MINTEMP 5 +#define BED_MINTEMP 5 + +// When temperature exceeds max temp, your heater will be switched off. +// This feature exists to protect your hotend from overheating accidentally, but *NOT* from thermistor short/failure! +// You should use MINTEMP for thermistor short/failure protection. +#define HEATER_0_MAXTEMP 275 +#define HEATER_1_MAXTEMP 275 +#define BED_MAXTEMP 120 + +//=========================================================================== +//============================= PID Settings ================================ +//=========================================================================== +// PID Tuning Guide here: http://reprap.org/wiki/PID_Tuning + +// Comment the following line to disable PID and enable bang-bang. +#define PIDTEMP +#define BANG_MAX 255 // limits current to nozzle while in bang-bang mode; 255=full current +#define PID_MAX BANG_MAX // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current +#if ENABLED(PIDTEMP) + //#define PID_AUTOTUNE_MENU // Add PID Autotune to the LCD "Temperature" menu to run M303 and apply the result. + //#define PID_DEBUG // Sends debug data to the serial port. + //#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX + //#define SLOW_PWM_HEATERS // PWM with very low frequency (roughly 0.125Hz=8s) and minimum state time of approximately 1s useful for heaters driven by a relay + //#define PID_PARAMS_PER_HOTEND // Uses separate PID parameters for each extruder (useful for mismatched extruders) + // Set/get with gcode: M301 E[extruder number, 0-2] + #define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature + // is more than PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max. + #define K1 0.95 //smoothing factor within the PID + + // If you are using a pre-configured hotend then you can use one of the value sets by uncommenting it + + // + #define DEFAULT_Kp 28.33 + #define DEFAULT_Ki 2.01 + #define DEFAULT_Kd 99.81 + + // Ultimaker + //#define DEFAULT_Kp 22.2 + //#define DEFAULT_Ki 1.08 + //#define DEFAULT_Kd 114 + + // MakerGear + //#define DEFAULT_Kp 7.0 + //#define DEFAULT_Ki 0.1 + //#define DEFAULT_Kd 12 + + // Mendel Parts V9 on 12V + //#define DEFAULT_Kp 63.0 + //#define DEFAULT_Ki 2.25 + //#define DEFAULT_Kd 440 + +#endif // PIDTEMP + +//=========================================================================== +//============================= PID > Bed Temperature Control =============== +//=========================================================================== +// Select PID or bang-bang with PIDTEMPBED. If bang-bang, BED_LIMIT_SWITCHING will enable hysteresis +// +// Uncomment this to enable PID on the bed. It uses the same frequency PWM as the extruder. +// If your PID_dT is the default, and correct for your hardware/configuration, that means 7.689Hz, +// which is fine for driving a square wave into a resistive load and does not significantly impact you FET heating. +// This also works fine on a Fotek SSR-10DA Solid State Relay into a 250W heater. +// If your configuration is significantly different than this and you don't understand the issues involved, you probably +// shouldn't use bed PID until someone else verifies your hardware works. +// If this is enabled, find your own PID constants below. +//#define PIDTEMPBED + +//#define BED_LIMIT_SWITCHING + +// This sets the max power delivered to the bed, and replaces the HEATER_BED_DUTY_CYCLE_DIVIDER option. +// all forms of bed control obey this (PID, bang-bang, bang-bang with hysteresis) +// setting this to anything other than 255 enables a form of PWM to the bed just like HEATER_BED_DUTY_CYCLE_DIVIDER did, +// so you shouldn't use it unless you are OK with PWM on your bed. (see the comment on enabling PIDTEMPBED) +#define MAX_BED_POWER 255 // limits duty cycle to bed; 255=full current + +#if ENABLED(PIDTEMPBED) + + //#define PID_BED_DEBUG // Sends debug data to the serial port. + + //120V 250W silicone heater into 4mm borosilicate (MendelMax 1.5+) + //from FOPDT model - kp=.39 Tp=405 Tdead=66, Tc set to 79.2, aggressive factor of .15 (vs .1, 1, 10) + #define DEFAULT_bedKp 10.00 + #define DEFAULT_bedKi .023 + #define DEFAULT_bedKd 305.4 + + //120V 250W silicone heater into 4mm borosilicate (MendelMax 1.5+) + //from pidautotune + //#define DEFAULT_bedKp 97.1 + //#define DEFAULT_bedKi 1.41 + //#define DEFAULT_bedKd 1675.16 + + // FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles. +#endif // PIDTEMPBED + +// @section extruder + +// This option prevents extrusion if the temperature is below EXTRUDE_MINTEMP. +// It also enables the M302 command to set the minimum extrusion temperature +// or to allow moving the extruder regardless of the hotend temperature. +// *** IT IS HIGHLY RECOMMENDED TO LEAVE THIS OPTION ENABLED! *** +#define PREVENT_COLD_EXTRUSION +#define EXTRUDE_MINTEMP 170 + +// This option prevents a single extrusion longer than EXTRUDE_MAXLENGTH. +// Note that for Bowden Extruders a too-small value here may prevent loading. +#define PREVENT_LENGTHY_EXTRUDE +#define EXTRUDE_MAXLENGTH 200 + +//=========================================================================== +//======================== Thermal Runaway Protection ======================= +//=========================================================================== + +/** + * Thermal Protection provides additional protection to your printer from damage + * and fire. Marlin always includes safe min and max temperature ranges which + * protect against a broken or disconnected thermistor wire. + * + * The issue: If a thermistor falls out, it will report the much lower + * temperature of the air in the room, and the the firmware will keep + * the heater on. + * + * If you get "Thermal Runaway" or "Heating failed" errors the + * details can be tuned in Configuration_adv.h + */ + +#define THERMAL_PROTECTION_HOTENDS // Enable thermal protection for all extruders +#define THERMAL_PROTECTION_BED // Enable thermal protection for the heated bed + +//=========================================================================== +//============================= Mechanical Settings ========================= +//=========================================================================== + +// @section machine + +// Uncomment one of these options to enable CoreXY, CoreXZ, or CoreYZ kinematics +// either in the usual order or reversed +//#define COREXY +//#define COREXZ +//#define COREYZ +//#define COREYX +//#define COREZX +//#define COREZY + +//=========================================================================== +//============================== Delta Settings ============================= +//=========================================================================== +// Enable DELTA kinematics and most of the default configuration for Deltas +// Note that if EEPROM is enabled, saved values will override these +//#define DELTA + +#if ENABLED(DELTA) + + // Make delta curves from many straight lines (linear interpolation). + // This is a trade-off between visible corners (not enough segments) + // and processor overload (too many expensive sqrt calls). + #define DELTA_SEGMENTS_PER_SECOND 200 + + // After homing move down to a height where XY movement is unconstrained + //#define DELTA_HOME_TO_SAFE_ZONE + + // Delta calibration menu + // uncomment to add three points calibration menu option. + // See http://minow.blogspot.com/index.html#4918805519571907051 + #define DELTA_CALIBRATION_MENU + + // uncomment to add G33 Delta Auto-Calibration (Enable EEPROM_SETTINGS to store results) + #define DELTA_AUTO_CALIBRATION + + // NOTE NB all values for DELTA_* values MUST be floating point, so always have a decimal point in them + + #if ENABLED(DELTA_AUTO_CALIBRATION) + // set the default number of probe points : n*n (1 -> 7) + #define DELTA_CALIBRATION_DEFAULT_POINTS 4 + + // Enable and set these values based on results of 'G33 A' + //#define H_FACTOR 1.01 + //#define R_FACTOR 2.61 + //#define A_FACTOR 0.87 + + #endif + + #if ENABLED(DELTA_AUTO_CALIBRATION) || ENABLED(DELTA_CALIBRATION_MENU) + // Set the radius for the calibration probe points - max DELTA_PRINTABLE_RADIUS for non-eccentric probes + #define DELTA_CALIBRATION_RADIUS 70 // mm + // Set the steprate for papertest probing + #define PROBE_MANUALLY_STEP 0.025 + #endif + + // Print surface diameter/2 minus unreachable space (avoid collisions with vertical towers). + #define DELTA_PRINTABLE_RADIUS 90.0 // mm + + // Center-to-center distance of the holes in the diagonal push rods. + #define DELTA_DIAGONAL_ROD 242.0 // mm + + // height from z=0 to home position + #define DELTA_HEIGHT 323.25 // get this value from auto calibrate + + #define DELTA_ENDSTOP_ADJ { 0.0, 0.0, 0.0 } // get these from auto calibrate + + // Horizontal distance bridged by diagonal push rods when effector is centered. + #define DELTA_RADIUS 103 //mm Get this value from auto calibrate + + // Trim adjustments for individual towers + // tower angle corrections for X and Y tower / rotate XYZ so Z tower angle = 0 + // measured in degrees anticlockwise looking from above the printer + #define DELTA_TOWER_ANGLE_TRIM { 0.0, 0.0, 0.0 } // get these values from auto calibrate + + // delta radius and diaginal rod adjustments measured in mm + //#define DELTA_RADIUS_TRIM_TOWER { 0.0, 0.0, 0.0 } + //#define DELTA_DIAGONAL_ROD_TRIM_TOWER { 0.0, 0.0, 0.0 } + +#endif + +//=========================================================================== +//============================== Endstop Settings =========================== +//=========================================================================== + +// @section homing + +// Specify here all the endstop connectors that are connected to any endstop or probe. +// Almost all printers will be using one per axis. Probes will use one or more of the +// extra connectors. Leave undefined any used for non-endstop and non-probe purposes. +#define USE_XMIN_PLUG +#define USE_YMIN_PLUG +#define USE_ZMIN_PLUG // a Z probe +//#define USE_XMAX_PLUG +//#define USE_YMAX_PLUG +//#define USE_ZMAX_PLUG + +// coarse Endstop Settings +#define ENDSTOPPULLUPS // Comment this out (using // at the start of the line) to disable the endstop pullup resistors + +#if DISABLED(ENDSTOPPULLUPS) + // fine endstop settings: Individual pullups. will be ignored if ENDSTOPPULLUPS is defined + //#define ENDSTOPPULLUP_XMAX + //#define ENDSTOPPULLUP_YMAX + //#define ENDSTOPPULLUP_ZMAX + //#define ENDSTOPPULLUP_XMIN + //#define ENDSTOPPULLUP_YMIN + //#define ENDSTOPPULLUP_ZMIN + //#define ENDSTOPPULLUP_ZMIN_PROBE +#endif + +// Mechanical endstop with COM to ground and NC to Signal uses "false" here (most common setup). +#define X_MIN_ENDSTOP_INVERTING false // set to true to invert the logic of the endstop. +#define Y_MIN_ENDSTOP_INVERTING false // set to true to invert the logic of the endstop. +#define Z_MIN_ENDSTOP_INVERTING true // set to true to invert the logic of the endstop. +#define X_MAX_ENDSTOP_INVERTING false // set to true to invert the logic of the endstop. +#define Y_MAX_ENDSTOP_INVERTING false // set to true to invert the logic of the endstop. +#define Z_MAX_ENDSTOP_INVERTING false // set to true to invert the logic of the endstop. +#define Z_MIN_PROBE_ENDSTOP_INVERTING false // set to true to invert the logic of the probe. + +// Enable this feature if all enabled endstop pins are interrupt-capable. +// This will remove the need to poll the interrupt pins, saving many CPU cycles. +//#define ENDSTOP_INTERRUPTS_FEATURE + +//============================================================================= +//============================== Movement Settings ============================ +//============================================================================= +// @section motion + +// delta speeds must be the same on xyz +/** + * Default Settings + * + * These settings can be reset by M502 + * + * Note that if EEPROM is enabled, saved values will override these. + */ + +/** + * With this option each E stepper can have its own factors for the + * following movement settings. If fewer factors are given than the + * total number of extruders, the last value applies to the rest. + */ +//#define DISTINCT_E_FACTORS + +/** + * Default Axis Steps Per Unit (steps/mm) + * Override with M92 + * X, Y, Z, E0 [, E1[, E2[, E3[, E4]]]] + */ +#define DEFAULT_AXIS_STEPS_PER_UNIT { 80, 80, 80, 96 } // default steps per unit for Kossel (GT2, 20 tooth) + +/** + * Default Max Feed Rate (mm/s) + * Override with M203 + * X, Y, Z, E0 [, E1[, E2[, E3[, E4]]]] + */ +#define DEFAULT_MAX_FEEDRATE { 200, 200, 200, 200 } + +/** + * Default Max Acceleration (change/s) change = mm/s + * (Maximum start speed for accelerated moves) + * Override with M201 + * X, Y, Z, E0 [, E1[, E2[, E3[, E4]]]] + */ +#define DEFAULT_MAX_ACCELERATION { 3000, 3000, 3000, 3000 } + +/** + * Default Acceleration (change/s) change = mm/s + * Override with M204 + * + * M204 P Acceleration + * M204 R Retract Acceleration + * M204 T Travel Acceleration + */ +#define DEFAULT_ACCELERATION 2000 // X, Y, Z and E acceleration for printing moves +#define DEFAULT_RETRACT_ACCELERATION 2000 // E acceleration for retracts +#define DEFAULT_TRAVEL_ACCELERATION 2000 // X, Y, Z acceleration for travel (non printing) moves + +/** + * Default Jerk (mm/s) + * Override with M205 X Y Z E + * + * "Jerk" specifies the minimum speed change that requires acceleration. + * When changing speed and direction, if the difference is less than the + * value set here, it may happen instantaneously. + */ +#define DEFAULT_XJERK 5.0 +#define DEFAULT_YJERK 5.0 +#define DEFAULT_ZJERK 5.0 // Must be same as XY for delta +#define DEFAULT_EJERK 5.0 + +//=========================================================================== +//============================= Z Probe Options ============================= +//=========================================================================== +// @section probes + +// +// See http://marlinfw.org/docs/configuration/probes.html +// + +/** + * Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN + * + * Enable this option for a probe connected to the Z Min endstop pin. + */ +#define Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN + +/** + * Z_MIN_PROBE_ENDSTOP + * + * Enable this option for a probe connected to any pin except Z-Min. + * (By default Marlin assumes the Z-Max endstop pin.) + * To use a custom Z Probe pin, set Z_MIN_PROBE_PIN below. + * + * - The simplest option is to use a free endstop connector. + * - Use 5V for powered (usually inductive) sensors. + * + * - RAMPS 1.3/1.4 boards may use the 5V, GND, and Aux4->D32 pin: + * - For simple switches connect... + * - normally-closed switches to GND and D32. + * - normally-open switches to 5V and D32. + * + * WARNING: Setting the wrong pin may have unexpected and potentially + * disastrous consequences. Use with caution and do your homework. + * + */ +//#define Z_MIN_PROBE_ENDSTOP + +/** + * Probe Type + * + * Allen Key Probes, Servo Probes, Z-Sled Probes, FIX_MOUNTED_PROBE, etc. + * Activate one of these to use Auto Bed Leveling below. + */ + +/** + * The "Manual Probe" provides a means to do "Auto" Bed Leveling without a probe. + * Use G29 repeatedly, adjusting the Z height at each point with movement commands + * or (with LCD_BED_LEVELING) the LCD controller. + */ +//#define PROBE_MANUALLY + +/** + * A Fix-Mounted Probe either doesn't deploy or needs manual deployment. + * (e.g., an inductive probe or a nozzle-based probe-switch.) + */ +//#define FIX_MOUNTED_PROBE + +/** + * Z Servo Probe, such as an endstop switch on a rotating arm. + */ +//#define Z_ENDSTOP_SERVO_NR 0 // Defaults to SERVO 0 connector. +//#define Z_SERVO_ANGLES {70,0} // Z Servo Deploy and Stow angles + +/** + * The BLTouch probe uses a Hall effect sensor and emulates a servo. + */ +//#define BLTOUCH +#if ENABLED(BLTOUCH) + //#define BLTOUCH_DELAY 375 // (ms) Enable and increase if needed +#endif + +/** + * Enable one or more of the following if probing seems unreliable. + * Heaters and/or fans can be disabled during probing to minimize electrical + * noise. A delay can also be added to allow noise and vibration to settle. + * These options are most useful for the BLTouch probe, but may also improve + * readings with inductive probes and piezo sensors. + */ +#define PROBING_HEATERS_OFF // Turn heaters off when probing +#define PROBING_FANS_OFF // Turn fans off when probing +//#define DELAY_BEFORE_PROBING 200 // (ms) To prevent vibrations from triggering piezo sensors + +// A probe that is deployed and stowed with a solenoid pin (SOL1_PIN) +//#define SOLENOID_PROBE + +// A sled-mounted probe like those designed by Charles Bell. +//#define Z_PROBE_SLED +//#define SLED_DOCKING_OFFSET 5 // The extra distance the X axis must travel to pickup the sled. 0 should be fine but you can push it further if you'd like. + +// +// For Z_PROBE_ALLEN_KEY see the Delta example configurations. +// + +/** + * Z Probe to nozzle (X,Y) offset, relative to (0, 0). + * X and Y offsets must be integers. + * + * In the following example the X and Y offsets are both positive: + * #define X_PROBE_OFFSET_FROM_EXTRUDER 10 + * #define Y_PROBE_OFFSET_FROM_EXTRUDER 10 + * + * +-- BACK ---+ + * | | + * L | (+) P | R <-- probe (20,20) + * E | | I + * F | (-) N (+) | G <-- nozzle (10,10) + * T | | H + * | (-) | T + * | | + * O-- FRONT --+ + * (0,0) + */ +#define X_PROBE_OFFSET_FROM_EXTRUDER 0 // X offset: -left +right [of the nozzle] +#define Y_PROBE_OFFSET_FROM_EXTRUDER 0 // Y offset: -front +behind [the nozzle] +#define Z_PROBE_OFFSET_FROM_EXTRUDER 0 // Z offset: -below +above [the nozzle] + +// X and Y axis travel speed (mm/m) between probes +#define XY_PROBE_SPEED 2000 + +// Speed for the first approach when double-probing (with PROBE_DOUBLE_TOUCH) +#define Z_PROBE_SPEED_FAST HOMING_FEEDRATE_Z + +// Speed for the "accurate" probe of each point +#define Z_PROBE_SPEED_SLOW (Z_PROBE_SPEED_FAST / 2) + +// Use double touch for probing +//#define PROBE_DOUBLE_TOUCH + +/** + * Allen key retractable z-probe as seen on many Kossel delta printers - http://reprap.org/wiki/Kossel#Automatic_bed_leveling_probe + * Deploys by touching z-axis belt. Retracts by pushing the probe down. Uses Z_MIN_PIN. + */ +//#define Z_PROBE_ALLEN_KEY + +#if ENABLED(Z_PROBE_ALLEN_KEY) + // 2 or 3 sets of coordinates for deploying and retracting the spring loaded touch probe on G29, + // if servo actuated touch probe is not defined. Uncomment as appropriate for your printer/probe. + + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_X 30.0 + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Y DELTA_PRINTABLE_RADIUS + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Z 100.0 + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE XY_PROBE_SPEED + + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_X 0.0 + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Y DELTA_PRINTABLE_RADIUS + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Z 100.0 + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE (XY_PROBE_SPEED)/10 + + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_X Z_PROBE_ALLEN_KEY_DEPLOY_2_X * 0.75 + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Y Z_PROBE_ALLEN_KEY_DEPLOY_2_Y * 0.75 + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Z Z_PROBE_ALLEN_KEY_DEPLOY_2_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE XY_PROBE_SPEED + + #define Z_PROBE_ALLEN_KEY_STOW_1_X -64.0 // Move the probe into position + #define Z_PROBE_ALLEN_KEY_STOW_1_Y 56.0 + #define Z_PROBE_ALLEN_KEY_STOW_1_Z 23.0 + #define Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE XY_PROBE_SPEED + + #define Z_PROBE_ALLEN_KEY_STOW_2_X -64.0 // Push it down + #define Z_PROBE_ALLEN_KEY_STOW_2_Y 56.0 + #define Z_PROBE_ALLEN_KEY_STOW_2_Z 3.0 + #define Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE (XY_PROBE_SPEED)/10 + + #define Z_PROBE_ALLEN_KEY_STOW_3_X -64.0 // Move it up to clear + #define Z_PROBE_ALLEN_KEY_STOW_3_Y 56.0 + #define Z_PROBE_ALLEN_KEY_STOW_3_Z 50.0 + #define Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE XY_PROBE_SPEED + + #define Z_PROBE_ALLEN_KEY_STOW_4_X 0.0 + #define Z_PROBE_ALLEN_KEY_STOW_4_Y 0.0 + #define Z_PROBE_ALLEN_KEY_STOW_4_Z Z_PROBE_ALLEN_KEY_STOW_3_Z + #define Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE XY_PROBE_SPEED + +#endif // Z_PROBE_ALLEN_KEY + +/** + * Z probes require clearance when deploying, stowing, and moving between + * probe points to avoid hitting the bed and other hardware. + * Servo-mounted probes require extra space for the arm to rotate. + * Inductive probes need space to keep from triggering early. + * + * Use these settings to specify the distance (mm) to raise the probe (or + * lower the bed). The values set here apply over and above any (negative) + * probe Z Offset set with Z_PROBE_OFFSET_FROM_EXTRUDER, M851, or the LCD. + * Only integer values >= 1 are valid here. + * + * Example: `M851 Z-5` with a CLEARANCE of 4 => 9mm from bed to nozzle. + * But: `M851 Z+1` with a CLEARANCE of 2 => 2mm from bed to nozzle. + */ +#define Z_CLEARANCE_DEPLOY_PROBE 50 // Z Clearance for Deploy/Stow +#define Z_CLEARANCE_BETWEEN_PROBES 5 // Z Clearance between probe points + +// For M851 give a range for adjusting the Z probe offset +#define Z_PROBE_OFFSET_RANGE_MIN -20 +#define Z_PROBE_OFFSET_RANGE_MAX 20 + +// Enable the M48 repeatability test to test probe accuracy +//#define Z_MIN_PROBE_REPEATABILITY_TEST + +// For Inverting Stepper Enable Pins (Active Low) use 0, Non Inverting (Active High) use 1 +// :{ 0:'Low', 1:'High' } +#define X_ENABLE_ON 0 +#define Y_ENABLE_ON 0 +#define Z_ENABLE_ON 0 +#define E_ENABLE_ON 0 // For all extruders + +// Disables axis stepper immediately when it's not being used. +// WARNING: When motors turn off there is a chance of losing position accuracy! +#define DISABLE_X false +#define DISABLE_Y false +#define DISABLE_Z false +// Warn on display about possibly reduced accuracy +//#define DISABLE_REDUCED_ACCURACY_WARNING + +// @section extruder + +#define DISABLE_E false // For all extruders +#define DISABLE_INACTIVE_EXTRUDER true // Keep only the active extruder enabled. + +// @section machine + +// Invert the stepper direction. Change (or reverse the motor connector) if an axis goes the wrong way. +#define INVERT_X_DIR true // DELTA does not invert +#define INVERT_Y_DIR true +#define INVERT_Z_DIR true + +// Enable this option for Toshiba stepper drivers +//#define CONFIG_STEPPERS_TOSHIBA + +// @section extruder + +// For direct drive extruder v9 set to true, for geared extruder set to false. +#define INVERT_E0_DIR true +#define INVERT_E1_DIR false +#define INVERT_E2_DIR false +#define INVERT_E3_DIR false +#define INVERT_E4_DIR false + +// @section homing + +//#define NO_MOTION_BEFORE_HOMING // Inhibit movement until all axes have been homed + +//#define Z_HOMING_HEIGHT 15 // (in mm) Minimal z height before homing (G28) for Z clearance above the bed, clamps, ... + // Be sure you have this distance over your Z_MAX_POS in case. + +// Direction of endstops when homing; 1=MAX, -1=MIN +// :[-1,1] +#define X_HOME_DIR -1 // deltas always home to max +#define Y_HOME_DIR -1 +#define Z_HOME_DIR -1 + +// @section machine + +// The size of the print bed +#define X_BED_SIZE ((DELTA_PRINTABLE_RADIUS) * 2) +#define Y_BED_SIZE ((DELTA_PRINTABLE_RADIUS) * 2) + +// Travel limits (mm) after homing, corresponding to endstop positions. +#define X_MIN_POS -(DELTA_PRINTABLE_RADIUS) +#define Y_MIN_POS -(DELTA_PRINTABLE_RADIUS) +#define Z_MIN_POS -1 +#define X_MAX_POS DELTA_PRINTABLE_RADIUS +#define Y_MAX_POS DELTA_PRINTABLE_RADIUS +#define Z_MAX_POS MANUAL_Z_HOME_POS + +/** + * Software Endstops + * + * - Prevent moves outside the set machine bounds. + * - Individual axes can be disabled, if desired. + * - X and Y only apply to Cartesian robots. + * - Use 'M211' to set software endstops on/off or report current state + */ + +// Min software endstops curtail movement below minimum coordinate bounds +#define MIN_SOFTWARE_ENDSTOPS +#if ENABLED(MIN_SOFTWARE_ENDSTOPS) + #define MIN_SOFTWARE_ENDSTOP_X + #define MIN_SOFTWARE_ENDSTOP_Y + #define MIN_SOFTWARE_ENDSTOP_Z +#endif + +// Max software endstops curtail movement above maximum coordinate bounds +#define MAX_SOFTWARE_ENDSTOPS +#if ENABLED(MAX_SOFTWARE_ENDSTOPS) + #define MAX_SOFTWARE_ENDSTOP_X + #define MAX_SOFTWARE_ENDSTOP_Y + #define MAX_SOFTWARE_ENDSTOP_Z +#endif + +/** + * Filament Runout Sensor + * A mechanical or opto endstop is used to check for the presence of filament. + * + * RAMPS-based boards use SERVO3_PIN. + * For other boards you may need to define FIL_RUNOUT_PIN. + * By default the firmware assumes HIGH = has filament, LOW = ran out + */ +//#define FILAMENT_RUNOUT_SENSOR +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + #define FIL_RUNOUT_INVERTING false // set to true to invert the logic of the sensor. + #define ENDSTOPPULLUP_FIL_RUNOUT // Uncomment to use internal pullup for filament runout pins if the sensor is defined. + #define FILAMENT_RUNOUT_SCRIPT "M600" +#endif + +//=========================================================================== +//=============================== Bed Leveling ============================== +//=========================================================================== +// @section bedlevel + +/** + * Choose one of the options below to enable G29 Bed Leveling. The parameters + * and behavior of G29 will change depending on your selection. + * + * If using a Probe for Z Homing, enable Z_SAFE_HOMING also! + * + * - AUTO_BED_LEVELING_3POINT + * Probe 3 arbitrary points on the bed (that aren't collinear) + * You specify the XY coordinates of all 3 points. + * The result is a single tilted plane. Best for a flat bed. + * + * - AUTO_BED_LEVELING_LINEAR + * Probe several points in a grid. + * You specify the rectangle and the density of sample points. + * The result is a single tilted plane. Best for a flat bed. + * + * - AUTO_BED_LEVELING_BILINEAR + * Probe several points in a grid. + * You specify the rectangle and the density of sample points. + * The result is a mesh, best for large or uneven beds. + * + * - AUTO_BED_LEVELING_UBL (Unified Bed Leveling) + * A comprehensive bed leveling system combining the features and benefits + * of other systems. UBL also includes integrated Mesh Generation, Mesh + * Validation and Mesh Editing systems. Currently, UBL is only checked out + * for Cartesian Printers. That said, it was primarily designed to correct + * poor quality Delta Printers. If you feel adventurous and have a Delta, + * please post an issue if something doesn't work correctly. Initially, + * you will need to set a reduced bed size so you have a rectangular area + * to test on. + * + * - MESH_BED_LEVELING + * Probe a grid manually + * The result is a mesh, suitable for large or uneven beds. (See BILINEAR.) + * For machines without a probe, Mesh Bed Leveling provides a method to perform + * leveling in steps so you can manually adjust the Z height at each grid-point. + * With an LCD controller the process is guided step-by-step. + */ +//#define AUTO_BED_LEVELING_3POINT +//#define AUTO_BED_LEVELING_LINEAR +//#define AUTO_BED_LEVELING_BILINEAR +//#define AUTO_BED_LEVELING_UBL +//#define MESH_BED_LEVELING + +/** + * Enable detailed logging of G28, G29, M48, etc. + * Turn on with the command 'M111 S32'. + * NOTE: Requires a lot of PROGMEM! + */ +//#define DEBUG_LEVELING_FEATURE + +#if ENABLED(MESH_BED_LEVELING) || ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(AUTO_BED_LEVELING_UBL) + // Gradually reduce leveling correction until a set height is reached, + // at which point movement will be level to the machine's XY plane. + // The height can be set with M420 Z + //#define ENABLE_LEVELING_FADE_HEIGHT + + // Set the boundaries for probing (where the probe can reach). + #define DELTA_PROBEABLE_RADIUS (DELTA_PRINTABLE_RADIUS - 10) + +#endif + +#if ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(AUTO_BED_LEVELING_BILINEAR) + + // Set the number of grid points per dimension. + // Works best with 5 or more points in each dimension. + #define GRID_MAX_POINTS_X 9 + #define GRID_MAX_POINTS_Y GRID_MAX_POINTS_X + + #define LEFT_PROBE_BED_POSITION -(DELTA_PROBEABLE_RADIUS) + #define RIGHT_PROBE_BED_POSITION DELTA_PROBEABLE_RADIUS + #define FRONT_PROBE_BED_POSITION -(DELTA_PROBEABLE_RADIUS) + #define BACK_PROBE_BED_POSITION DELTA_PROBEABLE_RADIUS + + // The Z probe minimum outer margin (to validate G29 parameters). + #define MIN_PROBE_EDGE 10 + + // Probe along the Y axis, advancing X after each column + //#define PROBE_Y_FIRST + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + // Beyond the probed grid, continue the implied tilt? + // Default is to maintain the height of the nearest edge. + //#define EXTRAPOLATE_BEYOND_GRID + + // + // Experimental Subdivision of the grid by Catmull-Rom method. + // Synthesizes intermediate points to produce a more detailed mesh. + // + //#define ABL_BILINEAR_SUBDIVISION + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + // Number of subdivisions between probe points + #define BILINEAR_SUBDIVISIONS 3 + #endif + + #endif + +#elif ENABLED(AUTO_BED_LEVELING_3POINT) + + // 3 arbitrary points to probe. + // A simple cross-product is used to estimate the plane of the bed. + #define ABL_PROBE_PT_1_X 15 + #define ABL_PROBE_PT_1_Y 180 + #define ABL_PROBE_PT_2_X 15 + #define ABL_PROBE_PT_2_Y 20 + #define ABL_PROBE_PT_3_X 170 + #define ABL_PROBE_PT_3_Y 20 + +#elif ENABLED(AUTO_BED_LEVELING_UBL) + + //=========================================================================== + //========================= Unified Bed Leveling ============================ + //=========================================================================== + + #define MESH_INSET 1 // Mesh inset margin on print area + #define GRID_MAX_POINTS_X 10 // Don't use more than 15 points per axis, implementation limited. + #define GRID_MAX_POINTS_Y GRID_MAX_POINTS_X + + #define _PX(R,A) (R) * cos(RADIANS(A)) + #define _PY(R,A) (R) * sin(RADIANS(A)) + #define UBL_PROBE_PT_1_X _PX(DELTA_PROBEABLE_RADIUS, 0) // Probing points for 3-Point leveling of the mesh + #define UBL_PROBE_PT_1_Y _PY(DELTA_PROBEABLE_RADIUS, 0) + #define UBL_PROBE_PT_2_X _PX(DELTA_PROBEABLE_RADIUS, 120) + #define UBL_PROBE_PT_2_Y _PY(DELTA_PROBEABLE_RADIUS, 120) + #define UBL_PROBE_PT_3_X _PX(DELTA_PROBEABLE_RADIUS, 240) + #define UBL_PROBE_PT_3_Y _PY(DELTA_PROBEABLE_RADIUS, 240) + + #define UBL_G26_MESH_VALIDATION // Enable G26 mesh validation + #define UBL_MESH_EDIT_MOVES_Z // Sophisticated users prefer no movement of nozzle + #define UBL_SAVE_ACTIVE_ON_M500 // Save the currently active mesh in the current slot on M500 + +#elif ENABLED(MESH_BED_LEVELING) + + //=========================================================================== + //=================================== Mesh ================================== + //=========================================================================== + + #define MESH_INSET 10 // Mesh inset margin on print area + #define GRID_MAX_POINTS_X 3 // Don't use more than 7 points per axis, implementation limited. + #define GRID_MAX_POINTS_Y GRID_MAX_POINTS_X + + //#define MESH_G28_REST_ORIGIN // After homing all axes ('G28' or 'G28 XYZ') rest Z at Z_MIN_POS + +#endif // BED_LEVELING + +/** + * Use the LCD controller for bed leveling + * Requires MESH_BED_LEVELING or PROBE_MANUALLY + */ +//#define LCD_BED_LEVELING + +#if ENABLED(LCD_BED_LEVELING) + #define MBL_Z_STEP 0.025 // Step size while manually probing Z axis. + #define LCD_PROBE_Z_RANGE 4 // Z Range centered on Z_MIN_POS for LCD Z adjustment +#endif + +// Add a menu item to move between bed corners for manual bed adjustment +//#define LEVEL_BED_CORNERS + +/** + * Commands to execute at the end of G29 probing. + * Useful to retract or move the Z probe out of the way. + */ +//#define Z_PROBE_END_SCRIPT "G1 Z10 F12000\nG1 X15 Y330\nG1 Z0.5\nG1 Z10" + + +// @section homing + +// The center of the bed is at (X=0, Y=0) +#define BED_CENTER_AT_0_0 + +// Manually set the home position. Leave these undefined for automatic settings. +// For DELTA this is the top-center of the Cartesian print volume. +//#define MANUAL_X_HOME_POS 0 +//#define MANUAL_Y_HOME_POS 0 +//#define MANUAL_Z_HOME_POS DELTA_HEIGHT // Distance between the nozzle to printbed after homing + +// Use "Z Safe Homing" to avoid homing with a Z probe outside the bed area. +// +// With this feature enabled: +// +// - Allow Z homing only after X and Y homing AND stepper drivers still enabled. +// - If stepper drivers time out, it will need X and Y homing again before Z homing. +// - Move the Z probe (or nozzle) to a defined XY point before Z Homing when homing all axes (G28). +// - Prevent Z homing when the Z probe is outside bed area. +// +#define Z_SAFE_HOMING + +#if ENABLED(Z_SAFE_HOMING) + #define Z_SAFE_HOMING_X_POINT ((X_BED_SIZE) / 2) // X point for Z homing when homing all axes (G28). + #define Z_SAFE_HOMING_Y_POINT ((Y_BED_SIZE) / 2) // Y point for Z homing when homing all axes (G28). +#endif + +// Delta only homes to Z +#define HOMING_FEEDRATE_Z (100*60) + +//============================================================================= +//============================= Additional Features =========================== +//============================================================================= + +// @section extras + +// +// EEPROM +// +// The microcontroller can store settings in the EEPROM, e.g. max velocity... +// M500 - stores parameters in EEPROM +// M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily). +// M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to. +// +#define EEPROM_SETTINGS // Enable for M500 and M501 commands +//#define DISABLE_M503 // Saves ~2700 bytes of PROGMEM. Disable for release! +#define EEPROM_CHITCHAT // Give feedback on EEPROM commands. Disable to save PROGMEM. + +// +// Host Keepalive +// +// When enabled Marlin will send a busy status message to the host +// every couple of seconds when it can't accept commands. +// +#define HOST_KEEPALIVE_FEATURE // Disable this if your host doesn't like keepalive messages +#define DEFAULT_KEEPALIVE_INTERVAL 2 // Number of seconds between "busy" messages. Set with M113. +#define BUSY_WHILE_HEATING // Some hosts require "busy" messages even during heating + +// +// M100 Free Memory Watcher +// +//#define M100_FREE_MEMORY_WATCHER // Add M100 (Free Memory Watcher) to debug memory usage + +// +// G20/G21 Inch mode support +// +//#define INCH_MODE_SUPPORT + +// +// M149 Set temperature units support +// +//#define TEMPERATURE_UNITS_SUPPORT + +// @section temperature + +// Preheat Constants +#define PREHEAT_1_TEMP_HOTEND 185 +#define PREHEAT_1_TEMP_BED 70 +#define PREHEAT_1_FAN_SPEED 255 // Value from 0 to 255 + +#define PREHEAT_2_TEMP_HOTEND 240 +#define PREHEAT_2_TEMP_BED 100 +#define PREHEAT_2_FAN_SPEED 255 // Value from 0 to 255 + +/** + * Nozzle Park -- EXPERIMENTAL + * + * Park the nozzle at the given XYZ position on idle or G27. + * + * The "P" parameter controls the action applied to the Z axis: + * + * P0 (Default) If Z is below park Z raise the nozzle. + * P1 Raise the nozzle always to Z-park height. + * P2 Raise the nozzle by Z-park amount, limited to Z_MAX_POS. + */ +//#define NOZZLE_PARK_FEATURE + +#if ENABLED(NOZZLE_PARK_FEATURE) + // Specify a park position as { X, Y, Z } + #define NOZZLE_PARK_POINT { (X_MIN_POS + 10), (Y_MAX_POS - 10), 20 } +#endif + +/** + * Clean Nozzle Feature -- EXPERIMENTAL + * + * Adds the G12 command to perform a nozzle cleaning process. + * + * Parameters: + * P Pattern + * S Strokes / Repetitions + * T Triangles (P1 only) + * + * Patterns: + * P0 Straight line (default). This process requires a sponge type material + * at a fixed bed location. "S" specifies strokes (i.e. back-forth motions) + * between the start / end points. + * + * P1 Zig-zag pattern between (X0, Y0) and (X1, Y1), "T" specifies the + * number of zig-zag triangles to do. "S" defines the number of strokes. + * Zig-zags are done in whichever is the narrower dimension. + * For example, "G12 P1 S1 T3" will execute: + * + * -- + * | (X0, Y1) | /\ /\ /\ | (X1, Y1) + * | | / \ / \ / \ | + * A | | / \ / \ / \ | + * | | / \ / \ / \ | + * | (X0, Y0) | / \/ \/ \ | (X1, Y0) + * -- +--------------------------------+ + * |________|_________|_________| + * T1 T2 T3 + * + * P2 Circular pattern with middle at NOZZLE_CLEAN_CIRCLE_MIDDLE. + * "R" specifies the radius. "S" specifies the stroke count. + * Before starting, the nozzle moves to NOZZLE_CLEAN_START_POINT. + * + * Caveats: The ending Z should be the same as starting Z. + * Attention: EXPERIMENTAL. G-code arguments may change. + * + */ +//#define NOZZLE_CLEAN_FEATURE + +#if ENABLED(NOZZLE_CLEAN_FEATURE) + // Default number of pattern repetitions + #define NOZZLE_CLEAN_STROKES 12 + + // Default number of triangles + #define NOZZLE_CLEAN_TRIANGLES 3 + + // Specify positions as { X, Y, Z } + #define NOZZLE_CLEAN_START_POINT { 30, 30, (Z_MIN_POS + 1)} + #define NOZZLE_CLEAN_END_POINT {100, 60, (Z_MIN_POS + 1)} + + // Circular pattern radius + #define NOZZLE_CLEAN_CIRCLE_RADIUS 6.5 + // Circular pattern circle fragments number + #define NOZZLE_CLEAN_CIRCLE_FN 10 + // Middle point of circle + #define NOZZLE_CLEAN_CIRCLE_MIDDLE NOZZLE_CLEAN_START_POINT + + // Moves the nozzle to the initial position + #define NOZZLE_CLEAN_GOBACK +#endif + +/** + * Print Job Timer + * + * Automatically start and stop the print job timer on M104/M109/M190. + * + * M104 (hotend, no wait) - high temp = none, low temp = stop timer + * M109 (hotend, wait) - high temp = start timer, low temp = stop timer + * M190 (bed, wait) - high temp = start timer, low temp = none + * + * The timer can also be controlled with the following commands: + * + * M75 - Start the print job timer + * M76 - Pause the print job timer + * M77 - Stop the print job timer + */ +#define PRINTJOB_TIMER_AUTOSTART + +/** + * Print Counter + * + * Track statistical data such as: + * + * - Total print jobs + * - Total successful print jobs + * - Total failed print jobs + * - Total time printing + * + * View the current statistics with M78. + */ +#define PRINTCOUNTER + +//============================================================================= +//============================= LCD and SD support ============================ +//============================================================================= + +// @section lcd + +/** + * LCD LANGUAGE + * + * Select the language to display on the LCD. These languages are available: + * + * en, an, bg, ca, cn, cz, cz_utf8, de, el, el-gr, es, eu, fi, fr, fr_utf8, gl, + * hr, it, kana, kana_utf8, nl, pl, pt, pt_utf8, pt-br, pt-br_utf8, ru, sk_utf8, + * tr, uk, zh_CN, zh_TW, test + * + * :{ 'en':'English', 'an':'Aragonese', 'bg':'Bulgarian', 'ca':'Catalan', 'cn':'Chinese', 'cz':'Czech', 'cz_utf8':'Czech (UTF8)', 'de':'German', 'el':'Greek', 'el-gr':'Greek (Greece)', 'es':'Spanish', 'eu':'Basque-Euskera', 'fi':'Finnish', 'fr':'French', 'fr_utf8':'French (UTF8)', 'gl':'Galician', 'hr':'Croatian', 'it':'Italian', 'kana':'Japanese', 'kana_utf8':'Japanese (UTF8)', 'nl':'Dutch', 'pl':'Polish', 'pt':'Portuguese', 'pt-br':'Portuguese (Brazilian)', 'pt-br_utf8':'Portuguese (Brazilian UTF8)', 'pt_utf8':'Portuguese (UTF8)', 'ru':'Russian', 'sk_utf8':'Slovak (UTF8)', 'tr':'Turkish', 'uk':'Ukrainian', 'zh_CN':'Chinese (Simplified)', 'zh_TW':'Chinese (Taiwan)', test':'TEST' } + */ +#define LCD_LANGUAGE de + +/** + * LCD Character Set + * + * Note: This option is NOT applicable to Graphical Displays. + * + * All character-based LCDs provide ASCII plus one of these + * language extensions: + * + * - JAPANESE ... the most common + * - WESTERN ... with more accented characters + * - CYRILLIC ... for the Russian language + * + * To determine the language extension installed on your controller: + * + * - Compile and upload with LCD_LANGUAGE set to 'test' + * - Click the controller to view the LCD menu + * - The LCD will display Japanese, Western, or Cyrillic text + * + * See http://marlinfw.org/docs/development/lcd_language.html + * + * :['JAPANESE', 'WESTERN', 'CYRILLIC'] + */ +#define DISPLAY_CHARSET_HD44780 JAPANESE + +/** + * LCD TYPE + * + * Enable ULTRA_LCD for a 16x2, 16x4, 20x2, or 20x4 character-based LCD. + * Enable DOGLCD for a 128x64 (ST7565R) Full Graphical Display. + * (These options will be enabled automatically for most displays.) + * + * IMPORTANT: The U8glib library is required for Full Graphic Display! + * https://github.com/olikraus/U8glib_Arduino + */ +//#define ULTRA_LCD // Character based +//#define DOGLCD // Full graphics display + +/** + * SD CARD + * + * SD Card support is disabled by default. If your controller has an SD slot, + * you must uncomment the following option or it won't work. + * + */ +//#define SDSUPPORT + +/** + * SD CARD: SPI SPEED + * + * Enable one of the following items for a slower SPI transfer speed. + * This may be required to resolve "volume init" errors. + */ +//#define SPI_SPEED SPI_HALF_SPEED +//#define SPI_SPEED SPI_QUARTER_SPEED +//#define SPI_SPEED SPI_EIGHTH_SPEED + +/** + * SD CARD: ENABLE CRC + * + * Use CRC checks and retries on the SD communication. + */ +#define SD_CHECK_AND_RETRY + +// +// ENCODER SETTINGS +// +// This option overrides the default number of encoder pulses needed to +// produce one step. Should be increased for high-resolution encoders. +// +#define ENCODER_PULSES_PER_STEP 2 + +// +// Use this option to override the number of step signals required to +// move between next/prev menu items. +// +//#define ENCODER_STEPS_PER_MENU_ITEM 5 + +/** + * Encoder Direction Options + * + * Test your encoder's behavior first with both options disabled. + * + * Reversed Value Edit and Menu Nav? Enable REVERSE_ENCODER_DIRECTION. + * Reversed Menu Navigation only? Enable REVERSE_MENU_DIRECTION. + * Reversed Value Editing only? Enable BOTH options. + */ + +// +// This option reverses the encoder direction everywhere. +// +// Set this option if CLOCKWISE causes values to DECREASE +// +//#define REVERSE_ENCODER_DIRECTION + +// +// This option reverses the encoder direction for navigating LCD menus. +// +// If CLOCKWISE normally moves DOWN this makes it go UP. +// If CLOCKWISE normally moves UP this makes it go DOWN. +// +//#define REVERSE_MENU_DIRECTION + +// +// Individual Axis Homing +// +// Add individual axis homing items (Home X, Home Y, and Home Z) to the LCD menu. +// +//#define INDIVIDUAL_AXIS_HOMING_MENU + +// +// SPEAKER/BUZZER +// +// If you have a speaker that can produce tones, enable it here. +// By default Marlin assumes you have a buzzer with a fixed frequency. +// +//#define SPEAKER + +// +// The duration and frequency for the UI feedback sound. +// Set these to 0 to disable audio feedback in the LCD menus. +// +// Note: Test audio output with the G-Code: +// M300 S P +// +//#define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100 +//#define LCD_FEEDBACK_FREQUENCY_HZ 1000 + +// +// CONTROLLER TYPE: Standard +// +// Marlin supports a wide variety of controllers. +// Enable one of the following options to specify your controller. +// + +// +// ULTIMAKER Controller. +// +//#define ULTIMAKERCONTROLLER + +// +// ULTIPANEL as seen on Thingiverse. +// +//#define ULTIPANEL + +// +// PanelOne from T3P3 (via RAMPS 1.4 AUX2/AUX3) +// http://reprap.org/wiki/PanelOne +// +//#define PANEL_ONE + +// +// MaKr3d Makr-Panel with graphic controller and SD support. +// http://reprap.org/wiki/MaKr3d_MaKrPanel +// +//#define MAKRPANEL + +// +// ReprapWorld Graphical LCD +// https://reprapworld.com/?products_details&products_id/1218 +// +//#define REPRAPWORLD_GRAPHICAL_LCD + +// +// Activate one of these if you have a Panucatt Devices +// Viki 2.0 or mini Viki with Graphic LCD +// http://panucatt.com +// +//#define VIKI2 +//#define miniVIKI + +// +// Adafruit ST7565 Full Graphic Controller. +// https://github.com/eboston/Adafruit-ST7565-Full-Graphic-Controller/ +// +//#define ELB_FULL_GRAPHIC_CONTROLLER + +// +// RepRapDiscount Smart Controller. +// http://reprap.org/wiki/RepRapDiscount_Smart_Controller +// +// Note: Usually sold with a white PCB. +// +//#define REPRAP_DISCOUNT_SMART_CONTROLLER + +// +// GADGETS3D G3D LCD/SD Controller +// http://reprap.org/wiki/RAMPS_1.3/1.4_GADGETS3D_Shield_with_Panel +// +// Note: Usually sold with a blue PCB. +// +//#define G3D_PANEL + +// +// RepRapDiscount FULL GRAPHIC Smart Controller +// http://reprap.org/wiki/RepRapDiscount_Full_Graphic_Smart_Controller +// +//#define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER + +// +// MakerLab Mini Panel with graphic +// controller and SD support - http://reprap.org/wiki/Mini_panel +// +//#define MINIPANEL + +// +// RepRapWorld REPRAPWORLD_KEYPAD v1.1 +// http://reprapworld.com/?products_details&products_id=202&cPath=1591_1626 +// +// REPRAPWORLD_KEYPAD_MOVE_STEP sets how much should the robot move when a key +// is pressed, a value of 10.0 means 10mm per click. +// +//#define REPRAPWORLD_KEYPAD +//#define REPRAPWORLD_KEYPAD_MOVE_STEP 1.0 + +// +// RigidBot Panel V1.0 +// http://www.inventapart.com/ +// +//#define RIGIDBOT_PANEL + +// +// BQ LCD Smart Controller shipped by +// default with the BQ Hephestos 2 and Witbox 2. +// +//#define BQ_LCD_SMART_CONTROLLER + +// +// Cartesio UI +// http://mauk.cc/webshop/cartesio-shop/electronics/user-interface +// +//#define CARTESIO_UI + +// +// ANET_10 Controller supported displays. +// +//#define ANET_KEYPAD_LCD // Requires ADC_KEYPAD_PIN to be assigned to an analog pin. + // This LCD is known to be susceptible to electrical interference + // which scrambles the display. Pressing any button clears it up. +//#define ANET_FULL_GRAPHICS_LCD // Anet 128x64 full graphics lcd with rotary encoder as used on Anet A6 + // A clone of the RepRapDiscount full graphics display but with + // different pins/wiring (see pins_ANET_10.h). + +// +// LCD for Melzi Card with Graphical LCD +// +//#define LCD_FOR_MELZI + +// +// CONTROLLER TYPE: I2C +// +// Note: These controllers require the installation of Arduino's LiquidCrystal_I2C +// library. For more info: https://github.com/kiyoshigawa/LiquidCrystal_I2C +// + +// +// Elefu RA Board Control Panel +// http://www.elefu.com/index.php?route=product/product&product_id=53 +// +//#define RA_CONTROL_PANEL + +// +// Sainsmart YW Robot (LCM1602) LCD Display +// +// Note: This controller requires F.Malpartida's LiquidCrystal_I2C library +// https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home +// +//#define LCD_I2C_SAINSMART_YWROBOT + +// +// Generic LCM1602 LCD adapter +// +//#define LCM1602 + +// +// PANELOLU2 LCD with status LEDs, +// separate encoder and click inputs. +// +// Note: This controller requires Arduino's LiquidTWI2 library v1.2.3 or later. +// For more info: https://github.com/lincomatic/LiquidTWI2 +// +// Note: The PANELOLU2 encoder click input can either be directly connected to +// a pin (if BTN_ENC defined to != -1) or read through I2C (when BTN_ENC == -1). +// +//#define LCD_I2C_PANELOLU2 + +// +// Panucatt VIKI LCD with status LEDs, +// integrated click & L/R/U/D buttons, separate encoder inputs. +// +//#define LCD_I2C_VIKI + +// +// SSD1306 OLED full graphics generic display +// +//#define U8GLIB_SSD1306 + +// +// SAV OLEd LCD module support using either SSD1306 or SH1106 based LCD modules +// +//#define SAV_3DGLCD +#if ENABLED(SAV_3DGLCD) + //#define U8GLIB_SSD1306 + #define U8GLIB_SH1106 +#endif + +// +// CONTROLLER TYPE: Shift register panels +// +// 2 wire Non-latching LCD SR from https://goo.gl/aJJ4sH +// LCD configuration: http://reprap.org/wiki/SAV_3D_LCD +// +//#define SAV_3DLCD + +// +// TinyBoy2 128x64 OLED / Encoder Panel +// +//#define OLED_PANEL_TINYBOY2 + +// +// Makeboard 3D Printer Parts 3D Printer Mini Display 1602 Mini Controller +// https://www.aliexpress.com/item/Micromake-Makeboard-3D-Printer-Parts-3D-Printer-Mini-Display-1602-Mini-Controller-Compatible-with-Ramps-1/32765887917.html +// +//#define MAKEBOARD_MINI_2_LINE_DISPLAY_1602 + +// +// MKS MINI12864 with graphic controller and SD support +// http://reprap.org/wiki/MKS_MINI_12864 +// +//#define MKS_MINI_12864 + +// +// Factory display for Creality CR-10 +// https://www.aliexpress.com/item/Universal-LCD-12864-3D-Printer-Display-Screen-With-Encoder-For-CR-10-CR-7-Model/32833148327.html +// +// This is RAMPS-compatible using a single 10-pin connector. +// (For CR-10 owners who want to replace the Melzi Creality board but retain the display) +// +//#define CR10_STOCKDISPLAY + +// +// MKS OLED 1.3" 128 × 64 FULL GRAPHICS CONTROLLER +// http://reprap.org/wiki/MKS_12864OLED +// +// Tiny, but very sharp OLED display +// +//#define MKS_12864OLED + +// Silvergate GLCD controller +// http://github.com/android444/Silvergate +// +//#define SILVER_GATE_GLCD_CONTROLLER + +//============================================================================= +//=============================== Extra Features ============================== +//============================================================================= + +// @section extras + +// Increase the FAN PWM frequency. Removes the PWM noise but increases heating in the FET/Arduino +//#define FAST_PWM_FAN + +// Use software PWM to drive the fan, as for the heaters. This uses a very low frequency +// which is not as annoying as with the hardware PWM. On the other hand, if this frequency +// is too low, you should also increment SOFT_PWM_SCALE. +//#define FAN_SOFT_PWM + +// Incrementing this by 1 will double the software PWM frequency, +// affecting heaters, and the fan if FAN_SOFT_PWM is enabled. +// However, control resolution will be halved for each increment; +// at zero value, there are 128 effective control positions. +#define SOFT_PWM_SCALE 0 + +// If SOFT_PWM_SCALE is set to a value higher than 0, dithering can +// be used to mitigate the associated resolution loss. If enabled, +// some of the PWM cycles are stretched so on average the desired +// duty cycle is attained. +//#define SOFT_PWM_DITHER + +// Temperature status LEDs that display the hotend and bed temperature. +// If all hotends, bed temperature, and target temperature are under 54C +// then the BLUE led is on. Otherwise the RED led is on. (1C hysteresis) +//#define TEMP_STAT_LEDS + +// M240 Triggers a camera by emulating a Canon RC-1 Remote +// Data from: http://www.doc-diy.net/photo/rc-1_hacked/ +//#define PHOTOGRAPH_PIN 23 + +// SkeinForge sends the wrong arc g-codes when using Arc Point as fillet procedure +//#define SF_ARC_FIX + +// Support for the BariCUDA Paste Extruder +//#define BARICUDA + +// Support for BlinkM/CyzRgb +//#define BLINKM + +// Support for PCA9632 PWM LED driver +//#define PCA9632 + +/** + * RGB LED / LED Strip Control + * + * Enable support for an RGB LED connected to 5V digital pins, or + * an RGB Strip connected to MOSFETs controlled by digital pins. + * + * Adds the M150 command to set the LED (or LED strip) color. + * If pins are PWM capable (e.g., 4, 5, 6, 11) then a range of + * luminance values can be set from 0 to 255. + * For Neopixel LED an overall brightness parameter is also available. + * + * *** CAUTION *** + * LED Strips require a MOFSET Chip between PWM lines and LEDs, + * as the Arduino cannot handle the current the LEDs will require. + * Failure to follow this precaution can destroy your Arduino! + * NOTE: A separate 5V power supply is required! The Neopixel LED needs + * more current than the Arduino 5V linear regulator can produce. + * *** CAUTION *** + * + * LED Type. Enable only one of the following two options. + * + */ +//#define RGB_LED +//#define RGBW_LED + +#if ENABLED(RGB_LED) || ENABLED(RGBW_LED) + #define RGB_LED_R_PIN 34 + #define RGB_LED_G_PIN 43 + #define RGB_LED_B_PIN 35 + #define RGB_LED_W_PIN -1 +#endif + +// Support for Adafruit Neopixel LED driver +//#define NEOPIXEL_LED +#if ENABLED(NEOPIXEL_LED) + #define NEOPIXEL_TYPE NEO_GRBW // NEO_GRBW / NEO_GRB - four/three channel driver type (defined in Adafruit_NeoPixel.h) + #define NEOPIXEL_PIN 4 // LED driving pin on motherboard 4 => D4 (EXP2-5 on Printrboard) / 30 => PC7 (EXP3-13 on Rumba) + #define NEOPIXEL_PIXELS 30 // Number of LEDs in the strip + #define NEOPIXEL_IS_SEQUENTIAL // Sequential display for temperature change - LED by LED. Disable to change all LEDs at once. + #define NEOPIXEL_BRIGHTNESS 127 // Initial brightness (0-255) + //#define NEOPIXEL_STARTUP_TEST // Cycle through colors at startup +#endif + +/** + * Printer Event LEDs + * + * During printing, the LEDs will reflect the printer status: + * + * - Gradually change from blue to violet as the heated bed gets to target temp + * - Gradually change from violet to red as the hotend gets to temperature + * - Change to white to illuminate work surface + * - Change to green once print has finished + * - Turn off after the print has finished and the user has pushed a button + */ +#if ENABLED(BLINKM) || ENABLED(RGB_LED) || ENABLED(RGBW_LED) || ENABLED(PCA9632) || ENABLED(NEOPIXEL_RGBW_LED) + #define PRINTER_EVENT_LEDS +#endif + +/** + * R/C SERVO support + * Sponsored by TrinityLabs, Reworked by codexmas + */ + +/** + * Number of servos + * + * For some servo-related options NUM_SERVOS will be set automatically. + * Set this manually if there are extra servos needing manual control. + * Leave undefined or set to 0 to entirely disable the servo subsystem. + */ +//#define NUM_SERVOS 3 // Servo index starts with 0 for M280 command + +// Delay (in milliseconds) before the next move will start, to give the servo time to reach its target angle. +// 300ms is a good value but you can try less delay. +// If the servo can't reach the requested position, increase it. +#define SERVO_DELAY { 300 } + +// Servo deactivation +// +// With this option servos are powered only during movement, then turned off to prevent jitter. +//#define DEACTIVATE_SERVOS_AFTER_MOVE + +/** + * Filament Width Sensor + * + * Measures the filament width in real-time and adjusts + * flow rate to compensate for any irregularities. + * + * Also allows the measured filament diameter to set the + * extrusion rate, so the slicer only has to specify the + * volume. + * + * Only a single extruder is supported at this time. + * + * 34 RAMPS_14 : Analog input 5 on the AUX2 connector + * 81 PRINTRBOARD : Analog input 2 on the Exp1 connector (version B,C,D,E) + * 301 RAMBO : Analog input 3 + * + * Note: May require analog pins to be defined for other boards. + */ +//#define FILAMENT_WIDTH_SENSOR + +#define DEFAULT_NOMINAL_FILAMENT_DIA 1.75 // (mm) Diameter of the filament generally used (3.0 or 1.75mm), also used in the slicer. Used to validate sensor reading. + +#if ENABLED(FILAMENT_WIDTH_SENSOR) + #define FILAMENT_SENSOR_EXTRUDER_NUM 0 // Index of the extruder that has the filament sensor (0,1,2,3) + #define MEASUREMENT_DELAY_CM 14 // (cm) The distance from the filament sensor to the melting chamber + + #define MEASURED_UPPER_LIMIT 3.30 // (mm) Upper limit used to validate sensor reading + #define MEASURED_LOWER_LIMIT 1.90 // (mm) Lower limit used to validate sensor reading + #define MAX_MEASUREMENT_DELAY 20 // (bytes) Buffer size for stored measurements (1 byte per cm). Must be larger than MEASUREMENT_DELAY_CM. + + #define DEFAULT_MEASURED_FILAMENT_DIA DEFAULT_NOMINAL_FILAMENT_DIA // Set measured to nominal initially + + // Display filament width on the LCD status line. Status messages will expire after 5 seconds. + //#define FILAMENT_LCD_DISPLAY +#endif + +#endif // CONFIGURATION_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration_adv.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration_adv.h new file mode 100644 index 0000000..963ae1b --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Configuration_adv.h @@ -0,0 +1,1515 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Configuration_adv.h + * + * Advanced settings. + * Only change these if you know exactly what you're doing. + * Some of these settings can damage your printer if improperly set! + * + * Basic settings can be found in Configuration.h + * + */ +#ifndef CONFIGURATION_ADV_H +#define CONFIGURATION_ADV_H +#define CONFIGURATION_ADV_H_VERSION 010107 + +// @section temperature + +//=========================================================================== +//=============================Thermal Settings ============================ +//=========================================================================== + +#if DISABLED(PIDTEMPBED) + #define BED_CHECK_INTERVAL 5000 // ms between checks in bang-bang control + #if ENABLED(BED_LIMIT_SWITCHING) + #define BED_HYSTERESIS 2 // Only disable heating if T>target+BED_HYSTERESIS and enable heating if T>target-BED_HYSTERESIS + #endif +#endif + +/** + * Thermal Protection provides additional protection to your printer from damage + * and fire. Marlin always includes safe min and max temperature ranges which + * protect against a broken or disconnected thermistor wire. + * + * The issue: If a thermistor falls out, it will report the much lower + * temperature of the air in the room, and the the firmware will keep + * the heater on. + * + * The solution: Once the temperature reaches the target, start observing. + * If the temperature stays too far below the target (hysteresis) for too + * long (period), the firmware will halt the machine as a safety precaution. + * + * If you get false positives for "Thermal Runaway", increase + * THERMAL_PROTECTION_HYSTERESIS and/or THERMAL_PROTECTION_PERIOD + */ +#if ENABLED(THERMAL_PROTECTION_HOTENDS) + #define THERMAL_PROTECTION_PERIOD 40 // Seconds + #define THERMAL_PROTECTION_HYSTERESIS 4 // Degrees Celsius + + /** + * Whenever an M104, M109, or M303 increases the target temperature, the + * firmware will wait for the WATCH_TEMP_PERIOD to expire. If the temperature + * hasn't increased by WATCH_TEMP_INCREASE degrees, the machine is halted and + * requires a hard reset. This test restarts with any M104/M109/M303, but only + * if the current temperature is far enough below the target for a reliable + * test. + * + * If you get false positives for "Heating failed", increase WATCH_TEMP_PERIOD + * and/or decrease WATCH_TEMP_INCREASE. WATCH_TEMP_INCREASE should not be set + * below 2. + */ + #define WATCH_TEMP_PERIOD 20 // Seconds + #define WATCH_TEMP_INCREASE 2 // Degrees Celsius +#endif + +/** + * Thermal Protection parameters for the bed are just as above for hotends. + */ +#if ENABLED(THERMAL_PROTECTION_BED) + #define THERMAL_PROTECTION_BED_PERIOD 20 // Seconds + #define THERMAL_PROTECTION_BED_HYSTERESIS 2 // Degrees Celsius + + /** + * As described above, except for the bed (M140/M190/M303). + */ + #define WATCH_BED_TEMP_PERIOD 60 // Seconds + #define WATCH_BED_TEMP_INCREASE 2 // Degrees Celsius +#endif + +#if ENABLED(PIDTEMP) + // this adds an experimental additional term to the heating power, proportional to the extrusion speed. + // if Kc is chosen well, the additional required power due to increased melting should be compensated. + //#define PID_EXTRUSION_SCALING + #if ENABLED(PID_EXTRUSION_SCALING) + #define DEFAULT_Kc (100) //heating power=Kc*(e_speed) + #define LPQ_MAX_LEN 50 + #endif +#endif + +/** + * Automatic Temperature: + * The hotend target temperature is calculated by all the buffered lines of gcode. + * The maximum buffered steps/sec of the extruder motor is called "se". + * Start autotemp mode with M109 S B F + * The target temperature is set to mintemp+factor*se[steps/sec] and is limited by + * mintemp and maxtemp. Turn this off by executing M109 without F* + * Also, if the temperature is set to a value below mintemp, it will not be changed by autotemp. + * On an Ultimaker, some initial testing worked with M109 S215 B260 F1 in the start.gcode + */ +#define AUTOTEMP +#if ENABLED(AUTOTEMP) + #define AUTOTEMP_OLDWEIGHT 0.98 +#endif + +// Show Temperature ADC value +// Enable for M105 to include ADC values read from temperature sensors. +//#define SHOW_TEMP_ADC_VALUES + +/** + * High Temperature Thermistor Support + * + * Thermistors able to support high temperature tend to have a hard time getting + * good readings at room and lower temperatures. This means HEATER_X_RAW_LO_TEMP + * will probably be caught when the heating element first turns on during the + * preheating process, which will trigger a min_temp_error as a safety measure + * and force stop everything. + * To circumvent this limitation, we allow for a preheat time (during which, + * min_temp_error won't be triggered) and add a min_temp buffer to handle + * aberrant readings. + * + * If you want to enable this feature for your hotend thermistor(s) + * uncomment and set values > 0 in the constants below + */ + +// The number of consecutive low temperature errors that can occur +// before a min_temp_error is triggered. (Shouldn't be more than 10.) +//#define MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED 0 + +// The number of milliseconds a hotend will preheat before starting to check +// the temperature. This value should NOT be set to the time it takes the +// hot end to reach the target temperature, but the time it takes to reach +// the minimum temperature your thermistor can read. The lower the better/safer. +// This shouldn't need to be more than 30 seconds (30000) +//#define MILLISECONDS_PREHEAT_TIME 0 + +// @section extruder + +// Extruder runout prevention. +// If the machine is idle and the temperature over MINTEMP +// then extrude some filament every couple of SECONDS. +//#define EXTRUDER_RUNOUT_PREVENT +#if ENABLED(EXTRUDER_RUNOUT_PREVENT) + #define EXTRUDER_RUNOUT_MINTEMP 190 + #define EXTRUDER_RUNOUT_SECONDS 30 + #define EXTRUDER_RUNOUT_SPEED 1500 // mm/m + #define EXTRUDER_RUNOUT_EXTRUDE 5 // mm +#endif + +// @section temperature + +//These defines help to calibrate the AD595 sensor in case you get wrong temperature measurements. +//The measured temperature is defined as "actualTemp = (measuredTemp * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET" +#define TEMP_SENSOR_AD595_OFFSET 0.0 +#define TEMP_SENSOR_AD595_GAIN 1.0 + +/** + * Controller Fan + * To cool down the stepper drivers and MOSFETs. + * + * The fan will turn on automatically whenever any stepper is enabled + * and turn off after a set period after all steppers are turned off. + */ +#define USE_CONTROLLER_FAN +#if ENABLED(USE_CONTROLLER_FAN) + #define CONTROLLER_FAN_PIN FAN1_PIN // Set a custom pin for the controller fan + #define CONTROLLERFAN_SECS 60 // Duration in seconds for the fan to run after all motors are disabled + #define CONTROLLERFAN_SPEED 255 // 255 == full speed +#endif + +// When first starting the main fan, run it at full speed for the +// given number of milliseconds. This gets the fan spinning reliably +// before setting a PWM value. (Does not work with software PWM for fan on Sanguinololu) +#define FAN_KICKSTART_TIME 250 + +// This defines the minimal speed for the main fan, run in PWM mode +// to enable uncomment and set minimal PWM speed for reliable running (1-255) +// if fan speed is [1 - (FAN_MIN_PWM-1)] it is set to FAN_MIN_PWM +#define FAN_MIN_PWM 50 + +// @section extruder + +/** + * Extruder cooling fans + * + * Extruder auto fans automatically turn on when their extruders' + * temperatures go above EXTRUDER_AUTO_FAN_TEMPERATURE. + * + * Your board's pins file specifies the recommended pins. Override those here + * or set to -1 to disable completely. + * + * Multiple extruders can be assigned to the same pin in which case + * the fan will turn on when any selected extruder is above the threshold. + */ +#define E0_AUTO_FAN_PIN FAN2_PIN +#define E1_AUTO_FAN_PIN -1 +#define E2_AUTO_FAN_PIN -1 +#define E3_AUTO_FAN_PIN -1 +#define E4_AUTO_FAN_PIN -1 +#define EXTRUDER_AUTO_FAN_TEMPERATURE 50 +#define EXTRUDER_AUTO_FAN_SPEED 255 // == half speed + + + +/** + * Part-Cooling Fan Multiplexer + * + * This feature allows you to digitally multiplex the fan output. + * The multiplexer is automatically switched at tool-change. + * Set FANMUX[012]_PINs below for up to 2, 4, or 8 multiplexed fans. + */ +#define FANMUX0_PIN -1 +#define FANMUX1_PIN -1 +#define FANMUX2_PIN -1 + +/** + * M355 Case Light on-off / brightness + */ +//#define CASE_LIGHT_ENABLE +#if ENABLED(CASE_LIGHT_ENABLE) + //#define CASE_LIGHT_PIN 4 // Override the default pin if needed + #define INVERT_CASE_LIGHT false // Set true if Case Light is ON when pin is LOW + #define CASE_LIGHT_DEFAULT_ON true // Set default power-up state on + #define CASE_LIGHT_DEFAULT_BRIGHTNESS 105 // Set default power-up brightness (0-255, requires PWM pin) + //#define MENU_ITEM_CASE_LIGHT // Add a Case Light option to the LCD main menu +#endif + +//=========================================================================== +//============================ Mechanical Settings ========================== +//=========================================================================== + +// @section homing + +// If you want endstops to stay on (by default) even when not homing +// enable this option. Override at any time with M120, M121. +//#define ENDSTOPS_ALWAYS_ON_DEFAULT + +// @section extras + +//#define Z_LATE_ENABLE // Enable Z the last moment. Needed if your Z driver overheats. + +/** + * Dual Steppers / Dual Endstops + * + * This section will allow you to use extra E drivers to drive a second motor for X, Y, or Z axes. + * + * For example, set X_DUAL_STEPPER_DRIVERS setting to use a second motor. If the motors need to + * spin in opposite directions set INVERT_X2_VS_X_DIR. If the second motor needs its own endstop + * set X_DUAL_ENDSTOPS. This can adjust for "racking." Use X2_USE_ENDSTOP to set the endstop plug + * that should be used for the second endstop. Extra endstops will appear in the output of 'M119'. + * + * Use X_DUAL_ENDSTOP_ADJUSTMENT to adjust for mechanical imperfection. After homing both motors + * this offset is applied to the X2 motor. To find the offset home the X axis, and measure the error + * in X2. Dual endstop offsets can be set at runtime with 'M666 X Y Z'. + */ + +//#define X_DUAL_STEPPER_DRIVERS +#if ENABLED(X_DUAL_STEPPER_DRIVERS) + #define INVERT_X2_VS_X_DIR true // Set 'true' if X motors should rotate in opposite directions + //#define X_DUAL_ENDSTOPS + #if ENABLED(X_DUAL_ENDSTOPS) + #define X2_USE_ENDSTOP _XMAX_ + #define X_DUAL_ENDSTOPS_ADJUSTMENT 0 + #endif +#endif + +//#define Y_DUAL_STEPPER_DRIVERS +#if ENABLED(Y_DUAL_STEPPER_DRIVERS) + #define INVERT_Y2_VS_Y_DIR true // Set 'true' if Y motors should rotate in opposite directions + //#define Y_DUAL_ENDSTOPS + #if ENABLED(Y_DUAL_ENDSTOPS) + #define Y2_USE_ENDSTOP _YMAX_ + #define Y_DUAL_ENDSTOPS_ADJUSTMENT 0 + #endif +#endif + +//#define Z_DUAL_STEPPER_DRIVERS +#if ENABLED(Z_DUAL_STEPPER_DRIVERS) + //#define Z_DUAL_ENDSTOPS + #if ENABLED(Z_DUAL_ENDSTOPS) + #define Z2_USE_ENDSTOP _XMAX_ + #define Z_DUAL_ENDSTOPS_ADJUSTMENT 0 + #endif +#endif + +// Dual X Steppers +// Uncomment this option to drive two X axis motors. +// The next unused E driver will be assigned to the second X stepper. +//#define X_DUAL_STEPPER_DRIVERS +#if ENABLED(X_DUAL_STEPPER_DRIVERS) + // Set true if the two X motors need to rotate in opposite directions + #define INVERT_X2_VS_X_DIR true +#endif + +// Dual Y Steppers +// Uncomment this option to drive two Y axis motors. +// The next unused E driver will be assigned to the second Y stepper. +//#define Y_DUAL_STEPPER_DRIVERS +#if ENABLED(Y_DUAL_STEPPER_DRIVERS) + // Set true if the two Y motors need to rotate in opposite directions + #define INVERT_Y2_VS_Y_DIR true +#endif + +// A single Z stepper driver is usually used to drive 2 stepper motors. +// Uncomment this option to use a separate stepper driver for each Z axis motor. +// The next unused E driver will be assigned to the second Z stepper. +//#define Z_DUAL_STEPPER_DRIVERS + +#if ENABLED(Z_DUAL_STEPPER_DRIVERS) + + // Z_DUAL_ENDSTOPS is a feature to enable the use of 2 endstops for both Z steppers - Let's call them Z stepper and Z2 stepper. + // That way the machine is capable to align the bed during home, since both Z steppers are homed. + // There is also an implementation of M666 (software endstops adjustment) to this feature. + // After Z homing, this adjustment is applied to just one of the steppers in order to align the bed. + // One just need to home the Z axis and measure the distance difference between both Z axis and apply the math: Z adjust = Z - Z2. + // If the Z stepper axis is closer to the bed, the measure Z > Z2 (yes, it is.. think about it) and the Z adjust would be positive. + // Play a little bit with small adjustments (0.5mm) and check the behaviour. + // The M119 (endstops report) will start reporting the Z2 Endstop as well. + + //#define Z_DUAL_ENDSTOPS + + #if ENABLED(Z_DUAL_ENDSTOPS) + #define Z2_USE_ENDSTOP _XMAX_ + #define Z_DUAL_ENDSTOPS_ADJUSTMENT 0 // Use M666 to determine/test this value + #endif + +#endif // Z_DUAL_STEPPER_DRIVERS + +// Enable this for dual x-carriage printers. +// A dual x-carriage design has the advantage that the inactive extruder can be parked which +// prevents hot-end ooze contaminating the print. It also reduces the weight of each x-carriage +// allowing faster printing speeds. Connect your X2 stepper to the first unused E plug. +//#define DUAL_X_CARRIAGE +#if ENABLED(DUAL_X_CARRIAGE) + // Configuration for second X-carriage + // Note: the first x-carriage is defined as the x-carriage which homes to the minimum endstop; + // the second x-carriage always homes to the maximum endstop. + #define X2_MIN_POS 80 // set minimum to ensure second x-carriage doesn't hit the parked first X-carriage + #define X2_MAX_POS 353 // set maximum to the distance between toolheads when both heads are homed + #define X2_HOME_DIR 1 // the second X-carriage always homes to the maximum endstop position + #define X2_HOME_POS X2_MAX_POS // default home position is the maximum carriage position + // However: In this mode the HOTEND_OFFSET_X value for the second extruder provides a software + // override for X2_HOME_POS. This also allow recalibration of the distance between the two endstops + // without modifying the firmware (through the "M218 T1 X???" command). + // Remember: you should set the second extruder x-offset to 0 in your slicer. + + // There are a few selectable movement modes for dual x-carriages using M605 S + // Mode 0 (DXC_FULL_CONTROL_MODE): Full control. The slicer has full control over both x-carriages and can achieve optimal travel results + // as long as it supports dual x-carriages. (M605 S0) + // Mode 1 (DXC_AUTO_PARK_MODE) : Auto-park mode. The firmware will automatically park and unpark the x-carriages on tool changes so + // that additional slicer support is not required. (M605 S1) + // Mode 2 (DXC_DUPLICATION_MODE) : Duplication mode. The firmware will transparently make the second x-carriage and extruder copy all + // actions of the first x-carriage. This allows the printer to print 2 arbitrary items at + // once. (2nd extruder x offset and temp offset are set using: M605 S2 [Xnnn] [Rmmm]) + + // This is the default power-up mode which can be later using M605. + #define DEFAULT_DUAL_X_CARRIAGE_MODE DXC_FULL_CONTROL_MODE + + // Default settings in "Auto-park Mode" + #define TOOLCHANGE_PARK_ZLIFT 0.2 // the distance to raise Z axis when parking an extruder + #define TOOLCHANGE_UNPARK_ZLIFT 1 // the distance to raise Z axis when unparking an extruder + + // Default x offset in duplication mode (typically set to half print bed width) + #define DEFAULT_DUPLICATION_X_OFFSET 100 + +#endif // DUAL_X_CARRIAGE + +// Activate a solenoid on the active extruder with M380. Disable all with M381. +// Define SOL0_PIN, SOL1_PIN, etc., for each extruder that has a solenoid. +//#define EXT_SOLENOID + +// @section homing + +//homing hits the endstop, then retracts by this distance, before it tries to slowly bump again: +#define X_HOME_BUMP_MM 5 +#define Y_HOME_BUMP_MM 5 +#define Z_HOME_BUMP_MM 5 // deltas need the same for all three axes +#define HOMING_BUMP_DIVISOR { 10, 10, 10 } // Re-Bump Speed Divisor (Divides the Homing Feedrate) +//#define QUICK_HOME // If homing includes X and Y, do a diagonal move initially + +// When G28 is called, this option will make Y home before X +//#define HOME_Y_BEFORE_X + +// @section machine + +#define AXIS_RELATIVE_MODES {false, false, false, false} + +// Allow duplication mode with a basic dual-nozzle extruder +//#define DUAL_NOZZLE_DUPLICATION_MODE + +// By default pololu step drivers require an active high signal. However, some high power drivers require an active low signal as step. +#define INVERT_X_STEP_PIN false +#define INVERT_Y_STEP_PIN false +#define INVERT_Z_STEP_PIN false +#define INVERT_E_STEP_PIN false + +// Default stepper release if idle. Set to 0 to deactivate. +// Steppers will shut down DEFAULT_STEPPER_DEACTIVE_TIME seconds after the last move when DISABLE_INACTIVE_? is true. +// Time can be set by M18 and M84. +#define DEFAULT_STEPPER_DEACTIVE_TIME 60 +#define DISABLE_INACTIVE_X true +#define DISABLE_INACTIVE_Y true +#define DISABLE_INACTIVE_Z true // set to false if the nozzle will fall down on your printed part when print has finished. +#define DISABLE_INACTIVE_E true + +#define DEFAULT_MINIMUMFEEDRATE 0.0 // minimum feedrate +#define DEFAULT_MINTRAVELFEEDRATE 0.0 + +//#define HOME_AFTER_DEACTIVATE // Require rehoming after steppers are deactivated + +// @section lcd + +#if ENABLED(ULTIPANEL) + #define MANUAL_FEEDRATE_XYZ 50*60 + #define MANUAL_FEEDRATE { MANUAL_FEEDRATE_XYZ, MANUAL_FEEDRATE_XYZ, MANUAL_FEEDRATE_XYZ, 60 } // Feedrates for manual moves along X, Y, Z, E from panel + #define ULTIPANEL_FEEDMULTIPLY // Comment to disable setting feedrate multiplier via encoder +#endif + +// @section extras + +// minimum time in microseconds that a movement needs to take if the buffer is emptied. +#define DEFAULT_MINSEGMENTTIME 20000 + +// If defined the movements slow down when the look ahead buffer is only half full +// (don't use SLOWDOWN with DELTA because DELTA generates hundreds of segments per second) +//#define SLOWDOWN + +// Frequency limit +// See nophead's blog for more info +// Not working O +//#define XY_FREQUENCY_LIMIT 15 + +// Minimum planner junction speed. Sets the default minimum speed the planner plans for at the end +// of the buffer and all stops. This should not be much greater than zero and should only be changed +// if unwanted behavior is observed on a user's machine when running at very slow speeds. +#define MINIMUM_PLANNER_SPEED 0.05 // (mm/sec) + +// Microstep setting (Only functional when stepper driver microstep pins are connected to MCU. +#define MICROSTEP_MODES {16,16,16,16,16} // [1,2,4,8,16] + +/** + * @section stepper motor current + * + * Some boards have a means of setting the stepper motor current via firmware. + * + * The power on motor currents are set by: + * PWM_MOTOR_CURRENT - used by MINIRAMBO & ULTIMAIN_2 + * known compatible chips: A4982 + * DIGIPOT_MOTOR_CURRENT - used by BQ_ZUM_MEGA_3D, RAMBO & SCOOVO_X9H + * known compatible chips: AD5206 + * DAC_MOTOR_CURRENT_DEFAULT - used by PRINTRBOARD_REVF & RIGIDBOARD_V2 + * known compatible chips: MCP4728 + * DIGIPOT_I2C_MOTOR_CURRENTS - used by 5DPRINT, AZTEEG_X3_PRO, MIGHTYBOARD_REVE + * known compatible chips: MCP4451, MCP4018 + * + * Motor currents can also be set by M907 - M910 and by the LCD. + * M907 - applies to all. + * M908 - BQ_ZUM_MEGA_3D, RAMBO, PRINTRBOARD_REVF, RIGIDBOARD_V2 & SCOOVO_X9H + * M909, M910 & LCD - only PRINTRBOARD_REVF & RIGIDBOARD_V2 + */ +//#define PWM_MOTOR_CURRENT { 1300, 1300, 1250 } // Values in milliamps +//#define DIGIPOT_MOTOR_CURRENT { 135,135,135,135,135 } // Values 0-255 (RAMBO 135 = ~0.75A, 185 = ~1A) +//#define DAC_MOTOR_CURRENT_DEFAULT { 70, 80, 90, 80 } // Default drive percent - X, Y, Z, E axis + +// Use an I2C based DIGIPOT (e.g., Azteeg X3 Pro) +//#define DIGIPOT_I2C +#if ENABLED(DIGIPOT_I2C) && !defined(DIGIPOT_I2C_ADDRESS_A) + /** + * Common slave addresses: + * + * A (A shifted) B (B shifted) IC + * Smoothie 0x2C (0x58) 0x2D (0x5A) MCP4451 + * AZTEEG_X3_PRO 0x2C (0x58) 0x2E (0x5C) MCP4451 + * MIGHTYBOARD_REVE 0x2F (0x5E) MCP4018 + */ + #define DIGIPOT_I2C_ADDRESS_A 0x2C // unshifted slave address for first DIGIPOT + #define DIGIPOT_I2C_ADDRESS_B 0x2D // unshifted slave address for second DIGIPOT +#endif + +//#define DIGIPOT_MCP4018 // Requires library from https://github.com/stawel/SlowSoftI2CMaster +#define DIGIPOT_I2C_NUM_CHANNELS 8 // 5DPRINT: 4 AZTEEG_X3_PRO: 8 +// Actual motor currents in Amps, need as many here as DIGIPOT_I2C_NUM_CHANNELS +#define DIGIPOT_I2C_MOTOR_CURRENTS { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } // AZTEEG_X3_PRO + +//=========================================================================== +//=============================Additional Features=========================== +//=========================================================================== + +#define ENCODER_RATE_MULTIPLIER // If defined, certain menu edit operations automatically multiply the steps when the encoder is moved quickly +#define ENCODER_10X_STEPS_PER_SEC 20 // If the encoder steps per sec exceeds this value, multiply steps moved x10 to quickly advance the value +#define ENCODER_100X_STEPS_PER_SEC 160 // If the encoder steps per sec exceeds this value, multiply steps moved x100 to really quickly advance the value + +//#define CHDK 4 //Pin for triggering CHDK to take a picture see how to use it here http://captain-slow.dk/2014/03/09/3d-printing-timelapses/ +#define CHDK_DELAY 50 //How long in ms the pin should stay HIGH before going LOW again + +// @section lcd + +// Include a page of printer information in the LCD Main Menu +//#define LCD_INFO_MENU + +// Scroll a longer status message into view +#define STATUS_MESSAGE_SCROLLING + +// On the Info Screen, display XY with one decimal place when possible +//#define LCD_DECIMAL_SMALL_XY + +// The timeout (in ms) to return to the status screen from sub-menus +//#define LCD_TIMEOUT_TO_STATUS 15000 + +#if ENABLED(SDSUPPORT) + + // Some RAMPS and other boards don't detect when an SD card is inserted. You can work + // around this by connecting a push button or single throw switch to the pin defined + // as SD_DETECT_PIN in your board's pins definitions. + // This setting should be disabled unless you are using a push button, pulling the pin to ground. + // Note: This is always disabled for ULTIPANEL (except ELB_FULL_GRAPHIC_CONTROLLER). + #define SD_DETECT_INVERTED + + #define SD_FINISHED_STEPPERRELEASE true // Disable steppers when SD Print is finished + #define SD_FINISHED_RELEASECOMMAND "M84 X Y Z E" // You might want to keep the z enabled so your bed stays in place. + + // Reverse SD sort to show "more recent" files first, according to the card's FAT. + // Since the FAT gets out of order with usage, SDCARD_SORT_ALPHA is recommended. + #define SDCARD_RATHERRECENTFIRST + + // Add an option in the menu to run all auto#.g files + #define MENU_ADDAUTOSTART + + /** + * Sort SD file listings in alphabetical order. + * + * With this option enabled, items on SD cards will be sorted + * by name for easier navigation. + * + * By default... + * + * - Use the slowest -but safest- method for sorting. + * - Folders are sorted to the top. + * - The sort key is statically allocated. + * - No added G-code (M34) support. + * - 40 item sorting limit. (Items after the first 40 are unsorted.) + * + * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the + * compiler to calculate the worst-case usage and throw an error if the SRAM + * limit is exceeded. + * + * - SDSORT_USES_RAM provides faster sorting via a static directory buffer. + * - SDSORT_USES_STACK does the same, but uses a local stack-based buffer. + * - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!) + * - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!) + */ + //#define SDCARD_SORT_ALPHA + + // SD Card Sorting options + #if ENABLED(SDCARD_SORT_ALPHA) + #define SDSORT_LIMIT 40 // Maximum number of sorted items (10-256). Costs 27 bytes each. + #define FOLDER_SORTING -1 // -1=above 0=none 1=below + #define SDSORT_GCODE false // Allow turning sorting on/off with LCD and M34 g-code. + #define SDSORT_USES_RAM false // Pre-allocate a static array for faster pre-sorting. + #define SDSORT_USES_STACK false // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.) + #define SDSORT_CACHE_NAMES false // Keep sorted items in RAM longer for speedy performance. Most expensive option. + #define SDSORT_DYNAMIC_RAM false // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use! + #define SDSORT_CACHE_VFATS 2 // Maximum number of 13-byte VFAT entries to use for sorting. + // Note: Only affects SCROLL_LONG_FILENAMES with SDSORT_CACHE_NAMES but not SDSORT_DYNAMIC_RAM. + #endif + + // Show a progress bar on HD44780 LCDs for SD printing + //#define LCD_PROGRESS_BAR + + #if ENABLED(LCD_PROGRESS_BAR) + // Amount of time (ms) to show the bar + #define PROGRESS_BAR_BAR_TIME 2000 + // Amount of time (ms) to show the status message + #define PROGRESS_BAR_MSG_TIME 3000 + // Amount of time (ms) to retain the status message (0=forever) + #define PROGRESS_MSG_EXPIRE 0 + // Enable this to show messages for MSG_TIME then hide them + //#define PROGRESS_MSG_ONCE + // Add a menu item to test the progress bar: + //#define LCD_PROGRESS_BAR_TEST + #endif + + // Add an 'M73' G-code to set the current percentage + #define LCD_SET_PROGRESS_MANUALLY + + // This allows hosts to request long names for files and folders with M33 + #define LONG_FILENAME_HOST_SUPPORT + + // Enable this option to scroll long filenames in the SD card menu + #define SCROLL_LONG_FILENAMES + + /** + * This option allows you to abort SD printing when any endstop is triggered. + * This feature must be enabled with "M540 S1" or from the LCD menu. + * To have any effect, endstops must be enabled during SD printing. + */ + //#define ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED + + /** + * This option makes it easier to print the same SD Card file again. + * On print completion the LCD Menu will open with the file selected. + * You can just click to start the print, or navigate elsewhere. + */ + //#define SD_REPRINT_LAST_SELECTED_FILE + +#endif // SDSUPPORT + +/** + * Additional options for Graphical Displays + * + * Use the optimizations here to improve printing performance, + * which can be adversely affected by graphical display drawing, + * especially when doing several short moves, and when printing + * on DELTA and SCARA machines. + * + * Some of these options may result in the display lagging behind + * controller events, as there is a trade-off between reliable + * printing performance versus fast display updates. + */ +#if ENABLED(DOGLCD) + // Enable to save many cycles by drawing a hollow frame on the Info Screen + #define XYZ_HOLLOW_FRAME + + // Enable to save many cycles by drawing a hollow frame on Menu Screens + #define MENU_HOLLOW_FRAME + + // A bigger font is available for edit items. Costs 3120 bytes of PROGMEM. + // Western only. Not available for Cyrillic, Kana, Turkish, Greek, or Chinese. + //#define USE_BIG_EDIT_FONT + + // A smaller font may be used on the Info Screen. Costs 2300 bytes of PROGMEM. + // Western only. Not available for Cyrillic, Kana, Turkish, Greek, or Chinese. + //#define USE_SMALL_INFOFONT + + // Enable this option and reduce the value to optimize screen updates. + // The normal delay is 10µs. Use the lowest value that still gives a reliable display. + //#define DOGM_SPI_DELAY_US 5 +#endif // DOGLCD + +// @section safety + +// The hardware watchdog should reset the microcontroller disabling all outputs, +// in case the firmware gets stuck and doesn't do temperature regulation. +#define USE_WATCHDOG + +#if ENABLED(USE_WATCHDOG) + // If you have a watchdog reboot in an ArduinoMega2560 then the device will hang forever, as a watchdog reset will leave the watchdog on. + // The "WATCHDOG_RESET_MANUAL" goes around this by not using the hardware reset. + // However, THIS FEATURE IS UNSAFE!, as it will only work if interrupts are disabled. And the code could hang in an interrupt routine with interrupts disabled. + //#define WATCHDOG_RESET_MANUAL +#endif + +// @section lcd + +/** + * Babystepping enables movement of the axes by tiny increments without changing + * the current position values. This feature is used primarily to adjust the Z + * axis in the first layer of a print in real-time. + * + * Warning: Does not respect endstops! + */ +//#define BABYSTEPPING +#if ENABLED(BABYSTEPPING) + //#define BABYSTEP_XY // Also enable X/Y Babystepping. Not supported on DELTA! + #define BABYSTEP_INVERT_Z false // Change if Z babysteps should go the other way + #define BABYSTEP_MULTIPLICATOR 1 // Babysteps are very small. Increase for faster motion. + //#define BABYSTEP_ZPROBE_OFFSET // Enable to combine M851 and Babystepping + //#define DOUBLECLICK_FOR_Z_BABYSTEPPING // Double-click on the Status Screen for Z Babystepping. + #define DOUBLECLICK_MAX_INTERVAL 1250 // Maximum interval between clicks, in milliseconds. + // Note: Extra time may be added to mitigate controller latency. + //#define BABYSTEP_ZPROBE_GFX_OVERLAY // Enable graphical overlay on Z-offset editor + //#define BABYSTEP_ZPROBE_GFX_REVERSE // Reverses the direction of the CW/CCW indicators +#endif + +// @section extruder + +/** + * Implementation of linear pressure control + * + * Assumption: advance = k * (delta velocity) + * K=0 means advance disabled. + * See Marlin documentation for calibration instructions. + */ +//#define LIN_ADVANCE + +#if ENABLED(LIN_ADVANCE) + #define LIN_ADVANCE_K 75 + + /** + * Some Slicers produce Gcode with randomly jumping extrusion widths occasionally. + * For example within a 0.4mm perimeter it may produce a single segment of 0.05mm width. + * While this is harmless for normal printing (the fluid nature of the filament will + * close this very, very tiny gap), it throws off the LIN_ADVANCE pressure adaption. + * + * For this case LIN_ADVANCE_E_D_RATIO can be used to set the extrusion:distance ratio + * to a fixed value. Note that using a fixed ratio will lead to wrong nozzle pressures + * if the slicer is using variable widths or layer heights within one print! + * + * This option sets the default E:D ratio at startup. Use `M900` to override this value. + * + * Example: `M900 W0.4 H0.2 D1.75`, where: + * - W is the extrusion width in mm + * - H is the layer height in mm + * - D is the filament diameter in mm + * + * Example: `M900 R0.0458` to set the ratio directly. + * + * Set to 0 to auto-detect the ratio based on given Gcode G1 print moves. + * + * Slic3r (including Průša Control) produces Gcode compatible with the automatic mode. + * Cura (as of this writing) may produce Gcode incompatible with the automatic mode. + */ + #define LIN_ADVANCE_E_D_RATIO 0 // The calculated ratio (or 0) according to the formula W * H / ((D / 2) ^ 2 * PI) + // Example: 0.4 * 0.2 / ((1.75 / 2) ^ 2 * PI) = 0.033260135 +#endif + +// @section leveling + +#if ENABLED(DELTA) && !defined(DELTA_PROBEABLE_RADIUS) + #define DELTA_PROBEABLE_RADIUS DELTA_PRINTABLE_RADIUS +#elif IS_SCARA && !defined(SCARA_PRINTABLE_RADIUS) + #define SCARA_PRINTABLE_RADIUS (SCARA_LINKAGE_1 + SCARA_LINKAGE_2) +#endif + +#if ENABLED(MESH_BED_LEVELING) || ENABLED(AUTO_BED_LEVELING_UBL) + // Override the mesh area if the automatic (max) area is too large + //#define MESH_MIN_X MESH_INSET + //#define MESH_MIN_Y MESH_INSET + //#define MESH_MAX_X X_BED_SIZE - (MESH_INSET) + //#define MESH_MAX_Y Y_BED_SIZE - (MESH_INSET) +#endif + +// @section extras + +// +// G2/G3 Arc Support +// +#define ARC_SUPPORT // Disable this feature to save ~3226 bytes +#if ENABLED(ARC_SUPPORT) + #define MM_PER_ARC_SEGMENT 1 // Length of each arc segment + #define N_ARC_CORRECTION 25 // Number of intertpolated segments between corrections + //#define ARC_P_CIRCLES // Enable the 'P' parameter to specify complete circles + //#define CNC_WORKSPACE_PLANES // Allow G2/G3 to operate in XY, ZX, or YZ planes +#endif + +// Support for G5 with XYZE destination and IJPQ offsets. Requires ~2666 bytes. +//#define BEZIER_CURVE_SUPPORT + +// G38.2 and G38.3 Probe Target +// Enable PROBE_DOUBLE_TOUCH if you want G38 to double touch +//#define G38_PROBE_TARGET +#if ENABLED(G38_PROBE_TARGET) + #define G38_MINIMUM_MOVE 0.0275 // minimum distance in mm that will produce a move (determined using the print statement in check_move) +#endif + +// Moves (or segments) with fewer steps than this will be joined with the next move +#define MIN_STEPS_PER_SEGMENT 6 + +// The minimum pulse width (in µs) for stepping a stepper. +// Set this if you find stepping unreliable, or if using a very fast CPU. +#define MINIMUM_STEPPER_PULSE 0 // (µs) The smallest stepper pulse allowed + +// @section temperature + +// Control heater 0 and heater 1 in parallel. +//#define HEATERS_PARALLEL + +//=========================================================================== +//================================= Buffers ================================= +//=========================================================================== + +// @section hidden + +// The number of linear motions that can be in the plan at any give time. +// THE BLOCK_BUFFER_SIZE NEEDS TO BE A POWER OF 2, i.g. 8,16,32 because shifts and ors are used to do the ring-buffering. +#if ENABLED(SDSUPPORT) + #define BLOCK_BUFFER_SIZE 16 // SD,LCD,Buttons take more memory, block buffer needs to be smaller +#else + #define BLOCK_BUFFER_SIZE 16 // maximize block buffer +#endif + +// @section serial + +// The ASCII buffer for serial input +#define MAX_CMD_SIZE 96 +#define BUFSIZE 4 + +// Transmission to Host Buffer Size +// To save 386 bytes of PROGMEM (and TX_BUFFER_SIZE+3 bytes of RAM) set to 0. +// To buffer a simple "ok" you need 4 bytes. +// For ADVANCED_OK (M105) you need 32 bytes. +// For debug-echo: 128 bytes for the optimal speed. +// Other output doesn't need to be that speedy. +// :[0, 2, 4, 8, 16, 32, 64, 128, 256] +#define TX_BUFFER_SIZE 0 + +// Host Receive Buffer Size +// Without XON/XOFF flow control (see SERIAL_XON_XOFF below) 32 bytes should be enough. +// To use flow control, set this buffer size to at least 1024 bytes. +// :[0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048] +//#define RX_BUFFER_SIZE 1024 + +#if RX_BUFFER_SIZE >= 1024 + // Enable to have the controller send XON/XOFF control characters to + // the host to signal the RX buffer is becoming full. + //#define SERIAL_XON_XOFF +#endif + +#if ENABLED(SDSUPPORT) + // Enable this option to collect and display the maximum + // RX queue usage after transferring a file to SD. + //#define SERIAL_STATS_MAX_RX_QUEUED + + // Enable this option to collect and display the number + // of dropped bytes after a file transfer to SD. + //#define SERIAL_STATS_DROPPED_RX +#endif + +// Enable an emergency-command parser to intercept certain commands as they +// enter the serial receive buffer, so they cannot be blocked. +// Currently handles M108, M112, M410 +// Does not work on boards using AT90USB (USBCON) processors! +#define EMERGENCY_PARSER + +// Bad Serial-connections can miss a received command by sending an 'ok' +// Therefore some clients abort after 30 seconds in a timeout. +// Some other clients start sending commands while receiving a 'wait'. +// This "wait" is only sent when the buffer is empty. 1 second is a good value here. +//#define NO_TIMEOUTS 1000 // Milliseconds + +// Some clients will have this feature soon. This could make the NO_TIMEOUTS unnecessary. +#define ADVANCED_OK + +// @section extras + +/** + * Firmware-based and LCD-controlled retract + * + * Add G10 / G11 commands for automatic firmware-based retract / recover. + * Use M207 and M208 to define parameters for retract / recover. + * + * Use M209 to enable or disable auto-retract. + * With auto-retract enabled, all G1 E moves within the set range + * will be converted to firmware-based retract/recover moves. + * + * Be sure to turn off auto-retract during filament change. + * + * Note that M207 / M208 / M209 settings are saved to EEPROM. + * + */ +//#define FWRETRACT // ONLY PARTIALLY TESTED +#if ENABLED(FWRETRACT) + #define MIN_AUTORETRACT 0.1 // When auto-retract is on, convert E moves of this length and over + #define MAX_AUTORETRACT 10.0 // Upper limit for auto-retract conversion + #define RETRACT_LENGTH 3 // Default retract length (positive mm) + #define RETRACT_LENGTH_SWAP 13 // Default swap retract length (positive mm), for extruder change + #define RETRACT_FEEDRATE 45 // Default feedrate for retracting (mm/s) + #define RETRACT_ZLIFT 0 // Default retract Z-lift + #define RETRACT_RECOVER_LENGTH 0 // Default additional recover length (mm, added to retract length when recovering) + #define RETRACT_RECOVER_LENGTH_SWAP 0 // Default additional swap recover length (mm, added to retract length when recovering from extruder change) + #define RETRACT_RECOVER_FEEDRATE 8 // Default feedrate for recovering from retraction (mm/s) + #define RETRACT_RECOVER_FEEDRATE_SWAP 8 // Default feedrate for recovering from swap retraction (mm/s) +#endif + +/** + * Extra Fan Speed + * Adds a secondary fan speed for each print-cooling fan. + * 'M106 P T3-255' : Set a secondary speed for + * 'M106 P T2' : Use the set secondary speed + * 'M106 P T1' : Restore the previous fan speed + */ +//#define EXTRA_FAN_SPEED + +/** + * Advanced Pause + * Experimental feature for filament change support and for parking the nozzle when paused. + * Adds the GCode M600 for initiating filament change. + * If PARK_HEAD_ON_PAUSE enabled, adds the GCode M125 to pause printing and park the nozzle. + * + * Requires an LCD display. + * This feature is required for the default FILAMENT_RUNOUT_SCRIPT. + */ +//#define ADVANCED_PAUSE_FEATURE +#if ENABLED(ADVANCED_PAUSE_FEATURE) + #define PAUSE_PARK_X_POS 3 // X position of hotend + #define PAUSE_PARK_Y_POS 3 // Y position of hotend + #define PAUSE_PARK_Z_ADD 10 // Z addition of hotend (lift) + #define PAUSE_PARK_XY_FEEDRATE 100 // X and Y axes feedrate in mm/s (also used for delta printers Z axis) + #define PAUSE_PARK_Z_FEEDRATE 5 // Z axis feedrate in mm/s (not used for delta printers) + #define PAUSE_PARK_RETRACT_FEEDRATE 60 // Initial retract feedrate in mm/s + #define PAUSE_PARK_RETRACT_LENGTH 2 // Initial retract in mm + // It is a short retract used immediately after print interrupt before move to filament exchange position + #define FILAMENT_CHANGE_UNLOAD_FEEDRATE 10 // Unload filament feedrate in mm/s - filament unloading can be fast + #define FILAMENT_CHANGE_UNLOAD_LENGTH 100 // Unload filament length from hotend in mm + // Longer length for bowden printers to unload filament from whole bowden tube, + // shorter length for printers without bowden to unload filament from extruder only, + // 0 to disable unloading for manual unloading + #define FILAMENT_CHANGE_LOAD_FEEDRATE 6 // Load filament feedrate in mm/s - filament loading into the bowden tube can be fast + #define FILAMENT_CHANGE_LOAD_LENGTH 0 // Load filament length over hotend in mm + // Longer length for bowden printers to fast load filament into whole bowden tube over the hotend, + // Short or zero length for printers without bowden where loading is not used + #define ADVANCED_PAUSE_EXTRUDE_FEEDRATE 3 // Extrude filament feedrate in mm/s - must be slower than load feedrate + #define ADVANCED_PAUSE_EXTRUDE_LENGTH 50 // Extrude filament length in mm after filament is loaded over the hotend, + // 0 to disable for manual extrusion + // Filament can be extruded repeatedly from the filament exchange menu to fill the hotend, + // or until outcoming filament color is not clear for filament color change + #define PAUSE_PARK_NOZZLE_TIMEOUT 45 // Turn off nozzle if user doesn't change filament within this time limit in seconds + #define FILAMENT_CHANGE_NUMBER_OF_ALERT_BEEPS 5 // Number of alert beeps before printer goes quiet + #define PAUSE_PARK_NO_STEPPER_TIMEOUT // Enable to have stepper motors hold position during filament change + // even if it takes longer than DEFAULT_STEPPER_DEACTIVE_TIME. + //#define PARK_HEAD_ON_PAUSE // Go to filament change position on pause, return to print position on resume + //#define HOME_BEFORE_FILAMENT_CHANGE // Ensure homing has been completed prior to parking for filament change +#endif + +// @section tmc + +/** + * Enable this section if you have TMC26X motor drivers. + * You will need to import the TMC26XStepper library into the Arduino IDE for this + * (https://github.com/trinamic/TMC26XStepper.git) + */ +//#define HAVE_TMCDRIVER + +#if ENABLED(HAVE_TMCDRIVER) + + //#define X_IS_TMC + //#define X2_IS_TMC + //#define Y_IS_TMC + //#define Y2_IS_TMC + //#define Z_IS_TMC + //#define Z2_IS_TMC + //#define E0_IS_TMC + //#define E1_IS_TMC + //#define E2_IS_TMC + //#define E3_IS_TMC + //#define E4_IS_TMC + + #define X_MAX_CURRENT 1000 // in mA + #define X_SENSE_RESISTOR 91 // in mOhms + #define X_MICROSTEPS 16 // number of microsteps + + #define X2_MAX_CURRENT 1000 + #define X2_SENSE_RESISTOR 91 + #define X2_MICROSTEPS 16 + + #define Y_MAX_CURRENT 1000 + #define Y_SENSE_RESISTOR 91 + #define Y_MICROSTEPS 16 + + #define Y2_MAX_CURRENT 1000 + #define Y2_SENSE_RESISTOR 91 + #define Y2_MICROSTEPS 16 + + #define Z_MAX_CURRENT 1000 + #define Z_SENSE_RESISTOR 91 + #define Z_MICROSTEPS 16 + + #define Z2_MAX_CURRENT 1000 + #define Z2_SENSE_RESISTOR 91 + #define Z2_MICROSTEPS 16 + + #define E0_MAX_CURRENT 1000 + #define E0_SENSE_RESISTOR 91 + #define E0_MICROSTEPS 16 + + #define E1_MAX_CURRENT 1000 + #define E1_SENSE_RESISTOR 91 + #define E1_MICROSTEPS 16 + + #define E2_MAX_CURRENT 1000 + #define E2_SENSE_RESISTOR 91 + #define E2_MICROSTEPS 16 + + #define E3_MAX_CURRENT 1000 + #define E3_SENSE_RESISTOR 91 + #define E3_MICROSTEPS 16 + + #define E4_MAX_CURRENT 1000 + #define E4_SENSE_RESISTOR 91 + #define E4_MICROSTEPS 16 + +#endif + +// @section TMC2130 + +/** + * Enable this for SilentStepStick Trinamic TMC2130 SPI-configurable stepper drivers. + * + * You'll also need the TMC2130Stepper Arduino library + * (https://github.com/teemuatlut/TMC2130Stepper). + * + * To use TMC2130 stepper drivers in SPI mode connect your SPI2130 pins to + * the hardware SPI interface on your board and define the required CS pins + * in your `pins_MYBOARD.h` file. (e.g., RAMPS 1.4 uses AUX3 pins `X_CS_PIN 53`, `Y_CS_PIN 49`, etc.). + */ +//#define HAVE_TMC2130 + +#if ENABLED(HAVE_TMC2130) + + // CHOOSE YOUR MOTORS HERE, THIS IS MANDATORY + //#define X_IS_TMC2130 + //#define X2_IS_TMC2130 + //#define Y_IS_TMC2130 + //#define Y2_IS_TMC2130 + //#define Z_IS_TMC2130 + //#define Z2_IS_TMC2130 + //#define E0_IS_TMC2130 + //#define E1_IS_TMC2130 + //#define E2_IS_TMC2130 + //#define E3_IS_TMC2130 + //#define E4_IS_TMC2130 + + /** + * Stepper driver settings + */ + + #define R_SENSE 0.11 // R_sense resistor for SilentStepStick2130 + #define HOLD_MULTIPLIER 0.5 // Scales down the holding current from run current + #define INTERPOLATE 1 // Interpolate X/Y/Z_MICROSTEPS to 256 + + #define X_CURRENT 1000 // rms current in mA. Multiply by 1.41 for peak current. + #define X_MICROSTEPS 16 // 0..256 + + #define Y_CURRENT 1000 + #define Y_MICROSTEPS 16 + + #define Z_CURRENT 1000 + #define Z_MICROSTEPS 16 + + //#define X2_CURRENT 1000 + //#define X2_MICROSTEPS 16 + + //#define Y2_CURRENT 1000 + //#define Y2_MICROSTEPS 16 + + //#define Z2_CURRENT 1000 + //#define Z2_MICROSTEPS 16 + + //#define E0_CURRENT 1000 + //#define E0_MICROSTEPS 16 + + //#define E1_CURRENT 1000 + //#define E1_MICROSTEPS 16 + + //#define E2_CURRENT 1000 + //#define E2_MICROSTEPS 16 + + //#define E3_CURRENT 1000 + //#define E3_MICROSTEPS 16 + + //#define E4_CURRENT 1000 + //#define E4_MICROSTEPS 16 + + /** + * Use Trinamic's ultra quiet stepping mode. + * When disabled, Marlin will use spreadCycle stepping mode. + */ + #define STEALTHCHOP + + /** + * Let Marlin automatically control stepper current. + * This is still an experimental feature. + * Increase current every 5s by CURRENT_STEP until stepper temperature prewarn gets triggered, + * then decrease current by CURRENT_STEP until temperature prewarn is cleared. + * Adjusting starts from X/Y/Z/E_CURRENT but will not increase over AUTO_ADJUST_MAX + * Relevant g-codes: + * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. + * M906 S1 - Start adjusting current + * M906 S0 - Stop adjusting current + * M911 - Report stepper driver overtemperature pre-warn condition. + * M912 - Clear stepper driver overtemperature pre-warn condition flag. + */ + //#define AUTOMATIC_CURRENT_CONTROL + + #if ENABLED(AUTOMATIC_CURRENT_CONTROL) + #define CURRENT_STEP 50 // [mA] + #define AUTO_ADJUST_MAX 1300 // [mA], 1300mA_rms = 1840mA_peak + #define REPORT_CURRENT_CHANGE + #endif + + /** + * The driver will switch to spreadCycle when stepper speed is over HYBRID_THRESHOLD. + * This mode allows for faster movements at the expense of higher noise levels. + * STEALTHCHOP needs to be enabled. + * M913 X/Y/Z/E to live tune the setting + */ + //#define HYBRID_THRESHOLD + + #define X_HYBRID_THRESHOLD 100 // [mm/s] + #define X2_HYBRID_THRESHOLD 100 + #define Y_HYBRID_THRESHOLD 100 + #define Y2_HYBRID_THRESHOLD 100 + #define Z_HYBRID_THRESHOLD 4 + #define Z2_HYBRID_THRESHOLD 4 + #define E0_HYBRID_THRESHOLD 30 + #define E1_HYBRID_THRESHOLD 30 + #define E2_HYBRID_THRESHOLD 30 + #define E3_HYBRID_THRESHOLD 30 + #define E4_HYBRID_THRESHOLD 30 + + /** + * Use stallGuard2 to sense an obstacle and trigger an endstop. + * You need to place a wire from the driver's DIAG1 pin to the X/Y endstop pin. + * If used along with STEALTHCHOP, the movement will be louder when homing. This is normal. + * + * X/Y_HOMING_SENSITIVITY is used for tuning the trigger sensitivity. + * Higher values make the system LESS sensitive. + * Lower value make the system MORE sensitive. + * Too low values can lead to false positives, while too high values will collide the axis without triggering. + * It is advised to set X/Y_HOME_BUMP_MM to 0. + * M914 X/Y to live tune the setting + */ + //#define SENSORLESS_HOMING + + #if ENABLED(SENSORLESS_HOMING) + #define X_HOMING_SENSITIVITY 19 + #define Y_HOMING_SENSITIVITY 19 + #endif + + /** + * You can set your own advanced settings by filling in predefined functions. + * A list of available functions can be found on the library github page + * https://github.com/teemuatlut/TMC2130Stepper + * + * Example: + * #define TMC2130_ADV() { \ + * stepperX.diag0_temp_prewarn(1); \ + * stepperX.interpolate(0); \ + * } + */ + #define TMC2130_ADV() { } + +#endif // HAVE_TMC2130 + +// @section L6470 + +/** + * Enable this section if you have L6470 motor drivers. + * You need to import the L6470 library into the Arduino IDE for this. + * (https://github.com/ameyer/Arduino-L6470) + */ + +//#define HAVE_L6470DRIVER +#if ENABLED(HAVE_L6470DRIVER) + + //#define X_IS_L6470 + //#define X2_IS_L6470 + //#define Y_IS_L6470 + //#define Y2_IS_L6470 + //#define Z_IS_L6470 + //#define Z2_IS_L6470 + //#define E0_IS_L6470 + //#define E1_IS_L6470 + //#define E2_IS_L6470 + //#define E3_IS_L6470 + //#define E4_IS_L6470 + + #define X_MICROSTEPS 16 // number of microsteps + #define X_K_VAL 50 // 0 - 255, Higher values, are higher power. Be careful not to go too high + #define X_OVERCURRENT 2000 // maxc current in mA. If the current goes over this value, the driver will switch off + #define X_STALLCURRENT 1500 // current in mA where the driver will detect a stall + + #define X2_MICROSTEPS 16 + #define X2_K_VAL 50 + #define X2_OVERCURRENT 2000 + #define X2_STALLCURRENT 1500 + + #define Y_MICROSTEPS 16 + #define Y_K_VAL 50 + #define Y_OVERCURRENT 2000 + #define Y_STALLCURRENT 1500 + + #define Y2_MICROSTEPS 16 + #define Y2_K_VAL 50 + #define Y2_OVERCURRENT 2000 + #define Y2_STALLCURRENT 1500 + + #define Z_MICROSTEPS 16 + #define Z_K_VAL 50 + #define Z_OVERCURRENT 2000 + #define Z_STALLCURRENT 1500 + + #define Z2_MICROSTEPS 16 + #define Z2_K_VAL 50 + #define Z2_OVERCURRENT 2000 + #define Z2_STALLCURRENT 1500 + + #define E0_MICROSTEPS 16 + #define E0_K_VAL 50 + #define E0_OVERCURRENT 2000 + #define E0_STALLCURRENT 1500 + + #define E1_MICROSTEPS 16 + #define E1_K_VAL 50 + #define E1_OVERCURRENT 2000 + #define E1_STALLCURRENT 1500 + + #define E2_MICROSTEPS 16 + #define E2_K_VAL 50 + #define E2_OVERCURRENT 2000 + #define E2_STALLCURRENT 1500 + + #define E3_MICROSTEPS 16 + #define E3_K_VAL 50 + #define E3_OVERCURRENT 2000 + #define E3_STALLCURRENT 1500 + + #define E4_MICROSTEPS 16 + #define E4_K_VAL 50 + #define E4_OVERCURRENT 2000 + #define E4_STALLCURRENT 1500 + +#endif + +/** + * TWI/I2C BUS + * + * This feature is an EXPERIMENTAL feature so it shall not be used on production + * machines. Enabling this will allow you to send and receive I2C data from slave + * devices on the bus. + * + * ; Example #1 + * ; This macro send the string "Marlin" to the slave device with address 0x63 (99) + * ; It uses multiple M260 commands with one B arg + * M260 A99 ; Target slave address + * M260 B77 ; M + * M260 B97 ; a + * M260 B114 ; r + * M260 B108 ; l + * M260 B105 ; i + * M260 B110 ; n + * M260 S1 ; Send the current buffer + * + * ; Example #2 + * ; Request 6 bytes from slave device with address 0x63 (99) + * M261 A99 B5 + * + * ; Example #3 + * ; Example serial output of a M261 request + * echo:i2c-reply: from:99 bytes:5 data:hello + */ + +// @section i2cbus + +//#define EXPERIMENTAL_I2CBUS +#define I2C_SLAVE_ADDRESS 0 // Set a value from 8 to 127 to act as a slave + +// @section extras + +/** + * Spindle & Laser control + * + * Add the M3, M4, and M5 commands to turn the spindle/laser on and off, and + * to set spindle speed, spindle direction, and laser power. + * + * SuperPid is a router/spindle speed controller used in the CNC milling community. + * Marlin can be used to turn the spindle on and off. It can also be used to set + * the spindle speed from 5,000 to 30,000 RPM. + * + * You'll need to select a pin for the ON/OFF function and optionally choose a 0-5V + * hardware PWM pin for the speed control and a pin for the rotation direction. + * + * See http://marlinfw.org/docs/configuration/laser_spindle.html for more config details. + */ +//#define SPINDLE_LASER_ENABLE +#if ENABLED(SPINDLE_LASER_ENABLE) + + #define SPINDLE_LASER_ENABLE_INVERT false // set to "true" if the on/off function is reversed + #define SPINDLE_LASER_PWM true // set to true if your controller supports setting the speed/power + #define SPINDLE_LASER_PWM_INVERT true // set to "true" if the speed/power goes up when you want it to go slower + #define SPINDLE_LASER_POWERUP_DELAY 5000 // delay in milliseconds to allow the spindle/laser to come up to speed/power + #define SPINDLE_LASER_POWERDOWN_DELAY 5000 // delay in milliseconds to allow the spindle to stop + #define SPINDLE_DIR_CHANGE true // set to true if your spindle controller supports changing spindle direction + #define SPINDLE_INVERT_DIR false + #define SPINDLE_STOP_ON_DIR_CHANGE true // set to true if Marlin should stop the spindle before changing rotation direction + + /** + * The M3 & M4 commands use the following equation to convert PWM duty cycle to speed/power + * + * SPEED/POWER = PWM duty cycle * SPEED_POWER_SLOPE + SPEED_POWER_INTERCEPT + * where PWM duty cycle varies from 0 to 255 + * + * set the following for your controller (ALL MUST BE SET) + */ + + #define SPEED_POWER_SLOPE 118.4 + #define SPEED_POWER_INTERCEPT 0 + #define SPEED_POWER_MIN 5000 + #define SPEED_POWER_MAX 30000 // SuperPID router controller 0 - 30,000 RPM + + //#define SPEED_POWER_SLOPE 0.3922 + //#define SPEED_POWER_INTERCEPT 0 + //#define SPEED_POWER_MIN 10 + //#define SPEED_POWER_MAX 100 // 0-100% +#endif + +/** + * CNC Coordinate Systems + * + * Enables G53 and G54-G59.3 commands to select coordinate systems + * and G92.1 to reset the workspace to native machine space. + */ +//#define CNC_COORDINATE_SYSTEMS + +/** + * M43 - display pin status, watch pins for changes, watch endstops & toggle LED, Z servo probe test, toggle pins + */ +//#define PINS_DEBUGGING + +/** + * Auto-report temperatures with M155 S + */ +#define AUTO_REPORT_TEMPERATURES + +/** + * Include capabilities in M115 output + */ +#define EXTENDED_CAPABILITIES_REPORT + +/** + * Volumetric extrusion default state + * Activate to make volumetric extrusion the default method, + * with DEFAULT_NOMINAL_FILAMENT_DIA as the default diameter. + * + * M200 D0 to disable, M200 Dn to set a new diameter. + */ +//#define VOLUMETRIC_DEFAULT_ON + +/** + * Enable this option for a leaner build of Marlin that removes all + * workspace offsets, simplifying coordinate transformations, leveling, etc. + * + * - M206 and M428 are disabled. + * - G92 will revert to its behavior from Marlin 1.0. + */ +//#define NO_WORKSPACE_OFFSETS + +/** + * Set the number of proportional font spaces required to fill up a typical character space. + * This can help to better align the output of commands like `G29 O` Mesh Output. + * + * For clients that use a fixed-width font (like OctoPrint), leave this set to 1.0. + * Otherwise, adjust according to your client and font. + */ +#define PROPORTIONAL_FONT_RATIO 1.0 + +/** + * Spend 28 bytes of SRAM to optimize the GCode parser + */ +#define FASTER_GCODE_PARSER + +/** + * User-defined menu items that execute custom GCode + */ +//#define CUSTOM_USER_MENUS +#if ENABLED(CUSTOM_USER_MENUS) + #define USER_SCRIPT_DONE "M117 User Script Done" + #define USER_SCRIPT_AUDIBLE_FEEDBACK + //#define USER_SCRIPT_RETURN // Return to status screen after a script + + #define USER_DESC_1 "Home & UBL Info" + #define USER_GCODE_1 "G28\nG29 W" + + #define USER_DESC_2 "Preheat for PLA" + #define USER_GCODE_2 "M140 S" STRINGIFY(PREHEAT_1_TEMP_BED) "\nM104 S" STRINGIFY(PREHEAT_1_TEMP_HOTEND) + + #define USER_DESC_3 "Preheat for ABS" + #define USER_GCODE_3 "M140 S" STRINGIFY(PREHEAT_2_TEMP_BED) "\nM104 S" STRINGIFY(PREHEAT_2_TEMP_HOTEND) + + #define USER_DESC_4 "Heat Bed/Home/Level" + #define USER_GCODE_4 "M140 S" STRINGIFY(PREHEAT_2_TEMP_BED) "\nG28\nG29" + + #define USER_DESC_5 "Home & Info" + #define USER_GCODE_5 "G28\nM503" +#endif + +/** + * Specify an action command to send to the host when the printer is killed. + * Will be sent in the form '//action:ACTION_ON_KILL', e.g. '//action:poweroff'. + * The host must be configured to handle the action command. + */ +#define ACTION_ON_KILL "poweroff" + +//=========================================================================== +//====================== I2C Position Encoder Settings ====================== +//=========================================================================== + +/** + * I2C position encoders for closed loop control. + * Developed by Chris Barr at Aus3D. + * + * Wiki: http://wiki.aus3d.com.au/Magnetic_Encoder + * Github: https://github.com/Aus3D/MagneticEncoder + * + * Supplier: http://aus3d.com.au/magnetic-encoder-module + * Alternative Supplier: http://reliabuild3d.com/ + * + * Reilabuild encoders have been modified to improve reliability. + */ + +//#define I2C_POSITION_ENCODERS +#if ENABLED(I2C_POSITION_ENCODERS) + + #define I2CPE_ENCODER_CNT 1 // The number of encoders installed; max of 5 + // encoders supported currently. + + #define I2CPE_ENC_1_ADDR I2CPE_PRESET_ADDR_X // I2C address of the encoder. 30-200. + #define I2CPE_ENC_1_AXIS X_AXIS // Axis the encoder module is installed on. _AXIS. + #define I2CPE_ENC_1_TYPE I2CPE_ENC_TYPE_LINEAR // Type of encoder: I2CPE_ENC_TYPE_LINEAR -or- + // I2CPE_ENC_TYPE_ROTARY. + #define I2CPE_ENC_1_TICKS_UNIT 2048 // 1024 for magnetic strips with 2mm poles; 2048 for + // 1mm poles. For linear encoders this is ticks / mm, + // for rotary encoders this is ticks / revolution. + //#define I2CPE_ENC_1_TICKS_REV (16 * 200) // Only needed for rotary encoders; number of stepper + // steps per full revolution (motor steps/rev * microstepping) + //#define I2CPE_ENC_1_INVERT // Invert the direction of axis travel. + #define I2CPE_ENC_1_EC_METHOD I2CPE_ECM_NONE // Type of error error correction. + #define I2CPE_ENC_1_EC_THRESH 0.10 // Threshold size for error (in mm) above which the + // printer will attempt to correct the error; errors + // smaller than this are ignored to minimize effects of + // measurement noise / latency (filter). + + #define I2CPE_ENC_2_ADDR I2CPE_PRESET_ADDR_Y // Same as above, but for encoder 2. + #define I2CPE_ENC_2_AXIS Y_AXIS + #define I2CPE_ENC_2_TYPE I2CPE_ENC_TYPE_LINEAR + #define I2CPE_ENC_2_TICKS_UNIT 2048 + //#define I2CPE_ENC_2_TICKS_REV (16 * 200) + //#define I2CPE_ENC_2_INVERT + #define I2CPE_ENC_2_EC_METHOD I2CPE_ECM_NONE + #define I2CPE_ENC_2_EC_THRESH 0.10 + + #define I2CPE_ENC_3_ADDR I2CPE_PRESET_ADDR_Z // Encoder 3. Add additional configuration options + #define I2CPE_ENC_3_AXIS Z_AXIS // as above, or use defaults below. + + #define I2CPE_ENC_4_ADDR I2CPE_PRESET_ADDR_E // Encoder 4. + #define I2CPE_ENC_4_AXIS E_AXIS + + #define I2CPE_ENC_5_ADDR 34 // Encoder 5. + #define I2CPE_ENC_5_AXIS E_AXIS + + // Default settings for encoders which are enabled, but without settings configured above. + #define I2CPE_DEF_TYPE I2CPE_ENC_TYPE_LINEAR + #define I2CPE_DEF_ENC_TICKS_UNIT 2048 + #define I2CPE_DEF_TICKS_REV (16 * 200) + #define I2CPE_DEF_EC_METHOD I2CPE_ECM_NONE + #define I2CPE_DEF_EC_THRESH 0.1 + + //#define I2CPE_ERR_THRESH_ABORT 100.0 // Threshold size for error (in mm) error on any given + // axis after which the printer will abort. Comment out to + // disable abort behaviour. + + #define I2CPE_TIME_TRUSTED 10000 // After an encoder fault, there must be no further fault + // for this amount of time (in ms) before the encoder + // is trusted again. + + /** + * Position is checked every time a new command is executed from the buffer but during long moves, + * this setting determines the minimum update time between checks. A value of 100 works well with + * error rolling average when attempting to correct only for skips and not for vibration. + */ + #define I2CPE_MIN_UPD_TIME_MS 100 // Minimum time in miliseconds between encoder checks. + + // Use a rolling average to identify persistant errors that indicate skips, as opposed to vibration and noise. + #define I2CPE_ERR_ROLLING_AVERAGE + +#endif // I2C_POSITION_ENCODERS + +/** + * MAX7219 Debug Matrix + * + * Add support for a low-cost 8x8 LED Matrix based on the Max7219 chip, which can be used as a status + * display. Requires 3 signal wires. Some useful debug options are included to demonstrate its usage. + * + * Fully assembled MAX7219 boards can be found on the internet for under $2(US). + * For example, see https://www.ebay.com/sch/i.html?_nkw=332349290049 + */ +//#define MAX7219_DEBUG +#if ENABLED(MAX7219_DEBUG) + #define MAX7219_CLK_PIN 64 // 77 on Re-ARM // Configuration of the 3 pins to control the display + #define MAX7219_DIN_PIN 57 // 78 on Re-ARM + #define MAX7219_LOAD_PIN 44 // 79 on Re-ARM + + /** + * Sample debug features + * If you add more debug displays, be careful to avoid conflicts! + */ + #define MAX7219_DEBUG_PRINTER_ALIVE // Blink corner LED of 8x8 matrix to show that the firmware is functioning + #define MAX7219_DEBUG_STEPPER_HEAD 3 // Show the stepper queue head position on this and the next LED matrix row + #define MAX7219_DEBUG_STEPPER_TAIL 5 // Show the stepper queue tail position on this and the next LED matrix row + + #define MAX7219_DEBUG_STEPPER_QUEUE 0 // Show the current stepper queue depth on this and the next LED matrix row + // If you experience stuttering, reboots, etc. this option can reveal how + // tweaks made to the configuration are affecting the printer in real-time. +#endif + +#endif // CONFIGURATION_ADV_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/G26_Mesh_Validation_Tool.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/G26_Mesh_Validation_Tool.cpp new file mode 100644 index 0000000..f5a3707 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/G26_Mesh_Validation_Tool.cpp @@ -0,0 +1,880 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Marlin Firmware -- G26 - Mesh Validation Tool + */ + +#include "MarlinConfig.h" + +#if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_VALIDATION) + + #include "ubl.h" + #include "Marlin.h" + #include "planner.h" + #include "stepper.h" + #include "temperature.h" + #include "ultralcd.h" + #include "gcode.h" + + #define EXTRUSION_MULTIPLIER 1.0 + #define RETRACTION_MULTIPLIER 1.0 + #define PRIME_LENGTH 10.0 + #define OOZE_AMOUNT 0.3 + + #define SIZE_OF_INTERSECTION_CIRCLES 5 + #define SIZE_OF_CROSSHAIRS 3 + + #if SIZE_OF_CROSSHAIRS >= SIZE_OF_INTERSECTION_CIRCLES + #error "SIZE_OF_CROSSHAIRS must be less than SIZE_OF_INTERSECTION_CIRCLES." + #endif + + /** + * G26 Mesh Validation Tool + * + * G26 is a Mesh Validation Tool intended to provide support for the Marlin Unified Bed Leveling System. + * In order to fully utilize and benefit from the Marlin Unified Bed Leveling System an accurate Mesh must + * be defined. G29 is designed to allow the user to quickly validate the correctness of her Mesh. It will + * first heat the bed and nozzle. It will then print lines and circles along the Mesh Cell boundaries and + * the intersections of those lines (respectively). + * + * This action allows the user to immediately see where the Mesh is properly defined and where it needs to + * be edited. The command will generate the Mesh lines closest to the nozzle's starting position. Alternatively + * the user can specify the X and Y position of interest with command parameters. This allows the user to + * focus on a particular area of the Mesh where attention is needed. + * + * B # Bed Set the Bed Temperature. If not specified, a default of 60 C. will be assumed. + * + * C Current When searching for Mesh Intersection points to draw, use the current nozzle location + * as the base for any distance comparison. + * + * D Disable Disable the Unified Bed Leveling System. In the normal case the user is invoking this + * command to see how well a Mesh as been adjusted to match a print surface. In order to do + * this the Unified Bed Leveling System is turned on by the G26 command. The D parameter + * alters the command's normal behaviour and disables the Unified Bed Leveling System even if + * it is on. + * + * H # Hotend Set the Nozzle Temperature. If not specified, a default of 205 C. will be assumed. + * + * F # Filament Used to specify the diameter of the filament being used. If not specified + * 1.75mm filament is assumed. If you are not getting acceptable results by using the + * 'correct' numbers, you can scale this number up or down a little bit to change the amount + * of filament that is being extruded during the printing of the various lines on the bed. + * + * K Keep-On Keep the heaters turned on at the end of the command. + * + * L # Layer Layer height. (Height of nozzle above bed) If not specified .20mm will be used. + * + * O # Ooooze How much your nozzle will Ooooze filament while getting in position to print. This + * is over kill, but using this parameter will let you get the very first 'circle' perfect + * so you have a trophy to peel off of the bed and hang up to show how perfectly you have your + * Mesh calibrated. If not specified, a filament length of .3mm is assumed. + * + * P # Prime Prime the nozzle with specified length of filament. If this parameter is not + * given, no prime action will take place. If the parameter specifies an amount, that much + * will be purged before continuing. If no amount is specified the command will start + * purging filament until the user provides an LCD Click and then it will continue with + * printing the Mesh. You can carefully remove the spent filament with a needle nose + * pliers while holding the LCD Click wheel in a depressed state. If you do not have + * an LCD, you must specify a value if you use P. + * + * Q # Multiplier Retraction Multiplier. Normally not needed. Retraction defaults to 1.0mm and + * un-retraction is at 1.2mm These numbers will be scaled by the specified amount + * + * R # Repeat Prints the number of patterns given as a parameter, starting at the current location. + * If a parameter isn't given, every point will be printed unless G26 is interrupted. + * This works the same way that the UBL G29 P4 R parameter works. + * + * NOTE: If you do not have an LCD, you -must- specify R. This is to ensure that you are + * aware that there's some risk associated with printing without the ability to abort in + * cases where mesh point Z value may be inaccurate. As above, if you do not include a + * parameter, every point will be printed. + * + * S # Nozzle Used to control the size of nozzle diameter. If not specified, a .4mm nozzle is assumed. + * + * U # Random Randomize the order that the circles are drawn on the bed. The search for the closest + * undrawn cicle is still done. But the distance to the location for each circle has a + * random number of the size specified added to it. Specifying S50 will give an interesting + * deviation from the normal behaviour on a 10 x 10 Mesh. + * + * X # X Coord. Specify the starting location of the drawing activity. + * + * Y # Y Coord. Specify the starting location of the drawing activity. + */ + + // External references + + extern float feedrate_mm_s; // must set before calling prepare_move_to_destination + extern Planner planner; + #if ENABLED(ULTRA_LCD) + extern char lcd_status_message[]; + #endif + extern float destination[XYZE]; + void set_destination_from_current(); + void prepare_move_to_destination(); + inline void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); } + inline void set_current_from_destination() { COPY(current_position, destination); } + #if ENABLED(NEWPANEL) + void lcd_setstatusPGM(const char* const message, const int8_t level); + void chirp_at_user(); + #endif + + // Private functions + + static uint16_t circle_flags[16], horizontal_mesh_line_flags[16], vertical_mesh_line_flags[16]; + float g26_e_axis_feedrate = 0.020, + random_deviation = 0.0; + + static bool g26_retracted = false; // Track the retracted state of the nozzle so mismatched + // retracts/recovers won't result in a bad state. + + float valid_trig_angle(float); + + float unified_bed_leveling::g26_extrusion_multiplier, + unified_bed_leveling::g26_retraction_multiplier, + unified_bed_leveling::g26_nozzle, + unified_bed_leveling::g26_filament_diameter, + unified_bed_leveling::g26_layer_height, + unified_bed_leveling::g26_prime_length, + unified_bed_leveling::g26_x_pos, + unified_bed_leveling::g26_y_pos, + unified_bed_leveling::g26_ooze_amount; + + int16_t unified_bed_leveling::g26_bed_temp, + unified_bed_leveling::g26_hotend_temp; + + int8_t unified_bed_leveling::g26_prime_flag; + + bool unified_bed_leveling::g26_continue_with_closest, + unified_bed_leveling::g26_keep_heaters_on; + + int16_t unified_bed_leveling::g26_repeats; + + void unified_bed_leveling::G26_line_to_destination(const float &feed_rate) { + const float save_feedrate = feedrate_mm_s; + feedrate_mm_s = feed_rate; // use specified feed rate + prepare_move_to_destination(); // will ultimately call ubl.line_to_destination_cartesian or ubl.prepare_linear_move_to for UBL_DELTA + feedrate_mm_s = save_feedrate; // restore global feed rate + } + + #if ENABLED(NEWPANEL) + /** + * Detect ubl_lcd_clicked, debounce it, and return true for cancel + */ + bool user_canceled() { + if (!ubl_lcd_clicked()) return false; + safe_delay(10); // Wait for click to settle + + #if ENABLED(ULTRA_LCD) + lcd_setstatusPGM(PSTR("Mesh Validation Stopped."), 99); + lcd_quick_feedback(); + #endif + + while (!ubl_lcd_clicked()) idle(); // Wait for button release + + // If the button is suddenly pressed again, + // ask the user to resolve the issue + lcd_setstatusPGM(PSTR("Release button"), 99); // will never appear... + while (ubl_lcd_clicked()) idle(); // unless this loop happens + lcd_reset_status(); + + return true; + } + #endif + + /** + * G26: Mesh Validation Pattern generation. + * + * Used to interactively edit UBL's Mesh by placing the + * nozzle in a problem area and doing a G29 P4 R command. + */ + void unified_bed_leveling::G26() { + SERIAL_ECHOLNPGM("G26 command started. Waiting for heater(s)."); + float tmp, start_angle, end_angle; + int i, xi, yi; + mesh_index_pair location; + + // Don't allow Mesh Validation without homing first, + // or if the parameter parsing did not go OK, abort + if (axis_unhomed_error() || parse_G26_parameters()) return; + + if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) { + do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES); + stepper.synchronize(); + set_current_from_destination(); + } + + if (turn_on_heaters()) goto LEAVE; + + current_position[E_AXIS] = 0.0; + sync_plan_position_e(); + + if (g26_prime_flag && prime_nozzle()) goto LEAVE; + + /** + * Bed is preheated + * + * Nozzle is at temperature + * + * Filament is primed! + * + * It's "Show Time" !!! + */ + + ZERO(circle_flags); + ZERO(horizontal_mesh_line_flags); + ZERO(vertical_mesh_line_flags); + + // Move nozzle to the specified height for the first layer + set_destination_from_current(); + destination[Z_AXIS] = g26_layer_height; + move_to(destination, 0.0); + move_to(destination, g26_ooze_amount); + + has_control_of_lcd_panel = true; + //debug_current_and_destination(PSTR("Starting G26 Mesh Validation Pattern.")); + + /** + * Declare and generate a sin() & cos() table to be used during the circle drawing. This will lighten + * the CPU load and make the arc drawing faster and more smooth + */ + float sin_table[360 / 30 + 1], cos_table[360 / 30 + 1]; + for (i = 0; i <= 360 / 30; i++) { + cos_table[i] = SIZE_OF_INTERSECTION_CIRCLES * cos(RADIANS(valid_trig_angle(i * 30.0))); + sin_table[i] = SIZE_OF_INTERSECTION_CIRCLES * sin(RADIANS(valid_trig_angle(i * 30.0))); + } + + do { + location = g26_continue_with_closest + ? find_closest_circle_to_print(current_position[X_AXIS], current_position[Y_AXIS]) + : find_closest_circle_to_print(g26_x_pos, g26_y_pos); // Find the closest Mesh Intersection to where we are now. + + if (location.x_index >= 0 && location.y_index >= 0) { + const float circle_x = mesh_index_to_xpos(location.x_index), + circle_y = mesh_index_to_ypos(location.y_index); + + // If this mesh location is outside the printable_radius, skip it. + + if (!position_is_reachable(circle_x, circle_y)) continue; + + xi = location.x_index; // Just to shrink the next few lines and make them easier to understand + yi = location.y_index; + + if (g26_debug_flag) { + SERIAL_ECHOPAIR(" Doing circle at: (xi=", xi); + SERIAL_ECHOPAIR(", yi=", yi); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + + start_angle = 0.0; // assume it is going to be a full circle + end_angle = 360.0; + if (xi == 0) { // Check for bottom edge + start_angle = -90.0; + end_angle = 90.0; + if (yi == 0) // it is an edge, check for the two left corners + start_angle = 0.0; + else if (yi == GRID_MAX_POINTS_Y - 1) + end_angle = 0.0; + } + else if (xi == GRID_MAX_POINTS_X - 1) { // Check for top edge + start_angle = 90.0; + end_angle = 270.0; + if (yi == 0) // it is an edge, check for the two right corners + end_angle = 180.0; + else if (yi == GRID_MAX_POINTS_Y - 1) + start_angle = 180.0; + } + else if (yi == 0) { + start_angle = 0.0; // only do the top side of the cirlce + end_angle = 180.0; + } + else if (yi == GRID_MAX_POINTS_Y - 1) { + start_angle = 180.0; // only do the bottom side of the cirlce + end_angle = 360.0; + } + + for (tmp = start_angle; tmp < end_angle - 0.1; tmp += 30.0) { + + #if ENABLED(NEWPANEL) + if (user_canceled()) goto LEAVE; // Check if the user wants to stop the Mesh Validation + #endif + + int tmp_div_30 = tmp / 30.0; + if (tmp_div_30 < 0) tmp_div_30 += 360 / 30; + if (tmp_div_30 > 11) tmp_div_30 -= 360 / 30; + + float rx = circle_x + cos_table[tmp_div_30], // for speed, these are now a lookup table entry + ry = circle_y + sin_table[tmp_div_30], + xe = circle_x + cos_table[tmp_div_30 + 1], + ye = circle_y + sin_table[tmp_div_30 + 1]; + #if IS_KINEMATIC + // Check to make sure this segment is entirely on the bed, skip if not. + if (!position_is_reachable(rx, ry) || !position_is_reachable(xe, ye)) continue; + #else // not, we need to skip + rx = constrain(rx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops + ry = constrain(ry, Y_MIN_POS + 1, Y_MAX_POS - 1); + xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1); + ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1); + #endif + + //if (g26_debug_flag) { + // char ccc, *cptr, seg_msg[50], seg_num[10]; + // strcpy(seg_msg, " segment: "); + // strcpy(seg_num, " \n"); + // cptr = (char*) "01234567890ABCDEF????????"; + // ccc = cptr[tmp_div_30]; + // seg_num[1] = ccc; + // strcat(seg_msg, seg_num); + // debug_current_and_destination(seg_msg); + //} + + print_line_from_here_to_there(rx, ry, g26_layer_height, xe, ye, g26_layer_height); + + } + if (look_for_lines_to_connect()) + goto LEAVE; + } + } while (--g26_repeats && location.x_index >= 0 && location.y_index >= 0); + + LEAVE: + lcd_setstatusPGM(PSTR("Leaving G26"), -1); + + retract_filament(destination); + destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; + + //debug_current_and_destination(PSTR("ready to do Z-Raise.")); + move_to(destination, 0); // Raise the nozzle + //debug_current_and_destination(PSTR("done doing Z-Raise.")); + + destination[X_AXIS] = g26_x_pos; // Move back to the starting position + destination[Y_AXIS] = g26_y_pos; + //destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Keep the nozzle where it is + + move_to(destination, 0); // Move back to the starting position + //debug_current_and_destination(PSTR("done doing X/Y move.")); + + has_control_of_lcd_panel = false; // Give back control of the LCD Panel! + + if (!g26_keep_heaters_on) { + #if HAS_TEMP_BED + thermalManager.setTargetBed(0); + #endif + thermalManager.setTargetHotend(0, 0); + } + } + + float valid_trig_angle(float d) { + while (d > 360.0) d -= 360.0; + while (d < 0.0) d += 360.0; + return d; + } + + mesh_index_pair unified_bed_leveling::find_closest_circle_to_print(const float &X, const float &Y) { + float closest = 99999.99; + mesh_index_pair return_val; + + return_val.x_index = return_val.y_index = -1; + + for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) { + for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) { + if (!is_bit_set(circle_flags, i, j)) { + const float mx = mesh_index_to_xpos(i), // We found a circle that needs to be printed + my = mesh_index_to_ypos(j); + + // Get the distance to this intersection + float f = HYPOT(X - mx, Y - my); + + // It is possible that we are being called with the values + // to let us find the closest circle to the start position. + // But if this is not the case, add a small weighting to the + // distance calculation to help it choose a better place to continue. + f += HYPOT(g26_x_pos - mx, g26_y_pos - my) / 15.0; + + // Add in the specified amount of Random Noise to our search + if (random_deviation > 1.0) + f += random(0.0, random_deviation); + + if (f < closest) { + closest = f; // We found a closer location that is still + return_val.x_index = i; // un-printed --- save the data for it + return_val.y_index = j; + return_val.distance = closest; + } + } + } + } + bit_set(circle_flags, return_val.x_index, return_val.y_index); // Mark this location as done. + return return_val; + } + + bool unified_bed_leveling::look_for_lines_to_connect() { + float sx, sy, ex, ey; + + for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) { + for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) { + + #if ENABLED(NEWPANEL) + if (user_canceled()) return true; // Check if the user wants to stop the Mesh Validation + #endif + + if (i < GRID_MAX_POINTS_X) { // We can't connect to anything to the right than GRID_MAX_POINTS_X. + // This is already a half circle because we are at the edge of the bed. + + if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i + 1, j)) { // check if we can do a line to the left + if (!is_bit_set(horizontal_mesh_line_flags, i, j)) { + + // + // We found two circles that need a horizontal line to connect them + // Print it! + // + sx = mesh_index_to_xpos( i ) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // right edge + ex = mesh_index_to_xpos(i + 1) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // left edge + + sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); + sy = ey = constrain(mesh_index_to_ypos(j), Y_MIN_POS + 1, Y_MAX_POS - 1); + ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1); + + if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) { + + if (g26_debug_flag) { + SERIAL_ECHOPAIR(" Connecting with horizontal line (sx=", sx); + SERIAL_ECHOPAIR(", sy=", sy); + SERIAL_ECHOPAIR(") -> (ex=", ex); + SERIAL_ECHOPAIR(", ey=", ey); + SERIAL_CHAR(')'); + SERIAL_EOL(); + //debug_current_and_destination(PSTR("Connecting horizontal line.")); + } + print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height); + } + bit_set(horizontal_mesh_line_flags, i, j); // Mark it as done so we don't do it again, even if we skipped it + } + } + + if (j < GRID_MAX_POINTS_Y) { // We can't connect to anything further back than GRID_MAX_POINTS_Y. + // This is already a half circle because we are at the edge of the bed. + + if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i, j + 1)) { // check if we can do a line straight down + if (!is_bit_set( vertical_mesh_line_flags, i, j)) { + // + // We found two circles that need a vertical line to connect them + // Print it! + // + sy = mesh_index_to_ypos( j ) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // top edge + ey = mesh_index_to_ypos(j + 1) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // bottom edge + + sx = ex = constrain(mesh_index_to_xpos(i), X_MIN_POS + 1, X_MAX_POS - 1); + sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1); + ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1); + + if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) { + + if (g26_debug_flag) { + SERIAL_ECHOPAIR(" Connecting with vertical line (sx=", sx); + SERIAL_ECHOPAIR(", sy=", sy); + SERIAL_ECHOPAIR(") -> (ex=", ex); + SERIAL_ECHOPAIR(", ey=", ey); + SERIAL_CHAR(')'); + SERIAL_EOL(); + debug_current_and_destination(PSTR("Connecting vertical line.")); + } + print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height); + } + bit_set(vertical_mesh_line_flags, i, j); // Mark it as done so we don't do it again, even if skipped + } + } + } + } + } + } + return false; + } + + void unified_bed_leveling::move_to(const float &x, const float &y, const float &z, const float &e_delta) { + float feed_value; + static float last_z = -999.99; + + bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement. + + if (z != last_z) { + last_z = z; + feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0); // Base the feed rate off of the configured Z_AXIS feed rate + + destination[X_AXIS] = current_position[X_AXIS]; + destination[Y_AXIS] = current_position[Y_AXIS]; + destination[Z_AXIS] = z; // We know the last_z==z or we wouldn't be in this block of code. + destination[E_AXIS] = current_position[E_AXIS]; + + G26_line_to_destination(feed_value); + + stepper.synchronize(); + set_destination_from_current(); + } + + // Check if X or Y is involved in the movement. + // Yes: a 'normal' movement. No: a retract() or recover() + feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5; + + if (g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() feed_value for XY:", feed_value); + + destination[X_AXIS] = x; + destination[Y_AXIS] = y; + destination[E_AXIS] += e_delta; + + G26_line_to_destination(feed_value); + + stepper.synchronize(); + set_destination_from_current(); + + } + + void unified_bed_leveling::retract_filament(const float where[XYZE]) { + if (!g26_retracted) { // Only retract if we are not already retracted! + g26_retracted = true; + move_to(where, -1.0 * g26_retraction_multiplier); + } + } + + void unified_bed_leveling::recover_filament(const float where[XYZE]) { + if (g26_retracted) { // Only un-retract if we are retracted. + move_to(where, 1.2 * g26_retraction_multiplier); + g26_retracted = false; + } + } + + /** + * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one + * to the other. But there are really three sets of coordinates involved. The first coordinate + * is the present location of the nozzle. We don't necessarily want to print from this location. + * We first need to move the nozzle to the start of line segment where we want to print. Once + * there, we can use the two coordinates supplied to draw the line. + * + * Note: Although we assume the first set of coordinates is the start of the line and the second + * set of coordinates is the end of the line, it does not always work out that way. This function + * optimizes the movement to minimize the travel distance before it can start printing. This saves + * a lot of time and eliminates a lot of nonsensical movement of the nozzle. However, it does + * cause a lot of very little short retracement of th nozzle when it draws the very first line + * segment of a 'circle'. The time this requires is very short and is easily saved by the other + * cases where the optimization comes into play. + */ + void unified_bed_leveling::print_line_from_here_to_there(const float &sx, const float &sy, const float &sz, const float &ex, const float &ey, const float &ez) { + const float dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual line segment + dy_s = current_position[Y_AXIS] - sy, + dist_start = HYPOT2(dx_s, dy_s), // We don't need to do a sqrt(), we can compare the distance^2 + // to save computation time + dx_e = current_position[X_AXIS] - ex, // find our distance from the end of the actual line segment + dy_e = current_position[Y_AXIS] - ey, + dist_end = HYPOT2(dx_e, dy_e), + + line_length = HYPOT(ex - sx, ey - sy); + + // If the end point of the line is closer to the nozzle, flip the direction, + // moving from the end to the start. On very small lines the optimization isn't worth it. + if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < FABS(line_length)) + return print_line_from_here_to_there(ex, ey, ez, sx, sy, sz); + + // Decide whether to retract & bump + + if (dist_start > 2.0) { + retract_filament(destination); + //todo: parameterize the bump height with a define + move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + 0.500, 0.0); // Z bump to minimize scraping + move_to(sx, sy, sz + 0.500, 0.0); // Get to the starting point with no extrusion while bumped + } + + move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion / un-Z bump + + const float e_pos_delta = line_length * g26_e_axis_feedrate * g26_extrusion_multiplier; + + recover_filament(destination); + move_to(ex, ey, ez, e_pos_delta); // Get to the ending point with an appropriate amount of extrusion + } + + /** + * This function used to be inline code in G26. But there are so many + * parameters it made sense to turn them into static globals and get + * this code out of sight of the main routine. + */ + bool unified_bed_leveling::parse_G26_parameters() { + + g26_extrusion_multiplier = EXTRUSION_MULTIPLIER; + g26_retraction_multiplier = RETRACTION_MULTIPLIER; + g26_nozzle = DEFAULT_NOZZLE_SIZE; + g26_filament_diameter = DEFAULT_NOMINAL_FILAMENT_DIA; + g26_layer_height = DEFAULT_LAYER_HEIGHT; + g26_prime_length = PRIME_LENGTH; + g26_bed_temp = DEFAULT_BED_TEMP; + g26_hotend_temp = DEFAULT_HOTEND_TEMP; + g26_prime_flag = 0; + + g26_ooze_amount = parser.linearval('O', OOZE_AMOUNT); + g26_keep_heaters_on = parser.boolval('K'); + g26_continue_with_closest = parser.boolval('C'); + + if (parser.seenval('B')) { + g26_bed_temp = parser.value_celsius(); + if (!WITHIN(g26_bed_temp, 15, 140)) { + SERIAL_PROTOCOLLNPGM("?Specified bed temperature not plausible."); + return UBL_ERR; + } + } + + if (parser.seenval('L')) { + g26_layer_height = parser.value_linear_units(); + if (!WITHIN(g26_layer_height, 0.0, 2.0)) { + SERIAL_PROTOCOLLNPGM("?Specified layer height not plausible."); + return UBL_ERR; + } + } + + if (parser.seen('Q')) { + if (parser.has_value()) { + g26_retraction_multiplier = parser.value_float(); + if (!WITHIN(g26_retraction_multiplier, 0.05, 15.0)) { + SERIAL_PROTOCOLLNPGM("?Specified Retraction Multiplier not plausible."); + return UBL_ERR; + } + } + else { + SERIAL_PROTOCOLLNPGM("?Retraction Multiplier must be specified."); + return UBL_ERR; + } + } + + if (parser.seenval('S')) { + g26_nozzle = parser.value_float(); + if (!WITHIN(g26_nozzle, 0.1, 1.0)) { + SERIAL_PROTOCOLLNPGM("?Specified nozzle size not plausible."); + return UBL_ERR; + } + } + + if (parser.seen('P')) { + if (!parser.has_value()) { + #if ENABLED(NEWPANEL) + g26_prime_flag = -1; + #else + SERIAL_PROTOCOLLNPGM("?Prime length must be specified when not using an LCD."); + return UBL_ERR; + #endif + } + else { + g26_prime_flag++; + g26_prime_length = parser.value_linear_units(); + if (!WITHIN(g26_prime_length, 0.0, 25.0)) { + SERIAL_PROTOCOLLNPGM("?Specified prime length not plausible."); + return UBL_ERR; + } + } + } + + if (parser.seenval('F')) { + g26_filament_diameter = parser.value_linear_units(); + if (!WITHIN(g26_filament_diameter, 1.0, 4.0)) { + SERIAL_PROTOCOLLNPGM("?Specified filament size not plausible."); + return UBL_ERR; + } + } + g26_extrusion_multiplier *= sq(1.75) / sq(g26_filament_diameter); // If we aren't using 1.75mm filament, we need to + // scale up or down the length needed to get the + // same volume of filament + + g26_extrusion_multiplier *= g26_filament_diameter * sq(g26_nozzle) / sq(0.3); // Scale up by nozzle size + + if (parser.seenval('H')) { + g26_hotend_temp = parser.value_celsius(); + if (!WITHIN(g26_hotend_temp, 165, 280)) { + SERIAL_PROTOCOLLNPGM("?Specified nozzle temperature not plausible."); + return UBL_ERR; + } + } + + if (parser.seen('U')) { + randomSeed(millis()); + // This setting will persist for the next G26 + random_deviation = parser.has_value() ? parser.value_float() : 50.0; + } + + #if ENABLED(NEWPANEL) + g26_repeats = parser.intval('R', GRID_MAX_POINTS + 1); + #else + if (!parser.seen('R')) { + SERIAL_PROTOCOLLNPGM("?(R)epeat must be specified when not using an LCD."); + return UBL_ERR; + } + else + g26_repeats = parser.has_value() ? parser.value_int() : GRID_MAX_POINTS + 1; + #endif + if (g26_repeats < 1) { + SERIAL_PROTOCOLLNPGM("?(R)epeat value not plausible; must be at least 1."); + return UBL_ERR; + } + + g26_x_pos = parser.seenval('X') ? RAW_X_POSITION(parser.value_linear_units()) : current_position[X_AXIS]; + g26_y_pos = parser.seenval('Y') ? RAW_Y_POSITION(parser.value_linear_units()) : current_position[Y_AXIS]; + if (!position_is_reachable(g26_x_pos, g26_y_pos)) { + SERIAL_PROTOCOLLNPGM("?Specified X,Y coordinate out of bounds."); + return UBL_ERR; + } + + /** + * Wait until all parameters are verified before altering the state! + */ + set_bed_leveling_enabled(!parser.seen('D')); + + return UBL_OK; + } + + #if ENABLED(NEWPANEL) + bool unified_bed_leveling::exit_from_g26() { + lcd_setstatusPGM(PSTR("Leaving G26"), -1); + while (ubl_lcd_clicked()) idle(); + return UBL_ERR; + } + #endif + + /** + * Turn on the bed and nozzle heat and + * wait for them to get up to temperature. + */ + bool unified_bed_leveling::turn_on_heaters() { + millis_t next = millis() + 5000UL; + #if HAS_TEMP_BED + #if ENABLED(ULTRA_LCD) + if (g26_bed_temp > 25) { + lcd_setstatusPGM(PSTR("G26 Heating Bed."), 99); + lcd_quick_feedback(); + #endif + has_control_of_lcd_panel = true; + thermalManager.setTargetBed(g26_bed_temp); + while (abs(thermalManager.degBed() - g26_bed_temp) > 3) { + + #if ENABLED(NEWPANEL) + if (ubl_lcd_clicked()) return exit_from_g26(); + #endif + + if (ELAPSED(millis(), next)) { + next = millis() + 5000UL; + print_heaterstates(); + SERIAL_EOL(); + } + idle(); + } + #if ENABLED(ULTRA_LCD) + } + lcd_setstatusPGM(PSTR("G26 Heating Nozzle."), 99); + lcd_quick_feedback(); + #endif + #endif + + // Start heating the nozzle and wait for it to reach temperature. + thermalManager.setTargetHotend(g26_hotend_temp, 0); + while (abs(thermalManager.degHotend(0) - g26_hotend_temp) > 3) { + + #if ENABLED(NEWPANEL) + if (ubl_lcd_clicked()) return exit_from_g26(); + #endif + + if (ELAPSED(millis(), next)) { + next = millis() + 5000UL; + print_heaterstates(); + SERIAL_EOL(); + } + idle(); + } + + #if ENABLED(ULTRA_LCD) + lcd_reset_status(); + lcd_quick_feedback(); + #endif + + return UBL_OK; + } + + /** + * Prime the nozzle if needed. Return true on error. + */ + bool unified_bed_leveling::prime_nozzle() { + + #if ENABLED(NEWPANEL) + float Total_Prime = 0.0; + + if (g26_prime_flag == -1) { // The user wants to control how much filament gets purged + + has_control_of_lcd_panel = true; + lcd_setstatusPGM(PSTR("User-Controlled Prime"), 99); + chirp_at_user(); + + set_destination_from_current(); + + recover_filament(destination); // Make sure G26 doesn't think the filament is retracted(). + + while (!ubl_lcd_clicked()) { + chirp_at_user(); + destination[E_AXIS] += 0.25; + #ifdef PREVENT_LENGTHY_EXTRUDE + Total_Prime += 0.25; + if (Total_Prime >= EXTRUDE_MAXLENGTH) return UBL_ERR; + #endif + G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0); + + stepper.synchronize(); // Without this synchronize, the purge is more consistent, + // but because the planner has a buffer, we won't be able + // to stop as quickly. So we put up with the less smooth + // action to give the user a more responsive 'Stop'. + set_destination_from_current(); + idle(); + } + + while (ubl_lcd_clicked()) idle(); // Debounce Encoder Wheel + + #if ENABLED(ULTRA_LCD) + strcpy_P(lcd_status_message, PSTR("Done Priming")); // We can't do lcd_setstatusPGM() without having it continue; + // So... We cheat to get a message up. + lcd_setstatusPGM(PSTR("Done Priming"), 99); + lcd_quick_feedback(); + #endif + + has_control_of_lcd_panel = false; + + } + else { + #else + { + #endif + #if ENABLED(ULTRA_LCD) + lcd_setstatusPGM(PSTR("Fixed Length Prime."), 99); + lcd_quick_feedback(); + #endif + set_destination_from_current(); + destination[E_AXIS] += g26_prime_length; + G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0); + stepper.synchronize(); + set_destination_from_current(); + retract_filament(destination); + } + + return UBL_OK; + } + +#endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_VALIDATION diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.cpp new file mode 100644 index 0000000..8433481 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.cpp @@ -0,0 +1,1132 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +//todo: add support for multiple encoders on a single axis +//todo: add z axis auto-leveling +//todo: consolidate some of the related M codes? +//todo: add endstop-replacement mode? +//todo: try faster I2C speed; tweak TWI_FREQ (400000L, or faster?); or just TWBR = ((CPU_FREQ / 400000L) - 16) / 2; +//todo: consider Marlin-optimized Wire library; i.e. MarlinWire, like MarlinSerial + + +#include "MarlinConfig.h" + +#if ENABLED(I2C_POSITION_ENCODERS) + + #include "Marlin.h" + #include "temperature.h" + #include "stepper.h" + #include "I2CPositionEncoder.h" + #include "gcode.h" + + #include + + + void I2CPositionEncoder::init(const uint8_t address, const AxisEnum axis) { + encoderAxis = axis; + i2cAddress = address; + + initialised++; + + SERIAL_ECHOPAIR("Setting up encoder on ", axis_codes[encoderAxis]); + SERIAL_ECHOLNPAIR(" axis, addr = ", address); + + position = get_position(); + } + + void I2CPositionEncoder::update() { + if (!initialised || !homed || !active) return; //check encoder is set up and active + + position = get_position(); + + //we don't want to stop things just because the encoder missed a message, + //so we only care about responses that indicate bad magnetic strength + + if (!passes_test(false)) { //check encoder data is good + lastErrorTime = millis(); + /* + if (trusted) { //commented out as part of the note below + trusted = false; + SERIAL_ECHOPGM("Fault detected on "); + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOLNPGM(" axis encoder. Disengaging error correction until module is trusted again."); + } + */ + return; + } + + if (!trusted) { + /** + * This is commented out because it introduces error and can cause bad print quality. + * + * This code is intended to manage situations where the encoder has reported bad magnetic strength. + * This indicates that the magnetic strip was too far away from the sensor to reliably track position. + * When this happens, this code resets the offset based on where the printer thinks it is. This has been + * shown to introduce errors in actual position which result in drifting prints and poor print quality. + * Perhaps a better method would be to disable correction on the axis with a problem, report it to the + * user via the status leds on the encoder module and prompt the user to re-home the axis at which point + * the encoder would be re-enabled. + */ + + /* + // If the magnetic strength has been good for a certain time, start trusting the module again + + if (millis() - lastErrorTime > I2CPE_TIME_TRUSTED) { + trusted = true; + + SERIAL_ECHOPGM("Untrusted encoder module on "); + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOLNPGM(" axis has been fault-free for set duration, reinstating error correction."); + + //the encoder likely lost its place when the error occured, so we'll reset and use the printer's + //idea of where it the axis is to re-initialise + float position = stepper.get_axis_position_mm(encoderAxis); + int32_t positionInTicks = position * get_ticks_unit(); + + //shift position from previous to current position + zeroOffset -= (positionInTicks - get_position()); + + #ifdef I2CPE_DEBUG + SERIAL_ECHOPGM("Current position is "); + SERIAL_ECHOLN(position); + + SERIAL_ECHOPGM("Position in encoder ticks is "); + SERIAL_ECHOLN(positionInTicks); + + SERIAL_ECHOPGM("New zero-offset of "); + SERIAL_ECHOLN(zeroOffset); + + SERIAL_ECHOPGM("New position reads as "); + SERIAL_ECHO(get_position()); + SERIAL_ECHOPGM("("); + SERIAL_ECHO(mm_from_count(get_position())); + SERIAL_ECHOLNPGM(")"); + #endif + } + */ + return; + } + + lastPosition = position; + const millis_t positionTime = millis(); + + //only do error correction if setup and enabled + if (ec && ecMethod != I2CPE_ECM_NONE) { + + #ifdef I2CPE_EC_THRESH_PROPORTIONAL + const millis_t deltaTime = positionTime - lastPositionTime; + const uint32_t distance = abs(position - lastPosition), + speed = distance / deltaTime; + const float threshold = constrain((speed / 50), 1, 50) * ecThreshold; + #else + const float threshold = get_error_correct_threshold(); + #endif + + //check error + #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE) + float sum = 0, diffSum = 0; + + errIdx = (errIdx >= I2CPE_ERR_ARRAY_SIZE - 1) ? 0 : errIdx + 1; + err[errIdx] = get_axis_error_steps(false); + + LOOP_L_N(i, I2CPE_ERR_ARRAY_SIZE) { + sum += err[i]; + if (i) diffSum += abs(err[i-1] - err[i]); + } + + const int32_t error = int32_t(sum / (I2CPE_ERR_ARRAY_SIZE + 1)); //calculate average for error + + #else + const int32_t error = get_axis_error_steps(false); + #endif + + //SERIAL_ECHOPGM("Axis error steps: "); + //SERIAL_ECHOLN(error); + + #ifdef I2CPE_ERR_THRESH_ABORT + if (labs(error) > I2CPE_ERR_THRESH_ABORT * planner.axis_steps_per_mm[encoderAxis]) { + //kill("Significant Error"); + SERIAL_ECHOPGM("Axis error greater than set threshold, aborting!"); + SERIAL_ECHOLN(error); + safe_delay(5000); + } + #endif + + #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE) + if (errIdx == 0) { + // in order to correct for "error" but avoid correcting for noise and non skips + // it must be > threshold and have a difference average of < 10 and be < 2000 steps + if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis] && + diffSum < 10 * (I2CPE_ERR_ARRAY_SIZE - 1) && labs(error) < 2000) { //Check for persistent error (skip) + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOPAIR(" diffSum: ", diffSum / (I2CPE_ERR_ARRAY_SIZE - 1)); + SERIAL_ECHOPAIR(" - err detected: ", error / planner.axis_steps_per_mm[encoderAxis]); + SERIAL_ECHOLNPGM("mm; correcting!"); + thermalManager.babystepsTodo[encoderAxis] = -LROUND(error); + } + } + #else + if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis]) { + //SERIAL_ECHOLN(error); + //SERIAL_ECHOLN(position); + thermalManager.babystepsTodo[encoderAxis] = -LROUND(error/2); + } + #endif + + if (labs(error) > I2CPE_ERR_CNT_THRESH * planner.axis_steps_per_mm[encoderAxis]) { + const millis_t ms = millis(); + if (ELAPSED(ms, nextErrorCountTime)) { + SERIAL_ECHOPAIR("Large error on ", axis_codes[encoderAxis]); + SERIAL_ECHOPAIR(" axis. error: ", (int)error); + SERIAL_ECHOLNPAIR("; diffSum: ", diffSum); + errorCount++; + nextErrorCountTime = ms + I2CPE_ERR_CNT_DEBOUNCE_MS; + } + } + } + + lastPositionTime = positionTime; + } + + void I2CPositionEncoder::set_homed() { + if (active) { + reset(); // Reset module's offset to zero (so current position is homed / zero) + delay(10); + + zeroOffset = get_raw_count(); + homed++; + trusted++; + + #ifdef I2CPE_DEBUG + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOPAIR(" axis encoder homed, offset of ", zeroOffset); + SERIAL_ECHOLNPGM(" ticks."); + #endif + } + } + + bool I2CPositionEncoder::passes_test(const bool report) { + if (report) { + if (H != I2CPE_MAG_SIG_GOOD) SERIAL_ECHOPGM("Warning. "); + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOPGM(" axis "); + serialprintPGM(H == I2CPE_MAG_SIG_BAD ? PSTR("magnetic strip ") : PSTR("encoder ")); + switch (H) { + case I2CPE_MAG_SIG_GOOD: + case I2CPE_MAG_SIG_MID: + SERIAL_ECHOLNPGM("passes test; field strength "); + serialprintPGM(H == I2CPE_MAG_SIG_GOOD ? PSTR("good.\n") : PSTR("fair.\n")); + break; + default: + SERIAL_ECHOLNPGM("not detected!"); + } + } + return (H == I2CPE_MAG_SIG_GOOD || H == I2CPE_MAG_SIG_MID); + } + + float I2CPositionEncoder::get_axis_error_mm(const bool report) { + float target, actual, error; + + target = stepper.get_axis_position_mm(encoderAxis); + actual = mm_from_count(position); + error = actual - target; + + if (labs(error) > 10000) error = 0; // ? + + if (report) { + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOPAIR(" axis target: ", target); + SERIAL_ECHOPAIR(", actual: ", actual); + SERIAL_ECHOLNPAIR(", error : ",error); + } + + return error; + } + + int32_t I2CPositionEncoder::get_axis_error_steps(const bool report) { + if (!active) { + if (report) { + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOLNPGM(" axis encoder not active!"); + } + return 0; + } + + float stepperTicksPerUnit; + int32_t encoderTicks = position, encoderCountInStepperTicksScaled; + //int32_t stepperTicks = stepper.position(encoderAxis); + + // With a rotary encoder we're concerned with ticks/rev; whereas with a linear we're concerned with ticks/mm + stepperTicksPerUnit = (type == I2CPE_ENC_TYPE_ROTARY) ? stepperTicks : planner.axis_steps_per_mm[encoderAxis]; + + //convert both 'ticks' into same units / base + encoderCountInStepperTicksScaled = LROUND((stepperTicksPerUnit * encoderTicks) / encoderTicksPerUnit); + + int32_t target = stepper.position(encoderAxis), + error = (encoderCountInStepperTicksScaled - target); + + //suppress discontinuities (might be caused by bad I2C readings...?) + bool suppressOutput = (labs(error - errorPrev) > 100); + + if (report) { + SERIAL_ECHO(axis_codes[encoderAxis]); + SERIAL_ECHOPAIR(" axis target: ", target); + SERIAL_ECHOPAIR(", actual: ", encoderCountInStepperTicksScaled); + SERIAL_ECHOLNPAIR(", error : ", error); + + if (suppressOutput) SERIAL_ECHOLNPGM("Discontinuity detected, suppressing error."); + } + + errorPrev = error; + + return (suppressOutput ? 0 : error); + } + + int32_t I2CPositionEncoder::get_raw_count() { + uint8_t index = 0; + i2cLong encoderCount; + + encoderCount.val = 0x00; + + if (Wire.requestFrom((int)i2cAddress, 3) != 3) { + //houston, we have a problem... + H = I2CPE_MAG_SIG_NF; + return 0; + } + + while (Wire.available()) + encoderCount.bval[index++] = (uint8_t)Wire.read(); + + //extract the magnetic strength + H = (B00000011 & (encoderCount.bval[2] >> 6)); + + //extract sign bit; sign = (encoderCount.bval[2] & B00100000); + //set all upper bits to the sign value to overwrite H + encoderCount.val = (encoderCount.bval[2] & B00100000) ? (encoderCount.val | 0xFFC00000) : (encoderCount.val & 0x003FFFFF); + + if (invert) encoderCount.val *= -1; + + return encoderCount.val; + } + + bool I2CPositionEncoder::test_axis() { + //only works on XYZ cartesian machines for the time being + if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false; + + float startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 }; + + const float startPosition = soft_endstop_min[encoderAxis] + 10, + endPosition = soft_endstop_max[encoderAxis] - 10, + feedrate = FLOOR(MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY)); + + ec = false; + + LOOP_NA(i) { + startCoord[i] = stepper.get_axis_position_mm((AxisEnum)i); + endCoord[i] = stepper.get_axis_position_mm((AxisEnum)i); + } + + startCoord[encoderAxis] = startPosition; + endCoord[encoderAxis] = endPosition; + + stepper.synchronize(); + + planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS], + stepper.get_axis_position_mm(E_AXIS), feedrate, 0); + stepper.synchronize(); + + // if the module isn't currently trusted, wait until it is (or until it should be if things are working) + if (!trusted) { + int32_t startWaitingTime = millis(); + while (!trusted && millis() - startWaitingTime < I2CPE_TIME_TRUSTED) + safe_delay(500); + } + + if (trusted) { // if trusted, commence test + planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS], + stepper.get_axis_position_mm(E_AXIS), feedrate, 0); + stepper.synchronize(); + } + + return trusted; + } + + void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) { + if (type != I2CPE_ENC_TYPE_LINEAR) { + SERIAL_ECHOLNPGM("Steps per mm calibration is only available using linear encoders."); + return; + } + + if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) { + SERIAL_ECHOLNPGM("Automatic steps / mm calibration not supported for this axis."); + return; + } + + float old_steps_mm, new_steps_mm, + startDistance, endDistance, + travelDistance, travelledDistance, total = 0, + startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 }; + + float feedrate; + + int32_t startCount, stopCount; + + feedrate = MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY); + + bool oldec = ec; + ec = false; + + startDistance = 20; + endDistance = soft_endstop_max[encoderAxis] - 20; + travelDistance = endDistance - startDistance; + + LOOP_NA(i) { + startCoord[i] = stepper.get_axis_position_mm((AxisEnum)i); + endCoord[i] = stepper.get_axis_position_mm((AxisEnum)i); + } + + startCoord[encoderAxis] = startDistance; + endCoord[encoderAxis] = endDistance; + + LOOP_L_N(i, iter) { + stepper.synchronize(); + + planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS], + stepper.get_axis_position_mm(E_AXIS), feedrate, 0); + stepper.synchronize(); + + delay(250); + startCount = get_position(); + + //do_blocking_move_to(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS]); + + planner.buffer_line(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS], + stepper.get_axis_position_mm(E_AXIS), feedrate, 0); + stepper.synchronize(); + + //Read encoder distance + delay(250); + stopCount = get_position(); + + travelledDistance = mm_from_count(abs(stopCount - startCount)); + + SERIAL_ECHOPAIR("Attempted to travel: ", travelDistance); + SERIAL_ECHOLNPGM("mm."); + + SERIAL_ECHOPAIR("Actually travelled: ", travelledDistance); + SERIAL_ECHOLNPGM("mm."); + + //Calculate new axis steps per unit + old_steps_mm = planner.axis_steps_per_mm[encoderAxis]; + new_steps_mm = (old_steps_mm * travelDistance) / travelledDistance; + + SERIAL_ECHOLNPAIR("Old steps per mm: ", old_steps_mm); + SERIAL_ECHOLNPAIR("New steps per mm: ", new_steps_mm); + + //Save new value + planner.axis_steps_per_mm[encoderAxis] = new_steps_mm; + + if (iter > 1) { + total += new_steps_mm; + + // swap start and end points so next loop runs from current position + float tempCoord = startCoord[encoderAxis]; + startCoord[encoderAxis] = endCoord[encoderAxis]; + endCoord[encoderAxis] = tempCoord; + } + } + + if (iter > 1) { + total /= (float)iter; + SERIAL_ECHOLNPAIR("Average steps per mm: ", total); + } + + ec = oldec; + + SERIAL_ECHOLNPGM("Calculated steps per mm has been set. Please save to EEPROM (M500) if you wish to keep these values."); + } + + void I2CPositionEncoder::reset() { + Wire.beginTransmission(i2cAddress); + Wire.write(I2CPE_RESET_COUNT); + Wire.endTransmission(); + + #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE) + ZERO(err); + #endif + } + + + bool I2CPositionEncodersMgr::I2CPE_anyaxis; + uint8_t I2CPositionEncodersMgr::I2CPE_addr, + I2CPositionEncodersMgr::I2CPE_idx; + I2CPositionEncoder I2CPositionEncodersMgr::encoders[I2CPE_ENCODER_CNT]; + + void I2CPositionEncodersMgr::init() { + Wire.begin(); + + #if I2CPE_ENCODER_CNT > 0 + uint8_t i = 0; + + encoders[i].init(I2CPE_ENC_1_ADDR, I2CPE_ENC_1_AXIS); + + #ifdef I2CPE_ENC_1_TYPE + encoders[i].set_type(I2CPE_ENC_1_TYPE); + #endif + #ifdef I2CPE_ENC_1_TICKS_UNIT + encoders[i].set_ticks_unit(I2CPE_ENC_1_TICKS_UNIT); + #endif + #ifdef I2CPE_ENC_1_TICKS_REV + encoders[i].set_stepper_ticks(I2CPE_ENC_1_TICKS_REV); + #endif + #ifdef I2CPE_ENC_1_INVERT + encoders[i].set_inverted(I2CPE_ENC_1_INVERT); + #endif + #ifdef I2CPE_ENC_1_EC_METHOD + encoders[i].set_ec_method(I2CPE_ENC_1_EC_METHOD); + #endif + #ifdef I2CPE_ENC_1_EC_THRESH + encoders[i].set_ec_threshold(I2CPE_ENC_1_EC_THRESH); + #endif + + encoders[i].set_active(encoders[i].passes_test(true)); + + #if I2CPE_ENC_1_AXIS == E_AXIS + encoders[i].set_homed(); + #endif + #endif + + #if I2CPE_ENCODER_CNT > 1 + i++; + + encoders[i].init(I2CPE_ENC_2_ADDR, I2CPE_ENC_2_AXIS); + + #ifdef I2CPE_ENC_2_TYPE + encoders[i].set_type(I2CPE_ENC_2_TYPE); + #endif + #ifdef I2CPE_ENC_2_TICKS_UNIT + encoders[i].set_ticks_unit(I2CPE_ENC_2_TICKS_UNIT); + #endif + #ifdef I2CPE_ENC_2_TICKS_REV + encoders[i].set_stepper_ticks(I2CPE_ENC_2_TICKS_REV); + #endif + #ifdef I2CPE_ENC_2_INVERT + encoders[i].set_inverted(I2CPE_ENC_2_INVERT); + #endif + #ifdef I2CPE_ENC_2_EC_METHOD + encoders[i].set_ec_method(I2CPE_ENC_2_EC_METHOD); + #endif + #ifdef I2CPE_ENC_2_EC_THRESH + encoders[i].set_ec_threshold(I2CPE_ENC_2_EC_THRESH); + #endif + + encoders[i].set_active(encoders[i].passes_test(true)); + + #if I2CPE_ENC_2_AXIS == E_AXIS + encoders[i].set_homed(); + #endif + #endif + + #if I2CPE_ENCODER_CNT > 2 + i++; + + encoders[i].init(I2CPE_ENC_3_ADDR, I2CPE_ENC_3_AXIS); + + #ifdef I2CPE_ENC_3_TYPE + encoders[i].set_type(I2CPE_ENC_3_TYPE); + #endif + #ifdef I2CPE_ENC_3_TICKS_UNIT + encoders[i].set_ticks_unit(I2CPE_ENC_3_TICKS_UNIT); + #endif + #ifdef I2CPE_ENC_3_TICKS_REV + encoders[i].set_stepper_ticks(I2CPE_ENC_3_TICKS_REV); + #endif + #ifdef I2CPE_ENC_3_INVERT + encoders[i].set_inverted(I2CPE_ENC_3_INVERT); + #endif + #ifdef I2CPE_ENC_3_EC_METHOD + encoders[i].set_ec_method(I2CPE_ENC_3_EC_METHOD); + #endif + #ifdef I2CPE_ENC_3_EC_THRESH + encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH); + #endif + + encoders[i].set_active(encoders[i].passes_test(true)); + + #if I2CPE_ENC_3_AXIS == E_AXIS + encoders[i].set_homed(); + #endif + #endif + + #if I2CPE_ENCODER_CNT > 3 + i++; + + encoders[i].init(I2CPE_ENC_4_ADDR, I2CPE_ENC_4_AXIS); + + #ifdef I2CPE_ENC_4_TYPE + encoders[i].set_type(I2CPE_ENC_4_TYPE); + #endif + #ifdef I2CPE_ENC_4_TICKS_UNIT + encoders[i].set_ticks_unit(I2CPE_ENC_4_TICKS_UNIT); + #endif + #ifdef I2CPE_ENC_4_TICKS_REV + encoders[i].set_stepper_ticks(I2CPE_ENC_4_TICKS_REV); + #endif + #ifdef I2CPE_ENC_4_INVERT + encoders[i].set_inverted(I2CPE_ENC_4_INVERT); + #endif + #ifdef I2CPE_ENC_4_EC_METHOD + encoders[i].set_ec_method(I2CPE_ENC_4_EC_METHOD); + #endif + #ifdef I2CPE_ENC_4_EC_THRESH + encoders[i].set_ec_threshold(I2CPE_ENC_4_EC_THRESH); + #endif + + encoders[i].set_active(encoders[i].passes_test(true)); + + #if I2CPE_ENC_4_AXIS == E_AXIS + encoders[i].set_homed(); + #endif + #endif + + #if I2CPE_ENCODER_CNT > 4 + i++; + + encoders[i].init(I2CPE_ENC_5_ADDR, I2CPE_ENC_5_AXIS); + + #ifdef I2CPE_ENC_5_TYPE + encoders[i].set_type(I2CPE_ENC_5_TYPE); + #endif + #ifdef I2CPE_ENC_5_TICKS_UNIT + encoders[i].set_ticks_unit(I2CPE_ENC_5_TICKS_UNIT); + #endif + #ifdef I2CPE_ENC_5_TICKS_REV + encoders[i].set_stepper_ticks(I2CPE_ENC_5_TICKS_REV); + #endif + #ifdef I2CPE_ENC_5_INVERT + encoders[i].set_inverted(I2CPE_ENC_5_INVERT); + #endif + #ifdef I2CPE_ENC_5_EC_METHOD + encoders[i].set_ec_method(I2CPE_ENC_5_EC_METHOD); + #endif + #ifdef I2CPE_ENC_5_EC_THRESH + encoders[i].set_ec_threshold(I2CPE_ENC_5_EC_THRESH); + #endif + + encoders[i].set_active(encoders[i].passes_test(true)); + + #if I2CPE_ENC_5_AXIS == E_AXIS + encoders[i].set_homed(); + #endif + #endif + } + + void I2CPositionEncodersMgr::report_position(const int8_t idx, const bool units, const bool noOffset) { + CHECK_IDX(); + + if (units) + SERIAL_ECHOLN(noOffset ? encoders[idx].mm_from_count(encoders[idx].get_raw_count()) : encoders[idx].get_position_mm()); + else { + if (noOffset) { + const int32_t raw_count = encoders[idx].get_raw_count(); + SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]); + SERIAL_CHAR(' '); + + for (uint8_t j = 31; j > 0; j--) + SERIAL_ECHO((bool)(0x00000001 & (raw_count >> j))); + + SERIAL_ECHO((bool)(0x00000001 & raw_count)); + SERIAL_CHAR(' '); + SERIAL_ECHOLN(raw_count); + } + else + SERIAL_ECHOLN(encoders[idx].get_position()); + } + } + + void I2CPositionEncodersMgr::change_module_address(const uint8_t oldaddr, const uint8_t newaddr) { + // First check 'new' address is not in use + Wire.beginTransmission(newaddr); + if (!Wire.endTransmission()) { + SERIAL_ECHOPAIR("?There is already a device with that address on the I2C bus! (", newaddr); + SERIAL_ECHOLNPGM(")"); + return; + } + + // Now check that we can find the module on the oldaddr address + Wire.beginTransmission(oldaddr); + if (Wire.endTransmission()) { + SERIAL_ECHOPAIR("?No module detected at this address! (", oldaddr); + SERIAL_ECHOLNPGM(")"); + return; + } + + SERIAL_ECHOPAIR("Module found at ", oldaddr); + SERIAL_ECHOLNPAIR(", changing address to ", newaddr); + + // Change the modules address + Wire.beginTransmission(oldaddr); + Wire.write(I2CPE_SET_ADDR); + Wire.write(newaddr); + Wire.endTransmission(); + + SERIAL_ECHOLNPGM("Address changed, resetting and waiting for confirmation.."); + + // Wait for the module to reset (can probably be improved by polling address with a timeout). + safe_delay(I2CPE_REBOOT_TIME); + + // Look for the module at the new address. + Wire.beginTransmission(newaddr); + if (Wire.endTransmission()) { + SERIAL_ECHOLNPGM("Address change failed! Check encoder module."); + return; + } + + SERIAL_ECHOLNPGM("Address change successful!"); + + // Now, if this module is configured, find which encoder instance it's supposed to correspond to + // and enable it (it will likely have failed initialisation on power-up, before the address change). + const int8_t idx = idx_from_addr(newaddr); + if (idx >= 0 && !encoders[idx].get_active()) { + SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]); + SERIAL_ECHOLNPGM(" axis encoder was not detected on printer startup. Trying again."); + encoders[idx].set_active(encoders[idx].passes_test(true)); + } + } + + void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) { + // First check there is a module + Wire.beginTransmission(address); + if (Wire.endTransmission()) { + SERIAL_ECHOPAIR("?No module detected at this address! (", address); + SERIAL_ECHOLNPGM(")"); + return; + } + + SERIAL_ECHOPAIR("Requesting version info from module at address ", address); + SERIAL_ECHOLNPGM(":"); + + Wire.beginTransmission(address); + Wire.write(I2CPE_SET_REPORT_MODE); + Wire.write(I2CPE_REPORT_VERSION); + Wire.endTransmission(); + + // Read value + if (Wire.requestFrom((int)address, 32)) { + char c; + while (Wire.available() > 0 && (c = (char)Wire.read()) > 0) + SERIAL_ECHO(c); + SERIAL_EOL(); + } + + // Set module back to normal (distance) mode + Wire.beginTransmission(address); + Wire.write(I2CPE_SET_REPORT_MODE); + Wire.write(I2CPE_REPORT_DISTANCE); + Wire.endTransmission(); + } + + int8_t I2CPositionEncodersMgr::parse() { + I2CPE_addr = 0; + + if (parser.seen('A')) { + + if (!parser.has_value()) { + SERIAL_PROTOCOLLNPGM("?A seen, but no address specified! [30-200]"); + return I2CPE_PARSE_ERR; + }; + + I2CPE_addr = parser.value_byte(); + if (!WITHIN(I2CPE_addr, 30, 200)) { // reserve the first 30 and last 55 + SERIAL_PROTOCOLLNPGM("?Address out of range. [30-200]"); + return I2CPE_PARSE_ERR; + } + + I2CPE_idx = idx_from_addr(I2CPE_addr); + if (I2CPE_idx >= I2CPE_ENCODER_CNT) { + SERIAL_PROTOCOLLNPGM("?No device with this address!"); + return I2CPE_PARSE_ERR; + } + } + else if (parser.seenval('I')) { + + if (!parser.has_value()) { + SERIAL_PROTOCOLLNPAIR("?I seen, but no index specified! [0-", I2CPE_ENCODER_CNT - 1); + SERIAL_PROTOCOLLNPGM("]"); + return I2CPE_PARSE_ERR; + }; + + I2CPE_idx = parser.value_byte(); + if (I2CPE_idx >= I2CPE_ENCODER_CNT) { + SERIAL_PROTOCOLLNPAIR("?Index out of range. [0-", I2CPE_ENCODER_CNT - 1); + SERIAL_ECHOLNPGM("]"); + return I2CPE_PARSE_ERR; + } + + I2CPE_addr = encoders[I2CPE_idx].get_address(); + } + else + I2CPE_idx = 0xFF; + + I2CPE_anyaxis = parser.seen_axis(); + + return I2CPE_PARSE_OK; + }; + + /** + * M860: Report the position(s) of position encoder module(s). + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1] + * O Include homed zero-offset in returned position. + * U Units in mm or raw step count. + * + * If A or I not specified: + * X Report on X axis encoder, if present. + * Y Report on Y axis encoder, if present. + * Z Report on Z axis encoder, if present. + * E Report on E axis encoder, if present. + * + */ + void I2CPositionEncodersMgr::M860() { + if (parse()) return; + + const bool hasU = parser.seen('U'), hasO = parser.seen('O'); + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) report_position(idx, hasU, hasO); + } + } + } + else + report_position(I2CPE_idx, hasU, hasO); + } + + /** + * M861: Report the status of position encoder modules. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1] + * + * If A or I not specified: + * X Report on X axis encoder, if present. + * Y Report on Y axis encoder, if present. + * Z Report on Z axis encoder, if present. + * E Report on E axis encoder, if present. + * + */ + void I2CPositionEncodersMgr::M861() { + if (parse()) return; + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) report_status(idx); + } + } + } + else + report_status(I2CPE_idx); + } + + /** + * M862: Perform an axis continuity test for position encoder + * modules. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1] + * + * If A or I not specified: + * X Report on X axis encoder, if present. + * Y Report on Y axis encoder, if present. + * Z Report on Z axis encoder, if present. + * E Report on E axis encoder, if present. + * + */ + void I2CPositionEncodersMgr::M862() { + if (parse()) return; + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) test_axis(idx); + } + } + } + else + test_axis(I2CPE_idx); + } + + /** + * M863: Perform steps-per-mm calibration for + * position encoder modules. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1] + * P Number of rePeats/iterations. + * + * If A or I not specified: + * X Report on X axis encoder, if present. + * Y Report on Y axis encoder, if present. + * Z Report on Z axis encoder, if present. + * E Report on E axis encoder, if present. + * + */ + void I2CPositionEncodersMgr::M863() { + if (parse()) return; + + const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10); + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations); + } + } + } + else + calibrate_steps_mm(I2CPE_idx, iterations); + } + + /** + * M864: Change position encoder module I2C address. + * + * A Module current/old I2C address. If not present, + * assumes default address (030). [30, 200]. + * S Module new I2C address. [30, 200]. + * + * If S is not specified: + * X Use I2CPE_PRESET_ADDR_X (030). + * Y Use I2CPE_PRESET_ADDR_Y (031). + * Z Use I2CPE_PRESET_ADDR_Z (032). + * E Use I2CPE_PRESET_ADDR_E (033). + */ + void I2CPositionEncodersMgr::M864() { + uint8_t newAddress; + + if (parse()) return; + + if (!I2CPE_addr) I2CPE_addr = I2CPE_PRESET_ADDR_X; + + if (parser.seen('S')) { + if (!parser.has_value()) { + SERIAL_PROTOCOLLNPGM("?S seen, but no address specified! [30-200]"); + return; + }; + + newAddress = parser.value_byte(); + if (!WITHIN(newAddress, 30, 200)) { + SERIAL_PROTOCOLLNPGM("?New address out of range. [30-200]"); + return; + } + } + else if (!I2CPE_anyaxis) { + SERIAL_PROTOCOLLNPGM("?You must specify S or [XYZE]."); + return; + } + else { + if (parser.seen('X')) newAddress = I2CPE_PRESET_ADDR_X; + else if (parser.seen('Y')) newAddress = I2CPE_PRESET_ADDR_Y; + else if (parser.seen('Z')) newAddress = I2CPE_PRESET_ADDR_Z; + else if (parser.seen('E')) newAddress = I2CPE_PRESET_ADDR_E; + else return; + } + + SERIAL_ECHOPAIR("Changing module at address ", I2CPE_addr); + SERIAL_ECHOLNPAIR(" to address ", newAddress); + + change_module_address(I2CPE_addr, newAddress); + } + + /** + * M865: Check position encoder module firmware version. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1]. + * + * If A or I not specified: + * X Check X axis encoder, if present. + * Y Check Y axis encoder, if present. + * Z Check Z axis encoder, if present. + * E Check E axis encoder, if present. + */ + void I2CPositionEncodersMgr::M865() { + if (parse()) return; + + if (!I2CPE_addr) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) report_module_firmware(encoders[idx].get_address()); + } + } + } + else + report_module_firmware(I2CPE_addr); + } + + /** + * M866: Report or reset position encoder module error + * count. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1]. + * R Reset error counter. + * + * If A or I not specified: + * X Act on X axis encoder, if present. + * Y Act on Y axis encoder, if present. + * Z Act on Z axis encoder, if present. + * E Act on E axis encoder, if present. + */ + void I2CPositionEncodersMgr::M866() { + if (parse()) return; + + const bool hasR = parser.seen('R'); + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) { + if (hasR) + reset_error_count(idx, AxisEnum(i)); + else + report_error_count(idx, AxisEnum(i)); + } + } + } + } + else if (hasR) + reset_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis()); + else + report_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis()); + } + + /** + * M867: Enable/disable or toggle error correction for position encoder modules. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1]. + * S<1|0> Enable/disable error correction. 1 enables, 0 disables. If not + * supplied, toggle. + * + * If A or I not specified: + * X Act on X axis encoder, if present. + * Y Act on Y axis encoder, if present. + * Z Act on Z axis encoder, if present. + * E Act on E axis encoder, if present. + */ + void I2CPositionEncodersMgr::M867() { + if (parse()) return; + + const int8_t onoff = parser.seenval('S') ? parser.value_int() : -1; + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) { + const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff; + enable_ec(idx, ena, AxisEnum(i)); + } + } + } + } + else { + const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff; + enable_ec(I2CPE_idx, ena, encoders[I2CPE_idx].get_axis()); + } + } + + /** + * M868: Report or set position encoder module error correction + * threshold. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1]. + * T New error correction threshold. + * + * If A not specified: + * X Act on X axis encoder, if present. + * Y Act on Y axis encoder, if present. + * Z Act on Z axis encoder, if present. + * E Act on E axis encoder, if present. + */ + void I2CPositionEncodersMgr::M868() { + if (parse()) return; + + const float newThreshold = parser.seenval('T') ? parser.value_float() : -9999; + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) { + if (newThreshold != -9999) + set_ec_threshold(idx, newThreshold, encoders[idx].get_axis()); + else + get_ec_threshold(idx, encoders[idx].get_axis()); + } + } + } + } + else if (newThreshold != -9999) + set_ec_threshold(I2CPE_idx, newThreshold, encoders[I2CPE_idx].get_axis()); + else + get_ec_threshold(I2CPE_idx, encoders[I2CPE_idx].get_axis()); + } + + /** + * M869: Report position encoder module error. + * + * A Module I2C address. [30, 200]. + * I Module index. [0, I2CPE_ENCODER_CNT - 1]. + * + * If A not specified: + * X Act on X axis encoder, if present. + * Y Act on Y axis encoder, if present. + * Z Act on Z axis encoder, if present. + * E Act on E axis encoder, if present. + */ + void I2CPositionEncodersMgr::M869() { + if (parse()) return; + + if (I2CPE_idx == 0xFF) { + LOOP_XYZE(i) { + if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) { + const uint8_t idx = idx_from_axis(AxisEnum(i)); + if ((int8_t)idx >= 0) report_error(idx); + } + } + } + else + report_error(I2CPE_idx); + } + +#endif // I2C_POSITION_ENCODERS diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.h new file mode 100644 index 0000000..8380241 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/I2CPositionEncoder.h @@ -0,0 +1,346 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef I2CPOSENC_H +#define I2CPOSENC_H + +#include "MarlinConfig.h" + +#if ENABLED(I2C_POSITION_ENCODERS) + + #include "enum.h" + #include "macros.h" + #include "types.h" + #include + + //=========== Advanced / Less-Common Encoder Configuration Settings ========== + + #define I2CPE_EC_THRESH_PROPORTIONAL // if enabled adjusts the error correction threshold + // proportional to the current speed of the axis allows + // for very small error margin at low speeds without + // stuttering due to reading latency at high speeds + + #define I2CPE_DEBUG // enable encoder-related debug serial echos + + #define I2CPE_REBOOT_TIME 5000 // time we wait for an encoder module to reboot + // after changing address. + + #define I2CPE_MAG_SIG_GOOD 0 + #define I2CPE_MAG_SIG_MID 1 + #define I2CPE_MAG_SIG_BAD 2 + #define I2CPE_MAG_SIG_NF 255 + + #define I2CPE_REQ_REPORT 0 + #define I2CPE_RESET_COUNT 1 + #define I2CPE_SET_ADDR 2 + #define I2CPE_SET_REPORT_MODE 3 + #define I2CPE_CLEAR_EEPROM 4 + + #define I2CPE_LED_PAR_MODE 10 + #define I2CPE_LED_PAR_BRT 11 + #define I2CPE_LED_PAR_RATE 14 + + #define I2CPE_REPORT_DISTANCE 0 + #define I2CPE_REPORT_STRENGTH 1 + #define I2CPE_REPORT_VERSION 2 + + // Default I2C addresses + #define I2CPE_PRESET_ADDR_X 30 + #define I2CPE_PRESET_ADDR_Y 31 + #define I2CPE_PRESET_ADDR_Z 32 + #define I2CPE_PRESET_ADDR_E 33 + + #define I2CPE_DEF_AXIS X_AXIS + #define I2CPE_DEF_ADDR I2CPE_PRESET_ADDR_X + + // Error event counter; tracks how many times there is an error exceeding a certain threshold + #define I2CPE_ERR_CNT_THRESH 3.00 + #define I2CPE_ERR_CNT_DEBOUNCE_MS 2000 + + #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE) + #define I2CPE_ERR_ARRAY_SIZE 32 + #endif + + // Error Correction Methods + #define I2CPE_ECM_NONE 0 + #define I2CPE_ECM_MICROSTEP 1 + #define I2CPE_ECM_PLANNER 2 + #define I2CPE_ECM_STALLDETECT 3 + + // Encoder types + #define I2CPE_ENC_TYPE_ROTARY 0 + #define I2CPE_ENC_TYPE_LINEAR 1 + + // Parser + #define I2CPE_PARSE_ERR 1 + #define I2CPE_PARSE_OK 0 + + #define LOOP_PE(VAR) LOOP_L_N(VAR, I2CPE_ENCODER_CNT) + #define CHECK_IDX() do{ if (!WITHIN(idx, 0, I2CPE_ENCODER_CNT - 1)) return; }while(0) + + extern const char axis_codes[XYZE]; + + typedef union { + volatile int32_t val = 0; + uint8_t bval[4]; + } i2cLong; + + class I2CPositionEncoder { + private: + AxisEnum encoderAxis = I2CPE_DEF_AXIS; + + uint8_t i2cAddress = I2CPE_DEF_ADDR, + ecMethod = I2CPE_DEF_EC_METHOD, + type = I2CPE_DEF_TYPE, + H = I2CPE_MAG_SIG_NF; // Magnetic field strength + + int encoderTicksPerUnit = I2CPE_DEF_ENC_TICKS_UNIT, + stepperTicks = I2CPE_DEF_TICKS_REV, + errorCount = 0, + errorPrev = 0; + + float ecThreshold = I2CPE_DEF_EC_THRESH; + + bool homed = false, + trusted = false, + initialised = false, + active = false, + invert = false, + ec = true; + + int32_t zeroOffset = 0, + lastPosition = 0, + position; + + millis_t lastPositionTime = 0, + nextErrorCountTime = 0, + lastErrorTime; + + //double positionMm; //calculate + + #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE) + uint8_t errIdx = 0; + int err[I2CPE_ERR_ARRAY_SIZE] = { 0 }; + #endif + + //float positionMm; //calculate + + public: + void init(const uint8_t address, const AxisEnum axis); + void reset(); + + void update(); + + void set_homed(); + + int32_t get_raw_count(); + + FORCE_INLINE float mm_from_count(const int32_t count) { + switch (type) { + default: return -1; + case I2CPE_ENC_TYPE_LINEAR: + return count / encoderTicksPerUnit; + case I2CPE_ENC_TYPE_ROTARY: + return (count * stepperTicks) / (encoderTicksPerUnit * planner.axis_steps_per_mm[encoderAxis]); + } + } + + FORCE_INLINE float get_position_mm() { return mm_from_count(get_position()); } + FORCE_INLINE int32_t get_position() { return get_raw_count() - zeroOffset; } + + int32_t get_axis_error_steps(const bool report); + float get_axis_error_mm(const bool report); + + void calibrate_steps_mm(const uint8_t iter); + + bool passes_test(const bool report); + + bool test_axis(void); + + FORCE_INLINE int get_error_count(void) { return errorCount; } + FORCE_INLINE void set_error_count(const int newCount) { errorCount = newCount; } + + FORCE_INLINE uint8_t get_address() { return i2cAddress; } + FORCE_INLINE void set_address(const uint8_t addr) { i2cAddress = addr; } + + FORCE_INLINE bool get_active(void) { return active; } + FORCE_INLINE void set_active(const bool a) { active = a; } + + FORCE_INLINE void set_inverted(const bool i) { invert = i; } + + FORCE_INLINE AxisEnum get_axis() { return encoderAxis; } + + FORCE_INLINE bool get_ec_enabled() { return ec; } + FORCE_INLINE void set_ec_enabled(const bool enabled) { ec = enabled; } + + FORCE_INLINE uint8_t get_ec_method() { return ecMethod; } + FORCE_INLINE void set_ec_method(const byte method) { ecMethod = method; } + + FORCE_INLINE float get_ec_threshold() { return ecThreshold; } + FORCE_INLINE void set_ec_threshold(const float newThreshold) { ecThreshold = newThreshold; } + + FORCE_INLINE int get_encoder_ticks_mm() { + switch (type) { + default: return 0; + case I2CPE_ENC_TYPE_LINEAR: + return encoderTicksPerUnit; + case I2CPE_ENC_TYPE_ROTARY: + return (int)((encoderTicksPerUnit / stepperTicks) * planner.axis_steps_per_mm[encoderAxis]); + } + } + + FORCE_INLINE int get_ticks_unit() { return encoderTicksPerUnit; } + FORCE_INLINE void set_ticks_unit(const int ticks) { encoderTicksPerUnit = ticks; } + + FORCE_INLINE uint8_t get_type() { return type; } + FORCE_INLINE void set_type(const byte newType) { type = newType; } + + FORCE_INLINE int get_stepper_ticks() { return stepperTicks; } + FORCE_INLINE void set_stepper_ticks(const int ticks) { stepperTicks = ticks; } + }; + + class I2CPositionEncodersMgr { + private: + static bool I2CPE_anyaxis; + static uint8_t I2CPE_addr, I2CPE_idx; + + public: + + static void init(void); + + // consider only updating one endoder per call / tick if encoders become too time intensive + static void update(void) { LOOP_PE(i) encoders[i].update(); } + + static void homed(const AxisEnum axis) { + LOOP_PE(i) + if (encoders[i].get_axis() == axis) encoders[i].set_homed(); + } + + static void report_position(const int8_t idx, const bool units, const bool noOffset); + + static void report_status(const int8_t idx) { + CHECK_IDX(); + SERIAL_ECHOPAIR("Encoder ",idx); + SERIAL_ECHOPGM(": "); + encoders[idx].get_raw_count(); + encoders[idx].passes_test(true); + } + + static void report_error(const int8_t idx) { + CHECK_IDX(); + encoders[idx].get_axis_error_steps(true); + } + + static void test_axis(const int8_t idx) { + CHECK_IDX(); + encoders[idx].test_axis(); + } + + static void calibrate_steps_mm(const int8_t idx, const int iterations) { + CHECK_IDX(); + encoders[idx].calibrate_steps_mm(iterations); + } + + static void change_module_address(const uint8_t oldaddr, const uint8_t newaddr); + static void report_module_firmware(const uint8_t address); + + static void report_error_count(const int8_t idx, const AxisEnum axis) { + CHECK_IDX(); + SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]); + SERIAL_ECHOLNPAIR(" axis is ", encoders[idx].get_error_count()); + } + + static void reset_error_count(const int8_t idx, const AxisEnum axis) { + CHECK_IDX(); + encoders[idx].set_error_count(0); + SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]); + SERIAL_ECHOLNPGM(" axis has been reset."); + } + + static void enable_ec(const int8_t idx, const bool enabled, const AxisEnum axis) { + CHECK_IDX(); + encoders[idx].set_ec_enabled(enabled); + SERIAL_ECHOPAIR("Error correction on ", axis_codes[axis]); + SERIAL_ECHOPGM(" axis is "); + serialprintPGM(encoders[idx].get_ec_enabled() ? PSTR("en") : PSTR("dis")); + SERIAL_ECHOLNPGM("abled."); + } + + static void set_ec_threshold(const int8_t idx, const float newThreshold, const AxisEnum axis) { + CHECK_IDX(); + encoders[idx].set_ec_threshold(newThreshold); + SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]); + SERIAL_ECHOPAIR_F(" axis set to ", newThreshold); + SERIAL_ECHOLNPGM("mm."); + } + + static void get_ec_threshold(const int8_t idx, const AxisEnum axis) { + CHECK_IDX(); + const float threshold = encoders[idx].get_ec_threshold(); + SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]); + SERIAL_ECHOPAIR_F(" axis is ", threshold); + SERIAL_ECHOLNPGM("mm."); + } + + static int8_t idx_from_axis(const AxisEnum axis) { + LOOP_PE(i) + if (encoders[i].get_axis() == axis) return i; + return -1; + } + + static int8_t idx_from_addr(const uint8_t addr) { + LOOP_PE(i) + if (encoders[i].get_address() == addr) return i; + return -1; + } + + static int8_t parse(); + + static void M860(); + static void M861(); + static void M862(); + static void M863(); + static void M864(); + static void M865(); + static void M866(); + static void M867(); + static void M868(); + static void M869(); + + static I2CPositionEncoder encoders[I2CPE_ENCODER_CNT]; + }; + + extern I2CPositionEncodersMgr I2CPEM; + + FORCE_INLINE static void gcode_M860() { I2CPEM.M860(); } + FORCE_INLINE static void gcode_M861() { I2CPEM.M861(); } + FORCE_INLINE static void gcode_M862() { I2CPEM.M862(); } + FORCE_INLINE static void gcode_M863() { I2CPEM.M863(); } + FORCE_INLINE static void gcode_M864() { I2CPEM.M864(); } + FORCE_INLINE static void gcode_M865() { I2CPEM.M865(); } + FORCE_INLINE static void gcode_M866() { I2CPEM.M866(); } + FORCE_INLINE static void gcode_M867() { I2CPEM.M867(); } + FORCE_INLINE static void gcode_M868() { I2CPEM.M868(); } + FORCE_INLINE static void gcode_M869() { I2CPEM.M869(); } + +#endif //I2C_POSITION_ENCODERS +#endif //I2CPOSENC_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/M100_Free_Mem_Chk.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/M100_Free_Mem_Chk.cpp new file mode 100644 index 0000000..6620b0a --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/M100_Free_Mem_Chk.cpp @@ -0,0 +1,333 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * M100 Free Memory Watcher + * + * This code watches the free memory block between the bottom of the heap and the top of the stack. + * This memory block is initialized and watched via the M100 command. + * + * M100 I Initializes the free memory block and prints vitals statistics about the area + * + * M100 F Identifies how much of the free memory block remains free and unused. It also + * detects and reports any corruption within the free memory block that may have + * happened due to errant firmware. + * + * M100 D Does a hex display of the free memory block along with a flag for any errant + * data that does not match the expected value. + * + * M100 C x Corrupts x locations within the free memory block. This is useful to check the + * correctness of the M100 F and M100 D commands. + * + * Also, there are two support functions that can be called from a developer's C code. + * + * uint16_t check_for_free_memory_corruption(const char * const ptr); + * void M100_dump_routine(const char * const title, const char *start, const char *end); + * + * Initial version by Roxy-3D + */ +#define M100_FREE_MEMORY_DUMPER // Enable for the `M110 D` Dump sub-command +#define M100_FREE_MEMORY_CORRUPTOR // Enable for the `M100 C` Corrupt sub-command + +#include "MarlinConfig.h" + +#if ENABLED(M100_FREE_MEMORY_WATCHER) + +#define TEST_BYTE ((char) 0xE5) + +extern char command_queue[BUFSIZE][MAX_CMD_SIZE]; + +extern char* __brkval; +extern size_t __heap_start, __heap_end, __flp; +extern char __bss_end; + +#include "Marlin.h" +#include "gcode.h" +#include "hex_print_routines.h" + +// +// Utility functions +// + +#define END_OF_HEAP() (__brkval ? __brkval : &__bss_end) +int check_for_free_memory_corruption(const char * const title); + +// Location of a variable on its stack frame. Returns a value above +// the stack (once the function returns to the caller). +char* top_of_stack() { + char x; + return &x + 1; // x is pulled on return; +} + +// Count the number of test bytes at the specified location. +int16_t count_test_bytes(const char * const ptr) { + for (uint16_t i = 0; i < 32000; i++) + if (((char) ptr[i]) != TEST_BYTE) + return i - 1; + + return -1; +} + +// +// M100 sub-commands +// + +#if ENABLED(M100_FREE_MEMORY_DUMPER) + /** + * M100 D + * Dump the free memory block from __brkval to the stack pointer. + * malloc() eats memory from the start of the block and the stack grows + * up from the bottom of the block. Solid test bytes indicate nothing has + * used that memory yet. There should not be anything but test bytes within + * the block. If so, it may indicate memory corruption due to a bad pointer. + * Unexpected bytes are flagged in the right column. + */ + void dump_free_memory(const char *ptr, const char *sp) { + // + // Start and end the dump on a nice 16 byte boundary + // (even though the values are not 16-byte aligned). + // + ptr = (char *)((uint16_t)ptr & 0xFFF0); // Align to 16-byte boundary + sp = (char *)((uint16_t)sp | 0x000F); // Align sp to the 15th byte (at or above sp) + + // Dump command main loop + while (ptr < sp) { + print_hex_word((uint16_t)ptr); // Print the address + SERIAL_CHAR(':'); + for (uint8_t i = 0; i < 16; i++) { // and 16 data bytes + if (i == 8) SERIAL_CHAR('-'); + print_hex_byte(ptr[i]); + SERIAL_CHAR(' '); + } + safe_delay(25); + SERIAL_CHAR('|'); // Point out non test bytes + for (uint8_t i = 0; i < 16; i++) { + char ccc = (char)ptr[i]; // cast to char before automatically casting to char on assignment, in case the compiler is broken + if (&ptr[i] >= (const char*)command_queue && &ptr[i] < (const char*)(command_queue + sizeof(command_queue))) { // Print out ASCII in the command buffer area + if (!WITHIN(ccc, ' ', 0x7E)) ccc = ' '; + } + else { // If not in the command buffer area, flag bytes that don't match the test byte + ccc = (ccc == TEST_BYTE) ? ' ' : '?'; + } + SERIAL_CHAR(ccc); + } + SERIAL_EOL(); + ptr += 16; + safe_delay(25); + idle(); + } + } + +void M100_dump_routine(const char * const title, const char *start, const char *end) { + SERIAL_ECHOLN(title); + // + // Round the start and end locations to produce full lines of output + // + start = (char*)((uint16_t) start & 0xFFF0); + end = (char*)((uint16_t) end | 0x000F); + dump_free_memory(start, end); +} + +#endif // M100_FREE_MEMORY_DUMPER + +/** + * M100 F + * Return the number of free bytes in the memory pool, + * with other vital statistics defining the pool. + */ +void free_memory_pool_report(char * const ptr, const int16_t size) { + int16_t max_cnt = -1, block_cnt = 0; + char *max_addr = NULL; + // Find the longest block of test bytes in the buffer + for (int16_t i = 0; i < size; i++) { + char *addr = ptr + i; + if (*addr == TEST_BYTE) { + const int16_t j = count_test_bytes(addr); + if (j > 8) { + SERIAL_ECHOPAIR("Found ", j); + SERIAL_ECHOLNPAIR(" bytes free at ", hex_address(addr)); + if (j > max_cnt) { + max_cnt = j; + max_addr = addr; + } + i += j; + block_cnt++; + } + } + } + if (block_cnt > 1) { + SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area."); + SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt); + SERIAL_ECHOLNPAIR(" bytes at ", hex_address(max_addr)); + } + SERIAL_ECHOLNPAIR("check_for_free_memory_corruption() = ", check_for_free_memory_corruption("M100 F ")); +} + +#if ENABLED(M100_FREE_MEMORY_CORRUPTOR) + /** + * M100 C + * Corrupt locations in the free memory pool and report the corrupt addresses. + * This is useful to check the correctness of the M100 D and the M100 F commands. + */ + void corrupt_free_memory(char *ptr, const uint16_t size) { + ptr += 8; + const uint16_t near_top = top_of_stack() - ptr - 250, // -250 to avoid interrupt activity that's altered the stack. + j = near_top / (size + 1); + + SERIAL_ECHOLNPGM("Corrupting free memory block.\n"); + for (uint16_t i = 1; i <= size; i++) { + char * const addr = ptr + i * j; + *addr = i; + SERIAL_ECHOPAIR("\nCorrupting address: ", hex_address(addr)); + } + SERIAL_EOL(); + } +#endif // M100_FREE_MEMORY_CORRUPTOR + +/** + * M100 I + * Init memory for the M100 tests. (Automatically applied on the first M100.) + */ +void init_free_memory(char *ptr, int16_t size) { + SERIAL_ECHOLNPGM("Initializing free memory block.\n\n"); + + size -= 250; // -250 to avoid interrupt activity that's altered the stack. + if (size < 0) { + SERIAL_ECHOLNPGM("Unable to initialize.\n"); + return; + } + + ptr += 8; // move a few bytes away from the heap just because we don't want + // to be altering memory that close to it. + memset(ptr, TEST_BYTE, size); + + SERIAL_ECHO(size); + SERIAL_ECHOLNPGM(" bytes of memory initialized.\n"); + + for (int16_t i = 0; i < size; i++) { + if (ptr[i] != TEST_BYTE) { + SERIAL_ECHOPAIR("? address : ", hex_address(ptr + i)); + SERIAL_ECHOLNPAIR("=", hex_byte(ptr[i])); + SERIAL_EOL(); + } + } +} + +/** + * M100: Free Memory Check + */ +void gcode_M100() { + SERIAL_ECHOPAIR("\n__brkval : ", hex_address(__brkval)); + SERIAL_ECHOPAIR("\n__bss_end : ", hex_address(&__bss_end)); + + char *ptr = END_OF_HEAP(), *sp = top_of_stack(); + + SERIAL_ECHOPAIR("\nstart of free space : ", hex_address(ptr)); + SERIAL_ECHOLNPAIR("\nStack Pointer : ", hex_address(sp)); + + // Always init on the first invocation of M100 + static bool m100_not_initialized = true; + if (m100_not_initialized || parser.seen('I')) { + m100_not_initialized = false; + init_free_memory(ptr, sp - ptr); + } + + #if ENABLED(M100_FREE_MEMORY_DUMPER) + if (parser.seen('D')) + return dump_free_memory(ptr, sp); + #endif + + if (parser.seen('F')) + return free_memory_pool_report(ptr, sp - ptr); + + #if ENABLED(M100_FREE_MEMORY_CORRUPTOR) + + if (parser.seen('C')) + return corrupt_free_memory(ptr, parser.value_int()); + + #endif +} + +int check_for_free_memory_corruption(const char * const title) { + SERIAL_ECHO(title); + + char *ptr = END_OF_HEAP(), *sp = top_of_stack(); + int n = sp - ptr; + + SERIAL_ECHOPAIR("\nfmc() n=", n); + SERIAL_ECHOPAIR("\n&__brkval: ", hex_address(&__brkval)); + SERIAL_ECHOPAIR("=", hex_address(__brkval)); + SERIAL_ECHOPAIR("\n__bss_end: ", hex_address(&__bss_end)); + SERIAL_ECHOPAIR(" sp=", hex_address(sp)); + + if (sp < ptr) { + SERIAL_ECHOPGM(" sp < Heap "); + // SET_INPUT_PULLUP(63); // if the developer has a switch wired up to their controller board + // safe_delay(5); // this code can be enabled to pause the display as soon as the + // while ( READ(63)) // malfunction is detected. It is currently defaulting to a switch + // idle(); // being on pin-63 which is unassigend and available on most controller + // safe_delay(20); // boards. + // while ( !READ(63)) + // idle(); + safe_delay(20); + #ifdef M100_FREE_MEMORY_DUMPER + M100_dump_routine(" Memory corruption detected with sp 8) { + // SERIAL_ECHOPAIR("Found ", j); + // SERIAL_ECHOLNPAIR(" bytes free at ", hex_address(ptr + i)); + i += j; + block_cnt++; + SERIAL_ECHOPAIR(" (", block_cnt); + SERIAL_ECHOPAIR(") found=", j); + SERIAL_ECHOPGM(" "); + } + } + } + SERIAL_ECHOPAIR(" block_found=", block_cnt); + + if (block_cnt != 1 || __brkval != 0x0000) + SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area."); + + if (block_cnt == 0) // Make sure the special case of no free blocks shows up as an + block_cnt = -1; // error to the calling code! + + SERIAL_ECHOPGM(" return="); + if (block_cnt == 1) { + SERIAL_CHAR('0'); // if the block_cnt is 1, nothing has broken up the free memory + SERIAL_EOL(); // area and it is appropriate to say 'no corruption'. + return 0; + } + SERIAL_ECHOLNPGM("true"); + return block_cnt; +} + +#endif // M100_FREE_MEMORY_WATCHER + + diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Makefile b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Makefile new file mode 100644 index 0000000..150f074 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Makefile @@ -0,0 +1,544 @@ +# Sprinter Arduino Project Makefile +# +# Makefile Based on: +# Arduino 0011 Makefile +# Arduino adaptation by mellis, eighthave, oli.keller +# Marlin adaption by Daid +# +# This has been tested with Arduino 0022. +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment (or Java). +# +# Detailed instructions for using the makefile: +# +# 1. Modify the line containing "ARDUINO_INSTALL_DIR" to point to the directory that +# contains the Arduino installation (for example, under Mac OS X, this +# might be /Applications/Arduino.app/Contents/Resources/Java). +# +# 2. Modify the line containing "UPLOAD_PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. UPLOAD_PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wild card (e.g. UPLOAD_PORT = /dev/tty.usb*). +# +# 3. Set the line containing "MCU" to match your board's processor. +# Older one's are atmega8 based, newer ones like Arduino Mini, Bluetooth +# or Diecimila have the atmega168. If you're using a LilyPad Arduino, +# change F_CPU to 8000000. If you are using Gen7 electronics, you +# probably need to use 20000000. Either way, you must regenerate +# the speed lookup table with create_speed_lookuptable.py. +# +# 4. Type "make" and press enter to compile/verify your program. +# +# 5. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# Note that all settings at the top of this file can be overriden from +# the command line with, for example, "make HARDWARE_MOTHERBOARD=71" +# +# To compile for RAMPS (atmega2560) with Arduino 1.6.9 at root/arduino you would use... +# +# make ARDUINO_VERSION=10609 AVR_TOOLS_PATH=/root/arduino/hardware/tools/avr/bin/ \ +# HARDWARE_MOTHERBOARD=33 ARDUINO_INSTALL_DIR=/root/arduino +# +# To compile and upload simply add "upload" to the end of the line... +# +# make ARDUINO_VERSION=10609 AVR_TOOLS_PATH=/root/arduino/hardware/tools/avr/bin/ \ +# HARDWARE_MOTHERBOARD=33 ARDUINO_INSTALL_DIR=/root/arduino upload +# +# If uploading doesn't work try adding the parameter "AVRDUDE_PROGRAMMER=wiring" or +# start upload manually (using stk500) like so: +# +# avrdude -C /root/arduino/hardware/tools/avr/etc/avrdude.conf -v -p m2560 -c stk500 \ +# -U flash:w:applet/Marlin.hex:i -P /dev/ttyUSB0 +# +# Or, try disconnecting USB to power down and then reconnecting before running avrdude. +# + +# This defines the board to compile for (see boards.h for your board's ID) +HARDWARE_MOTHERBOARD ?= 11 + +# Arduino source install directory, and version number +# On most linuxes this will be /usr/share/arduino +ARDUINO_INSTALL_DIR ?= ${HOME}/Arduino +ARDUINO_VERSION ?= 106 + +# You can optionally set a path to the avr-gcc tools. Requires a trailing slash. (ex: /usr/local/avr-gcc/bin) +AVR_TOOLS_PATH ?= + +#Programmer configuration +UPLOAD_RATE ?= 57600 +AVRDUDE_PROGRAMMER ?= arduino +# on most linuxes this will be /dev/ttyACM0 or /dev/ttyACM1 +UPLOAD_PORT ?= /dev/ttyUSB0 + +#Directory used to build files in, contains all the build files, from object files to the final hex file +#on linux it is best to put an absolute path like /home/username/tmp . +BUILD_DIR ?= applet + +# This defines whether Liquid_TWI2 support will be built +LIQUID_TWI2 ?= 0 + +# this defines if Wire is needed +WIRE ?= 0 + +# this defines if U8GLIB is needed (may require RELOC_WORKAROUND) +U8GLIB ?= 1 + +# this defines whether to add a workaround for the avr-gcc relocation bug +# https://www.stix.id.au/wiki/AVR_relocation_truncations_workaround +RELOC_WORKAROUND ?= 1 + +############################################################################ +# Below here nothing should be changed... + +# Here the Arduino variant is selected by the board type +# HARDWARE_VARIANT = "arduino", "Sanguino", "Gen7", ... +# MCU = "atmega1280", "Mega2560", "atmega2560", "atmega644p", ... + +#Gen7 +ifeq ($(HARDWARE_MOTHERBOARD),10) +HARDWARE_VARIANT ?= Gen7 +MCU ?= atmega644 +F_CPU ?= 20000000 +else ifeq ($(HARDWARE_MOTHERBOARD),11) +HARDWARE_VARIANT ?= Gen7 +MCU ?= atmega644p +F_CPU ?= 20000000 +else ifeq ($(HARDWARE_MOTHERBOARD),12) +HARDWARE_VARIANT ?= Gen7 +MCU ?= atmega644p +F_CPU ?= 20000000 +else ifeq ($(HARDWARE_MOTHERBOARD),13) +HARDWARE_VARIANT ?= Gen7 +MCU ?= atmega1284p +F_CPU ?= 20000000 + +#RAMPS +else ifeq ($(HARDWARE_MOTHERBOARD),3) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),33) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),34) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),35) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),36) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),38) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),43) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),44) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),45) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),46) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),48) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 + +#Gen6 +else ifeq ($(HARDWARE_MOTHERBOARD),5) +HARDWARE_VARIANT ?= Gen6 +MCU ?= atmega644p +else ifeq ($(HARDWARE_MOTHERBOARD),51) +HARDWARE_VARIANT ?= Gen6 +MCU ?= atmega644p + +#Sanguinololu +else ifeq ($(HARDWARE_MOTHERBOARD),6) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p +else ifeq ($(HARDWARE_MOTHERBOARD),62) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p +else ifeq ($(HARDWARE_MOTHERBOARD),63) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p +else ifeq ($(HARDWARE_MOTHERBOARD),65) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega1284p +else ifeq ($(HARDWARE_MOTHERBOARD),66) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega1284p +else ifeq ($(HARDWARE_MOTHERBOARD),69) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega1284p + +#Ultimaker +else ifeq ($(HARDWARE_MOTHERBOARD),7) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),71) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega1280 + +#Teensylu +else ifeq ($(HARDWARE_MOTHERBOARD),8) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb1286 +else ifeq ($(HARDWARE_MOTHERBOARD),81) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb1286 +else ifeq ($(HARDWARE_MOTHERBOARD),811) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb1286 +else ifeq ($(HARDWARE_MOTHERBOARD),82) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb646 +else ifeq ($(HARDWARE_MOTHERBOARD),83) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb1286 +else ifeq ($(HARDWARE_MOTHERBOARD),84) +HARDWARE_VARIANT ?= Teensy +MCU ?= at90usb1286 + +#Gen3+ +else ifeq ($(HARDWARE_MOTHERBOARD),9) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p + +#Gen3 Monolithic Electronics +else ifeq ($(HARDWARE_MOTHERBOARD),22) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p + +#Megatronics +else ifeq ($(HARDWARE_MOTHERBOARD),70) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 + +#Alpha OMCA board +else ifeq ($(HARDWARE_MOTHERBOARD),90) +HARDWARE_VARIANT ?= SanguinoA +MCU ?= atmega644 + +#Final OMCA board +else ifeq ($(HARDWARE_MOTHERBOARD),91) +HARDWARE_VARIANT ?= Sanguino +MCU ?= atmega644p + +#Rambo +else ifeq ($(HARDWARE_MOTHERBOARD),301) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 + +# Azteeg +else ifeq ($(HARDWARE_MOTHERBOARD),67) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 +else ifeq ($(HARDWARE_MOTHERBOARD),68) +HARDWARE_VARIANT ?= arduino +MCU ?= atmega2560 + +endif + +# Be sure to regenerate speed_lookuptable.h with create_speed_lookuptable.py +# if you are setting this to something other than 16MHz +# Set to 16Mhz if not yet set. +F_CPU ?= 16000000 + +# Arduino contained the main source code for the Arduino +# Libraries, the "hardware variant" are for boards +# that derives from that, and their source are present in +# the main Marlin source directory + +TARGET = $(notdir $(CURDIR)) + +# VPATH tells make to look into these directory for source files, +# there is no need to specify explicit pathnames as long as the +# directory is added here + +VPATH = . +VPATH += $(BUILD_DIR) +VPATH += $(ARDUINO_INSTALL_DIR)/hardware/arduino/avr/cores/arduino + +VPATH += $(ARDUINO_INSTALL_DIR)/hardware/arduino/avr/libraries/SPI +VPATH += $(ARDUINO_INSTALL_DIR)/hardware/arduino/avr/libraries/SPI/src +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/LiquidCrystal/src +ifeq ($(LIQUID_TWI2), 1) +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/Wire +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/Wire/utility +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/LiquidTWI2 +endif +ifeq ($(WIRE), 1) +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/Wire +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/Wire/utility +endif +ifeq ($(NEOPIXEL), 1) +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/Adafruit_NeoPixel +endif +ifeq ($(U8GLIB), 1) +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/U8glib +VPATH += $(ARDUINO_INSTALL_DIR)/libraries/U8glib/utility +endif + +ifeq ($(HARDWARE_VARIANT), arduino) +HARDWARE_SUB_VARIANT ?= mega +VPATH += $(ARDUINO_INSTALL_DIR)/hardware/arduino/avr/variants/$(HARDWARE_SUB_VARIANT) +else +ifeq ($(HARDWARE_VARIANT), Sanguino) +VPATH += $(HARDWARE_DIR)/marlin/avr/variants/sanguino +else +HARDWARE_SUB_VARIANT ?= standard +VPATH += $(HARDWARE_DIR)/$(HARDWARE_VARIANT)/variants/$(HARDWARE_SUB_VARIANT) +endif +endif +SRC = wiring.c \ + wiring_analog.c wiring_digital.c \ + wiring_pulse.c \ + wiring_shift.c WInterrupts.c hooks.c +ifeq ($(HARDWARE_VARIANT), Teensy) +SRC = wiring.c +VPATH += $(ARDUINO_INSTALL_DIR)/hardware/teensy/cores/teensy +endif +CXXSRC = WMath.cpp WString.cpp Print.cpp Marlin_main.cpp \ + MarlinSerial.cpp Sd2Card.cpp SdBaseFile.cpp SdFatUtil.cpp \ + SdFile.cpp SdVolume.cpp planner.cpp stepper.cpp \ + temperature.cpp cardreader.cpp configuration_store.cpp \ + watchdog.cpp SPI.cpp servo.cpp Tone.cpp ultralcd.cpp digipot_mcp4451.cpp \ + dac_mcp4728.cpp vector_3.cpp least_squares_fit.cpp endstops.cpp stopwatch.cpp utility.cpp \ + printcounter.cpp nozzle.cpp serial.cpp gcode.cpp Max7219_Debug_LEDs.cpp +ifeq ($(NEOPIXEL), 1) +CXXSRC += Adafruit_NeoPixel.cpp +endif +ifeq ($(LIQUID_TWI2), 0) +CXXSRC += LiquidCrystal.cpp +else +SRC += twi.c +CXXSRC += Wire.cpp LiquidTWI2.cpp +endif + +ifeq ($(WIRE), 1) +SRC += twi.c +CXXSRC += Wire.cpp +endif + +ifeq ($(U8GLIB), 1) +SRC += u8g_ll_api.c u8g_bitmap.c u8g_clip.c u8g_com_null.c u8g_delay.c u8g_page.c u8g_pb.c u8g_pb16h1.c u8g_rect.c u8g_state.c u8g_font.c u8g_font_data.c +endif + +ifeq ($(RELOC_WORKAROUND), 1) +LD_PREFIX=-nodefaultlibs +LD_SUFFIX=-lm -lgcc -lc -lgcc +endif + +#Check for Arduino 1.0.0 or higher and use the correct source files for that version +ifeq ($(shell [ $(ARDUINO_VERSION) -ge 100 ] && echo true), true) +CXXSRC += main.cpp +else +SRC += pins_arduino.c main.c +endif + +FORMAT = ihex + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +DEFINES ?= + +# Program settings +CC = $(AVR_TOOLS_PATH)avr-gcc +CXX = $(AVR_TOOLS_PATH)avr-g++ +OBJCOPY = $(AVR_TOOLS_PATH)avr-objcopy +OBJDUMP = $(AVR_TOOLS_PATH)avr-objdump +AR = $(AVR_TOOLS_PATH)avr-ar +SIZE = $(AVR_TOOLS_PATH)avr-size +NM = $(AVR_TOOLS_PATH)avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) ${addprefix -D , $(DEFINES)} +CXXDEFS = $(CDEFS) + +ifeq ($(HARDWARE_VARIANT), Teensy) +CDEFS += -DUSB_SERIAL +SRC += usb.c pins_teensy.c +CXXSRC += usb_api.cpp +endif + +# Add all the source directories as include directories too +CINCS = ${addprefix -I ,${VPATH}} +CXXINCS = ${addprefix -I ,${VPATH}} + +# Compiler flag to set the C/CPP Standard level. +CSTANDARD = -std=gnu99 +CXXSTANDARD = -std=gnu++11 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct \ + -fshort-enums -w -ffunction-sections -fdata-sections \ + -flto \ + -DARDUINO=$(ARDUINO_VERSION) +ifneq ($(HARDWARE_MOTHERBOARD),) +CTUNING += -DMOTHERBOARD=${HARDWARE_MOTHERBOARD} +endif +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) +CEXTRA = -fno-use-cxa-atexit -fno-threadsafe-statics + +CFLAGS := $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CEXTRA) $(CTUNING) $(CSTANDARD) +CXXFLAGS := $(CDEFS) $(CINCS) -O$(OPT) -Wall $(CEXTRA) $(CTUNING) $(CXXSTANDARD) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = -lm + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PORT = $(UPLOAD_PORT) +AVRDUDE_WRITE_FLASH = -Uflash:w:$(BUILD_DIR)/$(TARGET).hex:i +ifeq ($(shell uname -s), Linux) +AVRDUDE_CONF = /etc/avrdude/avrdude.conf +else +AVRDUDE_CONF = $(ARDUINO_INSTALL_DIR)/hardware/tools/avr/etc/avrdude.conf +endif +AVRDUDE_FLAGS = -D -C$(AVRDUDE_CONF) \ + -p$(MCU) -P$(AVRDUDE_PORT) -c$(AVRDUDE_PROGRAMMER) \ + -b$(UPLOAD_RATE) + +# Define all object files. +OBJ = ${patsubst %.c, $(BUILD_DIR)/%.o, ${SRC}} +OBJ += ${patsubst %.cpp, $(BUILD_DIR)/%.o, ${CXXSRC}} +OBJ += ${patsubst %.S, $(BUILD_DIR)/%.o, ${ASRC}} + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) + +# set V=1 (eg, "make V=1") to print the full commands etc. +ifneq ($V,1) + Pecho=@echo + P=@ +else + Pecho=@: + P= +endif + +# Default target. +all: sizeafter + +build: $(BUILD_DIR) elf hex + +# Creates the object directory +$(BUILD_DIR): + $P mkdir -p $(BUILD_DIR) + +elf: $(BUILD_DIR)/$(TARGET).elf +hex: $(BUILD_DIR)/$(TARGET).hex +eep: $(BUILD_DIR)/$(TARGET).eep +lss: $(BUILD_DIR)/$(TARGET).lss +sym: $(BUILD_DIR)/$(TARGET).sym + +# Program the device. +# Do not try to reset an Arduino if it's not one +upload: $(BUILD_DIR)/$(TARGET).hex +ifeq (${AVRDUDE_PROGRAMMER}, arduino) + stty hup < $(UPLOAD_PORT); true +endif + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) +ifeq (${AVRDUDE_PROGRAMMER}, arduino) + stty -hup < $(UPLOAD_PORT); true +endif + + # Display size of file. +HEXSIZE = $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex +ELFSIZE = $(SIZE) --mcu=$(MCU) -C $(BUILD_DIR)/$(TARGET).elf; \ + $(SIZE) $(BUILD_DIR)/$(TARGET).elf +sizebefore: + $P if [ -f $(BUILD_DIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi + +sizeafter: build + $P if [ -f $(BUILD_DIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ + --change-section-address .data-0x800000 \ + --change-section-address .bss-0x800000 \ + --change-section-address .noinit-0x800000 \ + --change-section-address .eeprom-0x810000 + + +coff: $(BUILD_DIR)/$(TARGET).elf + $(COFFCONVERT) -O coff-avr $(BUILD_DIR)/$(TARGET).elf $(TARGET).cof + + +extcoff: $(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr $(BUILD_DIR)/$(TARGET).elf $(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym +.PRECIOUS: .o + +.elf.hex: + $(Pecho) " COPY $@" + $P $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + # Link: create ELF output file from library. +$(BUILD_DIR)/$(TARGET).elf: $(OBJ) Configuration.h + $(Pecho) " CXX $@" + $P $(CC) $(LD_PREFIX) $(ALL_CXXFLAGS) -Wl,--gc-sections,--relax -o $@ -L. $(OBJ) $(LDFLAGS) $(LD_SUFFIX) + +$(BUILD_DIR)/%.o: %.c Configuration.h Configuration_adv.h $(MAKEFILE) + $(Pecho) " CC $<" + $P $(CC) -MMD -c $(ALL_CFLAGS) $< -o $@ + +$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.cpp Configuration.h Configuration_adv.h $(MAKEFILE) + $(Pecho) " CXX $<" + $P $(CXX) -MMD -c $(ALL_CXXFLAGS) $< -o $@ + +$(BUILD_DIR)/%.o: %.cpp Configuration.h Configuration_adv.h $(MAKEFILE) + $(Pecho) " CXX $<" + $P $(CXX) -MMD -c $(ALL_CXXFLAGS) $< -o $@ + + +# Target: clean project. +clean: + $(Pecho) " RM $(BUILD_DIR)/*" + $P $(REMOVE) $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET).cof $(BUILD_DIR)/$(TARGET).elf \ + $(BUILD_DIR)/$(TARGET).map $(BUILD_DIR)/$(TARGET).sym $(BUILD_DIR)/$(TARGET).lss $(BUILD_DIR)/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + $(Pecho) " RMDIR $(BUILD_DIR)/" + $P rm -rf $(BUILD_DIR) + + +.PHONY: all build elf hex eep lss sym program coff extcoff clean depend sizebefore sizeafter + +# Automaticaly include the dependency files created by gcc +-include ${wildcard $(BUILD_DIR)/*.d} diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.h new file mode 100644 index 0000000..bcbf318 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.h @@ -0,0 +1,498 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#ifndef MARLIN_H +#define MARLIN_H + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "MarlinConfig.h" + +#ifdef DEBUG_GCODE_PARSER + #include "gcode.h" +#endif + +#include "enum.h" +#include "types.h" +#include "fastio.h" +#include "utility.h" +#include "serial.h" + +#if ENABLED(PRINTCOUNTER) + #include "printcounter.h" +#else + #include "stopwatch.h" +#endif + +void idle( + #if ENABLED(ADVANCED_PAUSE_FEATURE) + bool no_stepper_sleep = false // pass true to keep steppers from disabling on timeout + #endif +); + +void manage_inactivity(bool ignore_stepper_queue = false); + +#if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + extern bool extruder_duplication_enabled; +#endif + +#if HAS_X2_ENABLE + #define enable_X() do{ X_ENABLE_WRITE( X_ENABLE_ON); X2_ENABLE_WRITE( X_ENABLE_ON); }while(0) + #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); X2_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0) +#elif HAS_X_ENABLE + #define enable_X() X_ENABLE_WRITE( X_ENABLE_ON) + #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0) +#else + #define enable_X() NOOP + #define disable_X() NOOP +#endif + +#if HAS_Y2_ENABLE + #define enable_Y() do{ Y_ENABLE_WRITE( Y_ENABLE_ON); Y2_ENABLE_WRITE(Y_ENABLE_ON); }while(0) + #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); Y2_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0) +#elif HAS_Y_ENABLE + #define enable_Y() Y_ENABLE_WRITE( Y_ENABLE_ON) + #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0) +#else + #define enable_Y() NOOP + #define disable_Y() NOOP +#endif + +#if HAS_Z2_ENABLE + #define enable_Z() do{ Z_ENABLE_WRITE( Z_ENABLE_ON); Z2_ENABLE_WRITE(Z_ENABLE_ON); }while(0) + #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); Z2_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0) +#elif HAS_Z_ENABLE + #define enable_Z() Z_ENABLE_WRITE( Z_ENABLE_ON) + #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0) +#else + #define enable_Z() NOOP + #define disable_Z() NOOP +#endif + +#if ENABLED(MIXING_EXTRUDER) + + /** + * Mixing steppers synchronize their enable (and direction) together + */ + #if MIXING_STEPPERS > 3 + #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); E3_ENABLE_WRITE( E_ENABLE_ON); } + #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); E3_ENABLE_WRITE(!E_ENABLE_ON); } + #elif MIXING_STEPPERS > 2 + #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); } + #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); } + #else + #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); } + #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); } + #endif + #define enable_E1() NOOP + #define disable_E1() NOOP + #define enable_E2() NOOP + #define disable_E2() NOOP + #define enable_E3() NOOP + #define disable_E3() NOOP + #define enable_E4() NOOP + #define disable_E4() NOOP + +#else // !MIXING_EXTRUDER + + #if HAS_E0_ENABLE + #define enable_E0() E0_ENABLE_WRITE( E_ENABLE_ON) + #define disable_E0() E0_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define enable_E0() NOOP + #define disable_E0() NOOP + #endif + + #if E_STEPPERS > 1 && HAS_E1_ENABLE + #define enable_E1() E1_ENABLE_WRITE( E_ENABLE_ON) + #define disable_E1() E1_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define enable_E1() NOOP + #define disable_E1() NOOP + #endif + + #if E_STEPPERS > 2 && HAS_E2_ENABLE + #define enable_E2() E2_ENABLE_WRITE( E_ENABLE_ON) + #define disable_E2() E2_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define enable_E2() NOOP + #define disable_E2() NOOP + #endif + + #if E_STEPPERS > 3 && HAS_E3_ENABLE + #define enable_E3() E3_ENABLE_WRITE( E_ENABLE_ON) + #define disable_E3() E3_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define enable_E3() NOOP + #define disable_E3() NOOP + #endif + + #if E_STEPPERS > 4 && HAS_E4_ENABLE + #define enable_E4() E4_ENABLE_WRITE( E_ENABLE_ON) + #define disable_E4() E4_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define enable_E4() NOOP + #define disable_E4() NOOP + #endif + +#endif // !MIXING_EXTRUDER + +#if ENABLED(G38_PROBE_TARGET) + extern bool G38_move, // flag to tell the interrupt handler that a G38 command is being run + G38_endstop_hit; // flag from the interrupt handler to indicate if the endstop went active +#endif + +/** + * The axis order in all axis related arrays is X, Y, Z, E + */ +#define _AXIS(AXIS) AXIS ##_AXIS + +void enable_all_steppers(); +void disable_e_steppers(); +void disable_all_steppers(); + +void FlushSerialRequestResend(); +void ok_to_send(); + +void kill(const char*); + +void quickstop_stepper(); + +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + void handle_filament_runout(); +#endif + +extern uint8_t marlin_debug_flags; +#define DEBUGGING(F) (marlin_debug_flags & (DEBUG_## F)) + +extern bool Running; +inline bool IsRunning() { return Running; } +inline bool IsStopped() { return !Running; } + +bool enqueue_and_echo_command(const char* cmd, bool say_ok=false); // Add a single command to the end of the buffer. Return false on failure. +void enqueue_and_echo_commands_P(const char * const cmd); // Set one or more commands to be prioritized over the next Serial/SD command. +void clear_command_queue(); + +extern millis_t previous_cmd_ms; +inline void refresh_cmd_timeout() { previous_cmd_ms = millis(); } + +#if ENABLED(FAST_PWM_FAN) + void setPwmFrequency(uint8_t pin, int val); +#endif + +/** + * Feedrate scaling and conversion + */ +extern int16_t feedrate_percentage; + +#define MMS_SCALED(MM_S) ((MM_S)*feedrate_percentage*0.01) + +extern bool axis_relative_modes[]; +extern bool axis_known_position[XYZ]; +extern bool axis_homed[XYZ]; +extern volatile bool wait_for_heatup; + +#if HAS_RESUME_CONTINUE + extern volatile bool wait_for_user; +#endif + +extern float current_position[NUM_AXIS]; + +// Workspace offsets +#if HAS_WORKSPACE_OFFSET + #if HAS_HOME_OFFSET + extern float home_offset[XYZ]; + #endif + #if HAS_POSITION_SHIFT + extern float position_shift[XYZ]; + #endif +#endif + +#if HAS_HOME_OFFSET && HAS_POSITION_SHIFT + extern float workspace_offset[XYZ]; + #define WORKSPACE_OFFSET(AXIS) workspace_offset[AXIS] +#elif HAS_HOME_OFFSET + #define WORKSPACE_OFFSET(AXIS) home_offset[AXIS] +#elif HAS_POSITION_SHIFT + #define WORKSPACE_OFFSET(AXIS) position_shift[AXIS] +#else + #define WORKSPACE_OFFSET(AXIS) 0 +#endif + +#define NATIVE_TO_LOGICAL(POS, AXIS) ((POS) + WORKSPACE_OFFSET(AXIS)) +#define LOGICAL_TO_NATIVE(POS, AXIS) ((POS) - WORKSPACE_OFFSET(AXIS)) + +#if HAS_POSITION_SHIFT || DISABLED(DELTA) + #define LOGICAL_X_POSITION(POS) NATIVE_TO_LOGICAL(POS, X_AXIS) + #define LOGICAL_Y_POSITION(POS) NATIVE_TO_LOGICAL(POS, Y_AXIS) + #define RAW_X_POSITION(POS) LOGICAL_TO_NATIVE(POS, X_AXIS) + #define RAW_Y_POSITION(POS) LOGICAL_TO_NATIVE(POS, Y_AXIS) +#else + #define LOGICAL_X_POSITION(POS) (POS) + #define LOGICAL_Y_POSITION(POS) (POS) + #define RAW_X_POSITION(POS) (POS) + #define RAW_Y_POSITION(POS) (POS) +#endif + +#define LOGICAL_Z_POSITION(POS) NATIVE_TO_LOGICAL(POS, Z_AXIS) +#define RAW_Z_POSITION(POS) LOGICAL_TO_NATIVE(POS, Z_AXIS) + +// Hotend Offsets +#if HOTENDS > 1 + extern float hotend_offset[XYZ][HOTENDS]; +#endif + +// Software Endstops +extern float soft_endstop_min[XYZ], soft_endstop_max[XYZ]; + +#if HAS_SOFTWARE_ENDSTOPS + extern bool soft_endstops_enabled; + void clamp_to_software_endstops(float target[XYZ]); +#else + #define soft_endstops_enabled false + #define clamp_to_software_endstops(x) NOOP +#endif + +#if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) + void update_software_endstops(const AxisEnum axis); +#endif + +#if ENABLED(CNC_COORDINATE_SYSTEMS) + #define MAX_COORDINATE_SYSTEMS 9 + extern float coordinate_system[MAX_COORDINATE_SYSTEMS][XYZ]; + bool select_coordinate_system(const int8_t _new); +#endif + +void report_current_position(); + +#if IS_KINEMATIC + extern float delta[ABC]; + void inverse_kinematics(const float raw[XYZ]); +#endif + +#if ENABLED(DELTA) + extern float delta_height, + delta_endstop_adj[ABC], + delta_radius, + delta_diagonal_rod, + delta_calibration_radius, + delta_segments_per_second, + delta_tower_angle_trim[ABC], + delta_clip_start_height; + void recalc_delta_settings(); +#elif IS_SCARA + void forward_kinematics_SCARA(const float &a, const float &b); +#endif + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) + extern int bilinear_grid_spacing[2], bilinear_start[2]; + extern float bilinear_grid_factor[2], + z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y]; + float bilinear_z_offset(const float raw[XYZ]); +#endif + +#if ENABLED(AUTO_BED_LEVELING_UBL) + typedef struct { double A, B, D; } linear_fit; + linear_fit* lsf_linear_fit(double x[], double y[], double z[], const int); +#endif + +#if HAS_LEVELING + bool leveling_is_valid(); + void set_bed_leveling_enabled(const bool enable=true); + void reset_bed_level(); +#endif + +#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + void set_z_fade_height(const float zfh); +#endif + +#if ENABLED(X_DUAL_ENDSTOPS) + extern float x_endstop_adj; +#endif +#if ENABLED(Y_DUAL_ENDSTOPS) + extern float y_endstop_adj; +#endif +#if ENABLED(Z_DUAL_ENDSTOPS) + extern float z_endstop_adj; +#endif + +#if HAS_BED_PROBE + extern float zprobe_zoffset; + void refresh_zprobe_zoffset(const bool no_babystep=false); + #define DEPLOY_PROBE() set_probe_deployed(true) + #define STOW_PROBE() set_probe_deployed(false) +#else + #define DEPLOY_PROBE() + #define STOW_PROBE() +#endif + +#if ENABLED(HOST_KEEPALIVE_FEATURE) + extern MarlinBusyState busy_state; + #define KEEPALIVE_STATE(n) do{ busy_state = n; }while(0) +#else + #define KEEPALIVE_STATE(n) NOOP +#endif + +#if FAN_COUNT > 0 + extern int16_t fanSpeeds[FAN_COUNT]; + #if ENABLED(EXTRA_FAN_SPEED) + extern int16_t old_fanSpeeds[FAN_COUNT], + new_fanSpeeds[FAN_COUNT]; + #endif + #if ENABLED(PROBING_FANS_OFF) + extern bool fans_paused; + extern int16_t paused_fanSpeeds[FAN_COUNT]; + #endif +#endif + +#if ENABLED(BARICUDA) + extern uint8_t baricuda_valve_pressure, baricuda_e_to_p_pressure; +#endif + +#if ENABLED(FILAMENT_WIDTH_SENSOR) + extern bool filament_sensor; // Flag that filament sensor readings should control extrusion + extern float filament_width_nominal, // Theoretical filament diameter i.e., 3.00 or 1.75 + filament_width_meas; // Measured filament diameter + extern uint8_t meas_delay_cm, // Delay distance + measurement_delay[]; // Ring buffer to delay measurement + extern int8_t filwidth_delay_index[2]; // Ring buffer indexes. Used by planner, temperature, and main code +#endif + +#if ENABLED(ADVANCED_PAUSE_FEATURE) + extern AdvancedPauseMenuResponse advanced_pause_menu_response; +#endif + +#if ENABLED(PID_EXTRUSION_SCALING) + extern int lpq_len; +#endif + +#if ENABLED(FWRETRACT) + extern bool autoretract_enabled; // M209 S - Autoretract switch + extern float retract_length, // M207 S - G10 Retract length + retract_feedrate_mm_s, // M207 F - G10 Retract feedrate + retract_zlift, // M207 Z - G10 Retract hop size + retract_recover_length, // M208 S - G11 Recover length + retract_recover_feedrate_mm_s, // M208 F - G11 Recover feedrate + swap_retract_length, // M207 W - G10 Swap Retract length + swap_retract_recover_length, // M208 W - G11 Swap Recover length + swap_retract_recover_feedrate_mm_s; // M208 R - G11 Swap Recover feedrate +#endif + +// Print job timer +#if ENABLED(PRINTCOUNTER) + extern PrintCounter print_job_timer; +#else + extern Stopwatch print_job_timer; +#endif + +// Handling multiple extruders pins +extern uint8_t active_extruder; + +#if HAS_TEMP_HOTEND || HAS_TEMP_BED + void print_heaterstates(); +#endif + +#if ENABLED(MIXING_EXTRUDER) + extern float mixing_factor[MIXING_STEPPERS]; +#endif + +/** + * Blocking movement and shorthand functions + */ +void do_blocking_move_to(const float &x, const float &y, const float &z, const float &fr_mm_s=0.0); +void do_blocking_move_to_x(const float &x, const float &fr_mm_s=0.0); +void do_blocking_move_to_z(const float &z, const float &fr_mm_s=0.0); +void do_blocking_move_to_xy(const float &x, const float &y, const float &fr_mm_s=0.0); + +#define HAS_AXIS_UNHOMED_ERR ( \ + ENABLED(Z_PROBE_ALLEN_KEY) \ + || ENABLED(Z_PROBE_SLED) \ + || HAS_PROBING_PROCEDURE \ + || HOTENDS > 1 \ + || ENABLED(NOZZLE_CLEAN_FEATURE) \ + || ENABLED(NOZZLE_PARK_FEATURE) \ + || (ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(HOME_BEFORE_FILAMENT_CHANGE)) \ + ) || ENABLED(NO_MOTION_BEFORE_HOMING) + +#if HAS_AXIS_UNHOMED_ERR + bool axis_unhomed_error(const bool x=true, const bool y=true, const bool z=true); +#endif + +/** + * position_is_reachable family of functions + */ + +#if IS_KINEMATIC // (DELTA or SCARA) + + #if IS_SCARA + extern const float L1, L2; + #endif + + inline bool position_is_reachable(const float &rx, const float &ry) { + #if ENABLED(DELTA) + return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS); + #elif IS_SCARA + #if MIDDLE_DEAD_ZONE_R > 0 + const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y); + return R2 >= sq(float(MIDDLE_DEAD_ZONE_R)) && R2 <= sq(L1 + L2); + #else + return HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y) <= sq(L1 + L2); + #endif + #else // CARTESIAN + // To be migrated from MakerArm branch in future + #endif + } + + inline bool position_is_reachable_by_probe(const float &rx, const float &ry) { + + // Both the nozzle and the probe must be able to reach the point. + // This won't work on SCARA since the probe offset rotates with the arm. + + return position_is_reachable(rx, ry) + && position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER)); + } + +#else // CARTESIAN + + inline bool position_is_reachable(const float &rx, const float &ry) { + // Add 0.001 margin to deal with float imprecision + return WITHIN(rx, X_MIN_POS - 0.001, X_MAX_POS + 0.001) + && WITHIN(ry, Y_MIN_POS - 0.001, Y_MAX_POS + 0.001); + } + + inline bool position_is_reachable_by_probe(const float &rx, const float &ry) { + // Add 0.001 margin to deal with float imprecision + return WITHIN(rx, MIN_PROBE_X - 0.001, MAX_PROBE_X + 0.001) + && WITHIN(ry, MIN_PROBE_Y - 0.001, MAX_PROBE_Y + 0.001); + } + +#endif // CARTESIAN + +#endif // MARLIN_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.ino b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.ino new file mode 100644 index 0000000..49b6fd1 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin.ino @@ -0,0 +1,72 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * About Marlin + * + * This firmware is a mashup between Sprinter and grbl. + * - https://github.com/kliment/Sprinter + * - https://github.com/simen/grbl/tree + */ + +#include "MarlinConfig.h" + +#if ENABLED(ULTRA_LCD) + #if ENABLED(LCD_I2C_TYPE_PCF8575) + #include + #include + #elif ENABLED(LCD_I2C_TYPE_MCP23017) || ENABLED(LCD_I2C_TYPE_MCP23008) + #include + #include + #elif ENABLED(LCM1602) + #include + #include + #include + #elif ENABLED(DOGLCD) + #include // library for graphics LCD by Oli Kraus (https://github.com/olikraus/U8glib_Arduino) + #else + #include // library for character LCD + #endif +#endif + +#if HAS_DIGIPOTSS + #include +#endif + +#if ENABLED(DIGIPOT_I2C) + #include +#endif + +#if ENABLED(HAVE_TMCDRIVER) + #include + #include +#endif + +#if ENABLED(HAVE_TMC2130) + #include + #include +#endif + +#if ENABLED(HAVE_L6470DRIVER) + #include + #include +#endif diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinConfig.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinConfig.h new file mode 100644 index 0000000..64e0bac --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinConfig.h @@ -0,0 +1,41 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef MARLIN_CONFIG_H +#define MARLIN_CONFIG_H + +#include "fastio.h" +#include "macros.h" +#include "boards.h" +#include "Version.h" +#include "Configuration.h" +#include "Conditionals_LCD.h" +#include "Configuration_adv.h" +#include "pins.h" +#ifndef USBCON + #define HardwareSerial_h // trick to disable the standard HWserial +#endif +#include "Arduino.h" +#include "Conditionals_post.h" +#include "SanityCheck.h" + +#endif // MARLIN_CONFIG_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.cpp new file mode 100644 index 0000000..896db69 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.cpp @@ -0,0 +1,654 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * MarlinSerial.cpp - Hardware serial library for Wiring + * Copyright (c) 2006 Nicholas Zambetti. All right reserved. + * + * Modified 23 November 2006 by David A. Mellis + * Modified 28 September 2010 by Mark Sproul + * Modified 14 February 2016 by Andreas Hardtung (added tx buffer) + * Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF) + */ + +// Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.) + +#include "MarlinConfig.h" + +#if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)) + + #include "MarlinSerial.h" + #include "Marlin.h" + + struct ring_buffer_r { + unsigned char buffer[RX_BUFFER_SIZE]; + volatile ring_buffer_pos_t head, tail; + }; + + #if TX_BUFFER_SIZE > 0 + struct ring_buffer_t { + unsigned char buffer[TX_BUFFER_SIZE]; + volatile uint8_t head, tail; + }; + #endif + + #if UART_PRESENT(SERIAL_PORT) + ring_buffer_r rx_buffer = { { 0 }, 0, 0 }; + #if TX_BUFFER_SIZE > 0 + ring_buffer_t tx_buffer = { { 0 }, 0, 0 }; + static bool _written; + #endif + #endif + + #if ENABLED(SERIAL_XON_XOFF) + constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80; // XON / XOFF Character was sent + constexpr uint8_t XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send + // XON / XOFF character definitions + constexpr uint8_t XON_CHAR = 17; + constexpr uint8_t XOFF_CHAR = 19; + uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR; + #endif + + #if ENABLED(SERIAL_STATS_DROPPED_RX) + uint8_t rx_dropped_bytes = 0; + #endif + + #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) + ring_buffer_pos_t rx_max_enqueued = 0; + #endif + + #if ENABLED(EMERGENCY_PARSER) + + #include "stepper.h" + #include "language.h" + + // Currently looking for: M108, M112, M410 + // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h + + FORCE_INLINE void emergency_parser(const unsigned char c) { + + static e_parser_state state = state_RESET; + + switch (state) { + case state_RESET: + switch (c) { + case ' ': break; + case 'N': state = state_N; break; + case 'M': state = state_M; break; + default: state = state_IGNORE; + } + break; + + case state_N: + switch (c) { + case '0': case '1': case '2': + case '3': case '4': case '5': + case '6': case '7': case '8': + case '9': case '-': case ' ': break; + case 'M': state = state_M; break; + default: state = state_IGNORE; + } + break; + + case state_M: + switch (c) { + case ' ': break; + case '1': state = state_M1; break; + case '4': state = state_M4; break; + default: state = state_IGNORE; + } + break; + + case state_M1: + switch (c) { + case '0': state = state_M10; break; + case '1': state = state_M11; break; + default: state = state_IGNORE; + } + break; + + case state_M10: + state = (c == '8') ? state_M108 : state_IGNORE; + break; + + case state_M11: + state = (c == '2') ? state_M112 : state_IGNORE; + break; + + case state_M4: + state = (c == '1') ? state_M41 : state_IGNORE; + break; + + case state_M41: + state = (c == '0') ? state_M410 : state_IGNORE; + break; + + case state_IGNORE: + if (c == '\n') state = state_RESET; + break; + + default: + if (c == '\n') { + switch (state) { + case state_M108: + wait_for_user = wait_for_heatup = false; + break; + case state_M112: + kill(PSTR(MSG_KILLED)); + break; + case state_M410: + quickstop_stepper(); + break; + default: + break; + } + state = state_RESET; + } + } + } + + #endif // EMERGENCY_PARSER + + FORCE_INLINE void store_rxd_char() { + const ring_buffer_pos_t h = rx_buffer.head, + i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); + + // If the character is to be stored at the index just before the tail + // (such that the head would advance to the current tail), the buffer is + // critical, so don't write the character or advance the head. + const char c = M_UDRx; + if (i != rx_buffer.tail) { + rx_buffer.buffer[h] = c; + rx_buffer.head = i; + } + else { + #if ENABLED(SERIAL_STATS_DROPPED_RX) + if (!++rx_dropped_bytes) ++rx_dropped_bytes; + #endif + } + + #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) + // calculate count of bytes stored into the RX buffer + ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); + // Keep track of the maximum count of enqueued bytes + NOLESS(rx_max_enqueued, rx_count); + #endif + + #if ENABLED(SERIAL_XON_XOFF) + + // for high speed transfers, we can use XON/XOFF protocol to do + // software handshake and avoid overruns. + if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) { + + // calculate count of bytes stored into the RX buffer + ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); + + // if we are above 12.5% of RX buffer capacity, send XOFF before + // we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to + // let the host react and stop sending bytes. This translates to 13mS + // propagation time. + if (rx_count >= (RX_BUFFER_SIZE) / 8) { + // If TX interrupts are disabled and data register is empty, + // just write the byte to the data register and be done. This + // shortcut helps significantly improve the effective datarate + // at high (>500kbit/s) bitrates, where interrupt overhead + // becomes a slowdown. + if (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) { + // Send an XOFF character + M_UDRx = XOFF_CHAR; + // clear the TXC bit -- "can be cleared by writing a one to its bit + // location". This makes sure flush() won't return until the bytes + // actually got written + SBI(M_UCSRxA, M_TXCx); + // And remember it was sent + xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT; + } + else { + // TX interrupts disabled, but buffer still not empty ... or + // TX interrupts enabled. Reenable TX ints and schedule XOFF + // character to be sent + #if TX_BUFFER_SIZE > 0 + SBI(M_UCSRxB, M_UDRIEx); + xon_xoff_state = XOFF_CHAR; + #else + // We are not using TX interrupts, we will have to send this manually + while (!TEST(M_UCSRxA, M_UDREx)) {/* nada */} + M_UDRx = XOFF_CHAR; + // And remember we already sent it + xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT; + #endif + } + } + } + #endif // SERIAL_XON_XOFF + + #if ENABLED(EMERGENCY_PARSER) + emergency_parser(c); + #endif + } + + #if TX_BUFFER_SIZE > 0 + + FORCE_INLINE void _tx_udr_empty_irq(void) { + // If interrupts are enabled, there must be more data in the output + // buffer. + + #if ENABLED(SERIAL_XON_XOFF) + // Do a priority insertion of an XON/XOFF char, if needed. + const uint8_t state = xon_xoff_state; + if (!(state & XON_XOFF_CHAR_SENT)) { + M_UDRx = state & XON_XOFF_CHAR_MASK; + xon_xoff_state = state | XON_XOFF_CHAR_SENT; + } + else + #endif + { // Send the next byte + const uint8_t t = tx_buffer.tail, c = tx_buffer.buffer[t]; + tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1); + M_UDRx = c; + } + + // clear the TXC bit -- "can be cleared by writing a one to its bit + // location". This makes sure flush() won't return until the bytes + // actually got written + SBI(M_UCSRxA, M_TXCx); + + // Disable interrupts if the buffer is empty + if (tx_buffer.head == tx_buffer.tail) + CBI(M_UCSRxB, M_UDRIEx); + } + + #ifdef M_USARTx_UDRE_vect + ISR(M_USARTx_UDRE_vect) { _tx_udr_empty_irq(); } + #endif + + #endif // TX_BUFFER_SIZE + + #ifdef M_USARTx_RX_vect + ISR(M_USARTx_RX_vect) { store_rxd_char(); } + #endif + + // Public Methods + + void MarlinSerial::begin(const long baud) { + uint16_t baud_setting; + bool useU2X = true; + + #if F_CPU == 16000000UL && SERIAL_PORT == 0 + // Hard-coded exception for compatibility with the bootloader shipped + // with the Duemilanove and previous boards, and the firmware on the + // 8U2 on the Uno and Mega 2560. + if (baud == 57600) useU2X = false; + #endif + + if (useU2X) { + M_UCSRxA = _BV(M_U2Xx); + baud_setting = (F_CPU / 4 / baud - 1) / 2; + } + else { + M_UCSRxA = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } + + // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) + M_UBRRxH = baud_setting >> 8; + M_UBRRxL = baud_setting; + + SBI(M_UCSRxB, M_RXENx); + SBI(M_UCSRxB, M_TXENx); + SBI(M_UCSRxB, M_RXCIEx); + #if TX_BUFFER_SIZE > 0 + CBI(M_UCSRxB, M_UDRIEx); + _written = false; + #endif + } + + void MarlinSerial::end() { + CBI(M_UCSRxB, M_RXENx); + CBI(M_UCSRxB, M_TXENx); + CBI(M_UCSRxB, M_RXCIEx); + CBI(M_UCSRxB, M_UDRIEx); + } + + void MarlinSerial::checkRx(void) { + if (TEST(M_UCSRxA, M_RXCx)) { + CRITICAL_SECTION_START; + store_rxd_char(); + CRITICAL_SECTION_END; + } + } + + int MarlinSerial::peek(void) { + CRITICAL_SECTION_START; + const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail]; + CRITICAL_SECTION_END; + return v; + } + + int MarlinSerial::read(void) { + int v; + CRITICAL_SECTION_START; + const ring_buffer_pos_t t = rx_buffer.tail; + if (rx_buffer.head == t) + v = -1; + else { + v = rx_buffer.buffer[t]; + rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1); + + #if ENABLED(SERIAL_XON_XOFF) + if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) { + // Get count of bytes in the RX buffer + ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); + // When below 10% of RX buffer capacity, send XON before + // running out of RX buffer bytes + if (rx_count < (RX_BUFFER_SIZE) / 10) { + xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT; + CRITICAL_SECTION_END; // End critical section before returning! + writeNoHandshake(XON_CHAR); + return v; + } + } + #endif + } + CRITICAL_SECTION_END; + return v; + } + + ring_buffer_pos_t MarlinSerial::available(void) { + CRITICAL_SECTION_START; + const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail; + CRITICAL_SECTION_END; + return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1); + } + + void MarlinSerial::flush(void) { + // Don't change this order of operations. If the RX interrupt occurs between + // reading rx_buffer_head and updating rx_buffer_tail, the previous rx_buffer_head + // may be written to rx_buffer_tail, making the buffer appear full rather than empty. + CRITICAL_SECTION_START; + rx_buffer.head = rx_buffer.tail; + CRITICAL_SECTION_END; + + #if ENABLED(SERIAL_XON_XOFF) + if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) { + xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT; + writeNoHandshake(XON_CHAR); + } + #endif + } + + #if TX_BUFFER_SIZE > 0 + uint8_t MarlinSerial::availableForWrite(void) { + CRITICAL_SECTION_START; + const uint8_t h = tx_buffer.head, t = tx_buffer.tail; + CRITICAL_SECTION_END; + return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1); + } + + void MarlinSerial::write(const uint8_t c) { + #if ENABLED(SERIAL_XON_XOFF) + const uint8_t state = xon_xoff_state; + if (!(state & XON_XOFF_CHAR_SENT)) { + // Send 2 chars: XON/XOFF, then a user-specified char + writeNoHandshake(state & XON_XOFF_CHAR_MASK); + xon_xoff_state = state | XON_XOFF_CHAR_SENT; + } + #endif + writeNoHandshake(c); + } + + void MarlinSerial::writeNoHandshake(const uint8_t c) { + _written = true; + CRITICAL_SECTION_START; + bool emty = (tx_buffer.head == tx_buffer.tail); + CRITICAL_SECTION_END; + + // If the buffer and the data register is empty, just write the byte + // to the data register and be done. This shortcut helps + // significantly improve the effective datarate at high (> + // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown. + if (emty && TEST(M_UCSRxA, M_UDREx)) { + CRITICAL_SECTION_START; + M_UDRx = c; + SBI(M_UCSRxA, M_TXCx); + CRITICAL_SECTION_END; + return; + } + const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1); + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + while (i == tx_buffer.tail) { + if (!TEST(SREG, SREG_I)) { + // Interrupts are disabled, so we'll have to poll the data + // register empty flag ourselves. If it is set, pretend an + // interrupt has happened and call the handler to free up + // space for us. + if (TEST(M_UCSRxA, M_UDREx)) + _tx_udr_empty_irq(); + } + else { + // nop, the interrupt handler will free up space for us + } + } + + tx_buffer.buffer[tx_buffer.head] = c; + { CRITICAL_SECTION_START; + tx_buffer.head = i; + SBI(M_UCSRxB, M_UDRIEx); + CRITICAL_SECTION_END; + } + return; + } + + void MarlinSerial::flushTX(void) { + // TX + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) + return; + + while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) { + if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx)) + // Interrupts are globally disabled, but the DR empty + // interrupt should be enabled, so poll the DR empty flag to + // prevent deadlock + if (TEST(M_UCSRxA, M_UDREx)) + _tx_udr_empty_irq(); + } + // If we get here, nothing is queued anymore (DRIE is disabled) and + // the hardware finished tranmission (TXC is set). + } + + #else // TX_BUFFER_SIZE == 0 + + void MarlinSerial::write(const uint8_t c) { + #if ENABLED(SERIAL_XON_XOFF) + // Do a priority insertion of an XON/XOFF char, if needed. + const uint8_t state = xon_xoff_state; + if (!(state & XON_XOFF_CHAR_SENT)) { + writeNoHandshake(state & XON_XOFF_CHAR_MASK); + xon_xoff_state = state | XON_XOFF_CHAR_SENT; + } + #endif + writeNoHandshake(c); + } + + void MarlinSerial::writeNoHandshake(uint8_t c) { + while (!TEST(M_UCSRxA, M_UDREx)) {/* nada */} + M_UDRx = c; + } + + #endif // TX_BUFFER_SIZE == 0 + + /** + * Imports from print.h + */ + + void MarlinSerial::print(char c, int base) { + print((long)c, base); + } + + void MarlinSerial::print(unsigned char b, int base) { + print((unsigned long)b, base); + } + + void MarlinSerial::print(int n, int base) { + print((long)n, base); + } + + void MarlinSerial::print(unsigned int n, int base) { + print((unsigned long)n, base); + } + + void MarlinSerial::print(long n, int base) { + if (base == 0) + write(n); + else if (base == 10) { + if (n < 0) { + print('-'); + n = -n; + } + printNumber(n, 10); + } + else + printNumber(n, base); + } + + void MarlinSerial::print(unsigned long n, int base) { + if (base == 0) write(n); + else printNumber(n, base); + } + + void MarlinSerial::print(double n, int digits) { + printFloat(n, digits); + } + + void MarlinSerial::println(void) { + print('\r'); + print('\n'); + } + + void MarlinSerial::println(const String& s) { + print(s); + println(); + } + + void MarlinSerial::println(const char c[]) { + print(c); + println(); + } + + void MarlinSerial::println(char c, int base) { + print(c, base); + println(); + } + + void MarlinSerial::println(unsigned char b, int base) { + print(b, base); + println(); + } + + void MarlinSerial::println(int n, int base) { + print(n, base); + println(); + } + + void MarlinSerial::println(unsigned int n, int base) { + print(n, base); + println(); + } + + void MarlinSerial::println(long n, int base) { + print(n, base); + println(); + } + + void MarlinSerial::println(unsigned long n, int base) { + print(n, base); + println(); + } + + void MarlinSerial::println(double n, int digits) { + print(n, digits); + println(); + } + + // Private Methods + + void MarlinSerial::printNumber(unsigned long n, uint8_t base) { + if (n) { + unsigned char buf[8 * sizeof(long)]; // Enough space for base 2 + int8_t i = 0; + while (n) { + buf[i++] = n % base; + n /= base; + } + while (i--) + print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10))); + } + else + print('0'); + } + + void MarlinSerial::printFloat(double number, uint8_t digits) { + // Handle negative numbers + if (number < 0.0) { + print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i = 0; i < digits; ++i) + rounding *= 0.1; + + number += rounding; + + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long)number; + double remainder = number - (double)int_part; + print(int_part); + + // Print the decimal point, but only if there are digits beyond + if (digits) { + print('.'); + // Extract digits from the remainder one at a time + while (digits--) { + remainder *= 10.0; + int toPrint = int(remainder); + print(toPrint); + remainder -= toPrint; + } + } + } + + // Preinstantiate + MarlinSerial customizedSerial; + +#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H) + +// For AT90USB targets use the UART for BT interfacing +#if defined(USBCON) && ENABLED(BLUETOOTH) + HardwareSerial bluetoothSerial; +#endif diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.h new file mode 100644 index 0000000..6282c89 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/MarlinSerial.h @@ -0,0 +1,180 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + MarlinSerial.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + Modified 28 September 2010 by Mark Sproul + Modified 14 February 2016 by Andreas Hardtung (added tx buffer) + +*/ + +#ifndef MARLINSERIAL_H +#define MARLINSERIAL_H + +#include "MarlinConfig.h" + +#ifndef SERIAL_PORT + #define SERIAL_PORT 0 +#endif + +// The presence of the UBRRH register is used to detect a UART. +#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \ + (port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \ + (port == 3 && defined(UBRR3H))) + +// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor +// requires two levels of indirection to expand macro values properly) +#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) +#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary + #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix +#else + #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix +#endif + +// Registers used by MarlinSerial class (expanded depending on selected serial port) +#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number +#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B) +#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,) +#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,) +#define M_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,) +#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,) +#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,) +#define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,) +#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,) +#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H) +#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L) +#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,) +#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect) +#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,) +#define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect) + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 +#define BYTE 0 + +#ifndef USBCON + // Define constants and variables for buffering incoming serial data. We're + // using a ring buffer (I think), in which rx_buffer_head is the index of the + // location to which to write the next incoming character and rx_buffer_tail + // is the index of the location from which to read. + // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256) + #ifndef RX_BUFFER_SIZE + #define RX_BUFFER_SIZE 128 + #endif + #ifndef TX_BUFFER_SIZE + #define TX_BUFFER_SIZE 32 + #endif + + #if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024 + #error "XON/XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops." + #endif + #if !IS_POWER_OF_2(RX_BUFFER_SIZE) || RX_BUFFER_SIZE < 2 + #error "RX_BUFFER_SIZE must be a power of 2 greater than 1." + #endif + #if TX_BUFFER_SIZE && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWER_OF_2(TX_BUFFER_SIZE)) + #error "TX_BUFFER_SIZE must be 0 or a power of 2 greater than 1." + #endif + + #if RX_BUFFER_SIZE > 256 + typedef uint16_t ring_buffer_pos_t; + #else + typedef uint8_t ring_buffer_pos_t; + #endif + + #if ENABLED(SERIAL_STATS_DROPPED_RX) + extern uint8_t rx_dropped_bytes; + #endif + + #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) + extern ring_buffer_pos_t rx_max_enqueued; + #endif + + class MarlinSerial { //: public Stream + + public: + MarlinSerial() {}; + static void begin(const long); + static void end(); + static int peek(void); + static int read(void); + static void flush(void); + static ring_buffer_pos_t available(void); + static void checkRx(void); + static void write(const uint8_t c); + #if TX_BUFFER_SIZE > 0 + static uint8_t availableForWrite(void); + static void flushTX(void); + #endif + static void writeNoHandshake(const uint8_t c); + + #if ENABLED(SERIAL_STATS_DROPPED_RX) + FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; } + #endif + + #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) + FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; } + #endif + + private: + static void printNumber(unsigned long, const uint8_t); + static void printFloat(double, uint8_t); + + public: + static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); } + static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); } + static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); } + static FORCE_INLINE void print(const char* str) { write(str); } + + static void print(char, int = BYTE); + static void print(unsigned char, int = BYTE); + static void print(int, int = DEC); + static void print(unsigned int, int = DEC); + static void print(long, int = DEC); + static void print(unsigned long, int = DEC); + static void print(double, int = 2); + + static void println(const String& s); + static void println(const char[]); + static void println(char, int = BYTE); + static void println(unsigned char, int = BYTE); + static void println(int, int = DEC); + static void println(unsigned int, int = DEC); + static void println(long, int = DEC); + static void println(unsigned long, int = DEC); + static void println(double, int = 2); + static void println(void); + }; + + extern MarlinSerial customizedSerial; + +#endif // !USBCON + +// Use the UART for Bluetooth in AT90USB configurations +#if defined(USBCON) && ENABLED(BLUETOOTH) + extern HardwareSerial bluetoothSerial; +#endif + +#endif // MARLINSERIAL_H diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin_main.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin_main.cpp new file mode 100644 index 0000000..813c5e1 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Marlin_main.cpp @@ -0,0 +1,14095 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * About Marlin + * + * This firmware is a mashup between Sprinter and grbl. + * - https://github.com/kliment/Sprinter + * - https://github.com/simen/grbl/tree + */ + +/** + * ----------------- + * G-Codes in Marlin + * ----------------- + * + * Helpful G-code references: + * - http://linuxcnc.org/handbook/gcode/g-code.html + * - http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes + * + * Help to document Marlin's G-codes online: + * - http://reprap.org/wiki/G-code + * - https://github.com/MarlinFirmware/MarlinDocumentation + * + * ----------------- + * + * "G" Codes + * + * G0 -> G1 + * G1 - Coordinated Movement X Y Z E + * G2 - CW ARC + * G3 - CCW ARC + * G4 - Dwell S or P + * G5 - Cubic B-spline with XYZE destination and IJPQ offsets + * G10 - Retract filament according to settings of M207 (Requires FWRETRACT) + * G11 - Retract recover filament according to settings of M208 (Requires FWRETRACT) + * G12 - Clean tool (Requires NOZZLE_CLEAN_FEATURE) + * G17 - Select Plane XY (Requires CNC_WORKSPACE_PLANES) + * G18 - Select Plane ZX (Requires CNC_WORKSPACE_PLANES) + * G19 - Select Plane YZ (Requires CNC_WORKSPACE_PLANES) + * G20 - Set input units to inches (Requires INCH_MODE_SUPPORT) + * G21 - Set input units to millimeters (Requires INCH_MODE_SUPPORT) + * G26 - Mesh Validation Pattern (Requires UBL_G26_MESH_VALIDATION) + * G27 - Park Nozzle (Requires NOZZLE_PARK_FEATURE) + * G28 - Home one or more axes + * G29 - Start or continue the bed leveling probe procedure (Requires bed leveling) + * G30 - Single Z probe, probes bed at X Y location (defaults to current XY location) + * G31 - Dock sled (Z_PROBE_SLED only) + * G32 - Undock sled (Z_PROBE_SLED only) + * G33 - Delta Auto-Calibration (Requires DELTA_AUTO_CALIBRATION) + * G38 - Probe in any direction using the Z_MIN_PROBE (Requires G38_PROBE_TARGET) + * G42 - Coordinated move to a mesh point (Requires AUTO_BED_LEVELING_UBL) + * G90 - Use Absolute Coordinates + * G91 - Use Relative Coordinates + * G92 - Set current position to coordinates given + * + * "M" Codes + * + * M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled) + * M1 -> M0 + * M3 - Turn laser/spindle on, set spindle/laser speed/power, set rotation to clockwise + * M4 - Turn laser/spindle on, set spindle/laser speed/power, set rotation to counter-clockwise + * M5 - Turn laser/spindle off + * M17 - Enable/Power all stepper motors + * M18 - Disable all stepper motors; same as M84 + * M20 - List SD card. (Requires SDSUPPORT) + * M21 - Init SD card. (Requires SDSUPPORT) + * M22 - Release SD card. (Requires SDSUPPORT) + * M23 - Select SD file: "M23 /path/file.gco". (Requires SDSUPPORT) + * M24 - Start/resume SD print. (Requires SDSUPPORT) + * M25 - Pause SD print. (Requires SDSUPPORT) + * M26 - Set SD position in bytes: "M26 S12345". (Requires SDSUPPORT) + * M27 - Report SD print status. (Requires SDSUPPORT) + * M28 - Start SD write: "M28 /path/file.gco". (Requires SDSUPPORT) + * M29 - Stop SD write. (Requires SDSUPPORT) + * M30 - Delete file from SD: "M30 /path/file.gco" + * M31 - Report time since last M109 or SD card start to serial. + * M32 - Select file and start SD print: "M32 [S] !/path/file.gco#". (Requires SDSUPPORT) + * Use P to run other files as sub-programs: "M32 P !filename#" + * The '#' is necessary when calling from within sd files, as it stops buffer prereading + * M33 - Get the longname version of a path. (Requires LONG_FILENAME_HOST_SUPPORT) + * M34 - Set SD Card sorting options. (Requires SDCARD_SORT_ALPHA) + * M42 - Change pin status via gcode: M42 P S. LED pin assumed if P is omitted. + * M43 - Display pin status, watch pins for changes, watch endstops & toggle LED, Z servo probe test, toggle pins + * M48 - Measure Z Probe repeatability: M48 P X Y V E L. (Requires Z_MIN_PROBE_REPEATABILITY_TEST) + * M75 - Start the print job timer. + * M76 - Pause the print job timer. + * M77 - Stop the print job timer. + * M78 - Show statistical information about the print jobs. (Requires PRINTCOUNTER) + * M80 - Turn on Power Supply. (Requires POWER_SUPPLY > 0) + * M81 - Turn off Power Supply. (Requires POWER_SUPPLY > 0) + * M82 - Set E codes absolute (default). + * M83 - Set E codes relative while in Absolute (G90) mode. + * M84 - Disable steppers until next move, or use S to specify an idle + * duration after which steppers should turn off. S0 disables the timeout. + * M85 - Set inactivity shutdown timer with parameter S. To disable set zero (default) + * M92 - Set planner.axis_steps_per_mm for one or more axes. + * M100 - Watch Free Memory (for debugging) (Requires M100_FREE_MEMORY_WATCHER) + * M104 - Set extruder target temp. + * M105 - Report current temperatures. + * M106 - Set print fan speed. + * M107 - Print fan off. + * M108 - Break out of heating loops (M109, M190, M303). With no controller, breaks out of M0/M1. (Requires EMERGENCY_PARSER) + * M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating + * Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling + * If AUTOTEMP is enabled, S B F. Exit autotemp by any M109 without F + * M110 - Set the current line number. (Used by host printing) + * M111 - Set debug flags: "M111 S". See flag bits defined in enum.h. + * M112 - Emergency stop. + * M113 - Get or set the timeout interval for Host Keepalive "busy" messages. (Requires HOST_KEEPALIVE_FEATURE) + * M114 - Report current position. + * M115 - Report capabilities. (Extended capabilities requires EXTENDED_CAPABILITIES_REPORT) + * M117 - Display a message on the controller screen. (Requires an LCD) + * M118 - Display a message in the host console. + * M119 - Report endstops status. + * M120 - Enable endstops detection. + * M121 - Disable endstops detection. + * M125 - Save current position and move to filament change position. (Requires PARK_HEAD_ON_PAUSE) + * M126 - Solenoid Air Valve Open. (Requires BARICUDA) + * M127 - Solenoid Air Valve Closed. (Requires BARICUDA) + * M128 - EtoP Open. (Requires BARICUDA) + * M129 - EtoP Closed. (Requires BARICUDA) + * M140 - Set bed target temp. S + * M145 - Set heatup values for materials on the LCD. H B F for S (0=PLA, 1=ABS) + * M149 - Set temperature units. (Requires TEMPERATURE_UNITS_SUPPORT) + * M150 - Set Status LED Color as R U B P. Values 0-255. (Requires BLINKM, RGB_LED, RGBW_LED, NEOPIXEL_LED, or PCA9632). + * M155 - Auto-report temperatures with interval of S. (Requires AUTO_REPORT_TEMPERATURES) + * M163 - Set a single proportion for a mixing extruder. (Requires MIXING_EXTRUDER) + * M164 - Save the mix as a virtual extruder. (Requires MIXING_EXTRUDER and MIXING_VIRTUAL_TOOLS) + * M165 - Set the proportions for a mixing extruder. Use parameters ABCDHI to set the mixing factors. (Requires MIXING_EXTRUDER) + * M190 - Sxxx Wait for bed current temp to reach target temp. ** Waits only when heating! ** + * Rxxx Wait for bed current temp to reach target temp. ** Waits for heating or cooling. ** + * M200 - Set filament diameter, D, setting E axis units to cubic. (Use S0 to revert to linear units.) + * M201 - Set max acceleration in units/s^2 for print moves: "M201 X Y Z E" + * M202 - Set max acceleration in units/s^2 for travel moves: "M202 X Y Z E" ** UNUSED IN MARLIN! ** + * M203 - Set maximum feedrate: "M203 X Y Z E" in units/sec. + * M204 - Set default acceleration in units/sec^2: P R T + * M205 - Set advanced settings. Current units apply: + S T minimum speeds + B + X, Y, Z, E + * M206 - Set additional homing offset. (Disabled by NO_WORKSPACE_OFFSETS or DELTA) + * M207 - Set Retract Length: S, Feedrate: F, and Z lift: Z. (Requires FWRETRACT) + * M208 - Set Recover (unretract) Additional (!) Length: S and Feedrate: F. (Requires FWRETRACT) + * M209 - Turn Automatic Retract Detection on/off: S<0|1> (For slicers that don't support G10/11). (Requires FWRETRACT) + Every normal extrude-only move will be classified as retract depending on the direction. + * M211 - Enable, Disable, and/or Report software endstops: S<0|1> (Requires MIN_SOFTWARE_ENDSTOPS or MAX_SOFTWARE_ENDSTOPS) + * M218 - Set a tool offset: "M218 T X Y". (Requires 2 or more extruders) + * M220 - Set Feedrate Percentage: "M220 S" (i.e., "FR" on the LCD) + * M221 - Set Flow Percentage: "M221 S" + * M226 - Wait until a pin is in a given state: "M226 P S" + * M240 - Trigger a camera to take a photograph. (Requires CHDK or PHOTOGRAPH_PIN) + * M250 - Set LCD contrast: "M250 C" (0-63). (Requires LCD support) + * M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS) + * M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS) + * M280 - Set servo position absolute: "M280 P S". (Requires servos) + * M290 - Babystepping (Requires BABYSTEPPING) + * M300 - Play beep sound S P + * M301 - Set PID parameters P I and D. (Requires PIDTEMP) + * M302 - Allow cold extrudes, or set the minimum extrude S. (Requires PREVENT_COLD_EXTRUSION) + * M303 - PID relay autotune S sets the target temperature. Default 150C. (Requires PIDTEMP) + * M304 - Set bed PID parameters P I and D. (Requires PIDTEMPBED) + * M350 - Set microstepping mode. (Requires digital microstepping pins.) + * M351 - Toggle MS1 MS2 pins directly. (Requires digital microstepping pins.) + * M355 - Set Case Light on/off and set brightness. (Requires CASE_LIGHT_PIN) + * M380 - Activate solenoid on active extruder. (Requires EXT_SOLENOID) + * M381 - Disable all solenoids. (Requires EXT_SOLENOID) + * M400 - Finish all moves. + * M401 - Lower Z probe. (Requires a probe) + * M402 - Raise Z probe. (Requires a probe) + * M404 - Display or set the Nominal Filament Width: "W". (Requires FILAMENT_WIDTH_SENSOR) + * M405 - Enable Filament Sensor flow control. "M405 D". (Requires FILAMENT_WIDTH_SENSOR) + * M406 - Disable Filament Sensor flow control. (Requires FILAMENT_WIDTH_SENSOR) + * M407 - Display measured filament diameter in millimeters. (Requires FILAMENT_WIDTH_SENSOR) + * M410 - Quickstop. Abort all planned moves. + * M420 - Enable/Disable Leveling (with current values) S1=enable S0=disable (Requires MESH_BED_LEVELING or ABL) + * M421 - Set a single Z coordinate in the Mesh Leveling grid. X Y Z (Requires MESH_BED_LEVELING or AUTO_BED_LEVELING_UBL) + * M428 - Set the home_offset based on the current_position. Nearest edge applies. (Disabled by NO_WORKSPACE_OFFSETS or DELTA) + * M500 - Store parameters in EEPROM. (Requires EEPROM_SETTINGS) + * M501 - Restore parameters from EEPROM. (Requires EEPROM_SETTINGS) + * M502 - Revert to the default "factory settings". ** Does not write them to EEPROM! ** + * M503 - Print the current settings (in memory): "M503 S". S0 specifies compact output. + * M540 - Enable/disable SD card abort on endstop hit: "M540 S". (Requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) + * M600 - Pause for filament change: "M600 X Y Z E L". (Requires ADVANCED_PAUSE_FEATURE) + * M665 - Set delta configurations: "M665 L R S A B C I J K" (Requires DELTA) + * M666 - Set delta endstop adjustment. (Requires DELTA) + * M605 - Set dual x-carriage movement mode: "M605 S [X] [R]". (Requires DUAL_X_CARRIAGE) + * M851 - Set Z probe's Z offset in current units. (Negative = below the nozzle.) + * M860 - Report the position of position encoder modules. + * M861 - Report the status of position encoder modules. + * M862 - Perform an axis continuity test for position encoder modules. + * M863 - Perform steps-per-mm calibration for position encoder modules. + * M864 - Change position encoder module I2C address. + * M865 - Check position encoder module firmware version. + * M866 - Report or reset position encoder module error count. + * M867 - Enable/disable or toggle error correction for position encoder modules. + * M868 - Report or set position encoder module error correction threshold. + * M869 - Report position encoder module error. + * M900 - Get and/or Set advance K factor and WH/D ratio. (Requires LIN_ADVANCE) + * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. (Requires HAVE_TMC2130) + * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots) + * M908 - Control digital trimpot directly. (Requires DAC_STEPPER_CURRENT or DIGIPOTSS_PIN) + * M909 - Print digipot/DAC current value. (Requires DAC_STEPPER_CURRENT) + * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires DAC_STEPPER_CURRENT) + * M911 - Report stepper driver overtemperature pre-warn condition. (Requires HAVE_TMC2130) + * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires HAVE_TMC2130) + * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD) + * M914 - Set SENSORLESS_HOMING sensitivity. (Requires SENSORLESS_HOMING) + * + * M360 - SCARA calibration: Move to cal-position ThetaA (0 deg calibration) + * M361 - SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree) + * M362 - SCARA calibration: Move to cal-position PsiA (0 deg calibration) + * M363 - SCARA calibration: Move to cal-position PsiB (90 deg calibration - steps per degree) + * M364 - SCARA calibration: Move to cal-position PSIC (90 deg to Theta calibration position) + * + * ************ Custom codes - This can change to suit future G-code regulations + * M928 - Start SD logging: "M928 filename.gco". Stop with M29. (Requires SDSUPPORT) + * M999 - Restart after being stopped by error + * + * "T" Codes + * + * T0-T3 - Select an extruder (tool) by index: "T F" + * + */ + +#include "Marlin.h" + +#include "ultralcd.h" +#include "planner.h" +#include "stepper.h" +#include "endstops.h" +#include "temperature.h" +#include "cardreader.h" +#include "configuration_store.h" +#include "language.h" +#include "pins_arduino.h" +#include "math.h" +#include "nozzle.h" +#include "duration_t.h" +#include "types.h" +#include "gcode.h" + +#if HAS_ABL + #include "vector_3.h" + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + #include "least_squares_fit.h" + #endif +#elif ENABLED(MESH_BED_LEVELING) + #include "mesh_bed_leveling.h" +#endif + +#if ENABLED(BEZIER_CURVE_SUPPORT) + #include "planner_bezier.h" +#endif + +#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER) + #include "buzzer.h" +#endif + +#if ENABLED(USE_WATCHDOG) + #include "watchdog.h" +#endif + +#if ENABLED(MAX7219_DEBUG) + #include "Max7219_Debug_LEDs.h" +#endif + +#if ENABLED(NEOPIXEL_LED) + #include +#endif + +#if ENABLED(BLINKM) + #include "blinkm.h" + #include "Wire.h" +#endif + +#if ENABLED(PCA9632) + #include "pca9632.h" +#endif + +#if HAS_SERVOS + #include "servo.h" +#endif + +#if HAS_DIGIPOTSS + #include +#endif + +#if ENABLED(DAC_STEPPER_CURRENT) + #include "stepper_dac.h" +#endif + +#if ENABLED(EXPERIMENTAL_I2CBUS) + #include "twibus.h" +#endif + +#if ENABLED(I2C_POSITION_ENCODERS) + #include "I2CPositionEncoder.h" +#endif + +#if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) + #include "endstop_interrupts.h" +#endif + +#if ENABLED(M100_FREE_MEMORY_WATCHER) + void gcode_M100(); + void M100_dump_routine(const char * const title, const char *start, const char *end); +#endif + +#if ENABLED(SDSUPPORT) + CardReader card; +#endif + +#if ENABLED(EXPERIMENTAL_I2CBUS) + TWIBus i2c; +#endif + +#if ENABLED(G38_PROBE_TARGET) + bool G38_move = false, + G38_endstop_hit = false; +#endif + +#if ENABLED(AUTO_BED_LEVELING_UBL) + #include "ubl.h" + extern bool defer_return_to_status; + unified_bed_leveling ubl; + #define UBL_MESH_VALID !( ( ubl.z_values[0][0] == ubl.z_values[0][1] && ubl.z_values[0][1] == ubl.z_values[0][2] \ + && ubl.z_values[1][0] == ubl.z_values[1][1] && ubl.z_values[1][1] == ubl.z_values[1][2] \ + && ubl.z_values[2][0] == ubl.z_values[2][1] && ubl.z_values[2][1] == ubl.z_values[2][2] \ + && ubl.z_values[0][0] == 0 && ubl.z_values[1][0] == 0 && ubl.z_values[2][0] == 0 ) \ + || isnan(ubl.z_values[0][0])) +#endif + +#if ENABLED(NEOPIXEL_LED) + #if NEOPIXEL_TYPE == NEO_RGB || NEOPIXEL_TYPE == NEO_RBG || NEOPIXEL_TYPE == NEO_GRB || NEOPIXEL_TYPE == NEO_GBR || NEOPIXEL_TYPE == NEO_BRG || NEOPIXEL_TYPE == NEO_BGR + #define NEO_WHITE 255, 255, 255 + #else + #define NEO_WHITE 0, 0, 0, 255 + #endif +#endif + +#if ENABLED(RGB_LED) || ENABLED(BLINKM) || ENABLED(PCA9632) + #define LED_WHITE 255, 255, 255 +#elif ENABLED(RGBW_LED) + #define LED_WHITE 0, 0, 0, 255 +#endif + +#if ENABLED(CNC_COORDINATE_SYSTEMS) + int8_t active_coordinate_system = -1; // machine space + float coordinate_system[MAX_COORDINATE_SYSTEMS][XYZ]; +#endif + +bool Running = true; + +uint8_t marlin_debug_flags = DEBUG_NONE; + +/** + * Cartesian Current Position + * Used to track the native machine position as moves are queued. + * Used by 'buffer_line_to_current_position' to do a move after changing it. + * Used by 'SYNC_PLAN_POSITION_KINEMATIC' to update 'planner.position'. + */ +float current_position[XYZE] = { 0.0 }; + +/** + * Cartesian Destination + * The destination for a move, filled in by G-code movement commands, + * and expected by functions like 'prepare_move_to_destination'. + * Set with 'gcode_get_destination' or 'set_destination_from_current'. + */ +float destination[XYZE] = { 0.0 }; + +/** + * axis_homed + * Flags that each linear axis was homed. + * XYZ on cartesian, ABC on delta, ABZ on SCARA. + * + * axis_known_position + * Flags that the position is known in each linear axis. Set when homed. + * Cleared whenever a stepper powers off, potentially losing its position. + */ +bool axis_homed[XYZ] = { false }, axis_known_position[XYZ] = { false }; + +/** + * GCode line number handling. Hosts may opt to include line numbers when + * sending commands to Marlin, and lines will be checked for sequentiality. + * M110 N sets the current line number. + */ +static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0; + +/** + * GCode Command Queue + * A simple ring buffer of BUFSIZE command strings. + * + * Commands are copied into this buffer by the command injectors + * (immediate, serial, sd card) and they are processed sequentially by + * the main loop. The process_next_command function parses the next + * command and hands off execution to individual handler functions. + */ +uint8_t commands_in_queue = 0; // Count of commands in the queue +static uint8_t cmd_queue_index_r = 0, // Ring buffer read position + cmd_queue_index_w = 0; // Ring buffer write position +#if ENABLED(M100_FREE_MEMORY_WATCHER) + char command_queue[BUFSIZE][MAX_CMD_SIZE]; // Necessary so M100 Free Memory Dumper can show us the commands and any corruption +#else // This can be collapsed back to the way it was soon. +static char command_queue[BUFSIZE][MAX_CMD_SIZE]; +#endif + +/** + * Next Injected Command pointer. NULL if no commands are being injected. + * Used by Marlin internally to ensure that commands initiated from within + * are enqueued ahead of any pending serial or sd card commands. + */ +static const char *injected_commands_P = NULL; + +#if ENABLED(TEMPERATURE_UNITS_SUPPORT) + TempUnit input_temp_units = TEMPUNIT_C; +#endif + +/** + * Feed rates are often configured with mm/m + * but the planner and stepper like mm/s units. + */ +static const float homing_feedrate_mm_s[] PROGMEM = { + #if ENABLED(DELTA) + MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z), + #else + MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY), + #endif + MMM_TO_MMS(HOMING_FEEDRATE_Z), 0 +}; +FORCE_INLINE float homing_feedrate(const AxisEnum a) { return pgm_read_float(&homing_feedrate_mm_s[a]); } + +float feedrate_mm_s = MMM_TO_MMS(1500.0); +static float saved_feedrate_mm_s; +int16_t feedrate_percentage = 100, saved_feedrate_percentage; + +// Initialized by settings.load() +bool axis_relative_modes[] = AXIS_RELATIVE_MODES; + +#if HAS_WORKSPACE_OFFSET + #if HAS_POSITION_SHIFT + // The distance that XYZ has been offset by G92. Reset by G28. + float position_shift[XYZ] = { 0 }; + #endif + #if HAS_HOME_OFFSET + // This offset is added to the configured home position. + // Set by M206, M428, or menu item. Saved to EEPROM. + float home_offset[XYZ] = { 0 }; + #endif + #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT + // The above two are combined to save on computes + float workspace_offset[XYZ] = { 0 }; + #endif +#endif + +// Software Endstops are based on the configured limits. +float soft_endstop_min[XYZ] = { X_MIN_BED, Y_MIN_BED, Z_MIN_POS }, + soft_endstop_max[XYZ] = { X_MAX_BED, Y_MAX_BED, Z_MAX_POS }; +#if HAS_SOFTWARE_ENDSTOPS + bool soft_endstops_enabled = true; + #if IS_KINEMATIC + float soft_endstop_radius, soft_endstop_radius_2; + #endif +#endif + +#if FAN_COUNT > 0 + int16_t fanSpeeds[FAN_COUNT] = { 0 }; + #if ENABLED(EXTRA_FAN_SPEED) + int16_t old_fanSpeeds[FAN_COUNT], + new_fanSpeeds[FAN_COUNT]; + #endif + #if ENABLED(PROBING_FANS_OFF) + bool fans_paused = false; + int16_t paused_fanSpeeds[FAN_COUNT] = { 0 }; + #endif +#endif + +// The active extruder (tool). Set with T command. +uint8_t active_extruder = 0; + +// Relative Mode. Enable with G91, disable with G90. +static bool relative_mode = false; + +// For M109 and M190, this flag may be cleared (by M108) to exit the wait loop +volatile bool wait_for_heatup = true; + +// For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop +#if HAS_RESUME_CONTINUE + volatile bool wait_for_user = false; +#endif + +const char axis_codes[XYZE] = { 'X', 'Y', 'Z', 'E' }; + +// Number of characters read in the current line of serial input +static int serial_count = 0; + +// Inactivity shutdown +millis_t previous_cmd_ms = 0; +static millis_t max_inactive_time = 0; +static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL; + +// Print Job Timer +#if ENABLED(PRINTCOUNTER) + PrintCounter print_job_timer = PrintCounter(); +#else + Stopwatch print_job_timer = Stopwatch(); +#endif + +// Buzzer - I2C on the LCD or a BEEPER_PIN +#if ENABLED(LCD_USE_I2C_BUZZER) + #define BUZZ(d,f) lcd_buzz(d, f) +#elif PIN_EXISTS(BEEPER) + Buzzer buzzer; + #define BUZZ(d,f) buzzer.tone(d, f) +#else + #define BUZZ(d,f) NOOP +#endif + +static uint8_t target_extruder; + +#if HAS_BED_PROBE + float zprobe_zoffset; // Initialized by settings.load() +#endif + +#if HAS_ABL + float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED); + #define XY_PROBE_FEEDRATE_MM_S xy_probe_feedrate_mm_s +#elif defined(XY_PROBE_SPEED) + #define XY_PROBE_FEEDRATE_MM_S MMM_TO_MMS(XY_PROBE_SPEED) +#else + #define XY_PROBE_FEEDRATE_MM_S PLANNER_XY_FEEDRATE() +#endif + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) + #if ENABLED(DELTA) + #define ADJUST_DELTA(V) \ + if (planner.leveling_active) { \ + const float zadj = bilinear_z_offset(V); \ + delta[A_AXIS] += zadj; \ + delta[B_AXIS] += zadj; \ + delta[C_AXIS] += zadj; \ + } + #else + #define ADJUST_DELTA(V) if (planner.leveling_active) { delta[Z_AXIS] += bilinear_z_offset(V); } + #endif +#elif IS_KINEMATIC + #define ADJUST_DELTA(V) NOOP +#endif + +#if ENABLED(X_DUAL_ENDSTOPS) + float x_endstop_adj; // Initialized by settings.load() +#endif +#if ENABLED(Y_DUAL_ENDSTOPS) + float y_endstop_adj; // Initialized by settings.load() +#endif +#if ENABLED(Z_DUAL_ENDSTOPS) + float z_endstop_adj; // Initialized by settings.load() +#endif + +// Extruder offsets +#if HOTENDS > 1 + float hotend_offset[XYZ][HOTENDS]; // Initialized by settings.load() +#endif + +#if HAS_Z_SERVO_ENDSTOP + const int z_servo_angle[2] = Z_SERVO_ANGLES; +#endif + +#if ENABLED(BARICUDA) + uint8_t baricuda_valve_pressure = 0, + baricuda_e_to_p_pressure = 0; +#endif + +#if ENABLED(FWRETRACT) // Initialized by settings.load()... + bool autoretract_enabled, // M209 S - Autoretract switch + retracted[EXTRUDERS] = { false }; // Which extruders are currently retracted + float retract_length, // M207 S - G10 Retract length + retract_feedrate_mm_s, // M207 F - G10 Retract feedrate + retract_zlift, // M207 Z - G10 Retract hop size + retract_recover_length, // M208 S - G11 Recover length + retract_recover_feedrate_mm_s, // M208 F - G11 Recover feedrate + swap_retract_length, // M207 W - G10 Swap Retract length + swap_retract_recover_length, // M208 W - G11 Swap Recover length + swap_retract_recover_feedrate_mm_s; // M208 R - G11 Swap Recover feedrate + #if EXTRUDERS > 1 + bool retracted_swap[EXTRUDERS] = { false }; // Which extruders are swap-retracted + #else + constexpr bool retracted_swap[1] = { false }; + #endif +#endif // FWRETRACT + +#if HAS_POWER_SWITCH + bool powersupply_on = + #if ENABLED(PS_DEFAULT_OFF) + false + #else + true + #endif + ; +#endif + +#if ENABLED(DELTA) + + float delta[ABC]; + + // Initialized by settings.load() + float delta_height, + delta_endstop_adj[ABC] = { 0 }, + delta_radius, + delta_tower_angle_trim[ABC], + delta_tower[ABC][2], + delta_diagonal_rod, + delta_calibration_radius, + delta_diagonal_rod_2_tower[ABC], + delta_segments_per_second, + delta_clip_start_height = Z_MAX_POS; + + float delta_safe_distance_from_top(); + +#endif + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) + int bilinear_grid_spacing[2], bilinear_start[2]; + float bilinear_grid_factor[2], + z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y]; +#endif + +#if IS_SCARA + // Float constants for SCARA calculations + const float L1 = SCARA_LINKAGE_1, L2 = SCARA_LINKAGE_2, + L1_2 = sq(float(L1)), L1_2_2 = 2.0 * L1_2, + L2_2 = sq(float(L2)); + + float delta_segments_per_second = SCARA_SEGMENTS_PER_SECOND, + delta[ABC]; +#endif + +float cartes[XYZ] = { 0 }; + +#if ENABLED(FILAMENT_WIDTH_SENSOR) + bool filament_sensor = false; // M405 turns on filament sensor control. M406 turns it off. + float filament_width_nominal = DEFAULT_NOMINAL_FILAMENT_DIA, // Nominal filament width. Change with M404. + filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA; // Measured filament diameter + uint8_t meas_delay_cm = MEASUREMENT_DELAY_CM, // Distance delay setting + measurement_delay[MAX_MEASUREMENT_DELAY + 1]; // Ring buffer to delayed measurement. Store extruder factor after subtracting 100 + int8_t filwidth_delay_index[2] = { 0, -1 }; // Indexes into ring buffer +#endif + +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + static bool filament_ran_out = false; +#endif + +#if ENABLED(ADVANCED_PAUSE_FEATURE) + AdvancedPauseMenuResponse advanced_pause_menu_response; +#endif + +#if ENABLED(MIXING_EXTRUDER) + float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0. + #if MIXING_VIRTUAL_TOOLS > 1 + float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS]; + #endif +#endif + +static bool send_ok[BUFSIZE]; + +#if HAS_SERVOS + Servo servo[NUM_SERVOS]; + #define MOVE_SERVO(I, P) servo[I].move(P) + #if HAS_Z_SERVO_ENDSTOP + #define DEPLOY_Z_SERVO() MOVE_SERVO(Z_ENDSTOP_SERVO_NR, z_servo_angle[0]) + #define STOW_Z_SERVO() MOVE_SERVO(Z_ENDSTOP_SERVO_NR, z_servo_angle[1]) + #endif +#endif + +#ifdef CHDK + millis_t chdkHigh = 0; + bool chdkActive = false; +#endif + +#ifdef AUTOMATIC_CURRENT_CONTROL + bool auto_current_control = 0; +#endif + +#if ENABLED(PID_EXTRUSION_SCALING) + int lpq_len = 20; +#endif + +#if ENABLED(HOST_KEEPALIVE_FEATURE) + MarlinBusyState busy_state = NOT_BUSY; + static millis_t next_busy_signal_ms = 0; + uint8_t host_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL; +#else + #define host_keepalive() NOOP +#endif + +#if ENABLED(I2C_POSITION_ENCODERS) + I2CPositionEncodersMgr I2CPEM; + uint8_t blockBufferIndexRef = 0; + millis_t lastUpdateMillis; +#endif + +#if ENABLED(CNC_WORKSPACE_PLANES) + static WorkspacePlane workspace_plane = PLANE_XY; +#endif + +FORCE_INLINE float pgm_read_any(const float *p) { return pgm_read_float_near(p); } +FORCE_INLINE signed char pgm_read_any(const signed char *p) { return pgm_read_byte_near(p); } + +#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \ + static const PROGMEM type array##_P[XYZ] = { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \ + static inline type array(AxisEnum axis) { return pgm_read_any(&array##_P[axis]); } \ + typedef void __void_##CONFIG##__ + +XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS); +XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS); +XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS); +XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH); +XYZ_CONSTS_FROM_CONFIG(float, home_bump_mm, HOME_BUMP_MM); +XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR); + +/** + * *************************************************************************** + * ******************************** FUNCTIONS ******************************** + * *************************************************************************** + */ + +void stop(); + +void get_available_commands(); +void process_next_command(); +void process_parsed_command(); +void prepare_move_to_destination(); + +void get_cartesian_from_steppers(); +void set_current_from_steppers_for_axis(const AxisEnum axis); + +#if ENABLED(ARC_SUPPORT) + void plan_arc(float target[XYZE], float* offset, uint8_t clockwise); +#endif + +#if ENABLED(BEZIER_CURVE_SUPPORT) + void plan_cubic_move(const float offset[4]); +#endif + +void tool_change(const uint8_t tmp_extruder, const float fr_mm_s=0.0, bool no_move=false); +void report_current_position(); +void report_current_position_detail(); + +#if ENABLED(DEBUG_LEVELING_FEATURE) + void print_xyz(const char* prefix, const char* suffix, const float x, const float y, const float z) { + serialprintPGM(prefix); + SERIAL_CHAR('('); + SERIAL_ECHO(x); + SERIAL_ECHOPAIR(", ", y); + SERIAL_ECHOPAIR(", ", z); + SERIAL_CHAR(')'); + if (suffix) serialprintPGM(suffix); else SERIAL_EOL(); + } + + void print_xyz(const char* prefix, const char* suffix, const float xyz[]) { + print_xyz(prefix, suffix, xyz[X_AXIS], xyz[Y_AXIS], xyz[Z_AXIS]); + } + + #if HAS_ABL + void print_xyz(const char* prefix, const char* suffix, const vector_3 &xyz) { + print_xyz(prefix, suffix, xyz.x, xyz.y, xyz.z); + } + #endif + + #define DEBUG_POS(SUFFIX,VAR) do { \ + print_xyz(PSTR(" " STRINGIFY(VAR) "="), PSTR(" : " SUFFIX "\n"), VAR); }while(0) +#endif + +/** + * sync_plan_position + * + * Set the planner/stepper positions directly from current_position with + * no kinematic translation. Used for homing axes and cartesian/core syncing. + */ +void sync_plan_position() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position); + #endif + planner.set_position_mm(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); +} +inline void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); } + +#if IS_KINEMATIC + + inline void sync_plan_position_kinematic() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position_kinematic", current_position); + #endif + planner.set_position_mm_kinematic(current_position); + } + #define SYNC_PLAN_POSITION_KINEMATIC() sync_plan_position_kinematic() + +#else + + #define SYNC_PLAN_POSITION_KINEMATIC() sync_plan_position() + +#endif + +#if ENABLED(SDSUPPORT) + #include "SdFatUtil.h" + int freeMemory() { return SdFatUtil::FreeRam(); } +#else +extern "C" { + extern char __bss_end; + extern char __heap_start; + extern void* __brkval; + + int freeMemory() { + int free_memory; + if ((int)__brkval == 0) + free_memory = ((int)&free_memory) - ((int)&__bss_end); + else + free_memory = ((int)&free_memory) - ((int)__brkval); + return free_memory; + } +} +#endif // !SDSUPPORT + +#if ENABLED(DIGIPOT_I2C) + extern void digipot_i2c_set_current(uint8_t channel, float current); + extern void digipot_i2c_init(); +#endif + +/** + * Inject the next "immediate" command, when possible, onto the front of the queue. + * Return true if any immediate commands remain to inject. + */ +static bool drain_injected_commands_P() { + if (injected_commands_P != NULL) { + size_t i = 0; + char c, cmd[30]; + strncpy_P(cmd, injected_commands_P, sizeof(cmd) - 1); + cmd[sizeof(cmd) - 1] = '\0'; + while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command + cmd[i] = '\0'; + if (enqueue_and_echo_command(cmd)) // success? + injected_commands_P = c ? injected_commands_P + i + 1 : NULL; // next command or done + } + return (injected_commands_P != NULL); // return whether any more remain +} + +/** + * Record one or many commands to run from program memory. + * Aborts the current queue, if any. + * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards + */ +void enqueue_and_echo_commands_P(const char * const pgcode) { + injected_commands_P = pgcode; + drain_injected_commands_P(); // first command executed asap (when possible) +} + +/** + * Clear the Marlin command queue + */ +void clear_command_queue() { + cmd_queue_index_r = cmd_queue_index_w; + commands_in_queue = 0; +} + +/** + * Once a new command is in the ring buffer, call this to commit it + */ +inline void _commit_command(bool say_ok) { + send_ok[cmd_queue_index_w] = say_ok; + if (++cmd_queue_index_w >= BUFSIZE) cmd_queue_index_w = 0; + commands_in_queue++; +} + +/** + * Copy a command from RAM into the main command buffer. + * Return true if the command was successfully added. + * Return false for a full buffer, or if the 'command' is a comment. + */ +inline bool _enqueuecommand(const char* cmd, bool say_ok=false) { + if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false; + strcpy(command_queue[cmd_queue_index_w], cmd); + _commit_command(say_ok); + return true; +} + +/** + * Enqueue with Serial Echo + */ +bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) { + if (_enqueuecommand(cmd, say_ok)) { + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd); + SERIAL_CHAR('"'); + SERIAL_EOL(); + return true; + } + return false; +} + +void setup_killpin() { + #if HAS_KILL + SET_INPUT_PULLUP(KILL_PIN); + #endif +} + +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + + void setup_filrunoutpin() { + #if ENABLED(ENDSTOPPULLUP_FIL_RUNOUT) + SET_INPUT_PULLUP(FIL_RUNOUT_PIN); + #else + SET_INPUT(FIL_RUNOUT_PIN); + #endif + } + +#endif + +void setup_powerhold() { + #if HAS_SUICIDE + OUT_WRITE(SUICIDE_PIN, HIGH); + #endif + #if HAS_POWER_SWITCH + #if ENABLED(PS_DEFAULT_OFF) + OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP); + #else + OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); + #endif + #endif +} + +void suicide() { + #if HAS_SUICIDE + OUT_WRITE(SUICIDE_PIN, LOW); + #endif +} + +void servo_init() { + #if NUM_SERVOS >= 1 && HAS_SERVO_0 + servo[0].attach(SERVO0_PIN); + servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position. + #endif + #if NUM_SERVOS >= 2 && HAS_SERVO_1 + servo[1].attach(SERVO1_PIN); + servo[1].detach(); + #endif + #if NUM_SERVOS >= 3 && HAS_SERVO_2 + servo[2].attach(SERVO2_PIN); + servo[2].detach(); + #endif + #if NUM_SERVOS >= 4 && HAS_SERVO_3 + servo[3].attach(SERVO3_PIN); + servo[3].detach(); + #endif + + #if HAS_Z_SERVO_ENDSTOP + /** + * Set position of Z Servo Endstop + * + * The servo might be deployed and positioned too low to stow + * when starting up the machine or rebooting the board. + * There's no way to know where the nozzle is positioned until + * homing has been done - no homing with z-probe without init! + * + */ + STOW_Z_SERVO(); + #endif +} + +/** + * Stepper Reset (RigidBoard, et.al.) + */ +#if HAS_STEPPER_RESET + void disableStepperDrivers() { + OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips + } + void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups +#endif + +#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0 + + void i2c_on_receive(int bytes) { // just echo all bytes received to serial + i2c.receive(bytes); + } + + void i2c_on_request() { // just send dummy data for now + i2c.reply("Hello World!\n"); + } + +#endif + +#if HAS_COLOR_LEDS + + #if ENABLED(NEOPIXEL_LED) + + Adafruit_NeoPixel pixels(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEOPIXEL_TYPE + NEO_KHZ800); + + void set_neopixel_color(const uint32_t color) { + for (uint16_t i = 0; i < pixels.numPixels(); ++i) + pixels.setPixelColor(i, color); + pixels.show(); + } + + void setup_neopixel() { + pixels.setBrightness(NEOPIXEL_BRIGHTNESS); // 0 - 255 range + pixels.begin(); + pixels.show(); // initialize to all off + + #if ENABLED(NEOPIXEL_STARTUP_TEST) + safe_delay(1000); + set_neopixel_color(pixels.Color(255, 0, 0, 0)); // red + safe_delay(1000); + set_neopixel_color(pixels.Color(0, 255, 0, 0)); // green + safe_delay(1000); + set_neopixel_color(pixels.Color(0, 0, 255, 0)); // blue + safe_delay(1000); + #endif + set_neopixel_color(pixels.Color(NEO_WHITE)); // white + } + + #endif // NEOPIXEL_LED + + void set_led_color( + const uint8_t r, const uint8_t g, const uint8_t b + #if ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_LED) + , const uint8_t w = 0 + #if ENABLED(NEOPIXEL_LED) + , const uint8_t p = NEOPIXEL_BRIGHTNESS + , bool isSequence = false + #endif + #endif + ) { + + #if ENABLED(NEOPIXEL_LED) + + const uint32_t color = pixels.Color(r, g, b, w); + static uint16_t nextLed = 0; + + pixels.setBrightness(p); + if (!isSequence) + set_neopixel_color(color); + else { + pixels.setPixelColor(nextLed, color); + pixels.show(); + if (++nextLed >= pixels.numPixels()) nextLed = 0; + return; + } + + #endif + + #if ENABLED(BLINKM) + + // This variant uses i2c to send the RGB components to the device. + SendColors(r, g, b); + + #endif + + #if ENABLED(RGB_LED) || ENABLED(RGBW_LED) + + // This variant uses 3 separate pins for the RGB components. + // If the pins can do PWM then their intensity will be set. + WRITE(RGB_LED_R_PIN, r ? HIGH : LOW); + WRITE(RGB_LED_G_PIN, g ? HIGH : LOW); + WRITE(RGB_LED_B_PIN, b ? HIGH : LOW); + analogWrite(RGB_LED_R_PIN, r); + analogWrite(RGB_LED_G_PIN, g); + analogWrite(RGB_LED_B_PIN, b); + + #if ENABLED(RGBW_LED) + WRITE(RGB_LED_W_PIN, w ? HIGH : LOW); + analogWrite(RGB_LED_W_PIN, w); + #endif + + #endif + + #if ENABLED(PCA9632) + // Update I2C LED driver + PCA9632_SetColor(r, g, b); + #endif + } + +#endif // HAS_COLOR_LEDS + +void gcode_line_error(const char* err, bool doFlush = true) { + SERIAL_ERROR_START(); + serialprintPGM(err); + SERIAL_ERRORLN(gcode_LastN); + //Serial.println(gcode_N); + if (doFlush) FlushSerialRequestResend(); + serial_count = 0; +} + +/** + * Get all commands waiting on the serial port and queue them. + * Exit when the buffer is full or when no more characters are + * left on the serial port. + */ +inline void get_serial_commands() { + static char serial_line_buffer[MAX_CMD_SIZE]; + static bool serial_comment_mode = false; + + // If the command buffer is empty for too long, + // send "wait" to indicate Marlin is still waiting. + #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0 + static millis_t last_command_time = 0; + const millis_t ms = millis(); + if (commands_in_queue == 0 && !MYSERIAL.available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) { + SERIAL_ECHOLNPGM(MSG_WAIT); + last_command_time = ms; + } + #endif + + /** + * Loop while serial characters are incoming and the queue is not full + */ + int c; + while (commands_in_queue < BUFSIZE && (c = MYSERIAL.read()) >= 0) { + + char serial_char = c; + + /** + * If the character ends the line + */ + if (serial_char == '\n' || serial_char == '\r') { + + serial_comment_mode = false; // end of line == end of comment + + if (!serial_count) continue; // Skip empty lines + + serial_line_buffer[serial_count] = 0; // Terminate string + serial_count = 0; // Reset buffer + + char* command = serial_line_buffer; + + while (*command == ' ') command++; // Skip leading spaces + char *npos = (*command == 'N') ? command : NULL; // Require the N parameter to start the line + + if (npos) { + + bool M110 = strstr_P(command, PSTR("M110")) != NULL; + + if (M110) { + char* n2pos = strchr(command + 4, 'N'); + if (n2pos) npos = n2pos; + } + + gcode_N = strtol(npos + 1, NULL, 10); + + if (gcode_N != gcode_LastN + 1 && !M110) { + gcode_line_error(PSTR(MSG_ERR_LINE_NO)); + return; + } + + char *apos = strrchr(command, '*'); + if (apos) { + uint8_t checksum = 0, count = uint8_t(apos - command); + while (count) checksum ^= command[--count]; + if (strtol(apos + 1, NULL, 10) != checksum) { + gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH)); + return; + } + } + else { + gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM)); + return; + } + + gcode_LastN = gcode_N; + } + + // Movement commands alert when stopped + if (IsStopped()) { + char* gpos = strchr(command, 'G'); + if (gpos) { + const int codenum = strtol(gpos + 1, NULL, 10); + switch (codenum) { + case 0: + case 1: + case 2: + case 3: + SERIAL_ERRORLNPGM(MSG_ERR_STOPPED); + LCD_MESSAGEPGM(MSG_STOPPED); + break; + } + } + } + + #if DISABLED(EMERGENCY_PARSER) + // If command was e-stop process now + if (strcmp(command, "M108") == 0) { + wait_for_heatup = false; + #if ENABLED(ULTIPANEL) + wait_for_user = false; + #endif + } + if (strcmp(command, "M112") == 0) kill(PSTR(MSG_KILLED)); + if (strcmp(command, "M410") == 0) { quickstop_stepper(); } + #endif + + #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0 + last_command_time = ms; + #endif + + // Add the command to the queue + _enqueuecommand(serial_line_buffer, true); + } + else if (serial_count >= MAX_CMD_SIZE - 1) { + // Keep fetching, but ignore normal characters beyond the max length + // The command will be injected when EOL is reached + } + else if (serial_char == '\\') { // Handle escapes + if ((c = MYSERIAL.read()) >= 0) { + // if we have one more character, copy it over + serial_char = c; + if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char; + } + // otherwise do nothing + } + else { // it's not a newline, carriage return or escape char + if (serial_char == ';') serial_comment_mode = true; + if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char; + } + + } // queue has space, serial has data +} + +#if ENABLED(SDSUPPORT) + + /** + * Get commands from the SD Card until the command buffer is full + * or until the end of the file is reached. The special character '#' + * can also interrupt buffering. + */ + inline void get_sdcard_commands() { + static bool stop_buffering = false, + sd_comment_mode = false; + + if (!card.sdprinting) return; + + /** + * '#' stops reading from SD to the buffer prematurely, so procedural + * macro calls are possible. If it occurs, stop_buffering is triggered + * and the buffer is run dry; this character _can_ occur in serial com + * due to checksums, however, no checksums are used in SD printing. + */ + + if (commands_in_queue == 0) stop_buffering = false; + + uint16_t sd_count = 0; + bool card_eof = card.eof(); + while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) { + const int16_t n = card.get(); + char sd_char = (char)n; + card_eof = card.eof(); + if (card_eof || n == -1 + || sd_char == '\n' || sd_char == '\r' + || ((sd_char == '#' || sd_char == ':') && !sd_comment_mode) + ) { + if (card_eof) { + SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED); + card.printingHasFinished(); + #if ENABLED(PRINTER_EVENT_LEDS) + LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS); + set_led_color(0, 255, 0); // Green + #if HAS_RESUME_CONTINUE + enqueue_and_echo_commands_P(PSTR("M0")); // end of the queue! + #else + safe_delay(1000); + #endif + set_led_color(0, 0, 0); // OFF + #endif + card.checkautostart(true); + } + else if (n == -1) { + SERIAL_ERROR_START(); + SERIAL_ECHOLNPGM(MSG_SD_ERR_READ); + } + if (sd_char == '#') stop_buffering = true; + + sd_comment_mode = false; // for new command + + if (!sd_count) continue; // skip empty lines (and comment lines) + + command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string + sd_count = 0; // clear sd line buffer + + _commit_command(false); + } + else if (sd_count >= MAX_CMD_SIZE - 1) { + /** + * Keep fetching, but ignore normal characters beyond the max length + * The command will be injected when EOL is reached + */ + } + else { + if (sd_char == ';') sd_comment_mode = true; + if (!sd_comment_mode) command_queue[cmd_queue_index_w][sd_count++] = sd_char; + } + } + } + +#endif // SDSUPPORT + +/** + * Add to the circular command queue the next command from: + * - The command-injection queue (injected_commands_P) + * - The active serial input (usually USB) + * - The SD card file being actively printed + */ +void get_available_commands() { + + // if any immediate commands remain, don't get other commands yet + if (drain_injected_commands_P()) return; + + get_serial_commands(); + + #if ENABLED(SDSUPPORT) + get_sdcard_commands(); + #endif +} + +/** + * Set target_extruder from the T parameter or the active_extruder + * + * Returns TRUE if the target is invalid + */ +bool get_target_extruder_from_command(const uint16_t code) { + if (parser.seenval('T')) { + const int8_t e = parser.value_byte(); + if (e >= EXTRUDERS) { + SERIAL_ECHO_START(); + SERIAL_CHAR('M'); + SERIAL_ECHO(code); + SERIAL_ECHOLNPAIR(" " MSG_INVALID_EXTRUDER " ", e); + return true; + } + target_extruder = e; + } + else + target_extruder = active_extruder; + + return false; +} + +#if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + bool extruder_duplication_enabled = false; // Used in Dual X mode 2 +#endif + +#if ENABLED(DUAL_X_CARRIAGE) + + static DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE; + + static float x_home_pos(const int extruder) { + if (extruder == 0) + return base_home_pos(X_AXIS); + else + /** + * In dual carriage mode the extruder offset provides an override of the + * second X-carriage position when homed - otherwise X2_HOME_POS is used. + * This allows soft recalibration of the second extruder home position + * without firmware reflash (through the M218 command). + */ + return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS; + } + + static int x_home_dir(const int extruder) { return extruder ? X2_HOME_DIR : X_HOME_DIR; } + + static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1 + static bool active_extruder_parked = false; // used in mode 1 & 2 + static float raised_parked_position[XYZE]; // used in mode 1 + static millis_t delayed_move_time = 0; // used in mode 1 + static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2 + static int16_t duplicate_extruder_temp_offset = 0; // used in mode 2 + +#endif // DUAL_X_CARRIAGE + +#if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) + + /** + * Software endstops can be used to monitor the open end of + * an axis that has a hardware endstop on the other end. Or + * they can prevent axes from moving past endstops and grinding. + * + * To keep doing their job as the coordinate system changes, + * the software endstop positions must be refreshed to remain + * at the same positions relative to the machine. + */ + void update_software_endstops(const AxisEnum axis) { + const float offs = 0.0 + #if HAS_HOME_OFFSET + + home_offset[axis] + #endif + #if HAS_POSITION_SHIFT + + position_shift[axis] + #endif + ; + + #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT + workspace_offset[axis] = offs; + #endif + + #if ENABLED(DUAL_X_CARRIAGE) + if (axis == X_AXIS) { + + // In Dual X mode hotend_offset[X] is T1's home position + float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS); + + if (active_extruder != 0) { + // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger) + soft_endstop_min[X_AXIS] = X2_MIN_POS + offs; + soft_endstop_max[X_AXIS] = dual_max_x + offs; + } + else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) { + // In Duplication Mode, T0 can move as far left as X_MIN_POS + // but not so far to the right that T1 would move past the end + soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS) + offs; + soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset) + offs; + } + else { + // In other modes, T0 can move from X_MIN_POS to X_MAX_POS + soft_endstop_min[axis] = base_min_pos(axis) + offs; + soft_endstop_max[axis] = base_max_pos(axis) + offs; + } + } + #elif ENABLED(DELTA) + soft_endstop_min[axis] = base_min_pos(axis) + offs; + soft_endstop_max[axis] = (axis == Z_AXIS ? delta_height : base_max_pos(axis)) + offs; + #else + soft_endstop_min[axis] = base_min_pos(axis) + offs; + soft_endstop_max[axis] = base_max_pos(axis) + offs; + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("For ", axis_codes[axis]); + #if HAS_HOME_OFFSET + SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]); + #endif + #if HAS_POSITION_SHIFT + SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]); + #endif + SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]); + SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]); + } + #endif + + #if ENABLED(DELTA) + switch(axis) { + case X_AXIS: + case Y_AXIS: + // Get a minimum radius for clamping + soft_endstop_radius = MIN3(FABS(max(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]); + soft_endstop_radius_2 = sq(soft_endstop_radius); + break; + case Z_AXIS: + delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top(); + default: break; + } + #endif + } + +#endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE + +#if HAS_M206_COMMAND + /** + * Change the home offset for an axis, update the current + * position and the software endstops to retain the same + * relative distance to the new home. + * + * Since this changes the current_position, code should + * call sync_plan_position soon after this. + */ + static void set_home_offset(const AxisEnum axis, const float v) { + home_offset[axis] = v; + update_software_endstops(axis); + } +#endif // HAS_M206_COMMAND + +/** + * Set an axis' current position to its home position (after homing). + * + * For Core and Cartesian robots this applies one-to-one when an + * individual axis has been homed. + * + * DELTA should wait until all homing is done before setting the XYZ + * current_position to home, because homing is a single operation. + * In the case where the axis positions are already known and previously + * homed, DELTA could home to X or Y individually by moving either one + * to the center. However, homing Z always homes XY and Z. + * + * SCARA should wait until all XY homing is done before setting the XY + * current_position to home, because neither X nor Y is at home until + * both are at home. Z can however be homed individually. + * + * Callers must sync the planner position after calling this! + */ +static void set_axis_is_at_home(const AxisEnum axis) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + axis_known_position[axis] = axis_homed[axis] = true; + + #if HAS_POSITION_SHIFT + position_shift[axis] = 0; + update_software_endstops(axis); + #endif + + #if ENABLED(DUAL_X_CARRIAGE) + if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) { + current_position[X_AXIS] = x_home_pos(active_extruder); + return; + } + #endif + + #if ENABLED(MORGAN_SCARA) + + /** + * Morgan SCARA homes XY at the same time + */ + if (axis == X_AXIS || axis == Y_AXIS) { + + float homeposition[XYZ] = { + base_home_pos(X_AXIS), + base_home_pos(Y_AXIS), + base_home_pos(Z_AXIS) + }; + + // SERIAL_ECHOPAIR("homeposition X:", homeposition[X_AXIS]); + // SERIAL_ECHOLNPAIR(" Y:", homeposition[Y_AXIS]); + + /** + * Get Home position SCARA arm angles using inverse kinematics, + * and calculate homing offset using forward kinematics + */ + inverse_kinematics(homeposition); + forward_kinematics_SCARA(delta[A_AXIS], delta[B_AXIS]); + + // SERIAL_ECHOPAIR("Cartesian X:", cartes[X_AXIS]); + // SERIAL_ECHOLNPAIR(" Y:", cartes[Y_AXIS]); + + current_position[axis] = cartes[axis]; + + /** + * SCARA home positions are based on configuration since the actual + * limits are determined by the inverse kinematic transform. + */ + soft_endstop_min[axis] = base_min_pos(axis); // + (cartes[axis] - base_home_pos(axis)); + soft_endstop_max[axis] = base_max_pos(axis); // + (cartes[axis] - base_home_pos(axis)); + } + else + #elif ENABLED(DELTA) + if (axis == Z_AXIS) + current_position[axis] = delta_height; + else + #endif + { + current_position[axis] = base_home_pos(axis); + } + + /** + * Z Probe Z Homing? Account for the probe's Z offset. + */ + #if HAS_BED_PROBE && Z_HOME_DIR < 0 + if (axis == Z_AXIS) { + #if HOMING_Z_WITH_PROBE + + current_position[Z_AXIS] -= zprobe_zoffset; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***"); + SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset); + } + #endif + + #elif ENABLED(DEBUG_LEVELING_FEATURE) + + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***"); + + #endif + } + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + #if HAS_HOME_OFFSET + SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]); + SERIAL_ECHOLNPAIR("] = ", home_offset[axis]); + #endif + DEBUG_POS("", current_position); + SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + #if ENABLED(I2C_POSITION_ENCODERS) + I2CPEM.homed(axis); + #endif +} + +/** + * Some planner shorthand inline functions + */ +inline float get_homing_bump_feedrate(const AxisEnum axis) { + static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR; + uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]); + if (hbd < 1) { + hbd = 10; + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1"); + } + return homing_feedrate(axis) / hbd; +} + +/** + * Move the planner to the current position from wherever it last moved + * (or from wherever it has been told it is located). + */ +inline void buffer_line_to_current_position() { + planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate_mm_s, active_extruder); +} + +/** + * Move the planner to the position stored in the destination array, which is + * used by G0/G1/G2/G3/G5 and many other functions to set a destination. + */ +inline void buffer_line_to_destination(const float fr_mm_s) { + planner.buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], fr_mm_s, active_extruder); +} + +inline void set_current_from_destination() { COPY(current_position, destination); } +inline void set_destination_from_current() { COPY(destination, current_position); } + +#if IS_KINEMATIC + /** + * Calculate delta, start a line, and set current_position to destination + */ + void prepare_uninterpolated_move_to_destination(const float fr_mm_s=0.0) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_uninterpolated_move_to_destination", destination); + #endif + + refresh_cmd_timeout(); + + #if UBL_DELTA + // ubl segmented line will do z-only moves in single segment + ubl.prepare_segmented_line_to(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s)); + #else + if ( current_position[X_AXIS] == destination[X_AXIS] + && current_position[Y_AXIS] == destination[Y_AXIS] + && current_position[Z_AXIS] == destination[Z_AXIS] + && current_position[E_AXIS] == destination[E_AXIS] + ) return; + + planner.buffer_line_kinematic(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s), active_extruder); + #endif + + set_current_from_destination(); + } +#endif // IS_KINEMATIC + +/** + * Plan a move to (X, Y, Z) and set the current_position + * The final current_position may not be the one that was requested + */ +void do_blocking_move_to(const float &rx, const float &ry, const float &rz, const float &fr_mm_s/*=0.0*/) { + const float old_feedrate_mm_s = feedrate_mm_s; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) print_xyz(PSTR(">>> do_blocking_move_to"), NULL, LOGICAL_X_POSITION(rx), LOGICAL_Y_POSITION(ry), LOGICAL_Z_POSITION(rz)); + #endif + + #if ENABLED(DELTA) + + if (!position_is_reachable(rx, ry)) return; + + feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S; + + set_destination_from_current(); // sync destination at the start + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_from_current", destination); + #endif + + // when in the danger zone + if (current_position[Z_AXIS] > delta_clip_start_height) { + if (rz > delta_clip_start_height) { // staying in the danger zone + destination[X_AXIS] = rx; // move directly (uninterpolated) + destination[Y_AXIS] = ry; + destination[Z_AXIS] = rz; + prepare_uninterpolated_move_to_destination(); // set_current_from_destination + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position); + #endif + return; + } + else { + destination[Z_AXIS] = delta_clip_start_height; + prepare_uninterpolated_move_to_destination(); // set_current_from_destination + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position); + #endif + } + } + + if (rz > current_position[Z_AXIS]) { // raising? + destination[Z_AXIS] = rz; + prepare_uninterpolated_move_to_destination(); // set_current_from_destination + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position); + #endif + } + + destination[X_AXIS] = rx; + destination[Y_AXIS] = ry; + prepare_move_to_destination(); // set_current_from_destination + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position); + #endif + + if (rz < current_position[Z_AXIS]) { // lowering? + destination[Z_AXIS] = rz; + prepare_uninterpolated_move_to_destination(); // set_current_from_destination + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position); + #endif + } + + #elif IS_SCARA + + if (!position_is_reachable(rx, ry)) return; + + set_destination_from_current(); + + // If Z needs to raise, do it before moving XY + if (destination[Z_AXIS] < rz) { + destination[Z_AXIS] = rz; + prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS)); + } + + destination[X_AXIS] = rx; + destination[Y_AXIS] = ry; + prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S); + + // If Z needs to lower, do it after moving XY + if (destination[Z_AXIS] > rz) { + destination[Z_AXIS] = rz; + prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS)); + } + + #else + + // If Z needs to raise, do it before moving XY + if (current_position[Z_AXIS] < rz) { + feedrate_mm_s = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS); + current_position[Z_AXIS] = rz; + buffer_line_to_current_position(); + } + + feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S; + current_position[X_AXIS] = rx; + current_position[Y_AXIS] = ry; + buffer_line_to_current_position(); + + // If Z needs to lower, do it after moving XY + if (current_position[Z_AXIS] > rz) { + feedrate_mm_s = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS); + current_position[Z_AXIS] = rz; + buffer_line_to_current_position(); + } + + #endif + + stepper.synchronize(); + + feedrate_mm_s = old_feedrate_mm_s; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< do_blocking_move_to"); + #endif +} +void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) { + do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s); +} +void do_blocking_move_to_z(const float &rz, const float &fr_mm_s/*=0.0*/) { + do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], rz, fr_mm_s); +} +void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s/*=0.0*/) { + do_blocking_move_to(rx, ry, current_position[Z_AXIS], fr_mm_s); +} + +// +// Prepare to do endstop or probe moves +// with custom feedrates. +// +// - Save current feedrates +// - Reset the rate multiplier +// - Reset the command timeout +// - Enable the endstops (for endstop moves) +// +static void setup_for_endstop_or_probe_move() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("setup_for_endstop_or_probe_move", current_position); + #endif + saved_feedrate_mm_s = feedrate_mm_s; + saved_feedrate_percentage = feedrate_percentage; + feedrate_percentage = 100; + refresh_cmd_timeout(); +} + +static void clean_up_after_endstop_or_probe_move() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("clean_up_after_endstop_or_probe_move", current_position); + #endif + feedrate_mm_s = saved_feedrate_mm_s; + feedrate_percentage = saved_feedrate_percentage; + refresh_cmd_timeout(); +} + +#if HAS_BED_PROBE + /** + * Raise Z to a minimum height to make room for a probe to move + */ + inline void do_probe_raise(const float z_raise) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("do_probe_raise(", z_raise); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + float z_dest = z_raise; + if (zprobe_zoffset < 0) z_dest -= zprobe_zoffset; + + if (z_dest > current_position[Z_AXIS]) + do_blocking_move_to_z(z_dest); + } + +#endif // HAS_BED_PROBE + +#if HAS_AXIS_UNHOMED_ERR + + bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) { + #if ENABLED(HOME_AFTER_DEACTIVATE) + const bool xx = x && !axis_known_position[X_AXIS], + yy = y && !axis_known_position[Y_AXIS], + zz = z && !axis_known_position[Z_AXIS]; + #else + const bool xx = x && !axis_homed[X_AXIS], + yy = y && !axis_homed[Y_AXIS], + zz = z && !axis_homed[Z_AXIS]; + #endif + if (xx || yy || zz) { + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_HOME " "); + if (xx) SERIAL_ECHOPGM(MSG_X); + if (yy) SERIAL_ECHOPGM(MSG_Y); + if (zz) SERIAL_ECHOPGM(MSG_Z); + SERIAL_ECHOLNPGM(" " MSG_FIRST); + + #if ENABLED(ULTRA_LCD) + lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : ""); + #endif + return true; + } + return false; + } + +#endif // HAS_AXIS_UNHOMED_ERR + +#if ENABLED(Z_PROBE_SLED) + + #ifndef SLED_DOCKING_OFFSET + #define SLED_DOCKING_OFFSET 0 + #endif + + /** + * Method to dock/undock a sled designed by Charles Bell. + * + * stow[in] If false, move to MAX_X and engage the solenoid + * If true, move to MAX_X and release the solenoid + */ + static void dock_sled(bool stow) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("dock_sled(", stow); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + // Dock sled a bit closer to ensure proper capturing + do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET - ((stow) ? 1 : 0)); + + #if HAS_SOLENOID_1 && DISABLED(EXT_SOLENOID) + WRITE(SOL1_PIN, !stow); // switch solenoid + #endif + } + +#elif ENABLED(Z_PROBE_ALLEN_KEY) + + FORCE_INLINE void do_blocking_move_to(const float raw[XYZ], const float &fr_mm_s) { + do_blocking_move_to(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], fr_mm_s); + } + + void run_deploy_moves_script() { + #if defined(Z_PROBE_ALLEN_KEY_DEPLOY_1_X) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_1_Y) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_1_Z) + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_X + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_Y + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE + #define Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE 0.0 + #endif + const float deploy_1[] = { Z_PROBE_ALLEN_KEY_DEPLOY_1_X, Z_PROBE_ALLEN_KEY_DEPLOY_1_Y, Z_PROBE_ALLEN_KEY_DEPLOY_1_Z }; + do_blocking_move_to(deploy_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_DEPLOY_2_X) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_2_Y) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_2_Z) + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_X + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_Y + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE + #define Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE 0.0 + #endif + const float deploy_2[] = { Z_PROBE_ALLEN_KEY_DEPLOY_2_X, Z_PROBE_ALLEN_KEY_DEPLOY_2_Y, Z_PROBE_ALLEN_KEY_DEPLOY_2_Z }; + do_blocking_move_to(deploy_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_DEPLOY_3_X) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_3_Y) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_3_Z) + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_X + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_Y + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE + #define Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE 0.0 + #endif + const float deploy_3[] = { Z_PROBE_ALLEN_KEY_DEPLOY_3_X, Z_PROBE_ALLEN_KEY_DEPLOY_3_Y, Z_PROBE_ALLEN_KEY_DEPLOY_3_Z }; + do_blocking_move_to(deploy_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_DEPLOY_4_X) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_4_Y) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_4_Z) + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_X + #define Z_PROBE_ALLEN_KEY_DEPLOY_4_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_Y + #define Z_PROBE_ALLEN_KEY_DEPLOY_4_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_4_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE + #define Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE 0.0 + #endif + const float deploy_4[] = { Z_PROBE_ALLEN_KEY_DEPLOY_4_X, Z_PROBE_ALLEN_KEY_DEPLOY_4_Y, Z_PROBE_ALLEN_KEY_DEPLOY_4_Z }; + do_blocking_move_to(deploy_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_DEPLOY_5_X) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_5_Y) || defined(Z_PROBE_ALLEN_KEY_DEPLOY_5_Z) + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_X + #define Z_PROBE_ALLEN_KEY_DEPLOY_5_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_Y + #define Z_PROBE_ALLEN_KEY_DEPLOY_5_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_Z + #define Z_PROBE_ALLEN_KEY_DEPLOY_5_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE + #define Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE 0.0 + #endif + const float deploy_5[] = { Z_PROBE_ALLEN_KEY_DEPLOY_5_X, Z_PROBE_ALLEN_KEY_DEPLOY_5_Y, Z_PROBE_ALLEN_KEY_DEPLOY_5_Z }; + do_blocking_move_to(deploy_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE)); + #endif + } + + void run_stow_moves_script() { + #if defined(Z_PROBE_ALLEN_KEY_STOW_1_X) || defined(Z_PROBE_ALLEN_KEY_STOW_1_Y) || defined(Z_PROBE_ALLEN_KEY_STOW_1_Z) + #ifndef Z_PROBE_ALLEN_KEY_STOW_1_X + #define Z_PROBE_ALLEN_KEY_STOW_1_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_1_Y + #define Z_PROBE_ALLEN_KEY_STOW_1_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_1_Z + #define Z_PROBE_ALLEN_KEY_STOW_1_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE + #define Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE 0.0 + #endif + const float stow_1[] = { Z_PROBE_ALLEN_KEY_STOW_1_X, Z_PROBE_ALLEN_KEY_STOW_1_Y, Z_PROBE_ALLEN_KEY_STOW_1_Z }; + do_blocking_move_to(stow_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_STOW_2_X) || defined(Z_PROBE_ALLEN_KEY_STOW_2_Y) || defined(Z_PROBE_ALLEN_KEY_STOW_2_Z) + #ifndef Z_PROBE_ALLEN_KEY_STOW_2_X + #define Z_PROBE_ALLEN_KEY_STOW_2_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_2_Y + #define Z_PROBE_ALLEN_KEY_STOW_2_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_2_Z + #define Z_PROBE_ALLEN_KEY_STOW_2_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE + #define Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE 0.0 + #endif + const float stow_2[] = { Z_PROBE_ALLEN_KEY_STOW_2_X, Z_PROBE_ALLEN_KEY_STOW_2_Y, Z_PROBE_ALLEN_KEY_STOW_2_Z }; + do_blocking_move_to(stow_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_STOW_3_X) || defined(Z_PROBE_ALLEN_KEY_STOW_3_Y) || defined(Z_PROBE_ALLEN_KEY_STOW_3_Z) + #ifndef Z_PROBE_ALLEN_KEY_STOW_3_X + #define Z_PROBE_ALLEN_KEY_STOW_3_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_3_Y + #define Z_PROBE_ALLEN_KEY_STOW_3_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_3_Z + #define Z_PROBE_ALLEN_KEY_STOW_3_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE + #define Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE 0.0 + #endif + const float stow_3[] = { Z_PROBE_ALLEN_KEY_STOW_3_X, Z_PROBE_ALLEN_KEY_STOW_3_Y, Z_PROBE_ALLEN_KEY_STOW_3_Z }; + do_blocking_move_to(stow_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_STOW_4_X) || defined(Z_PROBE_ALLEN_KEY_STOW_4_Y) || defined(Z_PROBE_ALLEN_KEY_STOW_4_Z) + #ifndef Z_PROBE_ALLEN_KEY_STOW_4_X + #define Z_PROBE_ALLEN_KEY_STOW_4_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_4_Y + #define Z_PROBE_ALLEN_KEY_STOW_4_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_4_Z + #define Z_PROBE_ALLEN_KEY_STOW_4_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE + #define Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE 0.0 + #endif + const float stow_4[] = { Z_PROBE_ALLEN_KEY_STOW_4_X, Z_PROBE_ALLEN_KEY_STOW_4_Y, Z_PROBE_ALLEN_KEY_STOW_4_Z }; + do_blocking_move_to(stow_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE)); + #endif + #if defined(Z_PROBE_ALLEN_KEY_STOW_5_X) || defined(Z_PROBE_ALLEN_KEY_STOW_5_Y) || defined(Z_PROBE_ALLEN_KEY_STOW_5_Z) + #ifndef Z_PROBE_ALLEN_KEY_STOW_5_X + #define Z_PROBE_ALLEN_KEY_STOW_5_X current_position[X_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_5_Y + #define Z_PROBE_ALLEN_KEY_STOW_5_Y current_position[Y_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_5_Z + #define Z_PROBE_ALLEN_KEY_STOW_5_Z current_position[Z_AXIS] + #endif + #ifndef Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE + #define Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE 0.0 + #endif + const float stow_5[] = { Z_PROBE_ALLEN_KEY_STOW_5_X, Z_PROBE_ALLEN_KEY_STOW_5_Y, Z_PROBE_ALLEN_KEY_STOW_5_Z }; + do_blocking_move_to(stow_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE)); + #endif + } + +#endif // Z_PROBE_ALLEN_KEY + +#if ENABLED(PROBING_FANS_OFF) + + void fans_pause(const bool p) { + if (p != fans_paused) { + fans_paused = p; + if (p) + for (uint8_t x = 0; x < FAN_COUNT; x++) { + paused_fanSpeeds[x] = fanSpeeds[x]; + fanSpeeds[x] = 0; + } + else + for (uint8_t x = 0; x < FAN_COUNT; x++) + fanSpeeds[x] = paused_fanSpeeds[x]; + } + } + +#endif // PROBING_FANS_OFF + +#if HAS_BED_PROBE + + // TRIGGERED_WHEN_STOWED_TEST can easily be extended to servo probes, ... if needed. + #if ENABLED(PROBE_IS_TRIGGERED_WHEN_STOWED_TEST) + #if ENABLED(Z_MIN_PROBE_ENDSTOP) + #define _TRIGGERED_WHEN_STOWED_TEST (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING) + #else + #define _TRIGGERED_WHEN_STOWED_TEST (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) + #endif + #endif + + #if QUIET_PROBING + void probing_pause(const bool p) { + #if ENABLED(PROBING_HEATERS_OFF) + thermalManager.pause(p); + #endif + #if ENABLED(PROBING_FANS_OFF) + fans_pause(p); + #endif + if (p) safe_delay( + #if DELAY_BEFORE_PROBING > 25 + DELAY_BEFORE_PROBING + #else + 25 + #endif + ); + } + #endif // QUIET_PROBING + + #if ENABLED(BLTOUCH) + + void bltouch_command(int angle) { + MOVE_SERVO(Z_ENDSTOP_SERVO_NR, angle); // Give the BL-Touch the command and wait + safe_delay(BLTOUCH_DELAY); + } + + bool set_bltouch_deployed(const bool deploy) { + if (deploy && TEST_BLTOUCH()) { // If BL-Touch says it's triggered + bltouch_command(BLTOUCH_RESET); // try to reset it. + bltouch_command(BLTOUCH_DEPLOY); // Also needs to deploy and stow to + bltouch_command(BLTOUCH_STOW); // clear the triggered condition. + safe_delay(1500); // Wait for internal self-test to complete. + // (Measured completion time was 0.65 seconds + // after reset, deploy, and stow sequence) + if (TEST_BLTOUCH()) { // If it still claims to be triggered... + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_STOP_BLTOUCH); + stop(); // punt! + return true; + } + } + + bltouch_command(deploy ? BLTOUCH_DEPLOY : BLTOUCH_STOW); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("set_bltouch_deployed(", deploy); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + return false; + } + + #endif // BLTOUCH + + // returns false for ok and true for failure + bool set_probe_deployed(bool deploy) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + DEBUG_POS("set_probe_deployed", current_position); + SERIAL_ECHOLNPAIR("deploy: ", deploy); + } + #endif + + if (endstops.z_probe_enabled == deploy) return false; + + // Make room for probe + do_probe_raise(_Z_CLEARANCE_DEPLOY_PROBE); + + #if ENABLED(Z_PROBE_SLED) || ENABLED(Z_PROBE_ALLEN_KEY) + #if ENABLED(Z_PROBE_SLED) + #define _AUE_ARGS true, false, false + #else + #define _AUE_ARGS + #endif + if (axis_unhomed_error(_AUE_ARGS)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_STOP_UNHOMED); + stop(); + return true; + } + #endif + + const float oldXpos = current_position[X_AXIS], + oldYpos = current_position[Y_AXIS]; + + #ifdef _TRIGGERED_WHEN_STOWED_TEST + + // If endstop is already false, the Z probe is deployed + if (_TRIGGERED_WHEN_STOWED_TEST == deploy) { // closed after the probe specific actions. + // Would a goto be less ugly? + //while (!_TRIGGERED_WHEN_STOWED_TEST) idle(); // would offer the opportunity + // for a triggered when stowed manual probe. + + if (!deploy) endstops.enable_z_probe(false); // Switch off triggered when stowed probes early + // otherwise an Allen-Key probe can't be stowed. + #endif + + #if ENABLED(SOLENOID_PROBE) + + #if HAS_SOLENOID_1 + WRITE(SOL1_PIN, deploy); + #endif + + #elif ENABLED(Z_PROBE_SLED) + + dock_sled(!deploy); + + #elif HAS_Z_SERVO_ENDSTOP && DISABLED(BLTOUCH) + + MOVE_SERVO(Z_ENDSTOP_SERVO_NR, z_servo_angle[deploy ? 0 : 1]); + + #elif ENABLED(Z_PROBE_ALLEN_KEY) + + deploy ? run_deploy_moves_script() : run_stow_moves_script(); + + #endif + + #ifdef _TRIGGERED_WHEN_STOWED_TEST + } // _TRIGGERED_WHEN_STOWED_TEST == deploy + + if (_TRIGGERED_WHEN_STOWED_TEST == deploy) { // State hasn't changed? + + if (IsRunning()) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Z-Probe failed"); + LCD_ALERTMESSAGEPGM("Err: ZPROBE"); + } + stop(); + return true; + + } // _TRIGGERED_WHEN_STOWED_TEST == deploy + + #endif + + do_blocking_move_to(oldXpos, oldYpos, current_position[Z_AXIS]); // return to position before deploy + endstops.enable_z_probe(deploy); + return false; + } + + /** + * @brief Used by run_z_probe to do a single Z probe move. + * + * @param z Z destination + * @param fr_mm_s Feedrate in mm/s + * @return true to indicate an error + */ + static bool do_probe_move(const float z, const float fr_mm_m) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS(">>> do_probe_move", current_position); + #endif + + // Deploy BLTouch at the start of any probe + #if ENABLED(BLTOUCH) + if (set_bltouch_deployed(true)) return true; + #endif + + #if QUIET_PROBING + probing_pause(true); + #endif + + // Move down until probe triggered + do_blocking_move_to_z(z, MMM_TO_MMS(fr_mm_m)); + + // Check to see if the probe was triggered + const bool probe_triggered = TEST(Endstops::endstop_hit_bits, + #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) + Z_MIN + #else + Z_MIN_PROBE + #endif + ); + + #if QUIET_PROBING + probing_pause(false); + #endif + + // Retract BLTouch immediately after a probe if it was triggered + #if ENABLED(BLTOUCH) + if (probe_triggered && set_bltouch_deployed(false)) return true; + #endif + + // Clear endstop flags + endstops.hit_on_purpose(); + + // Get Z where the steppers were interrupted + set_current_from_steppers_for_axis(Z_AXIS); + + // Tell the planner where we actually are + SYNC_PLAN_POSITION_KINEMATIC(); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("<<< do_probe_move", current_position); + #endif + + return !probe_triggered; + } + + /** + * @details Used by probe_pt to do a single Z probe. + * Leaves current_position[Z_AXIS] at the height where the probe triggered. + * + * @return The raw Z position where the probe was triggered + */ + static float run_z_probe() { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS(">>> run_z_probe", current_position); + #endif + + // Prevent stepper_inactive_time from running out and EXTRUDER_RUNOUT_PREVENT from extruding + refresh_cmd_timeout(); + + #if ENABLED(PROBE_DOUBLE_TOUCH) + + // Do a first probe at the fast speed + if (do_probe_move(-10, Z_PROBE_SPEED_FAST)) return NAN; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + float first_probe_z = current_position[Z_AXIS]; + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("1st Probe Z:", first_probe_z); + #endif + + // move up to make clearance for the probe + do_blocking_move_to_z(current_position[Z_AXIS] + Z_CLEARANCE_BETWEEN_PROBES, MMM_TO_MMS(Z_PROBE_SPEED_FAST)); + + #else + + // If the nozzle is above the travel height then + // move down quickly before doing the slow probe + float z = Z_CLEARANCE_DEPLOY_PROBE; + if (zprobe_zoffset < 0) z -= zprobe_zoffset; + + if (z < current_position[Z_AXIS]) { + + // If we don't make it to the z position (i.e. the probe triggered), move up to make clearance for the probe + if (!do_probe_move(z, Z_PROBE_SPEED_FAST)) + do_blocking_move_to_z(current_position[Z_AXIS] + Z_CLEARANCE_BETWEEN_PROBES, MMM_TO_MMS(Z_PROBE_SPEED_FAST)); + } + #endif + + // move down slowly to find bed + if (do_probe_move(-10, Z_PROBE_SPEED_SLOW)) return NAN; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("<<< run_z_probe", current_position); + #endif + + // Debug: compare probe heights + #if ENABLED(PROBE_DOUBLE_TOUCH) && ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("2nd Probe Z:", current_position[Z_AXIS]); + SERIAL_ECHOLNPAIR(" Discrepancy:", first_probe_z - current_position[Z_AXIS]); + } + #endif + + return current_position[Z_AXIS] + zprobe_zoffset; + } + + /** + * - Move to the given XY + * - Deploy the probe, if not already deployed + * - Probe the bed, get the Z position + * - Depending on the 'stow' flag + * - Stow the probe, or + * - Raise to the BETWEEN height + * - Return the probed Z position + */ + float probe_pt(const float &rx, const float &ry, const bool stow, const uint8_t verbose_level, const bool printable=true) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR(">>> probe_pt(", LOGICAL_X_POSITION(rx)); + SERIAL_ECHOPAIR(", ", LOGICAL_Y_POSITION(ry)); + SERIAL_ECHOPAIR(", ", stow ? "" : "no "); + SERIAL_ECHOLNPGM("stow)"); + DEBUG_POS("", current_position); + } + #endif + + const float nx = rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ny = ry - (Y_PROBE_OFFSET_FROM_EXTRUDER); + + if (!printable + ? !position_is_reachable(nx, ny) + : !position_is_reachable_by_probe(rx, ry) + ) return NAN; + + // Move the probe to the given XY + do_blocking_move_to_xy(nx, ny, XY_PROBE_FEEDRATE_MM_S); + + float measured_z = NAN; + if (!DEPLOY_PROBE()) { + measured_z = run_z_probe(); + + if (!stow) + do_blocking_move_to_z(current_position[Z_AXIS] + Z_CLEARANCE_BETWEEN_PROBES, MMM_TO_MMS(Z_PROBE_SPEED_FAST)); + else + if (STOW_PROBE()) measured_z = NAN; + } + + if (verbose_level > 2) { + SERIAL_PROTOCOLPGM("Bed X: "); + SERIAL_PROTOCOL_F(LOGICAL_X_POSITION(rx), 3); + SERIAL_PROTOCOLPGM(" Y: "); + SERIAL_PROTOCOL_F(LOGICAL_Y_POSITION(ry), 3); + SERIAL_PROTOCOLPGM(" Z: "); + SERIAL_PROTOCOL_F(measured_z, 3); + SERIAL_EOL(); + } + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< probe_pt"); + #endif + + if (isnan(measured_z)) { + LCD_MESSAGEPGM(MSG_ERR_PROBING_FAILED); + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_PROBING_FAILED); + } + + return measured_z; + } + +#endif // HAS_BED_PROBE + +#if HAS_LEVELING + + bool leveling_is_valid() { + return + #if ENABLED(MESH_BED_LEVELING) + mbl.has_mesh + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + !!bilinear_grid_spacing[X_AXIS] + #elif ENABLED(AUTO_BED_LEVELING_UBL) + true + #else // 3POINT, LINEAR + true + #endif + ; + } + + /** + * Turn bed leveling on or off, fixing the current + * position as-needed. + * + * Disable: Current position = physical position + * Enable: Current position = "unleveled" physical position + */ + void set_bed_leveling_enabled(const bool enable/*=true*/) { + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + const bool can_change = (!enable || leveling_is_valid()); + #else + constexpr bool can_change = true; + #endif + + if (can_change && enable != planner.leveling_active) { + + #if ENABLED(MESH_BED_LEVELING) + + if (!enable) + planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]); + + const bool enabling = enable && leveling_is_valid(); + planner.leveling_active = enabling; + if (enabling) planner.unapply_leveling(current_position); + + #elif ENABLED(AUTO_BED_LEVELING_UBL) + #if PLANNER_LEVELING + if (planner.leveling_active) { // leveling from on to off + // change unleveled current_position to physical current_position without moving steppers. + planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]); + planner.leveling_active = false; // disable only AFTER calling apply_leveling + } + else { // leveling from off to on + planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored + // change physical current_position to unleveled current_position without moving steppers. + planner.unapply_leveling(current_position); + } + #else + planner.leveling_active = enable; // just flip the bit, current_position will be wrong until next move. + #endif + + #else // ABL + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + // Force bilinear_z_offset to re-calculate next time + const float reset[XYZ] = { -9999.999, -9999.999, 0 }; + (void)bilinear_z_offset(reset); + #endif + + // Enable or disable leveling compensation in the planner + planner.leveling_active = enable; + + if (!enable) + // When disabling just get the current position from the steppers. + // This will yield the smallest error when first converted back to steps. + set_current_from_steppers_for_axis( + #if ABL_PLANAR + ALL_AXES + #else + Z_AXIS + #endif + ); + else + // When enabling, remove compensation from the current position, + // so compensation will give the right stepper counts. + planner.unapply_leveling(current_position); + + #endif // ABL + } + } + + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + + void set_z_fade_height(const float zfh) { + + const bool level_active = planner.leveling_active; + + #if ENABLED(AUTO_BED_LEVELING_UBL) + if (level_active) set_bed_leveling_enabled(false); // turn off before changing fade height for proper apply/unapply leveling to maintain current_position + #endif + + planner.set_z_fade_height(zfh); + + if (level_active) { + #if ENABLED(AUTO_BED_LEVELING_UBL) + set_bed_leveling_enabled(true); // turn back on after changing fade height + #else + set_current_from_steppers_for_axis( + #if ABL_PLANAR + ALL_AXES + #else + Z_AXIS + #endif + ); + #endif + } + } + + #endif // LEVELING_FADE_HEIGHT + + /** + * Reset calibration results to zero. + */ + void reset_bed_level() { + set_bed_leveling_enabled(false); + #if ENABLED(MESH_BED_LEVELING) + if (leveling_is_valid()) { + mbl.reset(); + mbl.has_mesh = false; + } + #else + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("reset_bed_level"); + #endif + #if ABL_PLANAR + planner.bed_level_matrix.set_to_identity(); + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + bilinear_start[X_AXIS] = bilinear_start[Y_AXIS] = + bilinear_grid_spacing[X_AXIS] = bilinear_grid_spacing[Y_AXIS] = 0; + for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) + for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) + z_values[x][y] = NAN; + #elif ENABLED(AUTO_BED_LEVELING_UBL) + ubl.reset(); + #endif + #endif + } + +#endif // HAS_LEVELING + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(MESH_BED_LEVELING) + + /** + * Enable to produce output in JSON format suitable + * for SCAD or JavaScript mesh visualizers. + * + * Visualize meshes in OpenSCAD using the included script. + * + * buildroot/shared/scripts/MarlinMesh.scad + */ + //#define SCAD_MESH_OUTPUT + + /** + * Print calibration results for plotting or manual frame adjustment. + */ + static void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, float (*fn)(const uint8_t, const uint8_t)) { + #ifndef SCAD_MESH_OUTPUT + for (uint8_t x = 0; x < sx; x++) { + for (uint8_t i = 0; i < precision + 2 + (x < 10 ? 1 : 0); i++) + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOL((int)x); + } + SERIAL_EOL(); + #endif + #ifdef SCAD_MESH_OUTPUT + SERIAL_PROTOCOLLNPGM("measured_z = ["); // open 2D array + #endif + for (uint8_t y = 0; y < sy; y++) { + #ifdef SCAD_MESH_OUTPUT + SERIAL_PROTOCOLPGM(" ["); // open sub-array + #else + if (y < 10) SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOL((int)y); + #endif + for (uint8_t x = 0; x < sx; x++) { + SERIAL_PROTOCOLCHAR(' '); + const float offset = fn(x, y); + if (!isnan(offset)) { + if (offset >= 0) SERIAL_PROTOCOLCHAR('+'); + SERIAL_PROTOCOL_F(offset, precision); + } + else { + #ifdef SCAD_MESH_OUTPUT + for (uint8_t i = 3; i < precision + 3; i++) + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOLPGM("NAN"); + #else + for (uint8_t i = 0; i < precision + 3; i++) + SERIAL_PROTOCOLCHAR(i ? '=' : ' '); + #endif + } + #ifdef SCAD_MESH_OUTPUT + if (x < sx - 1) SERIAL_PROTOCOLCHAR(','); + #endif + } + #ifdef SCAD_MESH_OUTPUT + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOLCHAR(']'); // close sub-array + if (y < sy - 1) SERIAL_PROTOCOLCHAR(','); + #endif + SERIAL_EOL(); + } + #ifdef SCAD_MESH_OUTPUT + SERIAL_PROTOCOLPGM("];"); // close 2D array + #endif + SERIAL_EOL(); + } + +#endif + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + /** + * Extrapolate a single point from its neighbors + */ + static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPGM("Extrapolate ["); + if (x < 10) SERIAL_CHAR(' '); + SERIAL_ECHO((int)x); + SERIAL_CHAR(xdir ? (xdir > 0 ? '+' : '-') : ' '); + SERIAL_CHAR(' '); + if (y < 10) SERIAL_CHAR(' '); + SERIAL_ECHO((int)y); + SERIAL_CHAR(ydir ? (ydir > 0 ? '+' : '-') : ' '); + SERIAL_CHAR(']'); + } + #endif + if (!isnan(z_values[x][y])) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM(" (done)"); + #endif + return; // Don't overwrite good values. + } + SERIAL_EOL(); + + // Get X neighbors, Y neighbors, and XY neighbors + const uint8_t x1 = x + xdir, y1 = y + ydir, x2 = x1 + xdir, y2 = y1 + ydir; + float a1 = z_values[x1][y ], a2 = z_values[x2][y ], + b1 = z_values[x ][y1], b2 = z_values[x ][y2], + c1 = z_values[x1][y1], c2 = z_values[x2][y2]; + + // Treat far unprobed points as zero, near as equal to far + if (isnan(a2)) a2 = 0.0; if (isnan(a1)) a1 = a2; + if (isnan(b2)) b2 = 0.0; if (isnan(b1)) b1 = b2; + if (isnan(c2)) c2 = 0.0; if (isnan(c1)) c1 = c2; + + const float a = 2 * a1 - a2, b = 2 * b1 - b2, c = 2 * c1 - c2; + + // Take the average instead of the median + z_values[x][y] = (a + b + c) / 3.0; + + // Median is robust (ignores outliers). + // z_values[x][y] = (a < b) ? ((b < c) ? b : (c < a) ? a : c) + // : ((c < b) ? b : (a < c) ? a : c); + } + + //Enable this if your SCARA uses 180° of total area + //#define EXTRAPOLATE_FROM_EDGE + + #if ENABLED(EXTRAPOLATE_FROM_EDGE) + #if GRID_MAX_POINTS_X < GRID_MAX_POINTS_Y + #define HALF_IN_X + #elif GRID_MAX_POINTS_Y < GRID_MAX_POINTS_X + #define HALF_IN_Y + #endif + #endif + + /** + * Fill in the unprobed points (corners of circular print surface) + * using linear extrapolation, away from the center. + */ + static void extrapolate_unprobed_bed_level() { + #ifdef HALF_IN_X + constexpr uint8_t ctrx2 = 0, xlen = GRID_MAX_POINTS_X - 1; + #else + constexpr uint8_t ctrx1 = (GRID_MAX_POINTS_X - 1) / 2, // left-of-center + ctrx2 = (GRID_MAX_POINTS_X) / 2, // right-of-center + xlen = ctrx1; + #endif + + #ifdef HALF_IN_Y + constexpr uint8_t ctry2 = 0, ylen = GRID_MAX_POINTS_Y - 1; + #else + constexpr uint8_t ctry1 = (GRID_MAX_POINTS_Y - 1) / 2, // top-of-center + ctry2 = (GRID_MAX_POINTS_Y) / 2, // bottom-of-center + ylen = ctry1; + #endif + + for (uint8_t xo = 0; xo <= xlen; xo++) + for (uint8_t yo = 0; yo <= ylen; yo++) { + uint8_t x2 = ctrx2 + xo, y2 = ctry2 + yo; + #ifndef HALF_IN_X + const uint8_t x1 = ctrx1 - xo; + #endif + #ifndef HALF_IN_Y + const uint8_t y1 = ctry1 - yo; + #ifndef HALF_IN_X + extrapolate_one_point(x1, y1, +1, +1); // left-below + + + #endif + extrapolate_one_point(x2, y1, -1, +1); // right-below - + + #endif + #ifndef HALF_IN_X + extrapolate_one_point(x1, y2, +1, -1); // left-above + - + #endif + extrapolate_one_point(x2, y2, -1, -1); // right-above - - + } + + } + + static void print_bilinear_leveling_grid() { + SERIAL_ECHOLNPGM("Bilinear Leveling Grid:"); + print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, + [](const uint8_t ix, const uint8_t iy) { return z_values[ix][iy]; } + ); + } + + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + + #define ABL_GRID_POINTS_VIRT_X (GRID_MAX_POINTS_X - 1) * (BILINEAR_SUBDIVISIONS) + 1 + #define ABL_GRID_POINTS_VIRT_Y (GRID_MAX_POINTS_Y - 1) * (BILINEAR_SUBDIVISIONS) + 1 + #define ABL_TEMP_POINTS_X (GRID_MAX_POINTS_X + 2) + #define ABL_TEMP_POINTS_Y (GRID_MAX_POINTS_Y + 2) + float z_values_virt[ABL_GRID_POINTS_VIRT_X][ABL_GRID_POINTS_VIRT_Y]; + int bilinear_grid_spacing_virt[2] = { 0 }; + float bilinear_grid_factor_virt[2] = { 0 }; + + static void print_bilinear_leveling_grid_virt() { + SERIAL_ECHOLNPGM("Subdivided with CATMULL ROM Leveling Grid:"); + print_2d_array(ABL_GRID_POINTS_VIRT_X, ABL_GRID_POINTS_VIRT_Y, 5, + [](const uint8_t ix, const uint8_t iy) { return z_values_virt[ix][iy]; } + ); + } + + #define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I)) + float bed_level_virt_coord(const uint8_t x, const uint8_t y) { + uint8_t ep = 0, ip = 1; + if (!x || x == ABL_TEMP_POINTS_X - 1) { + if (x) { + ep = GRID_MAX_POINTS_X - 1; + ip = GRID_MAX_POINTS_X - 2; + } + if (WITHIN(y, 1, ABL_TEMP_POINTS_Y - 2)) + return LINEAR_EXTRAPOLATION( + z_values[ep][y - 1], + z_values[ip][y - 1] + ); + else + return LINEAR_EXTRAPOLATION( + bed_level_virt_coord(ep + 1, y), + bed_level_virt_coord(ip + 1, y) + ); + } + if (!y || y == ABL_TEMP_POINTS_Y - 1) { + if (y) { + ep = GRID_MAX_POINTS_Y - 1; + ip = GRID_MAX_POINTS_Y - 2; + } + if (WITHIN(x, 1, ABL_TEMP_POINTS_X - 2)) + return LINEAR_EXTRAPOLATION( + z_values[x - 1][ep], + z_values[x - 1][ip] + ); + else + return LINEAR_EXTRAPOLATION( + bed_level_virt_coord(x, ep + 1), + bed_level_virt_coord(x, ip + 1) + ); + } + return z_values[x - 1][y - 1]; + } + + static float bed_level_virt_cmr(const float p[4], const uint8_t i, const float t) { + return ( + p[i-1] * -t * sq(1 - t) + + p[i] * (2 - 5 * sq(t) + 3 * t * sq(t)) + + p[i+1] * t * (1 + 4 * t - 3 * sq(t)) + - p[i+2] * sq(t) * (1 - t) + ) * 0.5; + } + + static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const float &tx, const float &ty) { + float row[4], column[4]; + for (uint8_t i = 0; i < 4; i++) { + for (uint8_t j = 0; j < 4; j++) { + column[j] = bed_level_virt_coord(i + x - 1, j + y - 1); + } + row[i] = bed_level_virt_cmr(column, 1, ty); + } + return bed_level_virt_cmr(row, 1, tx); + } + + void bed_level_virt_interpolate() { + bilinear_grid_spacing_virt[X_AXIS] = bilinear_grid_spacing[X_AXIS] / (BILINEAR_SUBDIVISIONS); + bilinear_grid_spacing_virt[Y_AXIS] = bilinear_grid_spacing[Y_AXIS] / (BILINEAR_SUBDIVISIONS); + bilinear_grid_factor_virt[X_AXIS] = RECIPROCAL(bilinear_grid_spacing_virt[X_AXIS]); + bilinear_grid_factor_virt[Y_AXIS] = RECIPROCAL(bilinear_grid_spacing_virt[Y_AXIS]); + for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) + for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) + for (uint8_t ty = 0; ty < BILINEAR_SUBDIVISIONS; ty++) + for (uint8_t tx = 0; tx < BILINEAR_SUBDIVISIONS; tx++) { + if ((ty && y == GRID_MAX_POINTS_Y - 1) || (tx && x == GRID_MAX_POINTS_X - 1)) + continue; + z_values_virt[x * (BILINEAR_SUBDIVISIONS) + tx][y * (BILINEAR_SUBDIVISIONS) + ty] = + bed_level_virt_2cmr( + x + 1, + y + 1, + (float)tx / (BILINEAR_SUBDIVISIONS), + (float)ty / (BILINEAR_SUBDIVISIONS) + ); + } + } + #endif // ABL_BILINEAR_SUBDIVISION + + // Refresh after other values have been updated + void refresh_bed_level() { + bilinear_grid_factor[X_AXIS] = RECIPROCAL(bilinear_grid_spacing[X_AXIS]); + bilinear_grid_factor[Y_AXIS] = RECIPROCAL(bilinear_grid_spacing[Y_AXIS]); + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + bed_level_virt_interpolate(); + #endif + } + +#endif // AUTO_BED_LEVELING_BILINEAR + +/** + * Home an individual linear axis + */ +static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]); + SERIAL_ECHOPAIR(", ", distance); + SERIAL_ECHOPAIR(", ", fr_mm_s); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) + const bool deploy_bltouch = (axis == Z_AXIS && distance < 0); + if (deploy_bltouch) set_bltouch_deployed(true); + #endif + + #if QUIET_PROBING + if (axis == Z_AXIS) probing_pause(true); + #endif + + // Tell the planner we're at Z=0 + current_position[axis] = 0; + + #if IS_SCARA + SYNC_PLAN_POSITION_KINEMATIC(); + current_position[axis] = distance; + inverse_kinematics(current_position); + planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder); + #else + sync_plan_position(); + current_position[axis] = distance; + planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder); + #endif + + stepper.synchronize(); + + #if QUIET_PROBING + if (axis == Z_AXIS) probing_pause(false); + #endif + + #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) + if (deploy_bltouch) set_bltouch_deployed(false); + #endif + + endstops.hit_on_purpose(); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif +} + +/** + * TMC2130 specific sensorless homing using stallGuard2. + * stallGuard2 only works when in spreadCycle mode. + * spreadCycle and stealthChop are mutually exclusive. + */ +#if ENABLED(SENSORLESS_HOMING) + void tmc2130_sensorless_homing(TMC2130Stepper &st, bool enable=true) { + #if ENABLED(STEALTHCHOP) + if (enable) { + st.coolstep_min_speed(1024UL * 1024UL - 1UL); + st.stealthChop(0); + } + else { + st.coolstep_min_speed(0); + st.stealthChop(1); + } + #endif + + st.diag1_stall(enable ? 1 : 0); + } +#endif + +/** + * Home an individual "raw axis" to its endstop. + * This applies to XYZ on Cartesian and Core robots, and + * to the individual ABC steppers on DELTA and SCARA. + * + * At the end of the procedure the axis is marked as + * homed and the current position of that axis is updated. + * Kinematic robots should wait till all axes are homed + * before updating the current position. + */ + +#define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS) + +static void homeaxis(const AxisEnum axis) { + + #if IS_SCARA + // Only Z homing (with probe) is permitted + if (axis != Z_AXIS) { BUZZ(100, 880); return; } + #else + #define CAN_HOME(A) \ + (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0))) + if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return; + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif + + const int axis_home_dir = + #if ENABLED(DUAL_X_CARRIAGE) + (axis == X_AXIS) ? x_home_dir(active_extruder) : + #endif + home_dir(axis); + + // Homing Z towards the bed? Deploy the Z probe or endstop. + #if HOMING_Z_WITH_PROBE + if (axis == Z_AXIS && DEPLOY_PROBE()) return; + #endif + + // Set flags for X, Y, Z motor locking + #if ENABLED(X_DUAL_ENDSTOPS) + if (axis == X_AXIS) stepper.set_homing_flag_x(true); + #endif + #if ENABLED(Y_DUAL_ENDSTOPS) + if (axis == Y_AXIS) stepper.set_homing_flag_y(true); + #endif + #if ENABLED(Z_DUAL_ENDSTOPS) + if (axis == Z_AXIS) stepper.set_homing_flag_z(true); + #endif + + // Disable stealthChop if used. Enable diag1 pin on driver. + #if ENABLED(SENSORLESS_HOMING) + #if ENABLED(X_IS_TMC2130) + if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX); + #endif + #if ENABLED(Y_IS_TMC2130) + if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY); + #endif + #endif + + // Fast move towards endstop until triggered + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:"); + #endif + do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir); + + // When homing Z with probe respect probe clearance + const float bump = axis_home_dir * ( + #if HOMING_Z_WITH_PROBE + (axis == Z_AXIS) ? max(Z_CLEARANCE_BETWEEN_PROBES, home_bump_mm(Z_AXIS)) : + #endif + home_bump_mm(axis) + ); + + // If a second homing move is configured... + if (bump) { + // Move away from the endstop by the axis HOME_BUMP_MM + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:"); + #endif + do_homing_move(axis, -bump); + + // Slow move towards endstop until triggered + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:"); + #endif + do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis)); + } + + /** + * Home axes that have dual endstops... differently + */ + #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS) + const bool pos_dir = axis_home_dir > 0; + #if ENABLED(X_DUAL_ENDSTOPS) + if (axis == X_AXIS) { + const bool lock_x1 = pos_dir ? (x_endstop_adj > 0) : (x_endstop_adj < 0); + const float adj = FABS(x_endstop_adj); + if (lock_x1) stepper.set_x_lock(true); else stepper.set_x2_lock(true); + do_homing_move(axis, pos_dir ? -adj : adj); + if (lock_x1) stepper.set_x_lock(false); else stepper.set_x2_lock(false); + stepper.set_homing_flag_x(false); + } + #endif + #if ENABLED(Y_DUAL_ENDSTOPS) + if (axis == Y_AXIS) { + const bool lock_y1 = pos_dir ? (y_endstop_adj > 0) : (y_endstop_adj < 0); + const float adj = FABS(y_endstop_adj); + if (lock_y1) stepper.set_y_lock(true); else stepper.set_y2_lock(true); + do_homing_move(axis, pos_dir ? -adj : adj); + if (lock_y1) stepper.set_y_lock(false); else stepper.set_y2_lock(false); + stepper.set_homing_flag_y(false); + } + #endif + #if ENABLED(Z_DUAL_ENDSTOPS) + if (axis == Z_AXIS) { + const bool lock_z1 = pos_dir ? (z_endstop_adj > 0) : (z_endstop_adj < 0); + const float adj = FABS(z_endstop_adj); + if (lock_z1) stepper.set_z_lock(true); else stepper.set_z2_lock(true); + do_homing_move(axis, pos_dir ? -adj : adj); + if (lock_z1) stepper.set_z_lock(false); else stepper.set_z2_lock(false); + stepper.set_homing_flag_z(false); + } + #endif + #endif + + #if IS_SCARA + + set_axis_is_at_home(axis); + SYNC_PLAN_POSITION_KINEMATIC(); + + #elif ENABLED(DELTA) + + // Delta has already moved all three towers up in G28 + // so here it re-homes each tower in turn. + // Delta homing treats the axes as normal linear axes. + + // retrace by the amount specified in delta_endstop_adj + additional 0.1mm in order to have minimum steps + if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:"); + #endif + do_homing_move(axis, delta_endstop_adj[axis] - 0.1 * Z_HOME_DIR); + } + + #else + + // For cartesian/core machines, + // set the axis to its home position + set_axis_is_at_home(axis); + sync_plan_position(); + + destination[axis] = current_position[axis]; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position); + #endif + + #endif + + // Re-enable stealthChop if used. Disable diag1 pin on driver. + #if ENABLED(SENSORLESS_HOMING) + #if ENABLED(X_IS_TMC2130) + if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX, false); + #endif + #if ENABLED(Y_IS_TMC2130) + if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY, false); + #endif + #endif + + // Put away the Z probe + #if HOMING_Z_WITH_PROBE + if (axis == Z_AXIS && STOW_PROBE()) return; + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]); + SERIAL_CHAR(')'); + SERIAL_EOL(); + } + #endif +} // homeaxis() + +#if ENABLED(FWRETRACT) + + /** + * Retract or recover according to firmware settings + * + * This function handles retract/recover moves for G10 and G11, + * plus auto-retract moves sent from G0/G1 when E-only moves are done. + * + * To simplify the logic, doubled retract/recover moves are ignored. + * + * Note: Z lift is done transparently to the planner. Aborting + * a print between G10 and G11 may corrupt the Z position. + * + * Note: Auto-retract will apply the set Z hop in addition to any Z hop + * included in the G-code. Use M207 Z0 to to prevent double hop. + */ + void retract(const bool retracting + #if EXTRUDERS > 1 + , bool swapping = false + #endif + ) { + + static float hop_amount = 0.0; // Total amount lifted, for use in recover + + // Prevent two retracts or recovers in a row + if (retracted[active_extruder] == retracting) return; + + // Prevent two swap-retract or recovers in a row + #if EXTRUDERS > 1 + // Allow G10 S1 only after G10 + if (swapping && retracted_swap[active_extruder] == retracting) return; + // G11 priority to recover the long retract if activated + if (!retracting) swapping = retracted_swap[active_extruder]; + #else + const bool swapping = false; + #endif + + /* // debugging + SERIAL_ECHOLNPAIR("retracting ", retracting); + SERIAL_ECHOLNPAIR("swapping ", swapping); + SERIAL_ECHOLNPAIR("active extruder ", active_extruder); + for (uint8_t i = 0; i < EXTRUDERS; ++i) { + SERIAL_ECHOPAIR("retracted[", i); + SERIAL_ECHOLNPAIR("] ", retracted[i]); + SERIAL_ECHOPAIR("retracted_swap[", i); + SERIAL_ECHOLNPAIR("] ", retracted_swap[i]); + } + SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]); + SERIAL_ECHOLNPAIR("hop_amount ", hop_amount); + //*/ + + const bool has_zhop = retract_zlift > 0.01; // Is there a hop set? + const float old_feedrate_mm_s = feedrate_mm_s; + + // The current position will be the destination for E and Z moves + set_destination_from_current(); + stepper.synchronize(); // Wait for buffered moves to complete + + const float renormalize = 1.0 / planner.e_factor[active_extruder]; + + if (retracting) { + // Retract by moving from a faux E position back to the current E position + feedrate_mm_s = retract_feedrate_mm_s; + current_position[E_AXIS] += (swapping ? swap_retract_length : retract_length) * renormalize; + sync_plan_position_e(); + prepare_move_to_destination(); + + // Is a Z hop set, and has the hop not yet been done? + if (has_zhop && !hop_amount) { + hop_amount += retract_zlift; // Carriage is raised for retraction hop + feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS]; // Z feedrate to max + current_position[Z_AXIS] -= retract_zlift; // Pretend current pos is lower. Next move raises Z. + SYNC_PLAN_POSITION_KINEMATIC(); // Set the planner to the new position + prepare_move_to_destination(); // Raise up to the old current pos + feedrate_mm_s = retract_feedrate_mm_s; // Restore feedrate + } + } + else { + // If a hop was done and Z hasn't changed, undo the Z hop + if (hop_amount) { + current_position[Z_AXIS] += retract_zlift; // Pretend current pos is lower. Next move raises Z. + SYNC_PLAN_POSITION_KINEMATIC(); // Set the planner to the new position + feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS]; // Z feedrate to max + prepare_move_to_destination(); // Raise up to the old current pos + hop_amount = 0.0; // Clear hop + } + + // A retract multiplier has been added here to get faster swap recovery + feedrate_mm_s = swapping ? swap_retract_recover_feedrate_mm_s : retract_recover_feedrate_mm_s; + + const float move_e = swapping ? swap_retract_length + swap_retract_recover_length : retract_length + retract_recover_length; + current_position[E_AXIS] -= move_e * renormalize; + sync_plan_position_e(); + prepare_move_to_destination(); // Recover E + } + + feedrate_mm_s = old_feedrate_mm_s; // Restore original feedrate + + retracted[active_extruder] = retracting; // Active extruder now retracted / recovered + + // If swap retract/recover update the retracted_swap flag too + #if EXTRUDERS > 1 + if (swapping) retracted_swap[active_extruder] = retracting; + #endif + + /* // debugging + SERIAL_ECHOLNPAIR("retracting ", retracting); + SERIAL_ECHOLNPAIR("swapping ", swapping); + SERIAL_ECHOLNPAIR("active_extruder ", active_extruder); + for (uint8_t i = 0; i < EXTRUDERS; ++i) { + SERIAL_ECHOPAIR("retracted[", i); + SERIAL_ECHOLNPAIR("] ", retracted[i]); + SERIAL_ECHOPAIR("retracted_swap[", i); + SERIAL_ECHOLNPAIR("] ", retracted_swap[i]); + } + SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]); + SERIAL_ECHOLNPAIR("hop_amount ", hop_amount); + //*/ + + } + +#endif // FWRETRACT + +#if ENABLED(MIXING_EXTRUDER) + + void normalize_mix() { + float mix_total = 0.0; + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]); + // Scale all values if they don't add up to ~1.0 + if (!NEAR(mix_total, 1.0)) { + SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling."); + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total; + } + } + + #if ENABLED(DIRECT_MIXING_IN_G1) + // Get mixing parameters from the GCode + // The total "must" be 1.0 (but it will be normalized) + // If no mix factors are given, the old mix is preserved + void gcode_get_mix() { + const char* mixing_codes = "ABCDHI"; + byte mix_bits = 0; + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) { + if (parser.seenval(mixing_codes[i])) { + SBI(mix_bits, i); + float v = parser.value_float(); + NOLESS(v, 0.0); + mixing_factor[i] = RECIPROCAL(v); + } + } + // If any mixing factors were included, clear the rest + // If none were included, preserve the last mix + if (mix_bits) { + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0; + normalize_mix(); + } + } + #endif + +#endif + +/** + * *************************************************************************** + * ***************************** G-CODE HANDLING ***************************** + * *************************************************************************** + */ + +/** + * Set XYZE destination and feedrate from the current GCode command + * + * - Set destination from included axis codes + * - Set to current for missing axis codes + * - Set the feedrate, if included + */ +void gcode_get_destination() { + LOOP_XYZE(i) { + if (parser.seen(axis_codes[i])) { + const float v = parser.value_axis_units((AxisEnum)i) + (axis_relative_modes[i] || relative_mode ? current_position[i] : 0); + destination[i] = i == E_AXIS ? v : LOGICAL_TO_NATIVE(v, i); + } + else + destination[i] = current_position[i]; + } + + if (parser.linearval('F') > 0.0) + feedrate_mm_s = MMM_TO_MMS(parser.value_feedrate()); + + #if ENABLED(PRINTCOUNTER) + if (!DEBUGGING(DRYRUN)) + print_job_timer.incFilamentUsed(destination[E_AXIS] - current_position[E_AXIS]); + #endif + + // Get ABCDHI mixing factors + #if ENABLED(MIXING_EXTRUDER) && ENABLED(DIRECT_MIXING_IN_G1) + gcode_get_mix(); + #endif +} + +#if ENABLED(HOST_KEEPALIVE_FEATURE) + + /** + * Output a "busy" message at regular intervals + * while the machine is not accepting commands. + */ + void host_keepalive() { + const millis_t ms = millis(); + if (host_keepalive_interval && busy_state != NOT_BUSY) { + if (PENDING(ms, next_busy_signal_ms)) return; + switch (busy_state) { + case IN_HANDLER: + case IN_PROCESS: + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_BUSY_PROCESSING); + break; + case PAUSED_FOR_USER: + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_USER); + break; + case PAUSED_FOR_INPUT: + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_INPUT); + break; + default: + break; + } + } + next_busy_signal_ms = ms + host_keepalive_interval * 1000UL; + } + +#endif // HOST_KEEPALIVE_FEATURE + + +/************************************************** + ***************** GCode Handlers ***************** + **************************************************/ + +#if ENABLED(NO_MOTION_BEFORE_HOMING) + #define G0_G1_CONDITION !axis_unhomed_error(parser.seen('X'), parser.seen('Y'), parser.seen('Z')) +#else + #define G0_G1_CONDITION true +#endif + +/** + * G0, G1: Coordinated movement of X Y Z E axes + */ +inline void gcode_G0_G1( + #if IS_SCARA + bool fast_move=false + #endif +) { + if (IsRunning() && G0_G1_CONDITION) { + gcode_get_destination(); // For X Y Z E F + + #if ENABLED(FWRETRACT) + if (MIN_AUTORETRACT <= MAX_AUTORETRACT) { + // When M209 Autoretract is enabled, convert E-only moves to firmware retract/recover moves + if (autoretract_enabled && parser.seen('E') && !(parser.seen('X') || parser.seen('Y') || parser.seen('Z'))) { + const float echange = destination[E_AXIS] - current_position[E_AXIS]; + // Is this a retract or recover move? + if (WITHIN(FABS(echange), MIN_AUTORETRACT, MAX_AUTORETRACT) && retracted[active_extruder] == (echange > 0.0)) { + current_position[E_AXIS] = destination[E_AXIS]; // Hide a G1-based retract/recover from calculations + sync_plan_position_e(); // AND from the planner + return retract(echange < 0.0); // Firmware-based retract/recover (double-retract ignored) + } + } + } + #endif // FWRETRACT + + #if IS_SCARA + fast_move ? prepare_uninterpolated_move_to_destination() : prepare_move_to_destination(); + #else + prepare_move_to_destination(); + #endif + } +} + +/** + * G2: Clockwise Arc + * G3: Counterclockwise Arc + * + * This command has two forms: IJ-form and R-form. + * + * - I specifies an X offset. J specifies a Y offset. + * At least one of the IJ parameters is required. + * X and Y can be omitted to do a complete circle. + * The given XY is not error-checked. The arc ends + * based on the angle of the destination. + * Mixing I or J with R will throw an error. + * + * - R specifies the radius. X or Y is required. + * Omitting both X and Y will throw an error. + * X or Y must differ from the current XY. + * Mixing R with I or J will throw an error. + * + * - P specifies the number of full circles to do + * before the specified arc move. + * + * Examples: + * + * G2 I10 ; CW circle centered at X+10 + * G3 X20 Y12 R14 ; CCW circle with r=14 ending at X20 Y12 + */ +#if ENABLED(ARC_SUPPORT) + + inline void gcode_G2_G3(const bool clockwise) { + #if ENABLED(NO_MOTION_BEFORE_HOMING) + if (axis_unhomed_error()) return; + #endif + + if (IsRunning()) { + + #if ENABLED(SF_ARC_FIX) + const bool relative_mode_backup = relative_mode; + relative_mode = true; + #endif + + gcode_get_destination(); + + #if ENABLED(SF_ARC_FIX) + relative_mode = relative_mode_backup; + #endif + + float arc_offset[2] = { 0.0, 0.0 }; + if (parser.seenval('R')) { + const float r = parser.value_linear_units(), + p1 = current_position[X_AXIS], q1 = current_position[Y_AXIS], + p2 = destination[X_AXIS], q2 = destination[Y_AXIS]; + if (r && (p2 != p1 || q2 != q1)) { + const float e = clockwise ^ (r < 0) ? -1 : 1, // clockwise -1/1, counterclockwise 1/-1 + dx = p2 - p1, dy = q2 - q1, // X and Y differences + d = HYPOT(dx, dy), // Linear distance between the points + h = SQRT(sq(r) - sq(d * 0.5)), // Distance to the arc pivot-point + mx = (p1 + p2) * 0.5, my = (q1 + q2) * 0.5, // Point between the two points + sx = -dy / d, sy = dx / d, // Slope of the perpendicular bisector + cx = mx + e * h * sx, cy = my + e * h * sy; // Pivot-point of the arc + arc_offset[0] = cx - p1; + arc_offset[1] = cy - q1; + } + } + else { + if (parser.seenval('I')) arc_offset[0] = parser.value_linear_units(); + if (parser.seenval('J')) arc_offset[1] = parser.value_linear_units(); + } + + if (arc_offset[0] || arc_offset[1]) { + + #if ENABLED(ARC_P_CIRCLES) + // P indicates number of circles to do + int8_t circles_to_do = parser.byteval('P'); + if (!WITHIN(circles_to_do, 0, 100)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_ARC_ARGS); + } + while (circles_to_do--) + plan_arc(current_position, arc_offset, clockwise); + #endif + + // Send the arc to the planner + plan_arc(destination, arc_offset, clockwise); + refresh_cmd_timeout(); + } + else { + // Bad arguments + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_ARC_ARGS); + } + } + } + +#endif // ARC_SUPPORT + +void dwell(millis_t time) { + refresh_cmd_timeout(); + time += previous_cmd_ms; + while (PENDING(millis(), time)) idle(); +} + +/** + * G4: Dwell S or P + */ +inline void gcode_G4() { + millis_t dwell_ms = 0; + + if (parser.seenval('P')) dwell_ms = parser.value_millis(); // milliseconds to wait + if (parser.seenval('S')) dwell_ms = parser.value_millis_from_seconds(); // seconds to wait + + stepper.synchronize(); + + if (!lcd_hasstatus()) LCD_MESSAGEPGM(MSG_DWELL); + + dwell(dwell_ms); +} + +#if ENABLED(BEZIER_CURVE_SUPPORT) + + /** + * Parameters interpreted according to: + * http://linuxcnc.org/docs/2.6/html/gcode/gcode.html#sec:G5-Cubic-Spline + * However I, J omission is not supported at this point; all + * parameters can be omitted and default to zero. + */ + + /** + * G5: Cubic B-spline + */ + inline void gcode_G5() { + #if ENABLED(NO_MOTION_BEFORE_HOMING) + if (axis_unhomed_error()) return; + #endif + + if (IsRunning()) { + + #if ENABLED(CNC_WORKSPACE_PLANES) + if (workspace_plane != PLANE_XY) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_BAD_PLANE_MODE); + return; + } + #endif + + gcode_get_destination(); + + const float offset[] = { + parser.linearval('I'), + parser.linearval('J'), + parser.linearval('P'), + parser.linearval('Q') + }; + + plan_cubic_move(offset); + } + } + +#endif // BEZIER_CURVE_SUPPORT + +#if ENABLED(FWRETRACT) + + /** + * G10 - Retract filament according to settings of M207 + */ + inline void gcode_G10() { + #if EXTRUDERS > 1 + const bool rs = parser.boolval('S'); + retracted_swap[active_extruder] = rs; // Use 'S' for swap, default to false + #endif + retract(true + #if EXTRUDERS > 1 + , rs + #endif + ); + } + + /** + * G11 - Recover filament according to settings of M208 + */ + inline void gcode_G11() { retract(false); } + +#endif // FWRETRACT + +#if ENABLED(NOZZLE_CLEAN_FEATURE) + /** + * G12: Clean the nozzle + */ + inline void gcode_G12() { + // Don't allow nozzle cleaning without homing first + if (axis_unhomed_error()) return; + + const uint8_t pattern = parser.ushortval('P', 0), + strokes = parser.ushortval('S', NOZZLE_CLEAN_STROKES), + objects = parser.ushortval('T', NOZZLE_CLEAN_TRIANGLES); + const float radius = parser.floatval('R', NOZZLE_CLEAN_CIRCLE_RADIUS); + + Nozzle::clean(pattern, strokes, radius, objects); + } +#endif + +#if ENABLED(CNC_WORKSPACE_PLANES) + + inline void report_workspace_plane() { + SERIAL_ECHO_START(); + SERIAL_ECHOPGM("Workspace Plane "); + serialprintPGM( + workspace_plane == PLANE_YZ ? PSTR("YZ\n") : + workspace_plane == PLANE_ZX ? PSTR("ZX\n") : + PSTR("XY\n") + ); + } + + inline void set_workspace_plane(const WorkspacePlane plane) { + workspace_plane = plane; + if (DEBUGGING(INFO)) report_workspace_plane(); + } + + /** + * G17: Select Plane XY + * G18: Select Plane ZX + * G19: Select Plane YZ + */ + inline void gcode_G17() { set_workspace_plane(PLANE_XY); } + inline void gcode_G18() { set_workspace_plane(PLANE_ZX); } + inline void gcode_G19() { set_workspace_plane(PLANE_YZ); } + +#endif // CNC_WORKSPACE_PLANES + +#if ENABLED(CNC_COORDINATE_SYSTEMS) + + /** + * Select a coordinate system and update the current position. + * System index -1 is used to specify machine-native. + */ + bool select_coordinate_system(const int8_t _new) { + if (active_coordinate_system == _new) return false; + float old_offset[XYZ] = { 0 }, new_offset[XYZ] = { 0 }; + if (WITHIN(active_coordinate_system, 0, MAX_COORDINATE_SYSTEMS - 1)) + COPY(old_offset, coordinate_system[active_coordinate_system]); + if (WITHIN(_new, 0, MAX_COORDINATE_SYSTEMS - 1)) + COPY(new_offset, coordinate_system[_new]); + active_coordinate_system = _new; + bool didXYZ = false; + LOOP_XYZ(i) { + const float diff = new_offset[i] - old_offset[i]; + if (diff) { + position_shift[i] += diff; + update_software_endstops((AxisEnum)i); + didXYZ = true; + } + } + if (didXYZ) SYNC_PLAN_POSITION_KINEMATIC(); + return true; + } + + /** + * In CNC G-code G53 is like a modifier + * It precedes a movement command (or other modifiers) on the same line. + * This is the first command to use parser.chain() to make this possible. + */ + inline void gcode_G53() { + // If this command has more following... + if (parser.chain()) { + const int8_t _system = active_coordinate_system; + active_coordinate_system = -1; + process_parsed_command(); + active_coordinate_system = _system; + } + } + + /** + * G54-G59.3: Select a new workspace + * + * A workspace is an XYZ offset to the machine native space. + * All workspaces default to 0,0,0 at start, or with EEPROM + * support they may be restored from a previous session. + * + * G92 is used to set the current workspace's offset. + */ + inline void gcode_G54_59(uint8_t subcode=0) { + const int8_t _space = parser.codenum - 54 + subcode; + if (select_coordinate_system(_space)) { + SERIAL_PROTOCOLLNPAIR("Select workspace ", _space); + report_current_position(); + } + } + FORCE_INLINE void gcode_G54() { gcode_G54_59(); } + FORCE_INLINE void gcode_G55() { gcode_G54_59(); } + FORCE_INLINE void gcode_G56() { gcode_G54_59(); } + FORCE_INLINE void gcode_G57() { gcode_G54_59(); } + FORCE_INLINE void gcode_G58() { gcode_G54_59(); } + FORCE_INLINE void gcode_G59() { gcode_G54_59(parser.subcode); } + +#endif + +#if ENABLED(INCH_MODE_SUPPORT) + /** + * G20: Set input mode to inches + */ + inline void gcode_G20() { parser.set_input_linear_units(LINEARUNIT_INCH); } + + /** + * G21: Set input mode to millimeters + */ + inline void gcode_G21() { parser.set_input_linear_units(LINEARUNIT_MM); } +#endif + +#if ENABLED(NOZZLE_PARK_FEATURE) + /** + * G27: Park the nozzle + */ + inline void gcode_G27() { + // Don't allow nozzle parking without homing first + if (axis_unhomed_error()) return; + Nozzle::park(parser.ushortval('P')); + } +#endif // NOZZLE_PARK_FEATURE + +#if ENABLED(QUICK_HOME) + + static void quick_home_xy() { + + // Pretend the current position is 0,0 + current_position[X_AXIS] = current_position[Y_AXIS] = 0.0; + sync_plan_position(); + + const int x_axis_home_dir = + #if ENABLED(DUAL_X_CARRIAGE) + x_home_dir(active_extruder) + #else + home_dir(X_AXIS) + #endif + ; + + const float mlx = max_length(X_AXIS), + mly = max_length(Y_AXIS), + mlratio = mlx > mly ? mly / mlx : mlx / mly, + fr_mm_s = min(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0); + + do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * home_dir(Y_AXIS), fr_mm_s); + endstops.hit_on_purpose(); // clear endstop hit flags + current_position[X_AXIS] = current_position[Y_AXIS] = 0.0; + } + +#endif // QUICK_HOME + +#if ENABLED(DEBUG_LEVELING_FEATURE) + + void log_machine_info() { + SERIAL_ECHOPGM("Machine Type: "); + #if ENABLED(DELTA) + SERIAL_ECHOLNPGM("Delta"); + #elif IS_SCARA + SERIAL_ECHOLNPGM("SCARA"); + #elif IS_CORE + SERIAL_ECHOLNPGM("Core"); + #else + SERIAL_ECHOLNPGM("Cartesian"); + #endif + + SERIAL_ECHOPGM("Probe: "); + #if ENABLED(PROBE_MANUALLY) + SERIAL_ECHOLNPGM("PROBE_MANUALLY"); + #elif ENABLED(FIX_MOUNTED_PROBE) + SERIAL_ECHOLNPGM("FIX_MOUNTED_PROBE"); + #elif ENABLED(BLTOUCH) + SERIAL_ECHOLNPGM("BLTOUCH"); + #elif HAS_Z_SERVO_ENDSTOP + SERIAL_ECHOLNPGM("SERVO PROBE"); + #elif ENABLED(Z_PROBE_SLED) + SERIAL_ECHOLNPGM("Z_PROBE_SLED"); + #elif ENABLED(Z_PROBE_ALLEN_KEY) + SERIAL_ECHOLNPGM("Z_PROBE_ALLEN_KEY"); + #else + SERIAL_ECHOLNPGM("NONE"); + #endif + + #if HAS_BED_PROBE + SERIAL_ECHOPAIR("Probe Offset X:", X_PROBE_OFFSET_FROM_EXTRUDER); + SERIAL_ECHOPAIR(" Y:", Y_PROBE_OFFSET_FROM_EXTRUDER); + SERIAL_ECHOPAIR(" Z:", zprobe_zoffset); + #if X_PROBE_OFFSET_FROM_EXTRUDER > 0 + SERIAL_ECHOPGM(" (Right"); + #elif X_PROBE_OFFSET_FROM_EXTRUDER < 0 + SERIAL_ECHOPGM(" (Left"); + #elif Y_PROBE_OFFSET_FROM_EXTRUDER != 0 + SERIAL_ECHOPGM(" (Middle"); + #else + SERIAL_ECHOPGM(" (Aligned With"); + #endif + #if Y_PROBE_OFFSET_FROM_EXTRUDER > 0 + SERIAL_ECHOPGM("-Back"); + #elif Y_PROBE_OFFSET_FROM_EXTRUDER < 0 + SERIAL_ECHOPGM("-Front"); + #elif X_PROBE_OFFSET_FROM_EXTRUDER != 0 + SERIAL_ECHOPGM("-Center"); + #endif + if (zprobe_zoffset < 0) + SERIAL_ECHOPGM(" & Below"); + else if (zprobe_zoffset > 0) + SERIAL_ECHOPGM(" & Above"); + else + SERIAL_ECHOPGM(" & Same Z as"); + SERIAL_ECHOLNPGM(" Nozzle)"); + #endif + + #if HAS_ABL + SERIAL_ECHOPGM("Auto Bed Leveling: "); + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + SERIAL_ECHOPGM("LINEAR"); + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + SERIAL_ECHOPGM("BILINEAR"); + #elif ENABLED(AUTO_BED_LEVELING_3POINT) + SERIAL_ECHOPGM("3POINT"); + #elif ENABLED(AUTO_BED_LEVELING_UBL) + SERIAL_ECHOPGM("UBL"); + #endif + if (planner.leveling_active) { + SERIAL_ECHOLNPGM(" (enabled)"); + #if ABL_PLANAR + const float diff[XYZ] = { + stepper.get_axis_position_mm(X_AXIS) - current_position[X_AXIS], + stepper.get_axis_position_mm(Y_AXIS) - current_position[Y_AXIS], + stepper.get_axis_position_mm(Z_AXIS) - current_position[Z_AXIS] + }; + SERIAL_ECHOPGM("ABL Adjustment X"); + if (diff[X_AXIS] > 0) SERIAL_CHAR('+'); + SERIAL_ECHO(diff[X_AXIS]); + SERIAL_ECHOPGM(" Y"); + if (diff[Y_AXIS] > 0) SERIAL_CHAR('+'); + SERIAL_ECHO(diff[Y_AXIS]); + SERIAL_ECHOPGM(" Z"); + if (diff[Z_AXIS] > 0) SERIAL_CHAR('+'); + SERIAL_ECHO(diff[Z_AXIS]); + #elif ENABLED(AUTO_BED_LEVELING_UBL) + SERIAL_ECHOPAIR("UBL Adjustment Z", stepper.get_axis_position_mm(Z_AXIS) - current_position[Z_AXIS]); + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + SERIAL_ECHOPAIR("ABL Adjustment Z", bilinear_z_offset(current_position)); + #endif + } + else + SERIAL_ECHOLNPGM(" (disabled)"); + + SERIAL_EOL(); + + #elif ENABLED(MESH_BED_LEVELING) + + SERIAL_ECHOPGM("Mesh Bed Leveling"); + if (planner.leveling_active) { + float rz = current_position[Z_AXIS]; + planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], rz); + SERIAL_ECHOLNPGM(" (enabled)"); + SERIAL_ECHOPAIR("MBL Adjustment Z", rz); + } + else + SERIAL_ECHOPGM(" (disabled)"); + + SERIAL_EOL(); + + #endif // MESH_BED_LEVELING + } + +#endif // DEBUG_LEVELING_FEATURE + +#if ENABLED(DELTA) + + /** + * A delta can only safely home all axes at the same time + * This is like quick_home_xy() but for 3 towers. + */ + inline bool home_delta() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS(">>> home_delta", current_position); + #endif + // Init the current position of all carriages to 0,0,0 + ZERO(current_position); + sync_plan_position(); + + // Move all carriages together linearly until an endstop is hit. + current_position[X_AXIS] = current_position[Y_AXIS] = current_position[Z_AXIS] = (delta_height + 10); + feedrate_mm_s = homing_feedrate(X_AXIS); + buffer_line_to_current_position(); + stepper.synchronize(); + + // If an endstop was not hit, then damage can occur if homing is continued. + // This can occur if the delta height not set correctly. + if (!(Endstops::endstop_hit_bits & (_BV(X_MAX) | _BV(Y_MAX) | _BV(Z_MAX)))) { + LCD_MESSAGEPGM(MSG_ERR_HOMING_FAILED); + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_HOMING_FAILED); + return false; + } + + endstops.hit_on_purpose(); // clear endstop hit flags + + // At least one carriage has reached the top. + // Now re-home each carriage separately. + HOMEAXIS(A); + HOMEAXIS(B); + HOMEAXIS(C); + + // Set all carriages to their home positions + // Do this here all at once for Delta, because + // XYZ isn't ABC. Applying this per-tower would + // give the impression that they are the same. + LOOP_XYZ(i) set_axis_is_at_home((AxisEnum)i); + + SYNC_PLAN_POSITION_KINEMATIC(); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("<<< home_delta", current_position); + #endif + + return true; + } + +#endif // DELTA + +#if ENABLED(Z_SAFE_HOMING) + + inline void home_z_safely() { + + // Disallow Z homing if X or Y are unknown + if (!axis_known_position[X_AXIS] || !axis_known_position[Y_AXIS]) { + LCD_MESSAGEPGM(MSG_ERR_Z_HOMING); + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_ERR_Z_HOMING); + return; + } + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Z_SAFE_HOMING >>>"); + #endif + + SYNC_PLAN_POSITION_KINEMATIC(); + + /** + * Move the Z probe (or just the nozzle) to the safe homing point + */ + destination[X_AXIS] = Z_SAFE_HOMING_X_POINT; + destination[Y_AXIS] = Z_SAFE_HOMING_Y_POINT; + destination[Z_AXIS] = current_position[Z_AXIS]; // Z is already at the right height + + #if HOMING_Z_WITH_PROBE + destination[X_AXIS] -= X_PROBE_OFFSET_FROM_EXTRUDER; + destination[Y_AXIS] -= Y_PROBE_OFFSET_FROM_EXTRUDER; + #endif + + if (position_is_reachable(destination[X_AXIS], destination[Y_AXIS])) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("Z_SAFE_HOMING", destination); + #endif + + // This causes the carriage on Dual X to unpark + #if ENABLED(DUAL_X_CARRIAGE) + active_extruder_parked = false; + #endif + + do_blocking_move_to_xy(destination[X_AXIS], destination[Y_AXIS]); + HOMEAXIS(Z); + } + else { + LCD_MESSAGEPGM(MSG_ZPROBE_OUT); + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_ZPROBE_OUT); + } + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< Z_SAFE_HOMING"); + #endif + } + +#endif // Z_SAFE_HOMING + +#if ENABLED(PROBE_MANUALLY) + bool g29_in_progress = false; +#else + constexpr bool g29_in_progress = false; +#endif + +/** + * G28: Home all axes according to settings + * + * Parameters + * + * None Home to all axes with no parameters. + * With QUICK_HOME enabled XY will home together, then Z. + * + * Cartesian parameters + * + * X Home to the X endstop + * Y Home to the Y endstop + * Z Home to the Z endstop + * + */ +inline void gcode_G28(const bool always_home_all) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPGM(">>> gcode_G28"); + log_machine_info(); + } + #endif + + // Wait for planner moves to finish! + stepper.synchronize(); + + // Cancel the active G29 session + #if ENABLED(PROBE_MANUALLY) + g29_in_progress = false; + #endif + + // Disable the leveling matrix before homing + #if HAS_LEVELING + #if ENABLED(AUTO_BED_LEVELING_UBL) + const bool ubl_state_at_entry = planner.leveling_active; + #endif + set_bed_leveling_enabled(false); + #endif + + #if ENABLED(CNC_WORKSPACE_PLANES) + workspace_plane = PLANE_XY; + #endif + + // Always home with tool 0 active + #if HOTENDS > 1 + const uint8_t old_tool_index = active_extruder; + tool_change(0, 0, true); + #endif + + #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + extruder_duplication_enabled = false; + #endif + + setup_for_endstop_or_probe_move(); + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(true)"); + #endif + endstops.enable(true); // Enable endstops for next homing move + + #if ENABLED(DELTA) + + home_delta(); + UNUSED(always_home_all); + + #else // NOT DELTA + + const bool homeX = always_home_all || parser.seen('X'), + homeY = always_home_all || parser.seen('Y'), + homeZ = always_home_all || parser.seen('Z'), + home_all = (!homeX && !homeY && !homeZ) || (homeX && homeY && homeZ); + + set_destination_from_current(); + + #if Z_HOME_DIR > 0 // If homing away from BED do Z first + + if (home_all || homeZ) { + HOMEAXIS(Z); + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> HOMEAXIS(Z)", current_position); + #endif + } + + #else + + if (home_all || homeX || homeY) { + // Raise Z before homing any other axes and z is not already high enough (never lower z) + destination[Z_AXIS] = Z_HOMING_HEIGHT; + if (destination[Z_AXIS] > current_position[Z_AXIS]) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) + SERIAL_ECHOLNPAIR("Raise Z (before homing) to ", destination[Z_AXIS]); + #endif + + do_blocking_move_to_z(destination[Z_AXIS]); + } + } + + #endif + + #if ENABLED(QUICK_HOME) + + if (home_all || (homeX && homeY)) quick_home_xy(); + + #endif + + #if ENABLED(HOME_Y_BEFORE_X) + + // Home Y + if (home_all || homeY) { + HOMEAXIS(Y); + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> homeY", current_position); + #endif + } + + #endif + + // Home X + if (home_all || homeX) { + + #if ENABLED(DUAL_X_CARRIAGE) + + // Always home the 2nd (right) extruder first + active_extruder = 1; + HOMEAXIS(X); + + // Remember this extruder's position for later tool change + inactive_extruder_x_pos = current_position[X_AXIS]; + + // Home the 1st (left) extruder + active_extruder = 0; + HOMEAXIS(X); + + // Consider the active extruder to be parked + COPY(raised_parked_position, current_position); + delayed_move_time = 0; + active_extruder_parked = true; + + #else + + HOMEAXIS(X); + + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> homeX", current_position); + #endif + } + + #if DISABLED(HOME_Y_BEFORE_X) + // Home Y + if (home_all || homeY) { + HOMEAXIS(Y); + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> homeY", current_position); + #endif + } + #endif + + // Home Z last if homing towards the bed + #if Z_HOME_DIR < 0 + if (home_all || homeZ) { + #if ENABLED(Z_SAFE_HOMING) + home_z_safely(); + #else + HOMEAXIS(Z); + #endif + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> (home_all || homeZ) > final", current_position); + #endif + } // home_all || homeZ + #endif // Z_HOME_DIR < 0 + + SYNC_PLAN_POSITION_KINEMATIC(); + + #endif // !DELTA (gcode_G28) + + endstops.not_homing(); + + #if ENABLED(DELTA) && ENABLED(DELTA_HOME_TO_SAFE_ZONE) + // move to a height where we can use the full xy-area + do_blocking_move_to_z(delta_clip_start_height); + #endif + + #if ENABLED(AUTO_BED_LEVELING_UBL) + set_bed_leveling_enabled(ubl_state_at_entry); + #endif + + clean_up_after_endstop_or_probe_move(); + + // Restore the active tool after homing + #if HOTENDS > 1 + #if ENABLED(PARKING_EXTRUDER) + #define NO_FETCH false // fetch the previous toolhead + #else + #define NO_FETCH true + #endif + tool_change(old_tool_index, 0, NO_FETCH); + #endif + + lcd_refresh(); + + report_current_position(); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< gcode_G28"); + #endif +} // G28 + +void home_all_axes() { gcode_G28(true); } + +#if HAS_PROBING_PROCEDURE + + void out_of_range_error(const char* p_edge) { + SERIAL_PROTOCOLPGM("?Probe "); + serialprintPGM(p_edge); + SERIAL_PROTOCOLLNPGM(" position out of range."); + } + +#endif + +#if ENABLED(MESH_BED_LEVELING) || ENABLED(PROBE_MANUALLY) + + #if ENABLED(PROBE_MANUALLY) && ENABLED(LCD_BED_LEVELING) + extern bool lcd_wait_for_move; + #endif + + inline void _manual_goto_xy(const float &rx, const float &ry) { + + #if MANUAL_PROBE_HEIGHT > 0 + const float prev_z = current_position[Z_AXIS]; + do_blocking_move_to_z(MANUAL_PROBE_HEIGHT, homing_feedrate(Z_AXIS)); + #endif + + do_blocking_move_to_xy(rx, ry, MMM_TO_MMS(XY_PROBE_SPEED)); + + #if MANUAL_PROBE_HEIGHT > 0 + do_blocking_move_to_z(prev_z, homing_feedrate(Z_AXIS)); + #endif + + current_position[X_AXIS] = rx; + current_position[Y_AXIS] = ry; + + #if ENABLED(PROBE_MANUALLY) && ENABLED(LCD_BED_LEVELING) + lcd_wait_for_move = false; + #endif + } + +#endif + +#if ENABLED(MESH_BED_LEVELING) + + // Save 130 bytes with non-duplication of PSTR + void echo_not_entered() { SERIAL_PROTOCOLLNPGM(" not entered."); } + + void mbl_mesh_report() { + SERIAL_PROTOCOLLNPGM("Num X,Y: " STRINGIFY(GRID_MAX_POINTS_X) "," STRINGIFY(GRID_MAX_POINTS_Y)); + SERIAL_PROTOCOLPGM("Z offset: "); SERIAL_PROTOCOL_F(mbl.z_offset, 5); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 5, + [](const uint8_t ix, const uint8_t iy) { return mbl.z_values[ix][iy]; } + ); + } + + void mesh_probing_done() { + mbl.has_mesh = true; + home_all_axes(); + set_bed_leveling_enabled(true); + #if ENABLED(MESH_G28_REST_ORIGIN) + current_position[Z_AXIS] = Z_MIN_POS; + set_destination_from_current(); + buffer_line_to_destination(homing_feedrate(Z_AXIS)); + stepper.synchronize(); + #endif + } + + /** + * G29: Mesh-based Z probe, probes a grid and produces a + * mesh to compensate for variable bed height + * + * Parameters With MESH_BED_LEVELING: + * + * S0 Produce a mesh report + * S1 Start probing mesh points + * S2 Probe the next mesh point + * S3 Xn Yn Zn.nn Manually modify a single point + * S4 Zn.nn Set z offset. Positive away from bed, negative closer to bed. + * S5 Reset and disable mesh + * + * The S0 report the points as below + * + * +----> X-axis 1-n + * | + * | + * v Y-axis 1-n + * + */ + inline void gcode_G29() { + + static int mbl_probe_index = -1; + #if HAS_SOFTWARE_ENDSTOPS + static bool enable_soft_endstops; + #endif + + const MeshLevelingState state = (MeshLevelingState)parser.byteval('S', (int8_t)MeshReport); + if (!WITHIN(state, 0, 5)) { + SERIAL_PROTOCOLLNPGM("S out of range (0-5)."); + return; + } + + int8_t px, py; + + switch (state) { + case MeshReport: + if (leveling_is_valid()) { + SERIAL_PROTOCOLLNPAIR("State: ", planner.leveling_active ? MSG_ON : MSG_OFF); + mbl_mesh_report(); + } + else + SERIAL_PROTOCOLLNPGM("Mesh bed leveling has no data."); + break; + + case MeshStart: + mbl.reset(); + mbl_probe_index = 0; + enqueue_and_echo_commands_P(PSTR("G28\nG29 S2")); + break; + + case MeshNext: + if (mbl_probe_index < 0) { + SERIAL_PROTOCOLLNPGM("Start mesh probing with \"G29 S1\" first."); + return; + } + // For each G29 S2... + if (mbl_probe_index == 0) { + #if HAS_SOFTWARE_ENDSTOPS + // For the initial G29 S2 save software endstop state + enable_soft_endstops = soft_endstops_enabled; + #endif + } + else { + // For G29 S2 after adjusting Z. + mbl.set_zigzag_z(mbl_probe_index - 1, current_position[Z_AXIS]); + #if HAS_SOFTWARE_ENDSTOPS + soft_endstops_enabled = enable_soft_endstops; + #endif + } + // If there's another point to sample, move there with optional lift. + if (mbl_probe_index < GRID_MAX_POINTS) { + mbl.zigzag(mbl_probe_index, px, py); + _manual_goto_xy(mbl.index_to_xpos[px], mbl.index_to_ypos[py]); + + #if HAS_SOFTWARE_ENDSTOPS + // Disable software endstops to allow manual adjustment + // If G29 is not completed, they will not be re-enabled + soft_endstops_enabled = false; + #endif + + mbl_probe_index++; + } + else { + // One last "return to the bed" (as originally coded) at completion + current_position[Z_AXIS] = Z_MIN_POS + MANUAL_PROBE_HEIGHT; + buffer_line_to_current_position(); + stepper.synchronize(); + + // After recording the last point, activate home and activate + mbl_probe_index = -1; + SERIAL_PROTOCOLLNPGM("Mesh probing done."); + BUZZ(100, 659); + BUZZ(100, 698); + mesh_probing_done(); + } + break; + + case MeshSet: + if (parser.seenval('X')) { + px = parser.value_int() - 1; + if (!WITHIN(px, 0, GRID_MAX_POINTS_X - 1)) { + SERIAL_PROTOCOLLNPGM("X out of range (1-" STRINGIFY(GRID_MAX_POINTS_X) ")."); + return; + } + } + else { + SERIAL_CHAR('X'); echo_not_entered(); + return; + } + + if (parser.seenval('Y')) { + py = parser.value_int() - 1; + if (!WITHIN(py, 0, GRID_MAX_POINTS_Y - 1)) { + SERIAL_PROTOCOLLNPGM("Y out of range (1-" STRINGIFY(GRID_MAX_POINTS_Y) ")."); + return; + } + } + else { + SERIAL_CHAR('Y'); echo_not_entered(); + return; + } + + if (parser.seenval('Z')) { + mbl.z_values[px][py] = parser.value_linear_units(); + } + else { + SERIAL_CHAR('Z'); echo_not_entered(); + return; + } + break; + + case MeshSetZOffset: + if (parser.seenval('Z')) { + mbl.z_offset = parser.value_linear_units(); + } + else { + SERIAL_CHAR('Z'); echo_not_entered(); + return; + } + break; + + case MeshReset: + reset_bed_level(); + break; + + } // switch(state) + + report_current_position(); + } + +#elif OLDSCHOOL_ABL + + #if ABL_GRID + #if ENABLED(PROBE_Y_FIRST) + #define PR_OUTER_VAR xCount + #define PR_OUTER_END abl_grid_points_x + #define PR_INNER_VAR yCount + #define PR_INNER_END abl_grid_points_y + #else + #define PR_OUTER_VAR yCount + #define PR_OUTER_END abl_grid_points_y + #define PR_INNER_VAR xCount + #define PR_INNER_END abl_grid_points_x + #endif + #endif + + /** + * G29: Detailed Z probe, probes the bed at 3 or more points. + * Will fail if the printer has not been homed with G28. + * + * Enhanced G29 Auto Bed Leveling Probe Routine + * + * D Dry-Run mode. Just evaluate the bed Topology - Don't apply + * or alter the bed level data. Useful to check the topology + * after a first run of G29. + * + * J Jettison current bed leveling data + * + * V Set the verbose level (0-4). Example: "G29 V3" + * + * Parameters With LINEAR leveling only: + * + * P Set the size of the grid that will be probed (P x P points). + * Example: "G29 P4" + * + * X Set the X size of the grid that will be probed (X x Y points). + * Example: "G29 X7 Y5" + * + * Y Set the Y size of the grid that will be probed (X x Y points). + * + * T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report. + * This is useful for manual bed leveling and finding flaws in the bed (to + * assist with part placement). + * Not supported by non-linear delta printer bed leveling. + * + * Parameters With LINEAR and BILINEAR leveling only: + * + * S Set the XY travel speed between probe points (in units/min) + * + * F Set the Front limit of the probing grid + * B Set the Back limit of the probing grid + * L Set the Left limit of the probing grid + * R Set the Right limit of the probing grid + * + * Parameters with DEBUG_LEVELING_FEATURE only: + * + * C Make a totally fake grid with no actual probing. + * For use in testing when no probing is possible. + * + * Parameters with BILINEAR leveling only: + * + * Z Supply an additional Z probe offset + * + * Extra parameters with PROBE_MANUALLY: + * + * To do manual probing simply repeat G29 until the procedure is complete. + * The first G29 accepts parameters. 'G29 Q' for status, 'G29 A' to abort. + * + * Q Query leveling and G29 state + * + * A Abort current leveling procedure + * + * Extra parameters with BILINEAR only: + * + * W Write a mesh point. (If G29 is idle.) + * I X index for mesh point + * J Y index for mesh point + * X X for mesh point, overrides I + * Y Y for mesh point, overrides J + * Z Z for mesh point. Otherwise, raw current Z. + * + * Without PROBE_MANUALLY: + * + * E By default G29 will engage the Z probe, test the bed, then disengage. + * Include "E" to engage/disengage the Z probe for each sample. + * There's no extra effect if you have a fixed Z probe. + * + */ + inline void gcode_G29() { + + // G29 Q is also available if debugging + #if ENABLED(DEBUG_LEVELING_FEATURE) + const bool query = parser.seen('Q'); + const uint8_t old_debug_flags = marlin_debug_flags; + if (query) marlin_debug_flags |= DEBUG_LEVELING; + if (DEBUGGING(LEVELING)) { + DEBUG_POS(">>> gcode_G29", current_position); + log_machine_info(); + } + marlin_debug_flags = old_debug_flags; + #if DISABLED(PROBE_MANUALLY) + if (query) return; + #endif + #endif + + #if ENABLED(PROBE_MANUALLY) + const bool seenA = parser.seen('A'), seenQ = parser.seen('Q'), no_action = seenA || seenQ; + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) && DISABLED(PROBE_MANUALLY) + const bool faux = parser.boolval('C'); + #elif ENABLED(PROBE_MANUALLY) + const bool faux = no_action; + #else + bool constexpr faux = false; + #endif + + // Don't allow auto-leveling without homing first + if (axis_unhomed_error()) return; + + // Define local vars 'static' for manual probing, 'auto' otherwise + #if ENABLED(PROBE_MANUALLY) + #define ABL_VAR static + #else + #define ABL_VAR + #endif + + ABL_VAR int verbose_level; + ABL_VAR float xProbe, yProbe, measured_z; + ABL_VAR bool dryrun, abl_should_enable; + + #if ENABLED(PROBE_MANUALLY) || ENABLED(AUTO_BED_LEVELING_LINEAR) + ABL_VAR int abl_probe_index; + #endif + + #if HAS_SOFTWARE_ENDSTOPS && ENABLED(PROBE_MANUALLY) + ABL_VAR bool enable_soft_endstops = true; + #endif + + #if ABL_GRID + + #if ENABLED(PROBE_MANUALLY) + ABL_VAR uint8_t PR_OUTER_VAR; + ABL_VAR int8_t PR_INNER_VAR; + #endif + + ABL_VAR int left_probe_bed_position, right_probe_bed_position, front_probe_bed_position, back_probe_bed_position; + ABL_VAR float xGridSpacing = 0, yGridSpacing = 0; + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + ABL_VAR uint8_t abl_grid_points_x = GRID_MAX_POINTS_X, + abl_grid_points_y = GRID_MAX_POINTS_Y; + ABL_VAR bool do_topography_map; + #else // Bilinear + uint8_t constexpr abl_grid_points_x = GRID_MAX_POINTS_X, + abl_grid_points_y = GRID_MAX_POINTS_Y; + #endif + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(PROBE_MANUALLY) + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + ABL_VAR int abl2; + #else // Bilinear + int constexpr abl2 = GRID_MAX_POINTS; + #endif + #endif + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + ABL_VAR float zoffset; + + #elif ENABLED(AUTO_BED_LEVELING_LINEAR) + + ABL_VAR int indexIntoAB[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y]; + + ABL_VAR float eqnAMatrix[GRID_MAX_POINTS * 3], // "A" matrix of the linear system of equations + eqnBVector[GRID_MAX_POINTS], // "B" vector of Z points + mean; + #endif + + #elif ENABLED(AUTO_BED_LEVELING_3POINT) + + int constexpr abl2 = 3; + + // Probe at 3 arbitrary points + ABL_VAR vector_3 points[3] = { + vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, 0), + vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, 0), + vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, 0) + }; + + #endif // AUTO_BED_LEVELING_3POINT + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + struct linear_fit_data lsf_results; + incremental_LSF_reset(&lsf_results); + #endif + + /** + * On the initial G29 fetch command parameters. + */ + if (!g29_in_progress) { + + #if ENABLED(PROBE_MANUALLY) || ENABLED(AUTO_BED_LEVELING_LINEAR) + abl_probe_index = -1; + #endif + + abl_should_enable = planner.leveling_active; + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + if (parser.seen('W')) { + if (!leveling_is_valid()) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("No bilinear grid"); + return; + } + + const float rz = parser.seenval('Z') ? RAW_Z_POSITION(parser.value_linear_units()) : current_position[Z_AXIS]; + if (!WITHIN(rz, -10, 10)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Bad Z value"); + return; + } + + const float rx = RAW_X_POSITION(parser.linearval('X', NAN)), + ry = RAW_Y_POSITION(parser.linearval('Y', NAN)); + int8_t i = parser.byteval('I', -1), + j = parser.byteval('J', -1); + + if (!isnan(rx) && !isnan(ry)) { + // Get nearest i / j from x / y + i = (rx - bilinear_start[X_AXIS] + 0.5 * xGridSpacing) / xGridSpacing; + j = (ry - bilinear_start[Y_AXIS] + 0.5 * yGridSpacing) / yGridSpacing; + i = constrain(i, 0, GRID_MAX_POINTS_X - 1); + j = constrain(j, 0, GRID_MAX_POINTS_Y - 1); + } + if (WITHIN(i, 0, GRID_MAX_POINTS_X - 1) && WITHIN(j, 0, GRID_MAX_POINTS_Y)) { + set_bed_leveling_enabled(false); + z_values[i][j] = rz; + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + bed_level_virt_interpolate(); + #endif + set_bed_leveling_enabled(abl_should_enable); + } + return; + } // parser.seen('W') + + #endif + + #if HAS_LEVELING + + // Jettison bed leveling data + if (parser.seen('J')) { + reset_bed_level(); + return; + } + + #endif + + verbose_level = parser.intval('V'); + if (!WITHIN(verbose_level, 0, 4)) { + SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0-4)."); + return; + } + + dryrun = parser.boolval('D') + #if ENABLED(PROBE_MANUALLY) + || no_action + #endif + ; + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + + do_topography_map = verbose_level > 2 || parser.boolval('T'); + + // X and Y specify points in each direction, overriding the default + // These values may be saved with the completed mesh + abl_grid_points_x = parser.intval('X', GRID_MAX_POINTS_X); + abl_grid_points_y = parser.intval('Y', GRID_MAX_POINTS_Y); + if (parser.seenval('P')) abl_grid_points_x = abl_grid_points_y = parser.value_int(); + + if (abl_grid_points_x < 2 || abl_grid_points_y < 2) { + SERIAL_PROTOCOLLNPGM("?Number of probe points is implausible (2 minimum)."); + return; + } + + abl2 = abl_grid_points_x * abl_grid_points_y; + + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + + zoffset = parser.linearval('Z'); + + #endif + + #if ABL_GRID + + xy_probe_feedrate_mm_s = MMM_TO_MMS(parser.linearval('S', XY_PROBE_SPEED)); + + left_probe_bed_position = parser.seenval('L') ? (int)RAW_X_POSITION(parser.value_linear_units()) : LEFT_PROBE_BED_POSITION; + right_probe_bed_position = parser.seenval('R') ? (int)RAW_X_POSITION(parser.value_linear_units()) : RIGHT_PROBE_BED_POSITION; + front_probe_bed_position = parser.seenval('F') ? (int)RAW_Y_POSITION(parser.value_linear_units()) : FRONT_PROBE_BED_POSITION; + back_probe_bed_position = parser.seenval('B') ? (int)RAW_Y_POSITION(parser.value_linear_units()) : BACK_PROBE_BED_POSITION; + + const bool left_out_l = left_probe_bed_position < MIN_PROBE_X, + left_out = left_out_l || left_probe_bed_position > right_probe_bed_position - (MIN_PROBE_EDGE), + right_out_r = right_probe_bed_position > MAX_PROBE_X, + right_out = right_out_r || right_probe_bed_position < left_probe_bed_position + MIN_PROBE_EDGE, + front_out_f = front_probe_bed_position < MIN_PROBE_Y, + front_out = front_out_f || front_probe_bed_position > back_probe_bed_position - (MIN_PROBE_EDGE), + back_out_b = back_probe_bed_position > MAX_PROBE_Y, + back_out = back_out_b || back_probe_bed_position < front_probe_bed_position + MIN_PROBE_EDGE; + + if (left_out || right_out || front_out || back_out) { + if (left_out) { + out_of_range_error(PSTR("(L)eft")); + left_probe_bed_position = left_out_l ? MIN_PROBE_X : right_probe_bed_position - (MIN_PROBE_EDGE); + } + if (right_out) { + out_of_range_error(PSTR("(R)ight")); + right_probe_bed_position = right_out_r ? MAX_PROBE_X : left_probe_bed_position + MIN_PROBE_EDGE; + } + if (front_out) { + out_of_range_error(PSTR("(F)ront")); + front_probe_bed_position = front_out_f ? MIN_PROBE_Y : back_probe_bed_position - (MIN_PROBE_EDGE); + } + if (back_out) { + out_of_range_error(PSTR("(B)ack")); + back_probe_bed_position = back_out_b ? MAX_PROBE_Y : front_probe_bed_position + MIN_PROBE_EDGE; + } + return; + } + + // probe at the points of a lattice grid + xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (abl_grid_points_x - 1); + yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (abl_grid_points_y - 1); + + #endif // ABL_GRID + + if (verbose_level > 0) { + SERIAL_PROTOCOLLNPGM("G29 Auto Bed Leveling"); + if (dryrun) SERIAL_PROTOCOLLNPGM("Running in DRY-RUN mode"); + } + + stepper.synchronize(); + + // Disable auto bed leveling during G29 + planner.leveling_active = false; + + if (!dryrun) { + // Re-orient the current position without leveling + // based on where the steppers are positioned. + set_current_from_steppers_for_axis(ALL_AXES); + + // Sync the planner to where the steppers stopped + SYNC_PLAN_POSITION_KINEMATIC(); + } + + #if HAS_BED_PROBE + // Deploy the probe. Probe will raise if needed. + if (DEPLOY_PROBE()) { + planner.leveling_active = abl_should_enable; + return; + } + #endif + + if (!faux) setup_for_endstop_or_probe_move(); + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + #if ENABLED(PROBE_MANUALLY) + if (!no_action) + #endif + if ( xGridSpacing != bilinear_grid_spacing[X_AXIS] + || yGridSpacing != bilinear_grid_spacing[Y_AXIS] + || left_probe_bed_position != bilinear_start[X_AXIS] + || front_probe_bed_position != bilinear_start[Y_AXIS] + ) { + if (dryrun) { + // Before reset bed level, re-enable to correct the position + planner.leveling_active = abl_should_enable; + } + // Reset grid to 0.0 or "not probed". (Also disables ABL) + reset_bed_level(); + + // Initialize a grid with the given dimensions + bilinear_grid_spacing[X_AXIS] = xGridSpacing; + bilinear_grid_spacing[Y_AXIS] = yGridSpacing; + bilinear_start[X_AXIS] = left_probe_bed_position; + bilinear_start[Y_AXIS] = front_probe_bed_position; + + // Can't re-enable (on error) until the new grid is written + abl_should_enable = false; + } + + #endif // AUTO_BED_LEVELING_BILINEAR + + #if ENABLED(AUTO_BED_LEVELING_3POINT) + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> 3-point Leveling"); + #endif + + // Probe at 3 arbitrary points + points[0].z = points[1].z = points[2].z = 0; + + #endif // AUTO_BED_LEVELING_3POINT + + } // !g29_in_progress + + #if ENABLED(PROBE_MANUALLY) + + // For manual probing, get the next index to probe now. + // On the first probe this will be incremented to 0. + if (!no_action) { + ++abl_probe_index; + g29_in_progress = true; + } + + // Abort current G29 procedure, go back to idle state + if (seenA && g29_in_progress) { + SERIAL_PROTOCOLLNPGM("Manual G29 aborted"); + #if HAS_SOFTWARE_ENDSTOPS + soft_endstops_enabled = enable_soft_endstops; + #endif + planner.leveling_active = abl_should_enable; + g29_in_progress = false; + #if ENABLED(LCD_BED_LEVELING) + lcd_wait_for_move = false; + #endif + } + + // Query G29 status + if (verbose_level || seenQ) { + SERIAL_PROTOCOLPGM("Manual G29 "); + if (g29_in_progress) { + SERIAL_PROTOCOLPAIR("point ", min(abl_probe_index + 1, abl2)); + SERIAL_PROTOCOLLNPAIR(" of ", abl2); + } + else + SERIAL_PROTOCOLLNPGM("idle"); + } + + if (no_action) return; + + if (abl_probe_index == 0) { + // For the initial G29 save software endstop state + #if HAS_SOFTWARE_ENDSTOPS + enable_soft_endstops = soft_endstops_enabled; + #endif + } + else { + // For G29 after adjusting Z. + // Save the previous Z before going to the next point + measured_z = current_position[Z_AXIS]; + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + + mean += measured_z; + eqnBVector[abl_probe_index] = measured_z; + eqnAMatrix[abl_probe_index + 0 * abl2] = xProbe; + eqnAMatrix[abl_probe_index + 1 * abl2] = yProbe; + eqnAMatrix[abl_probe_index + 2 * abl2] = 1; + + incremental_LSF(&lsf_results, xProbe, yProbe, measured_z); + + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + + z_values[xCount][yCount] = measured_z + zoffset; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_PROTOCOLPAIR("Save X", xCount); + SERIAL_PROTOCOLPAIR(" Y", yCount); + SERIAL_PROTOCOLLNPAIR(" Z", measured_z + zoffset); + } + #endif + + #elif ENABLED(AUTO_BED_LEVELING_3POINT) + + points[abl_probe_index].z = measured_z; + + #endif + } + + // + // If there's another point to sample, move there with optional lift. + // + + #if ABL_GRID + + // Skip any unreachable points + while (abl_probe_index < abl2) { + + // Set xCount, yCount based on abl_probe_index, with zig-zag + PR_OUTER_VAR = abl_probe_index / PR_INNER_END; + PR_INNER_VAR = abl_probe_index - (PR_OUTER_VAR * PR_INNER_END); + + // Probe in reverse order for every other row/column + bool zig = (PR_OUTER_VAR & 1); // != ((PR_OUTER_END) & 1); + + if (zig) PR_INNER_VAR = (PR_INNER_END - 1) - PR_INNER_VAR; + + const float xBase = xCount * xGridSpacing + left_probe_bed_position, + yBase = yCount * yGridSpacing + front_probe_bed_position; + + xProbe = FLOOR(xBase + (xBase < 0 ? 0 : 0.5)); + yProbe = FLOOR(yBase + (yBase < 0 ? 0 : 0.5)); + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + indexIntoAB[xCount][yCount] = abl_probe_index; + #endif + + // Keep looping till a reachable point is found + if (position_is_reachable(xProbe, yProbe)) break; + ++abl_probe_index; + } + + // Is there a next point to move to? + if (abl_probe_index < abl2) { + _manual_goto_xy(xProbe, yProbe); // Can be used here too! + #if HAS_SOFTWARE_ENDSTOPS + // Disable software endstops to allow manual adjustment + // If G29 is not completed, they will not be re-enabled + soft_endstops_enabled = false; + #endif + return; + } + else { + + // Leveling done! Fall through to G29 finishing code below + + SERIAL_PROTOCOLLNPGM("Grid probing done."); + + // Re-enable software endstops, if needed + #if HAS_SOFTWARE_ENDSTOPS + soft_endstops_enabled = enable_soft_endstops; + #endif + } + + #elif ENABLED(AUTO_BED_LEVELING_3POINT) + + // Probe at 3 arbitrary points + if (abl_probe_index < 3) { + xProbe = points[abl_probe_index].x; + yProbe = points[abl_probe_index].y; + #if HAS_SOFTWARE_ENDSTOPS + // Disable software endstops to allow manual adjustment + // If G29 is not completed, they will not be re-enabled + soft_endstops_enabled = false; + #endif + return; + } + else { + + SERIAL_PROTOCOLLNPGM("3-point probing done."); + + // Re-enable software endstops, if needed + #if HAS_SOFTWARE_ENDSTOPS + soft_endstops_enabled = enable_soft_endstops; + #endif + + if (!dryrun) { + vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal(); + if (planeNormal.z < 0) { + planeNormal.x *= -1; + planeNormal.y *= -1; + planeNormal.z *= -1; + } + planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal); + + // Can't re-enable (on error) until the new grid is written + abl_should_enable = false; + } + + } + + #endif // AUTO_BED_LEVELING_3POINT + + #else // !PROBE_MANUALLY + { + const bool stow_probe_after_each = parser.boolval('E'); + + #if ABL_GRID + + bool zig = PR_OUTER_END & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION + + measured_z = 0; + + // Outer loop is Y with PROBE_Y_FIRST disabled + for (uint8_t PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_END && !isnan(measured_z); PR_OUTER_VAR++) { + + int8_t inStart, inStop, inInc; + + if (zig) { // away from origin + inStart = 0; + inStop = PR_INNER_END; + inInc = 1; + } + else { // towards origin + inStart = PR_INNER_END - 1; + inStop = -1; + inInc = -1; + } + + zig ^= true; // zag + + // Inner loop is Y with PROBE_Y_FIRST enabled + for (int8_t PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; PR_INNER_VAR += inInc) { + + float xBase = left_probe_bed_position + xGridSpacing * xCount, + yBase = front_probe_bed_position + yGridSpacing * yCount; + + xProbe = FLOOR(xBase + (xBase < 0 ? 0 : 0.5)); + yProbe = FLOOR(yBase + (yBase < 0 ? 0 : 0.5)); + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + indexIntoAB[xCount][yCount] = ++abl_probe_index; // 0... + #endif + + #if IS_KINEMATIC + // Avoid probing outside the round or hexagonal area + if (!position_is_reachable_by_probe(xProbe, yProbe)) continue; + #endif + + measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level); + + if (isnan(measured_z)) { + planner.leveling_active = abl_should_enable; + break; + } + + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + + mean += measured_z; + eqnBVector[abl_probe_index] = measured_z; + eqnAMatrix[abl_probe_index + 0 * abl2] = xProbe; + eqnAMatrix[abl_probe_index + 1 * abl2] = yProbe; + eqnAMatrix[abl_probe_index + 2 * abl2] = 1; + + incremental_LSF(&lsf_results, xProbe, yProbe, measured_z); + + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + + z_values[xCount][yCount] = measured_z + zoffset; + + #endif + + abl_should_enable = false; + idle(); + + } // inner + } // outer + + #elif ENABLED(AUTO_BED_LEVELING_3POINT) + + // Probe at 3 arbitrary points + + for (uint8_t i = 0; i < 3; ++i) { + // Retain the last probe position + xProbe = points[i].x; + yProbe = points[i].y; + measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level); + if (isnan(measured_z)) { + planner.leveling_active = abl_should_enable; + break; + } + points[i].z = measured_z; + } + + if (!dryrun && !isnan(measured_z)) { + vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal(); + if (planeNormal.z < 0) { + planeNormal.x *= -1; + planeNormal.y *= -1; + planeNormal.z *= -1; + } + planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal); + + // Can't re-enable (on error) until the new grid is written + abl_should_enable = false; + } + + #endif // AUTO_BED_LEVELING_3POINT + + // Raise to _Z_CLEARANCE_DEPLOY_PROBE. Stow the probe. + if (STOW_PROBE()) { + planner.leveling_active = abl_should_enable; + measured_z = NAN; + } + } + #endif // !PROBE_MANUALLY + + // + // G29 Finishing Code + // + // Unless this is a dry run, auto bed leveling will + // definitely be enabled after this point. + // + // If code above wants to continue leveling, it should + // return or loop before this point. + // + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("> probing complete", current_position); + #endif + + #if ENABLED(PROBE_MANUALLY) + g29_in_progress = false; + #if ENABLED(LCD_BED_LEVELING) + lcd_wait_for_move = false; + #endif + #endif + + // Calculate leveling, print reports, correct the position + if (!isnan(measured_z)) { + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + if (!dryrun) extrapolate_unprobed_bed_level(); + print_bilinear_leveling_grid(); + + refresh_bed_level(); + + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + print_bilinear_leveling_grid_virt(); + #endif + + #elif ENABLED(AUTO_BED_LEVELING_LINEAR) + + // For LINEAR leveling calculate matrix, print reports, correct the position + + /** + * solve the plane equation ax + by + d = z + * A is the matrix with rows [x y 1] for all the probed points + * B is the vector of the Z positions + * the normal vector to the plane is formed by the coefficients of the + * plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0 + * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z + */ + float plane_equation_coefficients[3]; + + finish_incremental_LSF(&lsf_results); + plane_equation_coefficients[0] = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below + plane_equation_coefficients[1] = -lsf_results.B; // but that is not yet tested. + plane_equation_coefficients[2] = -lsf_results.D; + + mean /= abl2; + + if (verbose_level) { + SERIAL_PROTOCOLPGM("Eqn coefficients: a: "); + SERIAL_PROTOCOL_F(plane_equation_coefficients[0], 8); + SERIAL_PROTOCOLPGM(" b: "); + SERIAL_PROTOCOL_F(plane_equation_coefficients[1], 8); + SERIAL_PROTOCOLPGM(" d: "); + SERIAL_PROTOCOL_F(plane_equation_coefficients[2], 8); + SERIAL_EOL(); + if (verbose_level > 2) { + SERIAL_PROTOCOLPGM("Mean of sampled points: "); + SERIAL_PROTOCOL_F(mean, 8); + SERIAL_EOL(); + } + } + + // Create the matrix but don't correct the position yet + if (!dryrun) + planner.bed_level_matrix = matrix_3x3::create_look_at( + vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1) // We can eliminate the '-' here and up above + ); + + // Show the Topography map if enabled + if (do_topography_map) { + + SERIAL_PROTOCOLLNPGM("\nBed Height Topography:\n" + " +--- BACK --+\n" + " | |\n" + " L | (+) | R\n" + " E | | I\n" + " F | (-) N (+) | G\n" + " T | | H\n" + " | (-) | T\n" + " | |\n" + " O-- FRONT --+\n" + " (0,0)"); + + float min_diff = 999; + + for (int8_t yy = abl_grid_points_y - 1; yy >= 0; yy--) { + for (uint8_t xx = 0; xx < abl_grid_points_x; xx++) { + int ind = indexIntoAB[xx][yy]; + float diff = eqnBVector[ind] - mean, + x_tmp = eqnAMatrix[ind + 0 * abl2], + y_tmp = eqnAMatrix[ind + 1 * abl2], + z_tmp = 0; + + apply_rotation_xyz(planner.bed_level_matrix, x_tmp, y_tmp, z_tmp); + + NOMORE(min_diff, eqnBVector[ind] - z_tmp); + + if (diff >= 0.0) + SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment + else + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOL_F(diff, 5); + } // xx + SERIAL_EOL(); + } // yy + SERIAL_EOL(); + + if (verbose_level > 3) { + SERIAL_PROTOCOLLNPGM("\nCorrected Bed Height vs. Bed Topology:"); + + for (int8_t yy = abl_grid_points_y - 1; yy >= 0; yy--) { + for (uint8_t xx = 0; xx < abl_grid_points_x; xx++) { + int ind = indexIntoAB[xx][yy]; + float x_tmp = eqnAMatrix[ind + 0 * abl2], + y_tmp = eqnAMatrix[ind + 1 * abl2], + z_tmp = 0; + + apply_rotation_xyz(planner.bed_level_matrix, x_tmp, y_tmp, z_tmp); + + float diff = eqnBVector[ind] - z_tmp - min_diff; + if (diff >= 0.0) + SERIAL_PROTOCOLPGM(" +"); + // Include + for column alignment + else + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOL_F(diff, 5); + } // xx + SERIAL_EOL(); + } // yy + SERIAL_EOL(); + } + } //do_topography_map + + #endif // AUTO_BED_LEVELING_LINEAR + + #if ABL_PLANAR + + // For LINEAR and 3POINT leveling correct the current position + + if (verbose_level > 0) + planner.bed_level_matrix.debug(PSTR("\n\nBed Level Correction Matrix:")); + + if (!dryrun) { + // + // Correct the current XYZ position based on the tilted plane. + // + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position); + #endif + + float converted[XYZ]; + COPY(converted, current_position); + + planner.leveling_active = true; + planner.unapply_leveling(converted); // use conversion machinery + planner.leveling_active = false; + + // Use the last measured distance to the bed, if possible + if ( NEAR(current_position[X_AXIS], xProbe - (X_PROBE_OFFSET_FROM_EXTRUDER)) + && NEAR(current_position[Y_AXIS], yProbe - (Y_PROBE_OFFSET_FROM_EXTRUDER)) + ) { + const float simple_z = current_position[Z_AXIS] - measured_z; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("Z from Probe:", simple_z); + SERIAL_ECHOPAIR(" Matrix:", converted[Z_AXIS]); + SERIAL_ECHOLNPAIR(" Discrepancy:", simple_z - converted[Z_AXIS]); + } + #endif + converted[Z_AXIS] = simple_z; + } + + // The rotated XY and corrected Z are now current_position + COPY(current_position, converted); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position); + #endif + } + + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + + if (!dryrun) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("G29 uncorrected Z:", current_position[Z_AXIS]); + #endif + + // Unapply the offset because it is going to be immediately applied + // and cause compensation movement in Z + current_position[Z_AXIS] -= bilinear_z_offset(current_position); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR(" corrected Z:", current_position[Z_AXIS]); + #endif + } + + #endif // ABL_PLANAR + + #ifdef Z_PROBE_END_SCRIPT + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT); + #endif + enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT)); + stepper.synchronize(); + #endif + + // Auto Bed Leveling is complete! Enable if possible. + planner.leveling_active = dryrun ? abl_should_enable : true; + } // !isnan(measured_z) + + // Restore state after probing + if (!faux) clean_up_after_endstop_or_probe_move(); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< gcode_G29"); + #endif + + report_current_position(); + + KEEPALIVE_STATE(IN_HANDLER); + + if (planner.leveling_active) + SYNC_PLAN_POSITION_KINEMATIC(); + } + +#endif // OLDSCHOOL_ABL + +#if HAS_BED_PROBE + + /** + * G30: Do a single Z probe at the current XY + * + * Parameters: + * + * X Probe X position (default current X) + * Y Probe Y position (default current Y) + * E Engage the probe for each probe + */ + inline void gcode_G30() { + const float xpos = parser.linearval('X', current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER), + ypos = parser.linearval('Y', current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER); + + if (!position_is_reachable_by_probe(xpos, ypos)) return; + + // Disable leveling so the planner won't mess with us + #if HAS_LEVELING + set_bed_leveling_enabled(false); + #endif + + setup_for_endstop_or_probe_move(); + + const float measured_z = probe_pt(xpos, ypos, parser.boolval('E'), 1); + + if (!isnan(measured_z)) { + SERIAL_PROTOCOLPAIR("Bed X: ", FIXFLOAT(xpos)); + SERIAL_PROTOCOLPAIR(" Y: ", FIXFLOAT(ypos)); + SERIAL_PROTOCOLLNPAIR(" Z: ", FIXFLOAT(measured_z)); + } + + clean_up_after_endstop_or_probe_move(); + + report_current_position(); + } + + #if ENABLED(Z_PROBE_SLED) + + /** + * G31: Deploy the Z probe + */ + inline void gcode_G31() { DEPLOY_PROBE(); } + + /** + * G32: Stow the Z probe + */ + inline void gcode_G32() { STOW_PROBE(); } + + #endif // Z_PROBE_SLED + +#endif // HAS_BED_PROBE + +#if ENABLED(DELTA_AUTO_CALIBRATION) + + constexpr uint8_t _7P_STEP = 1, // 7-point step - to change number of calibration points + _4P_STEP = _7P_STEP * 2, // 4-point step + NPP = _7P_STEP * 6; // number of calibration points on the radius + enum CalEnum { // the 7 main calibration points - add definitions if needed + CEN = 0, + __A = 1, + _AB = __A + _7P_STEP, + __B = _AB + _7P_STEP, + _BC = __B + _7P_STEP, + __C = _BC + _7P_STEP, + _CA = __C + _7P_STEP, + }; + + #define LOOP_CAL_PT(VAR, S, N) for (uint8_t VAR=S; VAR<=NPP; VAR+=N) + #define F_LOOP_CAL_PT(VAR, S, N) for (float VAR=S; VARCEN+0.9999; VAR-=N) + #define LOOP_CAL_ALL(VAR) LOOP_CAL_PT(VAR, CEN, 1) + #define LOOP_CAL_RAD(VAR) LOOP_CAL_PT(VAR, __A, _7P_STEP) + #define LOOP_CAL_ACT(VAR, _4P, _OP) LOOP_CAL_PT(VAR, _OP ? _AB : __A, _4P ? _4P_STEP : _7P_STEP) + + static void print_signed_float(const char * const prefix, const float &f) { + SERIAL_PROTOCOLPGM(" "); + serialprintPGM(prefix); + SERIAL_PROTOCOLCHAR(':'); + if (f >= 0) SERIAL_CHAR('+'); + SERIAL_PROTOCOL_F(f, 2); + } + + static void print_G33_settings(const bool end_stops, const bool tower_angles) { + SERIAL_PROTOCOLPAIR(".Height:", delta_height); + if (end_stops) { + print_signed_float(PSTR("Ex"), delta_endstop_adj[A_AXIS]); + print_signed_float(PSTR("Ey"), delta_endstop_adj[B_AXIS]); + print_signed_float(PSTR("Ez"), delta_endstop_adj[C_AXIS]); + } + if (end_stops && tower_angles) { + SERIAL_PROTOCOLPAIR(" Radius:", delta_radius); + SERIAL_EOL(); + SERIAL_CHAR('.'); + SERIAL_PROTOCOL_SP(13); + } + if (tower_angles) { + print_signed_float(PSTR("Tx"), delta_tower_angle_trim[A_AXIS]); + print_signed_float(PSTR("Ty"), delta_tower_angle_trim[B_AXIS]); + print_signed_float(PSTR("Tz"), delta_tower_angle_trim[C_AXIS]); + } + if ((!end_stops && tower_angles) || (end_stops && !tower_angles)) { // XOR + SERIAL_PROTOCOLPAIR(" Radius:", delta_radius); + } + SERIAL_EOL(); + } + + static void print_G33_results(const float z_at_pt[NPP + 1], const bool tower_points, const bool opposite_points) { + SERIAL_PROTOCOLPGM(". "); + print_signed_float(PSTR("c"), z_at_pt[CEN]); + if (tower_points) { + print_signed_float(PSTR(" x"), z_at_pt[__A]); + print_signed_float(PSTR(" y"), z_at_pt[__B]); + print_signed_float(PSTR(" z"), z_at_pt[__C]); + } + if (tower_points && opposite_points) { + SERIAL_EOL(); + SERIAL_CHAR('.'); + SERIAL_PROTOCOL_SP(13); + } + if (opposite_points) { + print_signed_float(PSTR("yz"), z_at_pt[_BC]); + print_signed_float(PSTR("zx"), z_at_pt[_CA]); + print_signed_float(PSTR("xy"), z_at_pt[_AB]); + } + SERIAL_EOL(); + } + + /** + * After G33: + * - Move to the print ceiling (DELTA_HOME_TO_SAFE_ZONE only) + * - Stow the probe + * - Restore endstops state + * - Select the old tool, if needed + */ + static void G33_cleanup( + #if HOTENDS > 1 + const uint8_t old_tool_index + #endif + ) { + #if ENABLED(DELTA_HOME_TO_SAFE_ZONE) + do_blocking_move_to_z(delta_clip_start_height); + #endif + STOW_PROBE(); + clean_up_after_endstop_or_probe_move(); + #if HOTENDS > 1 + tool_change(old_tool_index, 0, true); + #endif + } + + static float probe_G33_points(float z_at_pt[NPP + 1], const int8_t probe_points, const bool towers_set, const bool stow_after_each) { + const bool _0p_calibration = probe_points == 0, + _1p_calibration = probe_points == 1, + _4p_calibration = probe_points == 2, + _4p_opposite_points = _4p_calibration && !towers_set, + _7p_calibration = probe_points >= 3 || probe_points == 0, + _7p_no_intermediates = probe_points == 3, + _7p_1_intermediates = probe_points == 4, + _7p_2_intermediates = probe_points == 5, + _7p_4_intermediates = probe_points == 6, + _7p_6_intermediates = probe_points == 7, + _7p_8_intermediates = probe_points == 8, + _7p_11_intermediates = probe_points == 9, + _7p_14_intermediates = probe_points == 10, + _7p_intermed_points = probe_points >= 4, + _7p_6_centre = probe_points >= 5 && probe_points <= 7, + _7p_9_centre = probe_points >= 8; + + #if HAS_BED_PROBE + const float dx = (X_PROBE_OFFSET_FROM_EXTRUDER), + dy = (Y_PROBE_OFFSET_FROM_EXTRUDER); + #endif + + LOOP_CAL_ALL(axis) z_at_pt[axis] = 0.0; + + if (!_0p_calibration) { + + if (!_7p_no_intermediates && !_7p_4_intermediates && !_7p_11_intermediates) { // probe the center + z_at_pt[CEN] += + #if HAS_BED_PROBE + probe_pt(dx, dy, stow_after_each, 1, false) + #else + lcd_probe_pt(0, 0) + #endif + ; + } + + if (_7p_calibration) { // probe extra center points + const float start = _7p_9_centre ? _CA + _7P_STEP / 3.0 : _7p_6_centre ? _CA : __C, + steps = _7p_9_centre ? _4P_STEP / 3.0 : _7p_6_centre ? _7P_STEP : _4P_STEP; + I_LOOP_CAL_PT(axis, start, steps) { + const float a = RADIANS(210 + (360 / NPP) * (axis - 1)), + r = delta_calibration_radius * 0.1; + z_at_pt[CEN] += + #if HAS_BED_PROBE + probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1, false) + #else + lcd_probe_pt(cos(a) * r, sin(a) * r) + #endif + ; + } + z_at_pt[CEN] /= float(_7p_2_intermediates ? 7 : probe_points); + } + + if (!_1p_calibration) { // probe the radius + const CalEnum start = _4p_opposite_points ? _AB : __A; + const float steps = _7p_14_intermediates ? _7P_STEP / 15.0 : // 15r * 6 + 10c = 100 + _7p_11_intermediates ? _7P_STEP / 12.0 : // 12r * 6 + 9c = 81 + _7p_8_intermediates ? _7P_STEP / 9.0 : // 9r * 6 + 10c = 64 + _7p_6_intermediates ? _7P_STEP / 7.0 : // 7r * 6 + 7c = 49 + _7p_4_intermediates ? _7P_STEP / 5.0 : // 5r * 6 + 6c = 36 + _7p_2_intermediates ? _7P_STEP / 3.0 : // 3r * 6 + 7c = 25 + _7p_1_intermediates ? _7P_STEP / 2.0 : // 2r * 6 + 4c = 16 + _7p_no_intermediates ? _7P_STEP : // 1r * 6 + 3c = 9 + _4P_STEP; // .5r * 6 + 1c = 4 + bool zig_zag = true; + F_LOOP_CAL_PT(axis, start, _7p_9_centre ? steps * 3 : steps) { + const int8_t offset = _7p_9_centre ? 1 : 0; + for (int8_t circle = -offset; circle <= offset; circle++) { + const float a = RADIANS(210 + (360 / NPP) * (axis - 1)), + r = delta_calibration_radius * (1 + 0.1 * (zig_zag ? circle : - circle)), + interpol = fmod(axis, 1); + const float z_temp = + #if HAS_BED_PROBE + probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1, false) + #else + lcd_probe_pt(cos(a) * r, sin(a) * r) + #endif + ; + // split probe point to neighbouring calibration points + z_at_pt[uint8_t(round(axis - interpol + NPP - 1)) % NPP + 1] += z_temp * sq(cos(RADIANS(interpol * 90))); + z_at_pt[uint8_t(round(axis - interpol )) % NPP + 1] += z_temp * sq(sin(RADIANS(interpol * 90))); + } + zig_zag = !zig_zag; + } + if (_7p_intermed_points) + LOOP_CAL_RAD(axis) + z_at_pt[axis] /= _7P_STEP / steps; + } + + float S1 = z_at_pt[CEN], + S2 = sq(z_at_pt[CEN]); + int16_t N = 1; + if (!_1p_calibration) { // std dev from zero plane + LOOP_CAL_ACT(axis, _4p_calibration, _4p_opposite_points) { + S1 += z_at_pt[axis]; + S2 += sq(z_at_pt[axis]); + N++; + } + return round(SQRT(S2 / N) * 1000.0) / 1000.0 + 0.00001; + } + } + + return 0.00001; + } + + #if HAS_BED_PROBE + + static void G33_auto_tune() { + float z_at_pt[NPP + 1] = { 0.0 }, + z_at_pt_base[NPP + 1] = { 0.0 }, + z_temp, h_fac = 0.0, r_fac = 0.0, a_fac = 0.0, norm = 0.8; + + #define ZP(N,I) ((N) * z_at_pt[I]) + #define Z06(I) ZP(6, I) + #define Z03(I) ZP(3, I) + #define Z02(I) ZP(2, I) + #define Z01(I) ZP(1, I) + #define Z32(I) ZP(3/2, I) + + SERIAL_PROTOCOLPGM("AUTO TUNE baseline"); + SERIAL_EOL(); + probe_G33_points(z_at_pt_base, 3, true, false); + print_G33_results(z_at_pt_base, true, true); + + LOOP_XYZ(axis) { + delta_endstop_adj[axis] -= 1.0; + recalc_delta_settings(); + + endstops.enable(true); + if (!home_delta()) return; + endstops.not_homing(); + + SERIAL_PROTOCOLPGM("Tuning E"); + SERIAL_CHAR(tolower(axis_codes[axis])); + SERIAL_EOL(); + + probe_G33_points(z_at_pt, 3, true, false); + LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis]; + print_G33_results(z_at_pt, true, true); + delta_endstop_adj[axis] += 1.0; + recalc_delta_settings(); + switch (axis) { + case A_AXIS : + h_fac += 4.0 / (Z03(CEN) +Z01(__A) +Z32(_CA) +Z32(_AB)); // Offset by X-tower end-stop + break; + case B_AXIS : + h_fac += 4.0 / (Z03(CEN) +Z01(__B) +Z32(_BC) +Z32(_AB)); // Offset by Y-tower end-stop + break; + case C_AXIS : + h_fac += 4.0 / (Z03(CEN) +Z01(__C) +Z32(_BC) +Z32(_CA) ); // Offset by Z-tower end-stop + break; + } + } + h_fac /= 3.0; + h_fac *= norm; // Normalize to 1.02 for Kossel mini + + for (int8_t zig_zag = -1; zig_zag < 2; zig_zag += 2) { + delta_radius += 1.0 * zig_zag; + recalc_delta_settings(); + + endstops.enable(true); + if (!home_delta()) return; + endstops.not_homing(); + + SERIAL_PROTOCOLPGM("Tuning R"); + SERIAL_PROTOCOL(zig_zag == -1 ? "-" : "+"); + SERIAL_EOL(); + probe_G33_points(z_at_pt, 3, true, false); + LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis]; + print_G33_results(z_at_pt, true, true); + delta_radius -= 1.0 * zig_zag; + recalc_delta_settings(); + r_fac -= zig_zag * 6.0 / (Z03(__A) +Z03(__B) +Z03(__C) +Z03(_BC) +Z03(_CA) +Z03(_AB)); // Offset by delta radius + } + r_fac /= 2.0; + r_fac *= 3 * norm; // Normalize to 2.25 for Kossel mini + + LOOP_XYZ(axis) { + delta_tower_angle_trim[axis] += 1.0; + delta_endstop_adj[(axis + 1) % 3] -= 1.0 / 4.5; + delta_endstop_adj[(axis + 2) % 3] += 1.0 / 4.5; + z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]); + delta_height -= z_temp; + LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp; + recalc_delta_settings(); + + endstops.enable(true); + if (!home_delta()) return; + endstops.not_homing(); + + SERIAL_PROTOCOLPGM("Tuning T"); + SERIAL_CHAR(tolower(axis_codes[axis])); + SERIAL_EOL(); + + probe_G33_points(z_at_pt, 3, true, false); + LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis]; + print_G33_results(z_at_pt, true, true); + + delta_tower_angle_trim[axis] -= 1.0; + delta_endstop_adj[(axis+1) % 3] += 1.0/4.5; + delta_endstop_adj[(axis+2) % 3] -= 1.0/4.5; + z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]); + delta_height -= z_temp; + LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp; + recalc_delta_settings(); + switch (axis) { + case A_AXIS : + a_fac += 4.0 / ( Z06(__B) -Z06(__C) +Z06(_CA) -Z06(_AB)); // Offset by alpha tower angle + break; + case B_AXIS : + a_fac += 4.0 / (-Z06(__A) +Z06(__C) -Z06(_BC) +Z06(_AB)); // Offset by beta tower angle + break; + case C_AXIS : + a_fac += 4.0 / (Z06(__A) -Z06(__B) +Z06(_BC) -Z06(_CA) ); // Offset by gamma tower angle + break; + } + } + a_fac /= 3.0; + a_fac *= norm; // Normalize to 0.83 for Kossel mini + + endstops.enable(true); + if (!home_delta()) return; + endstops.not_homing(); + print_signed_float(PSTR( "H_FACTOR: "), h_fac); + print_signed_float(PSTR(" R_FACTOR: "), r_fac); + print_signed_float(PSTR(" A_FACTOR: "), a_fac); + SERIAL_EOL(); + SERIAL_PROTOCOLPGM("Copy these values to Configuration.h"); + SERIAL_EOL(); + } + + #endif // HAS_BED_PROBE + + /** + * G33 - Delta '1-4-7-point' Auto-Calibration + * Calibrate height, endstops, delta radius, and tower angles. + * + * Parameters: + * + * Pn Number of probe points: + * P0 No probe. Normalize only. + * P1 Probe center and set height only. + * P2 Probe center and towers. Set height, endstops and delta radius. + * P3 Probe all positions: center, towers and opposite towers. Set all. + * P4-P10 Probe all positions + at different itermediate locations and average them. + * + * T Don't calibrate tower angle corrections + * + * Cn.nn Calibration precision; when omitted calibrates to maximum precision + * + * Fn Force to run at least n iterations and takes the best result + * + * A Auto tune calibartion factors (set in Configuration.h) + * + * Vn Verbose level: + * V0 Dry-run mode. Report settings and probe results. No calibration. + * V1 Report settings + * V2 Report settings and probe results + * + * E Engage the probe for each point + */ + inline void gcode_G33() { + + const int8_t probe_points = parser.intval('P', DELTA_CALIBRATION_DEFAULT_POINTS); + if (!WITHIN(probe_points, 0, 10)) { + SERIAL_PROTOCOLLNPGM("?(P)oints is implausible (0-10)."); + return; + } + + const int8_t verbose_level = parser.byteval('V', 1); + if (!WITHIN(verbose_level, 0, 2)) { + SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0-2)."); + return; + } + + const float calibration_precision = parser.floatval('C'); + if (calibration_precision < 0) { + SERIAL_PROTOCOLLNPGM("?(C)alibration precision is implausible (>=0)."); + return; + } + + const int8_t force_iterations = parser.intval('F', 0); + if (!WITHIN(force_iterations, 0, 30)) { + SERIAL_PROTOCOLLNPGM("?(F)orce iteration is implausible (0-30)."); + return; + } + + const bool towers_set = !parser.boolval('T'), + auto_tune = parser.boolval('A'), + stow_after_each = parser.boolval('E'), + _0p_calibration = probe_points == 0, + _1p_calibration = probe_points == 1, + _4p_calibration = probe_points == 2, + _7p_9_centre = probe_points >= 8, + _tower_results = (_4p_calibration && towers_set) + || probe_points >= 3 || probe_points == 0, + _opposite_results = (_4p_calibration && !towers_set) + || probe_points >= 3 || probe_points == 0, + _endstop_results = probe_points != 1, + _angle_results = (probe_points >= 3 || probe_points == 0) && towers_set; + const static char save_message[] PROGMEM = "Save with M500 and/or copy to Configuration.h"; + int8_t iterations = 0; + float test_precision, + zero_std_dev = (verbose_level ? 999.0 : 0.0), // 0.0 in dry-run mode : forced end + zero_std_dev_min = zero_std_dev, + e_old[ABC] = { + delta_endstop_adj[A_AXIS], + delta_endstop_adj[B_AXIS], + delta_endstop_adj[C_AXIS] + }, + dr_old = delta_radius, + zh_old = delta_height, + ta_old[ABC] = { + delta_tower_angle_trim[A_AXIS], + delta_tower_angle_trim[B_AXIS], + delta_tower_angle_trim[C_AXIS] + }; + + SERIAL_PROTOCOLLNPGM("G33 Auto Calibrate"); + + if (!_1p_calibration && !_0p_calibration) { // test if the outer radius is reachable + LOOP_CAL_RAD(axis) { + const float a = RADIANS(210 + (360 / NPP) * (axis - 1)), + r = delta_calibration_radius * (1 + (_7p_9_centre ? 0.1 : 0.0)); + if (!position_is_reachable(cos(a) * r, sin(a) * r)) { + SERIAL_PROTOCOLLNPGM("?(M665 B)ed radius is implausible."); + return; + } + } + } + + stepper.synchronize(); + #if HAS_LEVELING + reset_bed_level(); // After calibration bed-level data is no longer valid + #endif + + #if HOTENDS > 1 + const uint8_t old_tool_index = active_extruder; + tool_change(0, 0, true); + #define G33_CLEANUP() G33_cleanup(old_tool_index) + #else + #define G33_CLEANUP() G33_cleanup() + #endif + + setup_for_endstop_or_probe_move(); + endstops.enable(true); + if (!_0p_calibration) { + if (!home_delta()) + return; + endstops.not_homing(); + } + + if (auto_tune) { + #if HAS_BED_PROBE + G33_auto_tune(); + #else + SERIAL_PROTOCOLLNPGM("A probe is needed for auto-tune"); + #endif + G33_CLEANUP(); + return; + } + + // Report settings + + const char *checkingac = PSTR("Checking... AC"); // TODO: Make translatable string + serialprintPGM(checkingac); + if (verbose_level == 0) SERIAL_PROTOCOLPGM(" (DRY-RUN)"); + SERIAL_EOL(); + lcd_setstatusPGM(checkingac); + + print_G33_settings(_endstop_results, _angle_results); + + do { + + float z_at_pt[NPP + 1] = { 0.0 }; + + test_precision = zero_std_dev; + + iterations++; + + // Probe the points + + zero_std_dev = probe_G33_points(z_at_pt, probe_points, towers_set, stow_after_each); + + // Solve matrices + + if ((zero_std_dev < test_precision || iterations <= force_iterations) && zero_std_dev > calibration_precision) { + if (zero_std_dev < zero_std_dev_min) { + COPY(e_old, delta_endstop_adj); + dr_old = delta_radius; + zh_old = delta_height; + COPY(ta_old, delta_tower_angle_trim); + } + + float e_delta[ABC] = { 0.0 }, r_delta = 0.0, t_delta[ABC] = { 0.0 }; + const float r_diff = delta_radius - delta_calibration_radius, + h_factor = 1 / 6.0 * + #ifdef H_FACTOR + (H_FACTOR), // Set in Configuration.h + #else + (1.00 + r_diff * 0.001), // 1.02 for r_diff = 20mm + #endif + r_factor = 1 / 6.0 * + #ifdef R_FACTOR + -(R_FACTOR), // Set in Configuration.h + #else + -(1.75 + 0.005 * r_diff + 0.001 * sq(r_diff)), // 2.25 for r_diff = 20mm + #endif + a_factor = 1 / 6.0 * + #ifdef A_FACTOR + (A_FACTOR); // Set in Configuration.h + #else + (66.66 / delta_calibration_radius); // 0.83 for cal_rd = 80mm + #endif + + #define ZP(N,I) ((N) * z_at_pt[I]) + #define Z6(I) ZP(6, I) + #define Z4(I) ZP(4, I) + #define Z2(I) ZP(2, I) + #define Z1(I) ZP(1, I) + + #if !HAS_BED_PROBE + test_precision = 0.00; // forced end + #endif + + switch (probe_points) { + case 0: + test_precision = 0.00; // forced end + break; + + case 1: + test_precision = 0.00; // forced end + LOOP_XYZ(axis) e_delta[axis] = Z1(CEN); + break; + + case 2: + if (towers_set) { + e_delta[A_AXIS] = (Z6(CEN) +Z4(__A) -Z2(__B) -Z2(__C)) * h_factor; + e_delta[B_AXIS] = (Z6(CEN) -Z2(__A) +Z4(__B) -Z2(__C)) * h_factor; + e_delta[C_AXIS] = (Z6(CEN) -Z2(__A) -Z2(__B) +Z4(__C)) * h_factor; + r_delta = (Z6(CEN) -Z2(__A) -Z2(__B) -Z2(__C)) * r_factor; + } + else { + e_delta[A_AXIS] = (Z6(CEN) -Z4(_BC) +Z2(_CA) +Z2(_AB)) * h_factor; + e_delta[B_AXIS] = (Z6(CEN) +Z2(_BC) -Z4(_CA) +Z2(_AB)) * h_factor; + e_delta[C_AXIS] = (Z6(CEN) +Z2(_BC) +Z2(_CA) -Z4(_AB)) * h_factor; + r_delta = (Z6(CEN) -Z2(_BC) -Z2(_CA) -Z2(_AB)) * r_factor; + } + break; + + default: + e_delta[A_AXIS] = (Z6(CEN) +Z2(__A) -Z1(__B) -Z1(__C) -Z2(_BC) +Z1(_CA) +Z1(_AB)) * h_factor; + e_delta[B_AXIS] = (Z6(CEN) -Z1(__A) +Z2(__B) -Z1(__C) +Z1(_BC) -Z2(_CA) +Z1(_AB)) * h_factor; + e_delta[C_AXIS] = (Z6(CEN) -Z1(__A) -Z1(__B) +Z2(__C) +Z1(_BC) +Z1(_CA) -Z2(_AB)) * h_factor; + r_delta = (Z6(CEN) -Z1(__A) -Z1(__B) -Z1(__C) -Z1(_BC) -Z1(_CA) -Z1(_AB)) * r_factor; + + if (towers_set) { + t_delta[A_AXIS] = ( -Z4(__B) +Z4(__C) -Z4(_CA) +Z4(_AB)) * a_factor; + t_delta[B_AXIS] = ( Z4(__A) -Z4(__C) +Z4(_BC) -Z4(_AB)) * a_factor; + t_delta[C_AXIS] = (-Z4(__A) +Z4(__B) -Z4(_BC) +Z4(_CA) ) * a_factor; + e_delta[A_AXIS] += (t_delta[B_AXIS] - t_delta[C_AXIS]) / 4.5; + e_delta[B_AXIS] += (t_delta[C_AXIS] - t_delta[A_AXIS]) / 4.5; + e_delta[C_AXIS] += (t_delta[A_AXIS] - t_delta[B_AXIS]) / 4.5; + } + break; + } + + LOOP_XYZ(axis) delta_endstop_adj[axis] += e_delta[axis]; + delta_radius += r_delta; + LOOP_XYZ(axis) delta_tower_angle_trim[axis] += t_delta[axis]; + } + else if (zero_std_dev >= test_precision) { // step one back + COPY(delta_endstop_adj, e_old); + delta_radius = dr_old; + delta_height = zh_old; + COPY(delta_tower_angle_trim, ta_old); + } + + if (verbose_level != 0) { // !dry run + // normalise angles to least squares + if (_angle_results) { + float a_sum = 0.0; + LOOP_XYZ(axis) a_sum += delta_tower_angle_trim[axis]; + LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0; + } + + // adjust delta_height and endstops by the max amount + const float z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]); + delta_height -= z_temp; + LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp; + } + recalc_delta_settings(); + NOMORE(zero_std_dev_min, zero_std_dev); + + // print report + + if (verbose_level != 1) + print_G33_results(z_at_pt, _tower_results, _opposite_results); + + if (verbose_level != 0) { // !dry run + if ((zero_std_dev >= test_precision && iterations > force_iterations) || zero_std_dev <= calibration_precision) { // end iterations + SERIAL_PROTOCOLPGM("Calibration OK"); + SERIAL_PROTOCOL_SP(32); + #if HAS_BED_PROBE + if (zero_std_dev >= test_precision && !_1p_calibration) + SERIAL_PROTOCOLPGM("rolling back."); + else + #endif + { + SERIAL_PROTOCOLPGM("std dev:"); + SERIAL_PROTOCOL_F(zero_std_dev_min, 3); + } + SERIAL_EOL(); + char mess[21]; + strcpy_P(mess, PSTR("Calibration sd:")); + if (zero_std_dev_min < 1) + sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev_min * 1000.0)); + else + sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev_min)); + lcd_setstatus(mess); + print_G33_settings(_endstop_results, _angle_results); + serialprintPGM(save_message); + SERIAL_EOL(); + } + else { // !end iterations + char mess[15]; + if (iterations < 31) + sprintf_P(mess, PSTR("Iteration : %02i"), (int)iterations); + else + strcpy_P(mess, PSTR("No convergence")); + SERIAL_PROTOCOL(mess); + SERIAL_PROTOCOL_SP(32); + SERIAL_PROTOCOLPGM("std dev:"); + SERIAL_PROTOCOL_F(zero_std_dev, 3); + SERIAL_EOL(); + lcd_setstatus(mess); + print_G33_settings(_endstop_results, _angle_results); + } + } + else { // dry run + const char *enddryrun = PSTR("End DRY-RUN"); + serialprintPGM(enddryrun); + SERIAL_PROTOCOL_SP(35); + SERIAL_PROTOCOLPGM("std dev:"); + SERIAL_PROTOCOL_F(zero_std_dev, 3); + SERIAL_EOL(); + + char mess[21]; + strcpy_P(mess, enddryrun); + strcpy_P(&mess[11], PSTR(" sd:")); + if (zero_std_dev < 1) + sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev * 1000.0)); + else + sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev)); + lcd_setstatus(mess); + } + + endstops.enable(true); + if (!home_delta()) + return; + endstops.not_homing(); + + } + while (((zero_std_dev < test_precision && iterations < 31) || iterations <= force_iterations) && zero_std_dev > calibration_precision); + + G33_CLEANUP(); + } + +#endif // DELTA_AUTO_CALIBRATION + +#if ENABLED(G38_PROBE_TARGET) + + static bool G38_run_probe() { + + bool G38_pass_fail = false; + + #if ENABLED(PROBE_DOUBLE_TOUCH) + // Get direction of move and retract + float retract_mm[XYZ]; + LOOP_XYZ(i) { + float dist = destination[i] - current_position[i]; + retract_mm[i] = FABS(dist) < G38_MINIMUM_MOVE ? 0 : home_bump_mm((AxisEnum)i) * (dist > 0 ? -1 : 1); + } + #endif + + stepper.synchronize(); // wait until the machine is idle + + // Move until destination reached or target hit + endstops.enable(true); + G38_move = true; + G38_endstop_hit = false; + prepare_move_to_destination(); + stepper.synchronize(); + G38_move = false; + + endstops.hit_on_purpose(); + set_current_from_steppers_for_axis(ALL_AXES); + SYNC_PLAN_POSITION_KINEMATIC(); + + if (G38_endstop_hit) { + + G38_pass_fail = true; + + #if ENABLED(PROBE_DOUBLE_TOUCH) + // Move away by the retract distance + set_destination_from_current(); + LOOP_XYZ(i) destination[i] += retract_mm[i]; + endstops.enable(false); + prepare_move_to_destination(); + stepper.synchronize(); + + feedrate_mm_s /= 4; + + // Bump the target more slowly + LOOP_XYZ(i) destination[i] -= retract_mm[i] * 2; + + endstops.enable(true); + G38_move = true; + prepare_move_to_destination(); + stepper.synchronize(); + G38_move = false; + + set_current_from_steppers_for_axis(ALL_AXES); + SYNC_PLAN_POSITION_KINEMATIC(); + #endif + } + + endstops.hit_on_purpose(); + endstops.not_homing(); + return G38_pass_fail; + } + + /** + * G38.2 - probe toward workpiece, stop on contact, signal error if failure + * G38.3 - probe toward workpiece, stop on contact + * + * Like G28 except uses Z min probe for all axes + */ + inline void gcode_G38(bool is_38_2) { + // Get X Y Z E F + gcode_get_destination(); + + setup_for_endstop_or_probe_move(); + + // If any axis has enough movement, do the move + LOOP_XYZ(i) + if (FABS(destination[i] - current_position[i]) >= G38_MINIMUM_MOVE) { + if (!parser.seenval('F')) feedrate_mm_s = homing_feedrate((AxisEnum)i); + // If G38.2 fails throw an error + if (!G38_run_probe() && is_38_2) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Failed to reach target"); + } + break; + } + + clean_up_after_endstop_or_probe_move(); + } + +#endif // G38_PROBE_TARGET + +#if HAS_MESH + + /** + * G42: Move X & Y axes to mesh coordinates (I & J) + */ + inline void gcode_G42() { + #if ENABLED(NO_MOTION_BEFORE_HOMING) + if (axis_unhomed_error()) return; + #endif + + if (IsRunning()) { + const bool hasI = parser.seenval('I'); + const int8_t ix = RAW_X_POSITION(hasI ? parser.value_linear_units() : 0); + const bool hasJ = parser.seenval('J'); + const int8_t iy = RAW_Y_POSITION(hasJ ? parser.value_linear_units() : 0); + + if ((hasI && !WITHIN(ix, 0, GRID_MAX_POINTS_X - 1)) || (hasJ && !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1))) { + SERIAL_ECHOLNPGM(MSG_ERR_MESH_XY); + return; + } + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + #define _GET_MESH_X(I) bilinear_start[X_AXIS] + I * bilinear_grid_spacing[X_AXIS] + #define _GET_MESH_Y(J) bilinear_start[Y_AXIS] + J * bilinear_grid_spacing[Y_AXIS] + #elif ENABLED(AUTO_BED_LEVELING_UBL) + #define _GET_MESH_X(I) ubl.mesh_index_to_xpos(I) + #define _GET_MESH_Y(J) ubl.mesh_index_to_ypos(J) + #elif ENABLED(MESH_BED_LEVELING) + #define _GET_MESH_X(I) mbl.index_to_xpos[I] + #define _GET_MESH_Y(J) mbl.index_to_ypos[J] + #endif + + set_destination_from_current(); + if (hasI) destination[X_AXIS] = _GET_MESH_X(ix); + if (hasJ) destination[Y_AXIS] = _GET_MESH_Y(iy); + if (parser.boolval('P')) { + if (hasI) destination[X_AXIS] -= X_PROBE_OFFSET_FROM_EXTRUDER; + if (hasJ) destination[Y_AXIS] -= Y_PROBE_OFFSET_FROM_EXTRUDER; + } + + const float fval = parser.linearval('F'); + if (fval > 0.0) feedrate_mm_s = MMM_TO_MMS(fval); + + // SCARA kinematic has "safe" XY raw moves + #if IS_SCARA + prepare_uninterpolated_move_to_destination(); + #else + prepare_move_to_destination(); + #endif + } + } + +#endif // HAS_MESH + +/** + * G92: Set current position to given X Y Z E + */ +inline void gcode_G92() { + + stepper.synchronize(); + + #if ENABLED(CNC_COORDINATE_SYSTEMS) + switch (parser.subcode) { + case 1: + // Zero the G92 values and restore current position + #if !IS_SCARA + LOOP_XYZ(i) { + const float v = position_shift[i]; + if (v) { + position_shift[i] = 0; + update_software_endstops((AxisEnum)i); + } + } + #endif // Not SCARA + return; + } + #endif + + #if ENABLED(CNC_COORDINATE_SYSTEMS) + #define IS_G92_0 (parser.subcode == 0) + #else + #define IS_G92_0 true + #endif + + bool didXYZ = false, didE = false; + + if (IS_G92_0) LOOP_XYZE(i) { + if (parser.seenval(axis_codes[i])) { + const float l = parser.value_axis_units((AxisEnum)i), + v = i == E_AXIS ? l : LOGICAL_TO_NATIVE(l, i), + d = v - current_position[i]; + if (!NEAR_ZERO(d)) { + if (i == E_AXIS) didE = true; else didXYZ = true; + #if IS_SCARA + current_position[i] = v; // For SCARA just set the position directly + #elif HAS_POSITION_SHIFT + if (i == E_AXIS) + current_position[E_AXIS] = v; // When using coordinate spaces, only E is set directly + else { + position_shift[i] += d; // Other axes simply offset the coordinate space + update_software_endstops((AxisEnum)i); + } + #else + current_position[i] = v; // Without workspaces revert to Marlin 1.0 behavior + #endif + } + } + } + + #if ENABLED(CNC_COORDINATE_SYSTEMS) + // Apply workspace offset to the active coordinate system + if (WITHIN(active_coordinate_system, 0, MAX_COORDINATE_SYSTEMS - 1)) + COPY(coordinate_system[active_coordinate_system], position_shift); + #endif + + if (didXYZ) + SYNC_PLAN_POSITION_KINEMATIC(); + else if (didE) + sync_plan_position_e(); + + report_current_position(); +} + +#if HAS_RESUME_CONTINUE + + /** + * M0: Unconditional stop - Wait for user button press on LCD + * M1: Conditional stop - Wait for user button press on LCD + */ + inline void gcode_M0_M1() { + const char * const args = parser.string_arg; + + millis_t ms = 0; + bool hasP = false, hasS = false; + if (parser.seenval('P')) { + ms = parser.value_millis(); // milliseconds to wait + hasP = ms > 0; + } + if (parser.seenval('S')) { + ms = parser.value_millis_from_seconds(); // seconds to wait + hasS = ms > 0; + } + + #if ENABLED(ULTIPANEL) + + if (!hasP && !hasS && args && *args) + lcd_setstatus(args, true); + else { + LCD_MESSAGEPGM(MSG_USERWAIT); + #if ENABLED(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0 + dontExpireStatus(); + #endif + } + + #else + + if (!hasP && !hasS && args && *args) { + SERIAL_ECHO_START(); + SERIAL_ECHOLN(args); + } + + #endif + + KEEPALIVE_STATE(PAUSED_FOR_USER); + wait_for_user = true; + + stepper.synchronize(); + refresh_cmd_timeout(); + + if (ms > 0) { + ms += previous_cmd_ms; // wait until this time for a click + while (PENDING(millis(), ms) && wait_for_user) idle(); + } + else { + #if ENABLED(ULTIPANEL) + if (lcd_detected()) { + while (wait_for_user) idle(); + print_job_timer.isPaused() ? LCD_MESSAGEPGM(WELCOME_MSG) : LCD_MESSAGEPGM(MSG_RESUMING); + } + #else + while (wait_for_user) idle(); + #endif + } + + wait_for_user = false; + KEEPALIVE_STATE(IN_HANDLER); + } + +#endif // HAS_RESUME_CONTINUE + +#if ENABLED(SPINDLE_LASER_ENABLE) + /** + * M3: Spindle Clockwise + * M4: Spindle Counter-clockwise + * + * S0 turns off spindle. + * + * If no speed PWM output is defined then M3/M4 just turns it on. + * + * At least 12.8KHz (50Hz * 256) is needed for spindle PWM. + * Hardware PWM is required. ISRs are too slow. + * + * NOTE: WGM for timers 3, 4, and 5 must be either Mode 1 or Mode 5. + * No other settings give a PWM signal that goes from 0 to 5 volts. + * + * The system automatically sets WGM to Mode 1, so no special + * initialization is needed. + * + * WGM bits for timer 2 are automatically set by the system to + * Mode 1. This produces an acceptable 0 to 5 volt signal. + * No special initialization is needed. + * + * NOTE: A minimum PWM frequency of 50 Hz is needed. All prescaler + * factors for timers 2, 3, 4, and 5 are acceptable. + * + * SPINDLE_LASER_ENABLE_PIN needs an external pullup or it may power on + * the spindle/laser during power-up or when connecting to the host + * (usually goes through a reset which sets all I/O pins to tri-state) + * + * PWM duty cycle goes from 0 (off) to 255 (always on). + */ + + // Wait for spindle to come up to speed + inline void delay_for_power_up() { dwell(SPINDLE_LASER_POWERUP_DELAY); } + + // Wait for spindle to stop turning + inline void delay_for_power_down() { dwell(SPINDLE_LASER_POWERDOWN_DELAY); } + + /** + * ocr_val_mode() is used for debugging and to get the points needed to compute the RPM vs ocr_val line + * + * it accepts inputs of 0-255 + */ + + inline void ocr_val_mode() { + uint8_t spindle_laser_power = parser.value_byte(); + WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low) + if (SPINDLE_LASER_PWM_INVERT) spindle_laser_power = 255 - spindle_laser_power; + analogWrite(SPINDLE_LASER_PWM_PIN, spindle_laser_power); + } + + inline void gcode_M3_M4(bool is_M3) { + + stepper.synchronize(); // wait until previous movement commands (G0/G0/G2/G3) have completed before playing with the spindle + #if SPINDLE_DIR_CHANGE + const bool rotation_dir = (is_M3 && !SPINDLE_INVERT_DIR || !is_M3 && SPINDLE_INVERT_DIR) ? HIGH : LOW; + if (SPINDLE_STOP_ON_DIR_CHANGE \ + && READ(SPINDLE_LASER_ENABLE_PIN) == SPINDLE_LASER_ENABLE_INVERT \ + && READ(SPINDLE_DIR_PIN) != rotation_dir + ) { + WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // turn spindle off + delay_for_power_down(); + } + WRITE(SPINDLE_DIR_PIN, rotation_dir); + #endif + + /** + * Our final value for ocr_val is an unsigned 8 bit value between 0 and 255 which usually means uint8_t. + * Went to uint16_t because some of the uint8_t calculations would sometimes give 1000 0000 rather than 1111 1111. + * Then needed to AND the uint16_t result with 0x00FF to make sure we only wrote the byte of interest. + */ + #if ENABLED(SPINDLE_LASER_PWM) + if (parser.seen('O')) ocr_val_mode(); + else { + const float spindle_laser_power = parser.floatval('S'); + if (spindle_laser_power == 0) { + WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // turn spindle off (active low) + delay_for_power_down(); + } + else { + int16_t ocr_val = (spindle_laser_power - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE)); // convert RPM to PWM duty cycle + NOMORE(ocr_val, 255); // limit to max the Atmel PWM will support + if (spindle_laser_power <= SPEED_POWER_MIN) + ocr_val = (SPEED_POWER_MIN - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE)); // minimum setting + if (spindle_laser_power >= SPEED_POWER_MAX) + ocr_val = (SPEED_POWER_MAX - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE)); // limit to max RPM + if (SPINDLE_LASER_PWM_INVERT) ocr_val = 255 - ocr_val; + WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low) + analogWrite(SPINDLE_LASER_PWM_PIN, ocr_val & 0xFF); // only write low byte + delay_for_power_up(); + } + } + #else + WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low) if spindle speed option not enabled + delay_for_power_up(); + #endif + } + + /** + * M5 turn off spindle + */ + inline void gcode_M5() { + stepper.synchronize(); + WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); + delay_for_power_down(); + } + +#endif // SPINDLE_LASER_ENABLE + +/** + * M17: Enable power on all stepper motors + */ +inline void gcode_M17() { + LCD_MESSAGEPGM(MSG_NO_MOVE); + enable_all_steppers(); +} + +#if ENABLED(ADVANCED_PAUSE_FEATURE) + + static float resume_position[XYZE]; + static bool move_away_flag = false; + #if ENABLED(SDSUPPORT) + static bool sd_print_paused = false; + #endif + + static void filament_change_beep(const int8_t max_beep_count, const bool init=false) { + static millis_t next_buzz = 0; + static int8_t runout_beep = 0; + + if (init) next_buzz = runout_beep = 0; + + const millis_t ms = millis(); + if (ELAPSED(ms, next_buzz)) { + if (max_beep_count < 0 || runout_beep < max_beep_count + 5) { // Only beep as long as we're supposed to + next_buzz = ms + ((max_beep_count < 0 || runout_beep < max_beep_count) ? 2500 : 400); + BUZZ(300, 2000); + runout_beep++; + } + } + } + + static void ensure_safe_temperature() { + bool heaters_heating = true; + + wait_for_heatup = true; // M108 will clear this + while (wait_for_heatup && heaters_heating) { + idle(); + heaters_heating = false; + HOTEND_LOOP() { + if (thermalManager.degTargetHotend(e) && abs(thermalManager.degHotend(e) - thermalManager.degTargetHotend(e)) > TEMP_HYSTERESIS) { + heaters_heating = true; + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_WAIT_FOR_NOZZLES_TO_HEAT); + #endif + break; + } + } + } + } + + #if IS_KINEMATIC + #define RUNPLAN(RATE_MM_S) planner.buffer_line_kinematic(destination, RATE_MM_S, active_extruder) + #else + #define RUNPLAN(RATE_MM_S) buffer_line_to_destination(RATE_MM_S) + #endif + + void do_pause_e_move(const float &length, const float fr) { + current_position[E_AXIS] += length / planner.e_factor[active_extruder]; + set_destination_from_current(); + RUNPLAN(fr); + stepper.synchronize(); + } + + static bool pause_print(const float &retract, const float &z_lift, const float &x_pos, const float &y_pos, + const float &unload_length = 0 , const int8_t max_beep_count = 0, const bool show_lcd = false + ) { + if (move_away_flag) return false; // already paused + + if (!DEBUGGING(DRYRUN) && (unload_length != 0 || retract != 0)) { + #if ENABLED(PREVENT_COLD_EXTRUSION) + if (!thermalManager.allow_cold_extrude && + thermalManager.degTargetHotend(active_extruder) < thermalManager.extrude_min_temp) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_TOO_COLD_FOR_M600); + return false; + } + #endif + + ensure_safe_temperature(); // wait for extruder to heat up before unloading + } + + // Indicate that the printer is paused + move_away_flag = true; + + // Pause the print job and timer + #if ENABLED(SDSUPPORT) + if (card.sdprinting) { + card.pauseSDPrint(); + sd_print_paused = true; + } + #endif + print_job_timer.pause(); + + // Show initial message and wait for synchronize steppers + if (show_lcd) { + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INIT); + #endif + } + + // Save current position + stepper.synchronize(); + COPY(resume_position, current_position); + + // Initial retract before move to filament change position + if (retract) do_pause_e_move(retract, PAUSE_PARK_RETRACT_FEEDRATE); + + // Lift Z axis + if (z_lift > 0) + do_blocking_move_to_z(current_position[Z_AXIS] + z_lift, PAUSE_PARK_Z_FEEDRATE); + + // Move XY axes to filament exchange position + do_blocking_move_to_xy(x_pos, y_pos, PAUSE_PARK_XY_FEEDRATE); + + if (unload_length != 0) { + if (show_lcd) { + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_UNLOAD); + idle(); + #endif + } + + // Unload filament + do_pause_e_move(unload_length, FILAMENT_CHANGE_UNLOAD_FEEDRATE); + } + + if (show_lcd) { + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT); + #endif + } + + #if HAS_BUZZER + filament_change_beep(max_beep_count, true); + #endif + + idle(); + + // Disable extruders steppers for manual filament changing (only on boards that have separate ENABLE_PINS) + #if E0_ENABLE_PIN != X_ENABLE_PIN && E1_ENABLE_PIN != Y_ENABLE_PIN + disable_e_steppers(); + safe_delay(100); + #endif + + // Start the heater idle timers + const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL; + + HOTEND_LOOP() + thermalManager.start_heater_idle_timer(e, nozzle_timeout); + + return true; + } + + static void wait_for_filament_reload(const int8_t max_beep_count = 0) { + bool nozzle_timed_out = false; + + // Wait for filament insert by user and press button + KEEPALIVE_STATE(PAUSED_FOR_USER); + wait_for_user = true; // LCD click or M108 will clear this + while (wait_for_user) { + #if HAS_BUZZER + filament_change_beep(max_beep_count); + #endif + + // If the nozzle has timed out, wait for the user to press the button to re-heat the nozzle, then + // re-heat the nozzle, re-show the insert screen, restart the idle timers, and start over + if (!nozzle_timed_out) + HOTEND_LOOP() + nozzle_timed_out |= thermalManager.is_heater_idle(e); + + if (nozzle_timed_out) { + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_CLICK_TO_HEAT_NOZZLE); + #endif + + // Wait for LCD click or M108 + while (wait_for_user) idle(true); + + // Re-enable the heaters if they timed out + HOTEND_LOOP() thermalManager.reset_heater_idle_timer(e); + + // Wait for the heaters to reach the target temperatures + ensure_safe_temperature(); + + #if ENABLED(ULTIPANEL) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT); + #endif + + // Start the heater idle timers + const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL; + + HOTEND_LOOP() + thermalManager.start_heater_idle_timer(e, nozzle_timeout); + + wait_for_user = true; /* Wait for user to load filament */ + nozzle_timed_out = false; + + #if HAS_BUZZER + filament_change_beep(max_beep_count, true); + #endif + } + + idle(true); + } + KEEPALIVE_STATE(IN_HANDLER); + } + + static void resume_print(const float &load_length = 0, const float &initial_extrude_length = 0, const int8_t max_beep_count = 0) { + bool nozzle_timed_out = false; + + if (!move_away_flag) return; + + // Re-enable the heaters if they timed out + HOTEND_LOOP() { + nozzle_timed_out |= thermalManager.is_heater_idle(e); + thermalManager.reset_heater_idle_timer(e); + } + + if (nozzle_timed_out) ensure_safe_temperature(); + + #if HAS_BUZZER + filament_change_beep(max_beep_count, true); + #endif + + set_destination_from_current(); + + if (load_length != 0) { + #if ENABLED(ULTIPANEL) + // Show "insert filament" + if (nozzle_timed_out) + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT); + #endif + + KEEPALIVE_STATE(PAUSED_FOR_USER); + wait_for_user = true; // LCD click or M108 will clear this + while (wait_for_user && nozzle_timed_out) { + #if HAS_BUZZER + filament_change_beep(max_beep_count); + #endif + idle(true); + } + KEEPALIVE_STATE(IN_HANDLER); + + #if ENABLED(ULTIPANEL) + // Show "load" message + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_LOAD); + #endif + + // Load filament + do_pause_e_move(load_length, FILAMENT_CHANGE_LOAD_FEEDRATE); + } + + #if ENABLED(ULTIPANEL) && ADVANCED_PAUSE_EXTRUDE_LENGTH > 0 + + float extrude_length = initial_extrude_length; + + do { + if (extrude_length > 0) { + // "Wait for filament extrude" + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_EXTRUDE); + + // Extrude filament to get into hotend + do_pause_e_move(extrude_length, ADVANCED_PAUSE_EXTRUDE_FEEDRATE); + } + + // Show "Extrude More" / "Resume" menu and wait for reply + KEEPALIVE_STATE(PAUSED_FOR_USER); + wait_for_user = false; + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_OPTION); + while (advanced_pause_menu_response == ADVANCED_PAUSE_RESPONSE_WAIT_FOR) idle(true); + KEEPALIVE_STATE(IN_HANDLER); + + extrude_length = ADVANCED_PAUSE_EXTRUDE_LENGTH; + + // Keep looping if "Extrude More" was selected + } while (advanced_pause_menu_response == ADVANCED_PAUSE_RESPONSE_EXTRUDE_MORE); + + #endif + + #if ENABLED(ULTIPANEL) + // "Wait for print to resume" + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_RESUME); + #endif + + // Set extruder to saved position + destination[E_AXIS] = current_position[E_AXIS] = resume_position[E_AXIS]; + planner.set_e_position_mm(current_position[E_AXIS]); + + // Move XY to starting position, then Z + do_blocking_move_to_xy(resume_position[X_AXIS], resume_position[Y_AXIS], PAUSE_PARK_XY_FEEDRATE); + do_blocking_move_to_z(resume_position[Z_AXIS], PAUSE_PARK_Z_FEEDRATE); + + #if ENABLED(FILAMENT_RUNOUT_SENSOR) + filament_ran_out = false; + #endif + + #if ENABLED(ULTIPANEL) + // Show status screen + lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_STATUS); + #endif + + #if ENABLED(SDSUPPORT) + if (sd_print_paused) { + card.startFileprint(); + sd_print_paused = false; + } + #endif + + move_away_flag = false; + } +#endif // ADVANCED_PAUSE_FEATURE + +#if ENABLED(SDSUPPORT) + + /** + * M20: List SD card to serial output + */ + inline void gcode_M20() { + SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST); + card.ls(); + SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST); + } + + /** + * M21: Init SD Card + */ + inline void gcode_M21() { card.initsd(); } + + /** + * M22: Release SD Card + */ + inline void gcode_M22() { card.release(); } + + /** + * M23: Open a file + */ + inline void gcode_M23() { + // Simplify3D includes the size, so zero out all spaces (#7227) + for (char *fn = parser.string_arg; *fn; ++fn) if (*fn == ' ') *fn = '\0'; + card.openFile(parser.string_arg, true); + } + + /** + * M24: Start or Resume SD Print + */ + inline void gcode_M24() { + #if ENABLED(PARK_HEAD_ON_PAUSE) + resume_print(); + #endif + + card.startFileprint(); + print_job_timer.start(); + } + + /** + * M25: Pause SD Print + */ + inline void gcode_M25() { + card.pauseSDPrint(); + print_job_timer.pause(); + + #if ENABLED(PARK_HEAD_ON_PAUSE) + enqueue_and_echo_commands_P(PSTR("M125")); // Must be enqueued with pauseSDPrint set to be last in the buffer + #endif + } + + /** + * M26: Set SD Card file index + */ + inline void gcode_M26() { + if (card.cardOK && parser.seenval('S')) + card.setIndex(parser.value_long()); + } + + /** + * M27: Get SD Card status + */ + inline void gcode_M27() { card.getStatus(); } + + /** + * M28: Start SD Write + */ + inline void gcode_M28() { card.openFile(parser.string_arg, false); } + + /** + * M29: Stop SD Write + * Processed in write to file routine above + */ + inline void gcode_M29() { + // card.saving = false; + } + + /** + * M30 : Delete SD Card file + */ + inline void gcode_M30() { + if (card.cardOK) { + card.closefile(); + card.removeFile(parser.string_arg); + } + } + +#endif // SDSUPPORT + +/** + * M31: Get the time since the start of SD Print (or last M109) + */ +inline void gcode_M31() { + char buffer[21]; + duration_t elapsed = print_job_timer.duration(); + elapsed.toString(buffer); + lcd_setstatus(buffer); + + SERIAL_ECHO_START(); + SERIAL_ECHOLNPAIR("Print time: ", buffer); +} + +#if ENABLED(SDSUPPORT) + + /** + * M32: Select file and start SD Print + */ + inline void gcode_M32() { + if (card.sdprinting) + stepper.synchronize(); + + char* namestartpos = parser.string_arg; + const bool call_procedure = parser.boolval('P'); + + if (card.cardOK) { + card.openFile(namestartpos, true, call_procedure); + + if (parser.seenval('S')) + card.setIndex(parser.value_long()); + + card.startFileprint(); + + // Procedure calls count as normal print time. + if (!call_procedure) print_job_timer.start(); + } + } + + #if ENABLED(LONG_FILENAME_HOST_SUPPORT) + + /** + * M33: Get the long full path of a file or folder + * + * Parameters: + * Case-insensitive DOS-style path to a file or folder + * + * Example: + * M33 miscel~1/armchair/armcha~1.gco + * + * Output: + * /Miscellaneous/Armchair/Armchair.gcode + */ + inline void gcode_M33() { + card.printLongPath(parser.string_arg); + } + + #endif + + #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE) + /** + * M34: Set SD Card Sorting Options + */ + inline void gcode_M34() { + if (parser.seen('S')) card.setSortOn(parser.value_bool()); + if (parser.seenval('F')) { + const int v = parser.value_long(); + card.setSortFolders(v < 0 ? -1 : v > 0 ? 1 : 0); + } + //if (parser.seen('R')) card.setSortReverse(parser.value_bool()); + } + #endif // SDCARD_SORT_ALPHA && SDSORT_GCODE + + /** + * M928: Start SD Write + */ + inline void gcode_M928() { + card.openLogFile(parser.string_arg); + } + +#endif // SDSUPPORT + +/** + * Sensitive pin test for M42, M226 + */ +static bool pin_is_protected(const int8_t pin) { + static const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS; + for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) + if (pin == (int8_t)pgm_read_byte(&sensitive_pins[i])) return true; + return false; +} + +/** + * M42: Change pin status via GCode + * + * P Pin number (LED if omitted) + * S Pin status from 0 - 255 + */ +inline void gcode_M42() { + if (!parser.seenval('S')) return; + const byte pin_status = parser.value_byte(); + + const int pin_number = parser.intval('P', LED_PIN); + if (pin_number < 0) return; + + if (pin_is_protected(pin_number)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_PROTECTED_PIN); + return; + } + + pinMode(pin_number, OUTPUT); + digitalWrite(pin_number, pin_status); + analogWrite(pin_number, pin_status); + + #if FAN_COUNT > 0 + switch (pin_number) { + #if HAS_FAN0 + case FAN_PIN: fanSpeeds[0] = pin_status; break; + #endif + #if HAS_FAN1 + case FAN1_PIN: fanSpeeds[1] = pin_status; break; + #endif + #if HAS_FAN2 + case FAN2_PIN: fanSpeeds[2] = pin_status; break; + #endif + } + #endif +} + +#if ENABLED(PINS_DEBUGGING) + + #include "pinsDebug.h" + + inline void toggle_pins() { + const bool I_flag = parser.boolval('I'); + const int repeat = parser.intval('R', 1), + start = parser.intval('S'), + end = parser.intval('E', NUM_DIGITAL_PINS - 1), + wait = parser.intval('W', 500); + + for (uint8_t pin = start; pin <= end; pin++) { + //report_pin_state_extended(pin, I_flag, false); + + if (!I_flag && pin_is_protected(pin)) { + report_pin_state_extended(pin, I_flag, true, "Untouched "); + SERIAL_EOL(); + } + else { + report_pin_state_extended(pin, I_flag, true, "Pulsing "); + #if AVR_AT90USB1286_FAMILY // Teensy IDEs don't know about these pins so must use FASTIO + if (pin == TEENSY_E2) { + SET_OUTPUT(TEENSY_E2); + for (int16_t j = 0; j < repeat; j++) { + WRITE(TEENSY_E2, LOW); safe_delay(wait); + WRITE(TEENSY_E2, HIGH); safe_delay(wait); + WRITE(TEENSY_E2, LOW); safe_delay(wait); + } + } + else if (pin == TEENSY_E3) { + SET_OUTPUT(TEENSY_E3); + for (int16_t j = 0; j < repeat; j++) { + WRITE(TEENSY_E3, LOW); safe_delay(wait); + WRITE(TEENSY_E3, HIGH); safe_delay(wait); + WRITE(TEENSY_E3, LOW); safe_delay(wait); + } + } + else + #endif + { + pinMode(pin, OUTPUT); + for (int16_t j = 0; j < repeat; j++) { + digitalWrite(pin, 0); safe_delay(wait); + digitalWrite(pin, 1); safe_delay(wait); + digitalWrite(pin, 0); safe_delay(wait); + } + } + + } + SERIAL_EOL(); + } + SERIAL_ECHOLNPGM("Done."); + + } // toggle_pins + + inline void servo_probe_test() { + #if !(NUM_SERVOS > 0 && HAS_SERVO_0) + + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("SERVO not setup"); + + #elif !HAS_Z_SERVO_ENDSTOP + + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Z_ENDSTOP_SERVO_NR not setup"); + + #else // HAS_Z_SERVO_ENDSTOP + + const uint8_t probe_index = parser.byteval('P', Z_ENDSTOP_SERVO_NR); + + SERIAL_PROTOCOLLNPGM("Servo probe test"); + SERIAL_PROTOCOLLNPAIR(". using index: ", probe_index); + SERIAL_PROTOCOLLNPAIR(". deploy angle: ", z_servo_angle[0]); + SERIAL_PROTOCOLLNPAIR(". stow angle: ", z_servo_angle[1]); + + bool probe_inverting; + + #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) + + #define PROBE_TEST_PIN Z_MIN_PIN + + SERIAL_PROTOCOLLNPAIR(". probe uses Z_MIN pin: ", PROBE_TEST_PIN); + SERIAL_PROTOCOLLNPGM(". uses Z_MIN_ENDSTOP_INVERTING (ignores Z_MIN_PROBE_ENDSTOP_INVERTING)"); + SERIAL_PROTOCOLPGM(". Z_MIN_ENDSTOP_INVERTING: "); + + #if Z_MIN_ENDSTOP_INVERTING + SERIAL_PROTOCOLLNPGM("true"); + #else + SERIAL_PROTOCOLLNPGM("false"); + #endif + + probe_inverting = Z_MIN_ENDSTOP_INVERTING; + + #elif ENABLED(Z_MIN_PROBE_ENDSTOP) + + #define PROBE_TEST_PIN Z_MIN_PROBE_PIN + SERIAL_PROTOCOLLNPAIR(". probe uses Z_MIN_PROBE_PIN: ", PROBE_TEST_PIN); + SERIAL_PROTOCOLLNPGM(". uses Z_MIN_PROBE_ENDSTOP_INVERTING (ignores Z_MIN_ENDSTOP_INVERTING)"); + SERIAL_PROTOCOLPGM(". Z_MIN_PROBE_ENDSTOP_INVERTING: "); + + #if Z_MIN_PROBE_ENDSTOP_INVERTING + SERIAL_PROTOCOLLNPGM("true"); + #else + SERIAL_PROTOCOLLNPGM("false"); + #endif + + probe_inverting = Z_MIN_PROBE_ENDSTOP_INVERTING; + + #endif + + SERIAL_PROTOCOLLNPGM(". deploy & stow 4 times"); + SET_INPUT_PULLUP(PROBE_TEST_PIN); + bool deploy_state, stow_state; + for (uint8_t i = 0; i < 4; i++) { + MOVE_SERVO(probe_index, z_servo_angle[0]); //deploy + safe_delay(500); + deploy_state = READ(PROBE_TEST_PIN); + MOVE_SERVO(probe_index, z_servo_angle[1]); //stow + safe_delay(500); + stow_state = READ(PROBE_TEST_PIN); + } + if (probe_inverting != deploy_state) SERIAL_PROTOCOLLNPGM("WARNING - INVERTING setting probably backwards"); + + refresh_cmd_timeout(); + + if (deploy_state != stow_state) { + SERIAL_PROTOCOLLNPGM("BLTouch clone detected"); + if (deploy_state) { + SERIAL_PROTOCOLLNPGM(". DEPLOYED state: HIGH (logic 1)"); + SERIAL_PROTOCOLLNPGM(". STOWED (triggered) state: LOW (logic 0)"); + } + else { + SERIAL_PROTOCOLLNPGM(". DEPLOYED state: LOW (logic 0)"); + SERIAL_PROTOCOLLNPGM(". STOWED (triggered) state: HIGH (logic 1)"); + } + #if ENABLED(BLTOUCH) + SERIAL_PROTOCOLLNPGM("ERROR: BLTOUCH enabled - set this device up as a Z Servo Probe with inverting as true."); + #endif + + } + else { // measure active signal length + MOVE_SERVO(probe_index, z_servo_angle[0]); // deploy + safe_delay(500); + SERIAL_PROTOCOLLNPGM("please trigger probe"); + uint16_t probe_counter = 0; + + // Allow 30 seconds max for operator to trigger probe + for (uint16_t j = 0; j < 500 * 30 && probe_counter == 0 ; j++) { + + safe_delay(2); + + if (0 == j % (500 * 1)) // keep cmd_timeout happy + refresh_cmd_timeout(); + + if (deploy_state != READ(PROBE_TEST_PIN)) { // probe triggered + + for (probe_counter = 1; probe_counter < 50 && deploy_state != READ(PROBE_TEST_PIN); ++probe_counter) + safe_delay(2); + + if (probe_counter == 50) + SERIAL_PROTOCOLLNPGM("Z Servo Probe detected"); // >= 100mS active time + else if (probe_counter >= 2) + SERIAL_PROTOCOLLNPAIR("BLTouch compatible probe detected - pulse width (+/- 4mS): ", probe_counter * 2); // allow 4 - 100mS pulse + else + SERIAL_PROTOCOLLNPGM("noise detected - please re-run test"); // less than 2mS pulse + + MOVE_SERVO(probe_index, z_servo_angle[1]); //stow + + } // pulse detected + + } // for loop waiting for trigger + + if (probe_counter == 0) SERIAL_PROTOCOLLNPGM("trigger not detected"); + + } // measure active signal length + + #endif + + } // servo_probe_test + + /** + * M43: Pin debug - report pin state, watch pins, toggle pins and servo probe test/report + * + * M43 - report name and state of pin(s) + * P Pin to read or watch. If omitted, reads all pins. + * I Flag to ignore Marlin's pin protection. + * + * M43 W - Watch pins -reporting changes- until reset, click, or M108. + * P Pin to read or watch. If omitted, read/watch all pins. + * I Flag to ignore Marlin's pin protection. + * + * M43 E - Enable / disable background endstop monitoring + * - Machine continues to operate + * - Reports changes to endstops + * - Toggles LED_PIN when an endstop changes + * - Can not reliably catch the 5mS pulse from BLTouch type probes + * + * M43 T - Toggle pin(s) and report which pin is being toggled + * S - Start Pin number. If not given, will default to 0 + * L - End Pin number. If not given, will default to last pin defined for this board + * I - Flag to ignore Marlin's pin protection. Use with caution!!!! + * R - Repeat pulses on each pin this number of times before continueing to next pin + * W - Wait time (in miliseconds) between pulses. If not given will default to 500 + * + * M43 S - Servo probe test + * P - Probe index (optional - defaults to 0 + */ + inline void gcode_M43() { + + if (parser.seen('T')) { // must be first or else its "S" and "E" parameters will execute endstop or servo test + toggle_pins(); + return; + } + + // Enable or disable endstop monitoring + if (parser.seen('E')) { + endstop_monitor_flag = parser.value_bool(); + SERIAL_PROTOCOLPGM("endstop monitor "); + serialprintPGM(endstop_monitor_flag ? PSTR("en") : PSTR("dis")); + SERIAL_PROTOCOLLNPGM("abled"); + return; + } + + if (parser.seen('S')) { + servo_probe_test(); + return; + } + + // Get the range of pins to test or watch + const uint8_t first_pin = parser.byteval('P'), + last_pin = parser.seenval('P') ? first_pin : NUM_DIGITAL_PINS - 1; + + if (first_pin > last_pin) return; + + const bool ignore_protection = parser.boolval('I'); + + // Watch until click, M108, or reset + if (parser.boolval('W')) { + SERIAL_PROTOCOLLNPGM("Watching pins"); + byte pin_state[last_pin - first_pin + 1]; + for (int8_t pin = first_pin; pin <= last_pin; pin++) { + if (pin_is_protected(pin) && !ignore_protection) continue; + pinMode(pin, INPUT_PULLUP); + delay(1); + /* + if (IS_ANALOG(pin)) + pin_state[pin - first_pin] = analogRead(pin - analogInputToDigitalPin(0)); // int16_t pin_state[...] + else + //*/ + pin_state[pin - first_pin] = digitalRead(pin); + } + + #if HAS_RESUME_CONTINUE + wait_for_user = true; + KEEPALIVE_STATE(PAUSED_FOR_USER); + #endif + + for (;;) { + for (int8_t pin = first_pin; pin <= last_pin; pin++) { + if (pin_is_protected(pin) && !ignore_protection) continue; + const byte val = + /* + IS_ANALOG(pin) + ? analogRead(pin - analogInputToDigitalPin(0)) : // int16_t val + : + //*/ + digitalRead(pin); + if (val != pin_state[pin - first_pin]) { + report_pin_state_extended(pin, ignore_protection, false); + pin_state[pin - first_pin] = val; + } + } + + #if HAS_RESUME_CONTINUE + if (!wait_for_user) { + KEEPALIVE_STATE(IN_HANDLER); + break; + } + #endif + + safe_delay(200); + } + return; + } + + // Report current state of selected pin(s) + for (uint8_t pin = first_pin; pin <= last_pin; pin++) + report_pin_state_extended(pin, ignore_protection, true); + } + +#endif // PINS_DEBUGGING + +#if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST) + + /** + * M48: Z probe repeatability measurement function. + * + * Usage: + * M48 + * P = Number of sampled points (4-50, default 10) + * X = Sample X position + * Y = Sample Y position + * V = Verbose level (0-4, default=1) + * E = Engage Z probe for each reading + * L = Number of legs of movement before probe + * S = Schizoid (Or Star if you prefer) + * + * This function assumes the bed has been homed. Specifically, that a G28 command + * as been issued prior to invoking the M48 Z probe repeatability measurement function. + * Any information generated by a prior G29 Bed leveling command will be lost and need to be + * regenerated. + */ + inline void gcode_M48() { + + if (axis_unhomed_error()) return; + + const int8_t verbose_level = parser.byteval('V', 1); + if (!WITHIN(verbose_level, 0, 4)) { + SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0-4)."); + return; + } + + if (verbose_level > 0) + SERIAL_PROTOCOLLNPGM("M48 Z-Probe Repeatability Test"); + + const int8_t n_samples = parser.byteval('P', 10); + if (!WITHIN(n_samples, 4, 50)) { + SERIAL_PROTOCOLLNPGM("?Sample size not plausible (4-50)."); + return; + } + + const bool stow_probe_after_each = parser.boolval('E'); + + float X_current = current_position[X_AXIS], + Y_current = current_position[Y_AXIS]; + + const float X_probe_location = parser.linearval('X', X_current + X_PROBE_OFFSET_FROM_EXTRUDER), + Y_probe_location = parser.linearval('Y', Y_current + Y_PROBE_OFFSET_FROM_EXTRUDER); + + #if DISABLED(DELTA) + if (!WITHIN(X_probe_location, MIN_PROBE_X, MAX_PROBE_X)) { + out_of_range_error(PSTR("X")); + return; + } + if (!WITHIN(Y_probe_location, MIN_PROBE_Y, MAX_PROBE_Y)) { + out_of_range_error(PSTR("Y")); + return; + } + #else + if (!position_is_reachable_by_probe(X_probe_location, Y_probe_location)) { + SERIAL_PROTOCOLLNPGM("? (X,Y) location outside of probeable radius."); + return; + } + #endif + + bool seen_L = parser.seen('L'); + uint8_t n_legs = seen_L ? parser.value_byte() : 0; + if (n_legs > 15) { + SERIAL_PROTOCOLLNPGM("?Number of legs in movement not plausible (0-15)."); + return; + } + if (n_legs == 1) n_legs = 2; + + const bool schizoid_flag = parser.boolval('S'); + if (schizoid_flag && !seen_L) n_legs = 7; + + /** + * Now get everything to the specified probe point So we can safely do a + * probe to get us close to the bed. If the Z-Axis is far from the bed, + * we don't want to use that as a starting point for each probe. + */ + if (verbose_level > 2) + SERIAL_PROTOCOLLNPGM("Positioning the probe..."); + + // Disable bed level correction in M48 because we want the raw data when we probe + + #if HAS_LEVELING + const bool was_enabled = planner.leveling_active; + set_bed_leveling_enabled(false); + #endif + + setup_for_endstop_or_probe_move(); + + double mean = 0.0, sigma = 0.0, min = 99999.9, max = -99999.9, sample_set[n_samples]; + + // Move to the first point, deploy, and probe + const float t = probe_pt(X_probe_location, Y_probe_location, stow_probe_after_each, verbose_level); + bool probing_good = !isnan(t); + + if (probing_good) { + randomSeed(millis()); + + for (uint8_t n = 0; n < n_samples; n++) { + if (n_legs) { + const int dir = (random(0, 10) > 5.0) ? -1 : 1; // clockwise or counter clockwise + float angle = random(0.0, 360.0); + const float radius = random( + #if ENABLED(DELTA) + 0.1250000000 * (DELTA_PROBEABLE_RADIUS), + 0.3333333333 * (DELTA_PROBEABLE_RADIUS) + #else + 5.0, 0.125 * min(X_BED_SIZE, Y_BED_SIZE) + #endif + ); + + if (verbose_level > 3) { + SERIAL_ECHOPAIR("Starting radius: ", radius); + SERIAL_ECHOPAIR(" angle: ", angle); + SERIAL_ECHOPGM(" Direction: "); + if (dir > 0) SERIAL_ECHOPGM("Counter-"); + SERIAL_ECHOLNPGM("Clockwise"); + } + + for (uint8_t l = 0; l < n_legs - 1; l++) { + double delta_angle; + + if (schizoid_flag) + // The points of a 5 point star are 72 degrees apart. We need to + // skip a point and go to the next one on the star. + delta_angle = dir * 2.0 * 72.0; + + else + // If we do this line, we are just trying to move further + // around the circle. + delta_angle = dir * (float) random(25, 45); + + angle += delta_angle; + + while (angle > 360.0) // We probably do not need to keep the angle between 0 and 2*PI, but the + angle -= 360.0; // Arduino documentation says the trig functions should not be given values + while (angle < 0.0) // outside of this range. It looks like they behave correctly with + angle += 360.0; // numbers outside of the range, but just to be safe we clamp them. + + X_current = X_probe_location - (X_PROBE_OFFSET_FROM_EXTRUDER) + cos(RADIANS(angle)) * radius; + Y_current = Y_probe_location - (Y_PROBE_OFFSET_FROM_EXTRUDER) + sin(RADIANS(angle)) * radius; + + #if DISABLED(DELTA) + X_current = constrain(X_current, X_MIN_POS, X_MAX_POS); + Y_current = constrain(Y_current, Y_MIN_POS, Y_MAX_POS); + #else + // If we have gone out too far, we can do a simple fix and scale the numbers + // back in closer to the origin. + while (!position_is_reachable_by_probe(X_current, Y_current)) { + X_current *= 0.8; + Y_current *= 0.8; + if (verbose_level > 3) { + SERIAL_ECHOPAIR("Pulling point towards center:", X_current); + SERIAL_ECHOLNPAIR(", ", Y_current); + } + } + #endif + if (verbose_level > 3) { + SERIAL_PROTOCOLPGM("Going to:"); + SERIAL_ECHOPAIR(" X", X_current); + SERIAL_ECHOPAIR(" Y", Y_current); + SERIAL_ECHOLNPAIR(" Z", current_position[Z_AXIS]); + } + do_blocking_move_to_xy(X_current, Y_current); + } // n_legs loop + } // n_legs + + // Probe a single point + sample_set[n] = probe_pt(X_probe_location, Y_probe_location, stow_probe_after_each, 0); + + // Break the loop if the probe fails + probing_good = !isnan(sample_set[n]); + if (!probing_good) break; + + /** + * Get the current mean for the data points we have so far + */ + double sum = 0.0; + for (uint8_t j = 0; j <= n; j++) sum += sample_set[j]; + mean = sum / (n + 1); + + NOMORE(min, sample_set[n]); + NOLESS(max, sample_set[n]); + + /** + * Now, use that mean to calculate the standard deviation for the + * data points we have so far + */ + sum = 0.0; + for (uint8_t j = 0; j <= n; j++) + sum += sq(sample_set[j] - mean); + + sigma = SQRT(sum / (n + 1)); + if (verbose_level > 0) { + if (verbose_level > 1) { + SERIAL_PROTOCOL(n + 1); + SERIAL_PROTOCOLPGM(" of "); + SERIAL_PROTOCOL((int)n_samples); + SERIAL_PROTOCOLPGM(": z: "); + SERIAL_PROTOCOL_F(sample_set[n], 3); + if (verbose_level > 2) { + SERIAL_PROTOCOLPGM(" mean: "); + SERIAL_PROTOCOL_F(mean, 4); + SERIAL_PROTOCOLPGM(" sigma: "); + SERIAL_PROTOCOL_F(sigma, 6); + SERIAL_PROTOCOLPGM(" min: "); + SERIAL_PROTOCOL_F(min, 3); + SERIAL_PROTOCOLPGM(" max: "); + SERIAL_PROTOCOL_F(max, 3); + SERIAL_PROTOCOLPGM(" range: "); + SERIAL_PROTOCOL_F(max-min, 3); + } + SERIAL_EOL(); + } + } + + } // n_samples loop + } + + STOW_PROBE(); + + if (probing_good) { + SERIAL_PROTOCOLLNPGM("Finished!"); + + if (verbose_level > 0) { + SERIAL_PROTOCOLPGM("Mean: "); + SERIAL_PROTOCOL_F(mean, 6); + SERIAL_PROTOCOLPGM(" Min: "); + SERIAL_PROTOCOL_F(min, 3); + SERIAL_PROTOCOLPGM(" Max: "); + SERIAL_PROTOCOL_F(max, 3); + SERIAL_PROTOCOLPGM(" Range: "); + SERIAL_PROTOCOL_F(max-min, 3); + SERIAL_EOL(); + } + + SERIAL_PROTOCOLPGM("Standard Deviation: "); + SERIAL_PROTOCOL_F(sigma, 6); + SERIAL_EOL(); + SERIAL_EOL(); + } + + clean_up_after_endstop_or_probe_move(); + + // Re-enable bed level correction if it had been on + #if HAS_LEVELING + set_bed_leveling_enabled(was_enabled); + #endif + + report_current_position(); + } + +#endif // Z_MIN_PROBE_REPEATABILITY_TEST + +#if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_VALIDATION) + + inline void gcode_M49() { + ubl.g26_debug_flag ^= true; + SERIAL_PROTOCOLPGM("UBL Debug Flag turned "); + serialprintPGM(ubl.g26_debug_flag ? PSTR("on.") : PSTR("off.")); + } + +#endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_VALIDATION + +#if ENABLED(ULTRA_LCD) && ENABLED(LCD_SET_PROGRESS_MANUALLY) + /** + * M73: Set percentage complete (for display on LCD) + * + * Example: + * M73 P25 ; Set progress to 25% + * + * Notes: + * This has no effect during an SD print job + */ + inline void gcode_M73() { + if (!IS_SD_PRINTING && parser.seen('P')) { + progress_bar_percent = parser.value_byte(); + NOMORE(progress_bar_percent, 100); + } + } +#endif // ULTRA_LCD && LCD_SET_PROGRESS_MANUALLY + +/** + * M75: Start print timer + */ +inline void gcode_M75() { print_job_timer.start(); } + +/** + * M76: Pause print timer + */ +inline void gcode_M76() { print_job_timer.pause(); } + +/** + * M77: Stop print timer + */ +inline void gcode_M77() { print_job_timer.stop(); } + +#if ENABLED(PRINTCOUNTER) + /** + * M78: Show print statistics + */ + inline void gcode_M78() { + // "M78 S78" will reset the statistics + if (parser.intval('S') == 78) + print_job_timer.initStats(); + else + print_job_timer.showStats(); + } +#endif + +/** + * M104: Set hot end temperature + */ +inline void gcode_M104() { + if (get_target_extruder_from_command(104)) return; + if (DEBUGGING(DRYRUN)) return; + + #if ENABLED(SINGLENOZZLE) + if (target_extruder != active_extruder) return; + #endif + + if (parser.seenval('S')) { + const int16_t temp = parser.value_celsius(); + thermalManager.setTargetHotend(temp, target_extruder); + + #if ENABLED(DUAL_X_CARRIAGE) + if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && target_extruder == 0) + thermalManager.setTargetHotend(temp ? temp + duplicate_extruder_temp_offset : 0, 1); + #endif + + #if ENABLED(PRINTJOB_TIMER_AUTOSTART) + /** + * Stop the timer at the end of print. Start is managed by 'heat and wait' M109. + * We use half EXTRUDE_MINTEMP here to allow nozzles to be put into hot + * standby mode, for instance in a dual extruder setup, without affecting + * the running print timer. + */ + if (parser.value_celsius() <= (EXTRUDE_MINTEMP) / 2) { + print_job_timer.stop(); + LCD_MESSAGEPGM(WELCOME_MSG); + } + #endif + + if (parser.value_celsius() > thermalManager.degHotend(target_extruder)) + lcd_status_printf_P(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING); + } + + #if ENABLED(AUTOTEMP) + planner.autotemp_M104_M109(); + #endif +} + +#if HAS_TEMP_HOTEND || HAS_TEMP_BED + + void print_heater_state(const float &c, const float &t, + #if ENABLED(SHOW_TEMP_ADC_VALUES) + const float r, + #endif + const int8_t e=-2 + ) { + #if !(HAS_TEMP_BED && HAS_TEMP_HOTEND) && HOTENDS <= 1 + UNUSED(e); + #endif + + SERIAL_PROTOCOLCHAR(' '); + SERIAL_PROTOCOLCHAR( + #if HAS_TEMP_BED && HAS_TEMP_HOTEND + e == -1 ? 'B' : 'T' + #elif HAS_TEMP_HOTEND + 'T' + #else + 'B' + #endif + ); + #if HOTENDS > 1 + if (e >= 0) SERIAL_PROTOCOLCHAR('0' + e); + #endif + SERIAL_PROTOCOLCHAR(':'); + SERIAL_PROTOCOL(c); + SERIAL_PROTOCOLPAIR(" /" , t); + #if ENABLED(SHOW_TEMP_ADC_VALUES) + SERIAL_PROTOCOLPAIR(" (", r / OVERSAMPLENR); + SERIAL_PROTOCOLCHAR(')'); + #endif + } + + void print_heaterstates() { + #if HAS_TEMP_HOTEND + print_heater_state(thermalManager.degHotend(target_extruder), thermalManager.degTargetHotend(target_extruder) + #if ENABLED(SHOW_TEMP_ADC_VALUES) + , thermalManager.rawHotendTemp(target_extruder) + #endif + ); + #endif + #if HAS_TEMP_BED + print_heater_state(thermalManager.degBed(), thermalManager.degTargetBed(), + #if ENABLED(SHOW_TEMP_ADC_VALUES) + thermalManager.rawBedTemp(), + #endif + -1 // BED + ); + #endif + #if HOTENDS > 1 + HOTEND_LOOP() print_heater_state(thermalManager.degHotend(e), thermalManager.degTargetHotend(e), + #if ENABLED(SHOW_TEMP_ADC_VALUES) + thermalManager.rawHotendTemp(e), + #endif + e + ); + #endif + SERIAL_PROTOCOLPGM(" @:"); + SERIAL_PROTOCOL(thermalManager.getHeaterPower(target_extruder)); + #if HAS_TEMP_BED + SERIAL_PROTOCOLPGM(" B@:"); + SERIAL_PROTOCOL(thermalManager.getHeaterPower(-1)); + #endif + #if HOTENDS > 1 + HOTEND_LOOP() { + SERIAL_PROTOCOLPAIR(" @", e); + SERIAL_PROTOCOLCHAR(':'); + SERIAL_PROTOCOL(thermalManager.getHeaterPower(e)); + } + #endif + } +#endif + +/** + * M105: Read hot end and bed temperature + */ +inline void gcode_M105() { + if (get_target_extruder_from_command(105)) return; + + #if HAS_TEMP_HOTEND || HAS_TEMP_BED + SERIAL_PROTOCOLPGM(MSG_OK); + print_heaterstates(); + #else // !HAS_TEMP_HOTEND && !HAS_TEMP_BED + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS); + #endif + + SERIAL_EOL(); +} + +#if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED) + + static uint8_t auto_report_temp_interval; + static millis_t next_temp_report_ms; + + /** + * M155: Set temperature auto-report interval. M155 S + */ + inline void gcode_M155() { + if (parser.seenval('S')) { + auto_report_temp_interval = parser.value_byte(); + NOMORE(auto_report_temp_interval, 60); + next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval; + } + } + + inline void auto_report_temperatures() { + if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) { + next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval; + print_heaterstates(); + SERIAL_EOL(); + } + } + +#endif // AUTO_REPORT_TEMPERATURES + +#if FAN_COUNT > 0 + + /** + * M106: Set Fan Speed + * + * S Speed between 0-255 + * P Fan index, if more than one fan + * + * With EXTRA_FAN_SPEED enabled: + * + * T Restore/Use/Set Temporary Speed: + * 1 = Restore previous speed after T2 + * 2 = Use temporary speed set with T3-255 + * 3-255 = Set the speed for use with T2 + */ + inline void gcode_M106() { + const uint8_t p = parser.byteval('P'); + if (p < FAN_COUNT) { + #if ENABLED(EXTRA_FAN_SPEED) + const int16_t t = parser.intval('T'); + NOMORE(t, 255); + if (t > 0) { + switch (t) { + case 1: + fanSpeeds[p] = old_fanSpeeds[p]; + break; + case 2: + old_fanSpeeds[p] = fanSpeeds[p]; + fanSpeeds[p] = new_fanSpeeds[p]; + break; + default: + new_fanSpeeds[p] = t; + break; + } + return; + } + #endif // EXTRA_FAN_SPEED + const uint16_t s = parser.ushortval('S', 255); + fanSpeeds[p] = min(s, 255); + } + } + + /** + * M107: Fan Off + */ + inline void gcode_M107() { + const uint16_t p = parser.ushortval('P'); + if (p < FAN_COUNT) fanSpeeds[p] = 0; + } + +#endif // FAN_COUNT > 0 + +#if DISABLED(EMERGENCY_PARSER) + + /** + * M108: Stop the waiting for heaters in M109, M190, M303. Does not affect the target temperature. + */ + inline void gcode_M108() { wait_for_heatup = false; } + + + /** + * M112: Emergency Stop + */ + inline void gcode_M112() { kill(PSTR(MSG_KILLED)); } + + + /** + * M410: Quickstop - Abort all planned moves + * + * This will stop the carriages mid-move, so most likely they + * will be out of sync with the stepper position after this. + */ + inline void gcode_M410() { quickstop_stepper(); } + +#endif + +/** + * M109: Sxxx Wait for extruder(s) to reach temperature. Waits only when heating. + * Rxxx Wait for extruder(s) to reach temperature. Waits when heating and cooling. + */ + +#ifndef MIN_COOLING_SLOPE_DEG + #define MIN_COOLING_SLOPE_DEG 1.50 +#endif +#ifndef MIN_COOLING_SLOPE_TIME + #define MIN_COOLING_SLOPE_TIME 60 +#endif + +inline void gcode_M109() { + + if (get_target_extruder_from_command(109)) return; + if (DEBUGGING(DRYRUN)) return; + + #if ENABLED(SINGLENOZZLE) + if (target_extruder != active_extruder) return; + #endif + + const bool no_wait_for_cooling = parser.seenval('S'); + if (no_wait_for_cooling || parser.seenval('R')) { + const int16_t temp = parser.value_celsius(); + thermalManager.setTargetHotend(temp, target_extruder); + + #if ENABLED(DUAL_X_CARRIAGE) + if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && target_extruder == 0) + thermalManager.setTargetHotend(temp ? temp + duplicate_extruder_temp_offset : 0, 1); + #endif + + #if ENABLED(PRINTJOB_TIMER_AUTOSTART) + /** + * Use half EXTRUDE_MINTEMP to allow nozzles to be put into hot + * standby mode, (e.g., in a dual extruder setup) without affecting + * the running print timer. + */ + if (parser.value_celsius() <= (EXTRUDE_MINTEMP) / 2) { + print_job_timer.stop(); + LCD_MESSAGEPGM(WELCOME_MSG); + } + else + print_job_timer.start(); + #endif + + if (thermalManager.isHeatingHotend(target_extruder)) lcd_status_printf_P(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING); + } + else return; + + #if ENABLED(AUTOTEMP) + planner.autotemp_M104_M109(); + #endif + + #if TEMP_RESIDENCY_TIME > 0 + millis_t residency_start_ms = 0; + // Loop until the temperature has stabilized + #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_RESIDENCY_TIME) * 1000UL)) + #else + // Loop until the temperature is very close target + #define TEMP_CONDITIONS (wants_to_cool ? thermalManager.isCoolingHotend(target_extruder) : thermalManager.isHeatingHotend(target_extruder)) + #endif + + float target_temp = -1.0, old_temp = 9999.0; + bool wants_to_cool = false; + wait_for_heatup = true; + millis_t now, next_temp_ms = 0, next_cool_check_ms = 0; + + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(NOT_BUSY); + #endif + + #if ENABLED(PRINTER_EVENT_LEDS) + const float start_temp = thermalManager.degHotend(target_extruder); + uint8_t old_blue = 0; + #endif + + do { + // Target temperature might be changed during the loop + if (target_temp != thermalManager.degTargetHotend(target_extruder)) { + wants_to_cool = thermalManager.isCoolingHotend(target_extruder); + target_temp = thermalManager.degTargetHotend(target_extruder); + + // Exit if S, continue if S, R, or R + if (no_wait_for_cooling && wants_to_cool) break; + } + + now = millis(); + if (ELAPSED(now, next_temp_ms)) { //Print temp & remaining time every 1s while waiting + next_temp_ms = now + 1000UL; + print_heaterstates(); + #if TEMP_RESIDENCY_TIME > 0 + SERIAL_PROTOCOLPGM(" W:"); + if (residency_start_ms) + SERIAL_PROTOCOL(long((((TEMP_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL)); + else + SERIAL_PROTOCOLCHAR('?'); + #endif + SERIAL_EOL(); + } + + idle(); + refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out + + const float temp = thermalManager.degHotend(target_extruder); + + #if ENABLED(PRINTER_EVENT_LEDS) + // Gradually change LED strip from violet to red as nozzle heats up + if (!wants_to_cool) { + const uint8_t blue = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 255, 0); + if (blue != old_blue) { + old_blue = blue; + set_led_color(255, 0, blue + #if ENABLED(NEOPIXEL_LED) + , 0 + , pixels.getBrightness() + #if ENABLED(NEOPIXEL_IS_SEQUENTIAL) + , true + #endif + #endif + ); + } + } + #endif + + #if TEMP_RESIDENCY_TIME > 0 + + const float temp_diff = FABS(target_temp - temp); + + if (!residency_start_ms) { + // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time. + if (temp_diff < TEMP_WINDOW) residency_start_ms = now; + } + else if (temp_diff > TEMP_HYSTERESIS) { + // Restart the timer whenever the temperature falls outside the hysteresis. + residency_start_ms = now; + } + + #endif + + // Prevent a wait-forever situation if R is misused i.e. M109 R0 + if (wants_to_cool) { + // break after MIN_COOLING_SLOPE_TIME seconds + // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG + if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) { + if (old_temp - temp < MIN_COOLING_SLOPE_DEG) break; + next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME; + old_temp = temp; + } + } + + } while (wait_for_heatup && TEMP_CONDITIONS); + + if (wait_for_heatup) { + LCD_MESSAGEPGM(MSG_HEATING_COMPLETE); + #if ENABLED(PRINTER_EVENT_LEDS) + #if ENABLED(RGB_LED) || ENABLED(BLINKM) || ENABLED(PCA9632) || ENABLED(RGBW_LED) + set_led_color(LED_WHITE); + #endif + #if ENABLED(NEOPIXEL_LED) + set_neopixel_color(pixels.Color(NEO_WHITE)); + #endif + #endif + } + + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(IN_HANDLER); + #endif +} + +#if HAS_TEMP_BED + + #ifndef MIN_COOLING_SLOPE_DEG_BED + #define MIN_COOLING_SLOPE_DEG_BED 1.50 + #endif + #ifndef MIN_COOLING_SLOPE_TIME_BED + #define MIN_COOLING_SLOPE_TIME_BED 60 + #endif + + /** + * M190: Sxxx Wait for bed current temp to reach target temp. Waits only when heating + * Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling + */ + inline void gcode_M190() { + if (DEBUGGING(DRYRUN)) return; + + LCD_MESSAGEPGM(MSG_BED_HEATING); + const bool no_wait_for_cooling = parser.seenval('S'); + if (no_wait_for_cooling || parser.seenval('R')) { + thermalManager.setTargetBed(parser.value_celsius()); + #if ENABLED(PRINTJOB_TIMER_AUTOSTART) + if (parser.value_celsius() > BED_MINTEMP) + print_job_timer.start(); + #endif + } + else return; + + #if TEMP_BED_RESIDENCY_TIME > 0 + millis_t residency_start_ms = 0; + // Loop until the temperature has stabilized + #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_BED_RESIDENCY_TIME) * 1000UL)) + #else + // Loop until the temperature is very close target + #define TEMP_BED_CONDITIONS (wants_to_cool ? thermalManager.isCoolingBed() : thermalManager.isHeatingBed()) + #endif + + float target_temp = -1.0, old_temp = 9999.0; + bool wants_to_cool = false; + wait_for_heatup = true; + millis_t now, next_temp_ms = 0, next_cool_check_ms = 0; + + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(NOT_BUSY); + #endif + + target_extruder = active_extruder; // for print_heaterstates + + #if ENABLED(PRINTER_EVENT_LEDS) + const float start_temp = thermalManager.degBed(); + uint8_t old_red = 255; + #endif + + do { + // Target temperature might be changed during the loop + if (target_temp != thermalManager.degTargetBed()) { + wants_to_cool = thermalManager.isCoolingBed(); + target_temp = thermalManager.degTargetBed(); + + // Exit if S, continue if S, R, or R + if (no_wait_for_cooling && wants_to_cool) break; + } + + now = millis(); + if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up. + next_temp_ms = now + 1000UL; + print_heaterstates(); + #if TEMP_BED_RESIDENCY_TIME > 0 + SERIAL_PROTOCOLPGM(" W:"); + if (residency_start_ms) + SERIAL_PROTOCOL(long((((TEMP_BED_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL)); + else + SERIAL_PROTOCOLCHAR('?'); + #endif + SERIAL_EOL(); + } + + idle(); + refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out + + const float temp = thermalManager.degBed(); + + #if ENABLED(PRINTER_EVENT_LEDS) + // Gradually change LED strip from blue to violet as bed heats up + if (!wants_to_cool) { + const uint8_t red = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 0, 255); + if (red != old_red) { + old_red = red; + set_led_color(red, 0, 255 + #if ENABLED(NEOPIXEL_LED) + , 0, pixels.getBrightness() + #if ENABLED(NEOPIXEL_IS_SEQUENTIAL) + , true + #endif + #endif + ); + } + } + #endif + + #if TEMP_BED_RESIDENCY_TIME > 0 + + const float temp_diff = FABS(target_temp - temp); + + if (!residency_start_ms) { + // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time. + if (temp_diff < TEMP_BED_WINDOW) residency_start_ms = now; + } + else if (temp_diff > TEMP_BED_HYSTERESIS) { + // Restart the timer whenever the temperature falls outside the hysteresis. + residency_start_ms = now; + } + + #endif // TEMP_BED_RESIDENCY_TIME > 0 + + // Prevent a wait-forever situation if R is misused i.e. M190 R0 + if (wants_to_cool) { + // Break after MIN_COOLING_SLOPE_TIME_BED seconds + // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED + if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) { + if (old_temp - temp < MIN_COOLING_SLOPE_DEG_BED) break; + next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED; + old_temp = temp; + } + } + + } while (wait_for_heatup && TEMP_BED_CONDITIONS); + + if (wait_for_heatup) LCD_MESSAGEPGM(MSG_BED_DONE); + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(IN_HANDLER); + #endif + } + +#endif // HAS_TEMP_BED + +/** + * M110: Set Current Line Number + */ +inline void gcode_M110() { + if (parser.seenval('N')) gcode_LastN = parser.value_long(); +} + +/** + * M111: Set the debug level + */ +inline void gcode_M111() { + if (parser.seen('S')) marlin_debug_flags = parser.byteval('S'); + + const static char str_debug_1[] PROGMEM = MSG_DEBUG_ECHO, + str_debug_2[] PROGMEM = MSG_DEBUG_INFO, + str_debug_4[] PROGMEM = MSG_DEBUG_ERRORS, + str_debug_8[] PROGMEM = MSG_DEBUG_DRYRUN, + str_debug_16[] PROGMEM = MSG_DEBUG_COMMUNICATION + #if ENABLED(DEBUG_LEVELING_FEATURE) + , str_debug_32[] PROGMEM = MSG_DEBUG_LEVELING + #endif + ; + + const static char* const debug_strings[] PROGMEM = { + str_debug_1, str_debug_2, str_debug_4, str_debug_8, str_debug_16 + #if ENABLED(DEBUG_LEVELING_FEATURE) + , str_debug_32 + #endif + }; + + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_DEBUG_PREFIX); + if (marlin_debug_flags) { + uint8_t comma = 0; + for (uint8_t i = 0; i < COUNT(debug_strings); i++) { + if (TEST(marlin_debug_flags, i)) { + if (comma++) SERIAL_CHAR(','); + serialprintPGM((char*)pgm_read_word(&debug_strings[i])); + } + } + } + else { + SERIAL_ECHOPGM(MSG_DEBUG_OFF); + } + SERIAL_EOL(); +} + +#if ENABLED(HOST_KEEPALIVE_FEATURE) + + /** + * M113: Get or set Host Keepalive interval (0 to disable) + * + * S Optional. Set the keepalive interval. + */ + inline void gcode_M113() { + if (parser.seenval('S')) { + host_keepalive_interval = parser.value_byte(); + NOMORE(host_keepalive_interval, 60); + } + else { + SERIAL_ECHO_START(); + SERIAL_ECHOLNPAIR("M113 S", (unsigned long)host_keepalive_interval); + } + } + +#endif + +#if ENABLED(BARICUDA) + + #if HAS_HEATER_1 + /** + * M126: Heater 1 valve open + */ + inline void gcode_M126() { baricuda_valve_pressure = parser.byteval('S', 255); } + /** + * M127: Heater 1 valve close + */ + inline void gcode_M127() { baricuda_valve_pressure = 0; } + #endif + + #if HAS_HEATER_2 + /** + * M128: Heater 2 valve open + */ + inline void gcode_M128() { baricuda_e_to_p_pressure = parser.byteval('S', 255); } + /** + * M129: Heater 2 valve close + */ + inline void gcode_M129() { baricuda_e_to_p_pressure = 0; } + #endif + +#endif // BARICUDA + +/** + * M140: Set bed temperature + */ +inline void gcode_M140() { + if (DEBUGGING(DRYRUN)) return; + if (parser.seenval('S')) thermalManager.setTargetBed(parser.value_celsius()); +} + +#if ENABLED(ULTIPANEL) + + /** + * M145: Set the heatup state for a material in the LCD menu + * + * S (0=PLA, 1=ABS) + * H + * B + * F + */ + inline void gcode_M145() { + const uint8_t material = (uint8_t)parser.intval('S'); + if (material >= COUNT(lcd_preheat_hotend_temp)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_MATERIAL_INDEX); + } + else { + int v; + if (parser.seenval('H')) { + v = parser.value_int(); + lcd_preheat_hotend_temp[material] = constrain(v, EXTRUDE_MINTEMP, HEATER_0_MAXTEMP - 15); + } + if (parser.seenval('F')) { + v = parser.value_int(); + lcd_preheat_fan_speed[material] = constrain(v, 0, 255); + } + #if TEMP_SENSOR_BED != 0 + if (parser.seenval('B')) { + v = parser.value_int(); + lcd_preheat_bed_temp[material] = constrain(v, BED_MINTEMP, BED_MAXTEMP - 15); + } + #endif + } + } + +#endif // ULTIPANEL + +#if ENABLED(TEMPERATURE_UNITS_SUPPORT) + /** + * M149: Set temperature units + */ + inline void gcode_M149() { + if (parser.seenval('C')) parser.set_input_temp_units(TEMPUNIT_C); + else if (parser.seenval('K')) parser.set_input_temp_units(TEMPUNIT_K); + else if (parser.seenval('F')) parser.set_input_temp_units(TEMPUNIT_F); + } +#endif + +#if HAS_POWER_SWITCH + + /** + * M80 : Turn on the Power Supply + * M80 S : Report the current state and exit + */ + inline void gcode_M80() { + + // S: Report the current power supply state and exit + if (parser.seen('S')) { + serialprintPGM(powersupply_on ? PSTR("PS:1\n") : PSTR("PS:0\n")); + return; + } + + OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); // GND + + /** + * If you have a switch on suicide pin, this is useful + * if you want to start another print with suicide feature after + * a print without suicide... + */ + #if HAS_SUICIDE + OUT_WRITE(SUICIDE_PIN, HIGH); + #endif + + #if ENABLED(HAVE_TMC2130) + delay(100); + tmc2130_init(); // Settings only stick when the driver has power + #endif + + powersupply_on = true; + + #if ENABLED(ULTIPANEL) + LCD_MESSAGEPGM(WELCOME_MSG); + #endif + } + +#endif // HAS_POWER_SWITCH + +/** + * M81: Turn off Power, including Power Supply, if there is one. + * + * This code should ALWAYS be available for EMERGENCY SHUTDOWN! + */ +inline void gcode_M81() { + thermalManager.disable_all_heaters(); + stepper.finish_and_disable(); + + #if FAN_COUNT > 0 + for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0; + #if ENABLED(PROBING_FANS_OFF) + fans_paused = false; + ZERO(paused_fanSpeeds); + #endif + #endif + + safe_delay(1000); // Wait 1 second before switching off + + #if HAS_SUICIDE + stepper.synchronize(); + suicide(); + #elif HAS_POWER_SWITCH + OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP); + powersupply_on = false; + #endif + + #if ENABLED(ULTIPANEL) + LCD_MESSAGEPGM(MACHINE_NAME " " MSG_OFF "."); + #endif +} + +/** + * M82: Set E codes absolute (default) + */ +inline void gcode_M82() { axis_relative_modes[E_AXIS] = false; } + +/** + * M83: Set E codes relative while in Absolute Coordinates (G90) mode + */ +inline void gcode_M83() { axis_relative_modes[E_AXIS] = true; } + +/** + * M18, M84: Disable stepper motors + */ +inline void gcode_M18_M84() { + if (parser.seenval('S')) { + stepper_inactive_time = parser.value_millis_from_seconds(); + } + else { + bool all_axis = !((parser.seen('X')) || (parser.seen('Y')) || (parser.seen('Z')) || (parser.seen('E'))); + if (all_axis) { + stepper.finish_and_disable(); + } + else { + stepper.synchronize(); + if (parser.seen('X')) disable_X(); + if (parser.seen('Y')) disable_Y(); + if (parser.seen('Z')) disable_Z(); + #if E0_ENABLE_PIN != X_ENABLE_PIN && E1_ENABLE_PIN != Y_ENABLE_PIN // Only enable on boards that have separate ENABLE_PINS + if (parser.seen('E')) disable_e_steppers(); + #endif + } + + #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD + ubl_lcd_map_control = defer_return_to_status = false; + #endif + } +} + +/** + * M85: Set inactivity shutdown timer with parameter S. To disable set zero (default) + */ +inline void gcode_M85() { + if (parser.seen('S')) max_inactive_time = parser.value_millis_from_seconds(); +} + +/** + * Multi-stepper support for M92, M201, M203 + */ +#if ENABLED(DISTINCT_E_FACTORS) + #define GET_TARGET_EXTRUDER(CMD) if (get_target_extruder_from_command(CMD)) return + #define TARGET_EXTRUDER target_extruder +#else + #define GET_TARGET_EXTRUDER(CMD) NOOP + #define TARGET_EXTRUDER 0 +#endif + +/** + * M92: Set axis steps-per-unit for one or more axes, X, Y, Z, and E. + * (Follows the same syntax as G92) + * + * With multiple extruders use T to specify which one. + */ +inline void gcode_M92() { + + GET_TARGET_EXTRUDER(92); + + LOOP_XYZE(i) { + if (parser.seen(axis_codes[i])) { + if (i == E_AXIS) { + const float value = parser.value_per_axis_unit((AxisEnum)(E_AXIS + TARGET_EXTRUDER)); + if (value < 20.0) { + float factor = planner.axis_steps_per_mm[E_AXIS + TARGET_EXTRUDER] / value; // increase e constants if M92 E14 is given for netfab. + planner.max_jerk[E_AXIS] *= factor; + planner.max_feedrate_mm_s[E_AXIS + TARGET_EXTRUDER] *= factor; + planner.max_acceleration_steps_per_s2[E_AXIS + TARGET_EXTRUDER] *= factor; + } + planner.axis_steps_per_mm[E_AXIS + TARGET_EXTRUDER] = value; + } + else { + planner.axis_steps_per_mm[i] = parser.value_per_axis_unit((AxisEnum)i); + } + } + } + planner.refresh_positioning(); +} + +/** + * Output the current position to serial + */ +void report_current_position() { + SERIAL_PROTOCOLPGM("X:"); + SERIAL_PROTOCOL(LOGICAL_X_POSITION(current_position[X_AXIS])); + SERIAL_PROTOCOLPGM(" Y:"); + SERIAL_PROTOCOL(LOGICAL_X_POSITION(current_position[Y_AXIS])); + SERIAL_PROTOCOLPGM(" Z:"); + SERIAL_PROTOCOL(LOGICAL_Z_POSITION(current_position[Z_AXIS])); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL(current_position[E_AXIS]); + + stepper.report_positions(); + + #if IS_SCARA + SERIAL_PROTOCOLPAIR("SCARA Theta:", stepper.get_axis_position_degrees(A_AXIS)); + SERIAL_PROTOCOLLNPAIR(" Psi+Theta:", stepper.get_axis_position_degrees(B_AXIS)); + SERIAL_EOL(); + #endif +} + +#ifdef M114_DETAIL + + void report_xyze(const float pos[XYZE], const uint8_t n = 4, const uint8_t precision = 3) { + char str[12]; + for (uint8_t i = 0; i < n; i++) { + SERIAL_CHAR(' '); + SERIAL_CHAR(axis_codes[i]); + SERIAL_CHAR(':'); + SERIAL_PROTOCOL(dtostrf(pos[i], 8, precision, str)); + } + SERIAL_EOL(); + } + + inline void report_xyz(const float pos[XYZ]) { report_xyze(pos, 3); } + + void report_current_position_detail() { + + stepper.synchronize(); + + SERIAL_PROTOCOLPGM("\nLogical:"); + const float logical[XYZ] = { + LOGICAL_X_POSITION(current_position[X_AXIS]), + LOGICAL_Y_POSITION(current_position[Y_AXIS]), + LOGICAL_Z_POSITION(current_position[Z_AXIS]) + }; + report_xyze(logical); + + SERIAL_PROTOCOLPGM("Raw: "); + report_xyz(current_position); + + SERIAL_PROTOCOLPGM("Leveled:"); + float leveled[XYZ] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] }; + planner.apply_leveling(leveled); + report_xyz(leveled); + + SERIAL_PROTOCOLPGM("UnLevel:"); + float unleveled[XYZ] = { leveled[X_AXIS], leveled[Y_AXIS], leveled[Z_AXIS] }; + planner.unapply_leveling(unleveled); + report_xyz(unleveled); + + #if IS_KINEMATIC + #if IS_SCARA + SERIAL_PROTOCOLPGM("ScaraK: "); + #else + SERIAL_PROTOCOLPGM("DeltaK: "); + #endif + inverse_kinematics(leveled); // writes delta[] + report_xyz(delta); + #endif + + SERIAL_PROTOCOLPGM("Stepper:"); + const float step_count[XYZE] = { stepper.position(X_AXIS), stepper.position(Y_AXIS), stepper.position(Z_AXIS), stepper.position(E_AXIS) }; + report_xyze(step_count, 4, 0); + + #if IS_SCARA + const float deg[XYZ] = { + stepper.get_axis_position_degrees(A_AXIS), + stepper.get_axis_position_degrees(B_AXIS) + }; + SERIAL_PROTOCOLPGM("Degrees:"); + report_xyze(deg, 2); + #endif + + SERIAL_PROTOCOLPGM("FromStp:"); + get_cartesian_from_steppers(); // writes cartes[XYZ] (with forward kinematics) + const float from_steppers[XYZE] = { cartes[X_AXIS], cartes[Y_AXIS], cartes[Z_AXIS], stepper.get_axis_position_mm(E_AXIS) }; + report_xyze(from_steppers); + + const float diff[XYZE] = { + from_steppers[X_AXIS] - leveled[X_AXIS], + from_steppers[Y_AXIS] - leveled[Y_AXIS], + from_steppers[Z_AXIS] - leveled[Z_AXIS], + from_steppers[E_AXIS] - current_position[E_AXIS] + }; + SERIAL_PROTOCOLPGM("Differ: "); + report_xyze(diff); + } +#endif // M114_DETAIL + +/** + * M114: Report current position to host + */ +inline void gcode_M114() { + + #ifdef M114_DETAIL + if (parser.seen('D')) { + report_current_position_detail(); + return; + } + #endif + + stepper.synchronize(); + report_current_position(); +} + +/** + * M115: Capabilities string + */ +inline void gcode_M115() { + SERIAL_PROTOCOLLNPGM(MSG_M115_REPORT); + + #if ENABLED(EXTENDED_CAPABILITIES_REPORT) + + // EEPROM (M500, M501) + #if ENABLED(EEPROM_SETTINGS) + SERIAL_PROTOCOLLNPGM("Cap:EEPROM:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:EEPROM:0"); + #endif + + // AUTOREPORT_TEMP (M155) + #if ENABLED(AUTO_REPORT_TEMPERATURES) + SERIAL_PROTOCOLLNPGM("Cap:AUTOREPORT_TEMP:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:AUTOREPORT_TEMP:0"); + #endif + + // PROGRESS (M530 S L, M531 , M532 X L) + SERIAL_PROTOCOLLNPGM("Cap:PROGRESS:0"); + + // Print Job timer M75, M76, M77 + SERIAL_PROTOCOLLNPGM("Cap:PRINT_JOB:1"); + + // AUTOLEVEL (G29) + #if HAS_ABL + SERIAL_PROTOCOLLNPGM("Cap:AUTOLEVEL:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:AUTOLEVEL:0"); + #endif + + // Z_PROBE (G30) + #if HAS_BED_PROBE + SERIAL_PROTOCOLLNPGM("Cap:Z_PROBE:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:Z_PROBE:0"); + #endif + + // MESH_REPORT (M420 V) + #if HAS_LEVELING + SERIAL_PROTOCOLLNPGM("Cap:LEVELING_DATA:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:LEVELING_DATA:0"); + #endif + + // BUILD_PERCENT (M73) + #if ENABLED(LCD_SET_PROGRESS_MANUALLY) + SERIAL_PROTOCOLLNPGM("Cap:BUILD_PERCENT:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:BUILD_PERCENT:0"); + #endif + + // SOFTWARE_POWER (M80, M81) + #if HAS_POWER_SWITCH + SERIAL_PROTOCOLLNPGM("Cap:SOFTWARE_POWER:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:SOFTWARE_POWER:0"); + #endif + + // CASE LIGHTS (M355) + #if HAS_CASE_LIGHT + SERIAL_PROTOCOLLNPGM("Cap:TOGGLE_LIGHTS:1"); + if (USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) { + SERIAL_PROTOCOLLNPGM("Cap:CASE_LIGHT_BRIGHTNESS:1"); + } + else + SERIAL_PROTOCOLLNPGM("Cap:CASE_LIGHT_BRIGHTNESS:0"); + #else + SERIAL_PROTOCOLLNPGM("Cap:TOGGLE_LIGHTS:0"); + SERIAL_PROTOCOLLNPGM("Cap:CASE_LIGHT_BRIGHTNESS:0"); + #endif + + // EMERGENCY_PARSER (M108, M112, M410) + #if ENABLED(EMERGENCY_PARSER) + SERIAL_PROTOCOLLNPGM("Cap:EMERGENCY_PARSER:1"); + #else + SERIAL_PROTOCOLLNPGM("Cap:EMERGENCY_PARSER:0"); + #endif + + #endif // EXTENDED_CAPABILITIES_REPORT +} + +/** + * M117: Set LCD Status Message + */ +inline void gcode_M117() { lcd_setstatus(parser.string_arg); } + +/** + * M118: Display a message in the host console. + * + * A1 Append '// ' for an action command, as in OctoPrint + * E1 Have the host 'echo:' the text + */ +inline void gcode_M118() { + if (parser.boolval('E')) SERIAL_ECHO_START(); + if (parser.boolval('A')) SERIAL_ECHOPGM("// "); + SERIAL_ECHOLN(parser.string_arg); +} + +/** + * M119: Output endstop states to serial output + */ +inline void gcode_M119() { endstops.M119(); } + +/** + * M120: Enable endstops and set non-homing endstop state to "enabled" + */ +inline void gcode_M120() { endstops.enable_globally(true); } + +/** + * M121: Disable endstops and set non-homing endstop state to "disabled" + */ +inline void gcode_M121() { endstops.enable_globally(false); } + +#if ENABLED(PARK_HEAD_ON_PAUSE) + + /** + * M125: Store current position and move to filament change position. + * Called on pause (by M25) to prevent material leaking onto the + * object. On resume (M24) the head will be moved back and the + * print will resume. + * + * If Marlin is compiled without SD Card support, M125 can be + * used directly to pause the print and move to park position, + * resuming with a button click or M108. + * + * L = override retract length + * X = override X + * Y = override Y + * Z = override Z raise + */ + inline void gcode_M125() { + + // Initial retract before move to filament change position + const float retract = parser.seen('L') ? parser.value_axis_units(E_AXIS) : 0 + #ifdef PAUSE_PARK_RETRACT_LENGTH + - (PAUSE_PARK_RETRACT_LENGTH) + #endif + ; + + // Lift Z axis + const float z_lift = parser.linearval('Z') + #ifdef PAUSE_PARK_Z_ADD + + PAUSE_PARK_Z_ADD + #endif + ; + + // Move XY axes to filament change position or given position + const float x_pos = parser.linearval('X') + #ifdef PAUSE_PARK_X_POS + + PAUSE_PARK_X_POS + #endif + #if HOTENDS > 1 && DISABLED(DUAL_X_CARRIAGE) + + (active_extruder ? hotend_offset[X_AXIS][active_extruder] : 0) + #endif + ; + const float y_pos = parser.linearval('Y') + #ifdef PAUSE_PARK_Y_POS + + PAUSE_PARK_Y_POS + #endif + #if HOTENDS > 1 && DISABLED(DUAL_X_CARRIAGE) + + (active_extruder ? hotend_offset[Y_AXIS][active_extruder] : 0) + #endif + ; + + #if DISABLED(SDSUPPORT) + const bool job_running = print_job_timer.isRunning(); + #endif + + if (pause_print(retract, z_lift, x_pos, y_pos)) { + #if DISABLED(SDSUPPORT) + // Wait for lcd click or M108 + wait_for_filament_reload(); + + // Return to print position and continue + resume_print(); + + if (job_running) print_job_timer.start(); + #endif + } + } + +#endif // PARK_HEAD_ON_PAUSE + +#if HAS_COLOR_LEDS + + /** + * M150: Set Status LED Color - Use R-U-B-W for R-G-B-W + * and Brightness - Use P (for NEOPIXEL only) + * + * Always sets all 3 or 4 components. If a component is left out, set to 0. + * If brightness is left out, no value changed + * + * Examples: + * + * M150 R255 ; Turn LED red + * M150 R255 U127 ; Turn LED orange (PWM only) + * M150 ; Turn LED off + * M150 R U B ; Turn LED white + * M150 W ; Turn LED white using a white LED + * M150 P127 ; Set LED 50% brightness + * M150 P ; Set LED full brightness + */ + inline void gcode_M150() { + set_led_color( + parser.seen('R') ? (parser.has_value() ? parser.value_byte() : 255) : 0, + parser.seen('U') ? (parser.has_value() ? parser.value_byte() : 255) : 0, + parser.seen('B') ? (parser.has_value() ? parser.value_byte() : 255) : 0 + #if ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_LED) + , parser.seen('W') ? (parser.has_value() ? parser.value_byte() : 255) : 0 + #if ENABLED(NEOPIXEL_LED) + , parser.seen('P') ? (parser.has_value() ? parser.value_byte() : 255) : pixels.getBrightness() + #endif + #endif + ); + } + +#endif // HAS_COLOR_LEDS + +/** + * M200: Set filament diameter and set E axis units to cubic units + * + * T - Optional extruder number. Current extruder if omitted. + * D - Diameter of the filament. Use "D0" to switch back to linear units on the E axis. + */ +inline void gcode_M200() { + + if (get_target_extruder_from_command(200)) return; + + if (parser.seen('D')) { + // setting any extruder filament size disables volumetric on the assumption that + // slicers either generate in extruder values as cubic mm or as as filament feeds + // for all extruders + if ( (parser.volumetric_enabled = (parser.value_linear_units() != 0.0)) ) { + planner.filament_size[target_extruder] = parser.value_linear_units(); + // make sure all extruders have some sane value for the filament size + for (uint8_t i = 0; i < COUNT(planner.filament_size); i++) + if (!planner.filament_size[i]) planner.filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA; + } + } + planner.calculate_volumetric_multipliers(); +} + +/** + * M201: Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000) + * + * With multiple extruders use T to specify which one. + */ +inline void gcode_M201() { + + GET_TARGET_EXTRUDER(201); + + LOOP_XYZE(i) { + if (parser.seen(axis_codes[i])) { + const uint8_t a = i + (i == E_AXIS ? TARGET_EXTRUDER : 0); + planner.max_acceleration_mm_per_s2[a] = parser.value_axis_units((AxisEnum)a); + } + } + // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner) + planner.reset_acceleration_rates(); +} + +#if 0 // Not used for Sprinter/grbl gen6 + inline void gcode_M202() { + LOOP_XYZE(i) { + if (parser.seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = parser.value_axis_units((AxisEnum)i) * planner.axis_steps_per_mm[i]; + } + } +#endif + + +/** + * M203: Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in units/sec + * + * With multiple extruders use T to specify which one. + */ +inline void gcode_M203() { + + GET_TARGET_EXTRUDER(203); + + LOOP_XYZE(i) + if (parser.seen(axis_codes[i])) { + const uint8_t a = i + (i == E_AXIS ? TARGET_EXTRUDER : 0); + planner.max_feedrate_mm_s[a] = parser.value_axis_units((AxisEnum)a); + } +} + +/** + * M204: Set Accelerations in units/sec^2 (M204 P1200 R3000 T3000) + * + * P = Printing moves + * R = Retract only (no X, Y, Z) moves + * T = Travel (non printing) moves + * + * Also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate + */ +inline void gcode_M204() { + if (parser.seen('S')) { // Kept for legacy compatibility. Should NOT BE USED for new developments. + planner.travel_acceleration = planner.acceleration = parser.value_linear_units(); + SERIAL_ECHOLNPAIR("Setting Print and Travel Acceleration: ", planner.acceleration); + } + if (parser.seen('P')) { + planner.acceleration = parser.value_linear_units(); + SERIAL_ECHOLNPAIR("Setting Print Acceleration: ", planner.acceleration); + } + if (parser.seen('R')) { + planner.retract_acceleration = parser.value_linear_units(); + SERIAL_ECHOLNPAIR("Setting Retract Acceleration: ", planner.retract_acceleration); + } + if (parser.seen('T')) { + planner.travel_acceleration = parser.value_linear_units(); + SERIAL_ECHOLNPAIR("Setting Travel Acceleration: ", planner.travel_acceleration); + } +} + +/** + * M205: Set Advanced Settings + * + * S = Min Feed Rate (units/s) + * T = Min Travel Feed Rate (units/s) + * B = Min Segment Time (µs) + * X = Max X Jerk (units/sec^2) + * Y = Max Y Jerk (units/sec^2) + * Z = Max Z Jerk (units/sec^2) + * E = Max E Jerk (units/sec^2) + */ +inline void gcode_M205() { + if (parser.seen('S')) planner.min_feedrate_mm_s = parser.value_linear_units(); + if (parser.seen('T')) planner.min_travel_feedrate_mm_s = parser.value_linear_units(); + if (parser.seen('B')) planner.min_segment_time_us = parser.value_ulong(); + if (parser.seen('X')) planner.max_jerk[X_AXIS] = parser.value_linear_units(); + if (parser.seen('Y')) planner.max_jerk[Y_AXIS] = parser.value_linear_units(); + if (parser.seen('Z')) planner.max_jerk[Z_AXIS] = parser.value_linear_units(); + if (parser.seen('E')) planner.max_jerk[E_AXIS] = parser.value_linear_units(); +} + +#if HAS_M206_COMMAND + + /** + * M206: Set Additional Homing Offset (X Y Z). SCARA aliases T=X, P=Y + * + * *** @thinkyhead: I recommend deprecating M206 for SCARA in favor of M665. + * *** M206 for SCARA will remain enabled in 1.1.x for compatibility. + * *** In the next 1.2 release, it will simply be disabled by default. + */ + inline void gcode_M206() { + LOOP_XYZ(i) + if (parser.seen(axis_codes[i])) + set_home_offset((AxisEnum)i, parser.value_linear_units()); + + #if ENABLED(MORGAN_SCARA) + if (parser.seen('T')) set_home_offset(A_AXIS, parser.value_linear_units()); // Theta + if (parser.seen('P')) set_home_offset(B_AXIS, parser.value_linear_units()); // Psi + #endif + + report_current_position(); + } + +#endif // HAS_M206_COMMAND + +#if ENABLED(DELTA) + /** + * M665: Set delta configurations + * + * H = delta height + * L = diagonal rod + * R = delta radius + * S = segments per second + * B = delta calibration radius + * X = Alpha (Tower 1) angle trim + * Y = Beta (Tower 2) angle trim + * Z = Rotate A and B by this angle + */ + inline void gcode_M665() { + if (parser.seen('H')) { + delta_height = parser.value_linear_units(); + update_software_endstops(Z_AXIS); + } + if (parser.seen('L')) delta_diagonal_rod = parser.value_linear_units(); + if (parser.seen('R')) delta_radius = parser.value_linear_units(); + if (parser.seen('S')) delta_segments_per_second = parser.value_float(); + if (parser.seen('B')) delta_calibration_radius = parser.value_float(); + if (parser.seen('X')) delta_tower_angle_trim[A_AXIS] = parser.value_float(); + if (parser.seen('Y')) delta_tower_angle_trim[B_AXIS] = parser.value_float(); + if (parser.seen('Z')) delta_tower_angle_trim[C_AXIS] = parser.value_float(); + recalc_delta_settings(); + } + /** + * M666: Set delta endstop adjustment + */ + inline void gcode_M666() { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPGM(">>> gcode_M666"); + } + #endif + LOOP_XYZ(i) { + if (parser.seen(axis_codes[i])) { + if (parser.value_linear_units() * Z_HOME_DIR <= 0) + delta_endstop_adj[i] = parser.value_linear_units(); + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("delta_endstop_adj[", axis_codes[i]); + SERIAL_ECHOLNPAIR("] = ", delta_endstop_adj[i]); + } + #endif + } + } + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPGM("<<< gcode_M666"); + } + #endif + } + +#elif IS_SCARA + + /** + * M665: Set SCARA settings + * + * Parameters: + * + * S[segments-per-second] - Segments-per-second + * P[theta-psi-offset] - Theta-Psi offset, added to the shoulder (A/X) angle + * T[theta-offset] - Theta offset, added to the elbow (B/Y) angle + * + * A, P, and X are all aliases for the shoulder angle + * B, T, and Y are all aliases for the elbow angle + */ + inline void gcode_M665() { + if (parser.seen('S')) delta_segments_per_second = parser.value_float(); + + const bool hasA = parser.seen('A'), hasP = parser.seen('P'), hasX = parser.seen('X'); + const uint8_t sumAPX = hasA + hasP + hasX; + if (sumAPX == 1) + home_offset[A_AXIS] = parser.value_float(); + else if (sumAPX > 1) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Only one of A, P, or X is allowed."); + return; + } + + const bool hasB = parser.seen('B'), hasT = parser.seen('T'), hasY = parser.seen('Y'); + const uint8_t sumBTY = hasB + hasT + hasY; + if (sumBTY == 1) + home_offset[B_AXIS] = parser.value_float(); + else if (sumBTY > 1) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("Only one of B, T, or Y is allowed."); + return; + } + } + + + +#elif ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS) + + /** + * M666: For Z Dual Endstop setup, set z axis offset to the z2 axis. + */ + inline void gcode_M666() { + SERIAL_ECHOPGM("Dual Endstop Adjustment (mm): "); + #if ENABLED(X_DUAL_ENDSTOPS) + if (parser.seen('X')) x_endstop_adj = parser.value_linear_units(); + SERIAL_ECHOPAIR(" X", x_endstop_adj); + #endif + #if ENABLED(Y_DUAL_ENDSTOPS) + if (parser.seen('Y')) y_endstop_adj = parser.value_linear_units(); + SERIAL_ECHOPAIR(" Y", y_endstop_adj); + #endif + #if ENABLED(Z_DUAL_ENDSTOPS) + if (parser.seen('Z')) z_endstop_adj = parser.value_linear_units(); + SERIAL_ECHOPAIR(" Z", z_endstop_adj); + #endif + SERIAL_EOL(); + } + +#endif // !DELTA && Z_DUAL_ENDSTOPS + +#if ENABLED(FWRETRACT) + + /** + * M207: Set firmware retraction values + * + * S[+units] retract_length + * W[+units] swap_retract_length (multi-extruder) + * F[units/min] retract_feedrate_mm_s + * Z[units] retract_zlift + */ + inline void gcode_M207() { + if (parser.seen('S')) retract_length = parser.value_axis_units(E_AXIS); + if (parser.seen('F')) retract_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS)); + if (parser.seen('Z')) retract_zlift = parser.value_linear_units(); + if (parser.seen('W')) swap_retract_length = parser.value_axis_units(E_AXIS); + } + + /** + * M208: Set firmware un-retraction values + * + * S[+units] retract_recover_length (in addition to M207 S*) + * W[+units] swap_retract_recover_length (multi-extruder) + * F[units/min] retract_recover_feedrate_mm_s + * R[units/min] swap_retract_recover_feedrate_mm_s + */ + inline void gcode_M208() { + if (parser.seen('S')) retract_recover_length = parser.value_axis_units(E_AXIS); + if (parser.seen('F')) retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS)); + if (parser.seen('R')) swap_retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS)); + if (parser.seen('W')) swap_retract_recover_length = parser.value_axis_units(E_AXIS); + } + + /** + * M209: Enable automatic retract (M209 S1) + * For slicers that don't support G10/11, reversed extrude-only + * moves will be classified as retraction. + */ + inline void gcode_M209() { + if (MIN_AUTORETRACT <= MAX_AUTORETRACT) { + if (parser.seen('S')) { + autoretract_enabled = parser.value_bool(); + for (uint8_t i = 0; i < EXTRUDERS; i++) retracted[i] = false; + } + } + } + +#endif // FWRETRACT + +/** + * M211: Enable, Disable, and/or Report software endstops + * + * Usage: M211 S1 to enable, M211 S0 to disable, M211 alone for report + */ +inline void gcode_M211() { + SERIAL_ECHO_START(); + #if HAS_SOFTWARE_ENDSTOPS + if (parser.seen('S')) soft_endstops_enabled = parser.value_bool(); + SERIAL_ECHOPGM(MSG_SOFT_ENDSTOPS); + serialprintPGM(soft_endstops_enabled ? PSTR(MSG_ON) : PSTR(MSG_OFF)); + #else + SERIAL_ECHOPGM(MSG_SOFT_ENDSTOPS); + SERIAL_ECHOPGM(MSG_OFF); + #endif + SERIAL_ECHOPGM(MSG_SOFT_MIN); + SERIAL_ECHOPAIR( MSG_X, soft_endstop_min[X_AXIS]); + SERIAL_ECHOPAIR(" " MSG_Y, soft_endstop_min[Y_AXIS]); + SERIAL_ECHOPAIR(" " MSG_Z, soft_endstop_min[Z_AXIS]); + SERIAL_ECHOPGM(MSG_SOFT_MAX); + SERIAL_ECHOPAIR( MSG_X, soft_endstop_max[X_AXIS]); + SERIAL_ECHOPAIR(" " MSG_Y, soft_endstop_max[Y_AXIS]); + SERIAL_ECHOLNPAIR(" " MSG_Z, soft_endstop_max[Z_AXIS]); +} + +#if HOTENDS > 1 + + /** + * M218 - set hotend offset (in linear units) + * + * T + * X + * Y + * Z - Available with DUAL_X_CARRIAGE and SWITCHING_NOZZLE + */ + inline void gcode_M218() { + if (get_target_extruder_from_command(218) || target_extruder == 0) return; + + if (parser.seenval('X')) hotend_offset[X_AXIS][target_extruder] = parser.value_linear_units(); + if (parser.seenval('Y')) hotend_offset[Y_AXIS][target_extruder] = parser.value_linear_units(); + + #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(SWITCHING_NOZZLE) || ENABLED(PARKING_EXTRUDER) + if (parser.seenval('Z')) hotend_offset[Z_AXIS][target_extruder] = parser.value_linear_units(); + #endif + + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_HOTEND_OFFSET); + HOTEND_LOOP() { + SERIAL_CHAR(' '); + SERIAL_ECHO(hotend_offset[X_AXIS][e]); + SERIAL_CHAR(','); + SERIAL_ECHO(hotend_offset[Y_AXIS][e]); + #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(SWITCHING_NOZZLE) || ENABLED(PARKING_EXTRUDER) + SERIAL_CHAR(','); + SERIAL_ECHO(hotend_offset[Z_AXIS][e]); + #endif + } + SERIAL_EOL(); + } + +#endif // HOTENDS > 1 + +/** + * M220: Set speed percentage factor, aka "Feed Rate" (M220 S95) + */ +inline void gcode_M220() { + if (parser.seenval('S')) feedrate_percentage = parser.value_int(); +} + +/** + * M221: Set extrusion percentage (M221 T0 S95) + */ +inline void gcode_M221() { + if (get_target_extruder_from_command(221)) return; + if (parser.seenval('S')) { + planner.flow_percentage[target_extruder] = parser.value_int(); + planner.refresh_e_factor(target_extruder); + } +} + +/** + * M226: Wait until the specified pin reaches the state required (M226 P S) + */ +inline void gcode_M226() { + if (parser.seen('P')) { + const int pin_number = parser.value_int(), + pin_state = parser.intval('S', -1); // required pin state - default is inverted + + if (WITHIN(pin_state, -1, 1) && pin_number > -1 && !pin_is_protected(pin_number)) { + + int target = LOW; + + stepper.synchronize(); + + pinMode(pin_number, INPUT); + switch (pin_state) { + case 1: + target = HIGH; + break; + case 0: + target = LOW; + break; + case -1: + target = !digitalRead(pin_number); + break; + } + + while (digitalRead(pin_number) != target) idle(); + + } // pin_state -1 0 1 && pin_number > -1 + } // parser.seen('P') +} + +#if ENABLED(EXPERIMENTAL_I2CBUS) + + /** + * M260: Send data to a I2C slave device + * + * This is a PoC, the formating and arguments for the GCODE will + * change to be more compatible, the current proposal is: + * + * M260 A ; Sets the I2C slave address the data will be sent to + * + * M260 B + * M260 B + * M260 B + * + * M260 S1 ; Send the buffered data and reset the buffer + * M260 R1 ; Reset the buffer without sending data + * + */ + inline void gcode_M260() { + // Set the target address + if (parser.seen('A')) i2c.address(parser.value_byte()); + + // Add a new byte to the buffer + if (parser.seen('B')) i2c.addbyte(parser.value_byte()); + + // Flush the buffer to the bus + if (parser.seen('S')) i2c.send(); + + // Reset and rewind the buffer + else if (parser.seen('R')) i2c.reset(); + } + + /** + * M261: Request X bytes from I2C slave device + * + * Usage: M261 A B + */ + inline void gcode_M261() { + if (parser.seen('A')) i2c.address(parser.value_byte()); + + uint8_t bytes = parser.byteval('B', 1); + + if (i2c.addr && bytes && bytes <= TWIBUS_BUFFER_SIZE) { + i2c.relay(bytes); + } + else { + SERIAL_ERROR_START(); + SERIAL_ERRORLN("Bad i2c request"); + } + } + +#endif // EXPERIMENTAL_I2CBUS + +#if HAS_SERVOS + + /** + * M280: Get or set servo position. P [S] + */ + inline void gcode_M280() { + if (!parser.seen('P')) return; + const int servo_index = parser.value_int(); + if (WITHIN(servo_index, 0, NUM_SERVOS - 1)) { + if (parser.seen('S')) + MOVE_SERVO(servo_index, parser.value_int()); + else { + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR(" Servo ", servo_index); + SERIAL_ECHOLNPAIR(": ", servo[servo_index].read()); + } + } + else { + SERIAL_ERROR_START(); + SERIAL_ECHOPAIR("Servo ", servo_index); + SERIAL_ECHOLNPGM(" out of range"); + } + } + +#endif // HAS_SERVOS + +#if ENABLED(BABYSTEPPING) + + /** + * M290: Babystepping + */ + inline void gcode_M290() { + #if ENABLED(BABYSTEP_XY) + for (uint8_t a = X_AXIS; a <= Z_AXIS; a++) + if (parser.seenval(axis_codes[a]) || (a == Z_AXIS && parser.seenval('S'))) { + float offs = parser.value_axis_units(a); + constrain(offs, -2, 2); + #if ENABLED(BABYSTEP_ZPROBE_OFFSET) + if (a == Z_AXIS) { + zprobe_zoffset += offs; + refresh_zprobe_zoffset(true); // 'true' to not babystep + } + #endif + thermalManager.babystep_axis(a, offs * planner.axis_steps_per_mm[a]); + } + #else + if (parser.seenval('Z') || parser.seenval('S')) { + float offs = parser.value_axis_units(Z_AXIS); + constrain(offs, -2, 2); + #if ENABLED(BABYSTEP_ZPROBE_OFFSET) + zprobe_zoffset += offs; + refresh_zprobe_zoffset(); // This will babystep the axis + #else + thermalManager.babystep_axis(Z_AXIS, parser.value_axis_units(Z_AXIS) * planner.axis_steps_per_mm[Z_AXIS]); + #endif + } + #endif + } + +#endif // BABYSTEPPING + +#if HAS_BUZZER + + /** + * M300: Play beep sound S P + */ + inline void gcode_M300() { + uint16_t const frequency = parser.ushortval('S', 260); + uint16_t duration = parser.ushortval('P', 1000); + + // Limits the tone duration to 0-5 seconds. + NOMORE(duration, 5000); + + BUZZ(duration, frequency); + } + +#endif // HAS_BUZZER + +#if ENABLED(PIDTEMP) + + /** + * M301: Set PID parameters P I D (and optionally C, L) + * + * P[float] Kp term + * I[float] Ki term (unscaled) + * D[float] Kd term (unscaled) + * + * With PID_EXTRUSION_SCALING: + * + * C[float] Kc term + * L[float] LPQ length + */ + inline void gcode_M301() { + + // multi-extruder PID patch: M301 updates or prints a single extruder's PID values + // default behaviour (omitting E parameter) is to update for extruder 0 only + const uint8_t e = parser.byteval('E'); // extruder being updated + + if (e < HOTENDS) { // catch bad input value + if (parser.seen('P')) PID_PARAM(Kp, e) = parser.value_float(); + if (parser.seen('I')) PID_PARAM(Ki, e) = scalePID_i(parser.value_float()); + if (parser.seen('D')) PID_PARAM(Kd, e) = scalePID_d(parser.value_float()); + #if ENABLED(PID_EXTRUSION_SCALING) + if (parser.seen('C')) PID_PARAM(Kc, e) = parser.value_float(); + if (parser.seen('L')) lpq_len = parser.value_float(); + NOMORE(lpq_len, LPQ_MAX_LEN); + #endif + + thermalManager.updatePID(); + SERIAL_ECHO_START(); + #if ENABLED(PID_PARAMS_PER_HOTEND) + SERIAL_ECHOPAIR(" e:", e); // specify extruder in serial output + #endif // PID_PARAMS_PER_HOTEND + SERIAL_ECHOPAIR(" p:", PID_PARAM(Kp, e)); + SERIAL_ECHOPAIR(" i:", unscalePID_i(PID_PARAM(Ki, e))); + SERIAL_ECHOPAIR(" d:", unscalePID_d(PID_PARAM(Kd, e))); + #if ENABLED(PID_EXTRUSION_SCALING) + //Kc does not have scaling applied above, or in resetting defaults + SERIAL_ECHOPAIR(" c:", PID_PARAM(Kc, e)); + #endif + SERIAL_EOL(); + } + else { + SERIAL_ERROR_START(); + SERIAL_ERRORLN(MSG_INVALID_EXTRUDER); + } + } + +#endif // PIDTEMP + +#if ENABLED(PIDTEMPBED) + + inline void gcode_M304() { + if (parser.seen('P')) thermalManager.bedKp = parser.value_float(); + if (parser.seen('I')) thermalManager.bedKi = scalePID_i(parser.value_float()); + if (parser.seen('D')) thermalManager.bedKd = scalePID_d(parser.value_float()); + + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR(" p:", thermalManager.bedKp); + SERIAL_ECHOPAIR(" i:", unscalePID_i(thermalManager.bedKi)); + SERIAL_ECHOLNPAIR(" d:", unscalePID_d(thermalManager.bedKd)); + } + +#endif // PIDTEMPBED + +#if defined(CHDK) || HAS_PHOTOGRAPH + + /** + * M240: Trigger a camera by emulating a Canon RC-1 + * See http://www.doc-diy.net/photo/rc-1_hacked/ + */ + inline void gcode_M240() { + #ifdef CHDK + + OUT_WRITE(CHDK, HIGH); + chdkHigh = millis(); + chdkActive = true; + + #elif HAS_PHOTOGRAPH + + const uint8_t NUM_PULSES = 16; + const float PULSE_LENGTH = 0.01524; + for (int i = 0; i < NUM_PULSES; i++) { + WRITE(PHOTOGRAPH_PIN, HIGH); + _delay_ms(PULSE_LENGTH); + WRITE(PHOTOGRAPH_PIN, LOW); + _delay_ms(PULSE_LENGTH); + } + delay(7.33); + for (int i = 0; i < NUM_PULSES; i++) { + WRITE(PHOTOGRAPH_PIN, HIGH); + _delay_ms(PULSE_LENGTH); + WRITE(PHOTOGRAPH_PIN, LOW); + _delay_ms(PULSE_LENGTH); + } + + #endif // !CHDK && HAS_PHOTOGRAPH + } + +#endif // CHDK || PHOTOGRAPH_PIN + +#if HAS_LCD_CONTRAST + + /** + * M250: Read and optionally set the LCD contrast + */ + inline void gcode_M250() { + if (parser.seen('C')) set_lcd_contrast(parser.value_int()); + SERIAL_PROTOCOLPGM("lcd contrast value: "); + SERIAL_PROTOCOL(lcd_contrast); + SERIAL_EOL(); + } + +#endif // HAS_LCD_CONTRAST + +#if ENABLED(PREVENT_COLD_EXTRUSION) + + /** + * M302: Allow cold extrudes, or set the minimum extrude temperature + * + * S sets the minimum extrude temperature + * P enables (1) or disables (0) cold extrusion + * + * Examples: + * + * M302 ; report current cold extrusion state + * M302 P0 ; enable cold extrusion checking + * M302 P1 ; disables cold extrusion checking + * M302 S0 ; always allow extrusion (disables checking) + * M302 S170 ; only allow extrusion above 170 + * M302 S170 P1 ; set min extrude temp to 170 but leave disabled + */ + inline void gcode_M302() { + const bool seen_S = parser.seen('S'); + if (seen_S) { + thermalManager.extrude_min_temp = parser.value_celsius(); + thermalManager.allow_cold_extrude = (thermalManager.extrude_min_temp == 0); + } + + if (parser.seen('P')) + thermalManager.allow_cold_extrude = (thermalManager.extrude_min_temp == 0) || parser.value_bool(); + else if (!seen_S) { + // Report current state + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR("Cold extrudes are ", (thermalManager.allow_cold_extrude ? "en" : "dis")); + SERIAL_ECHOPAIR("abled (min temp ", thermalManager.extrude_min_temp); + SERIAL_ECHOLNPGM("C)"); + } + } + +#endif // PREVENT_COLD_EXTRUSION + +/** + * M303: PID relay autotune + * + * S sets the target temperature. (default 150C) + * E (-1 for the bed) (default 0) + * C + * U with a non-zero value will apply the result to current settings + */ +inline void gcode_M303() { + #if HAS_PID_HEATING + const int e = parser.intval('E'), c = parser.intval('C', 5); + const bool u = parser.boolval('U'); + + int16_t temp = parser.celsiusval('S', e < 0 ? 70 : 150); + + if (WITHIN(e, 0, HOTENDS - 1)) + target_extruder = e; + + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(NOT_BUSY); + #endif + + thermalManager.PID_autotune(temp, e, c, u); + + #if DISABLED(BUSY_WHILE_HEATING) + KEEPALIVE_STATE(IN_HANDLER); + #endif + #else + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M303_DISABLED); + #endif +} + +#if ENABLED(MORGAN_SCARA) + + bool SCARA_move_to_cal(const uint8_t delta_a, const uint8_t delta_b) { + if (IsRunning()) { + forward_kinematics_SCARA(delta_a, delta_b); + destination[X_AXIS] = cartes[X_AXIS]; + destination[Y_AXIS] = cartes[Y_AXIS]; + destination[Z_AXIS] = current_position[Z_AXIS]; + prepare_move_to_destination(); + return true; + } + return false; + } + + /** + * M360: SCARA calibration: Move to cal-position ThetaA (0 deg calibration) + */ + inline bool gcode_M360() { + SERIAL_ECHOLNPGM(" Cal: Theta 0"); + return SCARA_move_to_cal(0, 120); + } + + /** + * M361: SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree) + */ + inline bool gcode_M361() { + SERIAL_ECHOLNPGM(" Cal: Theta 90"); + return SCARA_move_to_cal(90, 130); + } + + /** + * M362: SCARA calibration: Move to cal-position PsiA (0 deg calibration) + */ + inline bool gcode_M362() { + SERIAL_ECHOLNPGM(" Cal: Psi 0"); + return SCARA_move_to_cal(60, 180); + } + + /** + * M363: SCARA calibration: Move to cal-position PsiB (90 deg calibration - steps per degree) + */ + inline bool gcode_M363() { + SERIAL_ECHOLNPGM(" Cal: Psi 90"); + return SCARA_move_to_cal(50, 90); + } + + /** + * M364: SCARA calibration: Move to cal-position PsiC (90 deg to Theta calibration position) + */ + inline bool gcode_M364() { + SERIAL_ECHOLNPGM(" Cal: Theta-Psi 90"); + return SCARA_move_to_cal(45, 135); + } + +#endif // SCARA + +#if ENABLED(EXT_SOLENOID) + + void enable_solenoid(const uint8_t num) { + switch (num) { + case 0: + OUT_WRITE(SOL0_PIN, HIGH); + break; + #if HAS_SOLENOID_1 && EXTRUDERS > 1 + case 1: + OUT_WRITE(SOL1_PIN, HIGH); + break; + #endif + #if HAS_SOLENOID_2 && EXTRUDERS > 2 + case 2: + OUT_WRITE(SOL2_PIN, HIGH); + break; + #endif + #if HAS_SOLENOID_3 && EXTRUDERS > 3 + case 3: + OUT_WRITE(SOL3_PIN, HIGH); + break; + #endif + #if HAS_SOLENOID_4 && EXTRUDERS > 4 + case 4: + OUT_WRITE(SOL4_PIN, HIGH); + break; + #endif + default: + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_INVALID_SOLENOID); + break; + } + } + + void enable_solenoid_on_active_extruder() { enable_solenoid(active_extruder); } + + void disable_all_solenoids() { + OUT_WRITE(SOL0_PIN, LOW); + #if HAS_SOLENOID_1 && EXTRUDERS > 1 + OUT_WRITE(SOL1_PIN, LOW); + #endif + #if HAS_SOLENOID_2 && EXTRUDERS > 2 + OUT_WRITE(SOL2_PIN, LOW); + #endif + #if HAS_SOLENOID_3 && EXTRUDERS > 3 + OUT_WRITE(SOL3_PIN, LOW); + #endif + #if HAS_SOLENOID_4 && EXTRUDERS > 4 + OUT_WRITE(SOL4_PIN, LOW); + #endif + } + + /** + * M380: Enable solenoid on the active extruder + */ + inline void gcode_M380() { enable_solenoid_on_active_extruder(); } + + /** + * M381: Disable all solenoids + */ + inline void gcode_M381() { disable_all_solenoids(); } + +#endif // EXT_SOLENOID + +/** + * M400: Finish all moves + */ +inline void gcode_M400() { stepper.synchronize(); } + +#if HAS_BED_PROBE + + /** + * M401: Engage Z Servo endstop if available + */ + inline void gcode_M401() { DEPLOY_PROBE(); } + + /** + * M402: Retract Z Servo endstop if enabled + */ + inline void gcode_M402() { STOW_PROBE(); } + +#endif // HAS_BED_PROBE + +#if ENABLED(FILAMENT_WIDTH_SENSOR) + + /** + * M404: Display or set (in current units) the nominal filament width (3mm, 1.75mm ) W<3.0> + */ + inline void gcode_M404() { + if (parser.seen('W')) { + filament_width_nominal = parser.value_linear_units(); + } + else { + SERIAL_PROTOCOLPGM("Filament dia (nominal mm):"); + SERIAL_PROTOCOLLN(filament_width_nominal); + } + } + + /** + * M405: Turn on filament sensor for control + */ + inline void gcode_M405() { + // This is technically a linear measurement, but since it's quantized to centimeters and is a different + // unit than everything else, it uses parser.value_byte() instead of parser.value_linear_units(). + if (parser.seen('D')) { + meas_delay_cm = parser.value_byte(); + NOMORE(meas_delay_cm, MAX_MEASUREMENT_DELAY); + } + + if (filwidth_delay_index[1] == -1) { // Initialize the ring buffer if not done since startup + const uint8_t temp_ratio = thermalManager.widthFil_to_size_ratio() - 100; // -100 to scale within a signed byte + + for (uint8_t i = 0; i < COUNT(measurement_delay); ++i) + measurement_delay[i] = temp_ratio; + + filwidth_delay_index[0] = filwidth_delay_index[1] = 0; + } + + filament_sensor = true; + + //SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); + //SERIAL_PROTOCOL(filament_width_meas); + //SERIAL_PROTOCOLPGM("Extrusion ratio(%):"); + //SERIAL_PROTOCOL(planner.flow_percentage[active_extruder]); + } + + /** + * M406: Turn off filament sensor for control + */ + inline void gcode_M406() { + filament_sensor = false; + planner.calculate_volumetric_multipliers(); // Restore correct 'volumetric_multiplier' value + } + + /** + * M407: Get measured filament diameter on serial output + */ + inline void gcode_M407() { + SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); + SERIAL_PROTOCOLLN(filament_width_meas); + } + +#endif // FILAMENT_WIDTH_SENSOR + +void quickstop_stepper() { + stepper.quick_stop(); + stepper.synchronize(); + set_current_from_steppers_for_axis(ALL_AXES); + SYNC_PLAN_POSITION_KINEMATIC(); +} + +#if HAS_LEVELING + /** + * M420: Enable/Disable Bed Leveling and/or set the Z fade height. + * + * S[bool] Turns leveling on or off + * Z[height] Sets the Z fade height (0 or none to disable) + * V[bool] Verbose - Print the leveling grid + * + * With AUTO_BED_LEVELING_UBL only: + * + * L[index] Load UBL mesh from index (0 is default) + */ + inline void gcode_M420() { + + #if ENABLED(AUTO_BED_LEVELING_UBL) + + // L to load a mesh from the EEPROM + if (parser.seen('L')) { + + #if ENABLED(EEPROM_SETTINGS) + const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot; + const int16_t a = settings.calc_num_meshes(); + + if (!a) { + SERIAL_PROTOCOLLNPGM("?EEPROM storage not available."); + return; + } + + if (!WITHIN(storage_slot, 0, a - 1)) { + SERIAL_PROTOCOLLNPGM("?Invalid storage slot."); + SERIAL_PROTOCOLLNPAIR("?Use 0 to ", a - 1); + return; + } + + settings.load_mesh(storage_slot); + ubl.storage_slot = storage_slot; + + #else + + SERIAL_PROTOCOLLNPGM("?EEPROM storage not available."); + return; + + #endif + } + + // L to load a mesh from the EEPROM + if (parser.seen('L') || parser.seen('V')) { + ubl.display_map(0); // Currently only supports one map type + SERIAL_ECHOLNPAIR("UBL_MESH_VALID = ", UBL_MESH_VALID); + SERIAL_ECHOLNPAIR("ubl.storage_slot = ", ubl.storage_slot); + } + + #endif // AUTO_BED_LEVELING_UBL + + // V to print the matrix or mesh + if (parser.seen('V')) { + #if ABL_PLANAR + planner.bed_level_matrix.debug(PSTR("Bed Level Correction Matrix:")); + #else + if (leveling_is_valid()) { + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + print_bilinear_leveling_grid(); + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + print_bilinear_leveling_grid_virt(); + #endif + #elif ENABLED(MESH_BED_LEVELING) + SERIAL_ECHOLNPGM("Mesh Bed Level data:"); + mbl_mesh_report(); + #endif + } + #endif + } + + const bool to_enable = parser.boolval('S'); + if (parser.seen('S')) + set_bed_leveling_enabled(to_enable); + + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units()); + #endif + + const bool new_status = planner.leveling_active; + + if (to_enable && !new_status) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M420_FAILED); + } + + SERIAL_ECHO_START(); + SERIAL_ECHOLNPAIR("Bed Leveling ", new_status ? MSG_ON : MSG_OFF); + + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + SERIAL_ECHO_START(); + SERIAL_ECHOPGM("Fade Height "); + if (planner.z_fade_height > 0.0) + SERIAL_ECHOLN(planner.z_fade_height); + else + SERIAL_ECHOLNPGM(MSG_OFF); + #endif + } +#endif + +#if ENABLED(MESH_BED_LEVELING) + + /** + * M421: Set a single Mesh Bed Leveling Z coordinate + * + * Usage: + * M421 X Y Z + * M421 X Y Q + * M421 I J Z + * M421 I J Q + */ + inline void gcode_M421() { + const bool hasX = parser.seen('X'), hasI = parser.seen('I'); + const int8_t ix = hasI ? parser.value_int() : hasX ? mbl.probe_index_x(parser.value_linear_units()) : -1; + const bool hasY = parser.seen('Y'), hasJ = parser.seen('J'); + const int8_t iy = hasJ ? parser.value_int() : hasY ? mbl.probe_index_y(parser.value_linear_units()) : -1; + const bool hasZ = parser.seen('Z'), hasQ = !hasZ && parser.seen('Q'); + + if (int(hasI && hasJ) + int(hasX && hasY) != 1 || !(hasZ || hasQ)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS); + } + else if (ix < 0 || iy < 0) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY); + } + else + mbl.set_z(ix, iy, parser.value_linear_units() + (hasQ ? mbl.z_values[ix][iy] : 0)); + } + +#elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + + /** + * M421: Set a single Mesh Bed Leveling Z coordinate + * + * Usage: + * M421 I J Z + * M421 I J Q + */ + inline void gcode_M421() { + int8_t ix = parser.intval('I', -1), iy = parser.intval('J', -1); + const bool hasI = ix >= 0, + hasJ = iy >= 0, + hasZ = parser.seen('Z'), + hasQ = !hasZ && parser.seen('Q'); + + if (!hasI || !hasJ || !(hasZ || hasQ)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS); + } + else if (!WITHIN(ix, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY); + } + else { + z_values[ix][iy] = parser.value_linear_units() + (hasQ ? z_values[ix][iy] : 0); + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + bed_level_virt_interpolate(); + #endif + } + } + +#elif ENABLED(AUTO_BED_LEVELING_UBL) + + /** + * M421: Set a single Mesh Bed Leveling Z coordinate + * + * Usage: + * M421 I J Z + * M421 I J Q + * M421 C Z + * M421 C Q + */ + inline void gcode_M421() { + int8_t ix = parser.intval('I', -1), iy = parser.intval('J', -1); + const bool hasI = ix >= 0, + hasJ = iy >= 0, + hasC = parser.seen('C'), + hasZ = parser.seen('Z'), + hasQ = !hasZ && parser.seen('Q'); + + if (hasC) { + const mesh_index_pair location = ubl.find_closest_mesh_point_of_type(REAL, current_position[X_AXIS], current_position[Y_AXIS], USE_NOZZLE_AS_REFERENCE, NULL); + ix = location.x_index; + iy = location.y_index; + } + + if (int(hasC) + int(hasI && hasJ) != 1 || !(hasZ || hasQ)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS); + } + else if (!WITHIN(ix, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1)) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY); + } + else + ubl.z_values[ix][iy] = parser.value_linear_units() + (hasQ ? ubl.z_values[ix][iy] : 0); + } + +#endif // AUTO_BED_LEVELING_UBL + +#if HAS_M206_COMMAND + + /** + * M428: Set home_offset based on the distance between the + * current_position and the nearest "reference point." + * If an axis is past center its endstop position + * is the reference-point. Otherwise it uses 0. This allows + * the Z offset to be set near the bed when using a max endstop. + * + * M428 can't be used more than 2cm away from 0 or an endstop. + * + * Use M206 to set these values directly. + */ + inline void gcode_M428() { + bool err = false; + LOOP_XYZ(i) { + if (axis_homed[i]) { + const float base = (current_position[i] > (soft_endstop_min[i] + soft_endstop_max[i]) * 0.5) ? base_home_pos((AxisEnum)i) : 0, + diff = base - current_position[i]; + if (WITHIN(diff, -20, 20)) { + set_home_offset((AxisEnum)i, diff); + } + else { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M428_TOO_FAR); + LCD_ALERTMESSAGEPGM("Err: Too far!"); + BUZZ(200, 40); + err = true; + break; + } + } + } + + if (!err) { + report_current_position(); + LCD_MESSAGEPGM(MSG_HOME_OFFSETS_APPLIED); + BUZZ(100, 659); + BUZZ(100, 698); + } + } + +#endif // HAS_M206_COMMAND + +/** + * M500: Store settings in EEPROM + */ +inline void gcode_M500() { + (void)settings.save(); +} + +/** + * M501: Read settings from EEPROM + */ +inline void gcode_M501() { + (void)settings.load(); +} + +/** + * M502: Revert to default settings + */ +inline void gcode_M502() { + (void)settings.reset(); +} + +#if DISABLED(DISABLE_M503) + /** + * M503: print settings currently in memory + */ + inline void gcode_M503() { + (void)settings.report(parser.boolval('S')); + } +#endif + +#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) + + /** + * M540: Set whether SD card print should abort on endstop hit (M540 S<0|1>) + */ + inline void gcode_M540() { + if (parser.seen('S')) stepper.abort_on_endstop_hit = parser.value_bool(); + } + +#endif // ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED + +#if HAS_BED_PROBE + + void refresh_zprobe_zoffset(const bool no_babystep/*=false*/) { + static float last_zoffset = NAN; + + if (!isnan(last_zoffset)) { + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(BABYSTEP_ZPROBE_OFFSET) || ENABLED(DELTA) + const float diff = zprobe_zoffset - last_zoffset; + #endif + + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + // Correct bilinear grid for new probe offset + if (diff) { + for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) + for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) + z_values[x][y] -= diff; + } + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + bed_level_virt_interpolate(); + #endif + #endif + + #if ENABLED(BABYSTEP_ZPROBE_OFFSET) + if (!no_babystep && planner.leveling_active) + thermalManager.babystep_axis(Z_AXIS, -LROUND(diff * planner.axis_steps_per_mm[Z_AXIS])); + #else + UNUSED(no_babystep); + #endif + + #if ENABLED(DELTA) // correct the delta_height + delta_height -= diff; + #endif + } + + last_zoffset = zprobe_zoffset; + } + + inline void gcode_M851() { + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_ZPROBE_ZOFFSET " "); + if (parser.seen('Z')) { + const float value = parser.value_linear_units(); + if (WITHIN(value, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) { + zprobe_zoffset = value; + refresh_zprobe_zoffset(); + SERIAL_ECHO(zprobe_zoffset); + } + else + SERIAL_ECHOPGM(MSG_Z_MIN " " STRINGIFY(Z_PROBE_OFFSET_RANGE_MIN) " " MSG_Z_MAX " " STRINGIFY(Z_PROBE_OFFSET_RANGE_MAX)); + } + else + SERIAL_ECHOPAIR(": ", zprobe_zoffset); + + SERIAL_EOL(); + } + +#endif // HAS_BED_PROBE + +#if ENABLED(ADVANCED_PAUSE_FEATURE) + + /** + * M600: Pause for filament change + * + * E[distance] - Retract the filament this far (negative value) + * Z[distance] - Move the Z axis by this distance + * X[position] - Move to this X position, with Y + * Y[position] - Move to this Y position, with X + * U[distance] - Retract distance for removal (negative value) (manual reload) + * L[distance] - Extrude distance for insertion (positive value) (manual reload) + * B[count] - Number of times to beep, -1 for indefinite (if equipped with a buzzer) + * + * Default values are used for omitted arguments. + * + */ + inline void gcode_M600() { + + #if ENABLED(HOME_BEFORE_FILAMENT_CHANGE) + // Don't allow filament change without homing first + if (axis_unhomed_error()) home_all_axes(); + #endif + + // Initial retract before move to filament change position + const float retract = parser.seen('E') ? parser.value_axis_units(E_AXIS) : 0 + #ifdef PAUSE_PARK_RETRACT_LENGTH + - (PAUSE_PARK_RETRACT_LENGTH) + #endif + ; + + // Lift Z axis + const float z_lift = parser.linearval('Z', 0 + #ifdef PAUSE_PARK_Z_ADD + + PAUSE_PARK_Z_ADD + #endif + ); + + // Move XY axes to filament exchange position + const float x_pos = parser.linearval('X', 0 + #ifdef PAUSE_PARK_X_POS + + PAUSE_PARK_X_POS + #endif + ); + const float y_pos = parser.linearval('Y', 0 + #ifdef PAUSE_PARK_Y_POS + + PAUSE_PARK_Y_POS + #endif + ); + + // Unload filament + const float unload_length = parser.seen('U') ? parser.value_axis_units(E_AXIS) : 0 + #if defined(FILAMENT_CHANGE_UNLOAD_LENGTH) && FILAMENT_CHANGE_UNLOAD_LENGTH > 0 + - (FILAMENT_CHANGE_UNLOAD_LENGTH) + #endif + ; + + // Load filament + const float load_length = parser.seen('L') ? parser.value_axis_units(E_AXIS) : 0 + #ifdef FILAMENT_CHANGE_LOAD_LENGTH + + FILAMENT_CHANGE_LOAD_LENGTH + #endif + ; + + const int beep_count = parser.intval('B', + #ifdef FILAMENT_CHANGE_NUMBER_OF_ALERT_BEEPS + FILAMENT_CHANGE_NUMBER_OF_ALERT_BEEPS + #else + -1 + #endif + ); + + const bool job_running = print_job_timer.isRunning(); + + if (pause_print(retract, z_lift, x_pos, y_pos, unload_length, beep_count, true)) { + wait_for_filament_reload(beep_count); + resume_print(load_length, ADVANCED_PAUSE_EXTRUDE_LENGTH, beep_count); + } + + // Resume the print job timer if it was running + if (job_running) print_job_timer.start(); + } + +#endif // ADVANCED_PAUSE_FEATURE + +#if ENABLED(MK2_MULTIPLEXER) + + inline void select_multiplexed_stepper(const uint8_t e) { + stepper.synchronize(); + disable_e_steppers(); + WRITE(E_MUX0_PIN, TEST(e, 0) ? HIGH : LOW); + WRITE(E_MUX1_PIN, TEST(e, 1) ? HIGH : LOW); + WRITE(E_MUX2_PIN, TEST(e, 2) ? HIGH : LOW); + safe_delay(100); + } + + /** + * M702: Unload all extruders + */ + inline void gcode_M702() { + for (uint8_t s = 0; s < E_STEPPERS; s++) { + select_multiplexed_stepper(e); + // TODO: standard unload filament function + // MK2 firmware behavior: + // - Make sure temperature is high enough + // - Raise Z to at least 15 to make room + // - Extrude 1cm of filament in 1 second + // - Under 230C quickly purge ~12mm, over 230C purge ~10mm + // - Change E max feedrate to 80, eject the filament from the tube. Sync. + // - Restore E max feedrate to 50 + } + // Go back to the last active extruder + select_multiplexed_stepper(active_extruder); + disable_e_steppers(); + } + +#endif // MK2_MULTIPLEXER + +#if ENABLED(DUAL_X_CARRIAGE) + + /** + * M605: Set dual x-carriage movement mode + * + * M605 S0: Full control mode. The slicer has full control over x-carriage movement + * M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement + * M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn + * units x-offset and an optional differential hotend temperature of + * mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate + * the first with a spacing of 100mm in the x direction and 2 degrees hotter. + * + * Note: the X axis should be homed after changing dual x-carriage mode. + */ + inline void gcode_M605() { + stepper.synchronize(); + if (parser.seen('S')) dual_x_carriage_mode = (DualXMode)parser.value_byte(); + switch (dual_x_carriage_mode) { + case DXC_FULL_CONTROL_MODE: + case DXC_AUTO_PARK_MODE: + break; + case DXC_DUPLICATION_MODE: + if (parser.seen('X')) duplicate_extruder_x_offset = max(parser.value_linear_units(), X2_MIN_POS - x_home_pos(0)); + if (parser.seen('R')) duplicate_extruder_temp_offset = parser.value_celsius_diff(); + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_HOTEND_OFFSET); + SERIAL_CHAR(' '); + SERIAL_ECHO(hotend_offset[X_AXIS][0]); + SERIAL_CHAR(','); + SERIAL_ECHO(hotend_offset[Y_AXIS][0]); + SERIAL_CHAR(' '); + SERIAL_ECHO(duplicate_extruder_x_offset); + SERIAL_CHAR(','); + SERIAL_ECHOLN(hotend_offset[Y_AXIS][1]); + break; + default: + dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE; + break; + } + active_extruder_parked = false; + extruder_duplication_enabled = false; + delayed_move_time = 0; + } + +#elif ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + + inline void gcode_M605() { + stepper.synchronize(); + extruder_duplication_enabled = parser.intval('S') == (int)DXC_DUPLICATION_MODE; + SERIAL_ECHO_START(); + SERIAL_ECHOLNPAIR(MSG_DUPLICATION_MODE, extruder_duplication_enabled ? MSG_ON : MSG_OFF); + } + +#endif // DUAL_NOZZLE_DUPLICATION_MODE + +#if ENABLED(LIN_ADVANCE) + /** + * M900: Set and/or Get advance K factor and WH/D ratio + * + * K Set advance K factor + * R Set ratio directly (overrides WH/D) + * W H D Set ratio from WH/D + */ + inline void gcode_M900() { + stepper.synchronize(); + + const float newK = parser.floatval('K', -1); + if (newK >= 0) planner.extruder_advance_k = newK; + + float newR = parser.floatval('R', -1); + if (newR < 0) { + const float newD = parser.floatval('D', -1), + newW = parser.floatval('W', -1), + newH = parser.floatval('H', -1); + if (newD >= 0 && newW >= 0 && newH >= 0) + newR = newD ? (newW * newH) / (sq(newD * 0.5) * M_PI) : 0; + } + if (newR >= 0) planner.advance_ed_ratio = newR; + + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR("Advance K=", planner.extruder_advance_k); + SERIAL_ECHOPGM(" E/D="); + const float ratio = planner.advance_ed_ratio; + if (ratio) SERIAL_ECHO(ratio); else SERIAL_ECHOPGM("Auto"); + SERIAL_EOL(); + } +#endif // LIN_ADVANCE + +#if ENABLED(HAVE_TMC2130) + + static void tmc2130_get_current(TMC2130Stepper &st, const char name) { + SERIAL_CHAR(name); + SERIAL_ECHOPGM(" axis driver current: "); + SERIAL_ECHOLN(st.getCurrent()); + } + static void tmc2130_set_current(TMC2130Stepper &st, const char name, const int mA) { + st.setCurrent(mA, R_SENSE, HOLD_MULTIPLIER); + tmc2130_get_current(st, name); + } + + static void tmc2130_report_otpw(TMC2130Stepper &st, const char name) { + SERIAL_CHAR(name); + SERIAL_ECHOPGM(" axis temperature prewarn triggered: "); + serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); + SERIAL_EOL(); + } + static void tmc2130_clear_otpw(TMC2130Stepper &st, const char name) { + st.clear_otpw(); + SERIAL_CHAR(name); + SERIAL_ECHOLNPGM(" prewarn flag cleared"); + } + + static void tmc2130_get_pwmthrs(TMC2130Stepper &st, const char name, const uint16_t spmm) { + SERIAL_CHAR(name); + SERIAL_ECHOPGM(" stealthChop max speed set to "); + SERIAL_ECHOLN(12650000UL * st.microsteps() / (256 * st.stealth_max_speed() * spmm)); + } + static void tmc2130_set_pwmthrs(TMC2130Stepper &st, const char name, const int32_t thrs, const uint32_t spmm) { + st.stealth_max_speed(12650000UL * st.microsteps() / (256 * thrs * spmm)); + tmc2130_get_pwmthrs(st, name, spmm); + } + + static void tmc2130_get_sgt(TMC2130Stepper &st, const char name) { + SERIAL_CHAR(name); + SERIAL_ECHOPGM(" driver homing sensitivity set to "); + SERIAL_ECHOLN(st.sgt()); + } + static void tmc2130_set_sgt(TMC2130Stepper &st, const char name, const int8_t sgt_val) { + st.sgt(sgt_val); + tmc2130_get_sgt(st, name); + } + + /** + * M906: Set motor current in milliamps using axis codes X, Y, Z, E + * Report driver currents when no axis specified + * + * S1: Enable automatic current control + * S0: Disable + */ + inline void gcode_M906() { + uint16_t values[XYZE]; + LOOP_XYZE(i) + values[i] = parser.intval(axis_codes[i]); + + #if ENABLED(X_IS_TMC2130) + if (values[X_AXIS]) tmc2130_set_current(stepperX, 'X', values[X_AXIS]); + else tmc2130_get_current(stepperX, 'X'); + #endif + #if ENABLED(Y_IS_TMC2130) + if (values[Y_AXIS]) tmc2130_set_current(stepperY, 'Y', values[Y_AXIS]); + else tmc2130_get_current(stepperY, 'Y'); + #endif + #if ENABLED(Z_IS_TMC2130) + if (values[Z_AXIS]) tmc2130_set_current(stepperZ, 'Z', values[Z_AXIS]); + else tmc2130_get_current(stepperZ, 'Z'); + #endif + #if ENABLED(E0_IS_TMC2130) + if (values[E_AXIS]) tmc2130_set_current(stepperE0, 'E', values[E_AXIS]); + else tmc2130_get_current(stepperE0, 'E'); + #endif + + #if ENABLED(AUTOMATIC_CURRENT_CONTROL) + if (parser.seen('S')) auto_current_control = parser.value_bool(); + #endif + } + + /** + * M911: Report TMC2130 stepper driver overtemperature pre-warn flag + * The flag is held by the library and persist until manually cleared by M912 + */ + inline void gcode_M911() { + const bool reportX = parser.seen('X'), reportY = parser.seen('Y'), reportZ = parser.seen('Z'), reportE = parser.seen('E'), + reportAll = (!reportX && !reportY && !reportZ && !reportE) || (reportX && reportY && reportZ && reportE); + #if ENABLED(X_IS_TMC2130) + if (reportX || reportAll) tmc2130_report_otpw(stepperX, 'X'); + #endif + #if ENABLED(Y_IS_TMC2130) + if (reportY || reportAll) tmc2130_report_otpw(stepperY, 'Y'); + #endif + #if ENABLED(Z_IS_TMC2130) + if (reportZ || reportAll) tmc2130_report_otpw(stepperZ, 'Z'); + #endif + #if ENABLED(E0_IS_TMC2130) + if (reportE || reportAll) tmc2130_report_otpw(stepperE0, 'E'); + #endif + } + + /** + * M912: Clear TMC2130 stepper driver overtemperature pre-warn flag held by the library + */ + inline void gcode_M912() { + const bool clearX = parser.seen('X'), clearY = parser.seen('Y'), clearZ = parser.seen('Z'), clearE = parser.seen('E'), + clearAll = (!clearX && !clearY && !clearZ && !clearE) || (clearX && clearY && clearZ && clearE); + #if ENABLED(X_IS_TMC2130) + if (clearX || clearAll) tmc2130_clear_otpw(stepperX, 'X'); + #endif + #if ENABLED(Y_IS_TMC2130) + if (clearY || clearAll) tmc2130_clear_otpw(stepperY, 'Y'); + #endif + #if ENABLED(Z_IS_TMC2130) + if (clearZ || clearAll) tmc2130_clear_otpw(stepperZ, 'Z'); + #endif + #if ENABLED(E0_IS_TMC2130) + if (clearE || clearAll) tmc2130_clear_otpw(stepperE0, 'E'); + #endif + } + + /** + * M913: Set HYBRID_THRESHOLD speed. + */ + #if ENABLED(HYBRID_THRESHOLD) + inline void gcode_M913() { + uint16_t values[XYZE]; + LOOP_XYZE(i) + values[i] = parser.intval(axis_codes[i]); + + #if ENABLED(X_IS_TMC2130) + if (values[X_AXIS]) tmc2130_set_pwmthrs(stepperX, 'X', values[X_AXIS], planner.axis_steps_per_mm[X_AXIS]); + else tmc2130_get_pwmthrs(stepperX, 'X', planner.axis_steps_per_mm[X_AXIS]); + #endif + #if ENABLED(Y_IS_TMC2130) + if (values[Y_AXIS]) tmc2130_set_pwmthrs(stepperY, 'Y', values[Y_AXIS], planner.axis_steps_per_mm[Y_AXIS]); + else tmc2130_get_pwmthrs(stepperY, 'Y', planner.axis_steps_per_mm[Y_AXIS]); + #endif + #if ENABLED(Z_IS_TMC2130) + if (values[Z_AXIS]) tmc2130_set_pwmthrs(stepperZ, 'Z', values[Z_AXIS], planner.axis_steps_per_mm[Z_AXIS]); + else tmc2130_get_pwmthrs(stepperZ, 'Z', planner.axis_steps_per_mm[Z_AXIS]); + #endif + #if ENABLED(E0_IS_TMC2130) + if (values[E_AXIS]) tmc2130_set_pwmthrs(stepperE0, 'E', values[E_AXIS], planner.axis_steps_per_mm[E_AXIS]); + else tmc2130_get_pwmthrs(stepperE0, 'E', planner.axis_steps_per_mm[E_AXIS]); + #endif + } + #endif // HYBRID_THRESHOLD + + /** + * M914: Set SENSORLESS_HOMING sensitivity. + */ + #if ENABLED(SENSORLESS_HOMING) + inline void gcode_M914() { + #if ENABLED(X_IS_TMC2130) + if (parser.seen(axis_codes[X_AXIS])) tmc2130_set_sgt(stepperX, 'X', parser.value_int()); + else tmc2130_get_sgt(stepperX, 'X'); + #endif + #if ENABLED(Y_IS_TMC2130) + if (parser.seen(axis_codes[Y_AXIS])) tmc2130_set_sgt(stepperY, 'Y', parser.value_int()); + else tmc2130_get_sgt(stepperY, 'Y'); + #endif + } + #endif // SENSORLESS_HOMING + +#endif // HAVE_TMC2130 + +/** + * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S + */ +inline void gcode_M907() { + #if HAS_DIGIPOTSS + + LOOP_XYZE(i) if (parser.seen(axis_codes[i])) stepper.digipot_current(i, parser.value_int()); + if (parser.seen('B')) stepper.digipot_current(4, parser.value_int()); + if (parser.seen('S')) for (uint8_t i = 0; i <= 4; i++) stepper.digipot_current(i, parser.value_int()); + + #elif HAS_MOTOR_CURRENT_PWM + + #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY) + if (parser.seen('X')) stepper.digipot_current(0, parser.value_int()); + #endif + #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z) + if (parser.seen('Z')) stepper.digipot_current(1, parser.value_int()); + #endif + #if PIN_EXISTS(MOTOR_CURRENT_PWM_E) + if (parser.seen('E')) stepper.digipot_current(2, parser.value_int()); + #endif + + #endif + + #if ENABLED(DIGIPOT_I2C) + // this one uses actual amps in floating point + LOOP_XYZE(i) if (parser.seen(axis_codes[i])) digipot_i2c_set_current(i, parser.value_float()); + // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...) + for (uint8_t i = NUM_AXIS; i < DIGIPOT_I2C_NUM_CHANNELS; i++) if (parser.seen('B' + i - (NUM_AXIS))) digipot_i2c_set_current(i, parser.value_float()); + #endif + + #if ENABLED(DAC_STEPPER_CURRENT) + if (parser.seen('S')) { + const float dac_percent = parser.value_float(); + for (uint8_t i = 0; i <= 4; i++) dac_current_percent(i, dac_percent); + } + LOOP_XYZE(i) if (parser.seen(axis_codes[i])) dac_current_percent(i, parser.value_float()); + #endif +} + +#if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT) + + /** + * M908: Control digital trimpot directly (M908 P S) + */ + inline void gcode_M908() { + #if HAS_DIGIPOTSS + stepper.digitalPotWrite( + parser.intval('P'), + parser.intval('S') + ); + #endif + #ifdef DAC_STEPPER_CURRENT + dac_current_raw( + parser.byteval('P', -1), + parser.ushortval('S', 0) + ); + #endif + } + + #if ENABLED(DAC_STEPPER_CURRENT) // As with Printrbot RevF + + inline void gcode_M909() { dac_print_values(); } + + inline void gcode_M910() { dac_commit_eeprom(); } + + #endif + +#endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT + +#if HAS_MICROSTEPS + + // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers. + inline void gcode_M350() { + if (parser.seen('S')) for (int i = 0; i <= 4; i++) stepper.microstep_mode(i, parser.value_byte()); + LOOP_XYZE(i) if (parser.seen(axis_codes[i])) stepper.microstep_mode(i, parser.value_byte()); + if (parser.seen('B')) stepper.microstep_mode(4, parser.value_byte()); + stepper.microstep_readings(); + } + + /** + * M351: Toggle MS1 MS2 pins directly with axis codes X Y Z E B + * S# determines MS1 or MS2, X# sets the pin high/low. + */ + inline void gcode_M351() { + if (parser.seenval('S')) switch (parser.value_byte()) { + case 1: + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.microstep_ms(i, parser.value_byte(), -1); + if (parser.seenval('B')) stepper.microstep_ms(4, parser.value_byte(), -1); + break; + case 2: + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.microstep_ms(i, -1, parser.value_byte()); + if (parser.seenval('B')) stepper.microstep_ms(4, -1, parser.value_byte()); + break; + } + stepper.microstep_readings(); + } + +#endif // HAS_MICROSTEPS + +#if HAS_CASE_LIGHT + #ifndef INVERT_CASE_LIGHT + #define INVERT_CASE_LIGHT false + #endif + uint8_t case_light_brightness; // LCD routine wants INT + bool case_light_on; + + void update_case_light() { + pinMode(CASE_LIGHT_PIN, OUTPUT); // digitalWrite doesn't set the port mode + if (case_light_on) { + if (USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) + analogWrite(CASE_LIGHT_PIN, INVERT_CASE_LIGHT ? 255 - case_light_brightness : case_light_brightness); + else + WRITE(CASE_LIGHT_PIN, INVERT_CASE_LIGHT ? LOW : HIGH); + } + else { + if (USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) + analogWrite(CASE_LIGHT_PIN, INVERT_CASE_LIGHT ? 255 : 0); + else + WRITE(CASE_LIGHT_PIN, INVERT_CASE_LIGHT ? HIGH : LOW); + } + } +#endif // HAS_CASE_LIGHT + +/** + * M355: Turn case light on/off and set brightness + * + * P Set case light brightness (PWM pin required - ignored otherwise) + * + * S Set case light on/off + * + * When S turns on the light on a PWM pin then the current brightness level is used/restored + * + * M355 P200 S0 turns off the light & sets the brightness level + * M355 S1 turns on the light with a brightness of 200 (assuming a PWM pin) + */ +inline void gcode_M355() { + #if HAS_CASE_LIGHT + uint8_t args = 0; + if (parser.seenval('P')) ++args, case_light_brightness = parser.value_byte(); + if (parser.seenval('S')) ++args, case_light_on = parser.value_bool(); + if (args) update_case_light(); + + // always report case light status + SERIAL_ECHO_START(); + if (!case_light_on) { + SERIAL_ECHOLN("Case light: off"); + } + else { + if (!USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) SERIAL_ECHOLN("Case light: on"); + else SERIAL_ECHOLNPAIR("Case light: ", (int)case_light_brightness); + } + + #else + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_M355_NONE); + #endif // HAS_CASE_LIGHT +} + +#if ENABLED(MIXING_EXTRUDER) + + /** + * M163: Set a single mix factor for a mixing extruder + * This is called "weight" by some systems. + * + * S[index] The channel index to set + * P[float] The mix value + * + */ + inline void gcode_M163() { + const int mix_index = parser.intval('S'); + if (mix_index < MIXING_STEPPERS) { + float mix_value = parser.floatval('P'); + NOLESS(mix_value, 0.0); + mixing_factor[mix_index] = RECIPROCAL(mix_value); + } + } + + #if MIXING_VIRTUAL_TOOLS > 1 + + /** + * M164: Store the current mix factors as a virtual tool. + * + * S[index] The virtual tool to store + * + */ + inline void gcode_M164() { + const int tool_index = parser.intval('S'); + if (tool_index < MIXING_VIRTUAL_TOOLS) { + normalize_mix(); + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + mixing_virtual_tool_mix[tool_index][i] = mixing_factor[i]; + } + } + + #endif + + #if ENABLED(DIRECT_MIXING_IN_G1) + /** + * M165: Set multiple mix factors for a mixing extruder. + * Factors that are left out will be set to 0. + * All factors together must add up to 1.0. + * + * A[factor] Mix factor for extruder stepper 1 + * B[factor] Mix factor for extruder stepper 2 + * C[factor] Mix factor for extruder stepper 3 + * D[factor] Mix factor for extruder stepper 4 + * H[factor] Mix factor for extruder stepper 5 + * I[factor] Mix factor for extruder stepper 6 + * + */ + inline void gcode_M165() { gcode_get_mix(); } + #endif + +#endif // MIXING_EXTRUDER + +/** + * M999: Restart after being stopped + * + * Default behaviour is to flush the serial buffer and request + * a resend to the host starting on the last N line received. + * + * Sending "M999 S1" will resume printing without flushing the + * existing command buffer. + * + */ +inline void gcode_M999() { + Running = true; + lcd_reset_alert_level(); + + if (parser.boolval('S')) return; + + // gcode_LastN = Stopped_gcode_LastN; + FlushSerialRequestResend(); +} + +#if ENABLED(SWITCHING_EXTRUDER) + #if EXTRUDERS > 3 + #define REQ_ANGLES 4 + #define _SERVO_NR (e < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR) + #else + #define REQ_ANGLES 2 + #define _SERVO_NR SWITCHING_EXTRUDER_SERVO_NR + #endif + inline void move_extruder_servo(const uint8_t e) { + constexpr int16_t angles[] = SWITCHING_EXTRUDER_SERVO_ANGLES; + static_assert(COUNT(angles) == REQ_ANGLES, "SWITCHING_EXTRUDER_SERVO_ANGLES needs " STRINGIFY(REQ_ANGLES) " angles."); + stepper.synchronize(); + #if EXTRUDERS & 1 + if (e < EXTRUDERS - 1) + #endif + { + MOVE_SERVO(_SERVO_NR, angles[e]); + safe_delay(500); + } + } +#endif // SWITCHING_EXTRUDER + +#if ENABLED(SWITCHING_NOZZLE) + inline void move_nozzle_servo(const uint8_t e) { + const int16_t angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES; + stepper.synchronize(); + MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, angles[e]); + safe_delay(500); + } +#endif + +inline void invalid_extruder_error(const uint8_t e) { + SERIAL_ECHO_START(); + SERIAL_CHAR('T'); + SERIAL_ECHO_F(e, DEC); + SERIAL_CHAR(' '); + SERIAL_ECHOLN(MSG_INVALID_EXTRUDER); +} + +#if ENABLED(PARKING_EXTRUDER) + + #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT) + #define PE_MAGNET_ON_STATE !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE + #else + #define PE_MAGNET_ON_STATE PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE + #endif + + void pe_set_magnet(const uint8_t extruder_num, const uint8_t state) { + switch (extruder_num) { + case 1: OUT_WRITE(SOL1_PIN, state); break; + default: OUT_WRITE(SOL0_PIN, state); break; + } + #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0 + dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY); + #endif + } + + inline void pe_activate_magnet(const uint8_t extruder_num) { pe_set_magnet(extruder_num, PE_MAGNET_ON_STATE); } + inline void pe_deactivate_magnet(const uint8_t extruder_num) { pe_set_magnet(extruder_num, !PE_MAGNET_ON_STATE); } + +#endif // PARKING_EXTRUDER + +#if HAS_FANMUX + + void fanmux_switch(const uint8_t e) { + WRITE(FANMUX0_PIN, TEST(e, 0) ? HIGH : LOW); + #if PIN_EXISTS(FANMUX1) + WRITE(FANMUX1_PIN, TEST(e, 1) ? HIGH : LOW); + #if PIN_EXISTS(FANMUX2) + WRITE(FANMUX2, TEST(e, 2) ? HIGH : LOW); + #endif + #endif + } + + FORCE_INLINE void fanmux_init(void) { + SET_OUTPUT(FANMUX0_PIN); + #if PIN_EXISTS(FANMUX1) + SET_OUTPUT(FANMUX1_PIN); + #if PIN_EXISTS(FANMUX2) + SET_OUTPUT(FANMUX2_PIN); + #endif + #endif + fanmux_switch(0); + } + +#endif // HAS_FANMUX + +/** + * Perform a tool-change, which may result in moving the + * previous tool out of the way and the new tool into place. + */ +void tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) { + #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1 + + if (tmp_extruder >= MIXING_VIRTUAL_TOOLS) + return invalid_extruder_error(tmp_extruder); + + // T0-Tnnn: Switch virtual tool by changing the mix + for (uint8_t j = 0; j < MIXING_STEPPERS; j++) + mixing_factor[j] = mixing_virtual_tool_mix[tmp_extruder][j]; + + #else // !MIXING_EXTRUDER || MIXING_VIRTUAL_TOOLS <= 1 + + if (tmp_extruder >= EXTRUDERS) + return invalid_extruder_error(tmp_extruder); + + #if HOTENDS > 1 + + const float old_feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : feedrate_mm_s; + + feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S; + + if (tmp_extruder != active_extruder) { + if (!no_move && axis_unhomed_error()) { + no_move = true; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("No move on toolchange"); + #endif + } + + // Save current position to destination, for use later + set_destination_from_current(); + + #if ENABLED(DUAL_X_CARRIAGE) + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPGM("Dual X Carriage Mode "); + switch (dual_x_carriage_mode) { + case DXC_FULL_CONTROL_MODE: SERIAL_ECHOLNPGM("DXC_FULL_CONTROL_MODE"); break; + case DXC_AUTO_PARK_MODE: SERIAL_ECHOLNPGM("DXC_AUTO_PARK_MODE"); break; + case DXC_DUPLICATION_MODE: SERIAL_ECHOLNPGM("DXC_DUPLICATION_MODE"); break; + } + } + #endif + + const float xhome = x_home_pos(active_extruder); + if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE + && IsRunning() + && (delayed_move_time || current_position[X_AXIS] != xhome) + ) { + float raised_z = current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT; + #if ENABLED(MAX_SOFTWARE_ENDSTOPS) + NOMORE(raised_z, soft_endstop_max[Z_AXIS]); + #endif + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPAIR("Raise to ", raised_z); + SERIAL_ECHOLNPAIR("MoveX to ", xhome); + SERIAL_ECHOLNPAIR("Lower to ", current_position[Z_AXIS]); + } + #endif + // Park old head: 1) raise 2) move to park position 3) lower + for (uint8_t i = 0; i < 3; i++) + planner.buffer_line( + i == 0 ? current_position[X_AXIS] : xhome, + current_position[Y_AXIS], + i == 2 ? current_position[Z_AXIS] : raised_z, + current_position[E_AXIS], + planner.max_feedrate_mm_s[i == 1 ? X_AXIS : Z_AXIS], + active_extruder + ); + stepper.synchronize(); + } + + // Apply Y & Z extruder offset (X offset is used as home pos with Dual X) + current_position[Y_AXIS] -= hotend_offset[Y_AXIS][active_extruder] - hotend_offset[Y_AXIS][tmp_extruder]; + current_position[Z_AXIS] -= hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder]; + + // Activate the new extruder ahead of calling set_axis_is_at_home! + active_extruder = tmp_extruder; + + // This function resets the max/min values - the current position may be overwritten below. + set_axis_is_at_home(X_AXIS); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("New Extruder", current_position); + #endif + + // Only when auto-parking are carriages safe to move + if (dual_x_carriage_mode != DXC_AUTO_PARK_MODE) no_move = true; + + switch (dual_x_carriage_mode) { + case DXC_FULL_CONTROL_MODE: + // New current position is the position of the activated extruder + current_position[X_AXIS] = inactive_extruder_x_pos; + // Save the inactive extruder's position (from the old current_position) + inactive_extruder_x_pos = destination[X_AXIS]; + break; + case DXC_AUTO_PARK_MODE: + // record raised toolhead position for use by unpark + COPY(raised_parked_position, current_position); + raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT; + #if ENABLED(MAX_SOFTWARE_ENDSTOPS) + NOMORE(raised_parked_position[Z_AXIS], soft_endstop_max[Z_AXIS]); + #endif + active_extruder_parked = true; + delayed_move_time = 0; + break; + case DXC_DUPLICATION_MODE: + // If the new extruder is the left one, set it "parked" + // This triggers the second extruder to move into the duplication position + active_extruder_parked = (active_extruder == 0); + + if (active_extruder_parked) + current_position[X_AXIS] = inactive_extruder_x_pos; + else + current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset; + inactive_extruder_x_pos = destination[X_AXIS]; + extruder_duplication_enabled = false; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPAIR("Set inactive_extruder_x_pos=", inactive_extruder_x_pos); + SERIAL_ECHOLNPGM("Clear extruder_duplication_enabled"); + } + #endif + break; + } + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOLNPAIR("Active extruder parked: ", active_extruder_parked ? "yes" : "no"); + DEBUG_POS("New extruder (parked)", current_position); + } + #endif + + // No extra case for HAS_ABL in DUAL_X_CARRIAGE. Does that mean they don't work together? + + #else // !DUAL_X_CARRIAGE + + #if ENABLED(PARKING_EXTRUDER) // Dual Parking extruder + const float z_diff = hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder]; + float z_raise = PARKING_EXTRUDER_SECURITY_RAISE; + if (!no_move) { + + const float parkingposx[] = PARKING_EXTRUDER_PARKING_X, + midpos = ((parkingposx[1] - parkingposx[0])/2) + parkingposx[0] + hotend_offset[X_AXIS][active_extruder], + grabpos = parkingposx[tmp_extruder] + hotend_offset[X_AXIS][active_extruder] + + (tmp_extruder == 0 ? -(PARKING_EXTRUDER_GRAB_DISTANCE) : PARKING_EXTRUDER_GRAB_DISTANCE); + /** + * Steps: + * 1. Raise Z-Axis to give enough clearance + * 2. Move to park position of old extruder + * 3. Disengage magnetic field, wait for delay + * 4. Move near new extruder + * 5. Engage magnetic field for new extruder + * 6. Move to parking incl. offset of new extruder + * 7. Lower Z-Axis + */ + + // STEP 1 + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("Starting Autopark"); + if (DEBUGGING(LEVELING)) DEBUG_POS("current position:", current_position); + #endif + current_position[Z_AXIS] += z_raise; + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("(1) Raise Z-Axis "); + if (DEBUGGING(LEVELING)) DEBUG_POS("Moving to Raised Z-Position", current_position); + #endif + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[Z_AXIS], active_extruder); + stepper.synchronize(); + + // STEP 2 + current_position[X_AXIS] = parkingposx[active_extruder] + hotend_offset[X_AXIS][active_extruder]; + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPAIR("(2) Park extruder ", active_extruder); + if (DEBUGGING(LEVELING)) DEBUG_POS("Moving ParkPos", current_position); + #endif + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[X_AXIS], active_extruder); + stepper.synchronize(); + + // STEP 3 + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("(3) Disengage magnet "); + #endif + pe_deactivate_magnet(active_extruder); + + // STEP 4 + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("(4) Move to position near new extruder"); + #endif + current_position[X_AXIS] += (active_extruder == 0 ? 10 : -10); // move 10mm away from parked extruder + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("Moving away from parked extruder", current_position); + #endif + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[X_AXIS], active_extruder); + stepper.synchronize(); + + // STEP 5 + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("(5) Engage magnetic field"); + #endif + + #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT) + pe_activate_magnet(active_extruder); //just save power for inverted magnets + #endif + pe_activate_magnet(tmp_extruder); + + // STEP 6 + current_position[X_AXIS] = grabpos + (tmp_extruder == 0 ? (+10) : (-10)); + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[X_AXIS], active_extruder); + current_position[X_AXIS] = grabpos; + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPAIR("(6) Unpark extruder ", tmp_extruder); + if (DEBUGGING(LEVELING)) DEBUG_POS("Move UnparkPos", current_position); + #endif + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[X_AXIS]/2, active_extruder); + stepper.synchronize(); + + // Step 7 + current_position[X_AXIS] = midpos - hotend_offset[X_AXIS][tmp_extruder]; + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("(7) Move midway between hotends"); + if (DEBUGGING(LEVELING)) DEBUG_POS("Move midway to new extruder", current_position); + #endif + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[X_AXIS], active_extruder); + stepper.synchronize(); + #if ENABLED(DEBUG_LEVELING_FEATURE) + SERIAL_ECHOLNPGM("Autopark done."); + #endif + } + else { // nomove == true + // Only engage magnetic field for new extruder + pe_activate_magnet(tmp_extruder); + #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT) + pe_activate_magnet(active_extruder); // Just save power for inverted magnets + #endif + } + current_position[Z_AXIS] -= hotend_offset[Z_AXIS][tmp_extruder] - hotend_offset[Z_AXIS][active_extruder]; // Apply Zoffset + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("Applying Z-offset", current_position); + #endif + + #endif // dualParking extruder + + #if ENABLED(SWITCHING_NOZZLE) + #define DONT_SWITCH (SWITCHING_EXTRUDER_SERVO_NR == SWITCHING_NOZZLE_SERVO_NR) + // <0 if the new nozzle is higher, >0 if lower. A bigger raise when lower. + const float z_diff = hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder], + z_raise = 0.3 + (z_diff > 0.0 ? z_diff : 0.0); + + // Always raise by some amount (destination copied from current_position earlier) + current_position[Z_AXIS] += z_raise; + planner.buffer_line_kinematic(current_position, planner.max_feedrate_mm_s[Z_AXIS], active_extruder); + move_nozzle_servo(tmp_extruder); + #endif + + /** + * Set current_position to the position of the new nozzle. + * Offsets are based on linear distance, so we need to get + * the resulting position in coordinate space. + * + * - With grid or 3-point leveling, offset XYZ by a tilted vector + * - With mesh leveling, update Z for the new position + * - Otherwise, just use the raw linear distance + * + * Software endstops are altered here too. Consider a case where: + * E0 at X=0 ... E1 at X=10 + * When we switch to E1 now X=10, but E1 can't move left. + * To express this we apply the change in XY to the software endstops. + * E1 can move farther right than E0, so the right limit is extended. + * + * Note that we don't adjust the Z software endstops. Why not? + * Consider a case where Z=0 (here) and switching to E1 makes Z=1 + * because the bed is 1mm lower at the new position. As long as + * the first nozzle is out of the way, the carriage should be + * allowed to move 1mm lower. This technically "breaks" the + * Z software endstop. But this is technically correct (and + * there is no viable alternative). + */ + #if ABL_PLANAR + // Offset extruder, make sure to apply the bed level rotation matrix + vector_3 tmp_offset_vec = vector_3(hotend_offset[X_AXIS][tmp_extruder], + hotend_offset[Y_AXIS][tmp_extruder], + 0), + act_offset_vec = vector_3(hotend_offset[X_AXIS][active_extruder], + hotend_offset[Y_AXIS][active_extruder], + 0), + offset_vec = tmp_offset_vec - act_offset_vec; + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + tmp_offset_vec.debug(PSTR("tmp_offset_vec")); + act_offset_vec.debug(PSTR("act_offset_vec")); + offset_vec.debug(PSTR("offset_vec (BEFORE)")); + } + #endif + + offset_vec.apply_rotation(planner.bed_level_matrix.transpose(planner.bed_level_matrix)); + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) offset_vec.debug(PSTR("offset_vec (AFTER)")); + #endif + + // Adjustments to the current position + const float xydiff[2] = { offset_vec.x, offset_vec.y }; + current_position[Z_AXIS] += offset_vec.z; + + #else // !ABL_PLANAR + + const float xydiff[2] = { + hotend_offset[X_AXIS][tmp_extruder] - hotend_offset[X_AXIS][active_extruder], + hotend_offset[Y_AXIS][tmp_extruder] - hotend_offset[Y_AXIS][active_extruder] + }; + + #if ENABLED(MESH_BED_LEVELING) + + if (planner.leveling_active) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOPAIR("Z before MBL: ", current_position[Z_AXIS]); + #endif + float x2 = current_position[X_AXIS] + xydiff[X_AXIS], + y2 = current_position[Y_AXIS] + xydiff[Y_AXIS], + z1 = current_position[Z_AXIS], z2 = z1; + planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], z1); + planner.apply_leveling(x2, y2, z2); + current_position[Z_AXIS] += z2 - z1; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) + SERIAL_ECHOLNPAIR(" after: ", current_position[Z_AXIS]); + #endif + } + + #endif // MESH_BED_LEVELING + + #endif // !HAS_ABL + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("Offset Tool XY by { ", xydiff[X_AXIS]); + SERIAL_ECHOPAIR(", ", xydiff[Y_AXIS]); + SERIAL_ECHOLNPGM(" }"); + } + #endif + + // The newly-selected extruder XY is actually at... + current_position[X_AXIS] += xydiff[X_AXIS]; + current_position[Y_AXIS] += xydiff[Y_AXIS]; + #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) || ENABLED(PARKING_EXTRUDER) + for (uint8_t i = X_AXIS; i <= Y_AXIS; i++) { + #if HAS_POSITION_SHIFT + position_shift[i] += xydiff[i]; + #endif + update_software_endstops((AxisEnum)i); + } + #endif + + // Set the new active extruder + active_extruder = tmp_extruder; + + #endif // !DUAL_X_CARRIAGE + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("Sync After Toolchange", current_position); + #endif + + // Tell the planner the new "current position" + SYNC_PLAN_POSITION_KINEMATIC(); + + // Move to the "old position" (move the extruder into place) + if (!no_move && IsRunning()) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) DEBUG_POS("Move back", destination); + #endif + prepare_move_to_destination(); + } + + #if ENABLED(SWITCHING_NOZZLE) + // Move back down, if needed. (Including when the new tool is higher.) + if (z_raise != z_diff) { + destination[Z_AXIS] += z_diff; + feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS]; + prepare_move_to_destination(); + } + #endif + + } // (tmp_extruder != active_extruder) + + stepper.synchronize(); + + #if ENABLED(EXT_SOLENOID) && !ENABLED(PARKING_EXTRUDER) + disable_all_solenoids(); + enable_solenoid_on_active_extruder(); + #endif // EXT_SOLENOID + + feedrate_mm_s = old_feedrate_mm_s; + + #else // HOTENDS <= 1 + + UNUSED(fr_mm_s); + UNUSED(no_move); + + #if ENABLED(MK2_MULTIPLEXER) + if (tmp_extruder >= E_STEPPERS) + return invalid_extruder_error(tmp_extruder); + + select_multiplexed_stepper(tmp_extruder); + #endif + + // Set the new active extruder + active_extruder = tmp_extruder; + + #endif // HOTENDS <= 1 + + #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH + stepper.synchronize(); + move_extruder_servo(active_extruder); + #endif + + #if HAS_FANMUX + fanmux_switch(active_extruder); + #endif + + SERIAL_ECHO_START(); + SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, (int)active_extruder); + + #endif // !MIXING_EXTRUDER || MIXING_VIRTUAL_TOOLS <= 1 +} + +/** + * T0-T3: Switch tool, usually switching extruders + * + * F[units/min] Set the movement feedrate + * S1 Don't move the tool in XY after change + */ +inline void gcode_T(const uint8_t tmp_extruder) { + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR(">>> gcode_T(", tmp_extruder); + SERIAL_CHAR(')'); + SERIAL_EOL(); + DEBUG_POS("BEFORE", current_position); + } + #endif + + #if HOTENDS == 1 || (ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1) + + tool_change(tmp_extruder); + + #elif HOTENDS > 1 + + tool_change( + tmp_extruder, + MMM_TO_MMS(parser.linearval('F')), + (tmp_extruder == active_extruder) || parser.boolval('S') + ); + + #endif + + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + DEBUG_POS("AFTER", current_position); + SERIAL_ECHOLNPGM("<<< gcode_T"); + } + #endif +} + +/** + * Process the parsed command and dispatch it to its handler + */ +void process_parsed_command() { + KEEPALIVE_STATE(IN_HANDLER); + + // Handle a known G, M, or T + switch (parser.command_letter) { + case 'G': switch (parser.codenum) { + + // G0, G1 + case 0: + case 1: + #if IS_SCARA + gcode_G0_G1(parser.codenum == 0); + #else + gcode_G0_G1(); + #endif + break; + + // G2, G3 + #if ENABLED(ARC_SUPPORT) && DISABLED(SCARA) + case 2: // G2: CW ARC + case 3: // G3: CCW ARC + gcode_G2_G3(parser.codenum == 2); + break; + #endif + + // G4 Dwell + case 4: + gcode_G4(); + break; + + #if ENABLED(BEZIER_CURVE_SUPPORT) + case 5: // G5: Cubic B_spline + gcode_G5(); + break; + #endif // BEZIER_CURVE_SUPPORT + + #if ENABLED(FWRETRACT) + case 10: // G10: retract + gcode_G10(); + break; + case 11: // G11: retract_recover + gcode_G11(); + break; + #endif // FWRETRACT + + #if ENABLED(NOZZLE_CLEAN_FEATURE) + case 12: + gcode_G12(); // G12: Nozzle Clean + break; + #endif // NOZZLE_CLEAN_FEATURE + + #if ENABLED(CNC_WORKSPACE_PLANES) + case 17: // G17: Select Plane XY + gcode_G17(); + break; + case 18: // G18: Select Plane ZX + gcode_G18(); + break; + case 19: // G19: Select Plane YZ + gcode_G19(); + break; + #endif // CNC_WORKSPACE_PLANES + + #if ENABLED(INCH_MODE_SUPPORT) + case 20: // G20: Inch Mode + gcode_G20(); + break; + + case 21: // G21: MM Mode + gcode_G21(); + break; + #endif // INCH_MODE_SUPPORT + + #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_VALIDATION) + case 26: // G26: Mesh Validation Pattern generation + gcode_G26(); + break; + #endif // AUTO_BED_LEVELING_UBL + + #if ENABLED(NOZZLE_PARK_FEATURE) + case 27: // G27: Nozzle Park + gcode_G27(); + break; + #endif // NOZZLE_PARK_FEATURE + + case 28: // G28: Home all axes, one at a time + gcode_G28(false); + break; + + #if HAS_LEVELING + case 29: // G29 Detailed Z probe, probes the bed at 3 or more points, + // or provides access to the UBL System if enabled. + gcode_G29(); + break; + #endif // HAS_LEVELING + + #if HAS_BED_PROBE + + case 30: // G30 Single Z probe + gcode_G30(); + break; + + #if ENABLED(Z_PROBE_SLED) + + case 31: // G31: dock the sled + gcode_G31(); + break; + + case 32: // G32: undock the sled + gcode_G32(); + break; + + #endif // Z_PROBE_SLED + + #endif // HAS_BED_PROBE + + #if ENABLED(DELTA_AUTO_CALIBRATION) + + case 33: // G33: Delta Auto-Calibration + gcode_G33(); + break; + + #endif // DELTA_AUTO_CALIBRATION + + #if ENABLED(G38_PROBE_TARGET) + case 38: // G38.2 & G38.3 + if (parser.subcode == 2 || parser.subcode == 3) + gcode_G38(parser.subcode == 2); + break; + #endif + + case 90: // G90 + relative_mode = false; + break; + case 91: // G91 + relative_mode = true; + break; + + case 92: // G92 + gcode_G92(); + break; + + #if HAS_MESH + case 42: + gcode_G42(); + break; + #endif + + #if ENABLED(DEBUG_GCODE_PARSER) + case 800: + parser.debug(); // GCode Parser Test for G + break; + #endif + } + break; + + case 'M': switch (parser.codenum) { + #if HAS_RESUME_CONTINUE + case 0: // M0: Unconditional stop - Wait for user button press on LCD + case 1: // M1: Conditional stop - Wait for user button press on LCD + gcode_M0_M1(); + break; + #endif // ULTIPANEL + + #if ENABLED(SPINDLE_LASER_ENABLE) + case 3: + gcode_M3_M4(true); // M3: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CW + break; // synchronizes with movement commands + case 4: + gcode_M3_M4(false); // M4: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CCW + break; // synchronizes with movement commands + case 5: + gcode_M5(); // M5 - turn spindle/laser off + break; // synchronizes with movement commands + #endif + case 17: // M17: Enable all stepper motors + gcode_M17(); + break; + + #if ENABLED(SDSUPPORT) + case 20: // M20: list SD card + gcode_M20(); break; + case 21: // M21: init SD card + gcode_M21(); break; + case 22: // M22: release SD card + gcode_M22(); break; + case 23: // M23: Select file + gcode_M23(); break; + case 24: // M24: Start SD print + gcode_M24(); break; + case 25: // M25: Pause SD print + gcode_M25(); break; + case 26: // M26: Set SD index + gcode_M26(); break; + case 27: // M27: Get SD status + gcode_M27(); break; + case 28: // M28: Start SD write + gcode_M28(); break; + case 29: // M29: Stop SD write + gcode_M29(); break; + case 30: // M30 Delete File + gcode_M30(); break; + case 32: // M32: Select file and start SD print + gcode_M32(); break; + + #if ENABLED(LONG_FILENAME_HOST_SUPPORT) + case 33: // M33: Get the long full path to a file or folder + gcode_M33(); break; + #endif + + #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE) + case 34: // M34: Set SD card sorting options + gcode_M34(); break; + #endif // SDCARD_SORT_ALPHA && SDSORT_GCODE + + case 928: // M928: Start SD write + gcode_M928(); break; + #endif // SDSUPPORT + + case 31: // M31: Report time since the start of SD print or last M109 + gcode_M31(); break; + + case 42: // M42: Change pin state + gcode_M42(); break; + + #if ENABLED(PINS_DEBUGGING) + case 43: // M43: Read pin state + gcode_M43(); break; + #endif + + + #if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST) + case 48: // M48: Z probe repeatability test + gcode_M48(); + break; + #endif // Z_MIN_PROBE_REPEATABILITY_TEST + + #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_VALIDATION) + case 49: // M49: Turn on or off G26 debug flag for verbose output + gcode_M49(); + break; + #endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_VALIDATION + + #if ENABLED(ULTRA_LCD) && ENABLED(LCD_SET_PROGRESS_MANUALLY) + case 73: // M73: Set print progress percentage + gcode_M73(); break; + #endif + + case 75: // M75: Start print timer + gcode_M75(); break; + case 76: // M76: Pause print timer + gcode_M76(); break; + case 77: // M77: Stop print timer + gcode_M77(); break; + + #if ENABLED(PRINTCOUNTER) + case 78: // M78: Show print statistics + gcode_M78(); break; + #endif + + #if ENABLED(M100_FREE_MEMORY_WATCHER) + case 100: // M100: Free Memory Report + gcode_M100(); + break; + #endif + + case 104: // M104: Set hot end temperature + gcode_M104(); + break; + + case 110: // M110: Set Current Line Number + gcode_M110(); + break; + + case 111: // M111: Set debug level + gcode_M111(); + break; + + #if DISABLED(EMERGENCY_PARSER) + + case 108: // M108: Cancel Waiting + gcode_M108(); + break; + + case 112: // M112: Emergency Stop + gcode_M112(); + break; + + case 410: // M410 quickstop - Abort all the planned moves. + gcode_M410(); + break; + + #endif + + #if ENABLED(HOST_KEEPALIVE_FEATURE) + case 113: // M113: Set Host Keepalive interval + gcode_M113(); + break; + #endif + + case 140: // M140: Set bed temperature + gcode_M140(); + break; + + case 105: // M105: Report current temperature + gcode_M105(); + KEEPALIVE_STATE(NOT_BUSY); + return; // "ok" already printed + + #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED) + case 155: // M155: Set temperature auto-report interval + gcode_M155(); + break; + #endif + + case 109: // M109: Wait for hotend temperature to reach target + gcode_M109(); + break; + + #if HAS_TEMP_BED + case 190: // M190: Wait for bed temperature to reach target + gcode_M190(); + break; + #endif // HAS_TEMP_BED + + #if FAN_COUNT > 0 + case 106: // M106: Fan On + gcode_M106(); + break; + case 107: // M107: Fan Off + gcode_M107(); + break; + #endif // FAN_COUNT > 0 + + #if ENABLED(PARK_HEAD_ON_PAUSE) + case 125: // M125: Store current position and move to filament change position + gcode_M125(); break; + #endif + + #if ENABLED(BARICUDA) + // PWM for HEATER_1_PIN + #if HAS_HEATER_1 + case 126: // M126: valve open + gcode_M126(); + break; + case 127: // M127: valve closed + gcode_M127(); + break; + #endif // HAS_HEATER_1 + + // PWM for HEATER_2_PIN + #if HAS_HEATER_2 + case 128: // M128: valve open + gcode_M128(); + break; + case 129: // M129: valve closed + gcode_M129(); + break; + #endif // HAS_HEATER_2 + #endif // BARICUDA + + #if HAS_POWER_SWITCH + + case 80: // M80: Turn on Power Supply + gcode_M80(); + break; + + #endif // HAS_POWER_SWITCH + + case 81: // M81: Turn off Power, including Power Supply, if possible + gcode_M81(); + break; + + case 82: // M82: Set E axis normal mode (same as other axes) + gcode_M82(); + break; + case 83: // M83: Set E axis relative mode + gcode_M83(); + break; + case 18: // M18 => M84 + case 84: // M84: Disable all steppers or set timeout + gcode_M18_M84(); + break; + case 85: // M85: Set inactivity stepper shutdown timeout + gcode_M85(); + break; + case 92: // M92: Set the steps-per-unit for one or more axes + gcode_M92(); + break; + case 114: // M114: Report current position + gcode_M114(); + break; + case 115: // M115: Report capabilities + gcode_M115(); + break; + case 117: // M117: Set LCD message text, if possible + gcode_M117(); + break; + case 118: // M118: Display a message in the host console + gcode_M118(); + break; + case 119: // M119: Report endstop states + gcode_M119(); + break; + case 120: // M120: Enable endstops + gcode_M120(); + break; + case 121: // M121: Disable endstops + gcode_M121(); + break; + + #if ENABLED(ULTIPANEL) + + case 145: // M145: Set material heatup parameters + gcode_M145(); + break; + + #endif + + #if ENABLED(TEMPERATURE_UNITS_SUPPORT) + case 149: // M149: Set temperature units + gcode_M149(); + break; + #endif + + #if HAS_COLOR_LEDS + + case 150: // M150: Set Status LED Color + gcode_M150(); + break; + + #endif // HAS_COLOR_LEDS + + #if ENABLED(MIXING_EXTRUDER) + case 163: // M163: Set a component weight for mixing extruder + gcode_M163(); + break; + #if MIXING_VIRTUAL_TOOLS > 1 + case 164: // M164: Save current mix as a virtual extruder + gcode_M164(); + break; + #endif + #if ENABLED(DIRECT_MIXING_IN_G1) + case 165: // M165: Set multiple mix weights + gcode_M165(); + break; + #endif + #endif + + case 200: // M200: Set filament diameter, E to cubic units + gcode_M200(); + break; + case 201: // M201: Set max acceleration for print moves (units/s^2) + gcode_M201(); + break; + #if 0 // Not used for Sprinter/grbl gen6 + case 202: // M202 + gcode_M202(); + break; + #endif + case 203: // M203: Set max feedrate (units/sec) + gcode_M203(); + break; + case 204: // M204: Set acceleration + gcode_M204(); + break; + case 205: // M205: Set advanced settings + gcode_M205(); + break; + + #if HAS_M206_COMMAND + case 206: // M206: Set home offsets + gcode_M206(); + break; + #endif + + #if ENABLED(DELTA) + case 665: // M665: Set delta configurations + gcode_M665(); + break; + #endif + + #if ENABLED(DELTA) || ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS) + case 666: // M666: Set delta or dual endstop adjustment + gcode_M666(); + break; + #endif + + #if ENABLED(FWRETRACT) + case 207: // M207: Set Retract Length, Feedrate, and Z lift + gcode_M207(); + break; + case 208: // M208: Set Recover (unretract) Additional Length and Feedrate + gcode_M208(); + break; + case 209: // M209: Turn Automatic Retract Detection on/off + if (MIN_AUTORETRACT <= MAX_AUTORETRACT) gcode_M209(); + break; + #endif // FWRETRACT + + case 211: // M211: Enable, Disable, and/or Report software endstops + gcode_M211(); + break; + + #if HOTENDS > 1 + case 218: // M218: Set a tool offset + gcode_M218(); + break; + #endif // HOTENDS > 1 + + case 220: // M220: Set Feedrate Percentage: S ("FR" on your LCD) + gcode_M220(); + break; + + case 221: // M221: Set Flow Percentage + gcode_M221(); + break; + + case 226: // M226: Wait until a pin reaches a state + gcode_M226(); + break; + + #if HAS_SERVOS + case 280: // M280: Set servo position absolute + gcode_M280(); + break; + #endif // HAS_SERVOS + + #if ENABLED(BABYSTEPPING) + case 290: // M290: Babystepping + gcode_M290(); + break; + #endif // BABYSTEPPING + + #if HAS_BUZZER + case 300: // M300: Play beep tone + gcode_M300(); + break; + #endif // HAS_BUZZER + + #if ENABLED(PIDTEMP) + case 301: // M301: Set hotend PID parameters + gcode_M301(); + break; + #endif // PIDTEMP + + #if ENABLED(PIDTEMPBED) + case 304: // M304: Set bed PID parameters + gcode_M304(); + break; + #endif // PIDTEMPBED + + #if defined(CHDK) || HAS_PHOTOGRAPH + case 240: // M240: Trigger a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/ + gcode_M240(); + break; + #endif // CHDK || PHOTOGRAPH_PIN + + #if HAS_LCD_CONTRAST + case 250: // M250: Set LCD contrast + gcode_M250(); + break; + #endif // HAS_LCD_CONTRAST + + #if ENABLED(EXPERIMENTAL_I2CBUS) + + case 260: // M260: Send data to an i2c slave + gcode_M260(); + break; + + case 261: // M261: Request data from an i2c slave + gcode_M261(); + break; + + #endif // EXPERIMENTAL_I2CBUS + + #if ENABLED(PREVENT_COLD_EXTRUSION) + case 302: // M302: Allow cold extrudes (set the minimum extrude temperature) + gcode_M302(); + break; + #endif // PREVENT_COLD_EXTRUSION + + case 303: // M303: PID autotune + gcode_M303(); + break; + + #if ENABLED(MORGAN_SCARA) + case 360: // M360: SCARA Theta pos1 + if (gcode_M360()) return; + break; + case 361: // M361: SCARA Theta pos2 + if (gcode_M361()) return; + break; + case 362: // M362: SCARA Psi pos1 + if (gcode_M362()) return; + break; + case 363: // M363: SCARA Psi pos2 + if (gcode_M363()) return; + break; + case 364: // M364: SCARA Psi pos3 (90 deg to Theta) + if (gcode_M364()) return; + break; + #endif // SCARA + + case 400: // M400: Finish all moves + gcode_M400(); + break; + + #if HAS_BED_PROBE + case 401: // M401: Deploy probe + gcode_M401(); + break; + case 402: // M402: Stow probe + gcode_M402(); + break; + #endif // HAS_BED_PROBE + + #if ENABLED(FILAMENT_WIDTH_SENSOR) + case 404: // M404: Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width + gcode_M404(); + break; + case 405: // M405: Turn on filament sensor for control + gcode_M405(); + break; + case 406: // M406: Turn off filament sensor for control + gcode_M406(); + break; + case 407: // M407: Display measured filament diameter + gcode_M407(); + break; + #endif // FILAMENT_WIDTH_SENSOR + + #if HAS_LEVELING + case 420: // M420: Enable/Disable Bed Leveling + gcode_M420(); + break; + #endif + + #if HAS_MESH + case 421: // M421: Set a Mesh Bed Leveling Z coordinate + gcode_M421(); + break; + #endif + + #if HAS_M206_COMMAND + case 428: // M428: Apply current_position to home_offset + gcode_M428(); + break; + #endif + + case 500: // M500: Store settings in EEPROM + gcode_M500(); + break; + case 501: // M501: Read settings from EEPROM + gcode_M501(); + break; + case 502: // M502: Revert to default settings + gcode_M502(); + break; + + #if DISABLED(DISABLE_M503) + case 503: // M503: print settings currently in memory + gcode_M503(); + break; + #endif + + #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) + case 540: // M540: Set abort on endstop hit for SD printing + gcode_M540(); + break; + #endif + + #if HAS_BED_PROBE + case 851: // M851: Set Z Probe Z Offset + gcode_M851(); + break; + #endif // HAS_BED_PROBE + + #if ENABLED(ADVANCED_PAUSE_FEATURE) + case 600: // M600: Pause for filament change + gcode_M600(); + break; + #endif // ADVANCED_PAUSE_FEATURE + + #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + case 605: // M605: Set Dual X Carriage movement mode + gcode_M605(); + break; + #endif // DUAL_X_CARRIAGE + + #if ENABLED(MK2_MULTIPLEXER) + case 702: // M702: Unload all extruders + gcode_M702(); + break; + #endif + + #if ENABLED(LIN_ADVANCE) + case 900: // M900: Set advance K factor. + gcode_M900(); + break; + #endif + + #if ENABLED(HAVE_TMC2130) + case 906: // M906: Set motor current in milliamps using axis codes X, Y, Z, E + gcode_M906(); + break; + #endif + + case 907: // M907: Set digital trimpot motor current using axis codes. + gcode_M907(); + break; + + #if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT) + + case 908: // M908: Control digital trimpot directly. + gcode_M908(); + break; + + #if ENABLED(DAC_STEPPER_CURRENT) // As with Printrbot RevF + + case 909: // M909: Print digipot/DAC current value + gcode_M909(); + break; + + case 910: // M910: Commit digipot/DAC value to external EEPROM + gcode_M910(); + break; + + #endif + + #endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT + + #if ENABLED(HAVE_TMC2130) + case 911: // M911: Report TMC2130 prewarn triggered flags + gcode_M911(); + break; + + case 912: // M911: Clear TMC2130 prewarn triggered flags + gcode_M912(); + break; + + #if ENABLED(HYBRID_THRESHOLD) + case 913: // M913: Set HYBRID_THRESHOLD speed. + gcode_M913(); + break; + #endif + + #if ENABLED(SENSORLESS_HOMING) + case 914: // M914: Set SENSORLESS_HOMING sensitivity. + gcode_M914(); + break; + #endif + #endif + + #if HAS_MICROSTEPS + + case 350: // M350: Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers. + gcode_M350(); + break; + + case 351: // M351: Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low. + gcode_M351(); + break; + + #endif // HAS_MICROSTEPS + + case 355: // M355 set case light brightness + gcode_M355(); + break; + + #if ENABLED(DEBUG_GCODE_PARSER) + case 800: + parser.debug(); // GCode Parser Test for M + break; + #endif + + #if ENABLED(I2C_POSITION_ENCODERS) + + case 860: // M860 Report encoder module position + gcode_M860(); + break; + + case 861: // M861 Report encoder module status + gcode_M861(); + break; + + case 862: // M862 Perform axis test + gcode_M862(); + break; + + case 863: // M863 Calibrate steps/mm + gcode_M863(); + break; + + case 864: // M864 Change module address + gcode_M864(); + break; + + case 865: // M865 Check module firmware version + gcode_M865(); + break; + + case 866: // M866 Report axis error count + gcode_M866(); + break; + + case 867: // M867 Toggle error correction + gcode_M867(); + break; + + case 868: // M868 Set error correction threshold + gcode_M868(); + break; + + case 869: // M869 Report axis error + gcode_M869(); + break; + + #endif // I2C_POSITION_ENCODERS + + case 999: // M999: Restart after being Stopped + gcode_M999(); + break; + } + break; + + case 'T': + gcode_T(parser.codenum); + break; + + default: parser.unknown_command_error(); + } + + KEEPALIVE_STATE(NOT_BUSY); + + ok_to_send(); +} + +void process_next_command() { + char * const current_command = command_queue[cmd_queue_index_r]; + + if (DEBUGGING(ECHO)) { + SERIAL_ECHO_START(); + SERIAL_ECHOLN(current_command); + #if ENABLED(M100_FREE_MEMORY_WATCHER) + SERIAL_ECHOPAIR("slot:", cmd_queue_index_r); + M100_dump_routine(" Command Queue:", (const char*)command_queue, (const char*)(command_queue + sizeof(command_queue))); + #endif + } + + // Parse the next command in the queue + parser.parse(current_command); + process_parsed_command(); +} + +/** + * Send a "Resend: nnn" message to the host to + * indicate that a command needs to be re-sent. + */ +void FlushSerialRequestResend() { + //char command_queue[cmd_queue_index_r][100]="Resend:"; + MYSERIAL.flush(); + SERIAL_PROTOCOLPGM(MSG_RESEND); + SERIAL_PROTOCOLLN(gcode_LastN + 1); + ok_to_send(); +} + +/** + * Send an "ok" message to the host, indicating + * that a command was successfully processed. + * + * If ADVANCED_OK is enabled also include: + * N Line number of the command, if any + * P Planner space remaining + * B Block queue space remaining + */ +void ok_to_send() { + refresh_cmd_timeout(); + if (!send_ok[cmd_queue_index_r]) return; + SERIAL_PROTOCOLPGM(MSG_OK); + #if ENABLED(ADVANCED_OK) + char* p = command_queue[cmd_queue_index_r]; + if (*p == 'N') { + SERIAL_PROTOCOL(' '); + SERIAL_ECHO(*p++); + while (NUMERIC_SIGNED(*p)) + SERIAL_ECHO(*p++); + } + SERIAL_PROTOCOLPGM(" P"); SERIAL_PROTOCOL(int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1)); + SERIAL_PROTOCOLPGM(" B"); SERIAL_PROTOCOL(BUFSIZE - commands_in_queue); + #endif + SERIAL_EOL(); +} + +#if HAS_SOFTWARE_ENDSTOPS + + /** + * Constrain the given coordinates to the software endstops. + * + * For DELTA/SCARA the XY constraint is based on the smallest + * radius within the set software endstops. + */ + void clamp_to_software_endstops(float target[XYZ]) { + if (!soft_endstops_enabled) return; + #if IS_KINEMATIC + const float dist_2 = HYPOT2(target[X_AXIS], target[Y_AXIS]); + if (dist_2 > soft_endstop_radius_2) { + const float ratio = soft_endstop_radius / SQRT(dist_2); // 200 / 300 = 0.66 + target[X_AXIS] *= ratio; + target[Y_AXIS] *= ratio; + } + #else + #if ENABLED(MIN_SOFTWARE_ENDSTOP_X) + NOLESS(target[X_AXIS], soft_endstop_min[X_AXIS]); + #endif + #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y) + NOLESS(target[Y_AXIS], soft_endstop_min[Y_AXIS]); + #endif + #if ENABLED(MAX_SOFTWARE_ENDSTOP_X) + NOMORE(target[X_AXIS], soft_endstop_max[X_AXIS]); + #endif + #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y) + NOMORE(target[Y_AXIS], soft_endstop_max[Y_AXIS]); + #endif + #endif + #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z) + NOLESS(target[Z_AXIS], soft_endstop_min[Z_AXIS]); + #endif + #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z) + NOMORE(target[Z_AXIS], soft_endstop_max[Z_AXIS]); + #endif + } + +#endif + +#if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + #if ENABLED(ABL_BILINEAR_SUBDIVISION) + #define ABL_BG_SPACING(A) bilinear_grid_spacing_virt[A] + #define ABL_BG_FACTOR(A) bilinear_grid_factor_virt[A] + #define ABL_BG_POINTS_X ABL_GRID_POINTS_VIRT_X + #define ABL_BG_POINTS_Y ABL_GRID_POINTS_VIRT_Y + #define ABL_BG_GRID(X,Y) z_values_virt[X][Y] + #else + #define ABL_BG_SPACING(A) bilinear_grid_spacing[A] + #define ABL_BG_FACTOR(A) bilinear_grid_factor[A] + #define ABL_BG_POINTS_X GRID_MAX_POINTS_X + #define ABL_BG_POINTS_Y GRID_MAX_POINTS_Y + #define ABL_BG_GRID(X,Y) z_values[X][Y] + #endif + + // Get the Z adjustment for non-linear bed leveling + float bilinear_z_offset(const float raw[XYZ]) { + + static float z1, d2, z3, d4, L, D, ratio_x, ratio_y, + last_x = -999.999, last_y = -999.999; + + // Whole units for the grid line indices. Constrained within bounds. + static int8_t gridx, gridy, nextx, nexty, + last_gridx = -99, last_gridy = -99; + + // XY relative to the probed area + const float rx = raw[X_AXIS] - bilinear_start[X_AXIS], + ry = raw[Y_AXIS] - bilinear_start[Y_AXIS]; + + #if ENABLED(EXTRAPOLATE_BEYOND_GRID) + // Keep using the last grid box + #define FAR_EDGE_OR_BOX 2 + #else + // Just use the grid far edge + #define FAR_EDGE_OR_BOX 1 + #endif + + if (last_x != rx) { + last_x = rx; + ratio_x = rx * ABL_BG_FACTOR(X_AXIS); + const float gx = constrain(FLOOR(ratio_x), 0, ABL_BG_POINTS_X - FAR_EDGE_OR_BOX); + ratio_x -= gx; // Subtract whole to get the ratio within the grid box + + #if DISABLED(EXTRAPOLATE_BEYOND_GRID) + // Beyond the grid maintain height at grid edges + NOLESS(ratio_x, 0); // Never < 0.0. (> 1.0 is ok when nextx==gridx.) + #endif + + gridx = gx; + nextx = min(gridx + 1, ABL_BG_POINTS_X - 1); + } + + if (last_y != ry || last_gridx != gridx) { + + if (last_y != ry) { + last_y = ry; + ratio_y = ry * ABL_BG_FACTOR(Y_AXIS); + const float gy = constrain(FLOOR(ratio_y), 0, ABL_BG_POINTS_Y - FAR_EDGE_OR_BOX); + ratio_y -= gy; + + #if DISABLED(EXTRAPOLATE_BEYOND_GRID) + // Beyond the grid maintain height at grid edges + NOLESS(ratio_y, 0); // Never < 0.0. (> 1.0 is ok when nexty==gridy.) + #endif + + gridy = gy; + nexty = min(gridy + 1, ABL_BG_POINTS_Y - 1); + } + + if (last_gridx != gridx || last_gridy != gridy) { + last_gridx = gridx; + last_gridy = gridy; + // Z at the box corners + z1 = ABL_BG_GRID(gridx, gridy); // left-front + d2 = ABL_BG_GRID(gridx, nexty) - z1; // left-back (delta) + z3 = ABL_BG_GRID(nextx, gridy); // right-front + d4 = ABL_BG_GRID(nextx, nexty) - z3; // right-back (delta) + } + + // Bilinear interpolate. Needed since ry or gridx has changed. + L = z1 + d2 * ratio_y; // Linear interp. LF -> LB + const float R = z3 + d4 * ratio_y; // Linear interp. RF -> RB + + D = R - L; + } + + const float offset = L + ratio_x * D; // the offset almost always changes + + /* + static float last_offset = 0; + if (FABS(last_offset - offset) > 0.2) { + SERIAL_ECHOPGM("Sudden Shift at "); + SERIAL_ECHOPAIR("x=", rx); + SERIAL_ECHOPAIR(" / ", bilinear_grid_spacing[X_AXIS]); + SERIAL_ECHOLNPAIR(" -> gridx=", gridx); + SERIAL_ECHOPAIR(" y=", ry); + SERIAL_ECHOPAIR(" / ", bilinear_grid_spacing[Y_AXIS]); + SERIAL_ECHOLNPAIR(" -> gridy=", gridy); + SERIAL_ECHOPAIR(" ratio_x=", ratio_x); + SERIAL_ECHOLNPAIR(" ratio_y=", ratio_y); + SERIAL_ECHOPAIR(" z1=", z1); + SERIAL_ECHOPAIR(" z2=", z2); + SERIAL_ECHOPAIR(" z3=", z3); + SERIAL_ECHOLNPAIR(" z4=", z4); + SERIAL_ECHOPAIR(" L=", L); + SERIAL_ECHOPAIR(" R=", R); + SERIAL_ECHOLNPAIR(" offset=", offset); + } + last_offset = offset; + //*/ + + return offset; + } + +#endif // AUTO_BED_LEVELING_BILINEAR + +#if ENABLED(DELTA) + + /** + * Recalculate factors used for delta kinematics whenever + * settings have been changed (e.g., by M665). + */ + void recalc_delta_settings() { + const float trt[ABC] = DELTA_RADIUS_TRIM_TOWER, + drt[ABC] = DELTA_DIAGONAL_ROD_TRIM_TOWER; + delta_tower[A_AXIS][X_AXIS] = cos(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]); // front left tower + delta_tower[A_AXIS][Y_AXIS] = sin(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]); + delta_tower[B_AXIS][X_AXIS] = cos(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]); // front right tower + delta_tower[B_AXIS][Y_AXIS] = sin(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]); + delta_tower[C_AXIS][X_AXIS] = cos(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]); // back middle tower + delta_tower[C_AXIS][Y_AXIS] = sin(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]); + delta_diagonal_rod_2_tower[A_AXIS] = sq(delta_diagonal_rod + drt[A_AXIS]); + delta_diagonal_rod_2_tower[B_AXIS] = sq(delta_diagonal_rod + drt[B_AXIS]); + delta_diagonal_rod_2_tower[C_AXIS] = sq(delta_diagonal_rod + drt[C_AXIS]); + update_software_endstops(Z_AXIS); + axis_homed[X_AXIS] = axis_homed[Y_AXIS] = axis_homed[Z_AXIS] = false; + } + + #if ENABLED(DELTA_FAST_SQRT) + /** + * Fast inverse sqrt from Quake III Arena + * See: https://en.wikipedia.org/wiki/Fast_inverse_square_root + */ + float Q_rsqrt(float number) { + long i; + float x2, y; + const float threehalfs = 1.5f; + x2 = number * 0.5f; + y = number; + i = * ( long * ) &y; // evil floating point bit level hacking + i = 0x5F3759DF - ( i >> 1 ); // what the f***? + y = * ( float * ) &i; + y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration + // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed + return y; + } + + #define _SQRT(n) (1.0f / Q_rsqrt(n)) + + #else + + #define _SQRT(n) SQRT(n) + + #endif + + /** + * Delta Inverse Kinematics + * + * Calculate the tower positions for a given machine + * position, storing the result in the delta[] array. + * + * This is an expensive calculation, requiring 3 square + * roots per segmented linear move, and strains the limits + * of a Mega2560 with a Graphical Display. + * + * Suggested optimizations include: + * + * - Disable the home_offset (M206) and/or position_shift (G92) + * features to remove up to 12 float additions. + * + * - Use a fast-inverse-sqrt function and add the reciprocal. + * (see above) + */ + + // Macro to obtain the Z position of an individual tower + #define DELTA_Z(T) raw[Z_AXIS] + _SQRT( \ + delta_diagonal_rod_2_tower[T] - HYPOT2( \ + delta_tower[T][X_AXIS] - raw[X_AXIS], \ + delta_tower[T][Y_AXIS] - raw[Y_AXIS] \ + ) \ + ) + + #define DELTA_RAW_IK() do { \ + delta[A_AXIS] = DELTA_Z(A_AXIS); \ + delta[B_AXIS] = DELTA_Z(B_AXIS); \ + delta[C_AXIS] = DELTA_Z(C_AXIS); \ + }while(0) + + #define DELTA_DEBUG() do { \ + SERIAL_ECHOPAIR("cartesian X:", raw[X_AXIS]); \ + SERIAL_ECHOPAIR(" Y:", raw[Y_AXIS]); \ + SERIAL_ECHOLNPAIR(" Z:", raw[Z_AXIS]); \ + SERIAL_ECHOPAIR("delta A:", delta[A_AXIS]); \ + SERIAL_ECHOPAIR(" B:", delta[B_AXIS]); \ + SERIAL_ECHOLNPAIR(" C:", delta[C_AXIS]); \ + }while(0) + + void inverse_kinematics(const float raw[XYZ]) { + DELTA_RAW_IK(); + // DELTA_DEBUG(); + } + + /** + * Calculate the highest Z position where the + * effector has the full range of XY motion. + */ + float delta_safe_distance_from_top() { + float cartesian[XYZ] = { 0, 0, 0 }; + inverse_kinematics(cartesian); + float distance = delta[A_AXIS]; + cartesian[Y_AXIS] = LOGICAL_Y_POSITION(DELTA_PRINTABLE_RADIUS); + inverse_kinematics(cartesian); + return FABS(distance - delta[A_AXIS]); + } + + /** + * Delta Forward Kinematics + * + * See the Wikipedia article "Trilateration" + * https://en.wikipedia.org/wiki/Trilateration + * + * Establish a new coordinate system in the plane of the + * three carriage points. This system has its origin at + * tower1, with tower2 on the X axis. Tower3 is in the X-Y + * plane with a Z component of zero. + * We will define unit vectors in this coordinate system + * in our original coordinate system. Then when we calculate + * the Xnew, Ynew and Znew values, we can translate back into + * the original system by moving along those unit vectors + * by the corresponding values. + * + * Variable names matched to Marlin, c-version, and avoid the + * use of any vector library. + * + * by Andreas Hardtung 2016-06-07 + * based on a Java function from "Delta Robot Kinematics V3" + * by Steve Graves + * + * The result is stored in the cartes[] array. + */ + void forward_kinematics_DELTA(float z1, float z2, float z3) { + // Create a vector in old coordinates along x axis of new coordinate + float p12[3] = { delta_tower[B_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[B_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z2 - z1 }; + + // Get the Magnitude of vector. + float d = SQRT( sq(p12[0]) + sq(p12[1]) + sq(p12[2]) ); + + // Create unit vector by dividing by magnitude. + float ex[3] = { p12[0] / d, p12[1] / d, p12[2] / d }; + + // Get the vector from the origin of the new system to the third point. + float p13[3] = { delta_tower[C_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[C_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z3 - z1 }; + + // Use the dot product to find the component of this vector on the X axis. + float i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2]; + + // Create a vector along the x axis that represents the x component of p13. + float iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i }; + + // Subtract the X component from the original vector leaving only Y. We use the + // variable that will be the unit vector after we scale it. + float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] }; + + // The magnitude of Y component + float j = SQRT( sq(ey[0]) + sq(ey[1]) + sq(ey[2]) ); + + // Convert to a unit vector + ey[0] /= j; ey[1] /= j; ey[2] /= j; + + // The cross product of the unit x and y is the unit z + // float[] ez = vectorCrossProd(ex, ey); + float ez[3] = { + ex[1] * ey[2] - ex[2] * ey[1], + ex[2] * ey[0] - ex[0] * ey[2], + ex[0] * ey[1] - ex[1] * ey[0] + }; + + // We now have the d, i and j values defined in Wikipedia. + // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew + float Xnew = (delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[B_AXIS] + sq(d)) / (d * 2), + Ynew = ((delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[C_AXIS] + HYPOT2(i, j)) / 2 - i * Xnew) / j, + Znew = SQRT(delta_diagonal_rod_2_tower[A_AXIS] - HYPOT2(Xnew, Ynew)); + + // Start from the origin of the old coordinates and add vectors in the + // old coords that represent the Xnew, Ynew and Znew to find the point + // in the old system. + cartes[X_AXIS] = delta_tower[A_AXIS][X_AXIS] + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew; + cartes[Y_AXIS] = delta_tower[A_AXIS][Y_AXIS] + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew; + cartes[Z_AXIS] = z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew; + } + + void forward_kinematics_DELTA(float point[ABC]) { + forward_kinematics_DELTA(point[A_AXIS], point[B_AXIS], point[C_AXIS]); + } + +#endif // DELTA + +/** + * Get the stepper positions in the cartes[] array. + * Forward kinematics are applied for DELTA and SCARA. + * + * The result is in the current coordinate space with + * leveling applied. The coordinates need to be run through + * unapply_leveling to obtain machine coordinates suitable + * for current_position, etc. + */ +void get_cartesian_from_steppers() { + #if ENABLED(DELTA) + forward_kinematics_DELTA( + stepper.get_axis_position_mm(A_AXIS), + stepper.get_axis_position_mm(B_AXIS), + stepper.get_axis_position_mm(C_AXIS) + ); + #else + #if IS_SCARA + forward_kinematics_SCARA( + stepper.get_axis_position_degrees(A_AXIS), + stepper.get_axis_position_degrees(B_AXIS) + ); + #else + cartes[X_AXIS] = stepper.get_axis_position_mm(X_AXIS); + cartes[Y_AXIS] = stepper.get_axis_position_mm(Y_AXIS); + #endif + cartes[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS); + #endif +} + +/** + * Set the current_position for an axis based on + * the stepper positions, removing any leveling that + * may have been applied. + */ +void set_current_from_steppers_for_axis(const AxisEnum axis) { + get_cartesian_from_steppers(); + #if PLANNER_LEVELING + planner.unapply_leveling(cartes); + #endif + if (axis == ALL_AXES) + COPY(current_position, cartes); + else + current_position[axis] = cartes[axis]; +} + +#if ENABLED(MESH_BED_LEVELING) + + /** + * Prepare a mesh-leveled linear move in a Cartesian setup, + * splitting the move where it crosses mesh borders. + */ + void mesh_line_to_destination(const float fr_mm_s, uint8_t x_splits = 0xFF, uint8_t y_splits = 0xFF) { + int cx1 = mbl.cell_index_x(current_position[X_AXIS]), + cy1 = mbl.cell_index_y(current_position[Y_AXIS]), + cx2 = mbl.cell_index_x(destination[X_AXIS]), + cy2 = mbl.cell_index_y(destination[Y_AXIS]); + NOMORE(cx1, GRID_MAX_POINTS_X - 2); + NOMORE(cy1, GRID_MAX_POINTS_Y - 2); + NOMORE(cx2, GRID_MAX_POINTS_X - 2); + NOMORE(cy2, GRID_MAX_POINTS_Y - 2); + + if (cx1 == cx2 && cy1 == cy2) { + // Start and end on same mesh square + buffer_line_to_destination(fr_mm_s); + set_current_from_destination(); + return; + } + + #define MBL_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist) + + float normalized_dist, end[XYZE]; + + // Split at the left/front border of the right/top square + const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); + if (cx2 != cx1 && TEST(x_splits, gcx)) { + COPY(end, destination); + destination[X_AXIS] = mbl.index_to_xpos[gcx]; + normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]); + destination[Y_AXIS] = MBL_SEGMENT_END(Y); + CBI(x_splits, gcx); + } + else if (cy2 != cy1 && TEST(y_splits, gcy)) { + COPY(end, destination); + destination[Y_AXIS] = mbl.index_to_ypos[gcy]; + normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]); + destination[X_AXIS] = MBL_SEGMENT_END(X); + CBI(y_splits, gcy); + } + else { + // Already split on a border + buffer_line_to_destination(fr_mm_s); + set_current_from_destination(); + return; + } + + destination[Z_AXIS] = MBL_SEGMENT_END(Z); + destination[E_AXIS] = MBL_SEGMENT_END(E); + + // Do the split and look for more borders + mesh_line_to_destination(fr_mm_s, x_splits, y_splits); + + // Restore destination from stack + COPY(destination, end); + mesh_line_to_destination(fr_mm_s, x_splits, y_splits); + } + +#elif ENABLED(AUTO_BED_LEVELING_BILINEAR) && !IS_KINEMATIC + + #define CELL_INDEX(A,V) ((V - bilinear_start[A##_AXIS]) * ABL_BG_FACTOR(A##_AXIS)) + + /** + * Prepare a bilinear-leveled linear move on Cartesian, + * splitting the move where it crosses grid borders. + */ + void bilinear_line_to_destination(const float fr_mm_s, uint16_t x_splits = 0xFFFF, uint16_t y_splits = 0xFFFF) { + int cx1 = CELL_INDEX(X, current_position[X_AXIS]), + cy1 = CELL_INDEX(Y, current_position[Y_AXIS]), + cx2 = CELL_INDEX(X, destination[X_AXIS]), + cy2 = CELL_INDEX(Y, destination[Y_AXIS]); + cx1 = constrain(cx1, 0, ABL_BG_POINTS_X - 2); + cy1 = constrain(cy1, 0, ABL_BG_POINTS_Y - 2); + cx2 = constrain(cx2, 0, ABL_BG_POINTS_X - 2); + cy2 = constrain(cy2, 0, ABL_BG_POINTS_Y - 2); + + if (cx1 == cx2 && cy1 == cy2) { + // Start and end on same mesh square + buffer_line_to_destination(fr_mm_s); + set_current_from_destination(); + return; + } + + #define LINE_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist) + + float normalized_dist, end[XYZE]; + + // Split at the left/front border of the right/top square + const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); + if (cx2 != cx1 && TEST(x_splits, gcx)) { + COPY(end, destination); + destination[X_AXIS] = bilinear_start[X_AXIS] + ABL_BG_SPACING(X_AXIS) * gcx; + normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]); + destination[Y_AXIS] = LINE_SEGMENT_END(Y); + CBI(x_splits, gcx); + } + else if (cy2 != cy1 && TEST(y_splits, gcy)) { + COPY(end, destination); + destination[Y_AXIS] = bilinear_start[Y_AXIS] + ABL_BG_SPACING(Y_AXIS) * gcy; + normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]); + destination[X_AXIS] = LINE_SEGMENT_END(X); + CBI(y_splits, gcy); + } + else { + // Already split on a border + buffer_line_to_destination(fr_mm_s); + set_current_from_destination(); + return; + } + + destination[Z_AXIS] = LINE_SEGMENT_END(Z); + destination[E_AXIS] = LINE_SEGMENT_END(E); + + // Do the split and look for more borders + bilinear_line_to_destination(fr_mm_s, x_splits, y_splits); + + // Restore destination from stack + COPY(destination, end); + bilinear_line_to_destination(fr_mm_s, x_splits, y_splits); + } + +#endif // AUTO_BED_LEVELING_BILINEAR + +#if !UBL_DELTA +#if IS_KINEMATIC + + /** + * Prepare a linear move in a DELTA or SCARA setup. + * + * This calls planner.buffer_line several times, adding + * small incremental moves for DELTA or SCARA. + * + * For Unified Bed Leveling (Delta or Segmented Cartesian) + * the ubl.prepare_segmented_line_to method replaces this. + */ + inline bool prepare_kinematic_move_to(float rtarget[XYZE]) { + + // Get the top feedrate of the move in the XY plane + const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s); + + // If the move is only in Z/E don't split up the move + if (rtarget[X_AXIS] == current_position[X_AXIS] && rtarget[Y_AXIS] == current_position[Y_AXIS]) { + planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder); + return false; + } + + // Fail if attempting move outside printable radius + if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true; + + // Get the cartesian distances moved in XYZE + const float difference[XYZE] = { + rtarget[X_AXIS] - current_position[X_AXIS], + rtarget[Y_AXIS] - current_position[Y_AXIS], + rtarget[Z_AXIS] - current_position[Z_AXIS], + rtarget[E_AXIS] - current_position[E_AXIS] + }; + + // Get the linear distance in XYZ + float cartesian_mm = SQRT(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS])); + + // If the move is very short, check the E move distance + if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(difference[E_AXIS]); + + // No E move either? Game over. + if (UNEAR_ZERO(cartesian_mm)) return true; + + // Minimum number of seconds to move the given distance + const float seconds = cartesian_mm / _feedrate_mm_s; + + // The number of segments-per-second times the duration + // gives the number of segments + uint16_t segments = delta_segments_per_second * seconds; + + // For SCARA minimum segment size is 0.25mm + #if IS_SCARA + NOMORE(segments, cartesian_mm * 4); + #endif + + // At least one segment is required + NOLESS(segments, 1); + + // The approximate length of each segment + const float inv_segments = 1.0 / float(segments), + segment_distance[XYZE] = { + difference[X_AXIS] * inv_segments, + difference[Y_AXIS] * inv_segments, + difference[Z_AXIS] * inv_segments, + difference[E_AXIS] * inv_segments + }; + + // SERIAL_ECHOPAIR("mm=", cartesian_mm); + // SERIAL_ECHOPAIR(" seconds=", seconds); + // SERIAL_ECHOLNPAIR(" segments=", segments); + + #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING) + // SCARA needs to scale the feed rate from mm/s to degrees/s + const float inv_segment_length = min(10.0, float(segments) / cartesian_mm), // 1/mm/segs + feed_factor = inv_segment_length * _feedrate_mm_s; + float oldA = stepper.get_axis_position_degrees(A_AXIS), + oldB = stepper.get_axis_position_degrees(B_AXIS); + #endif + + // Get the raw current position as starting point + float raw[XYZE]; + COPY(raw, current_position); + + // Drop one segment so the last move is to the exact target. + // If there's only 1 segment, loops will be skipped entirely. + --segments; + + // Calculate and execute the segments + for (uint16_t s = segments + 1; --s;) { + LOOP_XYZE(i) raw[i] += segment_distance[i]; + #if ENABLED(DELTA) + DELTA_RAW_IK(); // Delta can inline its kinematics + #else + inverse_kinematics(raw); + #endif + + ADJUST_DELTA(raw); // Adjust Z if bed leveling is enabled + + #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING) + // For SCARA scale the feed rate from mm/s to degrees/s + // Use ratio between the length of the move and the larger angle change + const float adiff = abs(delta[A_AXIS] - oldA), + bdiff = abs(delta[B_AXIS] - oldB); + planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder); + oldA = delta[A_AXIS]; + oldB = delta[B_AXIS]; + #else + planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], _feedrate_mm_s, active_extruder); + #endif + } + + // Since segment_distance is only approximate, + // the final move must be to the exact destination. + + #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING) + // For SCARA scale the feed rate from mm/s to degrees/s + // With segments > 1 length is 1 segment, otherwise total length + inverse_kinematics(rtarget); + ADJUST_DELTA(rtarget); + const float adiff = abs(delta[A_AXIS] - oldA), + bdiff = abs(delta[B_AXIS] - oldB); + planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder); + #else + planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder); + #endif + + return false; + } + +#else // !IS_KINEMATIC + + /** + * Prepare a linear move in a Cartesian setup. + * + * When a mesh-based leveling system is active, moves are segmented + * according to the configuration of the leveling system. + * + * Returns true if current_position[] was set to destination[] + */ + inline bool prepare_move_to_destination_cartesian() { + #if HAS_MESH + if (planner.leveling_active) { + #if ENABLED(AUTO_BED_LEVELING_UBL) + ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about + return true; // all moves, including Z-only moves. + #else + /** + * For MBL and ABL-BILINEAR only segment moves when X or Y are involved. + * Otherwise fall through to do a direct single move. + */ + if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) { + #if ENABLED(MESH_BED_LEVELING) + mesh_line_to_destination(MMS_SCALED(feedrate_mm_s)); + #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) + bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s)); + #endif + return true; + } + #endif + } + #endif // HAS_MESH + + buffer_line_to_destination(MMS_SCALED(feedrate_mm_s)); + return false; + } + +#endif // !IS_KINEMATIC +#endif // !UBL_DELTA + +#if ENABLED(DUAL_X_CARRIAGE) + + /** + * Prepare a linear move in a dual X axis setup + */ + inline bool prepare_move_to_destination_dualx() { + if (active_extruder_parked) { + switch (dual_x_carriage_mode) { + case DXC_FULL_CONTROL_MODE: + break; + case DXC_AUTO_PARK_MODE: + if (current_position[E_AXIS] == destination[E_AXIS]) { + // This is a travel move (with no extrusion) + // Skip it, but keep track of the current position + // (so it can be used as the start of the next non-travel move) + if (delayed_move_time != 0xFFFFFFFFUL) { + set_current_from_destination(); + NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]); + delayed_move_time = millis(); + return true; + } + } + // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower + for (uint8_t i = 0; i < 3; i++) + planner.buffer_line( + i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS], + i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS], + i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS], + current_position[E_AXIS], + i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS], + active_extruder + ); + delayed_move_time = 0; + active_extruder_parked = false; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked"); + #endif + break; + case DXC_DUPLICATION_MODE: + if (active_extruder == 0) { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPAIR("Set planner X", inactive_extruder_x_pos); + SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset); + } + #endif + // move duplicate extruder into correct duplication position. + planner.set_position_mm( + inactive_extruder_x_pos, + current_position[Y_AXIS], + current_position[Z_AXIS], + current_position[E_AXIS] + ); + planner.buffer_line( + current_position[X_AXIS] + duplicate_extruder_x_offset, + current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], + planner.max_feedrate_mm_s[X_AXIS], 1 + ); + SYNC_PLAN_POSITION_KINEMATIC(); + stepper.synchronize(); + extruder_duplication_enabled = true; + active_extruder_parked = false; + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked"); + #endif + } + else { + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0"); + #endif + } + break; + } + } + return prepare_move_to_destination_cartesian(); + } + +#endif // DUAL_X_CARRIAGE + +/** + * Prepare a single move and get ready for the next one + * + * This may result in several calls to planner.buffer_line to + * do smaller moves for DELTA, SCARA, mesh moves, etc. + * + * Make sure current_position[E] and destination[E] are good + * before calling or cold/lengthy extrusion may get missed. + */ +void prepare_move_to_destination() { + clamp_to_software_endstops(destination); + refresh_cmd_timeout(); + + #if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE) + + if (!DEBUGGING(DRYRUN)) { + if (destination[E_AXIS] != current_position[E_AXIS]) { + #if ENABLED(PREVENT_COLD_EXTRUSION) + if (thermalManager.tooColdToExtrude(active_extruder)) { + current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP); + } + #endif // PREVENT_COLD_EXTRUSION + #if ENABLED(PREVENT_LENGTHY_EXTRUDE) + if (FABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) { + current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP); + } + #endif // PREVENT_LENGTHY_EXTRUDE + } + } + + #endif + + if ( + #if UBL_DELTA // Also works for CARTESIAN (smaller segments follow mesh more closely) + ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s)) + #elif IS_KINEMATIC + prepare_kinematic_move_to(destination) + #elif ENABLED(DUAL_X_CARRIAGE) + prepare_move_to_destination_dualx() + #else + prepare_move_to_destination_cartesian() + #endif + ) return; + + set_current_from_destination(); +} + +#if ENABLED(ARC_SUPPORT) + + #if N_ARC_CORRECTION < 1 + #undef N_ARC_CORRECTION + #define N_ARC_CORRECTION 1 + #endif + + /** + * Plan an arc in 2 dimensions + * + * The arc is approximated by generating many small linear segments. + * The length of each segment is configured in MM_PER_ARC_SEGMENT (Default 1mm) + * Arcs should only be made relatively large (over 5mm), as larger arcs with + * larger segments will tend to be more efficient. Your slicer should have + * options for G2/G3 arc generation. In future these options may be GCode tunable. + */ + void plan_arc( + float raw[XYZE], // Destination position + float *offset, // Center of rotation relative to current_position + uint8_t clockwise // Clockwise? + ) { + #if ENABLED(CNC_WORKSPACE_PLANES) + AxisEnum p_axis, q_axis, l_axis; + switch (workspace_plane) { + default: + case PLANE_XY: p_axis = X_AXIS; q_axis = Y_AXIS; l_axis = Z_AXIS; break; + case PLANE_ZX: p_axis = Z_AXIS; q_axis = X_AXIS; l_axis = Y_AXIS; break; + case PLANE_YZ: p_axis = Y_AXIS; q_axis = Z_AXIS; l_axis = X_AXIS; break; + } + #else + constexpr AxisEnum p_axis = X_AXIS, q_axis = Y_AXIS, l_axis = Z_AXIS; + #endif + + // Radius vector from center to current location + float r_P = -offset[0], r_Q = -offset[1]; + + const float radius = HYPOT(r_P, r_Q), + center_P = current_position[p_axis] - r_P, + center_Q = current_position[q_axis] - r_Q, + rt_X = raw[p_axis] - center_P, + rt_Y = raw[q_axis] - center_Q, + linear_travel = raw[l_axis] - current_position[l_axis], + extruder_travel = raw[E_AXIS] - current_position[E_AXIS]; + + // CCW angle of rotation between position and target from the circle center. Only one atan2() trig computation required. + float angular_travel = ATAN2(r_P * rt_Y - r_Q * rt_X, r_P * rt_X + r_Q * rt_Y); + if (angular_travel < 0) angular_travel += RADIANS(360); + if (clockwise) angular_travel -= RADIANS(360); + + // Make a circle if the angular rotation is 0 and the target is current position + if (angular_travel == 0 && current_position[p_axis] == raw[p_axis] && current_position[q_axis] == raw[q_axis]) + angular_travel = RADIANS(360); + + const float mm_of_travel = HYPOT(angular_travel * radius, FABS(linear_travel)); + if (mm_of_travel < 0.001) return; + + uint16_t segments = FLOOR(mm_of_travel / (MM_PER_ARC_SEGMENT)); + if (segments == 0) segments = 1; + + /** + * Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector, + * and phi is the angle of rotation. Based on the solution approach by Jens Geisler. + * r_T = [cos(phi) -sin(phi); + * sin(phi) cos(phi)] * r ; + * + * For arc generation, the center of the circle is the axis of rotation and the radius vector is + * defined from the circle center to the initial position. Each line segment is formed by successive + * vector rotations. This requires only two cos() and sin() computations to form the rotation + * matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since + * all double numbers are single precision on the Arduino. (True double precision will not have + * round off issues for CNC applications.) Single precision error can accumulate to be greater than + * tool precision in some cases. Therefore, arc path correction is implemented. + * + * Small angle approximation may be used to reduce computation overhead further. This approximation + * holds for everything, but very small circles and large MM_PER_ARC_SEGMENT values. In other words, + * theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large + * to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for + * numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an + * issue for CNC machines with the single precision Arduino calculations. + * + * This approximation also allows plan_arc to immediately insert a line segment into the planner + * without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied + * a correction, the planner should have caught up to the lag caused by the initial plan_arc overhead. + * This is important when there are successive arc motions. + */ + // Vector rotation matrix values + float arc_target[XYZE]; + const float theta_per_segment = angular_travel / segments, + linear_per_segment = linear_travel / segments, + extruder_per_segment = extruder_travel / segments, + sin_T = theta_per_segment, + cos_T = 1 - 0.5 * sq(theta_per_segment); // Small angle approximation + + // Initialize the linear axis + arc_target[l_axis] = current_position[l_axis]; + + // Initialize the extruder axis + arc_target[E_AXIS] = current_position[E_AXIS]; + + const float fr_mm_s = MMS_SCALED(feedrate_mm_s); + + millis_t next_idle_ms = millis() + 200UL; + + #if N_ARC_CORRECTION > 1 + int8_t arc_recalc_count = N_ARC_CORRECTION; + #endif + + for (uint16_t i = 1; i < segments; i++) { // Iterate (segments-1) times + + thermalManager.manage_heater(); + if (ELAPSED(millis(), next_idle_ms)) { + next_idle_ms = millis() + 200UL; + idle(); + } + + #if N_ARC_CORRECTION > 1 + if (--arc_recalc_count) { + // Apply vector rotation matrix to previous r_P / 1 + const float r_new_Y = r_P * sin_T + r_Q * cos_T; + r_P = r_P * cos_T - r_Q * sin_T; + r_Q = r_new_Y; + } + else + #endif + { + #if N_ARC_CORRECTION > 1 + arc_recalc_count = N_ARC_CORRECTION; + #endif + + // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments. + // Compute exact location by applying transformation matrix from initial radius vector(=-offset). + // To reduce stuttering, the sin and cos could be computed at different times. + // For now, compute both at the same time. + const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment); + r_P = -offset[0] * cos_Ti + offset[1] * sin_Ti; + r_Q = -offset[0] * sin_Ti - offset[1] * cos_Ti; + } + + // Update arc_target location + arc_target[p_axis] = center_P + r_P; + arc_target[q_axis] = center_Q + r_Q; + arc_target[l_axis] += linear_per_segment; + arc_target[E_AXIS] += extruder_per_segment; + + clamp_to_software_endstops(arc_target); + + planner.buffer_line_kinematic(arc_target, fr_mm_s, active_extruder); + } + + // Ensure last segment arrives at target location. + planner.buffer_line_kinematic(raw, fr_mm_s, active_extruder); + + // As far as the parser is concerned, the position is now == target. In reality the + // motion control system might still be processing the action and the real tool position + // in any intermediate location. + set_current_from_destination(); + } // plan_arc + +#endif // ARC_SUPPORT + +#if ENABLED(BEZIER_CURVE_SUPPORT) + + void plan_cubic_move(const float offset[4]) { + cubic_b_spline(current_position, destination, offset, MMS_SCALED(feedrate_mm_s), active_extruder); + + // As far as the parser is concerned, the position is now == destination. In reality the + // motion control system might still be processing the action and the real tool position + // in any intermediate location. + set_current_from_destination(); + } + +#endif // BEZIER_CURVE_SUPPORT + +#if ENABLED(USE_CONTROLLER_FAN) + + void controllerFan() { + static millis_t lastMotorOn = 0, // Last time a motor was turned on + nextMotorCheck = 0; // Last time the state was checked + const millis_t ms = millis(); + if (ELAPSED(ms, nextMotorCheck)) { + nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s + if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || thermalManager.soft_pwm_amount_bed > 0 + || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled... + #if E_STEPPERS > 1 + || E1_ENABLE_READ == E_ENABLE_ON + #if HAS_X2_ENABLE + || X2_ENABLE_READ == X_ENABLE_ON + #endif + #if E_STEPPERS > 2 + || E2_ENABLE_READ == E_ENABLE_ON + #if E_STEPPERS > 3 + || E3_ENABLE_READ == E_ENABLE_ON + #if E_STEPPERS > 4 + || E4_ENABLE_READ == E_ENABLE_ON + #endif // E_STEPPERS > 4 + #endif // E_STEPPERS > 3 + #endif // E_STEPPERS > 2 + #endif // E_STEPPERS > 1 + ) { + lastMotorOn = ms; //... set time to NOW so the fan will turn on + } + + // Fan off if no steppers have been enabled for CONTROLLERFAN_SECS seconds + uint8_t speed = (!lastMotorOn || ELAPSED(ms, lastMotorOn + (CONTROLLERFAN_SECS) * 1000UL)) ? 0 : CONTROLLERFAN_SPEED; + + // allows digital or PWM fan output to be used (see M42 handling) + WRITE(CONTROLLER_FAN_PIN, speed); + analogWrite(CONTROLLER_FAN_PIN, speed); + } + } + +#endif // USE_CONTROLLER_FAN + +#if ENABLED(MORGAN_SCARA) + + /** + * Morgan SCARA Forward Kinematics. Results in cartes[]. + * Maths and first version by QHARLEY. + * Integrated into Marlin and slightly restructured by Joachim Cerny. + */ + void forward_kinematics_SCARA(const float &a, const float &b) { + + float a_sin = sin(RADIANS(a)) * L1, + a_cos = cos(RADIANS(a)) * L1, + b_sin = sin(RADIANS(b)) * L2, + b_cos = cos(RADIANS(b)) * L2; + + cartes[X_AXIS] = a_cos + b_cos + SCARA_OFFSET_X; //theta + cartes[Y_AXIS] = a_sin + b_sin + SCARA_OFFSET_Y; //theta+phi + + /* + SERIAL_ECHOPAIR("SCARA FK Angle a=", a); + SERIAL_ECHOPAIR(" b=", b); + SERIAL_ECHOPAIR(" a_sin=", a_sin); + SERIAL_ECHOPAIR(" a_cos=", a_cos); + SERIAL_ECHOPAIR(" b_sin=", b_sin); + SERIAL_ECHOLNPAIR(" b_cos=", b_cos); + SERIAL_ECHOPAIR(" cartes[X_AXIS]=", cartes[X_AXIS]); + SERIAL_ECHOLNPAIR(" cartes[Y_AXIS]=", cartes[Y_AXIS]); + //*/ + } + + /** + * Morgan SCARA Inverse Kinematics. Results in delta[]. + * + * See http://forums.reprap.org/read.php?185,283327 + * + * Maths and first version by QHARLEY. + * Integrated into Marlin and slightly restructured by Joachim Cerny. + */ + void inverse_kinematics(const float raw[XYZ]) { + + static float C2, S2, SK1, SK2, THETA, PSI; + + float sx = raw[X_AXIS] - SCARA_OFFSET_X, // Translate SCARA to standard X Y + sy = raw[Y_AXIS] - SCARA_OFFSET_Y; // With scaling factor. + + if (L1 == L2) + C2 = HYPOT2(sx, sy) / L1_2_2 - 1; + else + C2 = (HYPOT2(sx, sy) - (L1_2 + L2_2)) / (2.0 * L1 * L2); + + S2 = SQRT(1 - sq(C2)); + + // Unrotated Arm1 plus rotated Arm2 gives the distance from Center to End + SK1 = L1 + L2 * C2; + + // Rotated Arm2 gives the distance from Arm1 to Arm2 + SK2 = L2 * S2; + + // Angle of Arm1 is the difference between Center-to-End angle and the Center-to-Elbow + THETA = ATAN2(SK1, SK2) - ATAN2(sx, sy); + + // Angle of Arm2 + PSI = ATAN2(S2, C2); + + delta[A_AXIS] = DEGREES(THETA); // theta is support arm angle + delta[B_AXIS] = DEGREES(THETA + PSI); // equal to sub arm angle (inverted motor) + delta[C_AXIS] = raw[Z_AXIS]; + + /* + DEBUG_POS("SCARA IK", raw); + DEBUG_POS("SCARA IK", delta); + SERIAL_ECHOPAIR(" SCARA (x,y) ", sx); + SERIAL_ECHOPAIR(",", sy); + SERIAL_ECHOPAIR(" C2=", C2); + SERIAL_ECHOPAIR(" S2=", S2); + SERIAL_ECHOPAIR(" Theta=", THETA); + SERIAL_ECHOLNPAIR(" Phi=", PHI); + //*/ + } + +#endif // MORGAN_SCARA + +#if ENABLED(TEMP_STAT_LEDS) + + static bool red_led = false; + static millis_t next_status_led_update_ms = 0; + + void handle_status_leds(void) { + if (ELAPSED(millis(), next_status_led_update_ms)) { + next_status_led_update_ms += 500; // Update every 0.5s + float max_temp = 0.0; + #if HAS_TEMP_BED + max_temp = MAX3(max_temp, thermalManager.degTargetBed(), thermalManager.degBed()); + #endif + HOTEND_LOOP() + max_temp = MAX3(max_temp, thermalManager.degHotend(e), thermalManager.degTargetHotend(e)); + const bool new_led = (max_temp > 55.0) ? true : (max_temp < 54.0) ? false : red_led; + if (new_led != red_led) { + red_led = new_led; + #if PIN_EXISTS(STAT_LED_RED) + WRITE(STAT_LED_RED_PIN, new_led ? HIGH : LOW); + #if PIN_EXISTS(STAT_LED_BLUE) + WRITE(STAT_LED_BLUE_PIN, new_led ? LOW : HIGH); + #endif + #else + WRITE(STAT_LED_BLUE_PIN, new_led ? HIGH : LOW); + #endif + } + } + } + +#endif + +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + + void handle_filament_runout() { + if (!filament_ran_out) { + filament_ran_out = true; + enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT)); + stepper.synchronize(); + } + } + +#endif // FILAMENT_RUNOUT_SENSOR + +#if ENABLED(FAST_PWM_FAN) + + void setPwmFrequency(uint8_t pin, int val) { + val &= 0x07; + switch (digitalPinToTimer(pin)) { + #ifdef TCCR0A + #if !AVR_AT90USB1286_FAMILY + case TIMER0A: + #endif + case TIMER0B: + //_SET_CS(0, val); + break; + #endif + #ifdef TCCR1A + case TIMER1A: + case TIMER1B: + //_SET_CS(1, val); + break; + #endif + #ifdef TCCR2 + case TIMER2: + case TIMER2: + _SET_CS(2, val); + break; + #endif + #ifdef TCCR2A + case TIMER2A: + case TIMER2B: + _SET_CS(2, val); + break; + #endif + #ifdef TCCR3A + case TIMER3A: + case TIMER3B: + case TIMER3C: + _SET_CS(3, val); + break; + #endif + #ifdef TCCR4A + case TIMER4A: + case TIMER4B: + case TIMER4C: + _SET_CS(4, val); + break; + #endif + #ifdef TCCR5A + case TIMER5A: + case TIMER5B: + case TIMER5C: + _SET_CS(5, val); + break; + #endif + } + } + +#endif // FAST_PWM_FAN + +void enable_all_steppers() { + enable_X(); + enable_Y(); + enable_Z(); + enable_E0(); + enable_E1(); + enable_E2(); + enable_E3(); + enable_E4(); +} + +void disable_e_steppers() { + disable_E0(); + disable_E1(); + disable_E2(); + disable_E3(); + disable_E4(); +} + +void disable_all_steppers() { + disable_X(); + disable_Y(); + disable_Z(); + disable_e_steppers(); +} + +#if ENABLED(HAVE_TMC2130) + + void automatic_current_control(TMC2130Stepper &st, String axisID) { + // Check otpw even if we don't use automatic control. Allows for flag inspection. + const bool is_otpw = st.checkOT(); + + // Report if a warning was triggered + static bool previous_otpw = false; + if (is_otpw && !previous_otpw) { + char timestamp[10]; + duration_t elapsed = print_job_timer.duration(); + const bool has_days = (elapsed.value > 60*60*24L); + (void)elapsed.toDigital(timestamp, has_days); + SERIAL_ECHO(timestamp); + SERIAL_ECHOPGM(": "); + SERIAL_ECHO(axisID); + SERIAL_ECHOLNPGM(" driver overtemperature warning!"); + } + previous_otpw = is_otpw; + + #if CURRENT_STEP > 0 && ENABLED(AUTOMATIC_CURRENT_CONTROL) + // Return if user has not enabled current control start with M906 S1. + if (!auto_current_control) return; + + /** + * Decrease current if is_otpw is true. + * Bail out if driver is disabled. + * Increase current if OTPW has not been triggered yet. + */ + uint16_t current = st.getCurrent(); + if (is_otpw) { + st.setCurrent(current - CURRENT_STEP, R_SENSE, HOLD_MULTIPLIER); + #if ENABLED(REPORT_CURRENT_CHANGE) + SERIAL_ECHO(axisID); + SERIAL_ECHOPAIR(" current decreased to ", st.getCurrent()); + #endif + } + + else if (!st.isEnabled()) + return; + + else if (!is_otpw && !st.getOTPW()) { + current += CURRENT_STEP; + if (current <= AUTO_ADJUST_MAX) { + st.setCurrent(current, R_SENSE, HOLD_MULTIPLIER); + #if ENABLED(REPORT_CURRENT_CHANGE) + SERIAL_ECHO(axisID); + SERIAL_ECHOPAIR(" current increased to ", st.getCurrent()); + #endif + } + } + SERIAL_EOL(); + #endif + } + + void checkOverTemp() { + static millis_t next_cOT = 0; + if (ELAPSED(millis(), next_cOT)) { + next_cOT = millis() + 5000; + #if ENABLED(X_IS_TMC2130) + automatic_current_control(stepperX, "X"); + #endif + #if ENABLED(Y_IS_TMC2130) + automatic_current_control(stepperY, "Y"); + #endif + #if ENABLED(Z_IS_TMC2130) + automatic_current_control(stepperZ, "Z"); + #endif + #if ENABLED(X2_IS_TMC2130) + automatic_current_control(stepperX2, "X2"); + #endif + #if ENABLED(Y2_IS_TMC2130) + automatic_current_control(stepperY2, "Y2"); + #endif + #if ENABLED(Z2_IS_TMC2130) + automatic_current_control(stepperZ2, "Z2"); + #endif + #if ENABLED(E0_IS_TMC2130) + automatic_current_control(stepperE0, "E0"); + #endif + #if ENABLED(E1_IS_TMC2130) + automatic_current_control(stepperE1, "E1"); + #endif + #if ENABLED(E2_IS_TMC2130) + automatic_current_control(stepperE2, "E2"); + #endif + #if ENABLED(E3_IS_TMC2130) + automatic_current_control(stepperE3, "E3"); + #endif + #if ENABLED(E4_IS_TMC2130) + automatic_current_control(stepperE4, "E4"); + #endif + } + } + +#endif // HAVE_TMC2130 + +/** + * Manage several activities: + * - Check for Filament Runout + * - Keep the command buffer full + * - Check for maximum inactive time between commands + * - Check for maximum inactive time between stepper commands + * - Check if pin CHDK needs to go LOW + * - Check for KILL button held down + * - Check for HOME button held down + * - Check if cooling fan needs to be switched on + * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT) + */ +void manage_inactivity(bool ignore_stepper_queue/*=false*/) { + + #if ENABLED(FILAMENT_RUNOUT_SENSOR) + if ((IS_SD_PRINTING || print_job_timer.isRunning()) && (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING)) + handle_filament_runout(); + #endif + + if (commands_in_queue < BUFSIZE) get_available_commands(); + + const millis_t ms = millis(); + + if (max_inactive_time && ELAPSED(ms, previous_cmd_ms + max_inactive_time)) { + SERIAL_ERROR_START(); + SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr); + kill(PSTR(MSG_KILLED)); + } + + // Prevent steppers timing-out in the middle of M600 + #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT) + #define MOVE_AWAY_TEST !move_away_flag + #else + #define MOVE_AWAY_TEST true + #endif + + if (MOVE_AWAY_TEST && stepper_inactive_time && ELAPSED(ms, previous_cmd_ms + stepper_inactive_time) + && !ignore_stepper_queue && !planner.blocks_queued()) { + #if ENABLED(DISABLE_INACTIVE_X) + disable_X(); + #endif + #if ENABLED(DISABLE_INACTIVE_Y) + disable_Y(); + #endif + #if ENABLED(DISABLE_INACTIVE_Z) + disable_Z(); + #endif + #if ENABLED(DISABLE_INACTIVE_E) + disable_e_steppers(); + #endif + #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD + ubl_lcd_map_control = defer_return_to_status = false; + #endif + } + + #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH + if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) { + chdkActive = false; + WRITE(CHDK, LOW); + } + #endif + + #if HAS_KILL + + // Check if the kill button was pressed and wait just in case it was an accidental + // key kill key press + // ------------------------------------------------------------------------------- + static int killCount = 0; // make the inactivity button a bit less responsive + const int KILL_DELAY = 750; + if (!READ(KILL_PIN)) + killCount++; + else if (killCount > 0) + killCount--; + + // Exceeded threshold and we can confirm that it was not accidental + // KILL the machine + // ---------------------------------------------------------------- + if (killCount >= KILL_DELAY) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_KILL_BUTTON); + kill(PSTR(MSG_KILLED)); + } + #endif + + #if HAS_HOME + // Check to see if we have to home, use poor man's debouncer + // --------------------------------------------------------- + static int homeDebounceCount = 0; // poor man's debouncing count + const int HOME_DEBOUNCE_DELAY = 2500; + if (!IS_SD_PRINTING && !READ(HOME_PIN)) { + if (!homeDebounceCount) { + enqueue_and_echo_commands_P(PSTR("G28")); + LCD_MESSAGEPGM(MSG_AUTO_HOME); + } + if (homeDebounceCount < HOME_DEBOUNCE_DELAY) + homeDebounceCount++; + else + homeDebounceCount = 0; + } + #endif + + #if ENABLED(USE_CONTROLLER_FAN) + controllerFan(); // Check if fan should be turned on to cool stepper drivers down + #endif + + #if ENABLED(EXTRUDER_RUNOUT_PREVENT) + if (ELAPSED(ms, previous_cmd_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL) + && thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) { + #if ENABLED(SWITCHING_EXTRUDER) + const bool oldstatus = E0_ENABLE_READ; + enable_E0(); + #else // !SWITCHING_EXTRUDER + bool oldstatus; + switch (active_extruder) { + default: oldstatus = E0_ENABLE_READ; enable_E0(); break; + #if E_STEPPERS > 1 + case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break; + #if E_STEPPERS > 2 + case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break; + #if E_STEPPERS > 3 + case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break; + #if E_STEPPERS > 4 + case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break; + #endif // E_STEPPERS > 4 + #endif // E_STEPPERS > 3 + #endif // E_STEPPERS > 2 + #endif // E_STEPPERS > 1 + } + #endif // !SWITCHING_EXTRUDER + + previous_cmd_ms = ms; // refresh_cmd_timeout() + + const float olde = current_position[E_AXIS]; + current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE; + planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder); + current_position[E_AXIS] = olde; + planner.set_e_position_mm(olde); + stepper.synchronize(); + #if ENABLED(SWITCHING_EXTRUDER) + E0_ENABLE_WRITE(oldstatus); + #else + switch (active_extruder) { + case 0: E0_ENABLE_WRITE(oldstatus); break; + #if E_STEPPERS > 1 + case 1: E1_ENABLE_WRITE(oldstatus); break; + #if E_STEPPERS > 2 + case 2: E2_ENABLE_WRITE(oldstatus); break; + #if E_STEPPERS > 3 + case 3: E3_ENABLE_WRITE(oldstatus); break; + #if E_STEPPERS > 4 + case 4: E4_ENABLE_WRITE(oldstatus); break; + #endif // E_STEPPERS > 4 + #endif // E_STEPPERS > 3 + #endif // E_STEPPERS > 2 + #endif // E_STEPPERS > 1 + } + #endif // !SWITCHING_EXTRUDER + } + #endif // EXTRUDER_RUNOUT_PREVENT + + #if ENABLED(DUAL_X_CARRIAGE) + // handle delayed move timeout + if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) { + // travel moves have been received so enact them + delayed_move_time = 0xFFFFFFFFUL; // force moves to be done + set_destination_from_current(); + prepare_move_to_destination(); + } + #endif + + #if ENABLED(TEMP_STAT_LEDS) + handle_status_leds(); + #endif + + #if ENABLED(HAVE_TMC2130) + checkOverTemp(); + #endif + + planner.check_axes_activity(); +} + +/** + * Standard idle routine keeps the machine alive + */ +void idle( + #if ENABLED(ADVANCED_PAUSE_FEATURE) + bool no_stepper_sleep/*=false*/ + #endif +) { + #if ENABLED(MAX7219_DEBUG) + Max7219_idle_tasks(); + #endif // MAX7219_DEBUG + + lcd_update(); + + host_keepalive(); + + #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED) + auto_report_temperatures(); + #endif + + manage_inactivity( + #if ENABLED(ADVANCED_PAUSE_FEATURE) + no_stepper_sleep + #endif + ); + + thermalManager.manage_heater(); + + #if ENABLED(PRINTCOUNTER) + print_job_timer.tick(); + #endif + + #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER) + buzzer.tick(); + #endif + + #if ENABLED(I2C_POSITION_ENCODERS) + if (planner.blocks_queued() && + ( (blockBufferIndexRef != planner.block_buffer_head) || + ((lastUpdateMillis + I2CPE_MIN_UPD_TIME_MS) < millis())) ) { + blockBufferIndexRef = planner.block_buffer_head; + I2CPEM.update(); + lastUpdateMillis = millis(); + } + #endif +} + +/** + * Kill all activity and lock the machine. + * After this the machine will need to be reset. + */ +void kill(const char* lcd_msg) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_KILLED); + + thermalManager.disable_all_heaters(); + disable_all_steppers(); + + #if ENABLED(ULTRA_LCD) + kill_screen(lcd_msg); + #else + UNUSED(lcd_msg); + #endif + + _delay_ms(600); // Wait a short time (allows messages to get out before shutting down. + cli(); // Stop interrupts + + _delay_ms(250); //Wait to ensure all interrupts routines stopped + thermalManager.disable_all_heaters(); //turn off heaters again + + #ifdef ACTION_ON_KILL + SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL); + #endif + + #if HAS_POWER_SWITCH + SET_INPUT(PS_ON_PIN); + #endif + + suicide(); + while (1) { + #if ENABLED(USE_WATCHDOG) + watchdog_reset(); + #endif + } // Wait for reset +} + +/** + * Turn off heaters and stop the print in progress + * After a stop the machine may be resumed with M999 + */ +void stop() { + thermalManager.disable_all_heaters(); // 'unpause' taken care of in here + + #if ENABLED(PROBING_FANS_OFF) + if (fans_paused) fans_pause(false); // put things back the way they were + #endif + + if (IsRunning()) { + Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM(MSG_ERR_STOPPED); + LCD_MESSAGEPGM(MSG_STOPPED); + safe_delay(350); // allow enough time for messages to get out before stopping + Running = false; + } +} + +/** + * Marlin entry-point: Set up before the program loop + * - Set up the kill pin, filament runout, power hold + * - Start the serial port + * - Print startup messages and diagnostics + * - Get EEPROM or default settings + * - Initialize managers for: + * • temperature + * • planner + * • watchdog + * • stepper + * • photo pin + * • servos + * • LCD controller + * • Digipot I2C + * • Z probe sled + * • status LEDs + */ +void setup() { + + #if ENABLED(MAX7219_DEBUG) + Max7219_init(); + #endif + + #ifdef DISABLE_JTAG + // Disable JTAG on AT90USB chips to free up pins for IO + MCUCR = 0x80; + MCUCR = 0x80; + #endif + + #if ENABLED(FILAMENT_RUNOUT_SENSOR) + setup_filrunoutpin(); + #endif + + setup_killpin(); + + setup_powerhold(); + + #if HAS_STEPPER_RESET + disableStepperDrivers(); + #endif + + MYSERIAL.begin(BAUDRATE); + SERIAL_PROTOCOLLNPGM("start"); + SERIAL_ECHO_START(); + + // Check startup - does nothing if bootloader sets MCUSR to 0 + byte mcu = MCUSR; + if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP); + if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET); + if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET); + if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET); + if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET); + MCUSR = 0; + + SERIAL_ECHOPGM(MSG_MARLIN); + SERIAL_CHAR(' '); + SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION); + SERIAL_EOL(); + + #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR) + SERIAL_ECHO_START(); + SERIAL_ECHOPGM(MSG_CONFIGURATION_VER); + SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE); + SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR); + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM("Compiled: " __DATE__); + #endif + + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory()); + SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE); + + // Send "ok" after commands by default + for (int8_t i = 0; i < BUFSIZE; i++) send_ok[i] = true; + + // Load data from EEPROM if available (or use defaults) + // This also updates variables in the planner, elsewhere + (void)settings.load(); + + #if HAS_M206_COMMAND + // Initialize current position based on home_offset + COPY(current_position, home_offset); + #else + ZERO(current_position); + #endif + + // Vital to init stepper/planner equivalent for current_position + SYNC_PLAN_POSITION_KINEMATIC(); + + thermalManager.init(); // Initialize temperature loop + + #if ENABLED(USE_WATCHDOG) + watchdog_init(); + #endif + + stepper.init(); // Initialize stepper, this enables interrupts! + servo_init(); + + #if HAS_PHOTOGRAPH + OUT_WRITE(PHOTOGRAPH_PIN, LOW); + #endif + + #if HAS_CASE_LIGHT + case_light_on = CASE_LIGHT_DEFAULT_ON; + case_light_brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS; + update_case_light(); + #endif + + #if ENABLED(SPINDLE_LASER_ENABLE) + OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off + #if SPINDLE_DIR_CHANGE + OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3) + #endif + #if ENABLED(SPINDLE_LASER_PWM) + SET_OUTPUT(SPINDLE_LASER_PWM_PIN); + analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed + #endif + #endif + + #if HAS_BED_PROBE + endstops.enable_z_probe(false); + #endif + + #if ENABLED(USE_CONTROLLER_FAN) + SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan + #endif + + #if HAS_STEPPER_RESET + enableStepperDrivers(); + #endif + + #if ENABLED(DIGIPOT_I2C) + digipot_i2c_init(); + #endif + + #if ENABLED(DAC_STEPPER_CURRENT) + dac_init(); + #endif + + #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1 + OUT_WRITE(SOL1_PIN, LOW); // turn it off + #endif + + #if HAS_HOME + SET_INPUT_PULLUP(HOME_PIN); + #endif + + #if PIN_EXISTS(STAT_LED_RED) + OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off + #endif + + #if PIN_EXISTS(STAT_LED_BLUE) + OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off + #endif + + #if ENABLED(NEOPIXEL_LED) + SET_OUTPUT(NEOPIXEL_PIN); + setup_neopixel(); + #endif + + #if ENABLED(RGB_LED) || ENABLED(RGBW_LED) + SET_OUTPUT(RGB_LED_R_PIN); + SET_OUTPUT(RGB_LED_G_PIN); + SET_OUTPUT(RGB_LED_B_PIN); + #if ENABLED(RGBW_LED) + SET_OUTPUT(RGB_LED_W_PIN); + #endif + #endif + + #if ENABLED(MK2_MULTIPLEXER) + SET_OUTPUT(E_MUX0_PIN); + SET_OUTPUT(E_MUX1_PIN); + SET_OUTPUT(E_MUX2_PIN); + #endif + + #if HAS_FANMUX + fanmux_init(); + #endif + + lcd_init(); + + #if ENABLED(SHOW_BOOTSCREEN) + lcd_bootscreen(); + #if ENABLED(ULTRA_LCD) && DISABLED(SDSUPPORT) + lcd_init(); + #endif + #endif + + #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1 + // Initialize mixing to 100% color 1 + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + mixing_factor[i] = (i == 0) ? 1.0 : 0.0; + for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++) + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + mixing_virtual_tool_mix[t][i] = mixing_factor[i]; + #endif + + #if ENABLED(BLTOUCH) + // Make sure any BLTouch error condition is cleared + bltouch_command(BLTOUCH_RESET); + set_bltouch_deployed(true); + set_bltouch_deployed(false); + #endif + + #if ENABLED(I2C_POSITION_ENCODERS) + I2CPEM.init(); + #endif + + #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0 + i2c.onReceive(i2c_on_receive); + i2c.onRequest(i2c_on_request); + #endif + + #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) + setup_endstop_interrupts(); + #endif + + #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH + move_extruder_servo(0); // Initialize extruder servo + #endif + + #if ENABLED(SWITCHING_NOZZLE) + move_nozzle_servo(0); // Initialize nozzle servo + #endif + + #if ENABLED(PARKING_EXTRUDER) + #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT) + pe_activate_magnet(0); + pe_activate_magnet(1); + #else + pe_deactivate_magnet(0); + pe_deactivate_magnet(1); + #endif + #endif + #if ENABLED(MKS_12864OLED) + SET_OUTPUT(LCD_PINS_DC); + OUT_WRITE(LCD_PINS_RS, LOW); + delay(1000); + WRITE(LCD_PINS_RS, HIGH); + #endif +} + +/** + * The main Marlin program loop + * + * - Save or log commands to SD + * - Process available commands (if not saving) + * - Call heater manager + * - Call inactivity manager + * - Call endstop manager + * - Call LCD update + */ +void loop() { + if (commands_in_queue < BUFSIZE) get_available_commands(); + + #if ENABLED(SDSUPPORT) + card.checkautostart(false); + #endif + + if (commands_in_queue) { + + #if ENABLED(SDSUPPORT) + + if (card.saving) { + char* command = command_queue[cmd_queue_index_r]; + if (strstr_P(command, PSTR("M29"))) { + // M29 closes the file + card.closefile(); + SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED); + + #if ENABLED(SERIAL_STATS_DROPPED_RX) + SERIAL_ECHOLNPAIR("Dropped bytes: ", customizedSerial.dropped()); + #endif + + #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) + SERIAL_ECHOLNPAIR("Max RX Queue Size: ", customizedSerial.rxMaxEnqueued()); + #endif + + ok_to_send(); + } + else { + // Write the string from the read buffer to SD + card.write_command(command); + if (card.logging) + process_next_command(); // The card is saving because it's logging + else + ok_to_send(); + } + } + else + process_next_command(); + + #else + + process_next_command(); + + #endif // SDSUPPORT + + // The queue may be reset by a command handler or by code invoked by idle() within a handler + if (commands_in_queue) { + --commands_in_queue; + if (++cmd_queue_index_r >= BUFSIZE) cmd_queue_index_r = 0; + } + } + endstops.report_state(); + idle(); +} diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.cpp new file mode 100644 index 0000000..2eb8773 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.cpp @@ -0,0 +1,394 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * This module is off by default, but can be enabled to facilitate the display of + * extra debug information during code development. It assumes the existence of a + * Max7219 LED Matrix. A suitable device can be obtained on eBay similar to this: + * http://www.ebay.com/itm/191781645249 for under $2.00 including shipping. + * + * Just connect up +5v and GND to give it power, then connect up the pins assigned + * in Configuration_adv.h. For example, on the Re-ARM you could use: + * + * #define MAX7219_CLK_PIN 77 + * #define MAX7219_DIN_PIN 78 + * #define MAX7219_LOAD_PIN 79 + * + * Max7219_init() is called automatically at startup, and then there are a number of + * support functions available to control the LEDs in the 8x8 grid. + * + * void Max7219_init(); + * void Max7219_PutByte(uint8_t data); + * void Max7219(uint8_t reg, uint8_t data); + * void Max7219_LED_On(uint8_t col, uint8_t row); + * void Max7219_LED_Off(uint8_t col, uint8_t row); + * void Max7219_LED_Toggle(uint8_t col, uint8_t row); + * void Max7219_Clear_Row(uint8_t row); + * void Max7219_Clear_Column(uint8_t col); + * void Max7219_Set_Row(uint8_t row, uint8_t val); + * void Max7219_Set_2_Rows(uint8_t row, uint16_t val); + * void Max7219_Set_4_Rows(uint8_t row, uint32_t val); + * void Max7219_Set_Column(uint8_t col, uint8_t val); + * void Max7219_idle_tasks(); + */ + +#include "MarlinConfig.h" + +#if ENABLED(MAX7219_DEBUG) + +#include "Max7219_Debug_LEDs.h" + +#include "planner.h" +#include "stepper.h" +#include "Marlin.h" + +static uint8_t LEDs[8] = { 0 }; + +void Max7219_PutByte(uint8_t data) { + CRITICAL_SECTION_START + for (uint8_t i = 8; i--;) { + #ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed + delayMicroseconds(5); // to let the signal wires stabilize. + WRITE(MAX7219_CLK_PIN, LOW); // tick + delayMicroseconds(5); + WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit + delayMicroseconds(5); + WRITE(MAX7219_CLK_PIN, HIGH); // tock + delayMicroseconds(5); + #else + WRITE(MAX7219_CLK_PIN, LOW); // tick + WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit + WRITE(MAX7219_CLK_PIN, HIGH); // tock + #endif + + data <<= 1; + } + CRITICAL_SECTION_END +} + +void Max7219(const uint8_t reg, const uint8_t data) { + #ifdef CPU_32_BIT + delayMicroseconds(5); + #endif + CRITICAL_SECTION_START + WRITE(MAX7219_LOAD_PIN, LOW); // begin + #ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed + delayMicroseconds(5); // to let the signal wires stabilize. + #endif + Max7219_PutByte(reg); // specify register + #ifdef CPU_32_BIT + delayMicroseconds(5); + #endif + Max7219_PutByte(data); // put data + #ifdef CPU_32_BIT + delayMicroseconds(5); + #endif + WRITE(MAX7219_LOAD_PIN, LOW); // and tell the chip to load the data + #ifdef CPU_32_BIT + delayMicroseconds(5); + #endif + WRITE(MAX7219_LOAD_PIN, HIGH); + CRITICAL_SECTION_END + #ifdef CPU_32_BIT + delayMicroseconds(5); + #endif +} + +void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on) { + if (row > 7 || col > 7) { + int r,c; + r = row; + c = col; + SERIAL_ECHOPAIR("??? Max7219_LED_Set(",r); + SERIAL_ECHOPAIR(",",c); + SERIAL_ECHO(")\n"); + return; + } + if (TEST(LEDs[row], col) == on) return; // if LED is already on/off, leave alone + if (on) SBI(LEDs[row], col); else CBI(LEDs[row], col); + Max7219(8 - row, LEDs[row]); +} + +void Max7219_LED_On(const uint8_t col, const uint8_t row) { + if (row > 7 || col > 7) { + int r,c; + r = row; + c = col; + SERIAL_ECHOPAIR("??? Max7219_LED_On(",c); + SERIAL_ECHOPAIR(",",r); + SERIAL_ECHO(")\n"); + return; + } + Max7219_LED_Set(col, row, true); +} + +void Max7219_LED_Off(const uint8_t col, const uint8_t row) { + if (row > 7 || col > 7) { + int r,c; + r = row; + c = col; + SERIAL_ECHOPAIR("??? Max7219_LED_Off(",r); + SERIAL_ECHOPAIR(",",c); + SERIAL_ECHO(")\n"); + return; + } + Max7219_LED_Set(col, row, false); +} + +void Max7219_LED_Toggle(const uint8_t col, const uint8_t row) { + if (row > 7 || col > 7) { + int r,c; + r = row; + c = col; + SERIAL_ECHOPAIR("??? Max7219_LED_Toggle(",r); + SERIAL_ECHOPAIR(",",c); + SERIAL_ECHO(")\n"); + return; + } + if (TEST(LEDs[row], col)) + Max7219_LED_Off(col, row); + else + Max7219_LED_On(col, row); +} + +void Max7219_Clear_Column(const uint8_t col) { + if (col > 7) { + int c; + c = col; + SERIAL_ECHOPAIR("??? Max7219_Clear_Column(",c); + SERIAL_ECHO(")\n"); + return; + } + LEDs[col] = 0; + Max7219(8 - col, LEDs[col]); +} + +void Max7219_Clear_Row(const uint8_t row) { + if (row > 7) { + int r; + r = row; + SERIAL_ECHOPAIR("??? Max7219_Clear_Row(",r); + SERIAL_ECHO(")\n"); + return; + } + for (uint8_t c = 0; c <= 7; c++) + Max7219_LED_Off(c, row); +} + +void Max7219_Set_Row(const uint8_t row, const uint8_t val) { + if (row > 7 || val>255) { + int r, v; + r = row; + v = val; + SERIAL_ECHOPAIR("??? Max7219_Set_Row(",r); + SERIAL_ECHOPAIR(",",v); + SERIAL_ECHO(")\n"); + return; + } + for (uint8_t b = 0; b <= 7; b++) + if (TEST(val, b)) + Max7219_LED_On(7 - b, row); + else + Max7219_LED_Off(7 - b, row); +} + +void Max7219_Set_2_Rows(const uint8_t row, const uint16_t val) { + if (row > 6 || val>65535) { + int r, v; + r = row; + v = val; + SERIAL_ECHOPAIR("??? Max7219_Set_2_Rows(",r); + SERIAL_ECHOPAIR(",",v); + SERIAL_ECHO(")\n"); + return; + } + Max7219_Set_Row(row+1, (val & 0xff00) >> 8 ); + Max7219_Set_Row(row+0, (val & 0xff)); +} + +void Max7219_Set_4_Rows(const uint8_t row, const uint32_t val) { + if (row > 4 ) { + int r; + long v; + r = row; + v = val; + SERIAL_ECHOPAIR("??? Max7219_Set_4_Rows(",r); + SERIAL_ECHOPAIR(",",v); + SERIAL_ECHO(")\n"); + return; + } + Max7219_Set_Row(row+3, (val & 0xff000000) >> 24); + Max7219_Set_Row(row+2, (val & 0xff0000) >> 16); + Max7219_Set_Row(row+1, (val & 0xff00) >> 8); + Max7219_Set_Row(row+0, (val & 0xff)); +} + +void Max7219_Set_Column(const uint8_t col, const uint8_t val) { + if (val > 255 || col > 7) { + int v,c; + v = val; + c = col; + SERIAL_ECHOPAIR("??? Max7219_Column(",c); + SERIAL_ECHOPAIR(",",v); + SERIAL_ECHO(")\n"); + return; + } + LEDs[col] = val; + Max7219(8 - col, LEDs[col]); +} + +void Max7219_init() { + uint8_t i, x, y; + + SET_OUTPUT(MAX7219_DIN_PIN); + SET_OUTPUT(MAX7219_CLK_PIN); + + OUT_WRITE(MAX7219_LOAD_PIN, HIGH); + delay(1); + + //initiation of the max 7219 + Max7219(max7219_reg_scanLimit, 0x07); + Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) + Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode + Max7219(max7219_reg_displayTest, 0x00); // no display test + Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set + // range: 0x00 to 0x0F + for (i = 0; i <= 7; i++) { // empty registers, turn all LEDs off + LEDs[i] = 0x00; + Max7219(i + 1, 0); + } + + for (x = 0; x <= 7; x++) // Do an aesthetically pleasing pattern to fully test + for (y = 0; y <= 7; y++) { // the Max7219 module and LEDs. First, turn them + Max7219_LED_On(x, y); // all on. + delay(3); + } + + for (x = 0; x <= 7; x++) // Now, turn them all off. + for (y = 0; y <= 7; y++) { + Max7219_LED_Off(x, y); + delay(3); // delay() is OK here. Max7219_init() is only called from + } // setup() and nothing is running yet. + + delay(150); + + for (x = 8; x--;) // Now, do the same thing from the opposite direction + for (y = 0; y <= 7; y++) { + Max7219_LED_On(x, y); + delay(2); + } + + for (x = 8; x--;) + for (y = 0; y <= 7; y++) { + Max7219_LED_Off(x, y); + delay(2); + } +} + +/** +* These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes. +* There is very little CPU burden added to the system by displaying information within the idle() +* task. +* +* But with that said, if your debugging can be facilitated by making calls into the library from +* other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED +* or clear a row is not very significant. +*/ +void Max7219_idle_tasks() { +#if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE + CRITICAL_SECTION_START + #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE + uint8_t head; + head = planner.block_buffer_head; + #endif + #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE + uint8_t tail; + tail = planner.block_buffer_tail; + #endif + CRITICAL_SECTION_END +#endif + + #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE) + static millis_t next_blink = 0; + + if (ELAPSED(millis(), next_blink)) { + Max7219_LED_Toggle(7, 7); + next_blink = millis() + 750; + } + #endif + + #ifdef MAX7219_DEBUG_STEPPER_HEAD + static int16_t last_head_cnt=0; + if (last_head_cnt != head) { + if ( last_head_cnt < 8) + Max7219_LED_Off( last_head_cnt, MAX7219_DEBUG_STEPPER_HEAD); + else + Max7219_LED_Off( last_head_cnt-8, MAX7219_DEBUG_STEPPER_HEAD+1); + + last_head_cnt = head; + if ( head < 8) + Max7219_LED_On(head, MAX7219_DEBUG_STEPPER_HEAD); + else + Max7219_LED_On(head-8, MAX7219_DEBUG_STEPPER_HEAD+1); + } + #endif + + #ifdef MAX7219_DEBUG_STEPPER_TAIL + static int16_t last_tail_cnt=0; + if (last_tail_cnt != tail) { + if ( last_tail_cnt < 8) + Max7219_LED_Off( last_tail_cnt, MAX7219_DEBUG_STEPPER_TAIL); + else + Max7219_LED_Off( last_tail_cnt-8, MAX7219_DEBUG_STEPPER_TAIL+1); + + last_tail_cnt = tail; + if ( tail < 8) + Max7219_LED_On(tail, MAX7219_DEBUG_STEPPER_TAIL); + else + Max7219_LED_On(tail-8, MAX7219_DEBUG_STEPPER_TAIL+1); + } + #endif + + #ifdef MAX7219_DEBUG_STEPPER_QUEUE + static int16_t last_depth = 0; + int16_t current_depth = head - tail; + if (current_depth != last_depth) { // usually, no update will be needed. + if (current_depth < 0) current_depth += BLOCK_BUFFER_SIZE; + NOMORE(current_depth, BLOCK_BUFFER_SIZE); + NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines + // of LEDs is enough to see if the buffer is draining + + const uint8_t st = min(current_depth, last_depth), + en = max(current_depth, last_depth); + if (current_depth < last_depth) + for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs + Max7219_LED_Off(i/2, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); + else + for (uint8_t i = st; i <= en; i++) // set the LEDs to current depth + Max7219_LED_On(i/2, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); + + last_depth = current_depth; + } + #endif +} + +#endif // MAX7219_DEBUG diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.h new file mode 100644 index 0000000..3beccb0 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Max7219_Debug_LEDs.h @@ -0,0 +1,90 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * This module is off by default, but can be enabled to facilitate the display of + * extra debug information during code development. It assumes the existence of a + * Max7219 LED Matrix. A suitable device can be obtained on eBay similar to this: + * http://www.ebay.com/itm/191781645249 for under $2.00 including shipping. + * + * Just connect up +5v and GND to give it power, then connect up the pins assigned + * in Configuration_adv.h. For example, on the Re-ARM you could use: + * + * #define MAX7219_CLK_PIN 77 + * #define MAX7219_DIN_PIN 78 + * #define MAX7219_LOAD_PIN 79 + * + * Max7219_init() is called automatically at startup, and then there are a number of + * support functions available to control the LEDs in the 8x8 grid. + * + * void Max7219_init(); + * void Max7219_PutByte(uint8_t data); + * void Max7219(uint8_t reg, uint8_t data); + * void Max7219_LED_Set(uint8_t row, uint8_t col, bool on); + * void Max7219_LED_On(uint8_t col, uint8_t row); + * void Max7219_LED_Off(uint8_t col, uint8_t row); + * void Max7219_LED_Toggle(uint8_t row, uint8_t col); + * void Max7219_Clear_Row(uint8_t row); + * void Max7219_Clear_Column(uint8_t col); + * void Max7219_Set_Row(uint8_t row, uint8_t val); + * void Max7219_Set_2_Rows(uint8_t row, uint16_t val); + * void Max7219_Set_4_Rows(uint8_t row, uint32_t val); + * void Max7219_Set_Column(uint8_t col, uint8_t val); + * void Max7219_idle_tasks(); + */ + +#ifndef __MAX7219_DEBUG_LEDS_H__ +#define __MAX7219_DEBUG_LEDS_H__ + +// +// define max7219 registers +// +#define max7219_reg_noop 0x00 +#define max7219_reg_digit0 0x01 +#define max7219_reg_digit1 0x02 +#define max7219_reg_digit2 0x03 +#define max7219_reg_digit3 0x04 +#define max7219_reg_digit4 0x05 +#define max7219_reg_digit5 0x06 +#define max7219_reg_digit6 0x07 +#define max7219_reg_digit7 0x08 + +#define max7219_reg_intensity 0x0A +#define max7219_reg_displayTest 0x0F +#define max7219_reg_decodeMode 0x09 +#define max7219_reg_scanLimit 0x0B +#define max7219_reg_shutdown 0x0C + +void Max7219_init(); +void Max7219_PutByte(uint8_t data); +void Max7219(const uint8_t reg, const uint8_t data); +void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on); +void Max7219_LED_On(const uint8_t row, const uint8_t col); +void Max7219_LED_Off(const uint8_t row, const uint8_t col); +void Max7219_LED_Toggle(const uint8_t row, const uint8_t col); +void Max7219_Clear_Row(const uint8_t row); +void Max7219_Clear_Column(const uint8_t col); +void Max7219_Set_Row(const uint8_t row, const uint8_t val); +void Max7219_Set_Column(const uint8_t col, const uint8_t val); +void Max7219_idle_tasks(); + +#endif // __MAX7219_DEBUG_LEDS_H__ diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/SanityCheck.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/SanityCheck.h new file mode 100644 index 0000000..d2988c3 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/SanityCheck.h @@ -0,0 +1,1493 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * SanityCheck.h + * + * Test configuration values for errors at compile-time. + */ + +/** + * Require gcc 4.7 or newer (first included with Arduino 1.6.8) for C++11 features. + */ +#if __cplusplus < 201103L + #error "Marlin requires C++11 support (gcc >= 4.7, Arduino IDE >= 1.6.8). Please upgrade your toolchain." +#endif + +/** + * We try our best to include sanity checks for all changed configuration + * directives because users have a tendency to use outdated config files with + * the bleeding-edge source code, but sometimes this is not enough. This check + * forces a minimum config file revision. Otherwise Marlin will not build. + */ +#if ! defined(CONFIGURATION_H_VERSION) || CONFIGURATION_H_VERSION < REQUIRED_CONFIGURATION_H_VERSION + #error "You are using an old Configuration.h file, update it before building Marlin." +#endif + +#if ! defined(CONFIGURATION_ADV_H_VERSION) || CONFIGURATION_ADV_H_VERSION < REQUIRED_CONFIGURATION_ADV_H_VERSION + #error "You are using an old Configuration_adv.h file, update it before building Marlin." +#endif + +/** + * Warnings for old configurations + */ +#if !defined(X_BED_SIZE) || !defined(Y_BED_SIZE) + #error "X_BED_SIZE and Y_BED_SIZE are now required! Please update your configuration." +#elif WATCH_TEMP_PERIOD > 500 + #error "WATCH_TEMP_PERIOD now uses seconds instead of milliseconds." +#elif DISABLED(THERMAL_PROTECTION_HOTENDS) && (defined(WATCH_TEMP_PERIOD) || defined(THERMAL_PROTECTION_PERIOD)) + #error "Thermal Runaway Protection for hotends is now enabled with THERMAL_PROTECTION_HOTENDS." +#elif DISABLED(THERMAL_PROTECTION_BED) && defined(THERMAL_PROTECTION_BED_PERIOD) + #error "Thermal Runaway Protection for the bed is now enabled with THERMAL_PROTECTION_BED." +#elif (CORE_IS_XZ || CORE_IS_YZ) && ENABLED(Z_LATE_ENABLE) + #error "Z_LATE_ENABLE can't be used with COREXZ, COREZX, COREYZ, or COREZY." +#elif defined(X_HOME_RETRACT_MM) + #error "[XYZ]_HOME_RETRACT_MM settings have been renamed [XYZ]_HOME_BUMP_MM." +#elif defined(SDCARDDETECTINVERTED) + #error "SDCARDDETECTINVERTED is now SD_DETECT_INVERTED. Please update your configuration." +#elif defined(BTENABLED) + #error "BTENABLED is now BLUETOOTH. Please update your configuration." +#elif defined(CUSTOM_MENDEL_NAME) + #error "CUSTOM_MENDEL_NAME is now CUSTOM_MACHINE_NAME. Please update your configuration." +#elif defined(HAS_AUTOMATIC_VERSIONING) + #error "HAS_AUTOMATIC_VERSIONING is now USE_AUTOMATIC_VERSIONING. Please update your configuration." +#elif defined(SDSLOW) + #error "SDSLOW deprecated. Set SPI_SPEED to SPI_HALF_SPEED instead." +#elif defined(SDEXTRASLOW) + #error "SDEXTRASLOW deprecated. Set SPI_SPEED to SPI_QUARTER_SPEED instead." +#elif defined(FILAMENT_SENSOR) + #error "FILAMENT_SENSOR is deprecated. Use FILAMENT_WIDTH_SENSOR instead." +#elif defined(DISABLE_MAX_ENDSTOPS) || defined(DISABLE_MIN_ENDSTOPS) + #error "DISABLE_MAX_ENDSTOPS and DISABLE_MIN_ENDSTOPS deprecated. Use individual USE_*_PLUG options instead." +#elif defined(LANGUAGE_INCLUDE) + #error "LANGUAGE_INCLUDE has been replaced by LCD_LANGUAGE. Please update your configuration." +#elif defined(EXTRUDER_OFFSET_X) || defined(EXTRUDER_OFFSET_Y) + #error "EXTRUDER_OFFSET_[XY] is deprecated. Use HOTEND_OFFSET_[XY] instead." +#elif defined(PID_PARAMS_PER_EXTRUDER) + #error "PID_PARAMS_PER_EXTRUDER is deprecated. Use PID_PARAMS_PER_HOTEND instead." +#elif defined(EXTRUDER_WATTS) || defined(BED_WATTS) + #error "EXTRUDER_WATTS and BED_WATTS are deprecated. Remove them from your configuration." +#elif defined(SERVO_ENDSTOP_ANGLES) + #error "SERVO_ENDSTOP_ANGLES is deprecated. Use Z_SERVO_ANGLES instead." +#elif defined(X_ENDSTOP_SERVO_NR) || defined(Y_ENDSTOP_SERVO_NR) + #error "X_ENDSTOP_SERVO_NR and Y_ENDSTOP_SERVO_NR are deprecated and should be removed." +#elif defined(DEFAULT_XYJERK) + #error "DEFAULT_XYJERK is deprecated. Use DEFAULT_XJERK and DEFAULT_YJERK instead." +#elif defined(XY_TRAVEL_SPEED) + #error "XY_TRAVEL_SPEED is deprecated. Use XY_PROBE_SPEED instead." +#elif defined(PROBE_SERVO_DEACTIVATION_DELAY) + #error "PROBE_SERVO_DEACTIVATION_DELAY is deprecated. Use DEACTIVATE_SERVOS_AFTER_MOVE instead." +#elif defined(SERVO_DEACTIVATION_DELAY) + #error "SERVO_DEACTIVATION_DELAY is deprecated. Use SERVO_DELAY instead." +#elif ENABLED(FILAMENTCHANGEENABLE) + #error "FILAMENTCHANGEENABLE is now ADVANCED_PAUSE_FEATURE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_FEATURE) + #error "FILAMENT_CHANGE_FEATURE is now ADVANCED_PAUSE_FEATURE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_X_POS) + #error "FILAMENT_CHANGE_X_POS is now PAUSE_PARK_X_POS. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_Y_POS) + #error "FILAMENT_CHANGE_Y_POS is now PAUSE_PARK_Y_POS. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_Z_ADD) + #error "FILAMENT_CHANGE_Z_ADD is now PAUSE_PARK_Z_ADD. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_XY_FEEDRATE) + #error "FILAMENT_CHANGE_XY_FEEDRATE is now PAUSE_PARK_XY_FEEDRATE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_Z_FEEDRATE) + #error "FILAMENT_CHANGE_Z_FEEDRATE is now PAUSE_PARK_Z_FEEDRATE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_RETRACT_FEEDRATE) + #error "FILAMENT_CHANGE_RETRACT_FEEDRATE is now PAUSE_PARK_RETRACT_FEEDRATE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_RETRACT_LENGTH) + #error "FILAMENT_CHANGE_RETRACT_LENGTH is now PAUSE_PARK_RETRACT_LENGTH. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_EXTRUDE_FEEDRATE) + #error "FILAMENT_CHANGE_EXTRUDE_FEEDRATE is now ADVANCED_PAUSE_EXTRUDE_FEEDRATE. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_EXTRUDE_LENGTH) + #error "FILAMENT_CHANGE_EXTRUDE_LENGTH is now ADVANCED_PAUSE_EXTRUDE_LENGTH. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_NOZZLE_TIMEOUT) + #error "FILAMENT_CHANGE_NOZZLE_TIMEOUT is now PAUSE_PARK_NOZZLE_TIMEOUT. Please update your configuration." +#elif ENABLED(FILAMENT_CHANGE_NO_STEPPER_TIMEOUT) + #error "FILAMENT_CHANGE_NO_STEPPER_TIMEOUT is now PAUSE_PARK_NO_STEPPER_TIMEOUT. Please update your configuration." +#elif defined(PLA_PREHEAT_HOTEND_TEMP) + #error "PLA_PREHEAT_HOTEND_TEMP is now PREHEAT_1_TEMP_HOTEND. Please update your configuration." +#elif defined(PLA_PREHEAT_HPB_TEMP) + #error "PLA_PREHEAT_HPB_TEMP is now PREHEAT_1_TEMP_BED. Please update your configuration." +#elif defined(PLA_PREHEAT_FAN_SPEED) + #error "PLA_PREHEAT_FAN_SPEED is now PREHEAT_1_FAN_SPEED. Please update your configuration." +#elif defined(ABS_PREHEAT_HOTEND_TEMP) + #error "ABS_PREHEAT_HOTEND_TEMP is now PREHEAT_2_TEMP_HOTEND. Please update your configuration." +#elif defined(ABS_PREHEAT_HPB_TEMP) + #error "ABS_PREHEAT_HPB_TEMP is now PREHEAT_2_TEMP_BED. Please update your configuration." +#elif defined(ABS_PREHEAT_FAN_SPEED) + #error "ABS_PREHEAT_FAN_SPEED is now PREHEAT_2_FAN_SPEED. Please update your configuration." +#elif defined(ENDSTOPS_ONLY_FOR_HOMING) + #error "ENDSTOPS_ONLY_FOR_HOMING is deprecated. Use (disable) ENDSTOPS_ALWAYS_ON_DEFAULT instead." +#elif defined(HOMING_FEEDRATE) + #error "HOMING_FEEDRATE is deprecated. Set individual rates with HOMING_FEEDRATE_(XY|Z|E) instead." +#elif defined(MANUAL_HOME_POSITIONS) + #error "MANUAL_HOME_POSITIONS is deprecated. Set MANUAL_[XYZ]_HOME_POS as-needed instead." +#elif defined(PID_ADD_EXTRUSION_RATE) + #error "PID_ADD_EXTRUSION_RATE is now PID_EXTRUSION_SCALING and is DISABLED by default. Are you sure you want to use this option? Please update your configuration." +#elif defined(Z_RAISE_BEFORE_HOMING) + #error "Z_RAISE_BEFORE_HOMING is now Z_HOMING_HEIGHT. Please update your configuration." +#elif defined(MIN_Z_HEIGHT_FOR_HOMING) + #error "MIN_Z_HEIGHT_FOR_HOMING is now Z_HOMING_HEIGHT. Please update your configuration." +#elif defined(Z_RAISE_BEFORE_PROBING) || defined(Z_RAISE_AFTER_PROBING) + #error "Z_RAISE_(BEFORE|AFTER)_PROBING are deprecated. Use Z_CLEARANCE_DEPLOY_PROBE instead." +#elif defined(Z_RAISE_PROBE_DEPLOY_STOW) || defined(Z_RAISE_BETWEEN_PROBINGS) + #error "Z_RAISE_PROBE_DEPLOY_STOW and Z_RAISE_BETWEEN_PROBINGS are now Z_CLEARANCE_DEPLOY_PROBE and Z_CLEARANCE_BETWEEN_PROBES. Please update your configuration." +#elif defined(Z_PROBE_DEPLOY_HEIGHT) || defined(Z_PROBE_TRAVEL_HEIGHT) + #error "Z_PROBE_DEPLOY_HEIGHT and Z_PROBE_TRAVEL_HEIGHT are now Z_CLEARANCE_DEPLOY_PROBE and Z_CLEARANCE_BETWEEN_PROBES. Please update your configuration." +#elif defined(MANUAL_BED_LEVELING) + #error "MANUAL_BED_LEVELING is now LCD_BED_LEVELING. Please update your configuration." +#elif defined(MESH_HOME_SEARCH_Z) + #error "MESH_HOME_SEARCH_Z is now LCD_PROBE_Z_RANGE. Please update your configuration." +#elif defined(MANUAL_PROBE_Z_RANGE) + #error "MANUAL_PROBE_Z_RANGE is now LCD_PROBE_Z_RANGE. Please update your configuration." +#elif !defined(MIN_STEPS_PER_SEGMENT) + #error Please replace "const int dropsegments" with "#define MIN_STEPS_PER_SEGMENT" (and increase by 1) in Configuration_adv.h. +#elif defined(PREVENT_DANGEROUS_EXTRUDE) + #error "PREVENT_DANGEROUS_EXTRUDE is now PREVENT_COLD_EXTRUSION. Please update your configuration." +#elif defined(SCARA) + #error "SCARA is now MORGAN_SCARA. Please update your configuration." +#elif defined(ENABLE_AUTO_BED_LEVELING) + #error "ENABLE_AUTO_BED_LEVELING is deprecated. Specify AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_BILINEAR, or AUTO_BED_LEVELING_3POINT." +#elif defined(AUTO_BED_LEVELING_FEATURE) + #error "AUTO_BED_LEVELING_FEATURE is deprecated. Specify AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_BILINEAR, or AUTO_BED_LEVELING_3POINT." +#elif defined(ABL_GRID_POINTS) + #error "ABL_GRID_POINTS is now GRID_MAX_POINTS_X and GRID_MAX_POINTS_Y. Please update your configuration." +#elif defined(ABL_GRID_POINTS_X) || defined(ABL_GRID_POINTS_Y) + #error "ABL_GRID_POINTS_[XY] is now GRID_MAX_POINTS_[XY]. Please update your configuration." +#elif defined(ABL_GRID_MAX_POINTS_X) || defined(ABL_GRID_MAX_POINTS_Y) + #error "ABL_GRID_MAX_POINTS_[XY] is now GRID_MAX_POINTS_[XY]. Please update your configuration." +#elif defined(MESH_NUM_X_POINTS) || defined(MESH_NUM_Y_POINTS) + #error "MESH_NUM_[XY]_POINTS is now GRID_MAX_POINTS_[XY]. Please update your configuration." +#elif defined(UBL_MESH_NUM_X_POINTS) || defined(UBL_MESH_NUM_Y_POINTS) + #error "UBL_MESH_NUM_[XY]_POINTS is now GRID_MAX_POINTS_[XY]. Please update your configuration." +#elif defined(UBL_MESH_EDIT_ENABLED) + #error "UBL_MESH_EDIT_ENABLED is now UBL_G26_MESH_VALIDATION. Please update your configuration." +#elif defined(UBL_MESH_EDITING) + #error "UBL_MESH_EDITING is now UBL_G26_MESH_VALIDATION. Please update your configuration." +#elif defined(BLTOUCH_HEATERS_OFF) + #error "BLTOUCH_HEATERS_OFF is now PROBING_HEATERS_OFF. Please update your configuration." +#elif defined(BEEPER) + #error "BEEPER is now BEEPER_PIN. Please update your pins definitions." +#elif defined(SDCARDDETECT) + #error "SDCARDDETECT is now SD_DETECT_PIN. Please update your pins definitions." +#elif defined(STAT_LED_RED) || defined(STAT_LED_BLUE) + #error "STAT_LED_RED/STAT_LED_BLUE are now STAT_LED_RED_PIN/STAT_LED_BLUE_PIN. Please update your pins definitions." +#elif defined(LCD_PIN_BL) + #error "LCD_PIN_BL is now LCD_BACKLIGHT_PIN. Please update your pins definitions." +#elif defined(LCD_PIN_RESET) + #error "LCD_PIN_RESET is now LCD_RESET_PIN. Please update your pins definitions." +#elif defined(EXTRUDER_0_AUTO_FAN_PIN) || defined(EXTRUDER_1_AUTO_FAN_PIN) || defined(EXTRUDER_2_AUTO_FAN_PIN) || defined(EXTRUDER_3_AUTO_FAN_PIN) + #error "EXTRUDER_[0123]_AUTO_FAN_PIN is now E[0123]_AUTO_FAN_PIN. Please update your Configuration_adv.h." +#elif defined(min_software_endstops) || defined(max_software_endstops) + #error "(min|max)_software_endstops are now (MIN|MAX)_SOFTWARE_ENDSTOPS. Please update your configuration." +#elif ENABLED(Z_PROBE_SLED) && defined(SLED_PIN) + #error "Replace SLED_PIN with SOL1_PIN (applies to both Z_PROBE_SLED and SOLENOID_PROBE)." +#elif defined(CONTROLLERFAN_PIN) + #error "CONTROLLERFAN_PIN is now CONTROLLER_FAN_PIN, enabled with USE_CONTROLLER_FAN. Please update your Configuration_adv.h." +#elif defined(MIN_RETRACT) + #error "MIN_RETRACT is now MIN_AUTORETRACT and MAX_AUTORETRACT. Please update your Configuration_adv.h." +#elif defined(ADVANCE) + #error "ADVANCE was removed in Marlin 1.1.6. Please use LIN_ADVANCE." +#elif defined(NEOPIXEL_RGBW_LED) + #error "NEOPIXEL_RGBW_LED is now NEOPIXEL_LED. Please update your configuration." +#elif defined(UBL_MESH_INSET) + #error "UBL_MESH_INSET is now just MESH_INSET. Please update your configuration." +#elif defined(UBL_MESH_MIN_X) || defined(UBL_MESH_MIN_Y) || defined(UBL_MESH_MAX_X) || defined(UBL_MESH_MAX_Y) + #error "UBL_MESH_(MIN|MAX)_[XY] is now just MESH_(MIN|MAX)_[XY]. Please update your configuration." +#endif + +/** + * Marlin release, version and default string + */ +#ifndef SHORT_BUILD_VERSION + #error "SHORT_BUILD_VERSION must be specified." +#elif !defined(DETAILED_BUILD_VERSION) + #error "BUILD_VERSION must be specified." +#elif !defined(STRING_DISTRIBUTION_DATE) + #error "STRING_DISTRIBUTION_DATE must be specified." +#elif !defined(PROTOCOL_VERSION) + #error "PROTOCOL_VERSION must be specified." +#elif !defined(MACHINE_NAME) + #error "MACHINE_NAME must be specified." +#elif !defined(SOURCE_CODE_URL) + #error "SOURCE_CODE_URL must be specified." +#elif !defined(DEFAULT_MACHINE_UUID) + #error "DEFAULT_MACHINE_UUID must be specified." +#elif !defined(WEBSITE_URL) + #error "WEBSITE_URL must be specified." +#endif + +/** + * Dual Stepper Drivers + */ +#if ENABLED(X_DUAL_STEPPER_DRIVERS) && ENABLED(DUAL_X_CARRIAGE) + #error "DUAL_X_CARRIAGE is not compatible with X_DUAL_STEPPER_DRIVERS." +#elif ENABLED(X_DUAL_STEPPER_DRIVERS) && (!HAS_X2_ENABLE || !HAS_X2_STEP || !HAS_X2_DIR) + #error "X_DUAL_STEPPER_DRIVERS requires X2 pins (and an extra E plug)." +#elif ENABLED(Y_DUAL_STEPPER_DRIVERS) && (!HAS_Y2_ENABLE || !HAS_Y2_STEP || !HAS_Y2_DIR) + #error "Y_DUAL_STEPPER_DRIVERS requires Y2 pins (and an extra E plug)." +#elif ENABLED(Z_DUAL_STEPPER_DRIVERS) && (!HAS_Z2_ENABLE || !HAS_Z2_STEP || !HAS_Z2_DIR) + #error "Z_DUAL_STEPPER_DRIVERS requires Z2 pins (and an extra E plug)." +#endif + +/** + * Validate that the bed size fits + */ +static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE, + "Movement bounds ([XY]_MIN_POS, [XY]_MAX_POS) are too narrow to contain [XY]_BED_SIZE."); + +/** + * Granular software endstops (Marlin >= 1.1.7) + */ +#if ENABLED(MIN_SOFTWARE_ENDSTOPS) && DISABLED(MIN_SOFTWARE_ENDSTOP_Z) + #if IS_KINEMATIC + #error "MIN_SOFTWARE_ENDSTOPS on DELTA/SCARA also requires MIN_SOFTWARE_ENDSTOP_Z." + #elif DISABLED(MIN_SOFTWARE_ENDSTOP_X) && DISABLED(MIN_SOFTWARE_ENDSTOP_Y) + #error "MIN_SOFTWARE_ENDSTOPS requires at least one of the MIN_SOFTWARE_ENDSTOP_[XYZ] options." + #endif +#endif + +#if ENABLED(MAX_SOFTWARE_ENDSTOPS) && DISABLED(MAX_SOFTWARE_ENDSTOP_Z) + #if IS_KINEMATIC + #error "MAX_SOFTWARE_ENDSTOPS on DELTA/SCARA also requires MAX_SOFTWARE_ENDSTOP_Z." + #elif DISABLED(MAX_SOFTWARE_ENDSTOP_X) && DISABLED(MAX_SOFTWARE_ENDSTOP_Y) + #error "MAX_SOFTWARE_ENDSTOPS requires at least one of the MAX_SOFTWARE_ENDSTOP_[XYZ] options." + #endif +#endif + +/** + * Progress Bar + */ +#if ENABLED(LCD_PROGRESS_BAR) + #if DISABLED(SDSUPPORT) + #error "LCD_PROGRESS_BAR requires SDSUPPORT." + #elif ENABLED(DOGLCD) + #error "LCD_PROGRESS_BAR does not apply to graphical displays." + #elif ENABLED(FILAMENT_LCD_DISPLAY) + #error "LCD_PROGRESS_BAR and FILAMENT_LCD_DISPLAY are not fully compatible. Comment out this line to use both." + #endif +#endif + +/** + * SD File Sorting + */ +#if ENABLED(SDCARD_SORT_ALPHA) + #if SDSORT_LIMIT > 256 + #error "SDSORT_LIMIT must be 256 or smaller." + #elif SDSORT_LIMIT < 10 + #error "SDSORT_LIMIT should be greater than 9 to be useful." + #elif DISABLED(SDSORT_USES_RAM) + #if ENABLED(SDSORT_DYNAMIC_RAM) + #error "SDSORT_DYNAMIC_RAM requires SDSORT_USES_RAM (which reads the directory into RAM)." + #elif ENABLED(SDSORT_CACHE_NAMES) + #error "SDSORT_CACHE_NAMES requires SDSORT_USES_RAM (which reads the directory into RAM)." + #endif + #endif + + #if ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM) + #if SDSORT_CACHE_VFATS < 2 + #error "SDSORT_CACHE_VFATS must be 2 or greater!" + #elif SDSORT_CACHE_VFATS > MAX_VFAT_ENTRIES + #undef SDSORT_CACHE_VFATS + #define SDSORT_CACHE_VFATS MAX_VFAT_ENTRIES + #warning "SDSORT_CACHE_VFATS was reduced to MAX_VFAT_ENTRIES!" + #endif + #endif +#endif + +/** + * I2C Position Encoders + */ +#if ENABLED(I2C_POSITION_ENCODERS) + #if DISABLED(BABYSTEPPING) + #error "I2C_POSITION_ENCODERS requires BABYSTEPPING." + #elif !WITHIN(I2CPE_ENCODER_CNT, 1, 5) + #error "I2CPE_ENCODER_CNT must be between 1 and 5." + #endif +#endif + +/** + * Babystepping + */ +#if ENABLED(BABYSTEPPING) + #if DISABLED(ULTRA_LCD) && DISABLED(I2C_POSITION_ENCODERS) + #error "BABYSTEPPING requires an LCD controller." + #elif ENABLED(SCARA) + #error "BABYSTEPPING is not implemented for SCARA yet." + #elif ENABLED(DELTA) && ENABLED(BABYSTEP_XY) + #error "BABYSTEPPING only implemented for Z axis on deltabots." + #elif ENABLED(BABYSTEP_ZPROBE_OFFSET) && ENABLED(MESH_BED_LEVELING) + #error "MESH_BED_LEVELING and BABYSTEP_ZPROBE_OFFSET is not a valid combination" + #elif ENABLED(BABYSTEP_ZPROBE_OFFSET) && !HAS_BED_PROBE + #error "BABYSTEP_ZPROBE_OFFSET requires a probe." + #elif ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY) && !ENABLED(DOGLCD) + #error "BABYSTEP_ZPROBE_GFX_OVERLAY requires a DOGLCD." + #elif ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY) && !ENABLED(BABYSTEP_ZPROBE_OFFSET) + #error "BABYSTEP_ZPROBE_GFX_OVERLAY requires a BABYSTEP_ZPROBE_OFFSET." + #endif +#endif + +/** + * Filament Runout needs a pin and either SD Support or Auto print start detection + */ +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + #if !HAS_FIL_RUNOUT + #error "FILAMENT_RUNOUT_SENSOR requires FIL_RUNOUT_PIN." + #elif DISABLED(SDSUPPORT) && DISABLED(PRINTJOB_TIMER_AUTOSTART) + #error "FILAMENT_RUNOUT_SENSOR requires SDSUPPORT or PRINTJOB_TIMER_AUTOSTART." + #elif DISABLED(ADVANCED_PAUSE_FEATURE) + static_assert(NULL == strstr(FILAMENT_RUNOUT_SCRIPT, "M600"), "ADVANCED_PAUSE_FEATURE is required to use M600 with FILAMENT_RUNOUT_SENSOR."); + #endif +#endif + +/** + * Advanced Pause + */ +#if ENABLED(ADVANCED_PAUSE_FEATURE) + #if DISABLED(NEWPANEL) + #error "ADVANCED_PAUSE_FEATURE currently requires an LCD controller." + #elif ENABLED(EXTRUDER_RUNOUT_PREVENT) + #error "EXTRUDER_RUNOUT_PREVENT is incompatible with ADVANCED_PAUSE_FEATURE." + #elif ENABLED(PARK_HEAD_ON_PAUSE) && DISABLED(SDSUPPORT) && DISABLED(NEWPANEL) && DISABLED(EMERGENCY_PARSER) + #error "PARK_HEAD_ON_PAUSE requires SDSUPPORT, EMERGENCY_PARSER, or an LCD controller." + #elif ENABLED(HOME_BEFORE_FILAMENT_CHANGE) && DISABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT) + #error "HOME_BEFORE_FILAMENT_CHANGE requires PAUSE_PARK_NO_STEPPER_TIMEOUT" + #endif +#endif + +/** + * Individual axis homing is useless for DELTAS + */ +#if ENABLED(INDIVIDUAL_AXIS_HOMING_MENU) && ENABLED(DELTA) + #error "INDIVIDUAL_AXIS_HOMING_MENU is incompatible with DELTA kinematics." +#endif + +/** + * Options only for EXTRUDERS > 1 + */ +#if EXTRUDERS > 1 + + #if EXTRUDERS > 5 + #error "Marlin supports a maximum of 5 EXTRUDERS." + #endif + + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) + #error "EXTRUDERS must be 1 with TEMP_SENSOR_1_AS_REDUNDANT." + #endif + + #if ENABLED(HEATERS_PARALLEL) + #error "EXTRUDERS must be 1 with HEATERS_PARALLEL." + #endif + +#elif ENABLED(MK2_MULTIPLEXER) + #error "MK2_MULTIPLEXER requires 2 or more EXTRUDERS." +#elif ENABLED(SINGLENOZZLE) + #error "SINGLENOZZLE requires 2 or more EXTRUDERS." +#endif + +/** + * Sanity checking for the Průša MK2 Multiplexer + */ +#ifdef SNMM + #error "SNMM is now MK2_MULTIPLEXER. Please update your configuration." +#elif ENABLED(MK2_MULTIPLEXER) && DISABLED(ADVANCED_PAUSE_FEATURE) + #error "ADVANCED_PAUSE_FEATURE is required with MK2_MULTIPLEXER." +#endif + +/** + * A Dual Nozzle carriage with switching servo + */ +#if ENABLED(SWITCHING_NOZZLE) + #if ENABLED(DUAL_X_CARRIAGE) + #error "SWITCHING_NOZZLE and DUAL_X_CARRIAGE are incompatible." + #elif ENABLED(SINGLENOZZLE) + #error "SWITCHING_NOZZLE and SINGLENOZZLE are incompatible." + #elif EXTRUDERS != 2 + #error "SWITCHING_NOZZLE requires exactly 2 EXTRUDERS." + #elif NUM_SERVOS < 1 + #error "SWITCHING_NOZZLE requires NUM_SERVOS >= 1." + #endif +#endif + +/** + * Single Stepper Dual Extruder with switching servo + */ +#if ENABLED(SWITCHING_EXTRUDER) && NUM_SERVOS < 1 + #error "SWITCHING_EXTRUDER requires NUM_SERVOS >= 1." +#endif + +/** + * Mixing Extruder requirements + */ +#if ENABLED(MIXING_EXTRUDER) + #if EXTRUDERS > 1 + #error "MIXING_EXTRUDER currently only supports one extruder." + #elif MIXING_STEPPERS < 2 + #error "You must set MIXING_STEPPERS >= 2 for a mixing extruder." + #elif ENABLED(FILAMENT_SENSOR) + #error "MIXING_EXTRUDER is incompatible with FILAMENT_SENSOR. Comment out this line to use it anyway." + #elif ENABLED(SWITCHING_EXTRUDER) + #error "Please select either MIXING_EXTRUDER or SWITCHING_EXTRUDER, not both." + #elif ENABLED(SINGLENOZZLE) + #error "MIXING_EXTRUDER is incompatible with SINGLENOZZLE." + #elif ENABLED(LIN_ADVANCE) + #error "MIXING_EXTRUDER is incompatible with LIN_ADVANCE." + #endif +#endif + +/** + * Parking Extruder requirements + */ +#if ENABLED(PARKING_EXTRUDER) + #if ENABLED(DUAL_X_CARRIAGE) + #error "PARKING_EXTRUDER and DUAL_X_CARRIAGE are incompatible." + #elif ENABLED(SINGLENOZZLE) + #error "PARKING_EXTRUDER and SINGLENOZZLE are incompatible." + #elif ENABLED(EXT_SOLENOID) + #error "PARKING_EXTRUDER and EXT_SOLENOID are incompatible. (Pins are used twice.)" + #elif EXTRUDERS != 2 + #error "PARKING_EXTRUDER requires exactly 2 EXTRUDERS." + #elif !PIN_EXISTS(SOL0) || !PIN_EXISTS(SOL1) + #error "PARKING_EXTRUDER requires SOL0_PIN and SOL1_PIN." + #elif !defined(PARKING_EXTRUDER_PARKING_X) + #error "PARKING_EXTRUDER requires PARKING_EXTRUDER_PARKING_X." + #elif !defined(PARKING_EXTRUDER_SECURITY_RAISE) + #error "PARKING_EXTRUDER requires PARKING_EXTRUDER_SECURITY_RAISE." + #elif PARKING_EXTRUDER_SECURITY_RAISE < 0 + #error "PARKING_EXTRUDER_SECURITY_RAISE must be 0 or higher." + #elif !defined(PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE) || !WITHIN(PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE, LOW, HIGH) + #error "PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE must be defined as HIGH or LOW." + #elif !defined(PARKING_EXTRUDER_SOLENOIDS_DELAY) || !WITHIN(PARKING_EXTRUDER_SOLENOIDS_DELAY, 0, 2000) + #error "PARKING_EXTRUDER_SOLENOIDS_DELAY must be between 0 and 2000 (ms)." + #endif +#endif + +/** + * Part-Cooling Fan Multiplexer requirements + */ +#if PIN_EXISTS(FANMUX1) + #if !HAS_FANMUX + #error "FANMUX0_PIN must be set before FANMUX1_PIN can be set." + #endif +#elif PIN_EXISTS(FANMUX2) + #error "FANMUX0_PIN and FANMUX1_PIN must be set before FANMUX2_PIN can be set." +#endif + +/** + * Limited number of servos + */ +#if NUM_SERVOS > 4 + #error "The maximum number of SERVOS in Marlin is 4." +#endif + +/** + * Servo deactivation depends on servo endstops, switching nozzle, or switching extruder + */ +#if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE) && !HAS_Z_SERVO_ENDSTOP && !defined(SWITCHING_NOZZLE_SERVO_NR) && !defined(SWITCHING_EXTRUDER_SERVO_NR) + #error "Z_ENDSTOP_SERVO_NR, switching nozzle, or switching extruder is required for DEACTIVATE_SERVOS_AFTER_MOVE." +#endif + +/** + * Required LCD language + */ +#if DISABLED(DOGLCD) && ENABLED(ULTRA_LCD) && !defined(DISPLAY_CHARSET_HD44780) + #error "You must set DISPLAY_CHARSET_HD44780 to JAPANESE, WESTERN or CYRILLIC for your LCD controller." +#endif + +/** + * Bed Heating Options - PID vs Limit Switching + */ +#if ENABLED(PIDTEMPBED) && ENABLED(BED_LIMIT_SWITCHING) + #error "To use BED_LIMIT_SWITCHING you must disable PIDTEMPBED." +#endif + +/** + * Kinematics + */ + +/** + * Allow only one kinematic type to be defined + */ +static_assert(1 >= 0 + #if ENABLED(DELTA) + + 1 + #endif + #if ENABLED(MORGAN_SCARA) + + 1 + #endif + #if ENABLED(MAKERARM_SCARA) + + 1 + #endif + #if ENABLED(COREXY) + + 1 + #endif + #if ENABLED(COREXZ) + + 1 + #endif + #if ENABLED(COREYZ) + + 1 + #endif + #if ENABLED(COREYX) + + 1 + #endif + #if ENABLED(COREZX) + + 1 + #endif + #if ENABLED(COREZY) + + 1 + #endif + , "Please enable only one of DELTA, MORGAN_SCARA, MAKERARM_SCARA, COREXY, COREYX, COREXZ, COREZX, COREYZ, or COREZY." +); + +/** + * Delta requirements + */ +#if ENABLED(DELTA) + #if DISABLED(USE_XMAX_PLUG) && DISABLED(USE_YMAX_PLUG) && DISABLED(USE_ZMAX_PLUG) + #error "You probably want to use Max Endstops for DELTA!" + #elif ENABLED(ENABLE_LEVELING_FADE_HEIGHT) && DISABLED(AUTO_BED_LEVELING_BILINEAR) && !UBL_DELTA + #error "ENABLE_LEVELING_FADE_HEIGHT on DELTA requires AUTO_BED_LEVELING_BILINEAR or AUTO_BED_LEVELING_UBL." + #elif ENABLED(DELTA_AUTO_CALIBRATION) && !(HAS_BED_PROBE || ENABLED(ULTIPANEL)) + #error "DELTA_AUTO_CALIBRATION requires either a probe or an LCD Controller." + #elif ABL_GRID + #if (GRID_MAX_POINTS_X & 1) == 0 || (GRID_MAX_POINTS_Y & 1) == 0 + #error "DELTA requires GRID_MAX_POINTS_X and GRID_MAX_POINTS_Y to be odd numbers." + #elif GRID_MAX_POINTS_X < 3 + #error "DELTA requires GRID_MAX_POINTS_X and GRID_MAX_POINTS_Y to be 3 or higher." + #endif + #endif +#endif + +/** + * Probes + */ + +/** + * Allow only one probe option to be defined + */ +static_assert(1 >= 0 + #if ENABLED(PROBE_MANUALLY) + + 1 + #endif + #if ENABLED(FIX_MOUNTED_PROBE) + + 1 + #endif + #if HAS_Z_SERVO_ENDSTOP && DISABLED(BLTOUCH) + + 1 + #endif + #if ENABLED(BLTOUCH) + + 1 + #endif + #if ENABLED(SOLENOID_PROBE) + + 1 + #endif + #if ENABLED(Z_PROBE_ALLEN_KEY) + + 1 + #endif + #if ENABLED(Z_PROBE_SLED) + + 1 + #endif + , "Please enable only one probe option: PROBE_MANUALLY, FIX_MOUNTED_PROBE, BLTOUCH, SOLENOID_PROBE, Z_PROBE_ALLEN_KEY, Z_PROBE_SLED, or Z Servo." +); + +#if HAS_BED_PROBE + + /** + * Z_PROBE_SLED is incompatible with DELTA + */ + #if ENABLED(Z_PROBE_SLED) && ENABLED(DELTA) + #error "You cannot use Z_PROBE_SLED with DELTA." + #endif + + /** + * SOLENOID_PROBE requirements + */ + #if ENABLED(SOLENOID_PROBE) + #if ENABLED(EXT_SOLENOID) + #error "SOLENOID_PROBE is incompatible with EXT_SOLENOID." + #elif !HAS_SOLENOID_1 + #error "SOLENOID_PROBE requires SOL1_PIN. It can be added to your Configuration.h." + #endif + #endif + + /** + * NUM_SERVOS is required for a Z servo probe + */ + #if HAS_Z_SERVO_ENDSTOP + #ifndef NUM_SERVOS + #error "You must set NUM_SERVOS for a Z servo probe (Z_ENDSTOP_SERVO_NR)." + #elif Z_ENDSTOP_SERVO_NR >= NUM_SERVOS + #error "Z_ENDSTOP_SERVO_NR must be smaller than NUM_SERVOS." + #endif + #endif + + /** + * Require pin options and pins to be defined + */ + #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) + #if ENABLED(Z_MIN_PROBE_ENDSTOP) + #error "Enable only one option: Z_MIN_PROBE_ENDSTOP or Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN." + #elif DISABLED(USE_ZMIN_PLUG) + #error "Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN requires USE_ZMIN_PLUG to be enabled." + #elif !HAS_Z_MIN + #error "Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN requires the Z_MIN_PIN to be defined." + #elif ENABLED(Z_MIN_PROBE_ENDSTOP_INVERTING) != ENABLED(Z_MIN_ENDSTOP_INVERTING) + #error "Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN requires Z_MIN_ENDSTOP_INVERTING to match Z_MIN_PROBE_ENDSTOP_INVERTING." + #endif + #elif ENABLED(Z_MIN_PROBE_ENDSTOP) + #if !HAS_Z_MIN_PROBE_PIN + #error "Z_MIN_PROBE_ENDSTOP requires the Z_MIN_PROBE_PIN to be defined." + #endif + #else + #error "You must enable either Z_MIN_PROBE_ENDSTOP or Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN to use a probe." + #endif + + /** + * Make sure Z raise values are set + */ + #ifndef Z_CLEARANCE_DEPLOY_PROBE + #error "You must define Z_CLEARANCE_DEPLOY_PROBE in your configuration." + #elif !defined(Z_CLEARANCE_BETWEEN_PROBES) + #error "You must define Z_CLEARANCE_BETWEEN_PROBES in your configuration." + #elif Z_CLEARANCE_DEPLOY_PROBE < 0 + #error "Probes need Z_CLEARANCE_DEPLOY_PROBE >= 0." + #elif Z_CLEARANCE_BETWEEN_PROBES < 0 + #error "Probes need Z_CLEARANCE_BETWEEN_PROBES >= 0." + #endif + +#else + + /** + * Require some kind of probe for bed leveling and probe testing + */ + #if OLDSCHOOL_ABL && !PROBE_SELECTED + #error "Auto Bed Leveling requires one of these: PROBE_MANUALLY, FIX_MOUNTED_PROBE, BLTOUCH, SOLENOID_PROBE, Z_PROBE_ALLEN_KEY, Z_PROBE_SLED, or a Z Servo." + #endif + + #if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST) + #error "Z_MIN_PROBE_REPEATABILITY_TEST requires a probe: FIX_MOUNTED_PROBE, BLTOUCH, SOLENOID_PROBE, Z_PROBE_ALLEN_KEY, Z_PROBE_SLED, or Z Servo." + #endif + +#endif + +/** + * Allow only one bed leveling option to be defined + */ +static_assert(1 >= 0 + #if ENABLED(AUTO_BED_LEVELING_LINEAR) + + 1 + #endif + #if ENABLED(AUTO_BED_LEVELING_3POINT) + + 1 + #endif + #if ENABLED(AUTO_BED_LEVELING_BILINEAR) + + 1 + #endif + #if ENABLED(AUTO_BED_LEVELING_UBL) + + 1 + #endif + #if ENABLED(MESH_BED_LEVELING) + + 1 + #endif + , "Select only one of: MESH_BED_LEVELING, AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_3POINT, AUTO_BED_LEVELING_BILINEAR or AUTO_BED_LEVELING_UBL." +); + +/** + * Bed Leveling Requirements + */ + +#if ENABLED(AUTO_BED_LEVELING_UBL) + + /** + * Unified Bed Leveling + */ + + // Hide PROBE_MANUALLY from the rest of the code + #undef PROBE_MANUALLY + + #if IS_SCARA + #error "AUTO_BED_LEVELING_UBL does not yet support SCARA printers." + #elif DISABLED(EEPROM_SETTINGS) + #error "AUTO_BED_LEVELING_UBL requires EEPROM_SETTINGS. Please update your configuration." + #elif !WITHIN(GRID_MAX_POINTS_X, 3, 15) || !WITHIN(GRID_MAX_POINTS_Y, 3, 15) + #error "GRID_MAX_POINTS_[XY] must be a whole number between 3 and 15." + #else + static_assert(WITHIN(UBL_PROBE_PT_1_X, MIN_PROBE_X, MAX_PROBE_X), "UBL_PROBE_PT_1_X can't be reached by the Z probe."); + static_assert(WITHIN(UBL_PROBE_PT_2_X, MIN_PROBE_X, MAX_PROBE_X), "UBL_PROBE_PT_2_X can't be reached by the Z probe."); + static_assert(WITHIN(UBL_PROBE_PT_3_X, MIN_PROBE_X, MAX_PROBE_X), "UBL_PROBE_PT_3_X can't be reached by the Z probe."); + static_assert(WITHIN(UBL_PROBE_PT_1_Y, MIN_PROBE_Y, MAX_PROBE_Y), "UBL_PROBE_PT_1_Y can't be reached by the Z probe."); + static_assert(WITHIN(UBL_PROBE_PT_2_Y, MIN_PROBE_Y, MAX_PROBE_Y), "UBL_PROBE_PT_2_Y can't be reached by the Z probe."); + static_assert(WITHIN(UBL_PROBE_PT_3_Y, MIN_PROBE_Y, MAX_PROBE_Y), "UBL_PROBE_PT_3_Y can't be reached by the Z probe."); + #endif + +#elif OLDSCHOOL_ABL + + /** + * Auto Bed Leveling + */ + + #if ENABLED(USE_RAW_KINEMATICS) + #error "USE_RAW_KINEMATICS is not compatible with AUTO_BED_LEVELING" + #endif + + /** + * Delta and SCARA have limited bed leveling options + */ + #if IS_SCARA && DISABLED(AUTO_BED_LEVELING_BILINEAR) + #error "Only AUTO_BED_LEVELING_BILINEAR currently supports SCARA bed leveling." + #endif + + /** + * Check auto bed leveling probe points + */ + #if ABL_GRID + + #ifdef DELTA_PROBEABLE_RADIUS + static_assert(LEFT_PROBE_BED_POSITION >= -DELTA_PROBEABLE_RADIUS, "LEFT_PROBE_BED_POSITION must be within DELTA_PROBEABLE_RADIUS."); + static_assert(RIGHT_PROBE_BED_POSITION <= DELTA_PROBEABLE_RADIUS, "RIGHT_PROBE_BED_POSITION must be within DELTA_PROBEABLE_RADIUS."); + static_assert(FRONT_PROBE_BED_POSITION >= -DELTA_PROBEABLE_RADIUS, "FRONT_PROBE_BED_POSITION must be within DELTA_PROBEABLE_RADIUS."); + static_assert(BACK_PROBE_BED_POSITION <= DELTA_PROBEABLE_RADIUS, "BACK_PROBE_BED_POSITION must be within DELTA_PROBEABLE_RADIUS."); + #else + static_assert(LEFT_PROBE_BED_POSITION < RIGHT_PROBE_BED_POSITION, "LEFT_PROBE_BED_POSITION must be less than RIGHT_PROBE_BED_POSITION."); + static_assert(FRONT_PROBE_BED_POSITION < BACK_PROBE_BED_POSITION, "FRONT_PROBE_BED_POSITION must be less than BACK_PROBE_BED_POSITION."); + static_assert(LEFT_PROBE_BED_POSITION >= MIN_PROBE_X, "LEFT_PROBE_BED_POSITION can't be reached by the Z probe."); + static_assert(RIGHT_PROBE_BED_POSITION <= MAX_PROBE_X, "RIGHT_PROBE_BED_POSITION can't be reached by the Z probe."); + static_assert(FRONT_PROBE_BED_POSITION >= MIN_PROBE_Y, "FRONT_PROBE_BED_POSITION can't be reached by the Z probe."); + static_assert(BACK_PROBE_BED_POSITION <= MAX_PROBE_Y, "BACK_PROBE_BED_POSITION can't be reached by the Z probe."); + #endif + + #else // AUTO_BED_LEVELING_3POINT + + static_assert(WITHIN(ABL_PROBE_PT_1_X, MIN_PROBE_X, MAX_PROBE_X), "ABL_PROBE_PT_1_X can't be reached by the Z probe."); + static_assert(WITHIN(ABL_PROBE_PT_2_X, MIN_PROBE_X, MAX_PROBE_X), "ABL_PROBE_PT_2_X can't be reached by the Z probe."); + static_assert(WITHIN(ABL_PROBE_PT_3_X, MIN_PROBE_X, MAX_PROBE_X), "ABL_PROBE_PT_3_X can't be reached by the Z probe."); + static_assert(WITHIN(ABL_PROBE_PT_1_Y, MIN_PROBE_Y, MAX_PROBE_Y), "ABL_PROBE_PT_1_Y can't be reached by the Z probe."); + static_assert(WITHIN(ABL_PROBE_PT_2_Y, MIN_PROBE_Y, MAX_PROBE_Y), "ABL_PROBE_PT_2_Y can't be reached by the Z probe."); + static_assert(WITHIN(ABL_PROBE_PT_3_Y, MIN_PROBE_Y, MAX_PROBE_Y), "ABL_PROBE_PT_3_Y can't be reached by the Z probe."); + + #endif // AUTO_BED_LEVELING_3POINT + +#elif ENABLED(MESH_BED_LEVELING) + + // Hide PROBE_MANUALLY from the rest of the code + #undef PROBE_MANUALLY + + /** + * Mesh Bed Leveling + */ + + #if ENABLED(DELTA) + #error "MESH_BED_LEVELING is not compatible with DELTA printers." + #elif GRID_MAX_POINTS_X > 9 || GRID_MAX_POINTS_Y > 9 + #error "GRID_MAX_POINTS_X and GRID_MAX_POINTS_Y must be less than 10 for MBL." + #endif + +#endif + +/** + * LCD_BED_LEVELING requirements + */ +#if ENABLED(LCD_BED_LEVELING) + #if DISABLED(ULTIPANEL) + #error "LCD_BED_LEVELING requires an LCD controller." + #elif !(ENABLED(MESH_BED_LEVELING) || (OLDSCHOOL_ABL && ENABLED(PROBE_MANUALLY))) + #error "LCD_BED_LEVELING requires MESH_BED_LEVELING or ABL with PROBE_MANUALLY." + #endif +#endif + +/** + * Homing Bump + */ +#if X_HOME_BUMP_MM < 0 || Y_HOME_BUMP_MM < 0 || Z_HOME_BUMP_MM < 0 + #error "[XYZ]_HOME_BUMP_MM must be greater than or equal to 0." +#endif + +/** + * Make sure Z_SAFE_HOMING point is reachable + */ +#if ENABLED(Z_SAFE_HOMING) + #if !WITHIN(Z_SAFE_HOMING_X_POINT, MIN_PROBE_X, MAX_PROBE_X) + #if HAS_BED_PROBE + #error "Z_SAFE_HOMING_X_POINT can't be reached by the Z probe." + #else + #error "Z_SAFE_HOMING_X_POINT can't be reached by the nozzle." + #endif + #elif !WITHIN(Z_SAFE_HOMING_Y_POINT, MIN_PROBE_Y, MAX_PROBE_Y) + #if HAS_BED_PROBE + #error "Z_SAFE_HOMING_Y_POINT can't be reached by the Z probe." + #else + #error "Z_SAFE_HOMING_Y_POINT can't be reached by the nozzle." + #endif + #endif +#endif // Z_SAFE_HOMING + +/** + * Make sure DISABLE_[XYZ] compatible with selected homing options + */ +#if ENABLED(DISABLE_X) || ENABLED(DISABLE_Y) || ENABLED(DISABLE_Z) + #if ENABLED(HOME_AFTER_DEACTIVATE) || ENABLED(Z_SAFE_HOMING) + #error "DISABLE_[XYZ] not compatible with HOME_AFTER_DEACTIVATE or Z_SAFE_HOMING." + #endif +#endif // DISABLE_[XYZ] + +/** + * Filament Width Sensor + */ +#if ENABLED(FILAMENT_WIDTH_SENSOR) && !HAS_FILAMENT_WIDTH_SENSOR + #error "FILAMENT_WIDTH_SENSOR requires a FILWIDTH_PIN to be defined." +#endif + +/** + * ULTIPANEL encoder + */ +#if ENABLED(ULTIPANEL) && DISABLED(NEWPANEL) && DISABLED(SR_LCD_2W_NL) && !defined(SHIFT_CLK) + #error "ULTIPANEL requires some kind of encoder." +#endif + +#if ENCODER_PULSES_PER_STEP < 0 + #error "ENCODER_PULSES_PER_STEP should not be negative, use REVERSE_MENU_DIRECTION instead." +#endif + +/** + * SAV_3DGLCD display options + */ +#if ENABLED(U8GLIB_SSD1306) && ENABLED(U8GLIB_SH1106) + #error "Only enable one SAV_3DGLCD display type: U8GLIB_SSD1306 or U8GLIB_SH1106." +#endif + +/** + * Allen Key + * Deploying the Allen Key probe uses big moves in z direction. Too dangerous for an unhomed z-axis. + */ +#if ENABLED(Z_PROBE_ALLEN_KEY) && (Z_HOME_DIR < 0) && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) + #error "You can't home to a z min endstop with a Z_PROBE_ALLEN_KEY" +#endif + +/** + * Dual X Carriage requirements + */ +#if ENABLED(DUAL_X_CARRIAGE) + #if EXTRUDERS == 1 + #error "DUAL_X_CARRIAGE requires 2 (or more) extruders." + #elif CORE_IS_XY || CORE_IS_XZ + #error "DUAL_X_CARRIAGE cannot be used with COREXY, COREYX, COREXZ, or COREZX." + #elif !HAS_X2_ENABLE || !HAS_X2_STEP || !HAS_X2_DIR + #error "DUAL_X_CARRIAGE requires X2 stepper pins to be defined." + #elif !HAS_X_MAX + #error "DUAL_X_CARRIAGE requires USE_XMAX_PLUG and an X Max Endstop." + #elif !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) + #error "DUAL_X_CARRIAGE requires X2_HOME_POS, X2_MIN_POS, and X2_MAX_POS." + #elif X_HOME_DIR != -1 || X2_HOME_DIR != 1 + #error "DUAL_X_CARRIAGE requires X_HOME_DIR -1 and X2_HOME_DIR 1." + #endif +#endif // DUAL_X_CARRIAGE + +/** + * Make sure auto fan pins don't conflict with the fan pin + */ +#if HAS_AUTO_FAN + #if HAS_FAN0 + #if E0_AUTO_FAN_PIN == FAN_PIN + #error "You cannot set E0_AUTO_FAN_PIN equal to FAN_PIN." + #elif E1_AUTO_FAN_PIN == FAN_PIN + #error "You cannot set E1_AUTO_FAN_PIN equal to FAN_PIN." + #elif E2_AUTO_FAN_PIN == FAN_PIN + #error "You cannot set E2_AUTO_FAN_PIN equal to FAN_PIN." + #elif E3_AUTO_FAN_PIN == FAN_PIN + #error "You cannot set E3_AUTO_FAN_PIN equal to FAN_PIN." + #endif + #endif +#endif + +#if HAS_FAN0 && CONTROLLER_FAN_PIN == FAN_PIN + #error "You cannot set CONTROLLER_FAN_PIN equal to FAN_PIN." +#endif + +#if ENABLED(USE_CONTROLLER_FAN) + #if !HAS_CONTROLLER_FAN + #error "USE_CONTROLLER_FAN requires a CONTROLLER_FAN_PIN. Define in Configuration_adv.h." + #elif E0_AUTO_FAN_PIN == CONTROLLER_FAN_PIN + #error "You cannot set E0_AUTO_FAN_PIN equal to CONTROLLER_FAN_PIN." + #elif E1_AUTO_FAN_PIN == CONTROLLER_FAN_PIN + #error "You cannot set E1_AUTO_FAN_PIN equal to CONTROLLER_FAN_PIN." + #elif E2_AUTO_FAN_PIN == CONTROLLER_FAN_PIN + #error "You cannot set E2_AUTO_FAN_PIN equal to CONTROLLER_FAN_PIN." + #elif E3_AUTO_FAN_PIN == CONTROLLER_FAN_PIN + #error "You cannot set E3_AUTO_FAN_PIN equal to CONTROLLER_FAN_PIN." + #endif +#endif + +/** + * Test Heater, Temp Sensor, and Extruder Pins; Sensor Type must also be set. + */ +#if !HAS_HEATER_0 + #error "HEATER_0_PIN not defined for this board." +#elif !PIN_EXISTS(TEMP_0) && !(defined(MAX6675_SS) && MAX6675_SS >= 0) + #error "TEMP_0_PIN not defined for this board." +#elif !PIN_EXISTS(E0_STEP) || !PIN_EXISTS(E0_DIR) || !PIN_EXISTS(E0_ENABLE) + #error "E0_STEP_PIN, E0_DIR_PIN, or E0_ENABLE_PIN not defined for this board." +#elif TEMP_SENSOR_0 == 0 + #error "TEMP_SENSOR_0 is required." +#endif + +#if HOTENDS > 1 || ENABLED(HEATERS_PARALLEL) + #if !HAS_HEATER_1 + #error "HEATER_1_PIN not defined for this board." + #endif +#endif + +#if HOTENDS > 1 + #if TEMP_SENSOR_1 == 0 + #error "TEMP_SENSOR_1 is required with 2 or more HOTENDS." + #elif !PIN_EXISTS(TEMP_1) + #error "TEMP_1_PIN not defined for this board." + #endif + #if HOTENDS > 2 + #if TEMP_SENSOR_2 == 0 + #error "TEMP_SENSOR_2 is required with 3 or more HOTENDS." + #elif !HAS_HEATER_2 + #error "HEATER_2_PIN not defined for this board." + #elif !PIN_EXISTS(TEMP_2) + #error "TEMP_2_PIN not defined for this board." + #endif + #if HOTENDS > 3 + #if TEMP_SENSOR_3 == 0 + #error "TEMP_SENSOR_3 is required with 4 or more HOTENDS." + #elif !HAS_HEATER_3 + #error "HEATER_3_PIN not defined for this board." + #elif !PIN_EXISTS(TEMP_3) + #error "TEMP_3_PIN not defined for this board." + #endif + #if HOTENDS > 4 + #if TEMP_SENSOR_4 == 0 + #error "TEMP_SENSOR_4 is required with 5 HOTENDS." + #elif !HAS_HEATER_4 + #error "HEATER_4_PIN not defined for this board." + #elif !PIN_EXISTS(TEMP_4) + #error "TEMP_4_PIN not defined for this board." + #endif + #elif TEMP_SENSOR_4 != 0 + #error "TEMP_SENSOR_4 shouldn't be set with only 4 HOTENDS." + #endif + #elif TEMP_SENSOR_3 != 0 + #error "TEMP_SENSOR_3 shouldn't be set with only 3 HOTENDS." + #elif TEMP_SENSOR_4 != 0 + #error "TEMP_SENSOR_4 shouldn't be set with only 3 HOTENDS." + #endif + #elif TEMP_SENSOR_2 != 0 + #error "TEMP_SENSOR_2 shouldn't be set with only 2 HOTENDS." + #elif TEMP_SENSOR_3 != 0 + #error "TEMP_SENSOR_3 shouldn't be set with only 2 HOTENDS." + #elif TEMP_SENSOR_4 != 0 + #error "TEMP_SENSOR_4 shouldn't be set with only 2 HOTENDS." + #endif +#elif TEMP_SENSOR_1 != 0 && DISABLED(TEMP_SENSOR_1_AS_REDUNDANT) + #error "TEMP_SENSOR_1 shouldn't be set with only 1 HOTEND." +#elif TEMP_SENSOR_2 != 0 + #error "TEMP_SENSOR_2 shouldn't be set with only 1 HOTEND." +#elif TEMP_SENSOR_3 != 0 + #error "TEMP_SENSOR_3 shouldn't be set with only 1 HOTEND." +#elif TEMP_SENSOR_4 != 0 + #error "TEMP_SENSOR_4 shouldn't be set with only 1 HOTEND." +#endif + +#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) && TEMP_SENSOR_1 == 0 + #error "TEMP_SENSOR_1 is required with TEMP_SENSOR_1_AS_REDUNDANT." +#endif + +/** + * Temperature status LEDs + */ +#if ENABLED(TEMP_STAT_LEDS) && !PIN_EXISTS(STAT_LED_RED) && !PIN_EXISTS(STAT_LED_BLUE) + #error "TEMP_STAT_LEDS requires STAT_LED_RED_PIN or STAT_LED_BLUE_PIN, preferably both." +#endif + +/** + * Basic 2-nozzle duplication mode + */ +#if ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) + #if HOTENDS != 2 + #error "DUAL_NOZZLE_DUPLICATION_MODE requires exactly 2 hotends." + #elif ENABLED(DUAL_X_CARRIAGE) + #error "DUAL_NOZZLE_DUPLICATION_MODE is incompatible with DUAL_X_CARRIAGE." + #elif ENABLED(SINGLENOZZLE) + #error "DUAL_NOZZLE_DUPLICATION_MODE is incompatible with SINGLENOZZLE." + #elif ENABLED(MIXING_EXTRUDER) + #error "DUAL_NOZZLE_DUPLICATION_MODE is incompatible with MIXING_EXTRUDER." + #elif ENABLED(SWITCHING_EXTRUDER) + #error "DUAL_NOZZLE_DUPLICATION_MODE is incompatible with SWITCHING_EXTRUDER." + #endif +#endif + +/** + * Test Extruder Stepper Pins + */ +#if E_STEPPERS > 4 + #if !PIN_EXISTS(E4_STEP) || !PIN_EXISTS(E4_DIR) || !PIN_EXISTS(E4_ENABLE) + #error "E4_STEP_PIN, E4_DIR_PIN, or E4_ENABLE_PIN not defined for this board." + #endif +#elif E_STEPPERS > 3 + #if !PIN_EXISTS(E3_STEP) || !PIN_EXISTS(E3_DIR) || !PIN_EXISTS(E3_ENABLE) + #error "E3_STEP_PIN, E3_DIR_PIN, or E3_ENABLE_PIN not defined for this board." + #endif +#elif E_STEPPERS > 2 + #if !PIN_EXISTS(E2_STEP) || !PIN_EXISTS(E2_DIR) || !PIN_EXISTS(E2_ENABLE) + #error "E2_STEP_PIN, E2_DIR_PIN, or E2_ENABLE_PIN not defined for this board." + #endif +#elif E_STEPPERS > 1 + #if !PIN_EXISTS(E1_STEP) || !PIN_EXISTS(E1_DIR) || !PIN_EXISTS(E1_ENABLE) + #error "E1_STEP_PIN, E1_DIR_PIN, or E1_ENABLE_PIN not defined for this board." + #endif +#endif + +/** + * Endstop Tests + */ + +#define _PLUG_UNUSED_TEST(AXIS,PLUG) (DISABLED(USE_##PLUG##MIN_PLUG) && DISABLED(USE_##PLUG##MAX_PLUG) && !(ENABLED(AXIS##_DUAL_ENDSTOPS) && WITHIN(AXIS##2_USE_ENDSTOP, _##PLUG##MAX_, _##PLUG##MIN_))) +#define _AXIS_PLUG_UNUSED_TEST(AXIS) (_PLUG_UNUSED_TEST(AXIS,X) && _PLUG_UNUSED_TEST(AXIS,Y) && _PLUG_UNUSED_TEST(AXIS,Z)) + +// At least 3 endstop plugs must be used +#if _AXIS_PLUG_UNUSED_TEST(X) + #error "You must enable USE_XMIN_PLUG or USE_XMAX_PLUG." +#endif +#if _AXIS_PLUG_UNUSED_TEST(Y) + #error "You must enable USE_YMIN_PLUG or USE_YMAX_PLUG." +#endif +#if _AXIS_PLUG_UNUSED_TEST(Z) + #error "You must enable USE_ZMIN_PLUG or USE_ZMAX_PLUG." +#endif + +// Delta and Cartesian use 3 homing endstops +#if !IS_SCARA + #if X_HOME_DIR < 0 && DISABLED(USE_XMIN_PLUG) + #error "Enable USE_XMIN_PLUG when homing X to MIN." + #elif X_HOME_DIR > 0 && DISABLED(USE_XMAX_PLUG) + #error "Enable USE_XMAX_PLUG when homing X to MAX." + #elif Y_HOME_DIR < 0 && DISABLED(USE_YMIN_PLUG) + #error "Enable USE_YMIN_PLUG when homing Y to MIN." + #elif Y_HOME_DIR > 0 && DISABLED(USE_YMAX_PLUG) + #error "Enable USE_YMAX_PLUG when homing Y to MAX." + #endif +#endif +#if Z_HOME_DIR < 0 && DISABLED(USE_ZMIN_PLUG) + #error "Enable USE_ZMIN_PLUG when homing Z to MIN." +#elif Z_HOME_DIR > 0 && DISABLED(USE_ZMAX_PLUG) + #error "Enable USE_ZMAX_PLUG when homing Z to MAX." +#endif + +// Dual endstops requirements +#if ENABLED(X_DUAL_ENDSTOPS) + #if !X2_USE_ENDSTOP + #error "You must set X2_USE_ENDSTOP with X_DUAL_ENDSTOPS." + #elif X2_USE_ENDSTOP == _X_MIN_ && DISABLED(USE_XMIN_PLUG) + #error "USE_XMIN_PLUG is required when X2_USE_ENDSTOP is _X_MIN_." + #elif X2_USE_ENDSTOP == _X_MAX_ && DISABLED(USE_XMAX_PLUG) + #error "USE_XMAX_PLUG is required when X2_USE_ENDSTOP is _X_MAX_." + #elif X2_USE_ENDSTOP == _Y_MIN_ && DISABLED(USE_YMIN_PLUG) + #error "USE_YMIN_PLUG is required when X2_USE_ENDSTOP is _Y_MIN_." + #elif X2_USE_ENDSTOP == _Y_MAX_ && DISABLED(USE_YMAX_PLUG) + #error "USE_YMAX_PLUG is required when X2_USE_ENDSTOP is _Y_MAX_." + #elif X2_USE_ENDSTOP == _Z_MIN_ && DISABLED(USE_ZMIN_PLUG) + #error "USE_ZMIN_PLUG is required when X2_USE_ENDSTOP is _Z_MIN_." + #elif X2_USE_ENDSTOP == _Z_MAX_ && DISABLED(USE_ZMAX_PLUG) + #error "USE_ZMAX_PLUG is required when X2_USE_ENDSTOP is _Z_MAX_." + #elif !HAS_X2_MIN && !HAS_X2_MAX + #error "X2_USE_ENDSTOP has been assigned to a nonexistent endstop!" + #elif ENABLED(DELTA) + #error "X_DUAL_ENDSTOPS is not compatible with DELTA." + #endif +#endif +#if ENABLED(Y_DUAL_ENDSTOPS) + #if !Y2_USE_ENDSTOP + #error "You must set Y2_USE_ENDSTOP with Y_DUAL_ENDSTOPS." + #elif Y2_USE_ENDSTOP == _X_MIN_ && DISABLED(USE_XMIN_PLUG) + #error "USE_XMIN_PLUG is required when Y2_USE_ENDSTOP is _X_MIN_." + #elif Y2_USE_ENDSTOP == _X_MAX_ && DISABLED(USE_XMAX_PLUG) + #error "USE_XMAX_PLUG is required when Y2_USE_ENDSTOP is _X_MAX_." + #elif Y2_USE_ENDSTOP == _Y_MIN_ && DISABLED(USE_YMIN_PLUG) + #error "USE_YMIN_PLUG is required when Y2_USE_ENDSTOP is _Y_MIN_." + #elif Y2_USE_ENDSTOP == _Y_MAX_ && DISABLED(USE_YMAX_PLUG) + #error "USE_YMAX_PLUG is required when Y2_USE_ENDSTOP is _Y_MAX_." + #elif Y2_USE_ENDSTOP == _Z_MIN_ && DISABLED(USE_ZMIN_PLUG) + #error "USE_ZMIN_PLUG is required when Y2_USE_ENDSTOP is _Z_MIN_." + #elif Y2_USE_ENDSTOP == _Z_MAX_ && DISABLED(USE_ZMAX_PLUG) + #error "USE_ZMAX_PLUG is required when Y2_USE_ENDSTOP is _Z_MAX_." + #elif !HAS_Y2_MIN && !HAS_Y2_MAX + #error "Y2_USE_ENDSTOP has been assigned to a nonexistent endstop!" + #elif ENABLED(DELTA) + #error "Y_DUAL_ENDSTOPS is not compatible with DELTA." + #endif +#endif +#if ENABLED(Z_DUAL_ENDSTOPS) + #if !Z2_USE_ENDSTOP + #error "You must set Z2_USE_ENDSTOP with Z_DUAL_ENDSTOPS." + #elif Z2_USE_ENDSTOP == _X_MIN_ && DISABLED(USE_XMIN_PLUG) + #error "USE_XMIN_PLUG is required when Z2_USE_ENDSTOP is _X_MIN_." + #elif Z2_USE_ENDSTOP == _X_MAX_ && DISABLED(USE_XMAX_PLUG) + #error "USE_XMAX_PLUG is required when Z2_USE_ENDSTOP is _X_MAX_." + #elif Z2_USE_ENDSTOP == _Y_MIN_ && DISABLED(USE_YMIN_PLUG) + #error "USE_YMIN_PLUG is required when Z2_USE_ENDSTOP is _Y_MIN_." + #elif Z2_USE_ENDSTOP == _Y_MAX_ && DISABLED(USE_YMAX_PLUG) + #error "USE_YMAX_PLUG is required when Z2_USE_ENDSTOP is _Y_MAX_." + #elif Z2_USE_ENDSTOP == _Z_MIN_ && DISABLED(USE_ZMIN_PLUG) + #error "USE_ZMIN_PLUG is required when Z2_USE_ENDSTOP is _Z_MIN_." + #elif Z2_USE_ENDSTOP == _Z_MAX_ && DISABLED(USE_ZMAX_PLUG) + #error "USE_ZMAX_PLUG is required when Z2_USE_ENDSTOP is _Z_MAX_." + #elif !HAS_Z2_MIN && !HAS_Z2_MAX + #error "Z2_USE_ENDSTOP has been assigned to a nonexistent endstop!" + #elif ENABLED(DELTA) + #error "Z_DUAL_ENDSTOPS is not compatible with DELTA." + #endif +#endif + +/** + * emergency-command parser + */ +#if ENABLED(EMERGENCY_PARSER) && defined(USBCON) + #error "EMERGENCY_PARSER does not work on boards with AT90USB processors (USBCON)." +#endif + +/** + * I2C bus + */ +#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0 + #if I2C_SLAVE_ADDRESS < 8 + #error "I2C_SLAVE_ADDRESS can't be less than 8. (Addresses 0 - 7 are reserved.)" + #elif I2C_SLAVE_ADDRESS > 127 + #error "I2C_SLAVE_ADDRESS can't be over 127. (Only 7 bits allowed.)" + #endif +#endif + +/** + * G38 Probe Target + */ +#if ENABLED(G38_PROBE_TARGET) + #if !HAS_BED_PROBE + #error "G38_PROBE_TARGET requires a bed probe." + #elif !IS_CARTESIAN + #error "G38_PROBE_TARGET requires a Cartesian machine." + #endif +#endif + +/** + * RGB_LED Requirements + */ +#define _RGB_TEST (PIN_EXISTS(RGB_LED_R) && PIN_EXISTS(RGB_LED_G) && PIN_EXISTS(RGB_LED_B)) +#if ENABLED(RGB_LED) + #if !_RGB_TEST + #error "RGB_LED requires RGB_LED_R_PIN, RGB_LED_G_PIN, and RGB_LED_B_PIN." + #elif ENABLED(RGBW_LED) + #error "Please enable only one of RGB_LED and RGBW_LED." + #endif +#elif ENABLED(RGBW_LED) + #if !(_RGB_TEST && PIN_EXISTS(RGB_LED_W)) + #error "RGBW_LED requires RGB_LED_R_PIN, RGB_LED_G_PIN, RGB_LED_B_PIN, and RGB_LED_W_PIN." + #endif +#elif ENABLED(NEOPIXEL_LED) + #if !(PIN_EXISTS(NEOPIXEL) && NEOPIXEL_PIXELS > 0) + #error "NEOPIXEL_LED requires NEOPIXEL_PIN and NEOPIXEL_PIXELS." + #endif +#elif ENABLED(PRINTER_EVENT_LEDS) && DISABLED(BLINKM) && DISABLED(PCA9632) && DISABLED(NEOPIXEL_LED) + #error "PRINTER_EVENT_LEDS requires BLINKM, PCA9632, RGB_LED, RGBW_LED or NEOPIXEL_LED." +#endif + +/** + * Auto Fan check for PWM pins + */ +#if HAS_AUTO_FAN && EXTRUDER_AUTO_FAN_SPEED != 255 + #define AF_ERR_SUFF "_AUTO_FAN_PIN is not a PWM pin. Set EXTRUDER_AUTO_FAN_SPEED to 255." + #if HAS_AUTO_FAN_0 + static_assert(GET_TIMER(E0_AUTO_FAN_PIN), "E0" AF_ERR_SUFF); + #elif HAS_AUTO_FAN_1 + static_assert(GET_TIMER(E1_AUTO_FAN_PIN), "E1" AF_ERR_SUFF); + #elif HAS_AUTO_FAN_2 + static_assert(GET_TIMER(E2_AUTO_FAN_PIN), "E2" AF_ERR_SUFF); + #elif HAS_AUTO_FAN_3 + static_assert(GET_TIMER(E3_AUTO_FAN_PIN), "E3" AF_ERR_SUFF); + #endif +#endif + +/** + * Make sure only one display is enabled + * + * Note: BQ_LCD_SMART_CONTROLLER => REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER + * REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER => REPRAP_DISCOUNT_SMART_CONTROLLER + * SAV_3DGLCD => U8GLIB_SH1106 => ULTIMAKERCONTROLLER + * MKS_12864OLED => U8GLIB_SH1106 => ULTIMAKERCONTROLLER + * miniVIKI => ULTIMAKERCONTROLLER + * VIKI2 => ULTIMAKERCONTROLLER + * ELB_FULL_GRAPHIC_CONTROLLER => ULTIMAKERCONTROLLER + * PANEL_ONE => ULTIMAKERCONTROLLER + */ +static_assert(1 >= 0 + #if ENABLED(ULTIMAKERCONTROLLER) \ + && DISABLED(SAV_3DGLCD) \ + && DISABLED(miniVIKI) \ + && DISABLED(VIKI2) \ + && DISABLED(ELB_FULL_GRAPHIC_CONTROLLER) \ + && DISABLED(PANEL_ONE) \ + && DISABLED(MKS_12864OLED) + + 1 + #endif + #if ENABLED(REPRAP_DISCOUNT_SMART_CONTROLLER) \ + && DISABLED(REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER) \ + && DISABLED(LCD_FOR_MELZI) \ + && DISABLED(MAKEBOARD_MINI_2_LINE_DISPLAY_1602) \ + && DISABLED(MKS_12864OLED) + + 1 + #endif + #if ENABLED(REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER) \ + && DISABLED(BQ_LCD_SMART_CONTROLLER) + + 1 + #endif + #if ENABLED(LCD_FOR_MELZI) + + 1 + #endif + #if ENABLED(MKS_12864OLED) + + 1 + #endif + #if ENABLED(MAKEBOARD_MINI_2_LINE_DISPLAY_1602) + + 1 + #endif + #if ENABLED(CARTESIO_UI) + + 1 + #endif + #if ENABLED(PANEL_ONE) + + 1 + #endif + #if ENABLED(MAKRPANEL) + + 1 + #endif + #if ENABLED(REPRAPWORLD_GRAPHICAL_LCD) + + 1 + #endif + #if ENABLED(VIKI2) + + 1 + #endif + #if ENABLED(miniVIKI) + + 1 + #endif + #if ENABLED(ELB_FULL_GRAPHIC_CONTROLLER) + + 1 + #endif + #if ENABLED(G3D_PANEL) + + 1 + #endif + #if ENABLED(MINIPANEL) && DISABLED(MKS_MINI_12864) + + 1 + #endif + #if ENABLED(MKS_MINI_12864) + + 1 + #endif + #if ENABLED(REPRAPWORLD_KEYPAD) \ + && DISABLED(CARTESIO_UI) \ + && DISABLED(ANET_KEYPAD_LCD) + + 1 + #endif + #if ENABLED(RIGIDBOT_PANEL) + + 1 + #endif + #if ENABLED(RA_CONTROL_PANEL) + + 1 + #endif + #if ENABLED(LCD_I2C_SAINSMART_YWROBOT) + + 1 + #endif + #if ENABLED(LCM1602) + + 1 + #endif + #if ENABLED(LCD_I2C_PANELOLU2) + + 1 + #endif + #if ENABLED(LCD_I2C_VIKI) + + 1 + #endif + #if ENABLED(U8GLIB_SSD1306) && DISABLED(OLED_PANEL_TINYBOY2) + + 1 + #endif + #if ENABLED(SAV_3DLCD) + + 1 + #endif + #if ENABLED(BQ_LCD_SMART_CONTROLLER) + + 1 + #endif + #if ENABLED(SAV_3DGLCD) + + 1 + #endif + #if ENABLED(OLED_PANEL_TINYBOY2) + + 1 + #endif + #if ENABLED(ANET_KEYPAD_LCD) + + 1 + #endif + , "Please select no more than one LCD controller option." +); + +/** + * Make sure HAVE_TMCDRIVER is warranted + */ +#if ENABLED(HAVE_TMCDRIVER) && !( \ + ENABLED( X_IS_TMC ) \ + || ENABLED( X2_IS_TMC ) \ + || ENABLED( Y_IS_TMC ) \ + || ENABLED( Y2_IS_TMC ) \ + || ENABLED( Z_IS_TMC ) \ + || ENABLED( Z2_IS_TMC ) \ + || ENABLED( E0_IS_TMC ) \ + || ENABLED( E1_IS_TMC ) \ + || ENABLED( E2_IS_TMC ) \ + || ENABLED( E3_IS_TMC ) \ + || ENABLED( E4_IS_TMC ) \ + ) + #error "HAVE_TMCDRIVER requires at least one TMC stepper to be set." +#endif + +/** + * Make sure HAVE_TMC2130 is warranted + */ +#if ENABLED(HAVE_TMC2130) + #if !( ENABLED( X_IS_TMC2130 ) \ + || ENABLED( X2_IS_TMC2130 ) \ + || ENABLED( Y_IS_TMC2130 ) \ + || ENABLED( Y2_IS_TMC2130 ) \ + || ENABLED( Z_IS_TMC2130 ) \ + || ENABLED( Z2_IS_TMC2130 ) \ + || ENABLED( E0_IS_TMC2130 ) \ + || ENABLED( E1_IS_TMC2130 ) \ + || ENABLED( E2_IS_TMC2130 ) \ + || ENABLED( E3_IS_TMC2130 ) \ + || ENABLED( E4_IS_TMC2130 ) \ + ) + #error "HAVE_TMC2130 requires at least one TMC2130 stepper to be set." + #elif ENABLED(HYBRID_THRESHOLD) && DISABLED(STEALTHCHOP) + #error "Enable STEALTHCHOP to use HYBRID_THRESHOLD." + #endif +#endif + +/** + * Make sure HAVE_L6470DRIVER is warranted + */ +#if ENABLED(HAVE_L6470DRIVER) && !( \ + ENABLED( X_IS_L6470 ) \ + || ENABLED( X2_IS_L6470 ) \ + || ENABLED( Y_IS_L6470 ) \ + || ENABLED( Y2_IS_L6470 ) \ + || ENABLED( Z_IS_L6470 ) \ + || ENABLED( Z2_IS_L6470 ) \ + || ENABLED( E0_IS_L6470 ) \ + || ENABLED( E1_IS_L6470 ) \ + || ENABLED( E2_IS_L6470 ) \ + || ENABLED( E3_IS_L6470 ) \ + || ENABLED( E4_IS_L6470 ) \ + ) + #error "HAVE_L6470DRIVER requires at least one L6470 stepper to be set." +#endif + +/** + * Digipot requirement + */ +#if ENABLED(DIGIPOT_MCP4018) + #if !defined(DIGIPOTS_I2C_SDA_X) || !defined(DIGIPOTS_I2C_SDA_Y) || !defined(DIGIPOTS_I2C_SDA_Z) \ + || !defined(DIGIPOTS_I2C_SDA_E0) || !defined(DIGIPOTS_I2C_SDA_E1) + #error "DIGIPOT_MCP4018 requires DIGIPOTS_I2C_SDA_* pins to be defined." + #endif +#endif + +/** + * Require 4 or more elements in per-axis initializers + */ +constexpr float sanity_arr_1[] = DEFAULT_AXIS_STEPS_PER_UNIT, + sanity_arr_2[] = DEFAULT_MAX_FEEDRATE, + sanity_arr_3[] = DEFAULT_MAX_ACCELERATION; +static_assert(COUNT(sanity_arr_1) >= XYZE, "DEFAULT_AXIS_STEPS_PER_UNIT requires 4 (or more) elements."); +static_assert(COUNT(sanity_arr_2) >= XYZE, "DEFAULT_MAX_FEEDRATE requires 4 (or more) elements."); +static_assert(COUNT(sanity_arr_3) >= XYZE, "DEFAULT_MAX_ACCELERATION requires 4 (or more) elements."); +static_assert(COUNT(sanity_arr_1) <= XYZE_N, "DEFAULT_AXIS_STEPS_PER_UNIT has too many elements."); +static_assert(COUNT(sanity_arr_2) <= XYZE_N, "DEFAULT_MAX_FEEDRATE has too many elements."); +static_assert(COUNT(sanity_arr_3) <= XYZE_N, "DEFAULT_MAX_ACCELERATION has too many elements."); + +/** + * Sanity checks for Spindle / Laser + */ +#if ENABLED(SPINDLE_LASER_ENABLE) + #if !PIN_EXISTS(SPINDLE_LASER_ENABLE) + #error "SPINDLE_LASER_ENABLE requires SPINDLE_LASER_ENABLE_PIN." + #elif SPINDLE_DIR_CHANGE && !PIN_EXISTS(SPINDLE_DIR) + #error "SPINDLE_DIR_PIN not defined." + #elif ENABLED(SPINDLE_LASER_PWM) && PIN_EXISTS(SPINDLE_LASER_PWM) + #if !(WITHIN(SPINDLE_LASER_PWM_PIN, 2, 13) || WITHIN(SPINDLE_LASER_PWM_PIN, 44, 46)) + #error "SPINDLE_LASER_PWM_PIN not assigned to a PWM pin." + #elif SPINDLE_LASER_POWERUP_DELAY < 1 + #error "SPINDLE_LASER_POWERUP_DELAY must be greater than 0." + #elif SPINDLE_LASER_POWERDOWN_DELAY < 1 + #error "SPINDLE_LASER_POWERDOWN_DELAY must be greater than 0." + #elif !defined(SPINDLE_LASER_PWM_INVERT) + #error "SPINDLE_LASER_PWM_INVERT missing." + #elif !defined(SPEED_POWER_SLOPE) || !defined(SPEED_POWER_INTERCEPT) || !defined(SPEED_POWER_MIN) || !defined(SPEED_POWER_MAX) + #error "SPINDLE_LASER_PWM equation constant(s) missing." + #elif SPINDLE_LASER_PWM_PIN == 4 || WITHIN(SPINDLE_LASER_PWM_PIN, 11, 13) + #error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by a system interrupt." + #elif PIN_EXISTS(X_MAX) && X_MAX_PIN == SPINDLE_LASER_PWM_PIN + #error "SPINDLE_LASER_PWM pin is in use by X_MAX endstop." + #elif PIN_EXISTS(X_MIN) && X_MIN_PIN == SPINDLE_LASER_PWM_PIN + #error "SPINDLE_LASER_PWM pin is in use by X_MIN endstop." + #elif PIN_EXISTS(Z_STEP) && Z_STEP_PIN == SPINDLE_LASER_PWM_PIN + #error "SPINDLE_LASER_PWM pin in use by Z_STEP." + #elif NUM_SERVOS > 0 && (WITHIN(SPINDLE_LASER_PWM_PIN, 2, 3) || SPINDLE_LASER_PWM_PIN == 5) + #error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by the servo system." + #elif PIN_EXISTS(CASE_LIGHT) && SPINDLE_LASER_PWM_PIN == CASE_LIGHT_PIN + #error "SPINDLE_LASER_PWM_PIN is used by CASE_LIGHT_PIN." + #elif PIN_EXISTS(E0_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E0_AUTO_FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by E0_AUTO_FAN_PIN." + #elif PIN_EXISTS(E1_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E1_AUTO_FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by E1_AUTO_FAN_PIN." + #elif PIN_EXISTS(E2_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E2_AUTO_FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by E2_AUTO_FAN_PIN." + #elif PIN_EXISTS(E3_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E3_AUTO_FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by E3_AUTO_FAN_PIN." + #elif PIN_EXISTS(E4_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E4_AUTO_FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by E4_AUTO_FAN_PIN." + #elif PIN_EXISTS(FAN) && SPINDLE_LASER_PWM_PIN == FAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used FAN_PIN." + #elif PIN_EXISTS(FAN1) && SPINDLE_LASER_PWM_PIN == FAN1_PIN + #error "SPINDLE_LASER_PWM_PIN is used FAN1_PIN." + #elif PIN_EXISTS(FAN2) && SPINDLE_LASER_PWM_PIN == FAN2_PIN + #error "SPINDLE_LASER_PWM_PIN is used FAN2_PIN." + #elif PIN_EXISTS(CONTROLLERFAN) && SPINDLE_LASER_PWM_PIN == CONTROLLERFAN_PIN + #error "SPINDLE_LASER_PWM_PIN is used by CONTROLLERFAN_PIN." + #elif PIN_EXISTS(MOTOR_CURRENT_PWM_XY) && SPINDLE_LASER_PWM_PIN == MOTOR_CURRENT_PWM_XY_PIN + #error "SPINDLE_LASER_PWM_PIN is used by MOTOR_CURRENT_PWM_XY." + #elif PIN_EXISTS(MOTOR_CURRENT_PWM_Z) && SPINDLE_LASER_PWM_PIN == MOTOR_CURRENT_PWM_Z_PIN + #error "SPINDLE_LASER_PWM_PIN is used by MOTOR_CURRENT_PWM_Z." + #elif PIN_EXISTS(MOTOR_CURRENT_PWM_E) && SPINDLE_LASER_PWM_PIN == MOTOR_CURRENT_PWM_E_PIN + #error "SPINDLE_LASER_PWM_PIN is used by MOTOR_CURRENT_PWM_E." + #elif PIN_EXISTS(CASE_LIGHT) && SPINDLE_LASER_PWM_PIN == CASE_LIGHT_PIN + #error "SPINDLE_LASER_PWM_PIN is used by CASE_LIGHT." + #endif + #endif +#endif // SPINDLE_LASER_ENABLE + +#if ENABLED(CNC_COORDINATE_SYSTEMS) && ENABLED(NO_WORKSPACE_OFFSETS) + #error "CNC_COORDINATE_SYSTEMS is incompatible with NO_WORKSPACE_OFFSETS." +#endif diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.cpp new file mode 100644 index 0000000..2afe9a8 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.cpp @@ -0,0 +1,729 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Arduino Sd2Card Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + */ +#include "Marlin.h" + +#if ENABLED(SDSUPPORT) +#include "Sd2Card.h" + +#if ENABLED(USE_WATCHDOG) + #include "watchdog.h" +#endif + +//------------------------------------------------------------------------------ +#if DISABLED(SOFTWARE_SPI) + // functions for hardware SPI + //------------------------------------------------------------------------------ + // make sure SPCR rate is in expected bits + #if (SPR0 != 0 || SPR1 != 1) + #error "unexpected SPCR bits" + #endif + /** + * Initialize hardware SPI + * Set SCK rate to F_CPU/pow(2, 1 + spiRate) for spiRate [0,6] + */ + static void spiInit(uint8_t spiRate) { + // See avr processor documentation + SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1); + SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X); + } + //------------------------------------------------------------------------------ + /** SPI receive a byte */ + static uint8_t spiRec() { + SPDR = 0xFF; + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + return SPDR; + } + //------------------------------------------------------------------------------ + /** SPI read data - only one call so force inline */ + static inline __attribute__((always_inline)) + void spiRead(uint8_t* buf, uint16_t nbyte) { + if (nbyte-- == 0) return; + SPDR = 0xFF; + for (uint16_t i = 0; i < nbyte; i++) { + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + buf[i] = SPDR; + SPDR = 0xFF; + } + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + buf[nbyte] = SPDR; + } + //------------------------------------------------------------------------------ + /** SPI send a byte */ + static void spiSend(uint8_t b) { + SPDR = b; + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + } + //------------------------------------------------------------------------------ + /** SPI send block - only one call so force inline */ + static inline __attribute__((always_inline)) + void spiSendBlock(uint8_t token, const uint8_t* buf) { + SPDR = token; + for (uint16_t i = 0; i < 512; i += 2) { + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + SPDR = buf[i]; + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + SPDR = buf[i + 1]; + } + while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } + } + //------------------------------------------------------------------------------ +#else // SOFTWARE_SPI + //------------------------------------------------------------------------------ + /** nop to tune soft SPI timing */ + #define nop asm volatile ("nop\n\t") + //------------------------------------------------------------------------------ + /** Soft SPI receive byte */ + static uint8_t spiRec() { + uint8_t data = 0; + // no interrupts during byte receive - about 8 us + cli(); + // output pin high - like sending 0xFF + WRITE(SPI_MOSI_PIN, HIGH); + + for (uint8_t i = 0; i < 8; i++) { + WRITE(SPI_SCK_PIN, HIGH); + + // adjust so SCK is nice + nop; + nop; + + data <<= 1; + + if (READ(SPI_MISO_PIN)) data |= 1; + + WRITE(SPI_SCK_PIN, LOW); + } + // enable interrupts + sei(); + return data; + } + //------------------------------------------------------------------------------ + /** Soft SPI read data */ + static void spiRead(uint8_t* buf, uint16_t nbyte) { + for (uint16_t i = 0; i < nbyte; i++) + buf[i] = spiRec(); + } + //------------------------------------------------------------------------------ + /** Soft SPI send byte */ + static void spiSend(uint8_t data) { + // no interrupts during byte send - about 8 us + cli(); + for (uint8_t i = 0; i < 8; i++) { + WRITE(SPI_SCK_PIN, LOW); + + WRITE(SPI_MOSI_PIN, data & 0x80); + + data <<= 1; + + WRITE(SPI_SCK_PIN, HIGH); + } + // hold SCK high for a few ns + nop; + nop; + nop; + nop; + + WRITE(SPI_SCK_PIN, LOW); + // enable interrupts + sei(); + } + //------------------------------------------------------------------------------ + /** Soft SPI send block */ + void spiSendBlock(uint8_t token, const uint8_t* buf) { + spiSend(token); + for (uint16_t i = 0; i < 512; i++) + spiSend(buf[i]); + } +#endif // SOFTWARE_SPI +//------------------------------------------------------------------------------ +// send command and return error code. Return zero for OK +uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { + // select card + chipSelectLow(); + + // wait up to 300 ms if busy + waitNotBusy(300); + + // send command + spiSend(cmd | 0x40); + + // send argument + for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s); + + // send CRC + uint8_t crc = 0xFF; + if (cmd == CMD0) crc = 0x95; // correct crc for CMD0 with arg 0 + if (cmd == CMD8) crc = 0x87; // correct crc for CMD8 with arg 0x1AA + spiSend(crc); + + // skip stuff byte for stop read + if (cmd == CMD12) spiRec(); + + // wait for response + for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ } + return status_; +} +//------------------------------------------------------------------------------ +/** + * Determine the size of an SD flash memory card. + * + * \return The number of 512 byte data blocks in the card + * or zero if an error occurs. + */ +uint32_t Sd2Card::cardSize() { + csd_t csd; + if (!readCSD(&csd)) return 0; + if (csd.v1.csd_ver == 0) { + uint8_t read_bl_len = csd.v1.read_bl_len; + uint16_t c_size = (csd.v1.c_size_high << 10) + | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low; + uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1) + | csd.v1.c_size_mult_low; + return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7); + } + else if (csd.v2.csd_ver == 1) { + uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16) + | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low; + return (c_size + 1) << 10; + } + else { + error(SD_CARD_ERROR_BAD_CSD); + return 0; + } +} +//------------------------------------------------------------------------------ +void Sd2Card::chipSelectHigh() { + digitalWrite(chipSelectPin_, HIGH); +} +//------------------------------------------------------------------------------ +void Sd2Card::chipSelectLow() { + #if DISABLED(SOFTWARE_SPI) + spiInit(spiRate_); + #endif // SOFTWARE_SPI + digitalWrite(chipSelectPin_, LOW); +} +//------------------------------------------------------------------------------ +/** Erase a range of blocks. + * + * \param[in] firstBlock The address of the first block in the range. + * \param[in] lastBlock The address of the last block in the range. + * + * \note This function requests the SD card to do a flash erase for a + * range of blocks. The data on the card after an erase operation is + * either 0 or 1, depends on the card vendor. The card must support + * single block erase. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) { + csd_t csd; + if (!readCSD(&csd)) goto FAIL; + // check for single block erase + if (!csd.v1.erase_blk_en) { + // erase size mask + uint8_t m = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low; + if ((firstBlock & m) != 0 || ((lastBlock + 1) & m) != 0) { + // error card can't erase specified area + error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK); + goto FAIL; + } + } + if (type_ != SD_CARD_TYPE_SDHC) { + firstBlock <<= 9; + lastBlock <<= 9; + } + if (cardCommand(CMD32, firstBlock) + || cardCommand(CMD33, lastBlock) + || cardCommand(CMD38, 0)) { + error(SD_CARD_ERROR_ERASE); + goto FAIL; + } + if (!waitNotBusy(SD_ERASE_TIMEOUT)) { + error(SD_CARD_ERROR_ERASE_TIMEOUT); + goto FAIL; + } + chipSelectHigh(); + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Determine if card supports single block erase. + * + * \return The value one, true, is returned if single block erase is supported. + * The value zero, false, is returned if single block erase is not supported. + */ +bool Sd2Card::eraseSingleBlockEnable() { + csd_t csd; + return readCSD(&csd) ? csd.v1.erase_blk_en : false; +} +//------------------------------------------------------------------------------ +/** + * Initialize an SD flash memory card. + * + * \param[in] sckRateID SPI clock rate selector. See setSckRate(). + * \param[in] chipSelectPin SD chip select pin number. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. The reason for failure + * can be determined by calling errorCode() and errorData(). + */ +bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { + errorCode_ = type_ = 0; + chipSelectPin_ = chipSelectPin; + // 16-bit init start time allows over a minute + uint16_t t0 = (uint16_t)millis(); + uint32_t arg; + + // If init takes more than 4s it could trigger + // watchdog leading to a reboot loop. + #if ENABLED(USE_WATCHDOG) + watchdog_reset(); + #endif + + // set pin modes + pinMode(chipSelectPin_, OUTPUT); + chipSelectHigh(); + SET_INPUT(SPI_MISO_PIN); + SET_OUTPUT(SPI_MOSI_PIN); + SET_OUTPUT(SPI_SCK_PIN); + + #if DISABLED(SOFTWARE_SPI) + // SS must be in output mode even it is not chip select + SET_OUTPUT(SS_PIN); + // set SS high - may be chip select for another SPI device + #if SET_SPI_SS_HIGH + WRITE(SS_PIN, HIGH); + #endif // SET_SPI_SS_HIGH + // set SCK rate for initialization commands + spiRate_ = SPI_SD_INIT_RATE; + spiInit(spiRate_); + #endif // SOFTWARE_SPI + + // must supply min of 74 clock cycles with CS high. + for (uint8_t i = 0; i < 10; i++) spiSend(0xFF); + + // command to go idle in SPI mode + while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { + if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { + error(SD_CARD_ERROR_CMD0); + goto FAIL; + } + } + // check SD version + if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { + type(SD_CARD_TYPE_SD1); + } + else { + // only need last byte of r7 response + for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); + if (status_ != 0xAA) { + error(SD_CARD_ERROR_CMD8); + goto FAIL; + } + type(SD_CARD_TYPE_SD2); + } + // initialize card and send host supports SDHC if SD2 + arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0; + + while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { + // check for timeout + if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { + error(SD_CARD_ERROR_ACMD41); + goto FAIL; + } + } + // if SD2 read OCR register to check for SDHC card + if (type() == SD_CARD_TYPE_SD2) { + if (cardCommand(CMD58, 0)) { + error(SD_CARD_ERROR_CMD58); + goto FAIL; + } + if ((spiRec() & 0xC0) == 0xC0) type(SD_CARD_TYPE_SDHC); + // discard rest of ocr - contains allowed voltage range + for (uint8_t i = 0; i < 3; i++) spiRec(); + } + chipSelectHigh(); + + #if DISABLED(SOFTWARE_SPI) + return setSckRate(sckRateID); + #else // SOFTWARE_SPI + UNUSED(sckRateID); + return true; + #endif // SOFTWARE_SPI + + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** + * Read a 512 byte block from an SD card. + * + * \param[in] blockNumber Logical block to be read. + * \param[out] dst Pointer to the location that will receive the data. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) { + // use address if not SDHC card + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + + #if ENABLED(SD_CHECK_AND_RETRY) + uint8_t retryCnt = 3; + do { + if (!cardCommand(CMD17, blockNumber)) { + if (readData(dst, 512)) return true; + } + else + error(SD_CARD_ERROR_CMD17); + + if (!--retryCnt) break; + + chipSelectHigh(); + cardCommand(CMD12, 0); // Try sending a stop command, ignore the result. + errorCode_ = 0; + } while (true); + #else + if (cardCommand(CMD17, blockNumber)) + error(SD_CARD_ERROR_CMD17); + else + return readData(dst, 512); + #endif + + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Read one data block in a multiple block read sequence + * + * \param[in] dst Pointer to the location for the data to be read. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readData(uint8_t* dst) { + chipSelectLow(); + return readData(dst, 512); +} + +#if ENABLED(SD_CHECK_AND_RETRY) +static const uint16_t crctab[] PROGMEM = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, + 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, + 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, + 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, + 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, + 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, + 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, + 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, + 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, + 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, + 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, + 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, + 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, + 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, + 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, + 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, + 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, + 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, + 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, + 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, + 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, + 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, + 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 +}; +static uint16_t CRC_CCITT(const uint8_t* data, size_t n) { + uint16_t crc = 0; + for (size_t i = 0; i < n; i++) { + crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0xFF]) ^ (crc << 8); + } + return crc; +} +#endif + +//------------------------------------------------------------------------------ +bool Sd2Card::readData(uint8_t* dst, uint16_t count) { + // wait for start block token + uint16_t t0 = millis(); + while ((status_ = spiRec()) == 0XFF) { + if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) { + error(SD_CARD_ERROR_READ_TIMEOUT); + goto FAIL; + } + } + if (status_ != DATA_START_BLOCK) { + error(SD_CARD_ERROR_READ); + goto FAIL; + } + // transfer data + spiRead(dst, count); + +#if ENABLED(SD_CHECK_AND_RETRY) + { + uint16_t calcCrc = CRC_CCITT(dst, count); + uint16_t recvCrc = spiRec() << 8; + recvCrc |= spiRec(); + if (calcCrc != recvCrc) { + error(SD_CARD_ERROR_CRC); + goto FAIL; + } + } +#else + // discard CRC + spiRec(); + spiRec(); +#endif + chipSelectHigh(); + // Send an additional dummy byte, required by Toshiba Flash Air SD Card + spiSend(0XFF); + return true; + FAIL: + chipSelectHigh(); + // Send an additional dummy byte, required by Toshiba Flash Air SD Card + spiSend(0XFF); + return false; +} +//------------------------------------------------------------------------------ +/** read CID or CSR register */ +bool Sd2Card::readRegister(uint8_t cmd, void* buf) { + uint8_t* dst = reinterpret_cast(buf); + if (cardCommand(cmd, 0)) { + error(SD_CARD_ERROR_READ_REG); + goto FAIL; + } + return readData(dst, 16); + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Start a read multiple blocks sequence. + * + * \param[in] blockNumber Address of first block in sequence. + * + * \note This function is used with readData() and readStop() for optimized + * multiple block reads. SPI chipSelect must be low for the entire sequence. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readStart(uint32_t blockNumber) { + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD18, blockNumber)) { + error(SD_CARD_ERROR_CMD18); + goto FAIL; + } + chipSelectHigh(); + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** End a read multiple blocks sequence. + * +* \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readStop() { + chipSelectLow(); + if (cardCommand(CMD12, 0)) { + error(SD_CARD_ERROR_CMD12); + goto FAIL; + } + chipSelectHigh(); + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** + * Set the SPI clock rate. + * + * \param[in] sckRateID A value in the range [0, 6]. + * + * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum + * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128 + * for \a scsRateID = 6. + * + * \return The value one, true, is returned for success and the value zero, + * false, is returned for an invalid value of \a sckRateID. + */ +bool Sd2Card::setSckRate(uint8_t sckRateID) { + if (sckRateID > 6) { + error(SD_CARD_ERROR_SCK_RATE); + return false; + } + spiRate_ = sckRateID; + return true; +} +//------------------------------------------------------------------------------ +// wait for card to go not busy +bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) { + uint16_t t0 = millis(); + while (spiRec() != 0XFF) { + if (((uint16_t)millis() - t0) >= timeoutMillis) goto FAIL; + } + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** + * Writes a 512 byte block to an SD card. + * + * \param[in] blockNumber Logical block to be written. + * \param[in] src Pointer to the location of the data to be written. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { + // use address if not SDHC card + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD24, blockNumber)) { + error(SD_CARD_ERROR_CMD24); + goto FAIL; + } + if (!writeData(DATA_START_BLOCK, src)) goto FAIL; + + // wait for flash programming to complete + if (!waitNotBusy(SD_WRITE_TIMEOUT)) { + error(SD_CARD_ERROR_WRITE_TIMEOUT); + goto FAIL; + } + // response is r2 so get and check two bytes for nonzero + if (cardCommand(CMD13, 0) || spiRec()) { + error(SD_CARD_ERROR_WRITE_PROGRAMMING); + goto FAIL; + } + chipSelectHigh(); + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Write one data block in a multiple block write sequence + * \param[in] src Pointer to the location of the data to be written. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeData(const uint8_t* src) { + chipSelectLow(); + // wait for previous write to finish + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; + if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto FAIL; + chipSelectHigh(); + return true; + FAIL: + error(SD_CARD_ERROR_WRITE_MULTIPLE); + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +// send one block of data for write block or write multiple blocks +bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { + spiSendBlock(token, src); + + spiSend(0xFF); // dummy crc + spiSend(0xFF); // dummy crc + + status_ = spiRec(); + if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { + error(SD_CARD_ERROR_WRITE); + goto FAIL; + } + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Start a write multiple blocks sequence. + * + * \param[in] blockNumber Address of first block in sequence. + * \param[in] eraseCount The number of blocks to be pre-erased. + * + * \note This function is used with writeData() and writeStop() + * for optimized multiple block writes. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { + // send pre-erase count + if (cardAcmd(ACMD23, eraseCount)) { + error(SD_CARD_ERROR_ACMD23); + goto FAIL; + } + // use address if not SDHC card + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD25, blockNumber)) { + error(SD_CARD_ERROR_CMD25); + goto FAIL; + } + chipSelectHigh(); + return true; + FAIL: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** End a write multiple blocks sequence. + * +* \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeStop() { + chipSelectLow(); + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; + spiSend(STOP_TRAN_TOKEN); + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; + chipSelectHigh(); + return true; + FAIL: + error(SD_CARD_ERROR_STOP_TRAN); + chipSelectHigh(); + return false; +} + +#endif diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.h b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.h new file mode 100644 index 0000000..1fbd527 --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/Sd2Card.h @@ -0,0 +1,251 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Arduino Sd2Card Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + */ + +#include "Marlin.h" +#if ENABLED(SDSUPPORT) + +#ifndef Sd2Card_h +#define Sd2Card_h +/** + * \file + * \brief Sd2Card class for V2 SD/SDHC cards + */ +#include "SdFatConfig.h" +#include "SdInfo.h" +//------------------------------------------------------------------------------ +// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6 +/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */ +uint8_t const SPI_FULL_SPEED = 0; +/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */ +uint8_t const SPI_HALF_SPEED = 1; +/** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */ +uint8_t const SPI_QUARTER_SPEED = 2; +/** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */ +uint8_t const SPI_EIGHTH_SPEED = 3; +/** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */ +uint8_t const SPI_SIXTEENTH_SPEED = 4; +//------------------------------------------------------------------------------ +/** init timeout ms */ +uint16_t const SD_INIT_TIMEOUT = 2000; +/** erase timeout ms */ +uint16_t const SD_ERASE_TIMEOUT = 10000; +/** read timeout ms */ +uint16_t const SD_READ_TIMEOUT = 300; +/** write time out ms */ +uint16_t const SD_WRITE_TIMEOUT = 600; +//------------------------------------------------------------------------------ +// SD card errors +/** timeout error for command CMD0 (initialize card in SPI mode) */ +uint8_t const SD_CARD_ERROR_CMD0 = 0X1; +/** CMD8 was not accepted - not a valid SD card*/ +uint8_t const SD_CARD_ERROR_CMD8 = 0X2; +/** card returned an error response for CMD12 (write stop) */ +uint8_t const SD_CARD_ERROR_CMD12 = 0X3; +/** card returned an error response for CMD17 (read block) */ +uint8_t const SD_CARD_ERROR_CMD17 = 0X4; +/** card returned an error response for CMD18 (read multiple block) */ +uint8_t const SD_CARD_ERROR_CMD18 = 0X5; +/** card returned an error response for CMD24 (write block) */ +uint8_t const SD_CARD_ERROR_CMD24 = 0X6; +/** WRITE_MULTIPLE_BLOCKS command failed */ +uint8_t const SD_CARD_ERROR_CMD25 = 0X7; +/** card returned an error response for CMD58 (read OCR) */ +uint8_t const SD_CARD_ERROR_CMD58 = 0X8; +/** SET_WR_BLK_ERASE_COUNT failed */ +uint8_t const SD_CARD_ERROR_ACMD23 = 0X9; +/** ACMD41 initialization process timeout */ +uint8_t const SD_CARD_ERROR_ACMD41 = 0XA; +/** card returned a bad CSR version field */ +uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB; +/** erase block group command failed */ +uint8_t const SD_CARD_ERROR_ERASE = 0XC; +/** card not capable of single block erase */ +uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD; +/** Erase sequence timed out */ +uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE; +/** card returned an error token instead of read data */ +uint8_t const SD_CARD_ERROR_READ = 0XF; +/** read CID or CSD failed */ +uint8_t const SD_CARD_ERROR_READ_REG = 0x10; +/** timeout while waiting for start of read data */ +uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0x11; +/** card did not accept STOP_TRAN_TOKEN */ +uint8_t const SD_CARD_ERROR_STOP_TRAN = 0x12; +/** card returned an error token as a response to a write operation */ +uint8_t const SD_CARD_ERROR_WRITE = 0x13; +/** attempt to write protected block zero */ +uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14; // REMOVE - not used +/** card did not go ready for a multiple block write */ +uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0x15; +/** card returned an error to a CMD13 status check after a write */ +uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16; +/** timeout occurred during write programming */ +uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0x17; +/** incorrect rate selected */ +uint8_t const SD_CARD_ERROR_SCK_RATE = 0x18; +/** init() not called */ +uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0x19; +/** crc check error */ +uint8_t const SD_CARD_ERROR_CRC = 0x20; +//------------------------------------------------------------------------------ +// card types +/** Standard capacity V1 SD card */ +uint8_t const SD_CARD_TYPE_SD1 = 1; +/** Standard capacity V2 SD card */ +uint8_t const SD_CARD_TYPE_SD2 = 2; +/** High Capacity SD card */ +uint8_t const SD_CARD_TYPE_SDHC = 3; +/** + * define SOFTWARE_SPI to use bit-bang SPI + */ +//------------------------------------------------------------------------------ +#if MEGA_SOFT_SPI + #define SOFTWARE_SPI +#elif USE_SOFTWARE_SPI + #define SOFTWARE_SPI +#endif // MEGA_SOFT_SPI +//------------------------------------------------------------------------------ +// SPI pin definitions - do not edit here - change in SdFatConfig.h +// +#if DISABLED(SOFTWARE_SPI) + // hardware pin defs + /** The default chip select pin for the SD card is SS. */ + #define SD_CHIP_SELECT_PIN SS_PIN + // The following three pins must not be redefined for hardware SPI. + /** SPI Master Out Slave In pin */ + #define SPI_MOSI_PIN MOSI_PIN + /** SPI Master In Slave Out pin */ + #define SPI_MISO_PIN MISO_PIN + /** SPI Clock pin */ + #define SPI_SCK_PIN SCK_PIN + +#else // SOFTWARE_SPI + + /** SPI chip select pin */ + #define SD_CHIP_SELECT_PIN SOFT_SPI_CS_PIN + /** SPI Master Out Slave In pin */ + #define SPI_MOSI_PIN SOFT_SPI_MOSI_PIN + /** SPI Master In Slave Out pin */ + #define SPI_MISO_PIN SOFT_SPI_MISO_PIN + /** SPI Clock pin */ + #define SPI_SCK_PIN SOFT_SPI_SCK_PIN +#endif // SOFTWARE_SPI +//------------------------------------------------------------------------------ +/** + * \class Sd2Card + * \brief Raw access to SD and SDHC flash memory cards. + */ +class Sd2Card { + public: + /** Construct an instance of Sd2Card. */ + Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {} + uint32_t cardSize(); + bool erase(uint32_t firstBlock, uint32_t lastBlock); + bool eraseSingleBlockEnable(); + /** + * Set SD error code. + * \param[in] code value for error code. + */ + void error(uint8_t code) {errorCode_ = code;} + /** + * \return error code for last error. See Sd2Card.h for a list of error codes. + */ + int errorCode() const {return errorCode_;} + /** \return error data for last error. */ + int errorData() const {return status_;} + /** + * Initialize an SD flash memory card with default clock rate and chip + * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). + * + * \return true for success or false for failure. + */ + bool init(uint8_t sckRateID = SPI_FULL_SPEED, + uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); + bool readBlock(uint32_t block, uint8_t* dst); + /** + * Read a card's CID register. The CID contains card identification + * information such as Manufacturer ID, Product name, Product serial + * number and Manufacturing date. + * + * \param[out] cid pointer to area for returned data. + * + * \return true for success or false for failure. + */ + bool readCID(cid_t* cid) { + return readRegister(CMD10, cid); + } + /** + * Read a card's CSD register. The CSD contains Card-Specific Data that + * provides information regarding access to the card's contents. + * + * \param[out] csd pointer to area for returned data. + * + * \return true for success or false for failure. + */ + bool readCSD(csd_t* csd) { + return readRegister(CMD9, csd); + } + bool readData(uint8_t* dst); + bool readStart(uint32_t blockNumber); + bool readStop(); + bool setSckRate(uint8_t sckRateID); + /** Return the card type: SD V1, SD V2 or SDHC + * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC. + */ + int type() const {return type_;} + bool writeBlock(uint32_t blockNumber, const uint8_t* src); + bool writeData(const uint8_t* src); + bool writeStart(uint32_t blockNumber, uint32_t eraseCount); + bool writeStop(); + private: + //---------------------------------------------------------------------------- + uint8_t chipSelectPin_; + uint8_t errorCode_; + uint8_t spiRate_; + uint8_t status_; + uint8_t type_; + // private functions + uint8_t cardAcmd(uint8_t cmd, uint32_t arg) { + cardCommand(CMD55, 0); + return cardCommand(cmd, arg); + } + uint8_t cardCommand(uint8_t cmd, uint32_t arg); + + bool readData(uint8_t* dst, uint16_t count); + bool readRegister(uint8_t cmd, void* buf); + void chipSelectHigh(); + void chipSelectLow(); + void type(uint8_t value) {type_ = value;} + bool waitNotBusy(uint16_t timeoutMillis); + bool writeData(uint8_t token, const uint8_t* src); +}; +#endif // Sd2Card_h + + +#endif diff --git a/Marlin-bugfix-1.1.x-trigorilla/Marlin/SdBaseFile.cpp b/Marlin-bugfix-1.1.x-trigorilla/Marlin/SdBaseFile.cpp new file mode 100644 index 0000000..241f07b --- /dev/null +++ b/Marlin-bugfix-1.1.x-trigorilla/Marlin/SdBaseFile.cpp @@ -0,0 +1,1826 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + */ + +#include "Marlin.h" +#if ENABLED(SDSUPPORT) + +#include "SdBaseFile.h" +//------------------------------------------------------------------------------ +// pointer to cwd directory +SdBaseFile* SdBaseFile::cwd_ = 0; +// callback function for date/time +void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0; +//------------------------------------------------------------------------------ +// add a cluster to a file +bool SdBaseFile::addCluster() { + if (!vol_->allocContiguous(1, &curCluster_)) goto FAIL; + + // if first cluster of file link to directory entry + if (firstCluster_ == 0) { + firstCluster_ = curCluster_; + flags_ |= F_FILE_DIR_DIRTY; + } + return true; + + FAIL: + return false; +} +//------------------------------------------------------------------------------ +// Add a cluster to a directory file and zero the cluster. +// return with first block of cluster in the cache +bool SdBaseFile::addDirCluster() { + uint32_t block; + // max folder size + if (fileSize_ / sizeof(dir_t) >= 0xFFFF) goto FAIL; + + if (!addCluster()) goto FAIL; + if (!vol_->cacheFlush()) goto FAIL; + + block = vol_->clusterStartBlock(curCluster_); + + // set cache to first block of cluster + vol_->cacheSetBlockNumber(block, true); + + // zero first block of cluster + memset(vol_->cacheBuffer_.data, 0, 512); + + // zero rest of cluster + for (uint8_t i = 1; i < vol_->blocksPerCluster_; i++) { + if (!vol_->writeBlock(block + i, vol_->cacheBuffer_.data)) goto FAIL; + } + // Increase directory file size by cluster size + fileSize_ += 512UL << vol_->clusterSizeShift_; + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +// cache a file's directory entry +// return pointer to cached entry or null for failure +dir_t* SdBaseFile::cacheDirEntry(uint8_t action) { + if (!vol_->cacheRawBlock(dirBlock_, action)) goto FAIL; + return vol_->cache()->dir + dirIndex_; + FAIL: + return 0; +} +//------------------------------------------------------------------------------ +/** Close a file and force cached data and directory information + * to be written to the storage device. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include no file is open or an I/O error. + */ +bool SdBaseFile::close() { + bool rtn = sync(); + type_ = FAT_FILE_TYPE_CLOSED; + return rtn; +} +//------------------------------------------------------------------------------ +/** Check for contiguous file and return its raw block range. + * + * \param[out] bgnBlock the first block address for the file. + * \param[out] endBlock the last block address for the file. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include file is not contiguous, file has zero length + * or an I/O error occurred. + */ +bool SdBaseFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) { + // error if no blocks + if (firstCluster_ == 0) goto FAIL; + + for (uint32_t c = firstCluster_; ; c++) { + uint32_t next; + if (!vol_->fatGet(c, &next)) goto FAIL; + + // check for contiguous + if (next != (c + 1)) { + // error if not end of chain + if (!vol_->isEOC(next)) goto FAIL; + *bgnBlock = vol_->clusterStartBlock(firstCluster_); + *endBlock = vol_->clusterStartBlock(c) + + vol_->blocksPerCluster_ - 1; + return true; + } + } + + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Create and open a new contiguous file of a specified size. + * + * \note This function only supports short DOS 8.3 names. + * See open() for more information. + * + * \param[in] dirFile The directory where the file will be created. + * \param[in] path A path with a valid DOS 8.3 file name. + * \param[in] size The desired file size. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include \a path contains + * an invalid DOS 8.3 file name, the FAT volume has not been initialized, + * a file is already open, the file already exists, the root + * directory is full or an I/O error. + * + */ +bool SdBaseFile::createContiguous(SdBaseFile* dirFile, + const char* path, uint32_t size) { + uint32_t count; + // don't allow zero length file + if (size == 0) goto FAIL; + if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) goto FAIL; + + // calculate number of clusters needed + count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1; + + // allocate clusters + if (!vol_->allocContiguous(count, &firstCluster_)) { + remove(); + goto FAIL; + } + fileSize_ = size; + + // insure sync() will update dir entry + flags_ |= F_FILE_DIR_DIRTY; + + return sync(); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Return a file's directory entry. + * + * \param[out] dir Location for return of the file's directory entry. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::dirEntry(dir_t* dir) { + dir_t* p; + // make sure fields on SD are correct + if (!sync()) goto FAIL; + + // read entry + p = cacheDirEntry(SdVolume::CACHE_FOR_READ); + if (!p) goto FAIL; + + // copy to caller's struct + memcpy(dir, p, sizeof(dir_t)); + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Format the name field of \a dir into the 13 byte array + * \a name in standard 8.3 short name format. + * + * \param[in] dir The directory structure containing the name. + * \param[out] name A 13 byte char array for the formatted name. + */ +void SdBaseFile::dirName(const dir_t& dir, char* name) { + uint8_t j = 0; + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) name[j++] = '.'; + name[j++] = dir.name[i]; + } + name[j] = 0; +} +//------------------------------------------------------------------------------ +/** Test for the existence of a file in a directory + * + * \param[in] name Name of the file to be tested for. + * + * The calling instance must be an open directory file. + * + * dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory + * dirFile. + * + * \return true if the file exists else false. + */ +bool SdBaseFile::exists(const char* name) { + SdBaseFile file; + return file.open(this, name, O_READ); +} +//------------------------------------------------------------------------------ +/** + * Get a string from a file. + * + * fgets() reads bytes from a file into the array pointed to by \a str, until + * \a num - 1 bytes are read, or a delimiter is read and transferred to \a str, + * or end-of-file is encountered. The string is then terminated + * with a null byte. + * + * fgets() deletes CR, '\\r', from the string. This insures only a '\\n' + * terminates the string for Windows text files which use CRLF for newline. + * + * \param[out] str Pointer to the array where the string is stored. + * \param[in] num Maximum number of characters to be read + * (including the final null byte). Usually the length + * of the array \a str is used. + * \param[in] delim Optional set of delimiters. The default is "\n". + * + * \return For success fgets() returns the length of the string in \a str. + * If no data is read, fgets() returns zero for EOF or -1 if an error occurred. + **/ +int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) { + char ch; + int16_t n = 0; + int16_t r = -1; + while ((n + 1) < num && (r = read(&ch, 1)) == 1) { + // delete CR + if (ch == '\r') continue; + str[n++] = ch; + if (!delim) { + if (ch == '\n') break; + } + else { + if (strchr(delim, ch)) break; + } + } + if (r < 0) { + // read error + return -1; + } + str[n] = '\0'; + return n; +} +//------------------------------------------------------------------------------ +/** Get a file's name + * + * \param[out] name An array of 13 characters for the file's name. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::getFilename(char* name) { + if (!isOpen()) return false; + + if (isRoot()) { + name[0] = '/'; + name[1] = '\0'; + return true; + } + // cache entry + dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ); + if (!p) return false; + + // format name + dirName(*p, name); + return true; +} +//------------------------------------------------------------------------------ +void SdBaseFile::getpos(filepos_t* pos) { + pos->position = curPosition_; + pos->cluster = curCluster_; +} + +//------------------------------------------------------------------------------ +/** List directory contents. + * + * \param[in] pr Print stream for list. + * + * \param[in] flags The inclusive OR of + * + * LS_DATE - %Print file modification date + * + * LS_SIZE - %Print file size. + * + * LS_R - Recursive list of subdirectories. + * + * \param[in] indent Amount of space before file name. Used for recursive + * list to indicate subdirectory level. + */ +void SdBaseFile::ls(uint8_t flags, uint8_t indent) { + rewind(); + int8_t status; + while ((status = lsPrintNext(flags, indent))) { + if (status > 1 && (flags & LS_R)) { + uint16_t index = curPosition() / 32 - 1; + SdBaseFile s; + if (s.open(this, index, O_READ)) s.ls(flags, indent + 2); + seekSet(32 * (index + 1)); + } + } +} +//------------------------------------------------------------------------------ +// saves 32 bytes on stack for ls recursion +// return 0 - EOF, 1 - normal file, or 2 - directory +int8_t SdBaseFile::lsPrintNext(uint8_t flags, uint8_t indent) { + dir_t dir; + uint8_t w = 0; + + while (1) { + if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0; + if (dir.name[0] == DIR_NAME_FREE) return 0; + + // skip deleted entry and entries for . and .. + if (dir.name[0] != DIR_NAME_DELETED && dir.name[0] != '.' + && DIR_IS_FILE_OR_SUBDIR(&dir)) break; + } + // indent for dir level + for (uint8_t i = 0; i < indent; i++) MYSERIAL.write(' '); + + // print name + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) { + MYSERIAL.write('.'); + w++; + } + MYSERIAL.write(dir.name[i]); + w++; + } + if (DIR_IS_SUBDIR(&dir)) { + MYSERIAL.write('/'); + w++; + } + if (flags & (LS_DATE | LS_SIZE)) { + while (w++ < 14) MYSERIAL.write(' '); + } + // print modify date/time if requested + if (flags & LS_DATE) { + MYSERIAL.write(' '); + printFatDate(dir.lastWriteDate); + MYSERIAL.write(' '); + printFatTime(dir.lastWriteTime); + } + // print size if requested + if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) { + MYSERIAL.write(' '); + MYSERIAL.print(dir.fileSize); + } + MYSERIAL.println(); + return DIR_IS_FILE(&dir) ? 1 : 2; +} +//------------------------------------------------------------------------------ +// format directory name field from a 8.3 name string +bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) { + uint8_t c; + uint8_t n = 7; // max index for part before dot + uint8_t i = 0; + // blank fill name and extension + while (i < 11) name[i++] = ' '; + i = 0; + while (*str != '\0' && *str != '/') { + c = *str++; + if (c == '.') { + if (n == 10) goto FAIL; // only one dot allowed + n = 10; // max index for full 8.3 name + i = 8; // place for extension + } + else { + // illegal FAT characters + PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); + uint8_t b; + while ((b = pgm_read_byte(p++))) if (b == c) goto FAIL; + // check size and only allow ASCII printable characters + if (i > n || c < 0x21 || c == 0x7F) goto FAIL; + // only upper case allowed in 8.3 names - convert lower to upper + name[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); + } + } + *ptr = str; + // must have a file name, extension is optional + return name[0] != ' '; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Make a new directory. + * + * \param[in] parent An open SdFat instance for the directory that will contain + * the new directory. + * + * \param[in] path A path with a valid 8.3 DOS name for the new directory. + * + * \param[in] pFlag Create missing parent directories if true. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include this file is already open, \a parent is not a + * directory, \a path is invalid or already exists in \a parent. + */ +bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) { + uint8_t dname[11]; + SdBaseFile dir1, dir2; + SdBaseFile* sub = &dir1; + SdBaseFile* start = parent; + + if (!parent || isOpen()) goto FAIL; + + if (*path == '/') { + while (*path == '/') path++; + if (!parent->isRoot()) { + if (!dir2.openRoot(parent->vol_)) goto FAIL; + parent = &dir2; + } + } + while (1) { + if (!make83Name(path, dname, &path)) goto FAIL; + while (*path == '/') path++; + if (!*path) break; + if (!sub->open(parent, dname, O_READ)) { + if (!pFlag || !sub->mkdir(parent, dname)) { + goto FAIL; + } + } + if (parent != start) parent->close(); + parent = sub; + sub = parent != &dir1 ? &dir1 : &dir2; + } + return mkdir(parent, dname); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) { + uint32_t block; + dir_t d; + dir_t* p; + + if (!parent->isDir()) goto FAIL; + + // create a normal file + if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) goto FAIL; + + // convert file to directory + flags_ = O_READ; + type_ = FAT_FILE_TYPE_SUBDIR; + + // allocate and zero first cluster + if (!addDirCluster())goto FAIL; + + // force entry to SD + if (!sync()) goto FAIL; + + // cache entry - should already be in cache due to sync() call + p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!p) goto FAIL; + + // change directory entry attribute + p->attributes = DIR_ATT_DIRECTORY; + + // make entry for '.' + memcpy(&d, p, sizeof(d)); + d.name[0] = '.'; + for (uint8_t i = 1; i < 11; i++) d.name[i] = ' '; + + // cache block for '.' and '..' + block = vol_->clusterStartBlock(firstCluster_); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto FAIL; + + // copy '.' to block + memcpy(&vol_->cache()->dir[0], &d, sizeof(d)); + + // make entry for '..' + d.name[1] = '.'; + if (parent->isRoot()) { + d.firstClusterLow = 0; + d.firstClusterHigh = 0; + } + else { + d.firstClusterLow = parent->firstCluster_ & 0xFFFF; + d.firstClusterHigh = parent->firstCluster_ >> 16; + } + // copy '..' to block + memcpy(&vol_->cache()->dir[1], &d, sizeof(d)); + + // write first block + return vol_->cacheFlush(); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Open a file in the current working directory. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::open(const char* path, uint8_t oflag) { + return open(cwd_, path, oflag); +} +//------------------------------------------------------------------------------ +/** Open a file or directory by name. + * + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags from the following list + * + * O_READ - Open for reading. + * + * O_RDONLY - Same as O_READ. + * + * O_WRITE - Open for writing. + * + * O_WRONLY - Same as O_WRITE. + * + * O_RDWR - Open for reading and writing. + * + * O_APPEND - If set, the file offset shall be set to the end of the + * file prior to each write. + * + * O_AT_END - Set the initial position at the end of the file. + * + * O_CREAT - If the file exists, this flag has no effect except as noted + * under O_EXCL below. Otherwise, the file shall be created + * + * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists. + * + * O_SYNC - Call sync() after each write. This flag should not be used with + * write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. + * These functions do character at a time writes so sync() will be called + * after each byte. + * + * O_TRUNC - If the file exists and is a regular file, and the file is + * successfully opened and is not read only, its length shall be truncated to 0. + * + * WARNING: A given file must not be opened by more than one SdBaseFile object + * of file corruption may occur. + * + * \note Directory files must be opened read only. Write and truncation is + * not allowed for directory files. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include this file is already open, \a dirFile is not + * a directory, \a path is invalid, the file does not exist + * or can't be opened in the access mode specified by oflag. + */ +bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) { + uint8_t dname[11]; + SdBaseFile dir1, dir2; + SdBaseFile* parent = dirFile; + SdBaseFile* sub = &dir1; + + if (!dirFile) goto FAIL; + + // error if already open + if (isOpen()) goto FAIL; + + if (*path == '/') { + while (*path == '/') path++; + if (!dirFile->isRoot()) { + if (!dir2.openRoot(dirFile->vol_)) goto FAIL; + parent = &dir2; + } + } + while (1) { + if (!make83Name(path, dname, &path)) goto FAIL; + while (*path == '/') path++; + if (!*path) break; + if (!sub->open(parent, dname, O_READ)) goto FAIL; + if (parent != dirFile) parent->close(); + parent = sub; + sub = parent != &dir1 ? &dir1 : &dir2; + } + return open(parent, dname, oflag); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +// open with filename in dname +bool SdBaseFile::open(SdBaseFile* dirFile, + const uint8_t dname[11], uint8_t oflag) { + bool emptyFound = false; + bool fileFound = false; + uint8_t index; + dir_t* p; + + vol_ = dirFile->vol_; + + dirFile->rewind(); + // search for file + + while (dirFile->curPosition_ < dirFile->fileSize_) { + index = 0XF & (dirFile->curPosition_ >> 5); + p = dirFile->readDirCache(); + if (!p) goto FAIL; + + if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) { + // remember first empty slot + if (!emptyFound) { + dirBlock_ = dirFile->vol_->cacheBlockNumber(); + dirIndex_ = index; + emptyFound = true; + } + // done if no entries follow + if (p->name[0] == DIR_NAME_FREE) break; + } + else if (!memcmp(dname, p->name, 11)) { + fileFound = true; + break; + } + } + if (fileFound) { + // don't open existing file if O_EXCL + if (oflag & O_EXCL) goto FAIL; + } + else { + // don't create unless O_CREAT and O_WRITE + if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto FAIL; + if (emptyFound) { + index = dirIndex_; + p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!p) goto FAIL; + } + else { + if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto FAIL; + + // add and zero cluster for dirFile - first cluster is in cache for write + if (!dirFile->addDirCluster()) goto FAIL; + + // use first entry in cluster + p = dirFile->vol_->cache()->dir; + index = 0; + } + // initialize as empty file + memset(p, 0, sizeof(*p)); + memcpy(p->name, dname, 11); + + // set timestamps + if (dateTime_) { + // call user date/time function + dateTime_(&p->creationDate, &p->creationTime); + } + else { + // use default date/time + p->creationDate = FAT_DEFAULT_DATE; + p->creationTime = FAT_DEFAULT_TIME; + } + p->lastAccessDate = p->creationDate; + p->lastWriteDate = p->creationDate; + p->lastWriteTime = p->creationTime; + + // write entry to SD + if (!dirFile->vol_->cacheFlush()) goto FAIL; + } + // open entry in cache + return openCachedEntry(index, oflag); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Open a file by index. + * + * \param[in] dirFile An open SdFat instance for the directory. + * + * \param[in] index The \a index of the directory entry for the file to be + * opened. The value for \a index is (directory file position)/32. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * + * See open() by path for definition of flags. + * \return true for success or false for failure. + */ +bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) { + dir_t* p; + + vol_ = dirFile->vol_; + + // error if already open + if (isOpen() || !dirFile) goto FAIL; + + // don't open existing file if O_EXCL - user call error + if (oflag & O_EXCL) goto FAIL; + + // seek to location of entry + if (!dirFile->seekSet(32 * index)) goto FAIL; + + // read entry into cache + p = dirFile->readDirCache(); + if (!p) goto FAIL; + + // error if empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_FREE || + p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') { + goto FAIL; + } + // open cached entry + return openCachedEntry(index & 0XF, oflag); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +// open a cached directory entry. Assumes vol_ is initialized +bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) { + // location of entry in cache + dir_t* p = &vol_->cache()->dir[dirIndex]; + + // write or truncate is an error for a directory or read-only file + if (p->attributes & (DIR_ATT_READ_ONLY | DIR_ATT_DIRECTORY)) { + if (oflag & (O_WRITE | O_TRUNC)) goto FAIL; + } + // remember location of directory entry on SD + dirBlock_ = vol_->cacheBlockNumber(); + dirIndex_ = dirIndex; + + // copy first cluster number for directory fields + firstCluster_ = (uint32_t)p->firstClusterHigh << 16; + firstCluster_ |= p->firstClusterLow; + + // make sure it is a normal file or subdirectory + if (DIR_IS_FILE(p)) { + fileSize_ = p->fileSize; + type_ = FAT_FILE_TYPE_NORMAL; + } + else if (DIR_IS_SUBDIR(p)) { + if (!vol_->chainSize(firstCluster_, &fileSize_)) goto FAIL; + type_ = FAT_FILE_TYPE_SUBDIR; + } + else { + goto FAIL; + } + // save open flags for read/write + flags_ = oflag & F_OFLAG; + + // set to start of file + curCluster_ = 0; + curPosition_ = 0; + if ((oflag & O_TRUNC) && !truncate(0)) return false; + return oflag & O_AT_END ? seekEnd(0) : true; + FAIL: + type_ = FAT_FILE_TYPE_CLOSED; + return false; +} +//------------------------------------------------------------------------------ +/** Open the next file or subdirectory in a directory. + * + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * + * See open() by path for definition of flags. + * \return true for success or false for failure. + */ +bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) { + dir_t* p; + uint8_t index; + + if (!dirFile) goto FAIL; + + // error if already open + if (isOpen()) goto FAIL; + + vol_ = dirFile->vol_; + + while (1) { + index = 0XF & (dirFile->curPosition_ >> 5); + + // read entry into cache + p = dirFile->readDirCache(); + if (!p) goto FAIL; + + // done if last entry + if (p->name[0] == DIR_NAME_FREE) goto FAIL; + + // skip empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') { + continue; + } + // must be file or dir + if (DIR_IS_FILE_OR_SUBDIR(p)) { + return openCachedEntry(index, oflag); + } + } + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Open a directory's parent directory. + * + * \param[in] dir Parent of this directory will be opened. Must not be root. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::openParent(SdBaseFile* dir) { + dir_t entry; + dir_t* p; + SdBaseFile file; + uint32_t c; + uint32_t cluster; + uint32_t lbn; + // error if already open or dir is root or dir is not a directory + if (isOpen() || !dir || dir->isRoot() || !dir->isDir()) goto FAIL; + vol_ = dir->vol_; + // position to '..' + if (!dir->seekSet(32)) goto FAIL; + // read '..' entry + if (dir->read(&entry, sizeof(entry)) != 32) goto FAIL; + // verify it is '..' + if (entry.name[0] != '.' || entry.name[1] != '.') goto FAIL; + // start cluster for '..' + cluster = entry.firstClusterLow; + cluster |= (uint32_t)entry.firstClusterHigh << 16; + if (cluster == 0) return openRoot(vol_); + // start block for '..' + lbn = vol_->clusterStartBlock(cluster); + // first block of parent dir + if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) { + goto FAIL; + } + p = &vol_->cacheBuffer_.dir[1]; + // verify name for '../..' + if (p->name[0] != '.' || p->name[1] != '.') goto FAIL; + // '..' is pointer to first cluster of parent. open '../..' to find parent + if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) { + if (!file.openRoot(dir->volume())) goto FAIL; + } + else if (!file.openCachedEntry(1, O_READ)) { + goto FAIL; + } + // search for parent in '../..' + do { + if (file.readDir(&entry, NULL) != 32) goto FAIL; + c = entry.firstClusterLow; + c |= (uint32_t)entry.firstClusterHigh << 16; + } while (c != cluster); + // open parent + return open(&file, file.curPosition() / 32 - 1, O_READ); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Open a volume's root directory. + * + * \param[in] vol The FAT volume containing the root directory to be opened. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is already open, the FAT volume has + * not been initialized or it a FAT12 volume. + */ +bool SdBaseFile::openRoot(SdVolume* vol) { + // error if file is already open + if (isOpen()) goto FAIL; + + if (vol->fatType() == 16 || (FAT12_SUPPORT && vol->fatType() == 12)) { + type_ = FAT_FILE_TYPE_ROOT_FIXED; + firstCluster_ = 0; + fileSize_ = 32 * vol->rootDirEntryCount(); + } + else if (vol->fatType() == 32) { + type_ = FAT_FILE_TYPE_ROOT32; + firstCluster_ = vol->rootDirStart(); + if (!vol->chainSize(firstCluster_, &fileSize_)) goto FAIL; + } + else { + // volume is not initialized, invalid, or FAT12 without support + return false; + } + vol_ = vol; + // read only + flags_ = O_READ; + + // set to start of file + curCluster_ = 0; + curPosition_ = 0; + + // root has no directory entry + dirBlock_ = 0; + dirIndex_ = 0; + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Return the next available byte without consuming it. + * + * \return The byte if no error and not at eof else -1; + */ +int SdBaseFile::peek() { + filepos_t pos; + getpos(&pos); + int c = read(); + if (c >= 0) setpos(&pos); + return c; +} + +//------------------------------------------------------------------------------ +/** %Print the name field of a directory entry in 8.3 format. + * \param[in] pr Print stream for output. + * \param[in] dir The directory structure containing the name. + * \param[in] width Blank fill name if length is less than \a width. + * \param[in] printSlash Print '/' after directory names if true. + */ +void SdBaseFile::printDirName(const dir_t& dir, + uint8_t width, bool printSlash) { + uint8_t w = 0; + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) { + MYSERIAL.write('.'); + w++; + } + MYSERIAL.write(dir.name[i]); + w++; + } + if (DIR_IS_SUBDIR(&dir) && printSlash) { + MYSERIAL.write('/'); + w++; + } + while (w < width) { + MYSERIAL.write(' '); + w++; + } +} +//------------------------------------------------------------------------------ +// print uint8_t with width 2 +static void print2u(uint8_t v) { + if (v < 10) MYSERIAL.write('0'); + MYSERIAL.print(v, DEC); +} +//------------------------------------------------------------------------------ +/** %Print a directory date field to Serial. + * + * Format is yyyy-mm-dd. + * + * \param[in] fatDate The date field from a directory entry. + */ + +//------------------------------------------------------------------------------ +/** %Print a directory date field. + * + * Format is yyyy-mm-dd. + * + * \param[in] pr Print stream for output. + * \param[in] fatDate The date field from a directory entry. + */ +void SdBaseFile::printFatDate(uint16_t fatDate) { + MYSERIAL.print(FAT_YEAR(fatDate)); + MYSERIAL.write('-'); + print2u(FAT_MONTH(fatDate)); + MYSERIAL.write('-'); + print2u(FAT_DAY(fatDate)); +} + +//------------------------------------------------------------------------------ +/** %Print a directory time field. + * + * Format is hh:mm:ss. + * + * \param[in] pr Print stream for output. + * \param[in] fatTime The time field from a directory entry. + */ +void SdBaseFile::printFatTime(uint16_t fatTime) { + print2u(FAT_HOUR(fatTime)); + MYSERIAL.write(':'); + print2u(FAT_MINUTE(fatTime)); + MYSERIAL.write(':'); + print2u(FAT_SECOND(fatTime)); +} +//------------------------------------------------------------------------------ +/** Print a file's name to Serial + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::printName() { + char name[FILENAME_LENGTH]; + if (!getFilename(name)) return false; + MYSERIAL.print(name); + return true; +} +//------------------------------------------------------------------------------ +/** Read the next byte from a file. + * + * \return For success read returns the next byte in the file as an int. + * If an error occurs or end of file is reached -1 is returned. + */ +int16_t SdBaseFile::read() { + uint8_t b; + return read(&b, 1) == 1 ? b : -1; +} +//------------------------------------------------------------------------------ +/** Read data from a file starting at the current position. + * + * \param[out] buf Pointer to the location that will receive the data. + * + * \param[in] nbyte Maximum number of bytes to read. + * + * \return For success read() returns the number of bytes read. + * A value less than \a nbyte, including zero, will be returned + * if end of file is reached. + * If an error occurs, read() returns -1. Possible errors include + * read() called before a file has been opened, corrupt file system + * or an I/O error occurred. + */ +int16_t SdBaseFile::read(void* buf, uint16_t nbyte) { + uint8_t* dst = reinterpret_cast(buf); + uint16_t offset; + uint16_t toRead; + uint32_t block; // raw device block number + + // error if not open or write only + if (!isOpen() || !(flags_ & O_READ)) goto FAIL; + + // max bytes left in file + NOMORE(nbyte, fileSize_ - curPosition_); + + // amount left to read + toRead = nbyte; + while (toRead > 0) { + offset = curPosition_ & 0x1FF; // offset in block + if (type_ == FAT_FILE_TYPE_ROOT_FIXED) { + block = vol_->rootDirStart() + (curPosition_ >> 9); + } + else { + uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_); + if (offset == 0 && blockOfCluster == 0) { + // start of new cluster + if (curPosition_ == 0) { + // use first cluster in file + curCluster_ = firstCluster_; + } + else { + // get next cluster from FAT + if (!vol_->fatGet(curCluster_, &curCluster_)) goto FAIL; + } + } + block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; + } + uint16_t n = toRead; + + // amount to be read from current block + NOMORE(n, 512 - offset); + + // no buffering needed if n == 512 + if (n == 512 && block != vol_->cacheBlockNumber()) { + if (!vol_->readBlock(block, dst)) goto FAIL; + } + else { + // read block to cache and copy data to caller + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto FAIL; + uint8_t* src = vol_->cache()->data + offset; + memcpy(dst, src, n); + } + dst += n; + curPosition_ += n; + toRead -= n; + } + return nbyte; + FAIL: + return -1; +} + +/** + * Read the next entry in a directory. + * + * \param[out] dir The dir_t struct that will receive the data. + * + * \return For success readDir() returns the number of bytes read. + * A value of zero will be returned if end of file is reached. + * If an error occurs, readDir() returns -1. Possible errors include + * readDir() called before a directory has been opened, this is not + * a directory file or an I/O error occurred. + */ +int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) { + int16_t n; + // if not a directory file or miss-positioned return an error + if (!isDir() || (0x1F & curPosition_)) return -1; + + //If we have a longFilename buffer, mark it as invalid. If we find a long filename it will be filled automaticly. + if (longFilename != NULL) longFilename[0] = '\0'; + + while (1) { + + n = read(dir, sizeof(dir_t)); + if (n != sizeof(dir_t)) return n == 0 ? 0 : -1; + + // last entry if DIR_NAME_FREE + if (dir->name[0] == DIR_NAME_FREE) return 0; + + // skip empty entries and entry for . and .. + if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue; + + // Fill the long filename if we have a long filename entry. + // Long filename entries are stored before the short filename. + if (longFilename != NULL && DIR_IS_LONG_NAME(dir)) { + vfat_t* VFAT = (vfat_t*)dir; + // Sanity-check the VFAT entry. The first cluster is always set to zero. And the sequence number should be higher than 0 + if (VFAT->firstClusterLow == 0 && (VFAT->sequenceNumber & 0x1F) > 0 && (VFAT->sequenceNumber & 0x1F) <= MAX_VFAT_ENTRIES) { + // TODO: Store the filename checksum to verify if a long-filename-unaware system modified the file table. + n = ((VFAT->sequenceNumber & 0x1F) - 1) * (FILENAME_LENGTH); + for (uint8_t i = 0; i < FILENAME_LENGTH; i++) + longFilename[n + i] = (i < 5) ? VFAT->name1[i] : (i < 11) ? VFAT->name2[i - 5] : VFAT->name3[i - 11]; + // If this VFAT entry is the last one, add a NUL terminator at the end of the string + if (VFAT->sequenceNumber & 0x40) longFilename[n + FILENAME_LENGTH] = '\0'; + } + } + // Return if normal file or subdirectory + if (DIR_IS_FILE_OR_SUBDIR(dir)) return n; + } +} + +//------------------------------------------------------------------------------ +// Read next directory entry into the cache +// Assumes file is correctly positioned +dir_t* SdBaseFile::readDirCache() { + uint8_t i; + // error if not directory + if (!isDir()) goto FAIL; + + // index of entry in cache + i = (curPosition_ >> 5) & 0XF; + + // use read to locate and cache block + if (read() < 0) goto FAIL; + + // advance to next entry + curPosition_ += 31; + + // return pointer to entry + return vol_->cache()->dir + i; + FAIL: + return 0; +} +//------------------------------------------------------------------------------ +/** Remove a file. + * + * The directory entry and all data for the file are deleted. + * + * \note This function should not be used to delete the 8.3 version of a + * file that has a long name. For example if a file has the long name + * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file read-only, is a directory, + * or an I/O error occurred. + */ +bool SdBaseFile::remove() { + dir_t* d; + // free any clusters - will fail if read-only or directory + if (!truncate(0)) goto FAIL; + + // cache directory entry + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto FAIL; + + // mark entry deleted + d->name[0] = DIR_NAME_DELETED; + + // set this file closed + type_ = FAT_FILE_TYPE_CLOSED; + + // write entry to SD + return vol_->cacheFlush(); + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Remove a file. + * + * The directory entry and all data for the file are deleted. + * + * \param[in] dirFile The directory that contains the file. + * \param[in] path Path for the file to be removed. + * + * \note This function should not be used to delete the 8.3 version of a + * file that has a long name. For example if a file has the long name + * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is a directory, is read only, + * \a dirFile is not a directory, \a path is not found + * or an I/O error occurred. + */ +bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) { + SdBaseFile file; + if (!file.open(dirFile, path, O_WRITE)) goto FAIL; + return file.remove(); + FAIL: + // can't set iostate - static function + return false; +} +//------------------------------------------------------------------------------ +/** Rename a file or subdirectory. + * + * \param[in] dirFile Directory for the new path. + * \param[in] newPath New path name for the file/directory. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include \a dirFile is not open or is not a directory + * file, newPath is invalid or already exists, or an I/O error occurs. + */ +bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) { + dir_t entry; + uint32_t dirCluster = 0; + SdBaseFile file; + dir_t* d; + + // must be an open file or subdirectory + if (!(isFile() || isSubDir())) goto FAIL; + + // can't move file + if (vol_ != dirFile->vol_) goto FAIL; + + // sync() and cache directory entry + sync(); + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto FAIL; + + // save directory entry + memcpy(&entry, d, sizeof(entry)); + + // mark entry deleted + d->name[0] = DIR_NAME_DELETED; + + // make directory entry for new path + if (isFile()) { + if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) { + goto restore; + } + } + else { + // don't create missing path prefix components + if (!file.mkdir(dirFile, newPath, false)) { + goto restore; + } + // save cluster containing new dot dot + dirCluster = file.firstCluster_; + } + // change to new directory entry + dirBlock_ = file.dirBlock_; + dirIndex_ = file.dirIndex_; + + // mark closed to avoid possible destructor close call + file.type_ = FAT_FILE_TYPE_CLOSED; + + // cache new directory entry + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto FAIL; + + // copy all but name field to new directory entry + memcpy(&d->attributes, &entry.attributes, sizeof(entry) - sizeof(d->name)); + + // update dot dot if directory + if (dirCluster) { + // get new dot dot + uint32_t block = vol_->clusterStartBlock(dirCluster); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto FAIL; + memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry)); + + // free unused cluster + if (!vol_->freeChain(dirCluster)) goto FAIL; + + // store new dot dot + block = vol_->clusterStartBlock(firstCluster_); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto FAIL; + memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry)); + } + return vol_->cacheFlush(); + +restore: + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto FAIL; + // restore entry + d->name[0] = entry.name[0]; + vol_->cacheFlush(); + + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Remove a directory file. + * + * The directory file will be removed only if it is empty and is not the + * root directory. rmdir() follows DOS and Windows and ignores the + * read-only attribute for the directory. + * + * \note This function should not be used to delete the 8.3 version of a + * directory that has a long name. For example if a directory has the + * long name "New folder" you should not delete the 8.3 name "NEWFOL~1". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is not a directory, is the root + * directory, is not empty, or an I/O error occurred. + */ +bool SdBaseFile::rmdir() { + // must be open subdirectory + if (!isSubDir()) goto FAIL; + + rewind(); + + // make sure directory is empty + while (curPosition_ < fileSize_) { + dir_t* p = readDirCache(); + if (!p) goto FAIL; + // done if past last used entry + if (p->name[0] == DIR_NAME_FREE) break; + // skip empty slot, '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue; + // error not empty + if (DIR_IS_FILE_OR_SUBDIR(p)) goto FAIL; + } + // convert empty directory to normal file for remove + type_ = FAT_FILE_TYPE_NORMAL; + flags_ |= O_WRITE; + return remove(); + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Recursively delete a directory and all contained files. + * + * This is like the Unix/Linux 'rm -rf *' if called with the root directory + * hence the name. + * + * Warning - This will remove all contents of the directory including + * subdirectories. The directory will then be removed if it is not root. + * The read-only attribute for files will be ignored. + * + * \note This function should not be used to delete the 8.3 version of + * a directory that has a long name. See remove() and rmdir(). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::rmRfStar() { + uint32_t index; + SdBaseFile f; + rewind(); + while (curPosition_ < fileSize_) { + // remember position + index = curPosition_ / 32; + + dir_t* p = readDirCache(); + if (!p) goto FAIL; + + // done if past last entry + if (p->name[0] == DIR_NAME_FREE) break; + + // skip empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue; + + // skip if part of long file name or volume label in root + if (!DIR_IS_FILE_OR_SUBDIR(p)) continue; + + if (!f.open(this, index, O_READ)) goto FAIL; + if (f.isSubDir()) { + // recursively delete + if (!f.rmRfStar()) goto FAIL; + } + else { + // ignore read-only + f.flags_ |= O_WRITE; + if (!f.remove()) goto FAIL; + } + // position to next entry if required + if (curPosition_ != (32 * (index + 1))) { + if (!seekSet(32 * (index + 1))) goto FAIL; + } + } + // don't try to delete root + if (!isRoot()) { + if (!rmdir()) goto FAIL; + } + return true; + FAIL: + return false; +} +//------------------------------------------------------------------------------ +/** Create a file object and open it in the current working directory. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t). + */ +SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) { + type_ = FAT_FILE_TYPE_CLOSED; + writeErr