From 00a28cd5029420d363dfa55c505caaf11568d81e Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 21 Nov 2016 17:34:52 -0800 Subject: [PATCH] DDA: use bitmask to track active axes for faster dda_step(). In dda_step instead of checking our 32-bit-wide delta[n] value, just check a single bit in an 8-bit field. Should be a tad faster. It does make the code larger, but also about 10% faster, I think. Performance: ATmega sizes '168 '328(P) '644(P) '1280 Program: 19696 bytes 138% 65% 32% 16% Data: 2191 bytes 214% 107% 54% 27% EEPROM: 32 bytes 4% 2% 2% 1% short-moves.gcode statistics: LED on occurences: 888. LED on time minimum: 263 clock cycles. LED on time maximum: 532 clock cycles. LED on time average: 269.273 clock cycles. smooth-curves.gcode statistics: LED on occurences: 23648. LED on time minimum: 255 clock cycles. LED on time maximum: 571 clock cycles. LED on time average: 297.792 clock cycles. triangle-odd.gcode statistics: LED on occurences: 1636. LED on time minimum: 255 clock cycles. LED on time maximum: 522 clock cycles. LED on time average: 283.861 clock cycles. --- dda.c | 14 ++++++++++---- dda.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dda.c b/dda.c index be43087..65dae85 100644 --- a/dda.c +++ b/dda.c @@ -187,6 +187,7 @@ void dda_create(DDA *dda, const TARGET *target) { dda->id = idcnt++; #endif + dda->active_axes = 0; // Handle bot axes. They're subject to kinematics considerations. code_axes_to_stepper_axes(&startpoint, target, delta_um, steps); for (i = X; i < E; i++) { @@ -194,6 +195,8 @@ void dda_create(DDA *dda, const TARGET *target) { delta_steps = steps[i] - startpoint_steps.axis[i]; dda->delta[i] = (uint32_t)labs(delta_steps); + if (delta_steps) + dda->active_axes |= 1 << i; startpoint_steps.axis[i] = steps[i]; set_direction(dda, i, delta_steps); @@ -255,6 +258,9 @@ void dda_create(DDA *dda, const TARGET *target) { dda->e_direction = (target->axis[E] >= 0)?1:0; } + if (dda->delta[E]) + dda->active_axes |= 1 << E; + if (DEBUG_DDA && (debug_flags & DEBUG_DDA)) sersendf_P(PSTR("[%ld,%ld,%ld,%ld]"), target->axis[X] - startpoint.axis[X], target->axis[Y] - startpoint.axis[Y], @@ -566,28 +572,28 @@ void dda_start(DDA *dda) { void dda_step(DDA *dda) { #if ! defined ACCELERATION_TEMPORAL - if (dda->delta[X]) { + if (dda->active_axes & (1 << X)) { move_state.counter[X] -= dda->delta[X]; if (move_state.counter[X] < 0) { x_step(); move_state.counter[X] += dda->total_steps; } } - if (dda->delta[Y]) { + if (dda->active_axes & (1 << Y)) { move_state.counter[Y] -= dda->delta[Y]; if (move_state.counter[Y] < 0) { y_step(); move_state.counter[Y] += dda->total_steps; } } - if (dda->delta[Z]) { + if (dda->active_axes & (1 << Z)) { move_state.counter[Z] -= dda->delta[Z]; if (move_state.counter[Z] < 0) { z_step(); move_state.counter[Z] += dda->total_steps; } } - if (dda->delta[E]) { + if (dda->active_axes & (1 << E)) { move_state.counter[E] -= dda->delta[E]; if (move_state.counter[E] < 0) { e_step(); diff --git a/dda.h b/dda.h index 0aa853f..dfa70c7 100644 --- a/dda.h +++ b/dda.h @@ -156,6 +156,7 @@ typedef struct { /// word boundaries only and fill smaller variables in between with gaps, /// so keep small variables grouped together to reduce the amount of these /// gaps. See e.g. NXP application note AN10963, page 10f. + uint8_t active_axes; ///< Bit mask of moving axes uint8_t fast_axis; ///< number of the fast axis /// Endstop homing