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.

bedlevel.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 HAS_LEVELING
  24. #include "bedlevel.h"
  25. #include "../../module/planner.h"
  26. #if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
  27. #include "../../module/motion.h"
  28. #endif
  29. #if ENABLED(PROBE_MANUALLY)
  30. bool g29_in_progress = false;
  31. #endif
  32. #if ENABLED(LCD_BED_LEVELING)
  33. #include "../../lcd/marlinui.h"
  34. #endif
  35. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  36. #include "../../core/debug_out.h"
  37. #if ENABLED(EXTENSIBLE_UI)
  38. #include "../../lcd/extui/ui_api.h"
  39. #endif
  40. bool leveling_is_valid() {
  41. return TERN1(HAS_MESH, bedlevel.mesh_is_valid());
  42. }
  43. /**
  44. * Turn bed leveling on or off, correcting the current position.
  45. *
  46. * Disable: Current position = physical position
  47. * Enable: Current position = "unleveled" physical position
  48. */
  49. void set_bed_leveling_enabled(const bool enable/*=true*/) {
  50. const bool can_change = TERN1(AUTO_BED_LEVELING_BILINEAR, !enable || leveling_is_valid());
  51. if (can_change && enable != planner.leveling_active) {
  52. auto _report_leveling = []{
  53. if (DEBUGGING(LEVELING)) {
  54. if (planner.leveling_active)
  55. DEBUG_POS("Leveling ON", current_position);
  56. else
  57. DEBUG_POS("Leveling OFF", current_position);
  58. }
  59. };
  60. _report_leveling();
  61. planner.synchronize();
  62. if (planner.leveling_active) { // leveling from on to off
  63. // change unleveled current_position to physical current_position without moving steppers.
  64. planner.apply_leveling(current_position);
  65. planner.leveling_active = false; // disable only AFTER calling apply_leveling
  66. }
  67. else { // leveling from off to on
  68. planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored
  69. // change physical current_position to unleveled current_position without moving steppers.
  70. planner.unapply_leveling(current_position);
  71. }
  72. sync_plan_position();
  73. _report_leveling();
  74. }
  75. }
  76. TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(planner.leveling_active) {
  77. set_bed_leveling_enabled(enable);
  78. }
  79. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  80. void set_z_fade_height(const_float_t zfh, const bool do_report/*=true*/) {
  81. if (planner.z_fade_height == zfh) return;
  82. const bool leveling_was_active = planner.leveling_active;
  83. set_bed_leveling_enabled(false);
  84. planner.set_z_fade_height(zfh);
  85. if (leveling_was_active) {
  86. const xyz_pos_t oldpos = current_position;
  87. set_bed_leveling_enabled(true);
  88. if (do_report && oldpos != current_position)
  89. report_current_position();
  90. }
  91. }
  92. #endif // ENABLE_LEVELING_FADE_HEIGHT
  93. /**
  94. * Reset calibration results to zero.
  95. */
  96. void reset_bed_level() {
  97. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("reset_bed_level");
  98. IF_DISABLED(AUTO_BED_LEVELING_UBL, set_bed_leveling_enabled(false));
  99. TERN_(HAS_MESH, bedlevel.reset());
  100. TERN_(ABL_PLANAR, planner.bed_level_matrix.set_to_identity());
  101. }
  102. #if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)
  103. /**
  104. * Enable to produce output in JSON format suitable
  105. * for SCAD or JavaScript mesh visualizers.
  106. *
  107. * Visualize meshes in OpenSCAD using the included script.
  108. *
  109. * buildroot/shared/scripts/MarlinMesh.scad
  110. */
  111. //#define SCAD_MESH_OUTPUT
  112. /**
  113. * Print calibration results for plotting or manual frame adjustment.
  114. */
  115. void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values) {
  116. #ifndef SCAD_MESH_OUTPUT
  117. LOOP_L_N(x, sx) {
  118. serial_spaces(precision + (x < 10 ? 3 : 2));
  119. SERIAL_ECHO(x);
  120. }
  121. SERIAL_EOL();
  122. #endif
  123. #ifdef SCAD_MESH_OUTPUT
  124. SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array
  125. #endif
  126. LOOP_L_N(y, sy) {
  127. #ifdef SCAD_MESH_OUTPUT
  128. SERIAL_ECHOPGM(" ["); // open sub-array
  129. #else
  130. if (y < 10) SERIAL_CHAR(' ');
  131. SERIAL_ECHO(y);
  132. #endif
  133. LOOP_L_N(x, sx) {
  134. SERIAL_CHAR(' ');
  135. const float offset = values[x * sx + y];
  136. if (!isnan(offset)) {
  137. if (offset >= 0) SERIAL_CHAR('+');
  138. SERIAL_ECHO_F(offset, int(precision));
  139. }
  140. else {
  141. #ifdef SCAD_MESH_OUTPUT
  142. for (uint8_t i = 3; i < precision + 3; i++)
  143. SERIAL_CHAR(' ');
  144. SERIAL_ECHOPGM("NAN");
  145. #else
  146. LOOP_L_N(i, precision + 3)
  147. SERIAL_CHAR(i ? '=' : ' ');
  148. #endif
  149. }
  150. #ifdef SCAD_MESH_OUTPUT
  151. if (x < sx - 1) SERIAL_CHAR(',');
  152. #endif
  153. }
  154. #ifdef SCAD_MESH_OUTPUT
  155. SERIAL_ECHOPGM(" ]"); // close sub-array
  156. if (y < sy - 1) SERIAL_CHAR(',');
  157. #endif
  158. SERIAL_EOL();
  159. }
  160. #ifdef SCAD_MESH_OUTPUT
  161. SERIAL_ECHOPGM("];"); // close 2D array
  162. #endif
  163. SERIAL_EOL();
  164. }
  165. #endif // AUTO_BED_LEVELING_BILINEAR || MESH_BED_LEVELING
  166. #if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
  167. void _manual_goto_xy(const xy_pos_t &pos) {
  168. // Get the resting Z position for after the XY move
  169. #ifdef MANUAL_PROBE_START_Z
  170. constexpr float finalz = _MAX(0, MANUAL_PROBE_START_Z); // If a MANUAL_PROBE_START_Z value is set, always respect it
  171. #else
  172. #warning "It's recommended to set some MANUAL_PROBE_START_Z value for manual leveling."
  173. #endif
  174. #if Z_CLEARANCE_BETWEEN_MANUAL_PROBES > 0 // A probe/obstacle clearance exists so there is a raise:
  175. #ifndef MANUAL_PROBE_START_Z
  176. const float finalz = current_position.z; // - Use the current Z for starting-Z if no MANUAL_PROBE_START_Z was provided
  177. #endif
  178. do_blocking_move_to_xy_z(pos, Z_CLEARANCE_BETWEEN_MANUAL_PROBES); // - Raise Z, then move to the new XY
  179. do_blocking_move_to_z(finalz); // - Lower down to the starting Z height, ready for adjustment!
  180. #elif defined(MANUAL_PROBE_START_Z) // A starting-Z was provided, but there's no raise:
  181. do_blocking_move_to_xy_z(pos, finalz); // - Move in XY then down to the starting Z height, ready for adjustment!
  182. #else // Zero raise and no starting Z height either:
  183. do_blocking_move_to_xy(pos); // - Move over with no raise, ready for adjustment!
  184. #endif
  185. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  186. }
  187. #endif // MESH_BED_LEVELING || PROBE_MANUALLY
  188. #endif // HAS_LEVELING