From 4edf813bde3abd32bbb5ff0525764d8f3aee9e23 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Jun 2016 01:41:57 -0700 Subject: [PATCH 1/5] Just-in-time declaration style in M48 --- Marlin/Marlin_main.cpp | 108 ++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 28324d02e..1070c3cdf 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -4201,57 +4201,41 @@ inline void gcode_M42() { return; } - double sum = 0.0, mean = 0.0, sigma = 0.0, sample_set[50]; - int8_t verbose_level = 1, n_samples = 10, n_legs = 0, schizoid_flag = 0; - - if (code_seen('V')) { - verbose_level = code_value_byte(); - if (verbose_level < 0 || verbose_level > 4) { - SERIAL_PROTOCOLPGM("?Verbose Level not plausible (0-4).\n"); - return; - } + int8_t verbose_level = code_seen('V') ? code_value_byte() : 1; + if (verbose_level < 0 || verbose_level > 4) { + SERIAL_PROTOCOLPGM("?Verbose Level not plausible (0-4).\n"); + return; } if (verbose_level > 0) SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test\n"); - if (code_seen('P')) { - n_samples = code_value_byte(); - if (n_samples < 4 || n_samples > 50) { - SERIAL_PROTOCOLPGM("?Sample size not plausible (4-50).\n"); - return; - } + int8_t n_samples = code_seen('P') ? code_value_byte() : 10; + if (n_samples < 4 || n_samples > 50) { + SERIAL_PROTOCOLPGM("?Sample size not plausible (4-50).\n"); + return; } float X_current = current_position[X_AXIS], Y_current = current_position[Y_AXIS], - Z_current = current_position[Z_AXIS], - X_probe_location = X_current + X_PROBE_OFFSET_FROM_EXTRUDER, - Y_probe_location = Y_current + Y_PROBE_OFFSET_FROM_EXTRUDER, - Z_start_location = Z_current + Z_RAISE_BEFORE_PROBING; + Z_start_location = current_position[Z_AXIS] + Z_RAISE_BEFORE_PROBING; bool deploy_probe_for_each_reading = code_seen('E'); - if (code_seen('X')) { - X_probe_location = code_value_axis_units(X_AXIS); - #if DISABLED(DELTA) - if (X_probe_location < MIN_PROBE_X || X_probe_location > MAX_PROBE_X) { - out_of_range_error(PSTR("X")); - return; - } - #endif - } + float X_probe_location = code_seen('X') ? code_value_axis_units(X_AXIS) : X_current + X_PROBE_OFFSET_FROM_EXTRUDER; + #if DISABLED(DELTA) + if (X_probe_location < MIN_PROBE_X || X_probe_location > MAX_PROBE_X) { + out_of_range_error(PSTR("X")); + return; + } + #endif - if (code_seen('Y')) { - Y_probe_location = code_value_axis_units(Y_AXIS); - #if DISABLED(DELTA) - if (Y_probe_location < MIN_PROBE_Y || Y_probe_location > MAX_PROBE_Y) { - out_of_range_error(PSTR("Y")); - return; - } - #endif - } - - #if ENABLED(DELTA) + float Y_probe_location = code_seen('Y') ? code_value_axis_units(Y_AXIS) : Y_current + Y_PROBE_OFFSET_FROM_EXTRUDER; + #if DISABLED(DELTA) + if (Y_probe_location < MIN_PROBE_Y || Y_probe_location > MAX_PROBE_Y) { + out_of_range_error(PSTR("Y")); + return; + } + #else if (sqrt(X_probe_location * X_probe_location + Y_probe_location * Y_probe_location) > DELTA_PROBEABLE_RADIUS) { SERIAL_PROTOCOLPGM("? (X,Y) location outside of probeable radius.\n"); return; @@ -4259,20 +4243,15 @@ inline void gcode_M42() { #endif bool seen_L = code_seen('L'); - - if (seen_L) { - n_legs = code_value_byte(); - if (n_legs < 0 || n_legs > 15) { - SERIAL_PROTOCOLPGM("?Number of legs in movement not plausible (0-15).\n"); - return; - } - if (n_legs == 1) n_legs = 2; + uint8_t n_legs = seen_L ? code_value_byte() : 0; + if (n_legs < 0 || n_legs > 15) { + SERIAL_PROTOCOLPGM("?Number of legs in movement not plausible (0-15).\n"); + return; } + if (n_legs == 1) n_legs = 2; - if (code_seen('S')) { - schizoid_flag++; - if (!seen_L) n_legs = 7; - } + bool schizoid_flag = code_seen('S'); + if (schizoid_flag && !seen_L) n_legs = 7; /** * Now get everything to the specified probe point So we can safely do a @@ -4307,20 +4286,21 @@ inline void gcode_M42() { raise_z_after_probing(); + randomSeed(millis()); + + double mean, sigma, sample_set[n_samples]; for (uint8_t n = 0; n < n_samples; n++) { - randomSeed(millis()); delay(500); if (n_legs) { - float radius, angle = random(0.0, 360.0); int dir = (random(0, 10) > 5.0) ? -1 : 1; // clockwise or counter clockwise - - radius = random( - #if ENABLED(DELTA) - DELTA_PROBEABLE_RADIUS / 8, DELTA_PROBEABLE_RADIUS / 3 - #else - 5, X_MAX_LENGTH / 8 - #endif - ); + float angle = random(0.0, 360.0), + radius = random( + #if ENABLED(DELTA) + DELTA_PROBEABLE_RADIUS / 8, DELTA_PROBEABLE_RADIUS / 3 + #else + 5, X_MAX_LENGTH / 8 + #endif + ); if (verbose_level > 3) { SERIAL_ECHOPAIR("Starting radius: ", radius); @@ -4404,7 +4384,7 @@ inline void gcode_M42() { /** * Get the current mean for the data points we have so far */ - sum = 0.0; + double sum = 0.0; for (uint8_t j = 0; j <= n; j++) sum += sample_set[j]; mean = sum / (n + 1); @@ -4437,19 +4417,15 @@ inline void gcode_M42() { do_blocking_move_to_z(current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS); } // End of probe loop code - // raise_z_after_probing(); - if (verbose_level > 0) { SERIAL_PROTOCOLPGM("Mean: "); SERIAL_PROTOCOL_F(mean, 6); SERIAL_EOL; - delay(25); } SERIAL_PROTOCOLPGM("Standard Deviation: "); SERIAL_PROTOCOL_F(sigma, 6); SERIAL_EOL; SERIAL_EOL; - delay(25); clean_up_after_endstop_move(); From a647b05f822278743c4f8cdc715e765372580589 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Jun 2016 03:13:34 -0700 Subject: [PATCH 2/5] Shrink some debug code in G28 --- Marlin/Marlin_main.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 1070c3cdf..84ba94786 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -3518,22 +3518,16 @@ inline void gcode_G28() { float measured_z, z_before = probePointCounter ? Z_RAISE_BETWEEN_PROBINGS + current_position[Z_AXIS] : Z_RAISE_BEFORE_PROBING + home_offset[Z_AXIS]; - if (probePointCounter) { - #if ENABLED(DEBUG_LEVELING_FEATURE) - if (DEBUGGING(LEVELING)) { - SERIAL_ECHOPAIR("z_before = (between) ", (Z_RAISE_BETWEEN_PROBINGS + current_position[Z_AXIS])); - SERIAL_EOL; - } - #endif - } - else { - #if ENABLED(DEBUG_LEVELING_FEATURE) - if (DEBUGGING(LEVELING)) { - SERIAL_ECHOPAIR("z_before = (before) ", Z_RAISE_BEFORE_PROBING + home_offset[Z_AXIS]); - SERIAL_EOL; - } - #endif - } + #if ENABLED(DEBUG_LEVELING_FEATURE) + if (DEBUGGING(LEVELING)) { + SERIAL_ECHOPGM("z_before = ("); + if (probePointCounter) + SERIAL_ECHOPGM("between) "); + else + SERIAL_ECHOPGM("before) "); + SERIAL_ECHOLN(z_before); + } + #endif #if ENABLED(DELTA) // Avoid probing the corners (outside the round or hexagon print surface) on a delta printer. From 46d3ef222368c032ca75dc11c13f6ae249ea53be Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Jun 2016 03:15:49 -0700 Subject: [PATCH 3/5] Simplify sample_set probe_pt call in M48 --- Marlin/Marlin_main.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 84ba94786..5665ba5ef 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -4362,18 +4362,18 @@ inline void gcode_M42() { /** * We don't really have to do this move, but if we don't we can see a * funny shift in the Z Height because the user might not have the - * Z_RAISE_BEFORE_PROBING height identical to the Z_RAISE_BETWEEN_PROBING + * Z_RAISE_BEFORE_PROBING height identical to the Z_RAISE_BETWEEN_PROBINGS * height. This gets us back to the probe location at the same height that * we have been running around the circle at. */ + bool last_probe = (n == n_samples - 1); do_blocking_move_to_xy(X_probe_location - (X_PROBE_OFFSET_FROM_EXTRUDER), Y_probe_location - (Y_PROBE_OFFSET_FROM_EXTRUDER)); - if (deploy_probe_for_each_reading) - sample_set[n] = probe_pt(X_probe_location, Y_probe_location, Z_RAISE_BEFORE_PROBING, ProbeDeployAndStow, verbose_level); - else { - if (n == n_samples - 1) - sample_set[n] = probe_pt(X_probe_location, Y_probe_location, Z_RAISE_BEFORE_PROBING, ProbeStow, verbose_level); else - sample_set[n] = probe_pt(X_probe_location, Y_probe_location, Z_RAISE_BEFORE_PROBING, ProbeStay, verbose_level); - } + sample_set[n] = probe_pt( + X_probe_location, Y_probe_location, + Z_RAISE_BEFORE_PROBING, + deploy_probe_for_each_reading ? ProbeDeployAndStow : last_probe ? ProbeStow : ProbeStay, + verbose_level + ); /** * Get the current mean for the data points we have so far @@ -4408,7 +4408,7 @@ inline void gcode_M42() { } if (verbose_level > 0) SERIAL_EOL; delay(50); - do_blocking_move_to_z(current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS); + do_blocking_move_to_z(current_position[Z_AXIS] + (last_probe ? Z_RAISE_AFTER_PROBING : Z_RAISE_BETWEEN_PROBINGS)); } // End of probe loop code if (verbose_level > 0) { From 1dad912b037363422a177a431a1e3dfcc6724fa8 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Jun 2016 03:16:13 -0700 Subject: [PATCH 4/5] Adjust M48 raise / move handling --- Marlin/Marlin_main.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 5665ba5ef..bac0f4202 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -4274,17 +4274,18 @@ inline void gcode_M42() { */ setup_for_endstop_move(); + // Height before each probe (except the first) + float z_before = current_position[Z_AXIS] + (deploy_probe_for_each_reading ? Z_RAISE_BEFORE_PROBING : Z_RAISE_BETWEEN_PROBINGS); + + // Deploy the probe and probe the first point probe_pt(X_probe_location, Y_probe_location, Z_RAISE_BEFORE_PROBING, deploy_probe_for_each_reading ? ProbeDeployAndStow : ProbeDeploy, verbose_level); - raise_z_after_probing(); - randomSeed(millis()); double mean, sigma, sample_set[n_samples]; for (uint8_t n = 0; n < n_samples; n++) { - delay(500); if (n_legs) { int dir = (random(0, 10) > 5.0) ? -1 : 1; // clockwise or counter clockwise float angle = random(0.0, 360.0), @@ -4359,18 +4360,13 @@ inline void gcode_M42() { } // n_legs loop } // n_legs - /** - * We don't really have to do this move, but if we don't we can see a - * funny shift in the Z Height because the user might not have the - * Z_RAISE_BEFORE_PROBING height identical to the Z_RAISE_BETWEEN_PROBINGS - * height. This gets us back to the probe location at the same height that - * we have been running around the circle at. - */ + // The last probe will differ bool last_probe = (n == n_samples - 1); - do_blocking_move_to_xy(X_probe_location - (X_PROBE_OFFSET_FROM_EXTRUDER), Y_probe_location - (Y_PROBE_OFFSET_FROM_EXTRUDER)); + + // Probe a single point sample_set[n] = probe_pt( X_probe_location, Y_probe_location, - Z_RAISE_BEFORE_PROBING, + z_before, deploy_probe_for_each_reading ? ProbeDeployAndStow : last_probe ? ProbeStow : ProbeStay, verbose_level ); @@ -4392,6 +4388,7 @@ inline void gcode_M42() { sum += ss * ss; } sigma = sqrt(sum / (n + 1)); + if (verbose_level > 1) { SERIAL_PROTOCOL(n + 1); SERIAL_PROTOCOLPGM(" of "); @@ -4407,9 +4404,14 @@ inline void gcode_M42() { } } if (verbose_level > 0) SERIAL_EOL; - delay(50); - do_blocking_move_to_z(current_position[Z_AXIS] + (last_probe ? Z_RAISE_AFTER_PROBING : Z_RAISE_BETWEEN_PROBINGS)); - } // End of probe loop code + + // Raise before the next loop for the legs + if (n_legs || last_probe) { + do_blocking_move_to_z(last_probe ? Z_RAISE_AFTER_PROBING : z_before); + if (!last_probe) delay(500); + } + + } // End of probe loop if (verbose_level > 0) { SERIAL_PROTOCOLPGM("Mean: "); From b7e3c1bbc4245a8f9a5ee4b1e63c7e06971bb85f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Jun 2016 15:38:15 -0700 Subject: [PATCH 5/5] Tweak M48 output conditions --- Marlin/Marlin_main.cpp | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index bac0f4202..3934c8ebe 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -4275,10 +4275,11 @@ inline void gcode_M42() { setup_for_endstop_move(); // Height before each probe (except the first) - float z_before = current_position[Z_AXIS] + (deploy_probe_for_each_reading ? Z_RAISE_BEFORE_PROBING : Z_RAISE_BETWEEN_PROBINGS); + float z_between = home_offset[Z_AXIS] + (deploy_probe_for_each_reading ? Z_RAISE_BEFORE_PROBING : Z_RAISE_BETWEEN_PROBINGS); // Deploy the probe and probe the first point - probe_pt(X_probe_location, Y_probe_location, Z_RAISE_BEFORE_PROBING, + probe_pt(X_probe_location, Y_probe_location, + home_offset[Z_AXIS] + Z_RAISE_BEFORE_PROBING, deploy_probe_for_each_reading ? ProbeDeployAndStow : ProbeDeploy, verbose_level); @@ -4366,7 +4367,7 @@ inline void gcode_M42() { // Probe a single point sample_set[n] = probe_pt( X_probe_location, Y_probe_location, - z_before, + z_between, deploy_probe_for_each_reading ? ProbeDeployAndStow : last_probe ? ProbeStow : ProbeStay, verbose_level ); @@ -4388,26 +4389,28 @@ inline void gcode_M42() { sum += ss * ss; } sigma = sqrt(sum / (n + 1)); - - if (verbose_level > 1) { - SERIAL_PROTOCOL(n + 1); - SERIAL_PROTOCOLPGM(" of "); - SERIAL_PROTOCOL((int)n_samples); - SERIAL_PROTOCOLPGM(" z: "); - SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6); - delay(50); - if (verbose_level > 2) { - SERIAL_PROTOCOLPGM(" mean: "); - SERIAL_PROTOCOL_F(mean, 6); - SERIAL_PROTOCOLPGM(" sigma: "); - SERIAL_PROTOCOL_F(sigma, 6); + if (verbose_level > 0) { + if (verbose_level > 1) { + SERIAL_PROTOCOL(n + 1); + SERIAL_PROTOCOLPGM(" of "); + SERIAL_PROTOCOL((int)n_samples); + SERIAL_PROTOCOLPGM(" z: "); + SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6); + delay(50); + if (verbose_level > 2) { + SERIAL_PROTOCOLPGM(" mean: "); + SERIAL_PROTOCOL_F(mean, 6); + SERIAL_PROTOCOLPGM(" sigma: "); + SERIAL_PROTOCOL_F(sigma, 6); + } } + SERIAL_EOL; } - if (verbose_level > 0) SERIAL_EOL; - // Raise before the next loop for the legs + // Raise before the next loop for the legs, + // or do the final raise after the last probe if (n_legs || last_probe) { - do_blocking_move_to_z(last_probe ? Z_RAISE_AFTER_PROBING : z_before); + do_blocking_move_to_z(last_probe ? home_offset[Z_AXIS] + Z_RAISE_AFTER_PROBING : z_between); if (!last_probe) delay(500); }