Skip to content

Commit

Permalink
apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Aug 7, 2023
1 parent 1fa21f5 commit acc8e44
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 70 deletions.
7 changes: 3 additions & 4 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2267,10 +2267,9 @@
/**
* Nonlinear Extrusion Control
*
* Enables control over extrusion rate based on instantaneous extruder velocity. This can be used
* to correct for underextrusion at high extruder speeds that are otherwise well-behaved (e.g.
* not yet skipping).
*/
* Control extrusion rate based on instantaneous extruder velocity. Can be used to correct for
* underextrusion at high extruder speeds that are otherwise well-behaved (i.e., not skipping).
*/
//#define NONLINEAR_EXTRUSION

// @section leveling
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/core/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
#define STR_CHAMBER_PID "Chamber PID"
#define STR_STEPS_PER_UNIT "Steps per unit"
#define STR_LINEAR_ADVANCE "Linear Advance"
#define STR_NONLINEAR_EXTRUSION "Nonlinear Extrusion"
#define STR_CONTROLLER_FAN "Controller Fan"
#define STR_STEPPER_MOTOR_CURRENTS "Stepper motor currents"
#define STR_RETRACT_S_F_Z "Retract (S<length> F<feedrate> Z<lift>)"
Expand Down
25 changes: 11 additions & 14 deletions Marlin/src/gcode/feature/nonlinear/M592.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,25 @@
#include "../../gcode.h"
#include "../../../module/stepper.h"

void GcodeSuite::M592_report(const bool forReplay/*=true*/) {
report_heading(forReplay, F(STR_NONLINEAR_EXTRUSION));
SERIAL_ECHOLNPGM(" M593 A", stepper.ne.A, " B", stepper.ne.B, " C", stepper.ne.C);
}

/**
* M592: Get or set nonlinear extrusion parameters
* A<factor> Linear coefficient (default 0.0)
* B<factor> Quadratic coefficient (default 0.0)
* C<factor> Constant coefficient (default 1.0)
*
* Adjusts the amount of extrusion based on the instantaneous velocity of extrusion, as a multiplier.
* The amount of extrusion is multiplied by max(C, C + A*v + B*v^2) where v is extruder velocity in mm/s.
* Only adjusts forward extrusions, since those are the ones affected by backpressure.
* Adjusts the amount of extrusion based on the instantaneous velocity of extrusion, as a multiplier.
* The amount of extrusion is multiplied by max(C, C + A*v + B*v^2) where v is extruder velocity in mm/s.
* Only adjusts forward extrusions, since those are the ones affected by backpressure.
*/
void GcodeSuite::M592() {
if (parser.seenval('A')) {
stepper.ne_A = parser.value_float();
}

if (parser.seenval('B')) {
stepper.ne_B = parser.value_float();
}

if (parser.seenval('C')) {
stepper.ne_C = parser.value_float();
}
if (parser.seenval('A')) stepper.ne.A = parser.value_float();
if (parser.seenval('B')) stepper.ne.B = parser.value_float();
if (parser.seenval('C')) stepper.ne.C = parser.value_float();
}

