Reduce / optimize LIN_ADVANCE code

master
Scott Lahteine 8 years ago
parent 271ced7341
commit 97b6fb6381

@ -672,8 +672,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
#endif #endif
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
const float target_float[XYZE] = { a, b, c, e }, const float mm_D_float = sqrt(sq(a - position_float[X_AXIS]) + sq(b - position_float[Y_AXIS]));
mm_D_float = sqrt(sq(target_float[X_AXIS] - position_float[X_AXIS]) + sq(target_float[Y_AXIS] - position_float[Y_AXIS]));
#endif #endif
const long da = target[X_AXIS] - position[X_AXIS], const long da = target[X_AXIS] - position[X_AXIS],
@ -707,13 +706,14 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
if (DEBUGGING(DRYRUN)) { if (DEBUGGING(DRYRUN)) {
position[E_AXIS] = target[E_AXIS]; position[E_AXIS] = target[E_AXIS];
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
position_float[E_AXIS] = target_float[E_AXIS]; position_float[E_AXIS] = e;
#endif #endif
} }
long de = target[E_AXIS] - position[E_AXIS]; long de = target[E_AXIS] - position[E_AXIS];
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
float de_float = target_float[E_AXIS] - position_float[E_AXIS]; float de_float = e - position_float[E_AXIS];
#endif #endif
#if ENABLED(PREVENT_COLD_EXTRUSION) #if ENABLED(PREVENT_COLD_EXTRUSION)
@ -722,7 +722,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
de = 0; // no difference de = 0; // no difference
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
position_float[E_AXIS] = target_float[E_AXIS]; position_float[E_AXIS] = e;
de_float = 0; de_float = 0;
#endif #endif
SERIAL_ECHO_START; SERIAL_ECHO_START;
@ -733,7 +733,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
de = 0; // no difference de = 0; // no difference
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
position_float[E_AXIS] = target_float[E_AXIS]; position_float[E_AXIS] = e;
de_float = 0; de_float = 0;
#endif #endif
SERIAL_ECHO_START; SERIAL_ECHO_START;
@ -1356,7 +1356,10 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
// Update the position (only when a move was queued) // Update the position (only when a move was queued)
memcpy(position, target, sizeof(position)); memcpy(position, target, sizeof(position));
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
memcpy(position_float, target_float, sizeof(position_float)); position_float[X_AXIS] = a;
position_float[Y_AXIS] = b;
position_float[Z_AXIS] = c;
position_float[E_AXIS] = e;
#endif #endif
recalculate(); recalculate();

@ -342,13 +342,14 @@ ISR(TIMER1_COMPA_vect) {
#endif #endif
} }
void Stepper::isr() { #define _ENABLE_ISRs() do { cli(); if (thermalManager.in_temp_isr) CBI(TIMSK0, OCIE0B); else SBI(TIMSK0, OCIE0B); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
#define _ENABLE_ISRs() cli(); SBI(TIMSK0, OCIE0B); ENABLE_STEPPER_DRIVER_INTERRUPT()
uint16_t timer, remainder, ocr_val; void Stepper::isr() {
static uint32_t step_remaining = 0; static uint32_t step_remaining = 0;
uint16_t ocr_val;
#define ENDSTOP_NOMINAL_OCR_VAL 3000 // check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch #define ENDSTOP_NOMINAL_OCR_VAL 3000 // check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
#define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms #define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms
@ -366,7 +367,7 @@ void Stepper::isr() {
#define SPLIT(L) do { \ #define SPLIT(L) do { \
_SPLIT(L); \ _SPLIT(L); \
if (ENDSTOPS_ENABLED && L > ENDSTOP_NOMINAL_OCR_VAL) { \ if (ENDSTOPS_ENABLED && L > ENDSTOP_NOMINAL_OCR_VAL) { \
remainder = (uint16_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \ uint16_t remainder = (uint16_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \
ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \ ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \
step_remaining = (uint16_t)L - ocr_val; \ step_remaining = (uint16_t)L - ocr_val; \
} \ } \
@ -374,13 +375,16 @@ void Stepper::isr() {
if (step_remaining && ENDSTOPS_ENABLED) { // Just check endstops - not yet time for a step if (step_remaining && ENDSTOPS_ENABLED) { // Just check endstops - not yet time for a step
endstops.update(); endstops.update();
ocr_val = step_remaining;
if (step_remaining > ENDSTOP_NOMINAL_OCR_VAL) { if (step_remaining > ENDSTOP_NOMINAL_OCR_VAL) {
step_remaining = step_remaining - ENDSTOP_NOMINAL_OCR_VAL; step_remaining -= ENDSTOP_NOMINAL_OCR_VAL;
ocr_val = ENDSTOP_NOMINAL_OCR_VAL; ocr_val = ENDSTOP_NOMINAL_OCR_VAL;
} }
else step_remaining = 0; // last one before the ISR that does the step else {
_NEXT_ISR(ocr_val); // ocr_val = step_remaining;
step_remaining = 0; // last one before the ISR that does the step
}
_NEXT_ISR(ocr_val);
NOLESS(OCR1A, TCNT1 + 16); NOLESS(OCR1A, TCNT1 + 16);
@ -867,9 +871,7 @@ void Stepper::isr() {
NOLESS(OCR1A, TCNT1 + 16); NOLESS(OCR1A, TCNT1 + 16);
// Restore original ISR settings // Restore original ISR settings
cli(); _ENABLE_ISRs();
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
} }
#endif // ADVANCE or LIN_ADVANCE #endif // ADVANCE or LIN_ADVANCE

Loading…
Cancel
Save