From ec159cd67757cb42f95b3e25ef25733ced59b922 Mon Sep 17 00:00:00 2001
From: DerAndere <26200979+DerAndere1@users.noreply.github.com>
Date: Thu, 3 Oct 2024 12:39:53 +0200
Subject: [PATCH] Add G43, G43.4 and G49
---
.../src/gcode/feature/fwretract/G10_G11.cpp | 4 +-
Marlin/src/gcode/gcode.cpp | 5 +
Marlin/src/gcode/geometry/G43_G49.cpp | 101 ++++++++++++++++++
Marlin/src/inc/Conditionals-5-post.h | 2 +-
Marlin/src/module/motion.cpp | 2 +-
Marlin/src/module/tool_change.cpp | 8 +-
ini/features.ini | 1 +
7 files changed, 118 insertions(+), 5 deletions(-)
create mode 100644 Marlin/src/gcode/geometry/G43_G49.cpp
diff --git a/Marlin/src/gcode/feature/fwretract/G10_G11.cpp b/Marlin/src/gcode/feature/fwretract/G10_G11.cpp
index c5346789e2a2..bba5bb18070a 100644
--- a/Marlin/src/gcode/feature/fwretract/G10_G11.cpp
+++ b/Marlin/src/gcode/feature/fwretract/G10_G11.cpp
@@ -70,7 +70,7 @@ void GcodeSuite::G10() {
default: break; // Ignore unknown G10 Lx
- case 1: // Sets the tool offset
+ case 1: // Sets the tool offset. Intended to work as in LinuxCNC.
LOOP_NUM_AXES(i) {
if (parser.seen(axis_codes[i])) {
const float axis_value = parser.value_axis_units((AxisEnum)i);
@@ -86,7 +86,7 @@ void GcodeSuite::G10() {
// G10 L10 changes the tool table entry for tool P so that if the tool offset is reloaded, with the machine in its
// current position and with the current G5x and G52/G92 offsets active, the current coordinates for the given axes
- // will become the given values. (Intended to work the same as LinuxCNC).
+ // will become the given values. Intended to work as in LinuxCNC.
// e.g. G10 L10 P1 Z1.5 followed by G43 sets the current position for Z to be 1.5.
case 10:
LOOP_NUM_AXES(i) {
diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp
index 3f6a7e156a76..1f28accd3d6d 100644
--- a/Marlin/src/gcode/gcode.cpp
+++ b/Marlin/src/gcode/gcode.cpp
@@ -433,6 +433,11 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 42: G42(); break; // G42: Coordinated move to a mesh point
#endif
+ #if HAS_TOOL_LENGTH_COMPENSATION
+ case 43: G43(); break; // G43.4: Rotational Tool Center Point Control Mode
+ case 49: G49(); break;
+ #endif
+
#if ENABLED(CNC_COORDINATE_SYSTEMS)
case 53: G53(); break; // G53: (prefix) Apply native workspace
case 54: G54(); break; // G54: Switch to Workspace 1
diff --git a/Marlin/src/gcode/geometry/G43_G49.cpp b/Marlin/src/gcode/geometry/G43_G49.cpp
new file mode 100644
index 000000000000..97a03d137147
--- /dev/null
+++ b/Marlin/src/gcode/geometry/G43_G49.cpp
@@ -0,0 +1,101 @@
+/**
+ * Marlin2ForPipetBot [https://github.com/DerAndere1/Marlin]
+ * Copyright 2019 - 2024 DerAndere and other Marlin2ForPipetBot authors [https://github.com/DerAndere1/Marlin]
+ *
+ * Based on:
+ * 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 .
+ *
+ */
+
+
+/**
+ * @file G43_G49.cpp
+ * @author DerAndere
+ * @brief G-codes related to Tool Length Compensation and Rotational Tool Center Point Control
+ *
+ * Copyright 2024 DerAndere
+ */
+
+#include "../../inc/MarlinConfig.h"
+
+#if HAS_TOOL_LENGTH_COMPENSATION
+
+#include "../gcode.h"
+#include "../../module/motion.h"
+
+/**
+ * G43: Enable Tool Length Compensation.
+ *
+ * G43: Enable Simple Tool Length Compensation.
+ * G43 Tool Length Compensation can be canceled with G49.
+ *
+ * G43.4: Enable Rotational Tool Center Point Control Mode.
+ * G43.4 Rotational Tool Center Point Control Mode can be canceled with G49.
+ *
+ * Only one can be active at any time.
+ */
+void GcodeSuite::G43() {
+
+ #if USE_GCODE_SUBCODES
+ const uint8_t subcode_G43 = parser.subcode;
+ #else
+ constexpr uint8_t subcode_G43 = 0;
+ #endif
+
+ switch (subcode_G43) {
+ default: return; // Ignore unknown G43.x
+
+ case 0: // G43 - Simple Tool Length Compensation Mode.
+ #if HAS_TOOL_CENTERPOINT_CONTROL
+ tool_centerpoint_control = false;
+ simple_tool_length_compensation = true;
+ #endif
+ break;
+
+ #if HAS_TOOL_CENTERPOINT_CONTROL
+ case 4: // G43.4 - Rotational Tool Center Point Control Mode.
+ simple_tool_length_compensation = false;
+ tool_centerpoint_control = true;
+ break;
+ #endif
+ }
+
+ current_position += hotend_offset[active_extruder];
+ sync_plan_position();
+}
+
+
+/**
+ * G49: Cancel Tool Length Compensation
+ *
+ * Cancels Simple Tool Length Compensation Mode and Rotational Tool Center Point Control Mode.
+ *
+ * Simple Tool Length Compensation Mode can be enabled with G43
+ * Rotational Tool Center Point Control Mode can be enabled with G43.4
+ */
+void GcodeSuite::G49() {
+ simple_tool_length_compensation = false;
+ tool_centerpoint_control = false;
+
+ current_position -= hotend_offset[active_extruder];
+ sync_plan_position();
+}
+
+#endif
diff --git a/Marlin/src/inc/Conditionals-5-post.h b/Marlin/src/inc/Conditionals-5-post.h
index ae16d18a5412..d959815feb52 100644
--- a/Marlin/src/inc/Conditionals-5-post.h
+++ b/Marlin/src/inc/Conditionals-5-post.h
@@ -3411,7 +3411,7 @@
#endif
// G92 shifts the workspace
-#if DISABLED(NO_WORKSPACE_OFFSETS)
+#if DISABLED(NO_WORKSPACE_OFFSETS) || HAS_TOOL_LENGTH_COMPENSATION
#define HAS_WORKSPACE_OFFSET 1
#endif
#if DISABLED(NO_HOME_OFFSETS)
diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp
index 93bff7738710..1eba6e4f171c 100644
--- a/Marlin/src/module/motion.cpp
+++ b/Marlin/src/module/motion.cpp
@@ -189,7 +189,7 @@ xyz_pos_t cartes;
#if ANY(PENTA_AXIS_TRT, PENTA_AXIS_HT)
- bool tool_centerpoint_control = true;
+ bool tool_centerpoint_control = false;
#endif
#endif
diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp
index e68f1ce76aae..2db4b177a844 100644
--- a/Marlin/src/module/tool_change.cpp
+++ b/Marlin/src/module/tool_change.cpp
@@ -1386,7 +1386,13 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
#endif
#if HAS_HOTEND_OFFSET
- xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
+ #if HAS_TOOL_LENGTH_COMPENSATION
+ xyz_pos_t diff{0};
+ if (simple_tool_length_compensation || tool_centerpoint_control)
+ diff = hotend_offset[new_tool] - hotend_offset[old_tool];
+ #else
+ const xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
+ #endif
TERN_(DUAL_X_CARRIAGE, diff.x = 0);
#else
constexpr xyz_pos_t diff{0};
diff --git a/ini/features.ini b/ini/features.ini
index 16f73b80b4a0..250f4be23f60 100644
--- a/ini/features.ini
+++ b/ini/features.ini
@@ -319,6 +319,7 @@ FILAMENT_LOAD_UNLOAD_GCODES = build_src_filter=+
CNC_WORKSPACE_PLANES = build_src_filter=+
CNC_COORDINATE_SYSTEMS = build_src_filter=+
+HAS_TOOL_LENGTH_COMPENSATION = src_filter=+ +
HAS_HOME_OFFSET = build_src_filter=+
EXPECTED_PRINTER_CHECK = build_src_filter=+
HOST_KEEPALIVE_FEATURE = build_src_filter=+