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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // Get the corrected leveled / unleveled position
  63. planner.apply_modifiers(current_position, true); // Physical position with all modifiers
  64. planner.leveling_active ^= true; // Toggle leveling between apply and unapply
  65. planner.unapply_modifiers(current_position, true); // Logical position with modifiers removed
  66. sync_plan_position();
  67. _report_leveling();
  68. }
  69. }
  70. TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(planner.leveling_active) {
  71. set_bed_leveling_enabled(enable);
  72. }
  73. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  74. void set_z_fade_height(const_float_t zfh, const bool do_report/*=true*/) {
  75. if (planner.z_fade_height == zfh) return;
  76. const bool leveling_was_active = planner.leveling_active;
  77. set_bed_leveling_enabled(false);
  78. planner.set_z_fade_height(zfh);
  79. if (leveling_was_active) {
  80. const xyz_pos_t oldpos = current_position;
  81. set_bed_leveling_enabled(true);
  82. if (do_report && oldpos != current_position)
  83. report_current_position();
  84. }
  85. }
  86. #endif // ENABLE_LEVELING_FADE_HEIGHT
  87. /**
  88. * Reset calibration results to zero.
  89. */
  90. void reset_bed_level() {
  91. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("reset_bed_level");
  92. IF_DISABLED(AUTO_BED_LEVELING_UBL, set_bed_leveling_enabled(false));
  93. TERN_(HAS_MESH, bedlevel.reset());
  94. TERN_(ABL_PLANAR, planner.bed_level_matrix.set_to_identity());
  95. }
  96. #if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)
  97. /**
  98. * Enable to produce output in JSON format suitable
  99. * for SCAD or JavaScript mesh visualizers.
  100. *
  101. * Visualize meshes in OpenSCAD using the included script.
  102. *
  103. * buildroot/shared/scripts/MarlinMesh.scad
  104. */
  105. //#define SCAD_MESH_OUTPUT
  106. /**
  107. * Print calibration results for plotting or manual frame adjustment.
  108. */
  109. void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values) {
  110. #ifndef SCAD_MESH_OUTPUT
  111. LOOP_L_N(x, sx) {
  112. serial_spaces(precision + (x < 10 ? 3 : 2));
  113. SERIAL_ECHO(x);
  114. }
  115. SERIAL_EOL();
  116. #endif
  117. #ifdef SCAD_MESH_OUTPUT
  118. SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array
  119. #endif
  120. LOOP_L_N(y, sy) {
  121. #ifdef SCAD_MESH_OUTPUT
  122. SERIAL_ECHOPGM(" ["); // open sub-array
  123. #else
  124. if (y < 10) SERIAL_CHAR(' ');
  125. SERIAL_ECHO(y);
  126. #endif
  127. LOOP_L_N(x, sx) {
  128. SERIAL_CHAR(' ');
  129. const float offset = values[x * sy + y];
  130. if (!isnan(offset)) {
  131. if (offset >= 0) SERIAL_CHAR('+');
  132. SERIAL_ECHO_F(offset, int(precision));
  133. }
  134. else {
  135. #ifdef SCAD_MESH_OUTPUT
  136. for (uint8_t i = 3; i < precision + 3; i++)
  137. SERIAL_CHAR(' ');
  138. SERIAL_ECHOPGM("NAN");
  139. #else
  140. LOOP_L_N(i, precision + 3)
  141. SERIAL_CHAR(i ? '=' : ' ');
  142. #endif
  143. }
  144. #ifdef SCAD_MESH_OUTPUT
  145. if (x < sx - 1) SERIAL_CHAR(',');
  146. #endif
  147. }
  148. #ifdef SCAD_MESH_OUTPUT
  149. SERIAL_ECHOPGM(" ]"); // close sub-array
  150. if (y < sy - 1) SERIAL_CHAR(',');
  151. #endif
  152. SERIAL_EOL();
  153. }
  154. #ifdef SCAD_MESH_OUTPUT
  155. SERIAL_ECHOPGM("];"); // close 2D array
  156. #endif
  157. SERIAL_EOL();
  158. }
  159. #endif // AUTO_BED_LEVELING_BILINEAR || MESH_BED_LEVELING
  160. #if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
  161. void _manual_goto_xy(const xy_pos_t &pos) {
  162. // Get the resting Z position for after the XY move
  163. #ifdef MANUAL_PROBE_START_Z
  164. constexpr float finalz = _MAX(0, MANUAL_PROBE_START_Z); // If a MANUAL_PROBE_START_Z value is set, always respect it
  165. #else
  166. #warning "It's recommended to set some MANUAL_PROBE_START_Z value for manual leveling."
  167. #endif
  168. #if Z_CLEARANCE_BETWEEN_MANUAL_PROBES > 0 // A probe/obstacle clearance exists so there is a raise:
  169. #ifndef MANUAL_PROBE_START_Z
  170. const float finalz = current_position.z; // - Use the current Z for starting-Z if no MANUAL_PROBE_START_Z was provided
  171. #endif
  172. do_blocking_move_to_xy_z(pos, Z_CLEARANCE_BETWEEN_MANUAL_PROBES); // - Raise Z, then move to the new XY
  173. do_blocking_move_to_z(finalz); // - Lower down to the starting Z height, ready for adjustment!
  174. #elif defined(MANUAL_PROBE_START_Z) // A starting-Z was provided, but there's no raise:
  175. do_blocking_move_to_xy_z(pos, finalz); // - Move in XY then down to the starting Z height, ready for adjustment!
  176. #else // Zero raise and no starting Z height either:
  177. do_blocking_move_to_xy(pos); // - Move over with no raise, ready for adjustment!
  178. #endif
  179. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  180. }
  181. #endif // MESH_BED_LEVELING || PROBE_MANUALLY
  182. #endif // HAS_LEVELING