Fix Bresenham rounding errors, add link to article (#10871)

This commit is contained in:
Eduardo José Tagle 2018-05-27 03:49:59 -03:00 committed by Scott Lahteine
parent 2566342979
commit 7b9f0302d4

View file

@ -111,6 +111,13 @@ bool Stepper::abort_current_block;
bool Stepper::locked_z_motor = false, Stepper::locked_z2_motor = false; bool Stepper::locked_z_motor = false, Stepper::locked_z2_motor = false;
#endif #endif
/**
* Marlin uses the Bresenham algorithm. For a detailed explanation of theory and
* method see https://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
*
* The implementation used here additionally rounds up the starting seed.
*/
int32_t Stepper::counter_X = 0, int32_t Stepper::counter_X = 0,
Stepper::counter_Y = 0, Stepper::counter_Y = 0,
Stepper::counter_Z = 0, Stepper::counter_Z = 0,
@ -1294,12 +1301,12 @@ void Stepper::stepper_pulse_phase_isr() {
// Advance the Bresenham counter; start a pulse if the axis needs a step // Advance the Bresenham counter; start a pulse if the axis needs a step
#define PULSE_START(AXIS) do{ \ #define PULSE_START(AXIS) do{ \
_COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \ _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); } \ if (_COUNTER(AXIS) >= 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); } \
}while(0) }while(0)
// Advance the Bresenham counter; start a pulse if the axis needs a step // Advance the Bresenham counter; start a pulse if the axis needs a step
#define STEP_TICK(AXIS) do { \ #define STEP_TICK(AXIS) do { \
if (_COUNTER(AXIS) > 0) { \ if (_COUNTER(AXIS) >= 0) { \
_COUNTER(AXIS) -= current_block->step_event_count; \ _COUNTER(AXIS) -= current_block->step_event_count; \
count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \ count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
} \ } \
@ -1387,7 +1394,7 @@ void Stepper::stepper_pulse_phase_isr() {
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
counter_E += current_block->steps[E_AXIS]; counter_E += current_block->steps[E_AXIS];
if (counter_E > 0) { if (counter_E >= 0) {
#if DISABLED(MIXING_EXTRUDER) #if DISABLED(MIXING_EXTRUDER)
// Don't step E here for mixing extruder // Don't step E here for mixing extruder
motor_direction(E_AXIS) ? --e_steps : ++e_steps; motor_direction(E_AXIS) ? --e_steps : ++e_steps;
@ -1399,7 +1406,7 @@ void Stepper::stepper_pulse_phase_isr() {
const bool dir = motor_direction(E_AXIS); const bool dir = motor_direction(E_AXIS);
MIXING_STEPPERS_LOOP(j) { MIXING_STEPPERS_LOOP(j) {
counter_m[j] += current_block->steps[E_AXIS]; counter_m[j] += current_block->steps[E_AXIS];
if (counter_m[j] > 0) { if (counter_m[j] >= 0) {
counter_m[j] -= current_block->mix_event_count[j]; counter_m[j] -= current_block->mix_event_count[j];
dir ? --e_steps[j] : ++e_steps[j]; dir ? --e_steps[j] : ++e_steps[j];
} }
@ -1416,7 +1423,7 @@ void Stepper::stepper_pulse_phase_isr() {
// Step mixing steppers (proportionally) // Step mixing steppers (proportionally)
counter_m[j] += current_block->steps[E_AXIS]; counter_m[j] += current_block->steps[E_AXIS];
// Step when the counter goes over zero // Step when the counter goes over zero
if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN); if (counter_m[j] >= 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
} }
#else // !MIXING_EXTRUDER #else // !MIXING_EXTRUDER
PULSE_START(E); PULSE_START(E);
@ -1456,7 +1463,7 @@ void Stepper::stepper_pulse_phase_isr() {
#if DISABLED(LIN_ADVANCE) #if DISABLED(LIN_ADVANCE)
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
MIXING_STEPPERS_LOOP(j) { MIXING_STEPPERS_LOOP(j) {
if (counter_m[j] > 0) { if (counter_m[j] >= 0) {
counter_m[j] -= current_block->mix_event_count[j]; counter_m[j] -= current_block->mix_event_count[j];
En_STEP_WRITE(j, INVERT_E_STEP_PIN); En_STEP_WRITE(j, INVERT_E_STEP_PIN);
} }
@ -1738,11 +1745,11 @@ uint32_t Stepper::stepper_block_phase_isr() {
bezier_2nd_half = false; bezier_2nd_half = false;
#endif #endif
// Initialize Bresenham counters to 1/2 the ceiling // Initialize Bresenham counters to 1/2 the ceiling, with proper roundup (as explained in the article linked above)
counter_X = counter_Y = counter_Z = counter_E = -((int32_t)(current_block->step_event_count >> 1)); counter_X = counter_Y = counter_Z = counter_E = -int32_t((current_block->step_event_count + 1) >> 1);
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
MIXING_STEPPERS_LOOP(i) MIXING_STEPPERS_LOOP(i)
counter_m[i] = -(current_block->mix_event_count[i] >> 1); counter_m[i] = -int32_t((current_block->mix_event_count[i] + 1) >> 1);
#endif #endif
#if ENABLED(Z_LATE_ENABLE) #if ENABLED(Z_LATE_ENABLE)