Skip to content

Commit 0203e32

Browse files
GMagicianthinkyhead
andcommitted
✨ ADVANCE_K per-extruder (MarlinFirmware#24821)
Co-authored-by: Scott Lahteine <[email protected]>
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

Lines changed: 10 additions & 6 deletions
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

Lines changed: 11 additions & 11 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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
/**

0 commit comments

Comments
 (0)