Firmware2/Marlin/src/gcode/calibrate/G28.cpp

397 lines
10 KiB
C++
Raw Normal View History

/**
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
2017-09-08 22:35:25 +02:00
#include "../../inc/MarlinConfig.h"
#include "../gcode.h"
#include "../../module/stepper.h"
#include "../../module/endstops.h"
#if HOTENDS > 1
2017-09-08 22:35:25 +02:00
#include "../../module/tool_change.h"
#endif
2017-09-08 22:35:25 +02:00
#if HAS_LEVELING
#include "../../feature/bedlevel/bedlevel.h"
#endif
2018-02-21 02:44:02 +01:00
#if ENABLED(SENSORLESS_HOMING)
#include "../../feature/tmc_util.h"
#endif
#if HOMING_Z_WITH_PROBE || ENABLED(BLTOUCH)
#include "../../module/probe.h"
#endif
2017-09-08 22:35:25 +02:00
#include "../../lcd/ultralcd.h"
#if ENABLED(QUICK_HOME)
static void quick_home_xy() {
// Pretend the current position is 0,0
current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;
sync_plan_position();
const int x_axis_home_dir =
#if ENABLED(DUAL_X_CARRIAGE)
x_home_dir(active_extruder)
#else
home_dir(X_AXIS)
#endif
;
const float mlx = max_length(X_AXIS),
mly = max_length(Y_AXIS),
mlratio = mlx > mly ? mly / mlx : mlx / mly,
fr_mm_s = MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0);
2018-02-21 02:44:02 +01:00
#if ENABLED(SENSORLESS_HOMING)
sensorless_homing_per_axis(X_AXIS);
sensorless_homing_per_axis(Y_AXIS);
2018-02-21 02:44:02 +01:00
#endif
do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * home_dir(Y_AXIS), fr_mm_s);
endstops.validate_homing_move();
current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;
2018-02-21 02:44:02 +01:00
#if ENABLED(SENSORLESS_HOMING)
sensorless_homing_per_axis(X_AXIS, false);
sensorless_homing_per_axis(Y_AXIS, false);
#endif
}
#endif // QUICK_HOME
#if ENABLED(Z_SAFE_HOMING)
inline void home_z_safely() {
// Disallow Z homing if X or Y are unknown
2018-06-12 04:29:31 +02:00
if (!TEST(axis_known_position, X_AXIS) || !TEST(axis_known_position, Y_AXIS)) {
LCD_MESSAGEPGM(MSG_ERR_Z_HOMING);
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM(MSG_ERR_Z_HOMING);
return;
}
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Z_SAFE_HOMING >>>");
#endif
SYNC_PLAN_POSITION_KINEMATIC();
/**
* Move the Z probe (or just the nozzle) to the safe homing point
*/
2017-11-03 05:59:42 +01:00
destination[X_AXIS] = Z_SAFE_HOMING_X_POINT;
destination[Y_AXIS] = Z_SAFE_HOMING_Y_POINT;
destination[Z_AXIS] = current_position[Z_AXIS]; // Z is already at the right height
#if HOMING_Z_WITH_PROBE
destination[X_AXIS] -= X_PROBE_OFFSET_FROM_EXTRUDER;
destination[Y_AXIS] -= Y_PROBE_OFFSET_FROM_EXTRUDER;
#endif
2017-11-03 05:59:42 +01:00
if (position_is_reachable(destination[X_AXIS], destination[Y_AXIS])) {
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) DEBUG_POS("Z_SAFE_HOMING", destination);
#endif
// This causes the carriage on Dual X to unpark
#if ENABLED(DUAL_X_CARRIAGE)
active_extruder_parked = false;
#endif
#if ENABLED(SENSORLESS_HOMING)
safe_delay(500); // Short delay needed to settle
#endif
do_blocking_move_to_xy(destination[X_AXIS], destination[Y_AXIS]);
2018-07-01 01:13:13 +02:00
homeaxis(Z_AXIS);
}
else {
LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM(MSG_ZPROBE_OUT);
}
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< Z_SAFE_HOMING");
#endif
}
#endif // Z_SAFE_HOMING
/**
* G28: Home all axes according to settings
*
* Parameters
*
* None Home to all axes with no parameters.
* With QUICK_HOME enabled XY will home together, then Z.
*
* O Home only if position is unknown
*
* Rn Raise by n mm/inches before homing
*
* Cartesian/SCARA parameters
*
* X Home to the X endstop
* Y Home to the Y endstop
* Z Home to the Z endstop
*
*/
2017-09-08 22:35:25 +02:00
void GcodeSuite::G28(const bool always_home_all) {
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) {
2017-09-08 22:35:25 +02:00
SERIAL_ECHOLNPGM(">>> G28");
log_machine_info();
}
#endif
2018-07-03 05:27:05 +02:00
#if ENABLED(MARLIN_DEV_MODE)
if (parser.seen('S')) {
LOOP_XYZ(a) set_axis_is_at_home((AxisEnum)a);
SYNC_PLAN_POSITION_KINEMATIC();
SERIAL_ECHOLNPGM("Simulated Homing");
report_current_position();
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< G28");
#endif
return;
}
#endif
2018-06-12 04:29:31 +02:00
if (all_axes_known() && parser.boolval('O')) { // home only if needed
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) {
SERIAL_ECHOLNPGM("> homing not needed, skip");
SERIAL_ECHOLNPGM("<<< G28");
}
#endif
return;
}
// Wait for planner moves to finish!
planner.synchronize();
// Cancel the active G29 session
#if ENABLED(PROBE_MANUALLY)
g29_in_progress = false;
#endif
// Disable the leveling matrix before homing
#if HAS_LEVELING
2018-02-23 19:41:00 +01:00
#if ENABLED(RESTORE_LEVELING_AFTER_G28)
const bool leveling_was_active = planner.leveling_active;
#endif
set_bed_leveling_enabled(false);
#endif
#if ENABLED(CNC_WORKSPACE_PLANES)
workspace_plane = PLANE_XY;
#endif
#if ENABLED(BLTOUCH)
bltouch_init();
#endif
// Always home with tool 0 active
#if HOTENDS > 1
#if DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE)
const uint8_t old_tool_index = active_extruder;
#endif
tool_change(0, 0, true);
#endif
#if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
extruder_duplication_enabled = false;
#endif
setup_for_endstop_or_probe_move();
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(true)");
#endif
endstops.enable(true); // Enable endstops for next homing move
#if ENABLED(DELTA)
home_delta();
UNUSED(always_home_all);
#else // NOT DELTA
const bool homeX = always_home_all || parser.seen('X'),
homeY = always_home_all || parser.seen('Y'),
homeZ = always_home_all || parser.seen('Z'),
home_all = (!homeX && !homeY && !homeZ) || (homeX && homeY && homeZ);
set_destination_from_current();
#if Z_HOME_DIR > 0 // If homing away from BED do Z first
2018-07-01 01:13:13 +02:00
if (home_all || homeZ) homeaxis(Z_AXIS);
#endif
const float z_homing_height = (
#if ENABLED(UNKNOWN_Z_NO_RAISE)
2018-06-12 04:29:31 +02:00
!TEST(axis_known_position, Z_AXIS) ? 0 :
#endif
(parser.seenval('R') ? parser.value_linear_units() : Z_HOMING_HEIGHT)
);
2018-04-24 01:00:06 +02:00
if (z_homing_height && (home_all || homeX || homeY)) {
// Raise Z before homing any other axes and z is not already high enough (never lower z)
destination[Z_AXIS] = z_homing_height;
if (destination[Z_AXIS] > current_position[Z_AXIS]) {
#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING))
SERIAL_ECHOLNPAIR("Raise Z (before homing) to ", destination[Z_AXIS]);
#endif
do_blocking_move_to_z(destination[Z_AXIS]);
}
}
#if ENABLED(QUICK_HOME)
if (home_all || (homeX && homeY)) quick_home_xy();
#endif
// Home Y (before X)
#if ENABLED(HOME_Y_BEFORE_X)
if (home_all || homeY
#if ENABLED(CODEPENDENT_XY_HOMING)
|| homeX
#endif
2018-07-01 01:13:13 +02:00
) homeaxis(Y_AXIS);
#endif
// Home X
if (home_all || homeX
#if ENABLED(CODEPENDENT_XY_HOMING) && DISABLED(HOME_Y_BEFORE_X)
|| homeY
#endif
) {
#if ENABLED(DUAL_X_CARRIAGE)
// Always home the 2nd (right) extruder first
active_extruder = 1;
2018-07-01 01:13:13 +02:00
homeaxis(X_AXIS);
// Remember this extruder's position for later tool change
2017-11-03 05:59:42 +01:00
inactive_extruder_x_pos = current_position[X_AXIS];
// Home the 1st (left) extruder
active_extruder = 0;
2018-07-01 01:13:13 +02:00
homeaxis(X_AXIS);
// Consider the active extruder to be parked
COPY(raised_parked_position, current_position);
delayed_move_time = 0;
active_extruder_parked = true;
#else
2018-07-01 01:13:13 +02:00
homeaxis(X_AXIS);
#endif
}
// Home Y (after X)
#if DISABLED(HOME_Y_BEFORE_X)
2018-07-01 01:13:13 +02:00
if (home_all || homeY) homeaxis(Y_AXIS);
#endif
// Home Z last if homing towards the bed
#if Z_HOME_DIR < 0
if (home_all || homeZ) {
#if ENABLED(Z_SAFE_HOMING)
home_z_safely();
#else
2018-07-01 01:13:13 +02:00
homeaxis(Z_AXIS);
#endif
2018-04-30 10:25:47 +02:00
#if HOMING_Z_WITH_PROBE && defined(Z_AFTER_PROBING)
move_z_after_probing();
#endif
} // home_all || homeZ
#endif // Z_HOME_DIR < 0
SYNC_PLAN_POSITION_KINEMATIC();
2017-09-08 22:35:25 +02:00
#endif // !DELTA (G28)
endstops.not_homing();
#if ENABLED(DELTA) && ENABLED(DELTA_HOME_TO_SAFE_ZONE)
// move to a height where we can use the full xy-area
do_blocking_move_to_z(delta_clip_start_height);
#endif
2018-02-23 19:41:00 +01:00
#if ENABLED(RESTORE_LEVELING_AFTER_G28)
set_bed_leveling_enabled(leveling_was_active);
#endif
clean_up_after_endstop_or_probe_move();
// Restore the active tool after homing
#if HOTENDS > 1 && (DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE))
2017-11-26 03:45:43 +01:00
#if ENABLED(PARKING_EXTRUDER)
#define NO_FETCH false // fetch the previous toolhead
#else
#define NO_FETCH true
#endif
tool_change(old_tool_index, 0, NO_FETCH);
#endif
lcd_refresh();
report_current_position();
#if ENABLED(NANODLP_Z_SYNC)
#if ENABLED(NANODLP_ALL_AXIS)
#define _HOME_SYNC true // For any axis, output sync text.
#else
#define _HOME_SYNC (home_all || homeZ) // Only for Z-axis
#endif
if (_HOME_SYNC)
SERIAL_ECHOLNPGM(MSG_Z_MOVE_COMP);
#endif
#if ENABLED(DEBUG_LEVELING_FEATURE)
2017-09-08 22:35:25 +02:00
if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< G28");
#endif
}