My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

M48.cpp 8.9KB

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