From 7e5ac3f38f814c51ae77d278af619a58b8fda92b Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 17 Nov 2025 14:17:26 -0600 Subject: [PATCH] common tramming header --- Marlin/src/lcd/e3v2/proui/dwin.cpp | 5 +- .../dgus_e3s1pro/DGUSReturnKeyCodeHandler.cpp | 5 +- .../lcd/extui/dgus_reloaded/DGUSRxHandler.cpp | 35 ++--- .../lcd/extui/ia_creality/ia_creality_rts.cpp | 5 +- Marlin/src/lcd/menu/menu_bed_tramming.cpp | 98 ++----------- Marlin/src/lcd/tramming.h | 130 ++++++++++++++++++ 6 files changed, 159 insertions(+), 119 deletions(-) create mode 100644 Marlin/src/lcd/tramming.h diff --git a/Marlin/src/lcd/e3v2/proui/dwin.cpp b/Marlin/src/lcd/e3v2/proui/dwin.cpp index 4245beb9c6..47c7f78f35 100644 --- a/Marlin/src/lcd/e3v2/proui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/proui/dwin.cpp @@ -38,6 +38,7 @@ #include "../../utf8.h" #include "../../marlinui.h" +#include "../../tramming.h" #include "../../extui/ui_api.h" #include "../../../MarlinCore.h" #include "../../../module/temperature.h" @@ -2430,14 +2431,12 @@ void setFlow() { setPIntOnClick(FLOW_EDIT_MIN, FLOW_EDIT_MAX, []{ planner.refres void tramXY(const uint8_t point, float &x, float &y) { #if ENABLED(BED_TRAMMING_USE_PROBE) constexpr float slop = 0.01f; - float lfrb[] = { + const float lfrb[4] = { (X_MIN_BED) + probe.min_x() + slop, (Y_MIN_BED) + probe.min_y() + slop, (X_MAX_BED) - probe.max_x() - slop, (Y_MAX_BED) - probe.max_y() - slop }; - #else - constexpr float lfrb[] = BED_TRAMMING_INSET_LFRB; #endif switch (point) { diff --git a/Marlin/src/lcd/extui/dgus_e3s1pro/DGUSReturnKeyCodeHandler.cpp b/Marlin/src/lcd/extui/dgus_e3s1pro/DGUSReturnKeyCodeHandler.cpp index a643e4078c..34d48d3f3a 100644 --- a/Marlin/src/lcd/extui/dgus_e3s1pro/DGUSReturnKeyCodeHandler.cpp +++ b/Marlin/src/lcd/extui/dgus_e3s1pro/DGUSReturnKeyCodeHandler.cpp @@ -31,6 +31,7 @@ #include "config/DGUS_Screen.h" #include "../ui_api.h" +#include "../../tramming.h" #include "../../../core/language.h" #include "../../../module/temperature.h" #include "../../../module/printcounter.h" @@ -396,14 +397,12 @@ void DGUSReturnKeyCodeHandler::Command_SettingsMenu(DGUS_VP &vp, void *data) { static void _gotoTrammingPoint(unsigned char point) { #if ENABLED(BED_TRAMMING_USE_PROBE) - float lfrb[4] = { + const float lfrb[4] = { (X_MIN_BED) + probe.min_x(), (Y_MIN_BED) + probe.min_y(), (X_MAX_BED) - probe.max_x(), (Y_MAX_BED) - probe.max_y() }; - #else - constexpr float lfrb[4] = BED_TRAMMING_INSET_LFRB; #endif float x, y; diff --git a/Marlin/src/lcd/extui/dgus_reloaded/DGUSRxHandler.cpp b/Marlin/src/lcd/extui/dgus_reloaded/DGUSRxHandler.cpp index ad800a9451..36a90c0ac6 100644 --- a/Marlin/src/lcd/extui/dgus_reloaded/DGUSRxHandler.cpp +++ b/Marlin/src/lcd/extui/dgus_reloaded/DGUSRxHandler.cpp @@ -30,6 +30,7 @@ #include "config/DGUS_ScreenID.h" #include "../ui_api.h" +#include "../../tramming.h" #include "../../../core/language.h" #include "../../../module/temperature.h" #include "../../../module/printcounter.h" @@ -80,6 +81,7 @@ void DGUSRxHandler::screenChange(DGUS_VP &vp, void *data_ptr) { } #if HAS_MEDIA + void DGUSRxHandler::scroll(DGUS_VP &vp, void *data_ptr) { UNUSED(vp); @@ -87,25 +89,22 @@ void DGUSRxHandler::screenChange(DGUS_VP &vp, void *data_ptr) { switch (scroll) { case DGUS_Data::Scroll::GO_BACK: - if (screen.filelist.isAtRootDir()) { + if (screen.filelist.isAtRootDir()) return; - } screen.filelist_offset = 0; screen.filelist_selected = -1; screen.filelist.upDir(); break; case DGUS_Data::Scroll::UP: - if (screen.filelist_offset < 1) { + if (screen.filelist_offset < 1) return; - } --screen.filelist_offset; break; case DGUS_Data::Scroll::DOWN: - if (screen.filelist_offset + 1 + DGUS_FILE_COUNT > screen.filelist.count()) { + if (screen.filelist_offset + 1 + DGUS_FILE_COUNT > screen.filelist.count()) return; - } ++screen.filelist_offset; break; @@ -119,18 +118,16 @@ void DGUSRxHandler::screenChange(DGUS_VP &vp, void *data_ptr) { const uint8_t index = ((uint8_t*)data_ptr)[1]; - if (!screen.filelist.seek(screen.filelist_offset + index)) { + if (!screen.filelist.seek(screen.filelist_offset + index)) return; - } if (screen.filelist.isDir()) { screen.filelist_offset = 0; screen.filelist_selected = -1; screen.filelist.changeDir(screen.filelist.filename()); } - else { + else screen.filelist_selected = screen.filelist_offset + index; - } screen.triggerFullUpdate(); } @@ -144,10 +141,8 @@ void DGUSRxHandler::screenChange(DGUS_VP &vp, void *data_ptr) { return; } - if (!screen.filelist.seek(screen.filelist_selected) - || screen.filelist.isDir()) { + if (!screen.filelist.seek(screen.filelist_selected) || screen.filelist.isDir()) return; - } if (!screen.isPrinterIdle()) { screen.setStatusMessage(GET_TEXT_F(DGUS_MSG_BUSY)); @@ -157,6 +152,7 @@ void DGUSRxHandler::screenChange(DGUS_VP &vp, void *data_ptr) { ExtUI::printFile(screen.filelist.shortFilename()); screen.triggerScreenChange(DGUS_ScreenID::PRINT_STATUS); } + #endif // HAS_MEDIA void DGUSRxHandler::printAbort(DGUS_VP &vp, void *data_ptr) { @@ -164,9 +160,8 @@ void DGUSRxHandler::printAbort(DGUS_VP &vp, void *data_ptr) { const DGUS_Data::Popup result = (DGUS_Data::Popup)((uint8_t*)data_ptr)[1]; - if (result != DGUS_Data::Popup::CONFIRMED) { + if (result != DGUS_Data::Popup::CONFIRMED) return; - } if (!ExtUI::isPrinting() && !ExtUI::isPrintingPaused()) { screen.triggerFullUpdate(); @@ -181,9 +176,8 @@ void DGUSRxHandler::printPause(DGUS_VP &vp, void *data_ptr) { const DGUS_Data::Popup result = (DGUS_Data::Popup)((uint8_t*)data_ptr)[1]; - if (result != DGUS_Data::Popup::CONFIRMED) { + if (result != DGUS_Data::Popup::CONFIRMED) return; - } if (!ExtUI::isPrinting()) { screen.triggerFullUpdate(); @@ -198,9 +192,8 @@ void DGUSRxHandler::printResume(DGUS_VP &vp, void *data_ptr) { const DGUS_Data::Popup result = (DGUS_Data::Popup)((uint8_t*)data_ptr)[1]; - if (result != DGUS_Data::Popup::CONFIRMED) { + if (result != DGUS_Data::Popup::CONFIRMED) return; - } if (!ExtUI::isPrintingPaused()) { screen.triggerFullUpdate(); @@ -469,14 +462,12 @@ void DGUSRxHandler::moveToPoint(DGUS_VP &vp, void *data_ptr) { const uint8_t point = ((uint8_t*)data_ptr)[1]; #if ENABLED(BED_TRAMMING_USE_PROBE) - float lfrb[4] = { + const float lfrb[4] = { (X_MIN_BED) + probe.min_x(), (Y_MIN_BED) + probe.min_y(), (X_MAX_BED) - probe.max_x(), (Y_MAX_BED) - probe.max_y() }; - #else - constexpr float lfrb[4] = BED_TRAMMING_INSET_LFRB; #endif float x, y; diff --git a/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp b/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp index 8bdacd92eb..46d116c488 100644 --- a/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp +++ b/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp @@ -42,6 +42,7 @@ using namespace ExtUI; #include // for memset +#include "../../tramming.h" #if ENABLED(BED_TRAMMING_USE_PROBE) #include "../../../module/probe.h" #endif @@ -695,14 +696,12 @@ void RTS::handleData() { } #if ENABLED(BED_TRAMMING_USE_PROBE) - float lfrb[4] = { + const float lfrb[4] = { (X_MIN_BED) + probe.min_x(), (Y_MIN_BED) + probe.min_y(), (X_MAX_BED) - probe.max_x(), (Y_MAX_BED) - probe.max_y() }; - #else - constexpr float lfrb[4] = BED_TRAMMING_INSET_LFRB; #endif switch (Checkkey) { diff --git a/Marlin/src/lcd/menu/menu_bed_tramming.cpp b/Marlin/src/lcd/menu/menu_bed_tramming.cpp index 42f591a351..02cde36dc1 100644 --- a/Marlin/src/lcd/menu/menu_bed_tramming.cpp +++ b/Marlin/src/lcd/menu/menu_bed_tramming.cpp @@ -40,114 +40,36 @@ #define NEEDS_PROBE_DEPLOY 1 #endif +#include "../tramming.h" + #if ENABLED(BED_TRAMMING_USE_PROBE) #include "../../module/probe.h" #include "../../module/endstops.h" #if ENABLED(BLTOUCH) #include "../../feature/bltouch.h" #endif - #ifndef BED_TRAMMING_PROBE_TOLERANCE - #define BED_TRAMMING_PROBE_TOLERANCE 0.2 + #if HAS_MARLINUI_U8GLIB + #include "../dogm/marlinui_DOGM.h" #endif float last_z; int good_points; bool tramming_done, wait_for_probe; - - #if HAS_MARLINUI_U8GLIB - #include "../dogm/marlinui_DOGM.h" - #endif - #define GOOD_POINTS_TO_STR(N) ui8tostr2(N) - #define LAST_Z_TO_STR(N) ftostr53_63(N) //ftostr42_52(N) - #endif -static_assert(BED_TRAMMING_Z_HOP >= 0, "BED_TRAMMING_Z_HOP must be >= 0. Please update your configuration."); - -#define LF 1 -#define RF 2 -#define RB 3 -#define LB 4 - -#ifndef BED_TRAMMING_LEVELING_ORDER - #define BED_TRAMMING_LEVELING_ORDER { LF, RF, LB, RB } // Default - //#define BED_TRAMMING_LEVELING_ORDER { LF, LB, RF } // 3 hard-coded points - //#define BED_TRAMMING_LEVELING_ORDER { LF, RF } // 3-Point tramming - Rear - //#define BED_TRAMMING_LEVELING_ORDER { LF, LB } // 3-Point tramming - Right - //#define BED_TRAMMING_LEVELING_ORDER { RF, RB } // 3-Point tramming - Left - //#define BED_TRAMMING_LEVELING_ORDER { LB, RB } // 3-Point tramming - Front -#endif - -constexpr int lco[] = BED_TRAMMING_LEVELING_ORDER; -constexpr bool tramming_3_points = COUNT(lco) == 2; -static_assert(tramming_3_points || COUNT(lco) == 4, "BED_TRAMMING_LEVELING_ORDER must have exactly 2 or 4 corners."); - -constexpr int lcodiff = ABS(lco[0] - lco[1]); -static_assert(COUNT(lco) == 4 || lcodiff == 1 || lcodiff == 3, "The first two BED_TRAMMING_LEVELING_ORDER corners must be on the same edge."); - -constexpr int nr_edge_points = tramming_3_points ? 3 : 4; -constexpr int available_points = nr_edge_points + ENABLED(BED_TRAMMING_INCLUDE_CENTER); -constexpr int center_index = TERN(BED_TRAMMING_INCLUDE_CENTER, available_points - 1, -1); static int8_t bed_corner; /** * Move to the next corner coordinates */ static void _lcd_goto_next_corner() { - #if ENABLED(BED_TRAMMING_USE_PROBE) - constexpr float slop = 0.01f; - const xy_pos_t lf = { (X_MIN_BED) + probe.min_x() + slop, (Y_MIN_BED) + probe.min_y() + slop }, - rb = { (X_MAX_BED) - probe.max_x() - slop, (Y_MAX_BED) - probe.max_y() - slop }; - #else - constexpr float inset_lfrb[4] = BED_TRAMMING_INSET_LFRB; - constexpr xy_pos_t lf { (X_MIN_BED) + inset_lfrb[0], (Y_MIN_BED) + inset_lfrb[1] }, - rb { (X_MAX_BED) - inset_lfrb[2], (Y_MAX_BED) - inset_lfrb[3] }; - #endif - - xy_pos_t corner_point = lf; // Left front + xy_pos_t tram_point = tram_point_by_index(bed_corner); if (tramming_3_points) { if (bed_corner >= available_points) bed_corner = 0; // Above max position -> move back to first corner - switch (bed_corner) { - case 0 ... 1: - // First two corners set explicitly by the configuration - switch (lco[bed_corner]) { - case RF: corner_point.x = rb.x; break; // Right Front - case RB: corner_point = rb; break; // Right Back - case LB: corner_point.y = rb.y; break; // Left Back - } - break; - - case 2: - // Determine which edge to probe for 3rd point - corner_point.set(lf.x + (rb.x - lf.x) / 2, lf.y + (rb.y - lf.y) / 2); - if ((lco[0] == LB && lco[1] == RB) || (lco[0] == RB && lco[1] == LB)) corner_point.y = lf.y; // Front Center - if ((lco[0] == LF && lco[1] == LB) || (lco[0] == LB && lco[1] == LF)) corner_point.x = rb.x; // Center Right - if ((lco[0] == RF && lco[1] == RB) || (lco[0] == RB && lco[1] == RF)) corner_point.x = lf.x; // Left Center - if ((lco[0] == LF && lco[1] == RF) || (lco[0] == RF && lco[1] == LF)) corner_point.y = rb.y; // Center Back - #if ENABLED(BED_TRAMMING_USE_PROBE) && DISABLED(BED_TRAMMING_INCLUDE_CENTER) - bed_corner++; // Must increment the count to ensure it resets the loop if the 3rd point is out of tolerance - #endif - break; - - #if ENABLED(BED_TRAMMING_INCLUDE_CENTER) - case 3: - corner_point.set(X_CENTER, Y_CENTER); - break; - #endif - } - } - else { - // Four-Corner Bed Tramming with optional center - if (TERN0(BED_TRAMMING_INCLUDE_CENTER, bed_corner == center_index)) { - corner_point.set(X_CENTER, Y_CENTER); - } - else { - switch (lco[bed_corner]) { - case RF: corner_point.x = rb.x; break; // Right Front - case RB: corner_point = rb; break; // Right Back - case LB: corner_point.y = rb.y; break; // Left Back - } - } + #if ENABLED(BED_TRAMMING_USE_PROBE) && DISABLED(BED_TRAMMING_INCLUDE_CENTER) + // Must increment the count to ensure it resets the loop if the 3rd point is out of tolerance + if (bed_corner == 2) bed_corner++; + #endif } float z = _MIN(current_position.z + (BED_TRAMMING_Z_HOP), Z_MAX_POS); @@ -155,7 +77,7 @@ static void _lcd_goto_next_corner() { z += bltouch.z_extra_clearance(); #endif line_to_z(z); - do_blocking_move_to_xy(DIFF_TERN(BED_TRAMMING_USE_PROBE, corner_point, probe.offset_xy), manual_feedrate_mm_s.x); + do_blocking_move_to_xy(DIFF_TERN(BED_TRAMMING_USE_PROBE, tram_point, probe.offset_xy), manual_feedrate_mm_s.x); #if DISABLED(BED_TRAMMING_USE_PROBE) line_to_z(BED_TRAMMING_HEIGHT); if (++bed_corner >= available_points) bed_corner = 0; diff --git a/Marlin/src/lcd/tramming.h b/Marlin/src/lcd/tramming.h new file mode 100644 index 0000000000..d64143b68c --- /dev/null +++ b/Marlin/src/lcd/tramming.h @@ -0,0 +1,130 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2025 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 . + * + */ +#pragma once + +#include "../inc/MarlinConfigPre.h" + +#if ENABLED(BED_TRAMMING_USE_PROBE) + #include "../module/probe.h" + #ifndef BED_TRAMMING_PROBE_TOLERANCE + #if ENABLED(DWIN_LCD_PROUI) + #define BED_TRAMMING_PROBE_TOLERANCE 0.05f + #endif + #if ENABLED(DWIN_LCD_PROUI) + #define BED_TRAMMING_PROBE_TOLERANCE 0.2f + #endif + #endif + #define GOOD_POINTS_TO_STR(N) ui8tostr2(N) + #define LAST_Z_TO_STR(N) ftostr53_63(N) //ftostr42_52(N) +#else + constexpr float lfrb[4] = BED_TRAMMING_INSET_LFRB; + constexpr xy_pos_t lf { (X_MIN_BED) + lfrb[0], (Y_MIN_BED) + lfrb[1] }, + rb { (X_MAX_BED) - lfrb[2], (Y_MAX_BED) - lfrb[3] }; +#endif + +static_assert(BED_TRAMMING_Z_HOP >= 0, "BED_TRAMMING_Z_HOP must be >= 0. Please update your configuration."); + +enum NamedPlace : uint8_t { NOPOINT = 0, LF, RF, RB, LB, FC, RC, LC, BC, CC }; + +#ifndef BED_TRAMMING_LEVELING_ORDER + #define BED_TRAMMING_LEVELING_ORDER { LF, RF, LB, RB } // Default + //#define BED_TRAMMING_LEVELING_ORDER { LF, LB, RF } // 3 hard-coded points + //#define BED_TRAMMING_LEVELING_ORDER { LF, RF } // 3-Point tramming - Rear + //#define BED_TRAMMING_LEVELING_ORDER { LF, LB } // 3-Point tramming - Right + //#define BED_TRAMMING_LEVELING_ORDER { RF, RB } // 3-Point tramming - Left + //#define BED_TRAMMING_LEVELING_ORDER { LB, RB } // 3-Point tramming - Front +#endif + +constexpr int8_t lco[] = BED_TRAMMING_LEVELING_ORDER; +constexpr bool tramming_3_points = COUNT(lco) == 2; +static_assert(tramming_3_points || COUNT(lco) == 4, "BED_TRAMMING_LEVELING_ORDER must have exactly 2 or 4 corners."); +constexpr bool corners_are(NamedPlace a, NamedPlace b) { + return (lco[0] == a && lco[1] == b) || (lco[0] == b && lco[1] == a); +} + +constexpr int8_t lcodiff = ABS(lco[0] - lco[1]); +static_assert(COUNT(lco) == 4 || lcodiff == 1 || lcodiff == 3, "The first two BED_TRAMMING_LEVELING_ORDER corners must be on the same edge."); + +constexpr int8_t nr_edge_points = tramming_3_points ? 3 : 4; +constexpr int8_t available_points = nr_edge_points + ENABLED(BED_TRAMMING_INCLUDE_CENTER); +constexpr int8_t center_index = TERN(BED_TRAMMING_INCLUDE_CENTER, available_points - 1, -1); + +// Center of the opposite edge for the 3rd point +static inline xy_pos_t third_point() { + #if ENABLED(BED_TRAMMING_USE_PROBE) + constexpr float slop = 0.01f; + const xy_pos_t lf = { (X_MIN_BED) + probe.min_x() + slop, (Y_MIN_BED) + probe.min_y() + slop }, + rb = { (X_MAX_BED) - probe.max_x() - slop, (Y_MAX_BED) - probe.max_y() - slop }; + #endif + if (corners_are(LB, RB)) + return xy_pos_t({ (lf.x + rb.x) / 2, lf.y }); // Front Center + if (corners_are(LF, LB)) + return xy_pos_t({ rb.x, (lf.y + rb.y) / 2 }); // Right Center + if (corners_are(RF, RB)) + return xy_pos_t({ lf.x, (lf.y + rb.y) / 2 }); // Left Center + //if (corners_are(LF, RF)) + return xy_pos_t({ (lf.x + rb.x) / 2, rb.y }); // Back Center +} + +inline static xy_pos_t tram_point_by_index(const uint8_t i) { + #if ENABLED(BED_TRAMMING_USE_PROBE) + constexpr float slop = 0.01f; + const xy_pos_t lf = { (X_MIN_BED) + probe.min_x() + slop, (Y_MIN_BED) + probe.min_y() + slop }, + rb = { (X_MAX_BED) - probe.max_x() - slop, (Y_MAX_BED) - probe.max_y() - slop }; + #endif + if (tramming_3_points) { + switch (i) { + case 0 ... 1: + // First two corners set explicitly by the configuration + switch (lco[i]) { + case LF: return lf; // Left Front + case RF: return xy_pos_t({ rb.x, lf.y }); // Right Front + case RB: return rb; // Right Back + case LB: return xy_pos_t({ lf.x, rb.y }); // Left Back + } + break; + + // 3rd point is based on the other two + case 2: return third_point(); + + // 4th point is optional center + #if ENABLED(BED_TRAMMING_INCLUDE_CENTER) + case 3: return xy_pos_t({ X_CENTER, Y_CENTER }); + #endif + } + } + else { + // Four-Corner Bed Tramming with optional center + if (TERN0(BED_TRAMMING_INCLUDE_CENTER, i == center_index)) { + return xy_pos_t({ X_CENTER, Y_CENTER }); + } + else { + switch (lco[i]) { + case LF: return lf; // Left Front + case RF: return xy_pos_t({ rb.x, lf.y }); // Right Front + case RB: return rb; // Right Back + case LB: return xy_pos_t({ lf.x, rb.y }); // Left Back + } + } + } + return lf; +}