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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(MESH_BED_LEVELING, mbl.has_mesh())
  42. && TERN1(AUTO_BED_LEVELING_BILINEAR, !!bilinear_grid_spacing.x)
  43. && TERN1(AUTO_BED_LEVELING_UBL, ubl.mesh_is_valid());
  44. }
  45. /**
  46. * Turn bed leveling on or off, fixing the current
  47. * position as-needed.
  48. *
  49. * Disable: Current position = physical position
  50. * Enable: Current position = "unleveled" physical position
  51. */
  52. void set_bed_leveling_enabled(const bool enable/*=true*/) {
  53. const bool can_change = TERN1(AUTO_BED_LEVELING_BILINEAR, !enable || leveling_is_valid());
  54. if (can_change && enable != planner.leveling_active) {
  55. planner.synchronize();
  56. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  57. // Force bilinear_z_offset to re-calculate next time
  58. const xyz_pos_t reset { -9999.999, -9999.999, 0 };
  59. (void)bilinear_z_offset(reset);
  60. #endif
  61. if (planner.leveling_active) { // leveling from on to off
  62. if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling ON", current_position);
  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. if (DEBUGGING(LEVELING)) DEBUG_POS("...Now OFF", current_position);
  67. }
  68. else { // leveling from off to on
  69. if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling OFF", current_position);
  70. planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored
  71. // change physical current_position to unleveled current_position without moving steppers.
  72. planner.unapply_leveling(current_position);
  73. if (DEBUGGING(LEVELING)) DEBUG_POS("...Now ON", current_position);
  74. }
  75. sync_plan_position();
  76. }
  77. }
  78. TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(planner.leveling_active) {
  79. set_bed_leveling_enabled(enable);
  80. }
  81. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  82. void set_z_fade_height(const_float_t zfh, const bool do_report/*=true*/) {
  83. if (planner.z_fade_height == zfh) return;
  84. const bool leveling_was_active = planner.leveling_active;
  85. set_bed_leveling_enabled(false);
  86. planner.set_z_fade_height(zfh);
  87. if (leveling_was_active) {
  88. const xyz_pos_t oldpos = current_position;
  89. set_bed_leveling_enabled(true);
  90. if (do_report && oldpos != current_position)
  91. report_current_position();
  92. }
  93. }
  94. #endif // ENABLE_LEVELING_FADE_HEIGHT
  95. /**
  96. * Reset calibration results to zero.
  97. */
  98. void reset_bed_level() {
  99. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("reset_bed_level");
  100. #if ENABLED(AUTO_BED_LEVELING_UBL)
  101. ubl.reset();
  102. #else
  103. set_bed_leveling_enabled(false);
  104. #if ENABLED(MESH_BED_LEVELING)
  105. mbl.reset();
  106. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  107. bilinear_start.reset();
  108. bilinear_grid_spacing.reset();
  109. GRID_LOOP(x, y) {
  110. z_values[x][y] = NAN;
  111. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, 0));
  112. }
  113. #elif ABL_PLANAR
  114. planner.bed_level_matrix.set_to_identity();
  115. #endif
  116. #endif
  117. }
  118. #if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)
  119. /**
  120. * Enable to produce output in JSON format suitable
  121. * for SCAD or JavaScript mesh visualizers.
  122. *
  123. * Visualize meshes in OpenSCAD using the included script.
  124. *
  125. * buildroot/shared/scripts/MarlinMesh.scad
  126. */
  127. //#define SCAD_MESH_OUTPUT
  128. /**
  129. * Print calibration results for plotting or manual frame adjustment.
  130. */
  131. void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, element_2d_fn fn) {
  132. #ifndef SCAD_MESH_OUTPUT
  133. LOOP_L_N(x, sx) {
  134. serial_spaces(precision + (x < 10 ? 3 : 2));
  135. SERIAL_ECHO(x);
  136. }
  137. SERIAL_EOL();
  138. #endif
  139. #ifdef SCAD_MESH_OUTPUT
  140. SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array
  141. #endif
  142. LOOP_L_N(y, sy) {
  143. #ifdef SCAD_MESH_OUTPUT
  144. SERIAL_ECHOPGM(" ["); // open sub-array
  145. #else
  146. if (y < 10) SERIAL_CHAR(' ');
  147. SERIAL_ECHO(y);
  148. #endif
  149. LOOP_L_N(x, sx) {
  150. SERIAL_CHAR(' ');
  151. const float offset = fn(x, y);
  152. if (!isnan(offset)) {
  153. if (offset >= 0) SERIAL_CHAR('+');
  154. SERIAL_ECHO_F(offset, int(precision));
  155. }
  156. else {
  157. #ifdef SCAD_MESH_OUTPUT
  158. for (uint8_t i = 3; i < precision + 3; i++)
  159. SERIAL_CHAR(' ');
  160. SERIAL_ECHOPGM("NAN");
  161. #else
  162. LOOP_L_N(i, precision + 3)
  163. SERIAL_CHAR(i ? '=' : ' ');
  164. #endif
  165. }
  166. #ifdef SCAD_MESH_OUTPUT
  167. if (x < sx - 1) SERIAL_CHAR(',');
  168. #endif
  169. }
  170. #ifdef SCAD_MESH_OUTPUT
  171. SERIAL_ECHOPGM(" ]"); // close sub-array
  172. if (y < sy - 1) SERIAL_CHAR(',');
  173. #endif
  174. SERIAL_EOL();
  175. }
  176. #ifdef SCAD_MESH_OUTPUT
  177. SERIAL_ECHOPGM("];"); // close 2D array
  178. #endif
  179. SERIAL_EOL();
  180. }
  181. #endif // AUTO_BED_LEVELING_BILINEAR || MESH_BED_LEVELING
  182. #if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
  183. void _manual_goto_xy(const xy_pos_t &pos) {
  184. #ifdef MANUAL_PROBE_START_Z
  185. constexpr float startz = _MAX(0, MANUAL_PROBE_START_Z);
  186. #if MANUAL_PROBE_HEIGHT > 0
  187. do_blocking_move_to_xy_z(pos, MANUAL_PROBE_HEIGHT);
  188. do_blocking_move_to_z(startz);
  189. #else
  190. do_blocking_move_to_xy_z(pos, startz);
  191. #endif
  192. #elif MANUAL_PROBE_HEIGHT > 0
  193. const float prev_z = current_position.z;
  194. do_blocking_move_to_xy_z(pos, MANUAL_PROBE_HEIGHT);
  195. do_blocking_move_to_z(prev_z);
  196. #else
  197. do_blocking_move_to_xy(pos);
  198. #endif
  199. current_position = pos;
  200. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  201. }
  202. #endif
  203. #endif // HAS_LEVELING