@ -89,13 +89,24 @@ long Stepper::counter_X = 0,
volatile unsigned long Stepper : : step_events_completed = 0 ; // The number of step events executed in the current block
volatile unsigned long Stepper : : step_events_completed = 0 ; // The number of step events executed in the current block
# if ENABLED(ADVANCE)
# if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
unsigned char Stepper : : old_OCR0A ;
unsigned char Stepper : : old_OCR0A ;
long Stepper : : final_advance = 0 ,
volatile unsigned char Stepper : : eISR_Rate = 200 ; // Keep the ISR at a low rate until needed
# if ENABLED(LIN_ADVANCE)
volatile int Stepper : : e_steps [ EXTRUDERS ] ;
int Stepper : : extruder_advance_k = LIN_ADVANCE_K ,
Stepper : : final_estep_rate ,
Stepper : : current_estep_rate [ EXTRUDERS ] ,
Stepper : : current_adv_steps [ EXTRUDERS ] ;
# else
long Stepper : : e_steps [ EXTRUDERS ] ,
Stepper : : final_advance = 0 ,
Stepper : : old_advance = 0 ,
Stepper : : old_advance = 0 ,
Stepper : : e_steps [ EXTRUDERS ] ,
Stepper : : advance_rate ,
Stepper : : advance_rate ,
Stepper : : advance ;
Stepper : : advance ;
# endif
# endif
# endif
long Stepper : : acceleration_time , Stepper : : deceleration_time ;
long Stepper : : acceleration_time , Stepper : : deceleration_time ;
@ -344,29 +355,31 @@ void Stepper::isr() {
customizedSerial . checkRx ( ) ; // Check for serial chars.
customizedSerial . checkRx ( ) ; // Check for serial chars.
# endif
# endif
# if ENABLED(ADVANCE)
counter_E + = current_block - > steps [ E_AXIS ] ;
if ( counter_E > 0 ) {
counter_E - = current_block - > step_event_count ;
e_steps [ current_block - > active_extruder ] + = motor_direction ( E_AXIS ) ? - 1 : 1 ;
}
# endif //ADVANCE
# 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 ) {
counter_E - = current_block - > step_event_count ;
counter_E - = current_block - > step_event_count ;
count_position [ _AXIS( E ) ] + = count_direction [ _AXIS( E ) ] ;
count_position [ E_AXIS ] + = count_direction [ E_AXIS ] ;
e_steps [ current_block - > active_extruder ] + = motor_direction ( E_AXIS ) ? - 1 : 1 ;
e_steps [ current_block - > active_extruder ] + = motor_direction ( E_AXIS ) ? - 1 : 1 ;
}
}
if ( current_block - > use_advance_lead ) {
if ( current_block - > use_advance_lead ) {
int delta_adv_steps ; //Maybe a char would be enough?
int delta_adv_steps ; //Maybe a char would be enough?
delta_adv_steps = ( ( ( long ) extruder_advance_k * current_estep_rate [ current_block - > active_extruder ] ) > > 9 ) - current_adv_steps [ current_block - > active_extruder ] ;
delta_adv_steps = ( ( ( long ) extruder_advance_k * current_estep_rate [ current_block - > active_extruder ] ) > > 9 ) - current_adv_steps [ current_block - > active_extruder ] ;
e_steps [ current_block - > active_extruder ] + = delta_adv_steps ;
e_steps [ current_block - > active_extruder ] + = delta_adv_steps ;
current_adv_steps [ current_block - > active_extruder ] + = delta_adv_steps ;
current_adv_steps [ current_block - > active_extruder ] + = delta_adv_steps ;
}
}
# endif //LIN_ADVANCE
# elif ENABLED(ADVANCE)
counter_E + = current_block - > steps [ E_AXIS ] ;
if ( counter_E > 0 ) {
counter_E - = current_block - > step_event_count ;
e_steps [ current_block - > active_extruder ] + = motor_direction ( E_AXIS ) ? - 1 : 1 ;
}
# endif // ADVANCE or LIN_ADVANCE
# define _COUNTER(AXIS) counter_## AXIS
# define _COUNTER(AXIS) counter_## AXIS
# define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
# define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
@ -379,7 +392,7 @@ void Stepper::isr() {
STEP_ADD ( X ) ;
STEP_ADD ( X ) ;
STEP_ADD ( Y ) ;
STEP_ADD ( Y ) ;
STEP_ADD ( Z ) ;
STEP_ADD ( Z ) ;
# if ( DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE) )
# if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
STEP_ADD ( E ) ;
STEP_ADD ( E ) ;
# endif
# endif
@ -393,7 +406,7 @@ void Stepper::isr() {
STEP_IF_COUNTER ( X ) ;
STEP_IF_COUNTER ( X ) ;
STEP_IF_COUNTER ( Y ) ;
STEP_IF_COUNTER ( Y ) ;
STEP_IF_COUNTER ( Z ) ;
STEP_IF_COUNTER ( Z ) ;
# if ( DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE) )
# if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
STEP_IF_COUNTER ( E ) ;
STEP_IF_COUNTER ( E ) ;
# endif
# endif
@ -416,12 +429,11 @@ void Stepper::isr() {
acceleration_time + = timer ;
acceleration_time + = timer ;
# if ENABLED(LIN_ADVANCE)
# if ENABLED(LIN_ADVANCE)
if ( current_block - > use_advance_lead ) {
if ( current_block - > use_advance_lead )
current_estep_rate [ current_block - > active_extruder ] = ( ( unsigned long ) acc_step_rate * current_block - > e_speed_multiplier8 ) > > 8 ;
current_estep_rate [ current_block - > active_extruder ] = ( ( unsigned long ) acc_step_rate * current_block - > e_speed_multiplier8 ) > > 8 ;
}
# endif
# if ENABLED(ADVANCE)
# elif ENABLED(ADVANCE)
advance + = advance_rate * step_loops ;
advance + = advance_rate * step_loops ;
//NOLESS(advance, current_block->advance);
//NOLESS(advance, current_block->advance);
@ -430,7 +442,8 @@ void Stepper::isr() {
e_steps [ current_block - > active_extruder ] + = ( ( advance > > 8 ) - old_advance ) ;
e_steps [ current_block - > active_extruder ] + = ( ( advance > > 8 ) - old_advance ) ;
old_advance = advance > > 8 ;
old_advance = advance > > 8 ;
# endif //ADVANCE
# endif // ADVANCE or LIN_ADVANCE
}
}
else if ( step_events_completed > ( unsigned long ) current_block - > decelerate_after ) {
else if ( step_events_completed > ( unsigned long ) current_block - > decelerate_after ) {
MultiU24X32toH16 ( step_rate , deceleration_time , current_block - > acceleration_rate ) ;
MultiU24X32toH16 ( step_rate , deceleration_time , current_block - > acceleration_rate ) ;
@ -448,12 +461,12 @@ void Stepper::isr() {
deceleration_time + = timer ;
deceleration_time + = timer ;
# if ENABLED(LIN_ADVANCE)
# if ENABLED(LIN_ADVANCE)
if ( current_block - > use_advance_lead ) {
if ( current_block - > use_advance_lead )
current_estep_rate [ current_block - > active_extruder ] = ( ( unsigned long ) step_rate * current_block - > e_speed_multiplier8 ) > > 8 ;
current_estep_rate [ current_block - > active_extruder ] = ( ( unsigned long ) step_rate * current_block - > e_speed_multiplier8 ) > > 8 ;
}
# endif
# if ENABLED(ADVANCE)
# elif ENABLED(ADVANCE)
advance - = advance_rate * step_loops ;
advance - = advance_rate * step_loops ;
NOLESS ( advance , final_advance ) ;
NOLESS ( advance , final_advance ) ;
@ -461,13 +474,13 @@ void Stepper::isr() {
uint32_t advance_whole = advance > > 8 ;
uint32_t advance_whole = advance > > 8 ;
e_steps [ current_block - > active_extruder ] + = advance_whole - old_advance ;
e_steps [ current_block - > active_extruder ] + = advance_whole - old_advance ;
old_advance = advance_whole ;
old_advance = advance_whole ;
# endif //ADVANCE
# endif // ADVANCE or LIN_ADVANCE
}
}
else {
else {
# if def LIN_ADVANCE
# if ENABLED( LIN_ADVANCE)
if ( current_block - > use_advance_lead ) {
if ( current_block - > use_advance_lead )
current_estep_rate [ current_block - > active_extruder ] = final_estep_rate ;
current_estep_rate [ current_block - > active_extruder ] = final_estep_rate ;
}
# endif
# endif
OCR1A = OCR1A_nominal ;
OCR1A = OCR1A_nominal ;
@ -485,12 +498,14 @@ void Stepper::isr() {
}
}
}
}
# if ENABLED(ADVANCE)
# if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
// Timer interrupt for E. e_steps is set in the main routine;
// Timer interrupt for E. e_steps is set in the main routine;
// Timer 0 is shared with millies
// Timer 0 is shared with millies
ISR ( TIMER0_COMPA_vect ) { Stepper : : advance_isr ( ) ; }
ISR ( TIMER0_COMPA_vect ) { Stepper : : advance_isr ( ) ; }
void Stepper : : advance_isr ( ) {
void Stepper : : advance_isr ( ) {
old_OCR0A + = 52 ; // ~10kHz interrupt (250000 / 26 = 9615kHz)
old_OCR0A + = 52 ; // ~10kHz interrupt (250000 / 26 = 9615kHz)
OCR0A = old_OCR0A ;
OCR0A = old_OCR0A ;
@ -521,58 +536,10 @@ void Stepper::isr() {
# endif
# endif
# endif
# endif
}
}
}
# endif // ADVANCE
# if ENABLED(LIN_ADVANCE)
unsigned char old_OCR0A ;
// 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 + = 52 ; // ~10kHz interrupt (250000 / 26 = 9615kHz) war 52
OCR0A = old_OCR0A ;
# define STEP_E_ONCE(INDEX) \
if ( e_steps [ INDEX ] ! = 0 ) { \
E # # INDEX # # _STEP_WRITE ( INVERT_E_STEP_PIN ) ; \
if ( e_steps [ INDEX ] < 0 ) { \
E # # INDEX # # _DIR_WRITE ( INVERT_E # # INDEX # # _DIR ) ; \
e_steps [ INDEX ] + + ; \
} \
else if ( e_steps [ INDEX ] > 0 ) { \
E # # INDEX # # _DIR_WRITE ( ! INVERT_E # # INDEX # # _DIR ) ; \
e_steps [ INDEX ] - - ; \
} \
E # # INDEX # # _STEP_WRITE ( ! INVERT_E_STEP_PIN ) ; \
}
}
// Step all E steppers that have steps, up to 4 steps per interrupt
# endif // ADVANCE or LIN_ADVANCE
for ( unsigned char i = 0 ; i < 4 ; i + + ) {
# if EXTRUDERS > 3
switch ( current_block - > active_extruder ) { case 3 : STEP_E_ONCE ( 3 ) ; break ; case 2 : STEP_E_ONCE ( 2 ) ; break ; case 1 : STEP_E_ONCE ( 1 ) ; break ; default : STEP_E_ONCE ( 0 ) ; }
# elif EXTRUDERS > 2
switch ( current_block - > active_extruder ) { case 2 : STEP_E_ONCE ( 2 ) ; break ; case 1 : STEP_E_ONCE ( 1 ) ; break ; default : STEP_E_ONCE ( 0 ) ; }
# elif EXTRUDERS > 1
# if DISABLED(DUAL_X_CARRIAGE)
if ( current_block - > active_extruder = = 1 ) { STEP_E_ONCE ( 1 ) } else { STEP_E_ONCE ( 0 ) ; }
# else
extern bool extruder_duplication_enabled ;
if ( extruder_duplication_enabled ) {
STEP_E_ONCE ( 0 ) ;
STEP_E_ONCE ( 1 ) ;
} else {
if ( current_block - > active_extruder = = 1 ) { STEP_E_ONCE ( 1 ) } else { STEP_E_ONCE ( 0 ) ; }
}
# endif
# else
STEP_E_ONCE ( 0 ) ;
# endif
}
}
# endif // LIN_ADVANCE
void Stepper : : init ( ) {
void Stepper : : init ( ) {
@ -739,26 +706,28 @@ void Stepper::init() {
TCNT1 = 0 ;
TCNT1 = 0 ;
ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
# if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
# if ENABLED(LIN_ADVANCE)
# if ENABLED(LIN_ADVANCE)
for ( int i = 0 ; i < EXTRUDERS ; i + + ) {
for ( int i = 0 ; i < EXTRUDERS ; i + + ) {
e_steps [ i ] = 0 ;
e_steps [ i ] = 0 ;
current_adv_steps [ i ] = 0 ;
current_adv_steps [ i ] = 0 ;
}
}
# if defined(TCCR0A) && defined(WGM01)
CBI ( TCCR0A , WGM01 ) ;
# elif ENABLED(ADVANCE)
CBI ( TCCR0A , WGM00 ) ;
for ( uint8_t i = 0 ; i < EXTRUDERS ; i + + ) e_steps [ i ] = 0 ;
# endif
# endif
SBI ( TIMSK0 , OCIE0A ) ;
# endif //LIN_ADVANCE
# if ENABLED(ADVANCE)
# if defined(TCCR0A) && defined(WGM01)
# if defined(TCCR0A) && defined(WGM01)
CBI ( TCCR0A , WGM01 ) ;
CBI ( TCCR0A , WGM01 ) ;
CBI ( TCCR0A , WGM00 ) ;
CBI ( TCCR0A , WGM00 ) ;
# endif
# endif
for ( uint8_t i = 0 ; i < EXTRUDERS ; i + + ) e_steps [ i ] = 0 ;
SBI ( TIMSK0 , OCIE0A ) ;
SBI ( TIMSK0 , OCIE0A ) ;
# endif //ADVANCE
# endif // ADVANCE or LIN_ADVANCE
endstops . enable ( true ) ; // Start with endstops active. After homing they can be disabled
endstops . enable ( true ) ; // Start with endstops active. After homing they can be disabled
sei ( ) ;
sei ( ) ;
@ -1137,15 +1106,12 @@ void Stepper::microstep_readings() {
}
}
# if ENABLED(LIN_ADVANCE)
# if ENABLED(LIN_ADVANCE)
void Stepper : : advance_M905 ( ) {
void Stepper : : advance_M905 ( ) {
if ( code_seen ( ' K ' ) ) extruder_advance_k = code_value ( ) ;
if ( code_seen ( ' K ' ) ) extruder_advance_k = code_value ( ) ;
SERIAL_ECHO_START ;
SERIAL_ECHO_START ;
SERIAL_ECHOPGM ( " Advance factor: " ) ;
SERIAL_ECHOPAIR ( " Advance factor: " , extruder_advance_k ) ;
SERIAL_CHAR ( ' ' ) ;
SERIAL_EOL ;
SERIAL_ECHOLN ( extruder_advance_k ) ;
}
}
int Stepper : : get_advance_k ( ) {
# endif // LIN_ADVANCE
return extruder_advance_k ;
}
# endif