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.

M420.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "../gcode.h"
  25. #include "../../feature/bedlevel/bedlevel.h"
  26. #include "../../module/planner.h"
  27. #include "../../module/probe.h"
  28. #if ENABLED(EEPROM_SETTINGS)
  29. #include "../../module/settings.h"
  30. #endif
  31. #if ENABLED(EXTENSIBLE_UI)
  32. #include "../../lcd/extui/ui_api.h"
  33. #endif
  34. //#define M420_C_USE_MEAN
  35. /**
  36. * M420: Enable/Disable Bed Leveling and/or set the Z fade height.
  37. *
  38. * S[bool] Turns leveling on or off
  39. * Z[height] Sets the Z fade height (0 or none to disable)
  40. * V[bool] Verbose - Print the leveling grid
  41. *
  42. * With AUTO_BED_LEVELING_UBL only:
  43. *
  44. * L[index] Load UBL mesh from index (0 is default)
  45. * T[map] 0:Human-readable 1:CSV 2:"LCD" 4:Compact
  46. *
  47. * With mesh-based leveling only:
  48. *
  49. * C Center mesh on the mean of the lowest and highest
  50. *
  51. * With MARLIN_DEV_MODE:
  52. * S2 Create a simple random mesh and enable
  53. */
  54. void GcodeSuite::M420() {
  55. const bool seen_S = parser.seen('S'),
  56. to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
  57. #if ENABLED(MARLIN_DEV_MODE)
  58. if (parser.intval('S') == 2) {
  59. const float x_min = probe.min_x(), x_max = probe.max_x(),
  60. y_min = probe.min_y(), y_max = probe.max_y();
  61. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  62. xy_pos_t start, spacing;
  63. start.set(x_min, y_min);
  64. spacing.set((x_max - x_min) / (GRID_MAX_CELLS_X),
  65. (y_max - y_min) / (GRID_MAX_CELLS_Y));
  66. bedlevel.set_grid(spacing, start);
  67. #endif
  68. GRID_LOOP(x, y) {
  69. bedlevel.z_values[x][y] = 0.001 * random(-200, 200);
  70. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, bedlevel.z_values[x][y]));
  71. }
  72. TERN_(AUTO_BED_LEVELING_BILINEAR, bedlevel.refresh_bed_level());
  73. SERIAL_ECHOPGM("Simulated " STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh ");
  74. SERIAL_ECHOPGM(" (", x_min);
  75. SERIAL_CHAR(','); SERIAL_ECHO(y_min);
  76. SERIAL_ECHOPGM(")-(", x_max);
  77. SERIAL_CHAR(','); SERIAL_ECHO(y_max);
  78. SERIAL_ECHOLNPGM(")");
  79. }
  80. #endif
  81. xyz_pos_t oldpos = current_position;
  82. // If disabling leveling do it right away
  83. // (Don't disable for just M420 or M420 V)
  84. if (seen_S && !to_enable) set_bed_leveling_enabled(false);
  85. #if ENABLED(AUTO_BED_LEVELING_UBL)
  86. // L to load a mesh from the EEPROM
  87. if (parser.seen('L')) {
  88. set_bed_leveling_enabled(false);
  89. #if ENABLED(EEPROM_SETTINGS)
  90. const int8_t storage_slot = parser.has_value() ? parser.value_int() : bedlevel.storage_slot;
  91. const int16_t a = settings.calc_num_meshes();
  92. if (!a) {
  93. SERIAL_ECHOLNPGM("?EEPROM storage not available.");
  94. return;
  95. }
  96. if (!WITHIN(storage_slot, 0, a - 1)) {
  97. SERIAL_ECHOLNPGM("?Invalid storage slot.");
  98. SERIAL_ECHOLNPGM("?Use 0 to ", a - 1);
  99. return;
  100. }
  101. settings.load_mesh(storage_slot);
  102. bedlevel.storage_slot = storage_slot;
  103. #else
  104. SERIAL_ECHOLNPGM("?EEPROM storage not available.");
  105. return;
  106. #endif
  107. }
  108. // L or V display the map info
  109. if (parser.seen("LV")) {
  110. bedlevel.display_map(parser.byteval('T'));
  111. SERIAL_ECHOPGM("Mesh is ");
  112. if (!bedlevel.mesh_is_valid()) SERIAL_ECHOPGM("in");
  113. SERIAL_ECHOLNPGM("valid\nStorage slot: ", bedlevel.storage_slot);
  114. }
  115. #endif // AUTO_BED_LEVELING_UBL
  116. const bool seenV = parser.seen_test('V');
  117. #if HAS_MESH
  118. if (leveling_is_valid()) {
  119. // Subtract the given value or the mean from all mesh values
  120. if (parser.seen('C')) {
  121. const float cval = parser.value_float();
  122. #if ENABLED(AUTO_BED_LEVELING_UBL)
  123. set_bed_leveling_enabled(false);
  124. bedlevel.adjust_mesh_to_mean(true, cval);
  125. #else
  126. #if ENABLED(M420_C_USE_MEAN)
  127. // Get the sum and average of all mesh values
  128. float mesh_sum = 0;
  129. GRID_LOOP(x, y) mesh_sum += bedlevel.z_values[x][y];
  130. const float zmean = mesh_sum / float(GRID_MAX_POINTS);
  131. #else // midrange
  132. // Find the low and high mesh values.
  133. float lo_val = 100, hi_val = -100;
  134. GRID_LOOP(x, y) {
  135. const float z = bedlevel.z_values[x][y];
  136. NOMORE(lo_val, z);
  137. NOLESS(hi_val, z);
  138. }
  139. // Get the midrange plus C value. (The median may be better.)
  140. const float zmean = (lo_val + hi_val) / 2.0 + cval;
  141. #endif
  142. // If not very close to 0, adjust the mesh
  143. if (!NEAR_ZERO(zmean)) {
  144. set_bed_leveling_enabled(false);
  145. // Subtract the mean from all values
  146. GRID_LOOP(x, y) {
  147. bedlevel.z_values[x][y] -= zmean;
  148. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, bedlevel.z_values[x][y]));
  149. }
  150. TERN_(AUTO_BED_LEVELING_BILINEAR, bedlevel.refresh_bed_level());
  151. }
  152. #endif
  153. }
  154. }
  155. else if (to_enable || seenV) {
  156. SERIAL_ECHO_MSG("Invalid mesh.");
  157. goto EXIT_M420;
  158. }
  159. #endif // HAS_MESH
  160. // V to print the matrix or mesh
  161. if (seenV) {
  162. #if ABL_PLANAR
  163. planner.bed_level_matrix.debug(F("Bed Level Correction Matrix:"));
  164. #else
  165. if (leveling_is_valid()) {
  166. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  167. bedlevel.print_leveling_grid();
  168. #elif ENABLED(MESH_BED_LEVELING)
  169. SERIAL_ECHOLNPGM("Mesh Bed Level data:");
  170. bedlevel.report_mesh();
  171. #endif
  172. }
  173. #endif
  174. }
  175. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  176. if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
  177. #endif
  178. // Enable leveling if specified, or if previously active
  179. set_bed_leveling_enabled(to_enable);
  180. #if HAS_MESH
  181. EXIT_M420:
  182. #endif
  183. // Error if leveling failed to enable or reenable
  184. if (to_enable && !planner.leveling_active)
  185. SERIAL_ERROR_MSG(STR_ERR_M420_FAILED);
  186. SERIAL_ECHO_START();
  187. SERIAL_ECHOPGM("Bed Leveling ");
  188. serialprintln_onoff(planner.leveling_active);
  189. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  190. SERIAL_ECHO_START();
  191. SERIAL_ECHOPGM("Fade Height ");
  192. if (planner.z_fade_height > 0.0)
  193. SERIAL_ECHOLN(planner.z_fade_height);
  194. else
  195. SERIAL_ECHOLNPGM(STR_OFF);
  196. #endif
  197. // Report change in position
  198. if (oldpos != current_position)
  199. report_current_position();
  200. }
  201. void GcodeSuite::M420_report(const bool forReplay/*=true*/) {
  202. report_heading_etc(forReplay, F(
  203. TERN(MESH_BED_LEVELING, "Mesh Bed Leveling", TERN(AUTO_BED_LEVELING_UBL, "Unified Bed Leveling", "Auto Bed Leveling"))
  204. ));
  205. SERIAL_ECHOF(
  206. F(" M420 S"), planner.leveling_active
  207. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  208. , FPSTR(SP_Z_STR), LINEAR_UNIT(planner.z_fade_height)
  209. #endif
  210. , F(" ; Leveling ")
  211. );
  212. serialprintln_onoff(planner.leveling_active);
  213. }
  214. #endif // HAS_LEVELING