2016-03-25 07:19:46 +01:00
/**
2016-03-24 19:01:20 +01:00
* Marlin 3 D Printer Firmware
* Copyright ( C ) 2016 MarlinFirmware [ https : //github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl .
* Copyright ( C ) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*
*/
2016-03-25 07:19:46 +01:00
/**
2016-04-27 16:15:20 +02:00
* stepper . h - stepper motor driver : executes motion plans of planner . c using the stepper motors
2016-05-08 21:16:26 +02:00
* Derived from Grbl
2016-04-27 16:15:20 +02:00
*
* Copyright ( c ) 2009 - 2011 Simen Svale Skogsrud
*
* Grbl is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Grbl is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Grbl . If not , see < http : //www.gnu.org/licenses/>.
*/
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
# ifndef STEPPER_H
# define STEPPER_H
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
# include "stepper_indirection.h"
2017-09-06 13:28:32 +02:00
2017-09-24 06:25:28 +02:00
# ifdef __AVR__
2017-09-06 13:28:32 +02:00
# include "speed_lookuptable.h"
# endif
# include "../inc/MarlinConfig.h"
# include "../module/planner.h"
# include "../core/language.h"
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
class Stepper ;
extern Stepper stepper ;
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
class Stepper {
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
public :
2016-05-12 00:24:24 +02:00
static block_t * current_block ; // A pointer to the block currently being traced
2016-04-27 16:15:20 +02:00
2017-10-29 09:43:44 +01:00
# if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
2018-06-03 03:39:00 +02:00
static bool homing_dual_axis ;
2016-04-27 16:15:20 +02:00
# endif
2017-06-03 07:38:07 +02:00
# if HAS_MOTOR_CURRENT_PWM
# ifndef PWM_MOTOR_CURRENT
# define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
# endif
static uint32_t motor_current_setting [ 3 ] ;
# endif
2016-04-27 16:15:20 +02:00
private :
2018-05-23 10:45:25 +02:00
static uint8_t last_direction_bits , // The next stepping-bits to be output
last_movement_extruder , // Last movement extruder, as computed when the last movement was fetched from planner
axis_did_move ; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
static bool abort_current_block ; // Signals to the stepper that current block should be aborted
2016-04-27 16:15:20 +02:00
2017-10-29 09:43:44 +01:00
# if ENABLED(X_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
static bool locked_X_motor , locked_X2_motor ;
2017-10-29 09:43:44 +01:00
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
static bool locked_Y_motor , locked_Y2_motor ;
2017-10-29 09:43:44 +01:00
# endif
2016-04-27 16:15:20 +02:00
# if ENABLED(Z_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
static bool locked_Z_motor , locked_Z2_motor ;
2016-04-27 16:15:20 +02:00
# endif
// Counter variables for the Bresenham line tracer
2018-05-04 03:13:01 +02:00
static int32_t counter_X , counter_Y , counter_Z , counter_E ;
2018-05-09 07:17:53 +02:00
static uint32_t step_events_completed ; // The number of step events executed in the current block
2016-04-27 16:15:20 +02:00
2018-05-26 09:02:39 +02:00
# if ENABLED(S_CURVE_ACCELERATION)
2018-04-12 01:13:42 +02:00
static int32_t bezier_A , // A coefficient in Bézier speed curve
bezier_B , // B coefficient in Bézier speed curve
bezier_C ; // C coefficient in Bézier speed curve
2018-05-21 05:20:11 +02:00
static uint32_t bezier_F , // F coefficient in Bézier speed curve
bezier_AV ; // AV coefficient in Bézier speed curve
2018-04-12 01:13:42 +02:00
# ifdef __AVR__
static bool A_negative ; // If A coefficient was negative
# endif
static bool bezier_2nd_half ; // If Bézier curve has been initialized or not
2018-04-07 03:48:06 +02:00
# endif
2018-05-09 07:17:53 +02:00
static uint32_t nextMainISR ; // time remaining for the next Step ISR
static bool all_steps_done ; // all steps done
2017-10-09 11:25:18 +02:00
# if ENABLED(LIN_ADVANCE)
2018-02-23 07:53:29 +01:00
static uint32_t LA_decelerate_after ; // Copy from current executed block. Needed because current_block is set to NULL "too early".
2018-05-09 07:17:53 +02:00
static uint32_t nextAdvanceISR , eISR_Rate ;
2018-02-23 07:53:29 +01:00
static uint16_t current_adv_steps , final_adv_steps , max_adv_steps ; // Copy from current executed block. Needed because current_block is set to NULL "too early".
static int8_t e_steps ;
static bool use_advance_lead ;
2018-03-07 02:21:41 +01:00
# if E_STEPPERS > 1
static int8_t LA_active_extruder ; // Copy from current executed block. Needed because current_block is set to NULL "too early".
# else
2018-03-07 12:49:10 +01:00
static constexpr int8_t LA_active_extruder = 0 ;
2018-03-07 02:21:41 +01:00
# endif
2018-02-23 07:53:29 +01:00
2018-05-09 07:17:53 +02:00
# endif // LIN_ADVANCE
2018-02-23 07:53:29 +01:00
2018-05-09 07:17:53 +02:00
static uint32_t acceleration_time , deceleration_time ;
2016-05-12 00:24:24 +02:00
static uint8_t step_loops , step_loops_nominal ;
2018-02-19 04:11:35 +01:00
2018-05-09 07:17:53 +02:00
static uint32_t ticks_nominal ;
2018-05-26 09:02:39 +02:00
# if DISABLED(S_CURVE_ACCELERATION)
2018-05-09 07:17:53 +02:00
static uint32_t acc_step_rate ; // needed for deceleration start point
2018-04-07 03:48:06 +02:00
# endif
2016-04-27 16:15:20 +02:00
2018-05-04 03:13:01 +02:00
static volatile int32_t endstops_trigsteps [ XYZ ] ;
static volatile int32_t endstops_stepsTotal , endstops_stepsDone ;
2016-04-27 16:15:20 +02:00
2016-09-15 10:18:10 +02:00
//
// Positions of stepper motors, in step units
//
2018-04-07 03:48:06 +02:00
static volatile int32_t count_position [ NUM_AXIS ] ;
2016-09-15 10:18:10 +02:00
2016-04-27 16:15:20 +02:00
//
// Current direction of stepper motors (+1 or -1)
//
2018-06-03 03:39:00 +02:00
static int8_t count_direction [ NUM_AXIS ] ;
2016-04-27 16:15:20 +02:00
2016-06-29 00:06:56 +02:00
//
// Mixing extruder mix counters
//
# if ENABLED(MIXING_EXTRUDER)
2018-05-04 03:13:01 +02:00
static int32_t counter_m [ MIXING_STEPPERS ] ;
2016-06-29 00:06:56 +02:00
# define MIXING_STEPPERS_LOOP(VAR) \
for ( uint8_t VAR = 0 ; VAR < MIXING_STEPPERS ; VAR + + ) \
if ( current_block - > mix_event_count [ VAR ] )
# endif
2016-04-27 16:15:20 +02:00
public :
//
// Constructor / initializer
//
2016-05-12 00:24:24 +02:00
Stepper ( ) { } ;
2016-04-27 16:15:20 +02:00
// Initialize stepper hardware
2016-06-03 02:57:56 +02:00
static void init ( ) ;
2016-04-27 16:15:20 +02:00
// Interrupt Service Routines
2018-05-09 07:17:53 +02:00
// The ISR scheduler
2018-06-02 02:02:22 +02:00
static void isr ( ) ;
2016-04-27 16:15:20 +02:00
2018-05-09 07:17:53 +02:00
// The stepper pulse phase ISR
static void stepper_pulse_phase_isr ( ) ;
2018-05-04 00:45:13 +02:00
2018-05-09 07:17:53 +02:00
// The stepper block processing phase ISR
static uint32_t stepper_block_phase_isr ( ) ;
2018-05-04 00:45:13 +02:00
2018-05-09 07:17:53 +02:00
# if ENABLED(LIN_ADVANCE)
// The Linear advance stepper ISR
static uint32_t advance_isr ( ) ;
# endif
2016-04-27 16:15:20 +02:00
// Get the position of a stepper, in steps
2018-05-04 03:13:01 +02:00
static int32_t position ( const AxisEnum axis ) ;
2016-04-27 16:15:20 +02:00
// Report the positions of the steppers, in steps
2016-06-03 02:57:56 +02:00
static void report_positions ( ) ;
2016-04-27 16:15:20 +02:00
// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
// to notify the subsystem that it is time to go to work.
2016-06-03 02:57:56 +02:00
static void wake_up ( ) ;
2016-04-27 16:15:20 +02:00
2018-05-09 07:17:53 +02:00
// Quickly stop all steppers
2018-05-16 09:08:43 +02:00
FORCE_INLINE static void quick_stop ( ) { abort_current_block = true ; }
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
// The direction of a single motor
2017-12-09 09:10:54 +01:00
FORCE_INLINE static bool motor_direction ( const AxisEnum axis ) { return TEST ( last_direction_bits , axis ) ; }
2011-12-12 19:34:37 +01:00
2018-05-16 09:08:43 +02:00
// The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
2018-05-23 10:45:25 +02:00
FORCE_INLINE static bool axis_is_moving ( const AxisEnum axis ) { return TEST ( axis_did_move , axis ) ; }
2018-05-16 09:08:43 +02:00
// The extruder associated to the last movement
FORCE_INLINE static uint8_t movement_extruder ( ) { return last_movement_extruder ; }
2018-05-09 07:17:53 +02:00
// Handle a triggered endstop
static void endstop_triggered ( const AxisEnum axis ) ;
// Triggered position of an axis in steps
static int32_t triggered_position ( const AxisEnum axis ) ;
2016-09-25 13:32:58 +02:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2017-06-25 05:23:45 +02:00
static void digitalPotWrite ( const int16_t address , const int16_t value ) ;
static void digipot_current ( const uint8_t driver , const int16_t current ) ;
2016-09-25 13:32:58 +02:00
# endif
# if HAS_MICROSTEPS
2017-06-25 05:23:45 +02:00
static void microstep_ms ( const uint8_t driver , const int8_t ms1 , const int8_t ms2 ) ;
static void microstep_mode ( const uint8_t driver , const uint8_t stepping ) ;
2016-09-25 13:32:58 +02:00
static void microstep_readings ( ) ;
2016-04-27 16:15:20 +02:00
# endif
2011-11-20 14:50:08 +01:00
2018-06-03 03:39:00 +02:00
# if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
FORCE_INLINE static void set_homing_dual_axis ( const bool state ) { homing_dual_axis = state ; }
# endif
2017-10-29 09:43:44 +01:00
# if ENABLED(X_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
FORCE_INLINE static void set_x_lock ( const bool state ) { locked_X_motor = state ; }
FORCE_INLINE static void set_x2_lock ( const bool state ) { locked_X2_motor = state ; }
2017-10-29 09:43:44 +01:00
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
FORCE_INLINE static void set_y_lock ( const bool state ) { locked_Y_motor = state ; }
FORCE_INLINE static void set_y2_lock ( const bool state ) { locked_Y2_motor = state ; }
2017-10-29 09:43:44 +01:00
# endif
2016-04-27 16:15:20 +02:00
# if ENABLED(Z_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
FORCE_INLINE static void set_z_lock ( const bool state ) { locked_Z_motor = state ; }
FORCE_INLINE static void set_z2_lock ( const bool state ) { locked_Z2_motor = state ; }
2016-04-27 16:15:20 +02:00
# endif
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
# if ENABLED(BABYSTEPPING)
2016-11-04 00:23:31 +01:00
static void babystep ( const AxisEnum axis , const bool direction ) ; // perform a short step with a single stepper motor, outside of any convention
2016-04-27 16:15:20 +02:00
# endif
2011-11-20 14:50:08 +01:00
2017-06-03 07:38:07 +02:00
# if HAS_MOTOR_CURRENT_PWM
static void refresh_motor_power ( ) ;
# endif
2018-05-21 22:05:17 +02:00
// Set the current position in steps
inline static void set_position ( const int32_t & a , const int32_t & b , const int32_t & c , const int32_t & e ) {
planner . synchronize ( ) ;
2018-06-02 02:02:22 +02:00
const bool was_enabled = STEPPER_ISR_ENABLED ( ) ;
if ( was_enabled ) DISABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
2018-05-21 22:05:17 +02:00
_set_position ( a , b , c , e ) ;
2018-06-02 02:02:22 +02:00
if ( was_enabled ) ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
2018-05-21 22:05:17 +02:00
}
inline static void set_position ( const AxisEnum a , const int32_t & v ) {
planner . synchronize ( ) ;
2018-06-02 02:02:22 +02:00
# ifdef __AVR__
// Protect the access to the position. Only required for AVR, as
// any 32bit CPU offers atomic access to 32bit variables
const bool was_enabled = STEPPER_ISR_ENABLED ( ) ;
if ( was_enabled ) DISABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
# endif
2018-05-21 22:05:17 +02:00
count_position [ a ] = v ;
2018-06-02 02:02:22 +02:00
# ifdef __AVR__
// Reenable Stepper ISR
if ( was_enabled ) ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
# endif
2018-05-21 22:05:17 +02:00
}
2016-05-04 02:00:28 +02:00
private :
2018-05-09 07:17:53 +02:00
// Set the current position in steps
static void _set_position ( const int32_t & a , const int32_t & b , const int32_t & c , const int32_t & e ) ;
// Set direction bits for all steppers
static void set_directions ( ) ;
2018-05-29 00:39:23 +02:00
// Limit the speed to 10KHz for AVR
# ifndef STEP_DOUBLER_FREQUENCY
# define STEP_DOUBLER_FREQUENCY 10000
# endif
2018-05-09 07:17:53 +02:00
FORCE_INLINE static uint32_t calc_timer_interval ( uint32_t step_rate ) {
uint32_t timer ;
2015-10-03 08:08:58 +02:00
2018-05-16 09:08:43 +02:00
NOMORE ( step_rate , uint32_t ( MAX_STEP_FREQUENCY ) ) ;
2011-11-20 14:50:08 +01:00
2018-05-29 00:39:23 +02:00
# if DISABLED(DISABLE_MULTI_STEPPING)
if ( step_rate > STEP_DOUBLER_FREQUENCY * 2 ) { // If steprate > (STEP_DOUBLER_FREQUENCY * 2) kHz >> step 4 times
2017-06-18 01:36:10 +02:00
step_rate > > = 2 ;
step_loops = 4 ;
}
2018-05-29 00:39:23 +02:00
else if ( step_rate > STEP_DOUBLER_FREQUENCY ) { // If steprate > STEP_DOUBLER_FREQUENCY kHz >> step 2 times
2017-06-18 01:36:10 +02:00
step_rate > > = 1 ;
step_loops = 2 ;
}
2018-05-29 00:39:23 +02:00
else
2017-06-18 01:36:10 +02:00
# endif
2018-05-29 00:39:23 +02:00
step_loops = 1 ;
2017-06-18 01:36:10 +02:00
# ifdef CPU_32_BIT
2017-09-27 11:57:14 +02:00
// In case of high-performance processor, it is able to calculate in real-time
2018-05-13 15:10:08 +02:00
const uint32_t min_time_per_step = ( HAL_STEPPER_TIMER_RATE ) / ( ( STEP_DOUBLER_FREQUENCY ) * 2 ) ;
2017-10-07 20:34:25 +02:00
timer = uint32_t ( HAL_STEPPER_TIMER_RATE ) / step_rate ;
2018-05-13 15:10:08 +02:00
NOLESS ( timer , min_time_per_step ) ; // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
2017-06-18 01:36:10 +02:00
# else
2018-05-29 00:39:23 +02:00
constexpr uint32_t min_step_rate = F_CPU / 500000U ;
NOLESS ( step_rate , min_step_rate ) ;
step_rate - = min_step_rate ; // Correct for minimal speed
2017-06-18 01:36:10 +02:00
if ( step_rate > = ( 8 * 256 ) ) { // higher step rate
2018-05-21 05:20:11 +02:00
const uint8_t tmp_step_rate = ( step_rate & 0x00FF ) ;
const uint16_t table_address = ( uint16_t ) & speed_lookuptable_fast [ ( uint8_t ) ( step_rate > > 8 ) ] [ 0 ] ,
gain = ( uint16_t ) pgm_read_word_near ( table_address + 2 ) ;
2018-05-13 15:10:08 +02:00
timer = MultiU16X8toH16 ( tmp_step_rate , gain ) ;
timer = ( uint16_t ) pgm_read_word_near ( table_address ) - timer ;
2017-06-18 01:36:10 +02:00
}
else { // lower step rates
2018-05-13 15:10:08 +02:00
uint16_t table_address = ( uint16_t ) & speed_lookuptable_slow [ 0 ] [ 0 ] ;
2018-05-08 16:17:44 +02:00
table_address + = ( ( step_rate ) > > 1 ) & 0xFFFC ;
2018-05-09 07:17:53 +02:00
timer = ( uint16_t ) pgm_read_word_near ( table_address )
- ( ( ( uint16_t ) pgm_read_word_near ( table_address + 2 ) * ( uint8_t ) ( step_rate & 0x0007 ) ) > > 3 ) ;
2017-06-18 01:36:10 +02:00
}
if ( timer < 100 ) { // (20kHz - this should never happen)
timer = 100 ;
2018-05-23 10:45:25 +02:00
SERIAL_ECHOLNPAIR ( MSG_STEPPER_TOO_HIGH , step_rate ) ;
2017-06-18 01:36:10 +02:00
}
# endif
2016-04-27 16:15:20 +02:00
return timer ;
}
2016-03-24 11:18:45 +01:00
2018-05-26 09:02:39 +02:00
# if ENABLED(S_CURVE_ACCELERATION)
2018-04-12 01:13:42 +02:00
static void _calc_bezier_curve_coeffs ( const int32_t v0 , const int32_t v1 , const uint32_t av ) ;
2018-04-07 03:48:06 +02:00
static int32_t _eval_bezier_curve ( const uint32_t curr_step ) ;
# endif
2012-08-30 09:16:57 +02:00
2017-06-03 07:38:07 +02:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
static void digipot_init ( ) ;
# endif
2016-09-25 13:32:58 +02:00
# if HAS_MICROSTEPS
static void microstep_init ( ) ;
# endif
2015-03-24 18:06:44 +01:00
2016-04-27 16:15:20 +02:00
} ;
2015-10-03 08:08:58 +02:00
2016-09-20 22:57:48 +02:00
# endif // STEPPER_H