Improve G34, M422 (Z alignment) (#14142)

This commit is contained in:
InsanityAutomation 2019-06-18 08:02:18 -04:00 committed by Scott Lahteine
parent 391250b04f
commit bf64dd4db6
4 changed files with 137 additions and 116 deletions

View file

@ -73,11 +73,6 @@ void GcodeSuite::G34() {
do { // break out on error do { // break out on error
if (!TEST(axis_known_position, X_AXIS) || !TEST(axis_known_position, Y_AXIS)) {
SERIAL_ECHOLNPGM("Home XY first");
break;
}
const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS); const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS);
if (!WITHIN(z_auto_align_iterations, 1, 30)) { if (!WITHIN(z_auto_align_iterations, 1, 30)) {
SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30)."); SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30).");
@ -111,10 +106,6 @@ void GcodeSuite::G34() {
workspace_plane = PLANE_XY; workspace_plane = PLANE_XY;
#endif #endif
#if ENABLED(BLTOUCH)
bltouch.init();
#endif
// Always home with tool 0 active // Always home with tool 0 active
#if HOTENDS > 1 #if HOTENDS > 1
const uint8_t old_tool_index = active_extruder; const uint8_t old_tool_index = active_extruder;
@ -125,78 +116,126 @@ void GcodeSuite::G34() {
extruder_duplication_enabled = false; extruder_duplication_enabled = false;
#endif #endif
// Before moving other axes raise Z, if needed. Never lower Z. #if BOTH(BLTOUCH, BLTOUCH_HS_MODE)
if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) { // In BLTOUCH HS mode, the probe travels in a deployed state.
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Raise Z (before moving to probe pos) to ", Z_CLEARANCE_BETWEEN_PROBES); // Users of G34 might have a badly misaligned bed, so raise Z by the
do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES); // length of the deployed pin (BLTOUCH stroke < 7mm)
} #define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES + 7.0f
#else
#define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES
#endif
// 0.05 is a 5% incline. On a 300mm bed that would be a misalignment of about 1.5cm.
// This angle is the maximum misalignment catered for
#define MAX_ANGLE 0.05f
float z_probe = Z_BASIC_CLEARANCE + MAX_ANGLE * (
#if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
SQRT(MAX(HYPOT2(z_auto_align_xpos[0] - z_auto_align_ypos[0], z_auto_align_xpos[1] - z_auto_align_ypos[1]),
HYPOT2(z_auto_align_xpos[1] - z_auto_align_ypos[1], z_auto_align_xpos[2] - z_auto_align_ypos[2]),
HYPOT2(z_auto_align_xpos[2] - z_auto_align_ypos[2], z_auto_align_xpos[0] - z_auto_align_ypos[0])))
#else
HYPOT(z_auto_align_xpos[0] - z_auto_align_ypos[0], z_auto_align_xpos[1] - z_auto_align_ypos[1])
#endif
);
// Home before the alignment procedure
home_all_axes();
// Move the Z coordinate realm towards the positive - dirty trick
current_position[Z_AXIS] -= z_probe * 0.5;
// Remember corrections to determine errors on each iteration
float last_z_align_move[Z_STEPPER_COUNT] = ARRAY_N(Z_STEPPER_COUNT, 10000.0f, 10000.0f, 10000.0f), float last_z_align_move[Z_STEPPER_COUNT] = ARRAY_N(Z_STEPPER_COUNT, 10000.0f, 10000.0f, 10000.0f),
z_measured[Z_STEPPER_COUNT] = { 0 }; z_measured[Z_STEPPER_COUNT] = { 0 },
z_maxdiff = 0.0f,
amplification = z_auto_align_amplification;
uint8_t iteration;
bool err_break = false; bool err_break = false;
for (uint8_t iteration = 0; iteration < z_auto_align_iterations; ++iteration) { for (iteration = 0; iteration < z_auto_align_iterations; ++iteration) {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions.");
// Reset minimum value SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1));
float z_measured_min = 100000.0f;
// For each iteration go through all probe positions (one per Z-Stepper)
for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
#if BOTH(BLTOUCH, BLTOUCH_HS_MODE) // Initialize minimum value
// In BLTOUCH HS mode, the probe travels in a deployed state. float z_measured_min = 100000.0f;
// Users of G34 might have a badly misaligned bed, so raise Z by the // Probe all positions (one per Z-Stepper)
// length of the deployed pin (BLTOUCH stroke < 7mm) for (uint8_t izstepper = 0; izstepper < Z_STEPPER_COUNT; ++izstepper) {
do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES + 7); // iteration odd/even --> downward / upward stepper sequence
#endif const uint8_t zstepper = (iteration & 1) ? Z_STEPPER_COUNT - 1 - izstepper : izstepper;
// Safe clearance even on an incline
if (iteration == 0 || izstepper > 0) do_blocking_move_to_z(z_probe);
// Probe a Z height for each stepper // Probe a Z height for each stepper
z_measured[zstepper] = probe_pt(z_auto_align_xpos[zstepper], z_auto_align_ypos[zstepper], PROBE_PT_RAISE, false); if (isnan(probe_pt(z_auto_align_xpos[zstepper], z_auto_align_ypos[zstepper], PROBE_PT_RAISE, 0, true))) {
SERIAL_ECHOLNPGM("Probing failed.");
// Stop on error
if (isnan(z_measured[zstepper])) {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> PROBING FAILED!");
err_break = true; err_break = true;
break; break;
} }
// This is not the trigger Z value. It is the position of the probe after raising it.
// It is higher than the trigger value by a constant value (not known here). This value
// is more useful for determining the desired next iteration Z position for probing. It is
// equally well suited for determining the misalignment, just like the trigger position would be.
z_measured[zstepper] = current_position[Z_AXIS];
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " measured position is ", z_measured[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " measured position is ", z_measured[zstepper]);
// Remember the maximum position to calculate the correction // Remember the minimum measurement to calculate the correction later on
z_measured_min = MIN(z_measured_min, z_measured[zstepper]); z_measured_min = MIN(z_measured_min, z_measured[zstepper]);
} } // for (zstepper)
if (err_break) break; if (err_break) break;
// Remember the current z position to return to // Adapt the next probe clearance height based on the new measurements.
float z_original_position = current_position[Z_AXIS]; // Safe_height = lowest distance to bed (= highest measurement) plus highest measured misalignment.
#if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
z_maxdiff = MAX(ABS(z_measured[0] - z_measured[1]), ABS(z_measured[1] - z_measured[2]), ABS(z_measured[2] - z_measured[0]));
z_probe = Z_BASIC_CLEARANCE + MAX(z_measured[0], z_measured[1], z_measured[2]) + z_maxdiff;
#else
z_maxdiff = ABS(z_measured[0] - z_measured[1]);
z_probe = Z_BASIC_CLEARANCE + MAX(z_measured[0], z_measured[1]) + z_maxdiff;
#endif
SERIAL_ECHOPAIR("\n"
"DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1])
#if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
, " Z2-Z3=", ABS(z_measured[1] - z_measured[2])
, " Z3-Z1=", ABS(z_measured[2] - z_measured[0])
#endif
);
SERIAL_EOL();
SERIAL_EOL();
// The following correction actions are to be enabled for select Z-steppers only
stepper.set_separate_multi_axis(true);
// Iterations can stop early if all corrections are below required accuracy
bool success_break = true; bool success_break = true;
// Correct stepper offsets and re-iterate // Correct the individual stepper offsets
for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) { for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
stepper.set_separate_multi_axis(true);
set_all_z_lock(true); // Steppers will be enabled separately
// Calculate current stepper move // Calculate current stepper move
const float z_align_move = z_measured[zstepper] - z_measured_min, const float z_align_move = z_measured[zstepper] - z_measured_min,
z_align_abs = ABS(z_align_move); z_align_abs = ABS(z_align_move);
// Check for lost accuracy compared to last move // Optimize one iterations correction based on the first measurements
if (z_align_abs > 0.0f) amplification = iteration == 1 ? MIN(last_z_align_move[zstepper] / z_align_abs, 2.0f) : z_auto_align_amplification;
// Check for less accuracy compared to last move
if (last_z_align_move[zstepper] < z_align_abs - 1.0) { if (last_z_align_move[zstepper] < z_align_abs - 1.0) {
// Stop here SERIAL_ECHOLNPGM("Decreasing accuracy detected.");
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> detected decreasing accuracy.");
err_break = true; err_break = true;
break; break;
} }
else
last_z_align_move[zstepper] = z_align_abs;
// Only stop early if all measured points achieve accuracy target // Remember the alignment for the next iteration
last_z_align_move[zstepper] = z_align_abs;
// Stop early if all measured points achieve accuracy target
if (z_align_abs > z_auto_align_accuracy) success_break = false; if (z_align_abs > z_auto_align_accuracy) success_break = false;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " corrected by ", z_align_move); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " corrected by ", z_align_move);
// Lock all steppers except one
set_all_z_lock(true);
switch (zstepper) { switch (zstepper) {
case 0: stepper.set_z_lock(false); break; case 0: stepper.set_z_lock(false); break;
case 1: stepper.set_z2_lock(false); break; case 1: stepper.set_z2_lock(false); break;
@ -205,26 +244,25 @@ void GcodeSuite::G34() {
#endif #endif
} }
// This will lose home position and require re-homing // Do a move to correct part of the misalignment for the current stepper
do_blocking_move_to_z(z_auto_align_amplification * z_align_move + current_position[Z_AXIS]); do_blocking_move_to_z(amplification * z_align_move + current_position[Z_AXIS]);
} } // for (zstepper)
// Back to normal stepper operations
set_all_z_lock(false);
stepper.set_separate_multi_axis(false);
if (err_break) break; if (err_break) break;
// Move Z back to previous position if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); break; }
set_all_z_lock(true);
do_blocking_move_to_z(z_original_position);
set_all_z_lock(false);
stepper.set_separate_multi_axis(false); } // for (iteration)
if (success_break) { if (err_break) { SERIAL_ECHOLNPGM("G34 aborted."); break; }
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> achieved target accuracy.");
break;
}
}
if (err_break) break; SERIAL_ECHOLNPAIR("Did ", int(iteration + (iteration != z_auto_align_iterations)), " iterations of ", int(z_auto_align_iterations));
SERIAL_ECHOLNPAIR_F("Accuracy: ", z_maxdiff);
SERIAL_EOL();
// Restore the active tool after homing // Restore the active tool after homing
#if HOTENDS > 1 #if HOTENDS > 1
@ -250,7 +288,8 @@ void GcodeSuite::G34() {
bltouch._stow(); bltouch._stow();
#endif #endif
gcode.G28(false); // Home after the alignment procedure
home_all_axes();
} while(0); } while(0);

