My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bltouch.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(BLTOUCH)
  24. #include "bltouch.h"
  25. BLTouch bltouch;
  26. bool BLTouch::od_5v_mode; // Initialized by settings.load, 0 = Open Drain; 1 = 5V Drain
  27. #ifdef BLTOUCH_HS_MODE
  28. bool BLTouch::high_speed_mode; // Initialized by settings.load, 0 = Low Speed; 1 = High Speed
  29. #else
  30. constexpr bool BLTouch::high_speed_mode;
  31. #endif
  32. #include "../module/servo.h"
  33. #include "../module/probe.h"
  34. void stop();
  35. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  36. #include "../core/debug_out.h"
  37. bool BLTouch::command(const BLTCommand cmd, const millis_t &ms) {
  38. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("BLTouch Command :", cmd);
  39. servo[Z_PROBE_SERVO_NR].move(cmd);
  40. safe_delay(_MAX(ms, (uint32_t)BLTOUCH_DELAY)); // BLTOUCH_DELAY is also the *minimum* delay
  41. return triggered();
  42. }
  43. // Init the class and device. Call from setup().
  44. void BLTouch::init(const bool set_voltage/*=false*/) {
  45. // Voltage Setting (if enabled). At every Marlin initialization:
  46. // BLTOUCH < V3.0 and clones: This will be ignored by the probe
  47. // BLTOUCH V3.0: SET_5V_MODE or SET_OD_MODE (if enabled).
  48. // OD_MODE is the default on power on, but setting it does not hurt
  49. // This mode will stay active until manual SET_OD_MODE or power cycle
  50. // BLTOUCH V3.1: SET_5V_MODE or SET_OD_MODE (if enabled).
  51. // At power on, the probe will default to the eeprom settings configured by the user
  52. _reset();
  53. _stow();
  54. #if ENABLED(BLTOUCH_FORCE_MODE_SET)
  55. constexpr bool should_set = true;
  56. #else
  57. #ifdef DEBUG_OUT
  58. if (DEBUGGING(LEVELING)) {
  59. PGMSTR(mode0, "OD");
  60. PGMSTR(mode1, "5V");
  61. DEBUG_ECHOPGM("BLTouch Mode: ");
  62. DEBUG_ECHOPGM_P(bltouch.od_5v_mode ? mode1 : mode0);
  63. DEBUG_ECHOLNPGM(" (Default " TERN(BLTOUCH_SET_5V_MODE, "5V", "OD") ")");
  64. }
  65. #endif
  66. const bool should_set = od_5v_mode != ENABLED(BLTOUCH_SET_5V_MODE);
  67. #endif
  68. if (should_set && set_voltage)
  69. mode_conv_proc(ENABLED(BLTOUCH_SET_5V_MODE));
  70. }
  71. void BLTouch::clear() {
  72. _reset(); // RESET or RESET_SW will clear an alarm condition but...
  73. // ...it will not clear a triggered condition in SW mode when the pin is currently up
  74. // ANTClabs <-- CODE ERROR
  75. _stow(); // STOW will pull up the pin and clear any triggered condition unless it fails, don't care
  76. _deploy(); // DEPLOY to test the probe. Could fail, don't care
  77. _stow(); // STOW to be ready for meaningful work. Could fail, don't care
  78. }
  79. bool BLTouch::triggered() { return PROBE_TRIGGERED(); }
  80. bool BLTouch::deploy_proc() {
  81. // Do a DEPLOY
  82. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch DEPLOY requested");
  83. // Attempt to DEPLOY, wait for DEPLOY_DELAY or ALARM
  84. if (_deploy_query_alarm()) {
  85. // The deploy might have failed or the probe is already triggered (nozzle too low?)
  86. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch ALARM or TRIGGER after DEPLOY, recovering");
  87. clear(); // Get the probe into start condition
  88. // Last attempt to DEPLOY
  89. if (_deploy_query_alarm()) {
  90. // The deploy might have failed or the probe is actually triggered (nozzle too low?) again
  91. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch Deploy Failed");
  92. probe.probe_error_stop(); // Something is wrong, needs action, but not too bad, allow restart
  93. return true; // Tell our caller we goofed in case he cares to know
  94. }
  95. }
  96. // One of the recommended ANTClabs ways to probe, using SW MODE
  97. TERN_(BLTOUCH_FORCE_SW_MODE, _set_SW_mode());
  98. // Now the probe is ready to issue a 10ms pulse when the pin goes up.
  99. // The trigger STOW (see motion.cpp for example) will pull up the probes pin as soon as the pulse
  100. // is registered.
  101. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("bltouch.deploy_proc() end");
  102. return false; // report success to caller
  103. }
  104. bool BLTouch::stow_proc() {
  105. // Do a STOW
  106. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch STOW requested");
  107. // A STOW will clear a triggered condition in the probe (10ms pulse).
  108. // At the moment that we come in here, we might (pulse) or will (SW mode) see the trigger on the pin.
  109. // So even though we know a STOW will be ignored if an ALARM condition is active, we will STOW.
  110. // Note: If the probe is deployed AND in an ALARM condition, this STOW will not pull up the pin
  111. // and the ALARM condition will still be there. --> ANTClabs should change this behavior maybe
  112. // Attempt to STOW, wait for STOW_DELAY or ALARM
  113. if (_stow_query_alarm()) {
  114. // The stow might have failed
  115. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch ALARM or TRIGGER after STOW, recovering");
  116. _reset(); // This RESET will then also pull up the pin. If it doesn't
  117. // work and the pin is still down, there will no longer be
  118. // an ALARM condition though.
  119. // But one more STOW will catch that
  120. // Last attempt to STOW
  121. if (_stow_query_alarm()) { // so if there is now STILL an ALARM condition:
  122. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch Stow Failed");
  123. probe.probe_error_stop(); // Something is wrong, needs action, but not too bad, allow restart
  124. return true; // Tell our caller we goofed in case he cares to know
  125. }
  126. }
  127. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("bltouch.stow_proc() end");
  128. return false; // report success to caller
  129. }
  130. bool BLTouch::status_proc() {
  131. /**
  132. * Return a TRUE for "YES, it is DEPLOYED"
  133. * This function will ensure switch state is reset after execution
  134. */
  135. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch STATUS requested");
  136. _set_SW_mode(); // Incidentally, _set_SW_mode() will also RESET any active alarm
  137. const bool tr = triggered(); // If triggered in SW mode, the pin is up, it is STOWED
  138. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch is ", tr);
  139. if (tr) _stow(); else _deploy(); // Turn off SW mode, reset any trigger, honor pin state
  140. return !tr;
  141. }
  142. void BLTouch::mode_conv_proc(const bool M5V) {
  143. /**
  144. * BLTOUCH pre V3.0 and clones: No reaction at all to this sequence apart from a DEPLOY -> STOW
  145. * BLTOUCH V3.0: This will set the mode (twice) and sadly, a STOW is needed at the end, because of the deploy
  146. * BLTOUCH V3.1: This will set the mode and store it in the eeprom. The STOW is not needed but does not hurt
  147. */
  148. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch Set Mode - ", M5V);
  149. _deploy();
  150. if (M5V) _set_5V_mode(); else _set_OD_mode();
  151. _mode_store();
  152. if (M5V) _set_5V_mode(); else _set_OD_mode();
  153. _stow();
  154. od_5v_mode = M5V;
  155. }
  156. #endif // BLTOUCH