Merge pull request #5573 from thinkyhead/rc_unify_stepper_isr

LIN_ADVANCE with unified stepper ISR
2.0.x
Scott Lahteine 8 years ago committed by GitHub
commit fe2ea2906f

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -530,14 +530,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -560,14 +560,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -555,14 +555,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -549,14 +549,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -549,14 +549,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -549,14 +549,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -554,14 +554,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -549,14 +549,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -547,14 +547,7 @@
*
* Assumption: advance = k * (delta velocity)
* K=0 means advance disabled.
* To get a rough start value for calibration, measure your "free filament length"
* between the hobbed bolt and the nozzle (in cm). Use the formula below that fits
* your setup, where L is the "free filament length":
*
* Filament diameter | 1.75mm | 3.0mm |
* ----------------------------|-----------|------------|
* Stiff filament (PLA) | K=47*L/10 | K=139*L/10 |
* Softer filament (ABS, nGen) | K=88*L/10 | K=260*L/10 |
* See Marlin documentation for calibration instructions.
*/
//#define LIN_ADVANCE

@ -91,8 +91,9 @@ volatile uint32_t Stepper::step_events_completed = 0; // The number of step even
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
uint8_t Stepper::old_OCR0A = 0;
volatile uint8_t Stepper::eISR_Rate = 200; // Keep the ISR at a low rate until needed
uint16_t Stepper::nextMainISR = 0,
Stepper::nextAdvanceISR = 65535,
Stepper::eISR_Rate = 65535;
#if ENABLED(LIN_ADVANCE)
volatile int Stepper::e_steps[E_STEPPERS];
@ -328,16 +329,23 @@ void Stepper::set_directions() {
* 2000 1 KHz - sleep rate
* 4000 500 Hz - init rate
*/
ISR(TIMER1_COMPA_vect) { Stepper::isr(); }
ISR(TIMER1_COMPA_vect) {
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
Stepper::advance_isr_scheduler();
#else
Stepper::isr();
#endif
}
void Stepper::isr() {
#define _ENABLE_ISRs() cli(); SBI(TIMSK0, OCIE0B); ENABLE_STEPPER_DRIVER_INTERRUPT()
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
//Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
CBI(TIMSK0, OCIE0A); //estepper ISR
#endif
CBI(TIMSK0, OCIE0B); //Temperature ISR
DISABLE_STEPPER_DRIVER_INTERRUPT();
sei();
#endif
if (cleaning_buffer_counter) {
--cleaning_buffer_counter;
@ -346,13 +354,8 @@ void Stepper::isr() {
#ifdef SD_FINISHED_RELEASECOMMAND
if (!cleaning_buffer_counter && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
#endif
OCR1A = 200; // Run at max speed - 10 KHz
//re-enable ISRs
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
SBI(TIMSK0, OCIE0A);
#endif
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
_NEXT_ISR(200); // Run at max speed - 10 KHz
_ENABLE_ISRs(); // re-enable ISRs
return;
}
@ -381,12 +384,8 @@ void Stepper::isr() {
#if ENABLED(Z_LATE_ENABLE)
if (current_block->steps[Z_AXIS] > 0) {
enable_z();
OCR1A = 2000; // Run at slow speed - 1 KHz
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
SBI(TIMSK0, OCIE0A);
#endif
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
_NEXT_ISR(2000); // Run at slow speed - 1 KHz
_ENABLE_ISRs(); // re-enable ISRs
return;
}
#endif
@ -396,12 +395,8 @@ void Stepper::isr() {
// #endif
}
else {
OCR1A = 2000; // Run at slow speed - 1 KHz
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
SBI(TIMSK0, OCIE0A);
#endif
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
_NEXT_ISR(2000); // Run at slow speed - 1 KHz
_ENABLE_ISRs(); // re-enable ISRs
return;
}
}
@ -586,7 +581,7 @@ void Stepper::isr() {
#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;
if (e_steps[TOOL_E_INDEX]) nextAdvanceISR = 0;
#endif
// Calculate new timer value
@ -600,7 +595,7 @@ void Stepper::isr() {
// step_rate to timer interval
uint16_t timer = calc_timer(acc_step_rate);
OCR1A = timer;
_NEXT_ISR(timer);
acceleration_time += timer;
#if ENABLED(LIN_ADVANCE)
@ -637,7 +632,7 @@ void Stepper::isr() {
#endif // ADVANCE or LIN_ADVANCE
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
eISR_Rate = (timer >> 3) * step_loops / abs(e_steps[TOOL_E_INDEX]); //>> 3 is divide by 8. Reason: Timer 1 runs at 16/8=2MHz, Timer 0 at 16/64=0.25MHz. ==> 2/0.25=8.
eISR_Rate = !e_steps[TOOL_E_INDEX] ? 65535 : timer * step_loops / abs(e_steps[TOOL_E_INDEX]);
#endif
}
else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
@ -653,7 +648,7 @@ void Stepper::isr() {
// step_rate to timer interval
uint16_t timer = calc_timer(step_rate);
OCR1A = timer;
_NEXT_ISR(timer);
deceleration_time += timer;
#if ENABLED(LIN_ADVANCE)
@ -688,7 +683,7 @@ void Stepper::isr() {
#endif // ADVANCE or LIN_ADVANCE
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
eISR_Rate = (timer >> 3) * step_loops / abs(e_steps[TOOL_E_INDEX]);
eISR_Rate = !e_steps[TOOL_E_INDEX] ? 65535 : timer * step_loops / abs(e_steps[TOOL_E_INDEX]);
#endif
}
else {
@ -698,39 +693,36 @@ void Stepper::isr() {
if (current_block->use_advance_lead)
current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
eISR_Rate = (OCR1A_nominal >> 3) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
eISR_Rate = !e_steps[TOOL_E_INDEX] ? 65535 : OCR1A_nominal * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
#endif
OCR1A = OCR1A_nominal;
_NEXT_ISR(OCR1A_nominal);
// ensure we're running at the correct step rate, even if we just came off an acceleration
step_loops = step_loops_nominal;
}
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
NOLESS(OCR1A, TCNT1 + 16);
#endif
// If current block is finished, reset pointer
if (all_steps_done) {
current_block = NULL;
planner.discard_current_block();
}
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
SBI(TIMSK0, OCIE0A);
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
_ENABLE_ISRs(); // re-enable ISRs
#endif
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
}
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
// Timer interrupt for E. e_steps is set in the main routine;
// Timer 0 is shared with millies
ISR(TIMER0_COMPA_vect) { Stepper::advance_isr(); }
void Stepper::advance_isr() {
old_OCR0A += eISR_Rate;
OCR0A = old_OCR0A;
nextAdvanceISR = eISR_Rate;
#define SET_E_STEP_DIR(INDEX) \
if (e_steps[INDEX]) E## INDEX ##_DIR_WRITE(e_steps[INDEX] < 0 ? INVERT_E## INDEX ##_DIR : !INVERT_E## INDEX ##_DIR)
@ -795,6 +787,46 @@ void Stepper::isr() {
}
void Stepper::advance_isr_scheduler() {
// Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
CBI(TIMSK0, OCIE0B); // Temperature ISR
DISABLE_STEPPER_DRIVER_INTERRUPT();
sei();
// Run main stepping ISR if flagged
if (!nextMainISR) isr();
// Run Advance stepping ISR if flagged
if (!nextAdvanceISR) advance_isr();
// Is the next advance ISR scheduled before the next main ISR?
if (nextAdvanceISR <= nextMainISR) {
// Set up the next interrupt
OCR1A = nextAdvanceISR;
// New interval for the next main ISR
if (nextMainISR) nextMainISR -= nextAdvanceISR;
// Will call Stepper::advance_isr on the next interrupt
nextAdvanceISR = 0;
}
else {
// The next main ISR comes first
OCR1A = nextMainISR;
// New interval for the next advance ISR, if any
if (nextAdvanceISR && nextAdvanceISR != 65535)
nextAdvanceISR -= nextMainISR;
// Will call Stepper::isr on the next interrupt
nextMainISR = 0;
}
// Don't run the ISR faster than possible
NOLESS(OCR1A, TCNT1 + 16);
// Restore original ISR settings
cli();
SBI(TIMSK0, OCIE0B);
ENABLE_STEPPER_DRIVER_INTERRUPT();
}
#endif // ADVANCE or LIN_ADVANCE
void Stepper::init() {
@ -981,12 +1013,6 @@ void Stepper::init() {
#endif
}
#if defined(TCCR0A) && defined(WGM01)
CBI(TCCR0A, WGM01);
CBI(TCCR0A, WGM00);
#endif
SBI(TIMSK0, OCIE0A);
#endif // ADVANCE or LIN_ADVANCE
endstops.enable(true); // Start with endstops active. After homing they can be disabled

@ -105,8 +105,8 @@ class Stepper {
static volatile uint32_t step_events_completed; // The number of step events executed in the current block
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
static uint8_t old_OCR0A;
static volatile uint8_t eISR_Rate;
static uint16_t nextMainISR, nextAdvanceISR, eISR_Rate;
#define _NEXT_ISR(T) nextMainISR = T
#if ENABLED(LIN_ADVANCE)
static volatile int e_steps[E_STEPPERS];
static int final_estep_rate;
@ -119,6 +119,8 @@ class Stepper {
static long advance_rate, advance, final_advance;
static long old_advance;
#endif
#else
#define _NEXT_ISR(T) OCR1A = T
#endif // ADVANCE or LIN_ADVANCE
static long acceleration_time, deceleration_time;
@ -177,6 +179,7 @@ class Stepper {
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
static void advance_isr();
static void advance_isr_scheduler();
#endif
//
@ -318,8 +321,8 @@ class Stepper {
return timer;
}
// Initializes the trapezoid generator from the current block. Called whenever a new
// block begins.
// Initialize the trapezoid generator from the current block.
// Called whenever a new block begins.
static FORCE_INLINE void trapezoid_generator_reset() {
static int8_t last_extruder = -1;
@ -357,7 +360,7 @@ class Stepper {
step_loops_nominal = step_loops;
acc_step_rate = current_block->initial_rate;
acceleration_time = calc_timer(acc_step_rate);
OCR1A = acceleration_time;
_NEXT_ISR(acceleration_time);
#if ENABLED(LIN_ADVANCE)
if (current_block->use_advance_lead) {

Loading…
Cancel
Save