Browse Source

Less indentation in Stepper::isr

pull/1/head
Scott Lahteine 8 years ago
parent
commit
cc639d7d9c
  1. 478
      Marlin/stepper.cpp

478
Marlin/stepper.cpp

@ -357,316 +357,314 @@ void Stepper::isr() {
} }
else { else {
OCR1A = 2000; // 1kHz. OCR1A = 2000; // 1kHz.
return;
} }
} }
if (current_block) { // Update endstops state, if enabled
if (endstops.enabled
#if HAS_BED_PROBE
|| endstops.z_probe_enabled
#endif
) endstops.update();
// Update endstops state, if enabled // Take multiple steps per interrupt (For high speed moves)
if (endstops.enabled bool all_steps_done = false;
#if HAS_BED_PROBE for (int8_t i = 0; i < step_loops; i++) {
|| endstops.z_probe_enabled #ifndef USBCON
#endif customizedSerial.checkRx(); // Check for serial chars.
) endstops.update(); #endif
// Take multiple steps per interrupt (For high speed moves) #if ENABLED(LIN_ADVANCE)
bool all_steps_done = false;
for (int8_t i = 0; i < step_loops; i++) {
#ifndef USBCON
customizedSerial.checkRx(); // Check for serial chars.
#endif
#if ENABLED(LIN_ADVANCE) counter_E += current_block->steps[E_AXIS];
if (counter_E > 0) {
counter_E -= current_block->step_event_count;
#if DISABLED(MIXING_EXTRUDER)
// Don't step E here for mixing extruder
count_position[E_AXIS] += count_direction[E_AXIS];
motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX];
#endif
}
counter_E += current_block->steps[E_AXIS]; #if ENABLED(MIXING_EXTRUDER)
if (counter_E > 0) { // Step mixing steppers proportionally
counter_E -= current_block->step_event_count; bool dir = motor_direction(E_AXIS);
#if DISABLED(MIXING_EXTRUDER) MIXING_STEPPERS_LOOP(j) {
// Don't step E here for mixing extruder counter_m[j] += current_block->steps[E_AXIS];
count_position[E_AXIS] += count_direction[E_AXIS]; if (counter_m[j] > 0) {
motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX]; counter_m[j] -= current_block->mix_event_count[j];
#endif dir ? --e_steps[j] : ++e_steps[j];
}
} }
#endif
if (current_block->use_advance_lead) {
int delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[TOOL_E_INDEX]) >> 9) - current_adv_steps[TOOL_E_INDEX];
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
// Step mixing steppers proportionally // Mixing extruders apply advance lead proportionally
bool dir = motor_direction(E_AXIS);
MIXING_STEPPERS_LOOP(j) { MIXING_STEPPERS_LOOP(j) {
counter_m[j] += current_block->steps[E_AXIS]; int steps = delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
if (counter_m[j] > 0) { e_steps[j] += steps;
counter_m[j] -= current_block->mix_event_count[j]; current_adv_steps[j] += steps;
dir ? --e_steps[j] : ++e_steps[j];
}
} }
#else
// For most extruders, advance the single E stepper
e_steps[TOOL_E_INDEX] += delta_adv_steps;
current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
#endif #endif
}
if (current_block->use_advance_lead) { #elif ENABLED(ADVANCE)
int delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[TOOL_E_INDEX]) >> 9) - current_adv_steps[TOOL_E_INDEX];
#if ENABLED(MIXING_EXTRUDER)
// Mixing extruders apply advance lead proportionally
MIXING_STEPPERS_LOOP(j) {
int steps = delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
e_steps[j] += steps;
current_adv_steps[j] += steps;
}
#else
// For most extruders, advance the single E stepper
e_steps[TOOL_E_INDEX] += delta_adv_steps;
current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
#endif
}
#elif ENABLED(ADVANCE)
// Always count the unified E axis // Always count the unified E axis
counter_E += current_block->steps[E_AXIS]; counter_E += current_block->steps[E_AXIS];
if (counter_E > 0) { if (counter_E > 0) {
counter_E -= current_block->step_event_count; counter_E -= current_block->step_event_count;
#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[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX]; motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX];
#endif #endif
} }
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
// Step mixing steppers proportionally // Step mixing steppers proportionally
bool dir = motor_direction(E_AXIS); 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];
}
} }
}
#endif // MIXING_EXTRUDER #endif // MIXING_EXTRUDER
#endif // ADVANCE or LIN_ADVANCE
#define _COUNTER(AXIS) counter_## AXIS
#define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
#define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
// Advance the Bresenham counter; start a pulse if the axis needs a step #endif // ADVANCE or LIN_ADVANCE
#define PULSE_START(AXIS) \
_COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
// Stop an active pulse, reset the Bresenham counter, update the position #define _COUNTER(AXIS) counter_## AXIS
#define PULSE_STOP(AXIS) \ #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
if (_COUNTER(AXIS) > 0) { \ #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
_COUNTER(AXIS) -= current_block->step_event_count; \
count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
_APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
}
// If a minimum pulse time was specified get the CPU clock // Advance the Bresenham counter; start a pulse if the axis needs a step
#if MINIMUM_STEPPER_PULSE > 0 #define PULSE_START(AXIS) \
static uint32_t pulse_start; _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
pulse_start = TCNT0; if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
#endif
#if HAS_X_STEP // Stop an active pulse, reset the Bresenham counter, update the position
PULSE_START(X); #define PULSE_STOP(AXIS) \
#endif if (_COUNTER(AXIS) > 0) { \
#if HAS_Y_STEP _COUNTER(AXIS) -= current_block->step_event_count; \
PULSE_START(Y); count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
#endif _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
#if HAS_Z_STEP }
PULSE_START(Z);
#endif
// For non-advance use linear interpolation for E also // If a minimum pulse time was specified get the CPU clock
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE) #if MINIMUM_STEPPER_PULSE > 0
#if ENABLED(MIXING_EXTRUDER) static uint32_t pulse_start;
// Keep updating the single E axis pulse_start = TCNT0;
counter_E += current_block->steps[E_AXIS]; #endif
// Tick the counters used for this mix
MIXING_STEPPERS_LOOP(j) {
// Step mixing steppers (proportionally)
counter_m[j] += current_block->steps[E_AXIS];
// Step when the counter goes over zero
if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
}
#else // !MIXING_EXTRUDER
PULSE_START(E);
#endif
#endif // !ADVANCE && !LIN_ADVANCE
// For a minimum pulse time wait before stopping pulses #if HAS_X_STEP
#if MINIMUM_STEPPER_PULSE > 0 PULSE_START(X);
#define CYCLES_EATEN_BY_CODE 10 #endif
while ((uint32_t)(TCNT0 - pulse_start) < (MINIMUM_STEPPER_PULSE * (F_CPU / 1000000UL)) - CYCLES_EATEN_BY_CODE) { /* nada */ } #if HAS_Y_STEP
#endif PULSE_START(Y);
#endif
#if HAS_Z_STEP
PULSE_START(Z);
#endif
#if HAS_X_STEP // For non-advance use linear interpolation for E also
PULSE_STOP(X); #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
#endif #if ENABLED(MIXING_EXTRUDER)
#if HAS_Y_STEP // Keep updating the single E axis
PULSE_STOP(Y); counter_E += current_block->steps[E_AXIS];
#endif // Tick the counters used for this mix
#if HAS_Z_STEP MIXING_STEPPERS_LOOP(j) {
PULSE_STOP(Z); // Step mixing steppers (proportionally)
counter_m[j] += current_block->steps[E_AXIS];
// Step when the counter goes over zero
if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
}
#else // !MIXING_EXTRUDER
PULSE_START(E);
#endif #endif
#endif // !ADVANCE && !LIN_ADVANCE
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE) // For a minimum pulse time wait before stopping pulses
#if ENABLED(MIXING_EXTRUDER) #if MINIMUM_STEPPER_PULSE > 0
// Always step the single E axis #define CYCLES_EATEN_BY_CODE 10
if (counter_E > 0) { while ((uint32_t)(TCNT0 - pulse_start) < (MINIMUM_STEPPER_PULSE * (F_CPU / 1000000UL)) - CYCLES_EATEN_BY_CODE) { /* nada */ }
counter_E -= current_block->step_event_count;
count_position[E_AXIS] += count_direction[E_AXIS];
}
MIXING_STEPPERS_LOOP(j) {
if (counter_m[j] > 0) {
counter_m[j] -= current_block->mix_event_count[j];
En_STEP_WRITE(j, INVERT_E_STEP_PIN);
}
}
#else // !MIXING_EXTRUDER
PULSE_STOP(E);
#endif
#endif // !ADVANCE && !LIN_ADVANCE
if (++step_events_completed >= current_block->step_event_count) {
all_steps_done = true;
break;
}
}
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
// If we have esteps to execute, fire the next advance_isr "now"
if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
#endif #endif
// Calculate new timer value #if HAS_X_STEP
uint16_t timer, step_rate; PULSE_STOP(X);
if (step_events_completed <= (uint32_t)current_block->accelerate_until) { #endif
#if HAS_Y_STEP
PULSE_STOP(Y);
#endif
#if HAS_Z_STEP
PULSE_STOP(Z);
#endif
MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate); #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
acc_step_rate += current_block->initial_rate; #if ENABLED(MIXING_EXTRUDER)
// Always step the single E axis
if (counter_E > 0) {
counter_E -= current_block->step_event_count;
count_position[E_AXIS] += count_direction[E_AXIS];
}
MIXING_STEPPERS_LOOP(j) {
if (counter_m[j] > 0) {
counter_m[j] -= current_block->mix_event_count[j];
En_STEP_WRITE(j, INVERT_E_STEP_PIN);
}
}
#else // !MIXING_EXTRUDER
PULSE_STOP(E);
#endif
#endif // !ADVANCE && !LIN_ADVANCE
// upper limit if (++step_events_completed >= current_block->step_event_count) {
NOMORE(acc_step_rate, current_block->nominal_rate); all_steps_done = true;
break;
}
}
// step_rate to timer interval #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
timer = calc_timer(acc_step_rate); // If we have esteps to execute, fire the next advance_isr "now"
OCR1A = timer; if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
acceleration_time += timer; #endif
#if ENABLED(LIN_ADVANCE) // Calculate new timer value
uint16_t timer, step_rate;
if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
if (current_block->use_advance_lead) MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8; acc_step_rate += current_block->initial_rate;
if (current_block->use_advance_lead) { // upper limit
#if ENABLED(MIXING_EXTRUDER) NOMORE(acc_step_rate, current_block->nominal_rate);
MIXING_STEPPERS_LOOP(j)
current_estep_rate[j] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
#else
current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
#endif
}
#elif ENABLED(ADVANCE) // step_rate to timer interval
timer = calc_timer(acc_step_rate);
OCR1A = timer;
acceleration_time += timer;
advance += advance_rate * step_loops; #if ENABLED(LIN_ADVANCE)
//NOLESS(advance, current_block->advance);
long advance_whole = advance >> 8, if (current_block->use_advance_lead)
advance_factor = advance_whole - old_advance; current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
// Do E steps + advance steps if (current_block->use_advance_lead) {
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
// ...for mixing steppers proportionally
MIXING_STEPPERS_LOOP(j) MIXING_STEPPERS_LOOP(j)
e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j]; current_estep_rate[j] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
#else #else
// ...for the active extruder current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
e_steps[TOOL_E_INDEX] += advance_factor;
#endif #endif
}
old_advance = advance_whole; #elif ENABLED(ADVANCE)
#endif // ADVANCE or LIN_ADVANCE advance += advance_rate * step_loops;
//NOLESS(advance, current_block->advance);
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE) long advance_whole = advance >> 8,
eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]); advance_factor = advance_whole - old_advance;
#endif
}
else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
if (step_rate < acc_step_rate) { // Still decelerating? // Do E steps + advance steps
step_rate = acc_step_rate - step_rate; #if ENABLED(MIXING_EXTRUDER)
NOLESS(step_rate, current_block->final_rate); // ...for mixing steppers proportionally
} MIXING_STEPPERS_LOOP(j)
else e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j];
step_rate = current_block->final_rate; #else
// ...for the active extruder
e_steps[TOOL_E_INDEX] += advance_factor;
#endif
// step_rate to timer interval old_advance = advance_whole;
timer = calc_timer(step_rate);
OCR1A = timer;
deceleration_time += timer;
#if ENABLED(LIN_ADVANCE) #endif // ADVANCE or LIN_ADVANCE
if (current_block->use_advance_lead) { #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
#if ENABLED(MIXING_EXTRUDER) eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
MIXING_STEPPERS_LOOP(j) #endif
current_estep_rate[j] = ((uint32_t)step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8; }
#else else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->e_speed_multiplier8) >> 8; MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
#endif
}
#elif ENABLED(ADVANCE) if (step_rate < acc_step_rate) { // Still decelerating?
step_rate = acc_step_rate - step_rate;
NOLESS(step_rate, current_block->final_rate);
}
else
step_rate = current_block->final_rate;
advance -= advance_rate * step_loops; // step_rate to timer interval
NOLESS(advance, final_advance); timer = calc_timer(step_rate);
OCR1A = timer;
deceleration_time += timer;
// Do E steps + advance steps #if ENABLED(LIN_ADVANCE)
long advance_whole = advance >> 8,
advance_factor = advance_whole - old_advance;
if (current_block->use_advance_lead) {
#if ENABLED(MIXING_EXTRUDER) #if ENABLED(MIXING_EXTRUDER)
MIXING_STEPPERS_LOOP(j) MIXING_STEPPERS_LOOP(j)
e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j]; current_estep_rate[j] = ((uint32_t)step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
#else #else
e_steps[TOOL_E_INDEX] += advance_factor; current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->e_speed_multiplier8) >> 8;
#endif #endif
}
#elif ENABLED(ADVANCE)
old_advance = advance_whole; advance -= advance_rate * step_loops;
NOLESS(advance, final_advance);
#endif // ADVANCE or LIN_ADVANCE // Do E steps + advance steps
long advance_whole = advance >> 8,
advance_factor = advance_whole - old_advance;
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE) #if ENABLED(MIXING_EXTRUDER)
eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]); MIXING_STEPPERS_LOOP(j)
e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j];
#else
e_steps[TOOL_E_INDEX] += advance_factor;
#endif #endif
}
else {
#if ENABLED(LIN_ADVANCE) old_advance = advance_whole;
if (current_block->use_advance_lead) #endif // ADVANCE or LIN_ADVANCE
current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
eISR_Rate = (OCR1A_nominal >> 2) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]); #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
#endif
}
else {
#endif #if ENABLED(LIN_ADVANCE)
OCR1A = OCR1A_nominal; if (current_block->use_advance_lead)
// ensure we're running at the correct step rate, even if we just came off an acceleration current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
step_loops = step_loops_nominal;
}
NOLESS(OCR1A, TCNT1 + 16); eISR_Rate = (OCR1A_nominal >> 2) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
// If current block is finished, reset pointer #endif
if (all_steps_done) {
current_block = NULL; OCR1A = OCR1A_nominal;
planner.discard_current_block(); // ensure we're running at the correct step rate, even if we just came off an acceleration
} step_loops = step_loops_nominal;
}
NOLESS(OCR1A, TCNT1 + 16);
// If current block is finished, reset pointer
if (all_steps_done) {
current_block = NULL;
planner.discard_current_block();
} }
} }

Loading…
Cancel
Save