Skip to content

Commit 1666dbf

Browse files
committed
✨ EDITABLE_HOMING_CURRENT
1 parent f3bd147 commit 1666dbf

File tree

14 files changed

+291
-56
lines changed

14 files changed

+291
-56
lines changed

Marlin/Configuration_adv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,6 +2995,8 @@
29952995

29962996
#define HOLD_MULTIPLIER 0.5 // Scales down the holding current from run current
29972997

2998+
//#define EDITABLE_HOMING_CURRENT // Add a G-code and menu to modify the Homing Current
2999+
29983000
/**
29993001
* Interpolate microsteps to 256
30003002
* Override for each driver with <driver>_INTERPOLATE settings below

Marlin/src/core/language.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@
312312
#define STR_FILAMENT_RUNOUT_SENSOR "Filament runout sensor"
313313
#define STR_DRIVER_STEPPING_MODE "Driver stepping mode"
314314
#define STR_STEPPER_DRIVER_CURRENT "Stepper driver current"
315+
#define STR_HOMING_CURRENT "Homing Current (mA)"
315316
#define STR_HYBRID_THRESHOLD "Hybrid Threshold"
316-
#define STR_STALLGUARD_THRESHOLD "StallGuard threshold"
317+
#define STR_STALLGUARD_THRESHOLD "StallGuard Threshold"
317318
#define STR_HOME_OFFSET "Home offset"
318319
#define STR_SOFT_ENDSTOPS "Soft endstops"
319320
#define STR_MATERIAL_HEATUP "Material heatup parameters"

Marlin/src/feature/tmc_util.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#endif
4444
#endif
4545

46+
#if ENABLED(EDITABLE_HOMING_CURRENT)
47+
homing_current_t homing_current_mA;
48+
#endif
49+
4650
/**
4751
* Check for over temperature or short to ground error flags.
4852
* Report and log warning of overtemperature condition.

Marlin/src/feature/tmc_util.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,32 @@ void test_tmc_connection(LOGICAL_AXIS_DECL_LC(const bool, true));
375375

376376
#endif // USE_SENSORLESS
377377

378+
#if HAS_HOMING_CURRENT
379+
380+
// Axes that have a distinct homing current
381+
struct homing_current_t {
382+
OPTCODE(X_HAS_HOME_CURRENT, uint16_t X)
383+
OPTCODE(Y_HAS_HOME_CURRENT, uint16_t Y)
384+
OPTCODE(Z_HAS_HOME_CURRENT, uint16_t Z)
385+
OPTCODE(X2_HAS_HOME_CURRENT, uint16_t X2)
386+
OPTCODE(Y2_HAS_HOME_CURRENT, uint16_t Y2)
387+
OPTCODE(Z2_HAS_HOME_CURRENT, uint16_t Z2)
388+
OPTCODE(Z3_HAS_HOME_CURRENT, uint16_t Z3)
389+
OPTCODE(Z4_HAS_HOME_CURRENT, uint16_t Z4)
390+
OPTCODE(I_HAS_HOME_CURRENT, uint16_t I)
391+
OPTCODE(J_HAS_HOME_CURRENT, uint16_t J)
392+
OPTCODE(K_HAS_HOME_CURRENT, uint16_t K)
393+
OPTCODE(U_HAS_HOME_CURRENT, uint16_t U)
394+
OPTCODE(V_HAS_HOME_CURRENT, uint16_t V)
395+
OPTCODE(W_HAS_HOME_CURRENT, uint16_t W)
396+
};
397+
398+
#if ENABLED(EDITABLE_HOMING_CURRENT)
399+
extern homing_current_t homing_current_mA;
400+
#endif
401+
402+
#endif // HAS_HOMING_CURRENT
403+
378404
#endif // HAS_TRINAMIC_CONFIG
379405

380406
#if HAS_TMC_SPI

Marlin/src/gcode/feature/trinamic/M906.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ static void tmc_print_current(TMC &st) {
3737
/**
3838
* M906: Set motor current in milliamps.
3939
*
40+
* With no parameters report driver currents.
41+
*
4042
* Parameters:
4143
* X[current] - Set mA current for X driver(s)
4244
* Y[current] - Set mA current for Y driver(s)
@@ -52,9 +54,14 @@ static void tmc_print_current(TMC &st) {
5254
* I[index] - Axis sub-index (Omit or 0 for X, Y, Z; 1 for X2, Y2, Z2; 2 for Z3; 3 for Z4.)
5355
* T[index] - Extruder index (Zero-based. Omit for E0 only.)
5456
*
55-
* With no parameters report driver currents.
57+
* With EDITABLE_HOMING_CURRENT:
58+
* H - Set / Report Homing Current. Alias for M920.
5659
*/
5760
void GcodeSuite::M906() {
61+
#if ENABLED(EDITABLE_HOMING_CURRENT)
62+
if (parser.seen_test('H')) M920();
63+
#endif
64+
5865
#define TMC_SAY_CURRENT(Q) tmc_print_current(stepper##Q)
5966
#define TMC_SET_CURRENT(Q) stepper##Q.rms_current(value)
6067

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2025 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#include "../../../inc/MarlinConfigPre.h"
24+
25+
#if ENABLED(EDITABLE_HOMING_CURRENT)
26+
27+
#include "../../gcode.h"
28+
#include "../../../feature/tmc_util.h"
29+
30+
#if AXIS_COLLISION('I')
31+
#define I_PARAM 'S'
32+
#define I_PARAM_STR "S"
33+
#warning "Use 'M920 S' instead of 'M920 I' for the stepper number."
34+
#else
35+
#define I_PARAM 'I'
36+
#define I_PARAM_STR "I"
37+
#endif
38+
39+
/**
40+
* M920: Set Homing Current for one or more axes
41+
*
42+
* Parameters:
43+
* X[current] - Homing Current to use for X axis stepper(s)
44+
* Y[current] - Homing Current to use for Y axis stepper(s)
45+
* Z[current] - Homing Current to use for Z axis stepper(s)
46+
* A[current] - Homing Current to use for A axis stepper(s)
47+
* B[current] - Homing Current to use for B axis stepper(s)
48+
* C[current] - Homing Current to use for C axis stepper(s)
49+
* U[current] - Homing Current to use for U axis stepper(s)
50+
* V[current] - Homing Current to use for V axis stepper(s)
51+
* W[current] - Homing Current to use for W axis stepper(s)
52+
*
53+
* I<index> - For multi-stepper axes, the zero-based index of the stepper to modify in each axis.
54+
* If omitted all steppers of each axis will be set to the given axis current.
55+
*/
56+
void GcodeSuite::M920() {
57+
bool report = true;
58+
const uint8_t index = parser.byteval(I_PARAM);
59+
LOOP_NUM_AXES(i) if (parser.seen(AXIS_CHAR(i))) {
60+
const int16_t value = parser.value_int();
61+
report = false;
62+
switch (i) {
63+
#if X_HAS_HOME_CURRENT
64+
case X_AXIS:
65+
if (index < 1) homing_current_mA.X = value;
66+
TERN_(X2_HAS_HOME_CURRENT, if (!index || index == 1) homing_current_mA.X2 = value);
67+
break;
68+
#endif
69+
#if Y_HAS_HOME_CURRENT
70+
case Y_AXIS:
71+
if (index < 1) homing_current_mA.Y = value;
72+
TERN_(Y2_HAS_HOME_CURRENT, if (!index || index == 1) homing_current_mA.Y2 = value);
73+
break;
74+
#endif
75+
#if Z_HAS_HOME_CURRENT
76+
case Z_AXIS:
77+
if (index < 1) homing_current_mA.Z = value;
78+
TERN_(Z2_HAS_HOME_CURRENT, if (!index || index == 1) homing_current_mA.Z2 = value);
79+
TERN_(Z3_HAS_HOME_CURRENT, if (!index || index == 2) homing_current_mA.Z3 = value);
80+
TERN_(Z4_HAS_HOME_CURRENT, if (!index || index == 3) homing_current_mA.Z4 = value);
81+
break;
82+
#endif
83+
OPTCODE(I_HAS_HOME_CURRENT, case I_AXIS: homing_current_mA.I = value; break)
84+
OPTCODE(J_HAS_HOME_CURRENT, case J_AXIS: homing_current_mA.J = value; break)
85+
OPTCODE(K_HAS_HOME_CURRENT, case K_AXIS: homing_current_mA.K = value; break)
86+
OPTCODE(U_HAS_HOME_CURRENT, case U_AXIS: homing_current_mA.U = value; break)
87+
OPTCODE(V_HAS_HOME_CURRENT, case V_AXIS: homing_current_mA.V = value; break)
88+
OPTCODE(W_HAS_HOME_CURRENT, case W_AXIS: homing_current_mA.W = value; break)
89+
}
90+
}
91+
92+
if (report) M920_report();
93+
}
94+
95+
void GcodeSuite::M920_report(const bool forReplay/*=true*/) {
96+
TERN_(MARLIN_SMALL_BUILD, return);
97+
98+
report_heading(forReplay, F(STR_HOMING_CURRENT));
99+
100+
auto say_M920 = [](const bool forReplay, int16_t index=-1) {
101+
report_echo_start(forReplay);
102+
SERIAL_ECHOPGM(" M920");
103+
if (index >= 0) SERIAL_ECHOPGM(" " I_PARAM_STR, index);
104+
};
105+
106+
#if X_SENSORLESS || Y_SENSORLESS || Z_SENSORLESS
107+
#if X2_SENSORLESS || Y2_SENSORLESS || Z2_SENSORLESS || Z3_SENSORLESS || Z4_SENSORLESS
108+
say_M920(forReplay, 0);
109+
#else
110+
say_M920(forReplay);
111+
#endif
112+
TERN_(X_SENSORLESS, SERIAL_ECHOPGM_P(SP_X_STR, homing_current_mA.X));
113+
TERN_(Y_SENSORLESS, SERIAL_ECHOPGM_P(SP_Y_STR, homing_current_mA.Y));
114+
TERN_(Z_SENSORLESS, SERIAL_ECHOPGM_P(SP_Z_STR, homing_current_mA.Z));
115+
#if X2_SENSORLESS || Y2_SENSORLESS || Z2_SENSORLESS || Z3_SENSORLESS || Z4_SENSORLESS
116+
say_M920(forReplay);
117+
#endif
118+
TERN_(I_SENSORLESS, SERIAL_ECHOPGM_P(SP_I_STR, homing_current_mA.I));
119+
TERN_(J_SENSORLESS, SERIAL_ECHOPGM_P(SP_J_STR, homing_current_mA.J));
120+
TERN_(K_SENSORLESS, SERIAL_ECHOPGM_P(SP_K_STR, homing_current_mA.K));
121+
TERN_(U_SENSORLESS, SERIAL_ECHOPGM_P(SP_U_STR, homing_current_mA.U));
122+
TERN_(V_SENSORLESS, SERIAL_ECHOPGM_P(SP_V_STR, homing_current_mA.V));
123+
TERN_(W_SENSORLESS, SERIAL_ECHOPGM_P(SP_W_STR, homing_current_mA.W));
124+
SERIAL_EOL();
125+
#endif
126+
127+
#if X2_SENSORLESS || Y2_SENSORLESS || Z2_SENSORLESS
128+
say_M920(forReplay, 1);
129+
TERN_(X2_SENSORLESS, SERIAL_ECHOPGM_P(SP_X_STR, homing_current_mA.X2));
130+
TERN_(Y2_SENSORLESS, SERIAL_ECHOPGM_P(SP_Y_STR, homing_current_mA.Y2));
131+
TERN_(Z2_SENSORLESS, SERIAL_ECHOPGM_P(SP_Z_STR, homing_current_mA.Z2));
132+
SERIAL_EOL();
133+
#endif
134+
#if Z3_SENSORLESS
135+
say_M920(forReplay, 2);
136+
SERIAL_ECHOLNPGM(" Z", homing_current_mA.Z3);
137+
#endif
138+
#if Z4_SENSORLESS
139+
say_M920(forReplay, 3);
140+
SERIAL_ECHOLNPGM(" Z", homing_current_mA.Z4);
141+
#endif
142+
}
143+
144+
#endif // EDITABLE_HOMING_CURRENT

Marlin/src/gcode/gcode.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,15 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
10561056
case 912: M912(); break; // M912: Clear TMC2130 prewarn triggered flags
10571057
#endif
10581058
#if ENABLED(HYBRID_THRESHOLD)
1059-
case 913: M913(); break; // M913: Set HYBRID_THRESHOLD speed.
1059+
case 913: M913(); break; // M913: Set HYBRID_THRESHOLD speed
10601060
#endif
10611061
#if USE_SENSORLESS
1062-
case 914: M914(); break; // M914: Set StallGuard sensitivity.
1062+
case 914: M914(); break; // M914: Set StallGuard sensitivity
10631063
#endif
10641064
case 919: M919(); break; // M919: Set stepper Chopper Times
1065+
#if ENABLED(EDITABLE_HOMING_CURRENT)
1066+
case 920: M920(); break; // M920: Set Homing Current
1067+
#endif
10651068
#endif
10661069

10671070
#if HAS_MICROSTEPS

Marlin/src/gcode/gcode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
* M914 - Set StallGuard sensitivity. (Requires SENSORLESS_HOMING or SENSORLESS_PROBING)
321321
* M919 - Set or Report motor Chopper Times (time_off, hysteresis_end, hysteresis_start) using axis codes XYZE, etc.
322322
* If no parameters are given, report. (Requires *_DRIVER_TYPE TMC(2130|2160|5130|5160|2208|2209|2660))
323+
* M920 - Set Homing Current. (Requires distinct *_CURRENT_HOME settings)
323324
* M936 - OTA update firmware. (Requires OTA_FIRMWARE_UPDATE)
324325
* M951 - Set Magnetic Parking Extruder parameters. (Requires MAGNETIC_PARKING_EXTRUDER)
325326
* M3426 - Read MCP3426 ADC over I2C. (Requires HAS_MCP3426_ADC)
@@ -1259,6 +1260,10 @@ class GcodeSuite {
12591260
static void M914_report(const bool forReplay=true);
12601261
#endif
12611262
static void M919();
1263+
#if ENABLED(EDITABLE_HOMING_CURRENT)
1264+
static void M920();
1265+
static void M920_report(const bool forReplay=true);
1266+
#endif
12621267
#endif
12631268

12641269
#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_I2C || HAS_MOTOR_CURRENT_DAC

Marlin/src/lcd/language/language_en.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ namespace LanguageNarrow_en {
883883
LSTR MSG_TMC_HOMING_THRS = _UxGT("Sensorless Homing");
884884
LSTR MSG_TMC_STEPPING_MODE = _UxGT("Stepping Mode");
885885
LSTR MSG_TMC_STEALTHCHOP = _UxGT("StealthChop");
886+
LSTR MSG_TMC_HOMING_CURRENT = _UxGT("Homing Current");
886887
LSTR MSG_SERVICE_RESET = _UxGT("Reset");
887888
LSTR MSG_SERVICE_IN = _UxGT(" in:");
888889
LSTR MSG_BACKLASH = _UxGT("Backlash");

Marlin/src/lcd/menu/menu_tmc.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ void menu_tmc_current() {
110110

111111
#endif // SENSORLESS_HOMING
112112

113+
#if ENABLED(EDITABLE_HOMING_CURRENT)
114+
115+
#define TMC_EDIT_HOMING_CURRENT(ST, STR) EDIT_ITEM_FAST_F(uint16_4, F(STR), &homing_current_mA.ST, ST##_CURRENT / 3, ST##_CURRENT)
116+
117+
void menu_tmc_homing_current() {
118+
START_MENU();
119+
STATIC_ITEM(MSG_TMC_HOMING_CURRENT);
120+
BACK_ITEM(MSG_TMC_DRIVERS);
121+
TERN_( X_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(X, STR_X));
122+
TERN_(X2_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(X2, STR_X2));
123+
TERN_( Y_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Y, STR_Y));
124+
TERN_(Y2_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Y2, STR_Y2));
125+
TERN_( Z_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Z, STR_Z));
126+
TERN_(Z2_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Z2, STR_Z2));
127+
TERN_(Z3_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Z3, STR_Z3));
128+
TERN_(Z4_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(Z4, STR_Z4));
129+
TERN_( I_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(I, STR_I));
130+
TERN_( J_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(J, STR_J));
131+
TERN_( K_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(K, STR_K));
132+
TERN_( U_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(U, STR_U));
133+
TERN_( V_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(V, STR_V));
134+
TERN_( W_HAS_HOME_CURRENT, TMC_EDIT_HOMING_CURRENT(W, STR_W));
135+
END_MENU();
136+
}
137+
138+
#endif // EDITABLE_HOMING_CURRENT
139+
113140
#if HAS_STEALTHCHOP
114141