#endif // NONLINEAR_EXTRUSION
1 change: 1 addition & 0 deletions Marlin/src/gcode/gcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ class GcodeSuite {

#if ENABLED(NONLINEAR_EXTRUSION)
static void M592();
static void M592_report(const bool forReplay=true);
#endif

#if HAS_ZV_SHAPING
Expand Down
16 changes: 6 additions & 10 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -853,19 +853,15 @@ static_assert(COUNT(arm) == LOGICAL_AXES, "AXIS_RELATIVE_MODES must contain " _L
#endif

/**
* Nonlinear Extrusion requirements
* Nonlinear Extrusion requirements
*/
#if ENABLED(NONLINEAR_EXTRUSION)
#if DISABLED(ADAPTIVE_STEP_SMOOTHING)
#error "ADAPTIVE_STEP_SMOOTHING is required for NONLINEAR_EXTRUSION"
#endif

#if EXTRUDERS > 1
#error "NONLINEAR_EXTRUSION doesn't currently support multi-extruder setups"
#endif

#if DISABLED(CPU_32_BIT)
#error "NONLINEAR_EXTRUSION requires 32-bit CPU"
#error "ADAPTIVE_STEP_SMOOTHING is required for NONLINEAR_EXTRUSION."
#elif HAS_MULTI_EXTRUDER
#error "NONLINEAR_EXTRUSION doesn't currently support multi-extruder setups."
#elif DISABLED(CPU_32_BIT)
#error "NONLINEAR_EXTRUSION requires a 32-bit CPU."
#endif
#endif

Expand Down
29 changes: 16 additions & 13 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,10 @@ typedef struct SettingsDataStruct {
#endif

//
// NONLINEAR_EXTRUSION
// Nonlinear Extrusion
//
#if ENABLED(NONLINEAR_EXTRUSION)
float ne_A, ne_B, ne_C; // M592 A B C
ne_coeff_t stepper_ne; // M592 A B C
#endif

} SettingsData;
Expand Down Expand Up @@ -1737,12 +1737,10 @@ void MarlinSettings::postprocess() {
#endif

//
// NONLINEAR_EXTRUSION
// Nonlinear Extrusion
//
#if ENABLED(NONLINEAR_EXTRUSION)
EEPROM_WRITE(stepper.ne_A);
EEPROM_WRITE(stepper.ne_B);
EEPROM_WRITE(stepper.ne_C);
EEPROM_WRITE(stepper.ne);
#endif

//
Expand Down Expand Up @@ -2820,12 +2818,10 @@ void MarlinSettings::postprocess() {
#endif

//
// NONLINEAR_EXTRUSION
// Nonlinear Extrusion
//
#if ENABLED(NONLINEAR_EXTRUSION)
EEPROM_READ(stepper.ne_A);
EEPROM_READ(stepper.ne_B);
EEPROM_READ(stepper.ne_C);
EEPROM_READ(stepper.ne);
#endif

//
Expand Down Expand Up @@ -3421,15 +3417,13 @@ void MarlinSettings::reset() {
//
// Heated Bed PID
//

#if ENABLED(PIDTEMPBED)
thermalManager.temp_bed.pid.set(DEFAULT_bedKp, DEFAULT_bedKi, DEFAULT_bedKd);
#endif

//
// Heated Chamber PID
//

#if ENABLED(PIDTEMPCHAMBER)
thermalManager.temp_chamber.pid.set(DEFAULT_chamberKp, DEFAULT_chamberKi, DEFAULT_chamberKd);
#endif
Expand Down Expand Up @@ -3481,7 +3475,6 @@ void MarlinSettings::reset() {
//
// Volumetric & Filament Size
//

#if DISABLED(NO_VOLUMETRICS)
parser.volumetric_enabled = ENABLED(VOLUMETRIC_DEFAULT_ON);
for (uint8_t q = 0; q < COUNT(planner.filament_size); ++q)
Expand Down Expand Up @@ -3623,6 +3616,11 @@ void MarlinSettings::reset() {
//
TERN_(FT_MOTION, fxdTiCtrl.set_defaults());

//
// Nonlinear Extrusion
//
TERN_(NONLINEAR_EXTRUSION, stepper.ne.reset());

//
// Input Shaping
//
Expand Down Expand Up @@ -3892,6 +3890,11 @@ void MarlinSettings::reset() {
//
TERN_(FT_MOTION, gcode.M493_report(forReplay));

//
// Nonlinear Extrusion
//
TERN_(NONLINEAR_EXTRUSION, gcode.M592_report(forReplay));

//
// Input Shaping
//
Expand Down
37 changes: 17 additions & 20 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,9 @@ uint32_t Stepper::advance_divisor = 0,
#endif

#if ENABLED(NONLINEAR_EXTRUSION)
ne_coeff_t Stepper::ne;
ne_fix_t Stepper::ne_fix;
int32_t Stepper::ne_edividend;
float Stepper::ne_A = 0;
float Stepper::ne_B = 0;
float Stepper::ne_C = 1;
int32_t Stepper::ne_Afix;
int32_t Stepper::ne_Bfix;
int32_t Stepper::ne_Cfix;
uint32_t Stepper::ne_scale;
#endif

Expand Down Expand Up @@ -2206,11 +2202,11 @@ hal_timer_t Stepper::calc_timer_interval(uint32_t step_rate) {
hal_timer_t Stepper::calc_multistep_timer_interval(uint32_t step_rate) {

#if ENABLED(NONLINEAR_EXTRUSION)
uint32_t velocity = ne_scale * step_rate; // scale step_rate first so all intermediate values stay in range of 8.24 fixed point math
int32_t vd = (((int64_t)ne_Afix * velocity) >> 24) + (((((int64_t)ne_Bfix * velocity) >> 24) * velocity) >> 24);
if (vd < 0) vd = 0;
const uint32_t velocity = ne_scale * step_rate; // Scale step_rate first so all intermediate values stay in range of 8.24 fixed point math
int32_t vd = (((int64_t)nefix.A * velocity) >> 24) + (((((int64_t)nefix.B * velocity) >> 24) * velocity) >> 24);
NOLESS(vd, 0);

advance_dividend.e = ((uint64_t)(ne_Cfix + vd) * ne_edividend) >> 24;
advance_dividend.e = (uint64_t(nefix.C + vd) * ne_edividend) >> 24;
#endif

#if ENABLED(OLD_ADAPTIVE_MULTISTEPPING)
Expand Down Expand Up @@ -2655,9 +2651,10 @@ hal_timer_t Stepper::block_phase_isr() {
acceleration_time = deceleration_time = 0;

#if ENABLED(ADAPTIVE_STEP_SMOOTHING)
oversampling_factor = 0; // Assume no axis smoothing (via oversampling)
#if ENABLED(NONLINEAR_EXTRUSION)
oversampling_factor = 1; // We need at least 2x oversampling to ensure we can increase extruder step rate
oversampling_factor = 1; // Need at least 2x oversampling to permit increase of E step rate
#else
oversampling_factor = 0; // Assume no axis smoothing (via oversampling)
#endif
// Decide if axis smoothing is possible
uint32_t max_rate = current_block->nominal_rate; // Get the step event rate
Expand Down Expand Up @@ -2777,16 +2774,16 @@ hal_timer_t Stepper::block_phase_isr() {

#if ENABLED(NONLINEAR_EXTRUSION)
ne_edividend = advance_dividend.e;
float scale = (float(ne_edividend) / advance_divisor) * planner.mm_per_step[E_AXIS_N(current_block->extruder)];
const float scale = (float(ne_edividend) / advance_divisor) * planner.mm_per_step[E_AXIS_N(current_block->extruder)];
ne_scale = (1L << 24) * scale;
if (current_block->direction_bits.e) {
ne_Afix = (1L << 24) * ne_A;
ne_Bfix = (1L << 24) * ne_B;
ne_Cfix = (1L << 24) * ne_C;
} else {
ne_Afix = 0;
ne_Bfix = 0;
ne_Cfix = (1L << 24);
ne_fix.A = (1L << 24) * ne.A;
ne_fix.B = (1L << 24) * ne.B;
ne_fix.C = (1L << 24) * ne.C;
}
else {
ne_fix.A = ne_fix.B = 0;
ne_fix.C = (1L << 24);
}
#endif

Expand Down
17 changes: 9 additions & 8 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ constexpr ena_mask_t enable_overlap[] = {

#endif // HAS_ZV_SHAPING

#if ENABLED(NONLINEAR_EXTRUSION)
typedef struct { float A, B, C; void reset() { A = B = 0.0f; C = 1.0f; } } ne_coeff_t;
typedef struct { int32_t A, B, C; } ne_fix_t;
#endif

//
// Stepper class definition
//
Expand Down Expand Up @@ -327,9 +332,7 @@ class Stepper {
#endif

#if ENABLED(NONLINEAR_EXTRUSION)
static float ne_A;
static float ne_B;
static float ne_C;
static ne_coeff_t nla;
#endif

private:
Expand Down Expand Up @@ -423,11 +426,9 @@ class Stepper {
#endif

#if ENABLED(NONLINEAR_EXTRUSION)
static int32_t ne_edividend;
static int32_t ne_Afix;
static int32_t ne_Bfix;
static int32_t ne_Cfix;
static uint32_t ne_scale;
static int32_t ne_edividend;
static uint32_t ne_scale;
static ne_fix_t ne_fix;
#endif

#if ENABLED(BABYSTEPPING)
Expand Down
2 changes: 1 addition & 1 deletion buildroot/tests/STM32F103RC_btt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set -e
restore_configs
opt_set MOTHERBOARD BOARD_BTT_SKR_MINI_E3_V1_0 SERIAL_PORT 1 SERIAL_PORT_2 -1 \
X_DRIVER_TYPE TMC2209 Y_DRIVER_TYPE TMC2209 Z_DRIVER_TYPE TMC2209 E0_DRIVER_TYPE TMC2209
opt_enable CR10_STOCKDISPLAY PINS_DEBUGGING Z_IDLE_HEIGHT FT_MOTION FT_MOTION_MENU
opt_enable CR10_STOCKDISPLAY PINS_DEBUGGING Z_IDLE_HEIGHT FT_MOTION FT_MOTION_MENU NONLINEAR_EXTRUSION
exec_test $1 $2 "BigTreeTech SKR Mini E3 1.0 - TMC2209 HW Serial, FT_MOTION" "$3"

# clean up
Expand Down

0 comments on commit acc8e44

Please sign in to comment.