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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #if ENABLED(MESH_BED_LEVELING) || ENABLED(PROBE_MANUALLY)
  26. #include "../../module/motion.h"
  27. #endif
  28. #if PLANNER_LEVELING
  29. #include "../../module/planner.h"
  30. #endif
  31. #if ENABLED(PROBE_MANUALLY)
  32. bool g29_in_progress = false;
  33. #endif
  34. #if ENABLED(LCD_BED_LEVELING)
  35. #include "../../lcd/ultralcd.h"
  36. #endif
  37. #if ENABLED(G26_MESH_VALIDATION)
  38. bool g26_debug_flag; // = false
  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_AXIS]
  46. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  47. true
  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. #if ENABLED(MESH_BED_LEVELING)
  68. if (!enable)
  69. planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  70. const bool enabling = enable && leveling_is_valid();
  71. planner.leveling_active = enabling;
  72. if (enabling) planner.unapply_leveling(current_position);
  73. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  74. #if PLANNER_LEVELING
  75. if (planner.leveling_active) { // leveling from on to off
  76. // change unleveled current_position to physical current_position without moving steppers.
  77. planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  78. planner.leveling_active = false; // disable only AFTER calling apply_leveling
  79. }
  80. else { // leveling from off to on
  81. planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored
  82. // change physical current_position to unleveled current_position without moving steppers.
  83. planner.unapply_leveling(current_position);
  84. }
  85. #else
  86. planner.leveling_active = enable; // just flip the bit, current_position will be wrong until next move.
  87. #endif
  88. #else // OLDSCHOOL_ABL
  89. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  90. // Force bilinear_z_offset to re-calculate next time
  91. const float reset[XYZ] = { -9999.999, -9999.999, 0 };
  92. (void)bilinear_z_offset(reset);
  93. #endif
  94. // Enable or disable leveling compensation in the planner
  95. planner.leveling_active = enable;
  96. if (!enable)
  97. // When disabling just get the current position from the steppers.
  98. // This will yield the smallest error when first converted back to steps.
  99. set_current_from_steppers_for_axis(
  100. #if ABL_PLANAR
  101. ALL_AXES
  102. #else
  103. Z_AXIS
  104. #endif
  105. );
  106. else
  107. // When enabling, remove compensation from the current position,
  108. // so compensation will give the right stepper counts.
  109. planner.unapply_leveling(current_position);
  110. #endif // OLDSCHOOL_ABL
  111. }
  112. }
  113. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  114. void set_z_fade_height(const float zfh) {
  115. const bool level_active = planner.leveling_active;
  116. #if ENABLED(AUTO_BED_LEVELING_UBL)
  117. if (level_active) set_bed_leveling_enabled(false); // turn off before changing fade height for proper apply/unapply leveling to maintain current_position
  118. #endif
  119. planner.set_z_fade_height(zfh);
  120. if (level_active) {
  121. #if ENABLED(AUTO_BED_LEVELING_UBL)
  122. set_bed_leveling_enabled(true); // turn back on after changing fade height
  123. #else
  124. set_current_from_steppers_for_axis(
  125. #if ABL_PLANAR
  126. ALL_AXES
  127. #else
  128. Z_AXIS
  129. #endif
  130. );
  131. #endif
  132. }
  133. }
  134. #endif // ENABLE_LEVELING_FADE_HEIGHT
  135. /**
  136. * Reset calibration results to zero.
  137. */
  138. void reset_bed_level() {
  139. set_bed_leveling_enabled(false);
  140. #if ENABLED(MESH_BED_LEVELING)
  141. if (leveling_is_valid()) {
  142. mbl.reset();
  143. mbl.has_mesh = false;
  144. }
  145. #else
  146. #if ENABLED(DEBUG_LEVELING_FEATURE)
  147. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("reset_bed_level");
  148. #endif
  149. #if ABL_PLANAR
  150. planner.bed_level_matrix.set_to_identity();
  151. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  152. bilinear_start[X_AXIS] = bilinear_start[Y_AXIS] =
  153. bilinear_grid_spacing[X_AXIS] = bilinear_grid_spacing[Y_AXIS] = 0;
  154. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  155. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  156. z_values[x][y] = NAN;
  157. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  158. ubl.reset();
  159. #endif
  160. #endif
  161. }
  162. #if ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(MESH_BED_LEVELING)
  163. /**
  164. * Enable to produce output in JSON format suitable
  165. * for SCAD or JavaScript mesh visualizers.
  166. *
  167. * Visualize meshes in OpenSCAD using the included script.
  168. *
  169. * buildroot/shared/scripts/MarlinMesh.scad
  170. */
  171. //#define SCAD_MESH_OUTPUT
  172. /**
  173. * Print calibration results for plotting or manual frame adjustment.
  174. */
  175. void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, element_2d_fn fn) {
  176. #ifndef SCAD_MESH_OUTPUT
  177. for (uint8_t x = 0; x < sx; x++) {
  178. for (uint8_t i = 0; i < precision + 2 + (x < 10 ? 1 : 0); i++)
  179. SERIAL_PROTOCOLCHAR(' ');
  180. SERIAL_PROTOCOL((int)x);
  181. }
  182. SERIAL_EOL();
  183. #endif
  184. #ifdef SCAD_MESH_OUTPUT
  185. SERIAL_PROTOCOLLNPGM("measured_z = ["); // open 2D array
  186. #endif
  187. for (uint8_t y = 0; y < sy; y++) {
  188. #ifdef SCAD_MESH_OUTPUT
  189. SERIAL_PROTOCOLPGM(" ["); // open sub-array
  190. #else
  191. if (y < 10) SERIAL_PROTOCOLCHAR(' ');
  192. SERIAL_PROTOCOL((int)y);
  193. #endif
  194. for (uint8_t x = 0; x < sx; x++) {
  195. SERIAL_PROTOCOLCHAR(' ');
  196. const float offset = fn(x, y);
  197. if (!isnan(offset)) {
  198. if (offset >= 0) SERIAL_PROTOCOLCHAR('+');
  199. SERIAL_PROTOCOL_F(offset, precision);
  200. }
  201. else {
  202. #ifdef SCAD_MESH_OUTPUT
  203. for (uint8_t i = 3; i < precision + 3; i++)
  204. SERIAL_PROTOCOLCHAR(' ');
  205. SERIAL_PROTOCOLPGM("NAN");
  206. #else
  207. for (uint8_t i = 0; i < precision + 3; i++)
  208. SERIAL_PROTOCOLCHAR(i ? '=' : ' ');
  209. #endif
  210. }
  211. #ifdef SCAD_MESH_OUTPUT
  212. if (x < sx - 1) SERIAL_PROTOCOLCHAR(',');
  213. #endif
  214. }
  215. #ifdef SCAD_MESH_OUTPUT
  216. SERIAL_PROTOCOLCHAR(' ');
  217. SERIAL_PROTOCOLCHAR(']'); // close sub-array
  218. if (y < sy - 1) SERIAL_PROTOCOLCHAR(',');
  219. #endif
  220. SERIAL_EOL();
  221. }
  222. #ifdef SCAD_MESH_OUTPUT
  223. SERIAL_PROTOCOLPGM("];"); // close 2D array
  224. #endif
  225. SERIAL_EOL();
  226. }
  227. #endif // AUTO_BED_LEVELING_BILINEAR || MESH_BED_LEVELING
  228. #if ENABLED(MESH_BED_LEVELING) || ENABLED(PROBE_MANUALLY)
  229. void _manual_goto_xy(const float &rx, const float &ry) {
  230. #if MANUAL_PROBE_HEIGHT > 0
  231. const float prev_z = current_position[Z_AXIS];
  232. do_blocking_move_to(rx, ry, MANUAL_PROBE_HEIGHT);
  233. do_blocking_move_to_z(prev_z);
  234. #else
  235. do_blocking_move_to_xy(rx, ry);
  236. #endif
  237. current_position[X_AXIS] = rx;
  238. current_position[Y_AXIS] = ry;
  239. #if ENABLED(LCD_BED_LEVELING)
  240. lcd_wait_for_move = false;
  241. #endif
  242. }
  243. #endif
  244. #if HAS_PROBING_PROCEDURE
  245. void out_of_range_error(const char* p_edge) {
  246. SERIAL_PROTOCOLPGM("?Probe ");
  247. serialprintPGM(p_edge);
  248. SERIAL_PROTOCOLLNPGM(" position out of range.");
  249. }
  250. #endif
  251. #endif // HAS_LEVELING