From ac50a355a37673e521a052dc2d38d991144b4d05 Mon Sep 17 00:00:00 2001 From: Rui Caridade Date: Sun, 21 Jun 2020 10:27:51 +0100 Subject: [PATCH] G35: Bed tramming assistant (#16897) --- Marlin/Configuration_adv.h | 28 +++++ Marlin/src/gcode/bedlevel/G35.cpp | 192 ++++++++++++++++++++++++++++++ Marlin/src/gcode/gcode.cpp | 4 + Marlin/src/gcode/gcode.h | 3 + buildroot/share/tests/DUE-tests | 4 +- 5 files changed, 229 insertions(+), 2 deletions(-) create mode 100755 Marlin/src/gcode/bedlevel/G35.cpp diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index d3dbc83a3e..15297f7e64 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -767,6 +767,34 @@ #define HOME_AFTER_G34 #endif +// +// Add the G35 command to read bed corners to help adjust screws. +// +//#define ASSISTED_TRAMMING +#if ENABLED(ASSISTED_TRAMMING) + + // Define positions for probing points, use the hotend as reference not the sensor. + #define TRAMMING_POINT_XY { { 20, 20 }, { 200, 20 }, { 200, 200 }, { 20, 200 } } + + // Define positions names for probing points. + #define TRAMMING_POINT_NAME_1 "Front-Left" + #define TRAMMING_POINT_NAME_2 "Front-Right" + #define TRAMMING_POINT_NAME_3 "Back-Right" + #define TRAMMING_POINT_NAME_4 "Back-Left" + + // Enable to restore leveling setup after operation + #define RESTORE_LEVELING_AFTER_G35 + + /** + * Screw thread: + * M3: 30 = Clockwise, 31 = Counter-Clockwise + * M4: 40 = Clockwise, 41 = Counter-Clockwise + * M5: 50 = Clockwise, 51 = Counter-Clockwise + */ + #define TRAMMING_SCREW_THREAD 30 + +#endif + // @section motion #define AXIS_RELATIVE_MODES { false, false, false, false } diff --git a/Marlin/src/gcode/bedlevel/G35.cpp b/Marlin/src/gcode/bedlevel/G35.cpp new file mode 100755 index 0000000000..7a42744c74 --- /dev/null +++ b/Marlin/src/gcode/bedlevel/G35.cpp @@ -0,0 +1,192 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../inc/MarlinConfig.h" + +#if ENABLED(ASSISTED_TRAMMING) + +#include "../gcode.h" +#include "../../module/planner.h" +#include "../../module/probe.h" +#include "../../feature/bedlevel/bedlevel.h" + +#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) +#include "../../core/debug_out.h" + +constexpr xy_pos_t screws_tilt_adjust_pos[] = TRAMMING_POINT_XY; + +static PGMSTR(point_name_1, TRAMMING_POINT_NAME_1); +static PGMSTR(point_name_2, TRAMMING_POINT_NAME_2); +static PGMSTR(point_name_3, TRAMMING_POINT_NAME_3); +#ifdef TRAMMING_POINT_NAME_4 + static PGMSTR(point_name_4, TRAMMING_POINT_NAME_4); + #ifdef TRAMMING_POINT_NAME_5 + static PGMSTR(point_name_5, TRAMMING_POINT_NAME_5); + #endif +#endif + +static PGM_P const tramming_point_name[] PROGMEM = { + point_name_1, point_name_2, point_name_3 + #ifdef TRAMMING_POINT_NAME_4 + , point_name_4 + #ifdef TRAMMING_POINT_NAME_5 + , point_name_5 + #endif + #endif +}; + +#define G35_PROBE_COUNT COUNT(screws_tilt_adjust_pos) + +#if !WITHIN(TRAMMING_SCREW_THREAD, 30, 51) || TRAMMING_SCREW_THREAD % 10 > 1 + #error "TRAMMING_SCREW_THREAD must be equal to 30, 31, 40, 41, 50, or 51." +#endif + +static_assert(G35_PROBE_COUNT > 2, "TRAMMING_POINT_XY requires at least 3 XY positions."); + +/** + * G35: Read bed corners to help adjust bed screws + * + * S + * + * Screw thread: 30 - Clockwise M3 + * 31 - Counter-Clockwise M3 + * 40 - Clockwise M4 + * 41 - Counter-Clockwise M4 + * 50 - Clockwise M5 + * 51 - Counter-Clockwise M5 + **/ +void GcodeSuite::G35() { + if (DEBUGGING(LEVELING)) { + DEBUG_ECHOLNPGM(">>> G35"); + log_machine_info(); + } + + float z_measured[G35_PROBE_COUNT] = { 0 }; + + const uint8_t screw_thread = parser.byteval('S', TRAMMING_SCREW_THREAD); + if (!WITHIN(screw_thread, 30, 51) || screw_thread % 10 > 1) { + SERIAL_ECHOLNPGM("?(S)crew thread must be 30, 31, 40, 41, 50, or 51."); + goto EXIT_G35; + } + + // Wait for planner moves to finish! + planner.synchronize(); + + // Disable the leveling matrix before auto-aligning + #if HAS_LEVELING + TERN_(RESTORE_LEVELING_AFTER_G35, const bool leveling_was_active = planner.leveling_active); + set_bed_leveling_enabled(false); + #endif + + #if ENABLED(CNC_WORKSPACE_PLANES) + workspace_plane = PLANE_XY; + #endif + + // Always home with tool 0 active + #if HAS_MULTI_HOTEND + const uint8_t old_tool_index = active_extruder; + tool_change(0, true); + #endif + + #if HAS_DUPLICATION_MODE + extruder_duplication_enabled = false; + #endif + + // Home all before this procedure + home_all_axes(); + + bool err_break = false; + + // Probe all positions + LOOP_L_N(i, G35_PROBE_COUNT) { + + // In BLTOUCH HS mode, the probe travels in a deployed state. + // Users of G35 might have a badly misaligned bed, so raise Z by the + // length of the deployed pin (BLTOUCH stroke < 7mm) + current_position.z = (Z_CLEARANCE_BETWEEN_PROBES) + (7 * ENABLED(BLTOUCH_HS_MODE)); + + const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true); + + if (isnan(z_probed_height)) { + SERIAL_ECHOLNPAIR("G35 failed at point ", int(i), " (", tramming_point_name[i], ")" + " X", screws_tilt_adjust_pos[i].x, + " Y", screws_tilt_adjust_pos[i].y); + err_break = true; + break; + } + + if (DEBUGGING(LEVELING)) + DEBUG_ECHOLNPAIR("Probing point ", int(i), " (", tramming_point_name[i], ")" + " X", screws_tilt_adjust_pos[i].x, + " Y", screws_tilt_adjust_pos[i].y, + " Z", z_probed_height); + + z_measured[i] = z_probed_height; + } + + if (!err_break) { + const float threads_factor[] = { 0.5, 0.7, 0.8 }; + + // Calculate adjusts + LOOP_S_L_N(i, 1, G35_PROBE_COUNT) { + const float diff = z_measured[0] - z_measured[i], + adjust = abs(diff) < 0.001f ? 0 : diff / threads_factor[(screw_thread - 30) / 10]; + + const int full_turns = trunc(adjust); + const float decimal_part = adjust - float(full_turns); + const int minutes = trunc(decimal_part * 60.0f); + + SERIAL_ECHOPAIR("Turn ", tramming_point_name[i], + " ", (screw_thread & 1) == (adjust > 0) ? "Counter-Clockwise" : "Clockwise", + "by ", abs(full_turns), " turns"); + if (minutes) SERIAL_ECHOPAIR(" and ", abs(minutes), " minutes"); + SERIAL_EOL(); + } + } + else + SERIAL_ECHOLNPGM("G35 aborted."); + + // Restore the active tool after homing + #if HAS_MULTI_HOTEND + tool_change(old_tool_index, DISABLED(PARKING_EXTRUDER)); // Fetch previous toolhead if not PARKING_EXTRUDER + #endif + + #if BOTH(HAS_LEVELING, RESTORE_LEVELING_AFTER_G35) + set_bed_leveling_enabled(leveling_was_active); + #endif + + // Stow the probe, as the last call to probe.probe_at_point(...) left + // the probe deployed if it was successful. + probe.stow(); + + // After this operation the Z position needs correction + set_axis_not_trusted(Z_AXIS); + + // Home Z after the alignment procedure + process_subcommands_now_P(PSTR("G28Z")); + + EXIT_G35: + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G35"); +} + +#endif // ASSISTED_TRAMMING diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 859826ca2e..960acc3040 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -321,6 +321,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) { case 34: G34(); break; // G34: Z Stepper automatic alignment using probe #endif + #if ENABLED(ASSISTED_TRAMMING) + case 35: G35(); break; // G35: Read four bed corners to help adjust bed screws + #endif + #if ENABLED(G38_PROBE_TARGET) case 38: // G38.2, G38.3: Probe towards target if (WITHIN(parser.subcode, 2, diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 06fcce0fa3..8548f3a60c 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -65,6 +65,7 @@ * G32 - Undock sled (Z_PROBE_SLED only) * G33 - Delta Auto-Calibration (Requires DELTA_AUTO_CALIBRATION) * G34 - Z Stepper automatic alignment using probe: I T A (Requires Z_STEPPER_AUTO_ALIGN) + * G35 - Read bed corners to help adjust bed screws: T (Requires ASSISTED_TRAMMING) * G38 - Probe in any direction using the Z_MIN_PROBE (Requires G38_PROBE_TARGET) * G42 - Coordinated move to a mesh point (Requires MESH_BED_LEVELING, AUTO_BED_LEVELING_BLINEAR, or AUTO_BED_LEVELING_UBL) * G60 - Save current position. (Requires SAVED_POSITIONS) @@ -454,6 +455,8 @@ private: static void M422(); #endif + TERN_(ASSISTED_TRAMMING, static void G35()); + TERN_(G38_PROBE_TARGET, static void G38(const int8_t subcode)); TERN_(HAS_MESH, static void G42()); diff --git a/buildroot/share/tests/DUE-tests b/buildroot/share/tests/DUE-tests index 0243944cf1..5c36db0367 100755 --- a/buildroot/share/tests/DUE-tests +++ b/buildroot/share/tests/DUE-tests @@ -14,7 +14,7 @@ opt_set TEMP_SENSOR_BED 2 opt_set GRID_MAX_POINTS_X 16 opt_set FANMUX0_PIN 53 opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \ - PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING \ + FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING ASSISTED_TRAMMING \ EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \ BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \ NEOPIXEL_LED CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \ @@ -24,7 +24,7 @@ opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \ BACKLASH_COMPENSATION BACKLASH_GCODE BAUD_RATE_GCODE BEZIER_CURVE_SUPPORT \ FWRETRACT ARC_SUPPORT ARC_P_CIRCLES CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS \ PSU_CONTROL AUTO_POWER_CONTROL \ - SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER \ + PIDTEMPBED SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER \ PINS_DEBUGGING MAX7219_DEBUG M114_DETAIL \ EXTENSIBLE_UI opt_add EXTUI_EXAMPLE