2016-03-25 06:19:46 +00:00
/**
2016-03-24 18:01:20 +00: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 06:19:46 +00:00
/**
2016-04-27 14:15:20 +00:00
* stepper . h - stepper motor driver : executes motion plans of planner . c using the stepper motors
2016-05-08 19:16:26 +00:00
* Derived from Grbl
2016-04-27 14:15:20 +00: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 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
# ifndef STEPPER_H
# define STEPPER_H
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
# include "stepper_indirection.h"
2017-09-06 11:28:32 +00:00
2017-09-24 04:25:28 +00:00
# ifdef __AVR__
2017-09-06 11:28:32 +00:00
# include "speed_lookuptable.h"
# endif
# include "../inc/MarlinConfig.h"
# include "../module/planner.h"
# include "../core/language.h"
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
class Stepper ;
extern Stepper stepper ;
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
class Stepper {
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
public :
2016-05-11 22:24:24 +00:00
static block_t * current_block ; // A pointer to the block currently being traced
2016-04-27 14:15:20 +00:00
# if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
2016-05-11 22:24:24 +00:00
static bool abort_on_endstop_hit ;
2016-04-27 14:15:20 +00:00
# endif
2017-10-29 08:43:44 +00:00
# if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
2016-05-11 22:24:24 +00:00
static bool performing_homing ;
2016-04-27 14:15:20 +00:00
# endif
2017-06-03 05:38:07 +00: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
2017-12-07 03:28:50 +00:00
static int16_t cleaning_buffer_counter ;
2016-04-27 14:15:20 +00:00
private :
2017-06-25 03:23:45 +00:00
static uint8_t last_direction_bits ; // The next stepping-bits to be output
2016-04-27 14:15:20 +00:00
2017-10-29 08:43:44 +00:00
# if ENABLED(X_DUAL_ENDSTOPS)
static bool locked_x_motor , locked_x2_motor ;
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
static bool locked_y_motor , locked_y2_motor ;
# endif
2016-04-27 14:15:20 +00:00
# if ENABLED(Z_DUAL_ENDSTOPS)
2016-05-11 22:24:24 +00:00
static bool locked_z_motor , locked_z2_motor ;
2016-04-27 14:15:20 +00:00
# endif
// Counter variables for the Bresenham line tracer
2016-05-11 22:24:24 +00:00
static long counter_X , counter_Y , counter_Z , counter_E ;
2016-08-30 19:19:49 +00:00
static volatile uint32_t step_events_completed ; // The number of step events executed in the current block
2016-04-27 14:15:20 +00:00
2017-10-09 09:25:18 +00:00
# if ENABLED(LIN_ADVANCE)
2018-02-23 06:53:29 +00:00
static uint32_t LA_decelerate_after ; // Copy from current executed block. Needed because current_block is set to NULL "too early".
2017-11-06 01:31:07 +00:00
static hal_timer_t nextMainISR , nextAdvanceISR , eISR_Rate ;
2018-02-23 06:53:29 +00: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".
2016-12-12 19:30:02 +00:00
# define _NEXT_ISR(T) nextMainISR = T
2018-02-23 06:53:29 +00:00
static int8_t e_steps ;
static bool use_advance_lead ;
2018-03-07 01:21:41 +00: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 11:49:10 +00:00
static constexpr int8_t LA_active_extruder = 0 ;
2018-03-07 01:21:41 +00:00
# endif
2018-02-23 06:53:29 +00:00
# else // !LIN_ADVANCE
2017-06-17 23:36:10 +00:00
2018-02-11 02:42:00 +00:00
# define _NEXT_ISR(T) HAL_timer_set_compare(STEP_TIMER_NUM, T);
2018-02-23 06:53:29 +00:00
# endif // !LIN_ADVANCE
2016-04-27 14:15:20 +00:00
2016-05-11 22:24:24 +00:00
static long acceleration_time , deceleration_time ;
static uint8_t step_loops , step_loops_nominal ;
2018-02-19 03:11:35 +00:00
static hal_timer_t OCR1A_nominal ,
acc_step_rate ; // needed for deceleration start point
2016-04-27 14:15:20 +00:00
2016-08-21 03:38:32 +00:00
static volatile long endstops_trigsteps [ XYZ ] ;
2016-05-11 22:24:24 +00:00
static volatile long endstops_stepsTotal , endstops_stepsDone ;
2016-04-27 14:15:20 +00:00
2016-09-15 08:18:10 +00:00
//
// Positions of stepper motors, in step units
//
static volatile long count_position [ NUM_AXIS ] ;
2016-04-27 14:15:20 +00:00
//
// Current direction of stepper motors (+1 or -1)
//
2016-05-11 22:24:24 +00:00
static volatile signed char count_direction [ NUM_AXIS ] ;
2016-04-27 14:15:20 +00:00
2016-06-28 22:06:56 +00:00
//
// Mixing extruder mix counters
//
# if ENABLED(MIXING_EXTRUDER)
2016-09-02 16:31:45 +00:00
static long counter_m [ MIXING_STEPPERS ] ;
2016-06-28 22:06:56 +00: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 14:15:20 +00:00
public :
//
// Constructor / initializer
//
2016-05-11 22:24:24 +00:00
Stepper ( ) { } ;
2016-04-27 14:15:20 +00:00
//
// Initialize stepper hardware
//
2016-06-03 00:57:56 +00:00
static void init ( ) ;
2016-04-27 14:15:20 +00:00
//
// Interrupt Service Routines
//
2016-05-11 22:24:24 +00:00
static void isr ( ) ;
2016-04-27 14:15:20 +00:00
2017-10-09 09:25:18 +00:00
# if ENABLED(LIN_ADVANCE)
2016-05-11 22:24:24 +00:00
static void advance_isr ( ) ;
2016-12-12 19:30:02 +00:00
static void advance_isr_scheduler ( ) ;
2016-04-27 14:15:20 +00:00
# endif
//
// Block until all buffered steps are executed
//
2016-06-03 00:57:56 +00:00
static void synchronize ( ) ;
2016-04-27 14:15:20 +00:00
//
// Set the current position in steps
//
2016-10-09 18:25:25 +00:00
static void set_position ( const long & a , const long & b , const long & c , const long & e ) ;
static void set_position ( const AxisEnum & a , const long & v ) ;
static void set_e_position ( const long & e ) ;
2016-04-27 14:15:20 +00:00
//
// Set direction bits for all steppers
//
2016-05-11 22:24:24 +00:00
static void set_directions ( ) ;
2016-04-27 14:15:20 +00:00
//
// Get the position of a stepper, in steps
//
2017-12-09 08:10:54 +00:00
static long position ( const AxisEnum axis ) ;
2016-04-27 14:15:20 +00:00
//
// Report the positions of the steppers, in steps
//
2016-06-03 00:57:56 +00:00
static void report_positions ( ) ;
2016-04-27 14:15:20 +00:00
//
// Get the position (mm) of an axis based on stepper position(s)
//
2017-12-09 08:10:54 +00:00
static float get_axis_position_mm ( const AxisEnum axis ) ;
2016-04-27 14:15:20 +00:00
2016-09-12 01:18:52 +00:00
//
// SCARA AB axes are in degrees, not mm
//
# if IS_SCARA
2017-12-09 08:10:54 +00:00
FORCE_INLINE static float get_axis_position_degrees ( const AxisEnum axis ) { return get_axis_position_mm ( axis ) ; }
2016-09-12 01:18:52 +00:00
# endif
2016-04-27 14:15:20 +00: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 00:57:56 +00:00
static void wake_up ( ) ;
2016-04-27 14:15:20 +00:00
//
// Wait for moves to finish and disable all steppers
//
2016-06-03 00:57:56 +00:00
static void finish_and_disable ( ) ;
2016-04-27 14:15:20 +00:00
//
// Quickly stop all steppers and clear the blocks queue
//
2016-06-03 00:57:56 +00:00
static void quick_stop ( ) ;
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
//
// The direction of a single motor
//
2017-12-09 08:10:54 +00:00
FORCE_INLINE static bool motor_direction ( const AxisEnum axis ) { return TEST ( last_direction_bits , axis ) ; }
2011-12-12 18:34:37 +00:00
2016-09-25 11:32:58 +00:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2017-06-25 03:23:45 +00: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 11:32:58 +00:00
# endif
# if HAS_MICROSTEPS
2017-06-25 03:23:45 +00: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 11:32:58 +00:00
static void microstep_readings ( ) ;
2016-04-27 14:15:20 +00:00
# endif
2011-11-20 13:50:08 +00:00
2017-10-29 08:43:44 +00:00
# if ENABLED(X_DUAL_ENDSTOPS)
2017-12-08 06:37:09 +00:00
FORCE_INLINE static void set_homing_flag_x ( const bool state ) { performing_homing = state ; }
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 08:43:44 +00:00
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
2017-12-08 06:37:09 +00:00
FORCE_INLINE static void set_homing_flag_y ( const bool state ) { performing_homing = state ; }
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 08:43:44 +00:00
# endif
2016-04-27 14:15:20 +00:00
# if ENABLED(Z_DUAL_ENDSTOPS)
2017-12-08 06:37:09 +00:00
FORCE_INLINE static void set_homing_flag_z ( const bool state ) { performing_homing = state ; }
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 14:15:20 +00:00
# endif
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
# if ENABLED(BABYSTEPPING)
2016-11-03 23:23:31 +00: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 14:15:20 +00:00
# endif
2011-11-20 13:50:08 +00:00
2016-06-03 00:57:56 +00:00
static inline void kill_current_block ( ) {
2016-04-27 14:15:20 +00:00
step_events_completed = current_block - > step_event_count ;
}
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
//
// Handle a triggered endstop
//
2017-12-09 08:10:54 +00:00
static void endstop_triggered ( const AxisEnum axis ) ;
2013-09-29 16:20:06 +00:00
2016-04-27 14:15:20 +00:00
//
// Triggered position of an axis in mm (not core-savvy)
//
2017-12-09 08:10:54 +00:00
FORCE_INLINE static float triggered_position_mm ( const AxisEnum axis ) {
2016-07-24 02:36:26 +00:00
return endstops_trigsteps [ axis ] * planner . steps_to_mm [ axis ] ;
2016-04-27 14:15:20 +00:00
}
2011-11-20 13:50:08 +00:00
2017-06-03 05:38:07 +00:00
# if HAS_MOTOR_CURRENT_PWM
static void refresh_motor_power ( ) ;
# endif
2016-05-04 00:00:28 +00:00
private :
2017-12-09 04:13:03 +00:00
FORCE_INLINE static hal_timer_t calc_timer_interval ( hal_timer_t step_rate ) {
2017-11-06 01:31:07 +00:00
hal_timer_t timer ;
2015-10-03 06:08:58 +00:00
2016-04-27 14:15:20 +00:00
NOMORE ( step_rate , MAX_STEP_FREQUENCY ) ;
2011-11-20 13:50:08 +00:00
2017-06-17 23:36:10 +00:00
// TODO: HAL: tidy this up, use condtionals_post.h
# ifdef CPU_32_BIT
# if ENABLED(DISABLE_MULTI_STEPPING)
step_loops = 1 ;
# else
if ( step_rate > STEP_DOUBLER_FREQUENCY * 2 ) { // If steprate > (STEP_DOUBLER_FREQUENCY * 2) kHz >> step 4 times
step_rate > > = 2 ;
step_loops = 4 ;
}
else if ( step_rate > STEP_DOUBLER_FREQUENCY ) { // If steprate > STEP_DOUBLER_FREQUENCY kHz >> step 2 times
step_rate > > = 1 ;
step_loops = 2 ;
}
else {
step_loops = 1 ;
}
# endif
# else
if ( step_rate > 20000 ) { // If steprate > 20kHz >> step 4 times
step_rate > > = 2 ;
step_loops = 4 ;
}
else if ( step_rate > 10000 ) { // If steprate > 10kHz >> step 2 times
step_rate > > = 1 ;
step_loops = 2 ;
}
else {
step_loops = 1 ;
}
# endif
# ifdef CPU_32_BIT
2017-09-27 09:57:14 +00:00
// In case of high-performance processor, it is able to calculate in real-time
2017-10-11 04:52:53 +00:00
const uint32_t MIN_TIME_PER_STEP = ( HAL_STEPPER_TIMER_RATE ) / ( ( STEP_DOUBLER_FREQUENCY ) * 2 ) ;
2017-10-07 18:34:25 +00:00
timer = uint32_t ( HAL_STEPPER_TIMER_RATE ) / step_rate ;
NOLESS ( timer , MIN_TIME_PER_STEP ) ; // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
2017-06-17 23:36:10 +00:00
# else
NOLESS ( step_rate , F_CPU / 500000 ) ;
step_rate - = F_CPU / 500000 ; // Correct for minimal speed
if ( step_rate > = ( 8 * 256 ) ) { // higher step rate
unsigned short table_address = ( unsigned short ) & speed_lookuptable_fast [ ( unsigned char ) ( step_rate > > 8 ) ] [ 0 ] ;
unsigned char tmp_step_rate = ( step_rate & 0x00ff ) ;
unsigned short gain = ( unsigned short ) pgm_read_word_near ( table_address + 2 ) ;
MultiU16X8toH16 ( timer , tmp_step_rate , gain ) ;
timer = ( unsigned short ) pgm_read_word_near ( table_address ) - timer ;
}
else { // lower step rates
unsigned short table_address = ( unsigned short ) & speed_lookuptable_slow [ 0 ] [ 0 ] ;
table_address + = ( ( step_rate ) > > 1 ) & 0xfffc ;
timer = ( unsigned short ) pgm_read_word_near ( table_address ) ;
timer - = ( ( ( unsigned short ) pgm_read_word_near ( table_address + 2 ) * ( unsigned char ) ( step_rate & 0x0007 ) ) > > 3 ) ;
}
if ( timer < 100 ) { // (20kHz - this should never happen)
timer = 100 ;
2017-11-05 14:49:38 +00:00
SERIAL_ECHOPGM ( MSG_STEPPER_TOO_HIGH ) ;
SERIAL_ECHOLN ( step_rate ) ;
2017-06-17 23:36:10 +00:00
}
# endif
2016-04-27 14:15:20 +00:00
return timer ;
}
2016-03-24 10:18:45 +00:00
2016-12-12 19:30:02 +00:00
// Initialize the trapezoid generator from the current block.
// Called whenever a new block begins.
2017-12-08 06:37:09 +00:00
FORCE_INLINE static void trapezoid_generator_reset ( ) {
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
static int8_t last_extruder = - 1 ;
2011-11-26 10:50:23 +00:00
2018-02-23 06:53:29 +00:00
# if ENABLED(LIN_ADVANCE)
2018-03-07 01:21:41 +00:00
# if E_STEPPERS > 1
if ( current_block - > active_extruder ! = last_extruder ) {
current_adv_steps = 0 ; // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
LA_active_extruder = current_block - > active_extruder ;
}
# endif
2018-02-23 06:53:29 +00:00
2018-03-07 01:21:41 +00:00
if ( ( use_advance_lead = current_block - > use_advance_lead ) ) {
2018-02-23 06:53:29 +00:00
LA_decelerate_after = current_block - > decelerate_after ;
final_adv_steps = current_block - > final_adv_steps ;
max_adv_steps = current_block - > max_adv_steps ;
}
# endif
2016-04-27 14:15:20 +00:00
if ( current_block - > direction_bits ! = last_direction_bits | | current_block - > active_extruder ! = last_extruder ) {
last_direction_bits = current_block - > direction_bits ;
last_extruder = current_block - > active_extruder ;
set_directions ( ) ;
}
2011-11-20 13:50:08 +00:00
2016-04-27 14:15:20 +00:00
deceleration_time = 0 ;
// step_rate to timer interval
2017-12-09 04:13:03 +00:00
OCR1A_nominal = calc_timer_interval ( current_block - > nominal_rate ) ;
2016-04-27 14:15:20 +00:00
// make a note of the number of step loops required at nominal speed
step_loops_nominal = step_loops ;
2017-12-17 08:37:35 +00:00
acc_step_rate = current_block - > initial_rate ;
acceleration_time = calc_timer_interval ( acc_step_rate ) ;
_NEXT_ISR ( acceleration_time ) ;
2016-12-15 15:21:32 +00:00
2016-04-27 14:15:20 +00:00
}
2012-08-30 07:16:57 +00:00
2017-06-03 05:38:07 +00:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
static void digipot_init ( ) ;
# endif
2016-09-25 11:32:58 +00:00
# if HAS_MICROSTEPS
static void microstep_init ( ) ;
# endif
2015-03-24 17:06:44 +00:00
2016-04-27 14:15:20 +00:00
} ;
2015-10-03 06:08:58 +00:00
2016-09-20 20:57:48 +00:00
# endif // STEPPER_H