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

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