From 51321910bc1512bca9856e85b980bb57993093c0 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Fri, 1 Jul 2016 15:32:51 -0400 Subject: [PATCH] M221: use 256 for 100% flow internally for faster math The flow rate is given as a percentage which is kept as 100 = 100% internally. But this means we must divide by 100 for every movement which can be expensive. Convert the value to 256 = 100% so the compiler can optimize the division to a byte-shift. Also, avoid the math altogether in the normal case where the flow rate is already 100% and no change is required. Note: This also requires an increase in the size of e_multiplier to 16 bits so values >= 100% can be stored. Previously flow rates only up to 255% (2.5x) were supported which may have surprised some users. Now the flow rate can be as high as 10000% (100x), at least internally. --- dda.c | 8 +++++--- dda.h | 2 +- gcode_process.c | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/dda.c b/dda.c index 0884263..0fa3cf2 100644 --- a/dda.c +++ b/dda.c @@ -111,7 +111,7 @@ void dda_init(void) { if (startpoint.F == 0) startpoint.F = next_target.target.F = SEARCH_FEEDRATE_Z; if (startpoint.e_multiplier == 0) - startpoint.e_multiplier = next_target.target.e_multiplier = 100; + startpoint.e_multiplier = next_target.target.e_multiplier = 256; } /*! Distribute a new startpoint to DDA's internal structures without any movement. @@ -219,9 +219,11 @@ void dda_create(DDA *dda, TARGET *target) { steps[E] = um_to_steps(target->axis[E], E); // Apply extrusion multiplier. + if (target->e_multiplier != 256) { steps[E] *= target->e_multiplier; - steps[E] += 50; - steps[E] /= 100; + steps[E] += 128; + steps[E] /= 256; + } if ( ! target->e_relative) { int32_t delta_steps; diff --git a/dda.h b/dda.h index 4eaef1a..e580709 100644 --- a/dda.h +++ b/dda.h @@ -41,7 +41,7 @@ typedef struct { axes_int32_t axis; uint32_t F; - uint8_t e_multiplier; + uint16_t e_multiplier; uint8_t e_relative :1; ///< bool: e axis relative? Overrides all_relative } TARGET; diff --git a/gcode_process.c b/gcode_process.c index b87f2d5..1cb50be 100644 --- a/gcode_process.c +++ b/gcode_process.c @@ -797,7 +797,8 @@ void process_gcode_command() { //? --- M221: Control the extruders flow --- if ( ! next_target.seen_S) break; - next_target.target.e_multiplier = next_target.S; + // Scale 100% = 256 + next_target.target.e_multiplier = (next_target.S * 64 + 12) / 25; break; #ifdef DEBUG