115142
#define TMC_EDIT_STEP_MODE(ST, STR) EDIT_ITEM_F(bool, F(STR), &stepper##ST.stored.stealthChop_enabled, []{ stepper##ST.refresh_stepping_mode(); })
@@ -143,9 +170,10 @@ void menu_tmc() {
143170
START_MENU();
144171
BACK_ITEM(MSG_ADVANCED_SETTINGS);
145172
SUBMENU(MSG_TMC_CURRENT, menu_tmc_current);
146-
TERN_(HYBRID_THRESHOLD, SUBMENU(MSG_TMC_HYBRID_THRS, menu_tmc_hybrid_thrs));
147-
TERN_(SENSORLESS_HOMING, SUBMENU(MSG_TMC_HOMING_THRS, menu_tmc_homing_thrs));
148-
TERN_(HAS_STEALTHCHOP, SUBMENU(MSG_TMC_STEPPING_MODE, menu_tmc_step_mode));
173+
TERN_(HYBRID_THRESHOLD, SUBMENU(MSG_TMC_HYBRID_THRS, menu_tmc_hybrid_thrs));
174+
TERN_(SENSORLESS_HOMING, SUBMENU(MSG_TMC_HOMING_THRS, menu_tmc_homing_thrs));
175+
TERN_(EDITABLE_HOMING_CURRENT, SUBMENU(MSG_TMC_HOMING_CURRENT, menu_tmc_homing_current));
176+
TERN_(HAS_STEALTHCHOP, SUBMENU(MSG_TMC_STEALTHCHOP, menu_tmc_step_mode));
149177
END_MENU();
150178
}
151179

0 commit comments

Comments
 (0)