Skip to content

Commit 0203e32

Browse files
GMagicianthinkyhead
andcommitted
✨ ADVANCE_K per-extruder (MarlinFirmware#24821)
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
1 parent 8481264 commit 0203e32

File tree

13 files changed

+68
-52
lines changed

13 files changed

+68
-52
lines changed

Marlin/Configuration_adv.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -2061,12 +2061,16 @@
20612061
*/
20622062
//#define LIN_ADVANCE
20632063
#if ENABLED(LIN_ADVANCE)
2064-
//#define EXTRA_LIN_ADVANCE_K // Add a second linear advance constant, configurable with M900 L.
2065-
#define LIN_ADVANCE_K 0.22 // Unit: mm compression per 1mm/s extruder speed
2066-
//#define LA_DEBUG // Print debug information to serial during operation. Disable for production use.
2067-
//#define EXPERIMENTAL_SCURVE // Allow S-Curve Acceleration to be used with LA.
2068-
//#define ALLOW_LOW_EJERK // Allow a DEFAULT_EJERK value of <10. Recommended for direct drive hotends.
2069-
//#define EXPERIMENTAL_I2S_LA // Allow I2S_STEPPER_STREAM to be used with LA. Performance degrades as the LA step rate reaches ~20kHz.
2064+
#if ENABLED(DISTINCT_E_FACTORS)
2065+
#define ADVANCE_K { 0.22 } // (mm) Compression length per 1mm/s extruder speed, per extruder
2066+
#else
2067+
#define ADVANCE_K 0.22 // (mm) Compression length applying to all extruders
2068+
#endif
2069+
//#define ADVANCE_K_EXTRA // Add a second linear advance constant, configurable with M900 L.
2070+
//#define LA_DEBUG // Print debug information to serial during operation. Disable for production use.
2071+
//#define EXPERIMENTAL_SCURVE // Allow S-Curve Acceleration to be used with LA.
2072+
//#define ALLOW_LOW_EJERK // Allow a DEFAULT_EJERK value of <10. Recommended for direct drive hotends.
2073+
//#define EXPERIMENTAL_I2S_LA // Allow I2S_STEPPER_STREAM to be used with LA. Performance degrades as the LA step rate reaches ~20kHz.
20702074
#endif
20712075

20722076
// @section leveling

Marlin/src/gcode/feature/advance/M900.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
#include "../../gcode.h"
2828
#include "../../../module/planner.h"
2929

30-
#if ENABLED(EXTRA_LIN_ADVANCE_K)
31-
float other_extruder_advance_K[EXTRUDERS];
30+
#if ENABLED(ADVANCE_K_EXTRA)
31+
float other_extruder_advance_K[DISTINCT_E];
3232
uint8_t lin_adv_slot = 0;
3333
#endif
3434

3535
/**
3636
* M900: Get or Set Linear Advance K-factor
3737
* T<tool> Which tool to address
3838
* K<factor> Set current advance K factor (Slot 0).
39-
* L<factor> Set secondary advance K factor (Slot 1). Requires EXTRA_LIN_ADVANCE_K.
40-
* S<0/1> Activate slot 0 or 1. Requires EXTRA_LIN_ADVANCE_K.
39+
* L<factor> Set secondary advance K factor (Slot 1). Requires ADVANCE_K_EXTRA.
40+
* S<0/1> Activate slot 0 or 1. Requires ADVANCE_K_EXTRA.
4141
*/
4242
void GcodeSuite::M900() {
4343

@@ -58,12 +58,12 @@ void GcodeSuite::M900() {
5858
}
5959
#endif
6060

61-
float &kref = planner.extruder_advance_K[tool_index], newK = kref;
61+
float &kref = planner.extruder_advance_K[E_INDEX_N(tool_index)], newK = kref;
6262
const float oldK = newK;
6363

64-
#if ENABLED(EXTRA_LIN_ADVANCE_K)
64+
#if ENABLED(ADVANCE_K_EXTRA)
6565

66-
float &lref = other_extruder_advance_K[tool_index];
66+
float &lref = other_extruder_advance_K[E_INDEX_N(tool_index)];
6767

6868
const bool old_slot = TEST(lin_adv_slot, tool_index), // The tool's current slot (0 or 1)
6969
new_slot = parser.boolval('S', old_slot); // The passed slot (default = current)
@@ -111,9 +111,9 @@ void GcodeSuite::M900() {
111111

112112
if (!parser.seen_any()) {
113113

114-
#if ENABLED(EXTRA_LIN_ADVANCE_K)
114+
#if ENABLED(ADVANCE_K_EXTRA)
115115

116-
#if EXTRUDERS < 2
116+
#if DISTINCT_E < 2
117117
SERIAL_ECHOLNPGM("Advance S", new_slot, " K", kref, "(S", !new_slot, " K", lref, ")");
118118
#else
119119
EXTRUDER_LOOP() {
@@ -127,7 +127,7 @@ void GcodeSuite::M900() {
127127
#else
128128

129129
SERIAL_ECHO_START();
130-
#if EXTRUDERS < 2
130+
#if DISTINCT_E < 2
131131
SERIAL_ECHOLNPGM("Advance K=", planner.extruder_advance_K[0]);
132132
#else
133133
SERIAL_ECHOPGM("Advance K");
@@ -145,7 +145,7 @@ void GcodeSuite::M900() {
145145

146146
void GcodeSuite::M900_report(const bool forReplay/*=true*/) {
147147
report_heading(forReplay, F(STR_LINEAR_ADVANCE));
148-
#if EXTRUDERS < 2
148+
#if DISTINCT_E < 2
149149
report_echo_start(forReplay);
150150
SERIAL_ECHOLNPGM(" M900 K", planner.extruder_advance_K[0]);
151151
#else

Marlin/src/inc/Conditionals_LCD.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@
990990
* with shared motion and temperature settings.
991991
*
992992
* DISTINCT_E is the number of distinguished extruders. By default this
993-
* well be 1 which indicates all extruders share the same settings.
993+
* will be 1 which indicates all extruders share the same settings.
994994
*
995995
* E_INDEX_N(E) should be used to get the E index of any item that might be
996996
* distinguished.

Marlin/src/inc/SanityCheck.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@
652652
#error "USE_M73_REMAINING_TIME is now SET_REMAINING_TIME."
653653
#elif defined(SHOW_SD_PERCENT)
654654
#error "SHOW_SD_PERCENT is now SHOW_PROGRESS_PERCENT."
655+
#elif defined(EXTRA_LIN_ADVANCE_K)
656+
#error "EXTRA_LIN_ADVANCE_K is now ADVANCE_K_EXTRA."
655657
#endif
656658

657659
// L64xx stepper drivers have been removed
@@ -1336,10 +1338,15 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS
13361338
* Linear Advance 1.5 - Check K value range
13371339
*/
13381340
#if ENABLED(LIN_ADVANCE)
1339-
static_assert(
1340-
WITHIN(LIN_ADVANCE_K, 0, 10),
1341-
"LIN_ADVANCE_K must be a value from 0 to 10 (Changed in LIN_ADVANCE v1.5, Marlin 1.1.9)."
1342-
);
1341+
#if DISTINCT_E > 1
1342+
constexpr float lak[] = ADVANCE_K;
1343+
static_assert(COUNT(lak) < DISTINCT_E, "The ADVANCE_K array has too many elements (i.e., more than " STRINGIFY(DISTINCT_E) ").");
1344+
#define _LIN_ASSERT(N) static_assert(N >= COUNT(lak) || WITHIN(lak[N], 0, 10), "ADVANCE_K values must be from 0 to 10 (Changed in LIN_ADVANCE v1.5, Marlin 1.1.9).");
1345+
REPEAT(DISTINCT_E, _LIN_ASSERT)
1346+
#undef _LIN_ASSERT
1347+
#else
1348+
static_assert(WITHIN(ADVANCE_K, 0, 10), "ADVANCE_K must be from 0 to 10 (Changed in LIN_ADVANCE v1.5, Marlin 1.1.9).");
1349+
#endif
13431350
#if ENABLED(S_CURVE_ACCELERATION) && DISABLED(EXPERIMENTAL_SCURVE)
13441351
#error "LIN_ADVANCE and S_CURVE_ACCELERATION may not play well together! Enable EXPERIMENTAL_SCURVE to continue."
13451352
#elif ENABLED(DIRECT_STEPPING)

Marlin/src/lcd/e3v2/jyersui/dwin.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2772,7 +2772,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
27722772
#if ENABLED(LIN_ADVANCE)
27732773
case ADVANCED_LA:
27742774
if (draw) {
2775-
Draw_Menu_Item(row, ICON_MaxAccelerated, F("Lin Advance Kp"));
2775+
Draw_Menu_Item(row, ICON_MaxAccelerated, F("Lin Advance K"));
27762776
Draw_Float(planner.extruder_advance_K[0], row, false, 100);
27772777
}
27782778
else

Marlin/src/lcd/extui/ui_api.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -712,17 +712,17 @@ namespace ExtUI {
712712

713713
#if ENABLED(POWER_LOSS_RECOVERY)
714714
bool getPowerLossRecoveryEnabled() { return recovery.enabled; }
715-
void setPowerLossRecoveryEnabled(const bool value) { recovery.enable(value); }
715+
void setPowerLossRecoveryEnabled(const bool value) { recovery.enable(value); }
716716
#endif
717717

718718
#if ENABLED(LIN_ADVANCE)
719719
float getLinearAdvance_mm_mm_s(const extruder_t extruder) {
720-
return (extruder < EXTRUDERS) ? planner.extruder_advance_K[extruder - E0] : 0;
720+
return (extruder < EXTRUDERS) ? planner.extruder_advance_K[E_INDEX_N(extruder - E0)] : 0;
721721
}
722722

723723
void setLinearAdvance_mm_mm_s(const_float_t value, const extruder_t extruder) {
724724
if (extruder < EXTRUDERS)
725-
planner.extruder_advance_K[extruder - E0] = constrain(value, 0, 10);
725+
planner.extruder_advance_K[E_INDEX_N(extruder - E0)] = constrain(value, 0, 10);
726726
}
727727
#endif
728728

Marlin/src/lcd/menu/menu_advanced.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ void menu_backlash();
109109
BACK_ITEM(MSG_ADVANCED_SETTINGS);
110110

111111
#if ENABLED(LIN_ADVANCE)
112-
#if EXTRUDERS == 1
112+
#if DISTINCT_E < 2
113113
EDIT_ITEM(float42_52, MSG_ADVANCE_K, &planner.extruder_advance_K[0], 0, 10);
114-
#elif HAS_MULTI_EXTRUDER
114+
#else
115115
EXTRUDER_LOOP()
116116
EDIT_ITEM_N(float42_52, e, MSG_ADVANCE_K_E, &planner.extruder_advance_K[e], 0, 10);
117117
#endif
@@ -687,11 +687,11 @@ void menu_advanced_settings() {
687687
#if DISABLED(NO_VOLUMETRICS) || ENABLED(ADVANCED_PAUSE_FEATURE)
688688
SUBMENU(MSG_FILAMENT, menu_advanced_filament);
689689
#elif ENABLED(LIN_ADVANCE)
690-
#if EXTRUDERS == 1
690+
#if DISTINCT_E < 2
691691
EDIT_ITEM(float42_52, MSG_ADVANCE_K, &planner.extruder_advance_K[0], 0, 10);
692-
#elif HAS_MULTI_EXTRUDER
693-
LOOP_L_N(n, E_STEPPERS)
694-
EDIT_ITEM_N(float42_52, n, MSG_ADVANCE_K_E, &planner.extruder_advance_K[n], 0, 10);
692+
#else
693+
EXTRUDER_LOOP()
694+
EDIT_ITEM_N(float42_52, n, MSG_ADVANCE_K_E, &planner.extruder_advance_K[e], 0, 10);
695695
#endif
696696
#endif
697697

Marlin/src/lcd/menu/menu_tune.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ void menu_tune() {
210210
// Advance K:
211211
//
212212
#if ENABLED(LIN_ADVANCE) && DISABLED(SLIM_LCD_MENUS)
213-
#if EXTRUDERS == 1
213+
#if DISTINCT_E < 2
214214
EDIT_ITEM(float42_52, MSG_ADVANCE_K, &planner.extruder_advance_K[0], 0, 10);
215-
#elif HAS_MULTI_EXTRUDER
215+
#else
216216
EXTRUDER_LOOP()
217217
EDIT_ITEM_N(float42_52, e, MSG_ADVANCE_K_E, &planner.extruder_advance_K[e], 0, 10);
218218
#endif

Marlin/src/module/planner.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ float Planner::previous_nominal_speed;
227227
#endif
228228

229229
#if ENABLED(LIN_ADVANCE)
230-
float Planner::extruder_advance_K[EXTRUDERS]; // Initialized by settings.load()
230+
float Planner::extruder_advance_K[DISTINCT_E]; // Initialized by settings.load()
231231
#endif
232232

233233
#if HAS_POSITION_FLOAT
@@ -854,7 +854,7 @@ void Planner::calculate_trapezoid_for_block(block_t * const block, const_float_t
854854

855855
#if ENABLED(LIN_ADVANCE)
856856
if (block->la_advance_rate) {
857-
const float comp = extruder_advance_K[block->extruder] * block->steps.e / block->step_event_count;
857+
const float comp = extruder_advance_K[E_INDEX_N(block->extruder)] * block->steps.e / block->step_event_count;
858858
block->max_adv_steps = cruise_rate * comp;
859859
block->final_adv_steps = final_rate * comp;
860860
}
@@ -2541,7 +2541,7 @@ bool Planner::_populate_block(
25412541
*
25422542
* de > 0 : Extruder is running forward (e.g., for "Wipe while retracting" (Slic3r) or "Combing" (Cura) moves)
25432543
*/
2544-
use_advance_lead = esteps && extruder_advance_K[extruder] && de > 0;
2544+
use_advance_lead = esteps && extruder_advance_K[E_INDEX_N(extruder)] && de > 0;
25452545

25462546
if (use_advance_lead) {
25472547
float e_D_ratio = (target_float.e - position_float.e) /
@@ -2557,7 +2557,7 @@ bool Planner::_populate_block(
25572557
use_advance_lead = false;
25582558
else {
25592559
// Scale E acceleration so that it will be possible to jump to the advance speed.
2560-
const uint32_t max_accel_steps_per_s2 = MAX_E_JERK(extruder) / (extruder_advance_K[extruder] * e_D_ratio) * steps_per_mm;
2560+
const uint32_t max_accel_steps_per_s2 = MAX_E_JERK(extruder) / (extruder_advance_K[E_INDEX_N(extruder)] * e_D_ratio) * steps_per_mm;
25612561
if (TERN0(LA_DEBUG, accel > max_accel_steps_per_s2))
25622562
SERIAL_ECHOLNPGM("Acceleration limited.");
25632563
NOMORE(accel, max_accel_steps_per_s2);
@@ -2594,7 +2594,7 @@ bool Planner::_populate_block(
25942594

25952595
if (use_advance_lead) {
25962596
// the Bresenham algorithm will convert this step rate into extruder steps
2597-
block->la_advance_rate = extruder_advance_K[extruder] * block->acceleration_steps_per_s2;
2597+
block->la_advance_rate = extruder_advance_K[E_INDEX_N(extruder)] * block->acceleration_steps_per_s2;
25982598

25992599
// reduce LA ISR frequency by calling it only often enough to ensure that there will
26002600
// never be more than four extruder steps per call

Marlin/src/module/planner.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ class Planner {
459459
#endif
460460

461461
#if ENABLED(LIN_ADVANCE)
462-
static float extruder_advance_K[EXTRUDERS];
462+
static float extruder_advance_K[DISTINCT_E];
463463
#endif
464464

465465
/**

Marlin/src/module/settings.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
#endif
119119
#endif
120120

121-
#if ENABLED(EXTRA_LIN_ADVANCE_K)
122-
extern float other_extruder_advance_K[EXTRUDERS];
121+
#if ENABLED(ADVANCE_K_EXTRA)
122+
extern float other_extruder_advance_K[DISTINCT_E];
123123
#endif
124124

125125
#if HAS_MULTI_EXTRUDER
@@ -442,7 +442,7 @@ typedef struct SettingsDataStruct {
442442
//
443443
// LIN_ADVANCE
444444
//
445-
float planner_extruder_advance_K[_MAX(EXTRUDERS, 1)]; // M900 K planner.extruder_advance_K
445+
float planner_extruder_advance_K[DISTINCT_E]; // M900 K planner.extruder_advance_K
446446

447447
//
448448
// HAS_MOTOR_CURRENT_PWM
@@ -2334,7 +2334,7 @@ void MarlinSettings::postprocess() {
23342334
// Linear Advance
23352335
//
23362336
{
2337-
float extruder_advance_K[_MAX(EXTRUDERS, 1)];
2337+
float extruder_advance_K[DISTINCT_E];
23382338
_FIELD_TEST(planner_extruder_advance_K);
23392339
EEPROM_READ(extruder_advance_K);
23402340
#if ENABLED(LIN_ADVANCE)
@@ -3206,12 +3206,17 @@ void MarlinSettings::reset() {
32063206
//
32073207
// Linear Advance
32083208
//
3209-
32103209
#if ENABLED(LIN_ADVANCE)
3211-
EXTRUDER_LOOP() {
3212-
planner.extruder_advance_K[e] = LIN_ADVANCE_K;
3213-
TERN_(EXTRA_LIN_ADVANCE_K, other_extruder_advance_K[e] = LIN_ADVANCE_K);
3214-
}
3210+
#if ENABLED(DISTINCT_E_FACTORS)
3211+
constexpr float linAdvanceK[] = ADVANCE_K;
3212+
EXTRUDER_LOOP() {
3213+
const float a = linAdvanceK[_MAX(e, COUNT(linAdvanceK) - 1)];
3214+
planner.extruder_advance_K[e] = a;
3215+
TERN_(ADVANCE_K_EXTRA, other_extruder_advance_K[e] = a);
3216+
}
3217+
#else
3218+
planner.extruder_advance_K[0] = ADVANCE_K;
3219+
#endif
32153220
#endif
32163221

32173222
//

buildroot/tests/mega2560

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ opt_enable AUTO_BED_LEVELING_UBL RESTORE_LEVELING_AFTER_G28 DEBUG_LEVELING_FEATU
3030
REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER LIGHTWEIGHT_UI STATUS_MESSAGE_SCROLLING SHOW_CUSTOM_BOOTSCREEN BOOT_MARLIN_LOGO_SMALL \
3131
SDSUPPORT SDCARD_SORT_ALPHA USB_FLASH_DRIVE_SUPPORT AUTO_REPORT_SD_STATUS SCROLL_LONG_FILENAMES MEDIA_MENU_AT_TOP \
3232
EEPROM_SETTINGS EEPROM_CHITCHAT GCODE_MACROS CUSTOM_MENU_MAIN FREEZE_FEATURE CANCEL_OBJECTS SOUND_MENU_ITEM \
33-
MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE EXTRA_LIN_ADVANCE_K QUICK_HOME \
33+
MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE ADVANCE_K_EXTRA QUICK_HOME \
3434
SET_PROGRESS_MANUALLY SET_PROGRESS_PERCENT PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \
3535
ENCODER_NOISE_FILTER BABYSTEPPING BABYSTEP_XY NANODLP_Z_SYNC I2C_POSITION_ENCODERS M114_DETAIL
3636
exec_test $1 $2 "Azteeg X3 Pro | EXTRUDERS 5 | RRDFGSC | UBL | LIN_ADVANCE ..." "$3"

buildroot/tests/rambo

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ opt_enable USE_ZMAX_PLUG REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_P
3434
FWRETRACT ARC_P_CIRCLES CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS \
3535
PSU_CONTROL PS_OFF_CONFIRM PS_OFF_SOUND POWER_OFF_WAIT_FOR_COOLDOWN \
3636
POWER_LOSS_RECOVERY POWER_LOSS_PIN POWER_LOSS_STATE POWER_LOSS_RECOVER_ZHOME POWER_LOSS_ZHOME_POS \
37-
SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER LIN_ADVANCE EXTRA_LIN_ADVANCE_K \
37+
SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER LIN_ADVANCE ADVANCE_K_EXTRA \
3838
HOST_ACTION_COMMANDS HOST_PROMPT_SUPPORT PINS_DEBUGGING MAX7219_DEBUG M114_DETAIL
3939
opt_add DEBUG_POWER_LOSS_RECOVERY
4040
exec_test $1 $2 "RAMBO | EXTRUDERS 2 | CHAR LCD + SD | FIX Probe | ABL-Linear | Advanced Pause | PLR | LEDs ..." "$3"
@@ -93,7 +93,7 @@ opt_set MOTHERBOARD BOARD_MINIRAMBO \
9393
opt_enable EEPROM_SETTINGS EEPROM_CHITCHAT REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER \
9494
SDSUPPORT PCA9632 SOUND_MENU_ITEM GCODE_REPEAT_MARKERS \
9595
AUTO_BED_LEVELING_LINEAR PROBE_MANUALLY LCD_BED_LEVELING \
96-
LIN_ADVANCE EXTRA_LIN_ADVANCE_K \
96+
LIN_ADVANCE ADVANCE_K_EXTRA \
9797
INCH_MODE_SUPPORT TEMPERATURE_UNITS_SUPPORT EXPERIMENTAL_I2CBUS M100_FREE_MEMORY_WATCHER \
9898
NOZZLE_PARK_FEATURE NOZZLE_CLEAN_FEATURE \
9999
ADVANCED_PAUSE_FEATURE PARK_HEAD_ON_PAUSE ADVANCED_PAUSE_CONTINUOUS_PURGE FILAMENT_LOAD_UNLOAD_GCODES \

0 commit comments

Comments
 (0)