Skip to content

Commit 51be231

Browse files
committed
dda: move dda->n out into a new state variable
This required to also introduce dda_init() and re-adjust the number of accelerating steps a bit. Goal of this is to make look-ahead possible by just reducing the number of deceleration steps and acceleration steps of the next move. dda->c and dda->n no longer go down to their initial values, then. Also, quite a number of variables in the dda struct are used only when the dda is live, so there is no need to store that for each movement of the queue.
1 parent 2f7619a commit 51be231

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

dda.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "timer.h"
1313
#include "serial.h"
1414
#include "sermsg.h"
15+
#include "gcode_parse.h"
1516
#include "dda_queue.h"
1617
#include "debug.h"
1718
#include "sersendf.h"
@@ -39,6 +40,10 @@ TARGET startpoint __attribute__ ((__section__ (".bss")));
3940
/// \todo make current_position = real_position (from endstops) + offset from G28 and friends
4041
TARGET current_position __attribute__ ((__section__ (".bss")));
4142

43+
/// \var move_state
44+
/// \brief numbers for tracking the current state of movement
45+
MOVE_STATE move_state __attribute__ ((__section__ (".bss")));
46+
4247
/*
4348
utility functions
4449
*/
@@ -160,6 +165,17 @@ const uint8_t msbloc (uint32_t v) {
160165
return 0;
161166
}
162167

168+
/*! Inititalise DDA movement structures
169+
*/
170+
void dda_init(void) {
171+
// set up default feedrate
172+
current_position.F = startpoint.F = next_target.target.F = SEARCH_FEEDRATE_Z;
173+
174+
#ifdef ACCELERATION_RAMPING
175+
move_state.n = 1;
176+
#endif
177+
}
178+
163179
/*! CREATE a dda given current_position and a target, save to passed location so we can write directly into the queue
164180
\param *dda pointer to a dda_queue entry to overwrite
165181
\param *target the target position of this move
@@ -350,10 +366,6 @@ void dda_create(DDA *dda, TARGET *target) {
350366
if (dda->rampup_steps > dda->total_steps / 2)
351367
dda->rampup_steps = dda->total_steps / 2;
352368
dda->rampdown_steps = dda->total_steps - dda->rampup_steps;
353-
if (dda->rampup_steps == 0)
354-
dda->rampup_steps = 1;
355-
dda->n = 1; // TODO: this ends exactly where the next move starts,
356-
// so it should go out of dda->... into some state variable
357369
#else
358370
dda->c = (move_duration / target->F) << 8;
359371
if (dda->c < c_limit)
@@ -524,31 +536,31 @@ void dda_step(DDA *dda) {
524536

525537
// debug ramping algorithm
526538
//if (dda->step_no == 0) {
527-
// sersendf_P(PSTR("\r\nc %lu c_min %lu n %ld"), dda->c, dda->c_min, dda->n);
539+
// sersendf_P(PSTR("\r\nc %lu c_min %lu n %ld"), dda->c, dda->c_min, move_state.n);
528540
//}
529541

530542
recalc_speed = 0;
531-
if (dda->step_no <= dda->rampup_steps) {
532-
if (dda->n < 0) // wrong ramp direction
533-
dda->n = -((int32_t)2) - dda->n;
543+
if (dda->step_no < dda->rampup_steps) {
544+
if (move_state.n < 0) // wrong ramp direction
545+
move_state.n = -((int32_t)2) - move_state.n;
534546
recalc_speed = 1;
535547
}
536-
else if (dda->step_no >= dda->rampdown_steps) {
537-
if (dda->n > 0) // wrong ramp direction
538-
dda->n = -((int32_t)2) - dda->n;
548+
else if (dda->step_no > dda->rampdown_steps) {
549+
if (move_state.n > 0) // wrong ramp direction
550+
move_state.n = -((int32_t)2) - move_state.n;
539551
recalc_speed = 1;
540552
}
541553
if (recalc_speed) {
542-
dda->n += 4;
554+
move_state.n += 4;
543555
// be careful of signedness!
544-
dda->c = (int32_t)dda->c - ((int32_t)(dda->c * 2) / dda->n);
556+
dda->c = (int32_t)dda->c - ((int32_t)(dda->c * 2) / move_state.n);
545557
}
546558
dda->step_no++;
547559

548560
// debug ramping algorithm
549561
// for very low speeds like 10 mm/min, only
550562
//if (dda->step_no % 10 /* 10, 100, ...*/ == 0)
551-
// sersendf_P(PSTR("\r\nc %lu c_min %lu n %ld"), dda->c, dda->c_min, dda->n);
563+
// sersendf_P(PSTR("\r\nc %lu c_min %lu n %ld"), dda->c, dda->c_min, move_state.n);
552564
#endif
553565

554566
// TODO: did_step is obsolete ...

dda.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ typedef struct {
3434
uint32_t F;
3535
} TARGET;
3636

37+
/**
38+
\struct MOVE_STATE
39+
\brief this struct is made for tracking the current state of the movement
40+
41+
Parts of this struct are initialised only once per reboot, so make sure dda_step() leaves them with a value compatible to begin a new movement at the end of the movement. Other parts are filled in by dda_start().
42+
*/
43+
typedef struct {
44+
// // bresenham counters
45+
// int32_t x_counter; ///< counter for total_steps vs this axis
46+
// int32_t y_counter; ///< counter for total_steps vs this axis
47+
// int32_t z_counter; ///< counter for total_steps vs this axis
48+
// int32_t e_counter; ///< counter for total_steps vs this axis
49+
50+
// // step counters
51+
// uint32_t x_steps; ///< number of steps on X axis
52+
// uint32_t y_steps; ///< number of steps on Y axis
53+
// uint32_t z_steps; ///< number of steps on Z axis
54+
// uint32_t e_steps; ///< number of steps on E axis
55+
56+
#ifdef ACCELERATION_RAMPING
57+
// /// time until next step
58+
// uint32_t c;
59+
// /// counts actual steps done
60+
// uint32_t step_no;
61+
/// tracking variable
62+
int32_t n;
63+
#endif
64+
} MOVE_STATE;
65+
3766
/**
3867
\struct DDA
3968
\brief this is a digital differential analyser data struct
@@ -102,8 +131,6 @@ typedef struct {
102131
uint32_t step_no;
103132
/// 24.8 fixed point timer value, maximum speed
104133
uint32_t c_min;
105-
/// tracking variable
106-
int32_t n;
107134
#endif
108135
} DDA;
109136

@@ -131,6 +158,9 @@ uint32_t approx_distance_3( uint32_t dx, uint32_t dy, uint32_t dz ) __attribute_
131158
// const because return value is always the same given the same v
132159
const uint8_t msbloc (uint32_t v) __attribute__ ((const));
133160

161+
// initialize dda structures
162+
void dda_init(void);
163+
134164
// create a DDA
135165
void dda_create(DDA *dda, TARGET *target);
136166

mendel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ void init(void) {
225225
// read PID settings from EEPROM
226226
heater_init();
227227

228-
// set up default feedrate
229-
current_position.F = startpoint.F = next_target.target.F = SEARCH_FEEDRATE_Z;
228+
// set up dda
229+
dda_init();
230230

231231
// start up analog read interrupt loop,
232232
// if any of the temp sensors in your config.h use analog interface

0 commit comments

Comments
 (0)