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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <http://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::last_written_mode; // Initialized by settings.load, 0 = Open Drain; 1 = 5V Drain
  27. #include "../module/servo.h"
  28. void stop();
  29. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  30. #include "../core/debug_out.h"
  31. bool BLTouch::command(const BLTCommand cmd, const millis_t &ms) {
  32. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("BLTouch Command :", cmd);
  33. MOVE_SERVO(Z_PROBE_SERVO_NR, cmd);
  34. safe_delay(_MAX(ms, (uint32_t)BLTOUCH_DELAY)); // BLTOUCH_DELAY is also the *minimum* delay
  35. return triggered();
  36. }
  37. // Init the class and device. Call from setup().
  38. void BLTouch::init(const bool set_voltage/*=false*/) {
  39. // Voltage Setting (if enabled). At every Marlin initialization:
  40. // BLTOUCH < V3.0 and clones: This will be ignored by the probe
  41. // BLTOUCH V3.0: SET_5V_MODE or SET_OD_MODE (if enabled).
  42. // OD_MODE is the default on power on, but setting it does not hurt
  43. // This mode will stay active until manual SET_OD_MODE or power cycle
  44. // BLTOUCH V3.1: SET_5V_MODE or SET_OD_MODE (if enabled).
  45. // At power on, the probe will default to the eeprom settings configured by the user
  46. _reset();
  47. _stow();
  48. #if ENABLED(BLTOUCH_FORCE_MODE_SET)
  49. constexpr bool should_set = true;
  50. #else
  51. if (DEBUGGING(LEVELING)) {
  52. DEBUG_ECHOLNPAIR("last_written_mode - ", (int)last_written_mode);
  53. DEBUG_ECHOLNPGM("config mode - "
  54. #if ENABLED(BLTOUCH_SET_5V_MODE)
  55. "BLTOUCH_SET_5V_MODE"
  56. #else
  57. "OD"
  58. #endif
  59. );
  60. }
  61. const bool should_set = last_written_mode != ENABLED(BLTOUCH_SET_5V_MODE);
  62. #endif
  63. if (should_set && set_voltage)
  64. mode_conv_proc(ENABLED(BLTOUCH_SET_5V_MODE));
  65. }
  66. void BLTouch::clear() {
  67. _reset(); // RESET or RESET_SW will clear an alarm condition but...
  68. // ...it will not clear a triggered condition in SW mode when the pin is currently up
  69. // ANTClabs <-- CODE ERROR
  70. _stow(); // STOW will pull up the pin and clear any triggered condition unless it fails, don't care
  71. _deploy(); // DEPLOY to test the probe. Could fail, don't care
  72. _stow(); // STOW to be ready for meaningful work. Could fail, don't care
  73. }
  74. bool BLTouch::triggered() {
  75. return (
  76. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  77. READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING
  78. #else
  79. READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING
  80. #endif
  81. );
  82. }
  83. bool BLTouch::deploy_proc() {
  84. // Do a DEPLOY
  85. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch DEPLOY requested");
  86. // Attempt to DEPLOY, wait for DEPLOY_DELAY or ALARM
  87. if (_deploy_query_alarm()) {
  88. // The deploy might have failed or the probe is already triggered (nozzle too low?)
  89. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch ALARM or TRIGGER after DEPLOY, recovering");
  90. clear(); // Get the probe into start condition
  91. // Last attempt to DEPLOY
  92. if (_deploy_query_alarm()) {
  93. // The deploy might have failed or the probe is actually triggered (nozzle too low?) again
  94. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch Recovery Failed");
  95. SERIAL_ERROR_MSG(STR_STOP_BLTOUCH); // Tell the user something is wrong, needs action
  96. stop(); // but it's not too bad, no need to kill, allow restart
  97. return true; // Tell our caller we goofed in case he cares to know
  98. }
  99. }
  100. // One of the recommended ANTClabs ways to probe, using SW MODE
  101. TERN_(BLTOUCH_FORCE_SW_MODE, _set_SW_mode());
  102. // Now the probe is ready to issue a 10ms pulse when the pin goes up.
  103. // The trigger STOW (see motion.cpp for example) will pull up the probes pin as soon as the pulse
  104. // is registered.
  105. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("bltouch.deploy_proc() end");
  106. return false; // report success to caller
  107. }
  108. bool BLTouch::stow_proc() {
  109. // Do a STOW
  110. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch STOW requested");
  111. // A STOW will clear a triggered condition in the probe (10ms pulse).
  112. // At the moment that we come in here, we might (pulse) or will (SW mode) see the trigger on the pin.
  113. // So even though we know a STOW will be ignored if an ALARM condition is active, we will STOW.
  114. // Note: If the probe is deployed AND in an ALARM condition, this STOW will not pull up the pin
  115. // and the ALARM condition will still be there. --> ANTClabs should change this behavior maybe
  116. // Attempt to STOW, wait for STOW_DELAY or ALARM
  117. if (_stow_query_alarm()) {
  118. // The stow might have failed
  119. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch ALARM or TRIGGER after STOW, recovering");
  120. _reset(); // This RESET will then also pull up the pin. If it doesn't
  121. // work and the pin is still down, there will no longer be
  122. // an ALARM condition though.
  123. // But one more STOW will catch that
  124. // Last attempt to STOW
  125. if (_stow_query_alarm()) { // so if there is now STILL an ALARM condition:
  126. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch Recovery Failed");
  127. SERIAL_ERROR_MSG(STR_STOP_BLTOUCH); // Tell the user something is wrong, needs action
  128. stop(); // but it's not too bad, no need to kill, allow restart
  129. return true; // Tell our caller we goofed in case he cares to know
  130. }
  131. }
  132. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("bltouch.stow_proc() end");
  133. return false; // report success to caller
  134. }
  135. bool BLTouch::status_proc() {
  136. /**
  137. * Return a TRUE for "YES, it is DEPLOYED"
  138. * This function will ensure switch state is reset after execution
  139. */
  140. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("BLTouch STATUS requested");
  141. _set_SW_mode(); // Incidentally, _set_SW_mode() will also RESET any active alarm
  142. const bool tr = triggered(); // If triggered in SW mode, the pin is up, it is STOWED
  143. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("BLTouch is ", (int)tr);
  144. if (tr) _stow(); else _deploy(); // Turn off SW mode, reset any trigger, honor pin state
  145. return !tr;
  146. }
  147. void BLTouch::mode_conv_proc(const bool M5V) {
  148. /**
  149. * BLTOUCH pre V3.0 and clones: No reaction at all to this sequence apart from a DEPLOY -> STOW
  150. * BLTOUCH V3.0: This will set the mode (twice) and sadly, a STOW is needed at the end, because of the deploy
  151. * BLTOUCH V3.1: This will set the mode and store it in the eeprom. The STOW is not needed but does not hurt
  152. */
  153. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("BLTouch Set Mode - ", (int)M5V);
  154. _deploy();
  155. if (M5V) _set_5V_mode(); else _set_OD_mode();
  156. _mode_store();
  157. if (M5V) _set_5V_mode(); else _set_OD_mode();
  158. _stow();
  159. last_written_mode = M5V;
  160. }
  161. #endif // BLTOUCH