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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "../gcode.h"
  25. #include "../../feature/bedlevel/bedlevel.h"
  26. #include "../../module/planner.h"
  27. #if ENABLED(EEPROM_SETTINGS)
  28. #include "../../module/configuration_store.h"
  29. #endif
  30. //#define M420_C_USE_MEAN
  31. /**
  32. * M420: Enable/Disable Bed Leveling and/or set the Z fade height.
  33. *
  34. * S[bool] Turns leveling on or off
  35. * Z[height] Sets the Z fade height (0 or none to disable)
  36. * V[bool] Verbose - Print the leveling grid
  37. *
  38. * With AUTO_BED_LEVELING_UBL only:
  39. *
  40. * L[index] Load UBL mesh from index (0 is default)
  41. * T[map] 0:Human-readable 1:CSV 2:"LCD" 4:Compact
  42. *
  43. * With mesh-based leveling only:
  44. *
  45. * C Center mesh on the mean of the lowest and highest
  46. *
  47. * With MARLIN_DEV_MODE:
  48. * S2 Create a simple random mesh and enable
  49. */
  50. void GcodeSuite::M420() {
  51. const bool seen_S = parser.seen('S'),
  52. to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
  53. #if ENABLED(MARLIN_DEV_MODE)
  54. if (parser.intval('S') == 2) {
  55. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  56. bilinear_start[X_AXIS] = MIN_PROBE_X;
  57. bilinear_start[Y_AXIS] = MIN_PROBE_Y;
  58. bilinear_grid_spacing[X_AXIS] = (MAX_PROBE_X - (MIN_PROBE_X)) / (GRID_MAX_POINTS_X - 1);
  59. bilinear_grid_spacing[Y_AXIS] = (MAX_PROBE_Y - (MIN_PROBE_Y)) / (GRID_MAX_POINTS_Y - 1);
  60. #endif
  61. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  62. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  63. Z_VALUES(x, y) = 0.001 * random(-200, 200);
  64. SERIAL_ECHOPGM("Simulated " STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_X) " mesh ");
  65. SERIAL_ECHOPAIR(" (", MIN_PROBE_X);
  66. SERIAL_CHAR(','); SERIAL_ECHO(MIN_PROBE_Y);
  67. SERIAL_ECHOPAIR(")-(", MAX_PROBE_X);
  68. SERIAL_CHAR(','); SERIAL_ECHO(MAX_PROBE_Y);
  69. SERIAL_ECHOLNPGM(")");
  70. }
  71. #endif
  72. // If disabling leveling do it right away
  73. // (Don't disable for just M420 or M420 V)
  74. if (seen_S && !to_enable) set_bed_leveling_enabled(false);
  75. const float oldpos[] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] };
  76. #if ENABLED(AUTO_BED_LEVELING_UBL)
  77. // L to load a mesh from the EEPROM
  78. if (parser.seen('L')) {
  79. set_bed_leveling_enabled(false);
  80. #if ENABLED(EEPROM_SETTINGS)
  81. const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot;
  82. const int16_t a = settings.calc_num_meshes();
  83. if (!a) {
  84. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available.");
  85. return;
  86. }
  87. if (!WITHIN(storage_slot, 0, a - 1)) {
  88. SERIAL_PROTOCOLLNPGM("?Invalid storage slot.");
  89. SERIAL_PROTOCOLLNPAIR("?Use 0 to ", a - 1);
  90. return;
  91. }
  92. settings.load_mesh(storage_slot);
  93. ubl.storage_slot = storage_slot;
  94. #else
  95. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available.");
  96. return;
  97. #endif
  98. }
  99. // L or V display the map info
  100. if (parser.seen('L') || parser.seen('V')) {
  101. ubl.display_map(parser.byteval('T'));
  102. SERIAL_ECHOPGM("Mesh is ");
  103. if (!ubl.mesh_is_valid()) SERIAL_ECHOPGM("in");
  104. SERIAL_ECHOLNPAIR("valid\nStorage slot: ", ubl.storage_slot);
  105. }
  106. #endif // AUTO_BED_LEVELING_UBL
  107. #if HAS_MESH
  108. // Subtract the given value or the mean from all mesh values
  109. if (leveling_is_valid() && parser.seen('C')) {
  110. const float cval = parser.value_float();
  111. #if ENABLED(AUTO_BED_LEVELING_UBL)
  112. set_bed_leveling_enabled(false);
  113. ubl.adjust_mesh_to_mean(true, cval);
  114. #else
  115. #if ENABLED(M420_C_USE_MEAN)
  116. // Get the sum and average of all mesh values
  117. float mesh_sum = 0;
  118. for (uint8_t x = GRID_MAX_POINTS_X; x--;)
  119. for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
  120. mesh_sum += Z_VALUES(x, y);
  121. const float zmean = mesh_sum / float(GRID_MAX_POINTS);
  122. #else
  123. // Find the low and high mesh values
  124. float lo_val = 100, hi_val = -100;
  125. for (uint8_t x = GRID_MAX_POINTS_X; x--;)
  126. for (uint8_t y = GRID_MAX_POINTS_Y; y--;) {
  127. const float z = Z_VALUES(x, y);
  128. NOMORE(lo_val, z);
  129. NOLESS(hi_val, z);
  130. }
  131. // Take the mean of the lowest and highest
  132. const float zmean = (lo_val + hi_val) / 2.0 + cval;
  133. #endif
  134. // If not very close to 0, adjust the mesh
  135. if (!NEAR_ZERO(zmean)) {
  136. set_bed_leveling_enabled(false);
  137. // Subtract the mean from all values
  138. for (uint8_t x = GRID_MAX_POINTS_X; x--;)
  139. for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
  140. Z_VALUES(x, y) -= zmean;
  141. #if ENABLED(ABL_BILINEAR_SUBDIVISION)
  142. bed_level_virt_interpolate();
  143. #endif
  144. }
  145. #endif
  146. }
  147. #endif // HAS_MESH
  148. // V to print the matrix or mesh
  149. if (parser.seen('V')) {
  150. #if ABL_PLANAR
  151. planner.bed_level_matrix.debug(PSTR("Bed Level Correction Matrix:"));
  152. #else
  153. if (leveling_is_valid()) {
  154. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  155. print_bilinear_leveling_grid();
  156. #if ENABLED(ABL_BILINEAR_SUBDIVISION)
  157. print_bilinear_leveling_grid_virt();
  158. #endif
  159. #elif ENABLED(MESH_BED_LEVELING)
  160. SERIAL_ECHOLNPGM("Mesh Bed Level data:");
  161. mbl.report_mesh();
  162. #endif
  163. }
  164. #endif
  165. }
  166. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  167. if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
  168. #endif
  169. // Enable leveling if specified, or if previously active
  170. set_bed_leveling_enabled(to_enable);
  171. // Error if leveling failed to enable or reenable
  172. if (to_enable && !planner.leveling_active) {
  173. SERIAL_ERROR_START();
  174. SERIAL_ERRORLNPGM(MSG_ERR_M420_FAILED);
  175. }
  176. SERIAL_ECHO_START();
  177. SERIAL_ECHOPGM("Bed Leveling ");
  178. serialprintln_onoff(planner.leveling_active);
  179. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  180. SERIAL_ECHO_START();
  181. SERIAL_ECHOPGM("Fade Height ");
  182. if (planner.z_fade_height > 0.0)
  183. SERIAL_ECHOLN(planner.z_fade_height);
  184. else
  185. SERIAL_ECHOLNPGM(MSG_OFF);
  186. #endif
  187. // Report change in position
  188. if (memcmp(oldpos, current_position, sizeof(oldpos)))
  189. report_current_position();
  190. }
  191. #endif // HAS_LEVELING