View file

@ -187,11 +187,7 @@ void GcodeSuite::dwell(millis_t time) {
/** /**
* Process the parsed command and dispatch it to its handler * Process the parsed command and dispatch it to its handler
*/ */
void GcodeSuite::process_parsed_command( void GcodeSuite::process_parsed_command(const bool no_ok) {
#if USE_EXECUTE_COMMANDS_IMMEDIATE
const bool no_ok
#endif
) {
KEEPALIVE_STATE(IN_HANDLER); KEEPALIVE_STATE(IN_HANDLER);
// Handle a known G, M, or T // Handle a known G, M, or T
@ -802,10 +798,7 @@ void GcodeSuite::process_parsed_command(
KEEPALIVE_STATE(NOT_BUSY); KEEPALIVE_STATE(NOT_BUSY);
#if USE_EXECUTE_COMMANDS_IMMEDIATE if (!no_ok) ok_to_send();
if (!no_ok)
#endif
ok_to_send();
} }
/** /**
@ -831,43 +824,39 @@ void GcodeSuite::process_next_command() {
process_parsed_command(); process_parsed_command();
} }
#if USE_EXECUTE_COMMANDS_IMMEDIATE /**
* Run a series of commands, bypassing the command queue to allow
* G-code "macros" to be called from within other G-code handlers.
*/
/** void GcodeSuite::process_subcommands_now_P(PGM_P pgcode) {
* Run a series of commands, bypassing the command queue to allow char * const saved_cmd = parser.command_ptr; // Save the parser state
* G-code "macros" to be called from within other G-code handlers. for (;;) {
*/ PGM_P const delim = strchr_P(pgcode, '\n'); // Get address of next newline
const size_t len = delim ? delim - pgcode : strlen_P(pgcode); // Get the command length
void GcodeSuite::process_subcommands_now_P(PGM_P pgcode) { char cmd[len + 1]; // Allocate a stack buffer
char * const saved_cmd = parser.command_ptr; // Save the parser state strncpy_P(cmd, pgcode, len); // Copy the command to the stack
for (;;) { cmd[len] = '\0'; // End with a nul
PGM_P const delim = strchr_P(pgcode, '\n'); // Get address of next newline parser.parse(cmd); // Parse the command
const size_t len = delim ? delim - pgcode : strlen_P(pgcode); // Get the command length process_parsed_command(true); // Process it
char cmd[len + 1]; // Allocate a stack buffer if (!delim) break; // Last command?
strncpy_P(cmd, pgcode, len); // Copy the command to the stack pgcode = delim + 1; // Get the next command
cmd[len] = '\0'; // End with a nul
parser.parse(cmd); // Parse the command
process_parsed_command(true); // Process it
if (!delim) break; // Last command?
pgcode = delim + 1; // Get the next command
}
parser.parse(saved_cmd); // Restore the parser state
} }
parser.parse(saved_cmd); // Restore the parser state
}
void GcodeSuite::process_subcommands_now(char * gcode) { void GcodeSuite::process_subcommands_now(char * gcode) {
char * const saved_cmd = parser.command_ptr; // Save the parser state char * const saved_cmd = parser.command_ptr; // Save the parser state
for (;;) { for (;;) {
char * const delim = strchr(gcode, '\n'); // Get address of next newline char * const delim = strchr(gcode, '\n'); // Get address of next newline
if (delim) *delim = '\0'; // Replace with nul if (delim) *delim = '\0'; // Replace with nul
parser.parse(gcode); // Parse the current command parser.parse(gcode); // Parse the current command
process_parsed_command(true); // Process it process_parsed_command(true); // Process it
if (!delim) break; // Last command? if (!delim) break; // Last command?
gcode = delim + 1; // Get the next command gcode = delim + 1; // Get the next command
}
parser.parse(saved_cmd); // Restore the parser state
} }
parser.parse(saved_cmd); // Restore the parser state
#endif // USE_EXECUTE_COMMANDS_IMMEDIATE }
#if ENABLED(HOST_KEEPALIVE_FEATURE) #if ENABLED(HOST_KEEPALIVE_FEATURE)

View file

@ -312,19 +312,14 @@ public:
static int8_t get_target_e_stepper_from_command(); static int8_t get_target_e_stepper_from_command();
static void get_destination_from_command(); static void get_destination_from_command();
static void process_parsed_command( static void process_parsed_command(const bool no_ok=false);
#if USE_EXECUTE_COMMANDS_IMMEDIATE
const bool no_ok = false
#endif
);
static void process_next_command(); static void process_next_command();
#if USE_EXECUTE_COMMANDS_IMMEDIATE // Execute G-code as a macro, preserving parser state
static void process_subcommands_now_P(PGM_P pgcode); static void process_subcommands_now_P(PGM_P pgcode);
static void process_subcommands_now(char * gcode); static void process_subcommands_now(char * gcode);
#endif
FORCE_INLINE static void home_all_axes() { G28(true); } static inline void home_all_axes() { process_subcommands_now_P(PSTR("G28")); }
#if ENABLED(HOST_KEEPALIVE_FEATURE) #if ENABLED(HOST_KEEPALIVE_FEATURE)
/** /**

View file

@ -1700,8 +1700,6 @@
// If platform requires early initialization of watchdog to properly boot // If platform requires early initialization of watchdog to properly boot
#define EARLY_WATCHDOG (ENABLED(USE_WATCHDOG) && defined(ARDUINO_ARCH_SAM)) #define EARLY_WATCHDOG (ENABLED(USE_WATCHDOG) && defined(ARDUINO_ARCH_SAM))
#define USE_EXECUTE_COMMANDS_IMMEDIATE (ANY(G29_RETRY_AND_RECOVER, GCODE_MACROS, POWER_LOSS_RECOVERY) || HAS_DRIVER(L6470))
#if ENABLED(Z_TRIPLE_STEPPER_DRIVERS) #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
#define Z_STEPPER_COUNT 3 #define Z_STEPPER_COUNT 3
#elif ENABLED(Z_DUAL_STEPPER_DRIVERS) #elif ENABLED(Z_DUAL_STEPPER_DRIVERS)