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

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