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

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