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.

M48.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST)
  24. #include "../gcode.h"
  25. #include "../../module/motion.h"
  26. #include "../../module/probe.h"
  27. #include "../../lcd/marlinui.h"
  28. #include "../../feature/bedlevel/bedlevel.h"
  29. #if HAS_LEVELING
  30. #include "../../module/planner.h"
  31. #endif
  32. /**
  33. * M48: Z probe repeatability measurement function.
  34. *
  35. * Usage:
  36. * M48 <P#> <X#> <Y#> <V#> <E> <L#> <S>
  37. * P = Number of sampled points (4-50, default 10)
  38. * X = Sample X position
  39. * Y = Sample Y position
  40. * V = Verbose level (0-4, default=1)
  41. * E = Engage Z probe for each reading
  42. * L = Number of legs of movement before probe
  43. * S = Schizoid (Or Star if you prefer)
  44. *
  45. * This function requires the machine to be homed before invocation.
  46. */
  47. extern const char SP_Y_STR[];
  48. void GcodeSuite::M48() {
  49. if (homing_needed_error()) return;
  50. const int8_t verbose_level = parser.byteval('V', 1);
  51. if (!WITHIN(verbose_level, 0, 4)) {
  52. SERIAL_ECHOLNPGM("?(V)erbose level implausible (0-4).");
  53. return;
  54. }
  55. if (verbose_level > 0)
  56. SERIAL_ECHOLNPGM("M48 Z-Probe Repeatability Test");
  57. const int8_t n_samples = parser.byteval('P', 10);
  58. if (!WITHIN(n_samples, 4, 50)) {
  59. SERIAL_ECHOLNPGM("?Sample size not plausible (4-50).");
  60. return;
  61. }
  62. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  63. // Test at the current position by default, overridden by X and Y
  64. const xy_pos_t test_position = {
  65. parser.linearval('X', current_position.x + probe.offset_xy.x), // If no X use the probe's current X position
  66. parser.linearval('Y', current_position.y + probe.offset_xy.y) // If no Y, ditto
  67. };
  68. if (!probe.can_reach(test_position)) {
  69. ui.set_status_P(GET_TEXT(MSG_M48_OUT_OF_BOUNDS), 99);
  70. SERIAL_ECHOLNPGM("? (X,Y) out of bounds.");
  71. return;
  72. }
  73. // Get the number of leg moves per test-point
  74. bool seen_L = parser.seen('L');
  75. uint8_t n_legs = seen_L ? parser.value_byte() : 0;
  76. if (n_legs > 15) {
  77. SERIAL_ECHOLNPGM("?Legs of movement implausible (0-15).");
  78. return;
  79. }
  80. if (n_legs == 1) n_legs = 2;
  81. // Schizoid motion as an optional stress-test
  82. const bool schizoid_flag = parser.boolval('S');
  83. if (schizoid_flag && !seen_L) n_legs = 7;
  84. if (verbose_level > 2)
  85. SERIAL_ECHOLNPGM("Positioning the probe...");
  86. // Always disable Bed Level correction before probing...
  87. #if HAS_LEVELING
  88. const bool was_enabled = planner.leveling_active;
  89. set_bed_leveling_enabled(false);
  90. #endif
  91. // Work with reasonable feedrates
  92. remember_feedrate_scaling_off();
  93. // Working variables
  94. float mean = 0.0, // The average of all points so far, used to calculate deviation
  95. sigma = 0.0, // Standard deviation of all points so far
  96. min = 99999.9, // Smallest value sampled so far
  97. max = -99999.9, // Largest value sampled so far
  98. sample_set[n_samples]; // Storage for sampled values
  99. auto dev_report = [](const bool verbose, const float &mean, const float &sigma, const float &min, const float &max, const bool final=false) {
  100. if (verbose) {
  101. SERIAL_ECHOPAIR_F("Mean: ", mean, 6);
  102. if (!final) SERIAL_ECHOPAIR_F(" Sigma: ", sigma, 6);
  103. SERIAL_ECHOPAIR_F(" Min: ", min, 3);
  104. SERIAL_ECHOPAIR_F(" Max: ", max, 3);
  105. SERIAL_ECHOPAIR_F(" Range: ", max-min, 3);
  106. if (final) SERIAL_EOL();
  107. }
  108. if (final) {
  109. SERIAL_ECHOLNPAIR_F("Standard Deviation: ", sigma, 6);
  110. SERIAL_EOL();
  111. }
  112. };
  113. // Move to the first point, deploy, and probe
  114. const float t = probe.probe_at_point(test_position, raise_after, verbose_level);
  115. bool probing_good = !isnan(t);
  116. if (probing_good) {
  117. randomSeed(millis());
  118. float sample_sum = 0.0;
  119. LOOP_L_N(n, n_samples) {
  120. #if HAS_WIRED_LCD
  121. // Display M48 progress in the status bar
  122. ui.status_printf_P(0, PSTR(S_FMT ": %d/%d"), GET_TEXT(MSG_M48_POINT), int(n + 1), int(n_samples));
  123. #endif
  124. // When there are "legs" of movement move around the point before probing
  125. if (n_legs) {
  126. // Pick a random direction, starting angle, and radius
  127. const int dir = (random(0, 10) > 5.0) ? -1 : 1; // clockwise or counter clockwise
  128. float angle = random(0, 360);
  129. const float radius = random(
  130. #if ENABLED(DELTA)
  131. int(0.1250000000 * (DELTA_PRINTABLE_RADIUS)),
  132. int(0.3333333333 * (DELTA_PRINTABLE_RADIUS))
  133. #else
  134. int(5), int(0.125 * _MIN(X_BED_SIZE, Y_BED_SIZE))
  135. #endif
  136. );
  137. if (verbose_level > 3) {
  138. SERIAL_ECHOPAIR("Start radius:", radius, " angle:", angle, " dir:");
  139. if (dir > 0) SERIAL_CHAR('C');
  140. SERIAL_ECHOLNPGM("CW");
  141. }
  142. // Move from leg to leg in rapid succession
  143. LOOP_L_N(l, n_legs - 1) {
  144. // Move some distance around the perimeter
  145. float delta_angle;
  146. if (schizoid_flag) {
  147. // The points of a 5 point star are 72 degrees apart.
  148. // Skip a point and go to the next one on the star.
  149. delta_angle = dir * 2.0 * 72.0;
  150. }
  151. else {
  152. // Just move further along the perimeter.
  153. delta_angle = dir * (float)random(25, 45);
  154. }
  155. angle += delta_angle;
  156. // Trig functions work without clamping, but just to be safe...
  157. while (angle > 360.0) angle -= 360.0;
  158. while (angle < 0.0) angle += 360.0;
  159. // Choose the next position as an offset to chosen test position
  160. const xy_pos_t noz_pos = test_position - probe.offset_xy;
  161. xy_pos_t next_pos = {
  162. noz_pos.x + float(cos(RADIANS(angle))) * radius,
  163. noz_pos.y + float(sin(RADIANS(angle))) * radius
  164. };
  165. #if ENABLED(DELTA)
  166. // If the probe can't reach the point on a round bed...
  167. // Simply scale the numbers to bring them closer to origin.
  168. while (!probe.can_reach(next_pos)) {
  169. next_pos *= 0.8f;
  170. if (verbose_level > 3)
  171. SERIAL_ECHOLNPAIR_P(PSTR("Moving inward: X"), next_pos.x, SP_Y_STR, next_pos.y);
  172. }
  173. #else
  174. // For a rectangular bed just keep the probe in bounds
  175. LIMIT(next_pos.x, X_MIN_POS, X_MAX_POS);
  176. LIMIT(next_pos.y, Y_MIN_POS, Y_MAX_POS);
  177. #endif
  178. if (verbose_level > 3)
  179. SERIAL_ECHOLNPAIR_P(PSTR("Going to: X"), next_pos.x, SP_Y_STR, next_pos.y);
  180. do_blocking_move_to_xy(next_pos);
  181. } // n_legs loop
  182. } // n_legs
  183. // Probe a single point
  184. const float pz = probe.probe_at_point(test_position, raise_after, 0);
  185. // Break the loop if the probe fails
  186. probing_good = !isnan(pz);
  187. if (!probing_good) break;
  188. // Store the new sample
  189. sample_set[n] = pz;
  190. // Keep track of the largest and smallest samples
  191. NOMORE(min, pz);
  192. NOLESS(max, pz);
  193. // Get the mean value of all samples thus far
  194. sample_sum += pz;
  195. mean = sample_sum / (n + 1);
  196. // Calculate the standard deviation so far.
  197. // The value after the last sample will be the final output.
  198. float dev_sum = 0.0;
  199. LOOP_LE_N(j, n) dev_sum += sq(sample_set[j] - mean);
  200. sigma = SQRT(dev_sum / (n + 1));
  201. if (verbose_level > 1) {
  202. SERIAL_ECHO(n + 1);
  203. SERIAL_ECHOPAIR(" of ", int(n_samples));
  204. SERIAL_ECHOPAIR_F(": z: ", pz, 3);
  205. dev_report(verbose_level > 2, mean, sigma, min, max);
  206. SERIAL_EOL();
  207. }
  208. } // n_samples loop
  209. }
  210. probe.stow();
  211. if (probing_good) {
  212. SERIAL_ECHOLNPGM("Finished!");
  213. dev_report(verbose_level > 0, mean, sigma, min, max, true);
  214. #if HAS_WIRED_LCD
  215. // Display M48 results in the status bar
  216. char sigma_str[8];
  217. ui.status_printf_P(0, PSTR(S_FMT ": %s"), GET_TEXT(MSG_M48_DEVIATION), dtostrf(sigma, 2, 6, sigma_str));
  218. #endif
  219. }
  220. restore_feedrate_and_scaling();
  221. // Re-enable bed level correction if it had been on
  222. TERN_(HAS_LEVELING, set_bed_leveling_enabled(was_enabled));
  223. report_current_position();
  224. }
  225. #endif // Z_MIN_PROBE_REPEATABILITY_TEST