diff --git a/clock.c b/clock.c index 97e00c6..53adf65 100644 --- a/clock.c +++ b/clock.c @@ -64,5 +64,7 @@ void clock_10ms() { ifclock(CLOCK_FLAG_250MS) { clock_250ms(); } + + update_position(); } diff --git a/dda.c b/dda.c index be515a4..1bf4590 100644 --- a/dda.c +++ b/dda.c @@ -22,16 +22,6 @@ #include "heater.h" #endif -// Used in distance calculation during DDA setup -/// micrometers per step X -#define UM_PER_STEP_X 1000L / ((uint32_t) STEPS_PER_MM_X) -/// micrometers per step Y -#define UM_PER_STEP_Y 1000L / ((uint32_t) STEPS_PER_MM_Y) -/// micrometers per step Z -#define UM_PER_STEP_Z 1000L / ((uint32_t) STEPS_PER_MM_Z) -/// micrometers per step E -#define UM_PER_STEP_E 1000L / ((uint32_t) STEPS_PER_MM_E) - /// step timeout uint8_t steptimeout = 0; @@ -193,10 +183,10 @@ void dda_create(DDA *dda, TARGET *target) { // we end at the passed target memcpy(&(dda->endpoint), target, sizeof(TARGET)); - dda->x_delta = labs(target->X - startpoint.X); - dda->y_delta = labs(target->Y - startpoint.Y); - dda->z_delta = labs(target->Z - startpoint.Z); - dda->e_delta = labs(target->E - startpoint.E); + dda->x_steps = dda->x_delta = labs(target->X - startpoint.X); + dda->y_steps = dda->y_delta = labs(target->Y - startpoint.Y); + dda->z_steps = dda->z_delta = labs(target->Z - startpoint.Z); + dda->e_steps = dda->e_delta = labs(target->E - startpoint.E); dda->x_direction = (target->X >= startpoint.X)?1:0; dda->y_direction = (target->Y >= startpoint.Y)?1:0; @@ -352,21 +342,6 @@ void dda_create(DDA *dda, TARGET *target) { dda->c_min = c_limit; dda->n = 1; dda->ramp_state = RAMP_UP; - #elif defined ACCELERATION_TEMPORAL - dda->x_counter = dda->x_step_interval = move_duration / dda->x_delta; - dda->y_counter = dda->y_step_interval = move_duration / dda->y_delta; - dda->z_counter = dda->z_step_interval = move_duration / dda->z_delta; - dda->e_counter = dda->e_step_interval = move_duration / dda->e_delta; - - dda->c = dda->x_step_interval; - if (dda->y_step_interval < dda->c) - dda->c = dda->y_step_interval; - if (dda->z_step_interval < dda->c) - dda->c = dda->z_step_interval; - if (dda->e_step_interval < dda->c) - dda->c = dda->e_step_interval; - - dda->c <<= 8; #else dda->c = (move_duration / target->F) << 8; if (dda->c < c_limit) @@ -379,10 +354,10 @@ void dda_create(DDA *dda, TARGET *target) { // next dda starts where we finish memcpy(&startpoint, target, sizeof(TARGET)); - // if E is relative, reset it here - #ifndef E_ABSOLUTE - startpoint.E = 0; - #endif + // if E is relative, reset it here + #ifndef E_ABSOLUTE + startpoint.E = 0; + #endif } /*! Start a prepared DDA @@ -450,115 +425,68 @@ void dda_step(DDA *dda) { // called from interrupt context! keep it as simple as possible uint8_t did_step = 0; - #ifdef ACCELERATION_TEMPORAL - if (dda->x_counter <= 0) { - if ((current_position.X != dda->endpoint.X) /* && - (x_max() != dda->x_direction) && (x_min() == dda->x_direction) */) { - x_step(); - if (dda->x_direction) - current_position.X++; - else - current_position.X--; - } - dda->x_counter += dda->x_step_interval; - dda->x_delta--; - } - if (dda->y_counter <= 0) { - if ((current_position.Y != dda->endpoint.Y) /* && - (y_max() != dda->y_direction) && (y_min() == dda->y_direction) */) { - y_step(); - if (dda->y_direction) - current_position.Y++; - else - current_position.Y--; - } - dda->y_counter += dda->y_step_interval; - dda->y_delta--; - } - if (dda->z_counter <= 0) { - if ((current_position.Z != dda->endpoint.Z) /* && - (z_max() != dda->z_direction) && (z_min() == dda->z_direction) */) { - z_step(); - if (dda->z_direction) - current_position.Z++; - else - current_position.Z--; - } - dda->z_counter += dda->z_step_interval; - dda->z_delta--; - } - if (dda->e_counter <= 0) { - if ((current_position.E != dda->endpoint.E) /* && - (e_max() != dda->e_direction) && (e_min() == dda->e_direction) */) { - e_step(); - if (dda->e_direction) - current_position.E++; - else - current_position.E--; - } - dda->e_counter += dda->e_step_interval; - dda->e_delta--; - } - #else - if ((current_position.X != dda->endpoint.X) /* && - (x_max() != dda->x_direction) && (x_min() == dda->x_direction) */) { - dda->x_counter -= dda->x_delta; - if (dda->x_counter < 0) { - x_step(); - did_step = 1; - if (dda->x_direction) - current_position.X++; - else - current_position.X--; + if ((dda->x_steps) /* && + (x_max() != dda->x_direction) && (x_min() == dda->x_direction) */) { + dda->x_counter -= dda->x_delta; + if (dda->x_counter < 0) { + x_step(); + did_step = 1; + dda->x_steps--; + // if (dda->x_direction) + // current_position.X++; + // else + // current_position.X--; - dda->x_counter += dda->total_steps; - } + dda->x_counter += dda->total_steps; } + } - if ((current_position.Y != dda->endpoint.Y) /* && - (y_max() != dda->y_direction) && (y_min() == dda->y_direction) */) { - dda->y_counter -= dda->y_delta; - if (dda->y_counter < 0) { - y_step(); - did_step = 1; - if (dda->y_direction) - current_position.Y++; - else - current_position.Y--; + if ((dda->y_steps) /* && + (y_max() != dda->y_direction) && (y_min() == dda->y_direction) */) { + dda->y_counter -= dda->y_delta; + if (dda->y_counter < 0) { + y_step(); + did_step = 1; + dda->y_steps--; + // if (dda->y_direction) + // current_position.Y++; + // else + // current_position.Y--; - dda->y_counter += dda->total_steps; - } + dda->y_counter += dda->total_steps; } + } - if ((current_position.Z != dda->endpoint.Z) /* && - (z_max() != dda->z_direction) && (z_min() == dda->z_direction) */) { - dda->z_counter -= dda->z_delta; - if (dda->z_counter < 0) { - z_step(); - did_step = 1; - if (dda->z_direction) - current_position.Z++; - else - current_position.Z--; + if ((dda->z_steps) /* && + (z_max() != dda->z_direction) && (z_min() == dda->z_direction) */) { + dda->z_counter -= dda->z_delta; + if (dda->z_counter < 0) { + z_step(); + did_step = 1; + dda->z_steps--; + // if (dda->z_direction) + // current_position.Z++; + // else + // current_position.Z--; - dda->z_counter += dda->total_steps; - } + dda->z_counter += dda->total_steps; } + } - if (current_position.E != dda->endpoint.E) { - dda->e_counter -= dda->e_delta; - if (dda->e_counter < 0) { - e_step(); - did_step = 1; - if (dda->e_direction) - current_position.E++; - else - current_position.E--; + if (dda->e_steps) { + dda->e_counter -= dda->e_delta; + if (dda->e_counter < 0) { + e_step(); + did_step = 1; + dda->e_steps--; + // if (dda->e_direction) + // current_position.E++; + // else + // current_position.E--; - dda->e_counter += dda->total_steps; - } + dda->e_counter += dda->total_steps; } - #endif + } #if STEP_INTERRUPT_INTERRUPTIBLE // since we have sent steps to all the motors that will be stepping and the rest of this function isn't so time critical, @@ -613,32 +541,6 @@ void dda_step(DDA *dda) { } dda->step_no++; #endif - #ifdef ACCELERATION_TEMPORAL - dda->c = dda->x_counter; - if (dda->y_counter < dda->c) - dda->c = dda->y_counter; - if (dda->z_counter < dda->c) - dda->c = dda->z_counter; - if (dda->e_counter < dda->c) - dda->c = dda->e_counter; - - if (dda->x_delta) - dda->x_counter -= dda->c; - if (dda->y_delta) - dda->y_counter -= dda->c; - if (dda->z_delta) - dda->z_counter -= dda->c; - if (dda->e_delta) - dda->e_counter -= dda->c; - if ( - (dda->x_delta > 0) || - (dda->y_delta > 0) || - (dda->z_delta > 0) || - (dda->e_delta > 0)) - did_step = 1; - - dda->c <<= 8; - #endif if (did_step) { // we stepped, reset timeout @@ -647,12 +549,12 @@ void dda_step(DDA *dda) { // if we could do anything at all, we're still running // otherwise, must have finished } - else { + else if (dda->x_steps == 0 && dda->y_steps == 0 && dda->z_steps == 0 && dda->e_steps == 0) { dda->live = 0; - // if E is relative reset it - #ifndef E_ABSOLUTE - current_position.E = 0; - #endif + // if E is relative reset it + #ifndef E_ABSOLUTE + current_position.E = 0; + #endif // linear acceleration code doesn't alter F during a move, so we must update it here // in theory, we *could* update F every step, but that would require a divide in interrupt context which should be avoided if at all possible current_position.F = dda->endpoint.F; @@ -670,3 +572,32 @@ void dda_step(DDA *dda) { // we also hope that we don't step before the drivers register the low- limit maximum speed if you think this is a problem. unstep(); } + +/// update global current_position struct +void update_position() { + DDA *dda = &movebuffer[mb_tail]; + + if (dda->x_direction) + current_position.X = dda->endpoint.X - dda->x_steps; + else + current_position.X = dda->endpoint.X + dda->x_steps; + + if (dda->y_direction) + current_position.Y = dda->endpoint.Y - dda->y_steps; + else + current_position.Y = dda->endpoint.Y + dda->y_steps; + + if (dda->z_direction) + current_position.Z = dda->endpoint.Z - dda->z_steps; + else + current_position.Z = dda->endpoint.Z + dda->z_steps; + + #ifndef E_ABSOLUTE + current_position.E = dda->e_steps; + #else + if (dda->e_direction) + current_position.E = dda->endpoint.E - dda->e_steps; + else + current_position.E = dda->endpoint.E + dda->e_steps; + #endif +} diff --git a/dda.h b/dda.h index 067d6bb..4d05685 100644 --- a/dda.h +++ b/dda.h @@ -88,6 +88,12 @@ typedef struct { int32_t z_counter; ///< counter for total_steps vs this axis, used for bresenham calculations. int32_t e_counter; ///< counter for total_steps vs this axis, used for bresenham calculations. + // step counters + uint32_t x_steps; ///< number of steps on X axis + uint32_t y_steps; ///< number of steps on Y axis + uint32_t z_steps; ///< number of steps on Z axis + uint32_t e_steps; ///< number of steps on E axis + /// total number of steps: set to \f$\max(\Delta x, \Delta y, \Delta z, \Delta e)\f$ uint32_t total_steps; @@ -109,16 +115,6 @@ typedef struct { /// keep track of whether we're ramping up, down, or plateauing ramp_state_t ramp_state; #endif - #ifdef ACCELERATION_TEMPORAL - /// time between steps on X axis - uint32_t x_step_interval; - /// time between steps on Y axis - uint32_t y_step_interval; - /// time between steps on Z axis - uint32_t z_step_interval; - /// time between steps on E axis - uint32_t e_step_interval; - #endif } DDA; /* @@ -153,4 +149,7 @@ void dda_start(DDA *dda) __attribute__ ((hot)); // DDA takes one step (called from timer interrupt) void dda_step(DDA *dda) __attribute__ ((hot)); +// update current_position +void update_position(void); + #endif /* _DDA_H */