2009-01-28 23:48:21 +01:00
|
|
|
/*
|
2011-01-14 16:45:18 +01:00
|
|
|
stepper.c - stepper motor driver: executes motion plans using stepper motors
|
2009-01-28 23:48:21 +01:00
|
|
|
Part of Grbl
|
|
|
|
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
2012-01-29 04:41:08 +01:00
|
|
|
Copyright (c) 2011-2012 Sungeun K. Jeon
|
2011-09-24 15:46:41 +02:00
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-29 09:58:29 +01:00
|
|
|
/* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
|
2011-02-11 01:31:44 +01:00
|
|
|
and Philipp Tiefenbacher. */
|
2009-01-29 09:58:29 +01:00
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "stepper.h"
|
2011-02-05 00:55:37 +01:00
|
|
|
#include "config.h"
|
2011-02-05 00:45:41 +01:00
|
|
|
#include "settings.h"
|
2009-02-11 00:37:33 +01:00
|
|
|
#include <math.h>
|
2010-03-03 00:26:48 +01:00
|
|
|
#include <stdlib.h>
|
2010-02-27 19:55:09 +01:00
|
|
|
#include <util/delay.h>
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "nuts_bolts.h"
|
|
|
|
#include <avr/interrupt.h>
|
2011-02-11 00:34:53 +01:00
|
|
|
#include "planner.h"
|
2011-01-31 23:04:39 +01:00
|
|
|
|
|
|
|
// Some useful constants
|
2011-02-11 23:53:58 +01:00
|
|
|
#define TICKS_PER_MICROSECOND (F_CPU/1000000)
|
2011-02-04 22:09:09 +01:00
|
|
|
#define CYCLES_PER_ACCELERATION_TICK ((TICKS_PER_MICROSECOND*1000000)/ACCELERATION_TICKS_PER_SECOND)
|
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Stepper state variable. Contains running data and trapezoid variables.
|
|
|
|
typedef struct {
|
|
|
|
// Used by the bresenham line algorithm
|
|
|
|
int32_t counter_x, // Counter variables for the bresenham line tracer
|
|
|
|
counter_y,
|
|
|
|
counter_z;
|
|
|
|
uint32_t event_count;
|
|
|
|
uint32_t step_events_completed; // The number of step events left in current motion
|
|
|
|
|
|
|
|
// Used by the trapezoid generator
|
|
|
|
uint32_t cycles_per_step_event; // The number of machine cycles between each step event
|
|
|
|
uint32_t trapezoid_tick_cycle_counter; // The cycles since last trapezoid_tick. Used to generate ticks at a steady
|
|
|
|
// pace without allocating a separate timer
|
|
|
|
uint32_t trapezoid_adjusted_rate; // The current rate of step_events according to the trapezoid generator
|
|
|
|
uint32_t min_safe_rate; // Minimum safe rate for full deceleration rate reduction step. Otherwise halves step_rate.
|
2012-01-06 18:10:41 +01:00
|
|
|
} stepper_t;
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2012-01-06 18:10:41 +01:00
|
|
|
static stepper_t st;
|
2011-02-11 00:34:53 +01:00
|
|
|
static block_t *current_block; // A pointer to the block currently being traced
|
2010-12-20 14:01:38 +01:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Used by the stepper driver interrupt
|
|
|
|
static uint8_t step_pulse_time; // Step pulse reset time after step rise
|
2011-01-25 23:33:19 +01:00
|
|
|
static uint8_t out_bits; // The next stepping-bits to be output
|
2011-12-09 02:47:48 +01:00
|
|
|
static volatile uint8_t busy; // True when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
2011-01-22 23:29:02 +01:00
|
|
|
|
2012-06-27 05:48:42 +02:00
|
|
|
#if STEP_PULSE_DELAY > 0
|
|
|
|
static uint8_t step_bits; // Stores out_bits output to complete the step pulse delay
|
|
|
|
#endif
|
|
|
|
|
2011-01-03 00:36:33 +01:00
|
|
|
// __________________________
|
|
|
|
// /| |\ _________________ ^
|
|
|
|
// / | | \ /| |\ |
|
|
|
|
// / | | \ / | | \ s
|
|
|
|
// / | | | | | \ p
|
|
|
|
// / | | | | | \ e
|
|
|
|
// +-----+------------------------+---+--+---------------+----+ e
|
|
|
|
// | BLOCK 1 | BLOCK 2 | d
|
|
|
|
//
|
|
|
|
// time ----->
|
|
|
|
//
|
2011-01-24 20:55:25 +01:00
|
|
|
// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta
|
2011-01-25 14:27:28 +01:00
|
|
|
// during the first block->accelerate_until step_events_completed, then keeps going at constant speed until
|
|
|
|
// step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
|
2011-09-26 03:24:29 +02:00
|
|
|
// The slope of acceleration is always +/- block->rate_delta and is applied at a constant rate following the midpoint rule
|
|
|
|
// by the trapezoid generator, which is called ACCELERATION_TICKS_PER_SECOND times per second.
|
2011-01-14 12:10:18 +01:00
|
|
|
|
2011-02-19 23:03:10 +01:00
|
|
|
static void set_step_events_per_minute(uint32_t steps_per_minute);
|
2011-01-31 23:04:39 +01:00
|
|
|
|
2011-10-12 04:51:04 +02:00
|
|
|
// Stepper state initialization
|
2011-12-09 02:47:48 +01:00
|
|
|
static void st_wake_up()
|
|
|
|
{
|
2011-10-12 04:51:04 +02:00
|
|
|
// Initialize stepper output bits
|
2011-12-09 02:47:48 +01:00
|
|
|
out_bits = (0) ^ (settings.invert_mask);
|
2012-06-27 05:48:42 +02:00
|
|
|
// Initialize step pulse timing from settings. Here to ensure updating after re-writing.
|
2012-09-22 01:55:02 +02:00
|
|
|
#ifdef STEP_PULSE_DELAY
|
2012-06-27 05:48:42 +02:00
|
|
|
// Set total step pulse time after direction pin set. Ad hoc computation from oscilloscope.
|
|
|
|
step_pulse_time = -(((settings.pulse_microseconds+STEP_PULSE_DELAY-2)*TICKS_PER_MICROSECOND) >> 3);
|
|
|
|
// Set delay between direction pin write and step command.
|
|
|
|
OCR2A = -(((settings.pulse_microseconds)*TICKS_PER_MICROSECOND) >> 3);
|
|
|
|
#else // Normal operation
|
|
|
|
// Set step pulse time. Ad hoc computation from oscilloscope. Uses two's complement.
|
|
|
|
step_pulse_time = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND) >> 3);
|
|
|
|
#endif
|
2011-05-31 13:08:42 +02:00
|
|
|
// Enable steppers by resetting the stepper disable port
|
|
|
|
STEPPERS_DISABLE_PORT &= ~(1<<STEPPERS_DISABLE_BIT);
|
|
|
|
// Enable stepper driver interrupt
|
|
|
|
TIMSK1 |= (1<<OCIE1A);
|
|
|
|
}
|
|
|
|
|
2011-10-12 04:51:04 +02:00
|
|
|
// Stepper shutdown
|
2011-12-09 02:47:48 +01:00
|
|
|
void st_go_idle()
|
|
|
|
{
|
2011-11-19 18:08:41 +01:00
|
|
|
// Disable stepper driver interrupt
|
|
|
|
TIMSK1 &= ~(1<<OCIE1A);
|
2011-09-30 00:25:48 +02:00
|
|
|
// Force stepper dwell to lock axes for a defined amount of time to ensure the axes come to a complete
|
|
|
|
// stop and not drift from residual inertial forces at the end of the last movement.
|
2012-10-13 21:11:43 +02:00
|
|
|
delay_ms(settings.stepper_idle_lock_time);
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
// Disable steppers only upon system alarm activated or by user setting to keep enabled.
|
|
|
|
if ((settings.stepper_idle_lock_time != 0xff) || bit_istrue(sys.execute,EXEC_ALARM)) {
|
2012-10-13 21:11:43 +02:00
|
|
|
STEPPERS_DISABLE_PORT |= (1<<STEPPERS_DISABLE_BIT);
|
|
|
|
}
|
2011-02-11 00:34:53 +01:00
|
|
|
}
|
|
|
|
|
2011-09-24 15:46:41 +02:00
|
|
|
// This function determines an acceleration velocity change every CYCLES_PER_ACCELERATION_TICK by
|
|
|
|
// keeping track of the number of elapsed cycles during a de/ac-celeration. The code assumes that
|
|
|
|
// step_events occur significantly more often than the acceleration velocity iterations.
|
2012-01-29 04:41:08 +01:00
|
|
|
inline static uint8_t iterate_trapezoid_cycle_counter()
|
2011-12-09 02:47:48 +01:00
|
|
|
{
|
|
|
|
st.trapezoid_tick_cycle_counter += st.cycles_per_step_event;
|
|
|
|
if(st.trapezoid_tick_cycle_counter > CYCLES_PER_ACCELERATION_TICK) {
|
|
|
|
st.trapezoid_tick_cycle_counter -= CYCLES_PER_ACCELERATION_TICK;
|
2011-09-24 15:46:41 +02:00
|
|
|
return(true);
|
|
|
|
} else {
|
|
|
|
return(false);
|
2011-01-03 00:36:33 +01:00
|
|
|
}
|
2011-09-24 15:46:41 +02:00
|
|
|
}
|
2010-12-20 14:01:38 +01:00
|
|
|
|
2011-10-12 04:51:04 +02:00
|
|
|
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. It is executed at the rate set with
|
2010-12-20 14:01:38 +01:00
|
|
|
// config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
|
2011-09-24 15:46:41 +02:00
|
|
|
// It is supported by The Stepper Port Reset Interrupt which it uses to reset the stepper port after each pulse.
|
|
|
|
// The bresenham line tracer algorithm controls all three stepper outputs simultaneously with these two interrupts.
|
2012-01-16 02:25:12 +01:00
|
|
|
ISR(TIMER1_COMPA_vect)
|
2011-01-24 20:55:25 +01:00
|
|
|
{
|
2011-12-09 02:47:48 +01:00
|
|
|
if (busy) { return; } // The busy-flag is used to avoid reentering this interrupt
|
2011-01-24 20:55:25 +01:00
|
|
|
|
2011-10-12 04:51:04 +02:00
|
|
|
// Set the direction pins a couple of nanoseconds before we step the steppers
|
2010-03-02 21:46:51 +01:00
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);
|
|
|
|
// Then pulse the stepping pins
|
2012-09-22 01:55:02 +02:00
|
|
|
#ifdef STEP_PULSE_DELAY
|
2012-06-27 05:48:42 +02:00
|
|
|
step_bits = (STEPPING_PORT & ~STEP_MASK) | out_bits; // Store out_bits to prevent overwriting.
|
|
|
|
#else // Normal operation
|
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | out_bits;
|
|
|
|
#endif
|
2012-01-16 02:25:12 +01:00
|
|
|
// Enable step pulse reset timer so that The Stepper Port Reset Interrupt can reset the signal after
|
|
|
|
// exactly settings.pulse_microseconds microseconds, independent of the main Timer1 prescaler.
|
|
|
|
TCNT2 = step_pulse_time; // Reload timer counter
|
|
|
|
TCCR2B = (1<<CS21); // Begin timer2. Full speed, 1/8 prescaler
|
|
|
|
|
|
|
|
busy = true;
|
|
|
|
// Re-enable interrupts to allow ISR_TIMER2_OVERFLOW to trigger on-time and allow serial communications
|
|
|
|
// regardless of time in this handler. The following code prepares the stepper driver for the next
|
|
|
|
// step interrupt compare and will always finish before returning to the main program.
|
|
|
|
sei();
|
|
|
|
|
2011-01-03 00:36:33 +01:00
|
|
|
// If there is no current block, attempt to pop one from the buffer
|
|
|
|
if (current_block == NULL) {
|
2011-12-09 02:47:48 +01:00
|
|
|
// Anything in the buffer? If so, initialize next motion.
|
2011-02-06 23:52:12 +01:00
|
|
|
current_block = plan_get_current_block();
|
|
|
|
if (current_block != NULL) {
|
2012-01-06 18:10:41 +01:00
|
|
|
if (!sys.feed_hold) {
|
|
|
|
// During feed hold, do not update rate and trap counter. Keep decelerating.
|
2011-12-09 02:47:48 +01:00
|
|
|
st.trapezoid_adjusted_rate = current_block->initial_rate;
|
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate); // Initialize cycles_per_step_event
|
|
|
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2; // Start halfway for midpoint rule.
|
|
|
|
}
|
|
|
|
st.min_safe_rate = current_block->rate_delta + (current_block->rate_delta >> 1); // 1.5 x rate_delta
|
|
|
|
st.counter_x = -(current_block->step_event_count >> 1);
|
|
|
|
st.counter_y = st.counter_x;
|
|
|
|
st.counter_z = st.counter_x;
|
|
|
|
st.event_count = current_block->step_event_count;
|
|
|
|
st.step_events_completed = 0;
|
2010-03-03 00:26:48 +01:00
|
|
|
} else {
|
2011-05-31 13:08:42 +02:00
|
|
|
st_go_idle();
|
2012-01-06 18:10:41 +01:00
|
|
|
sys.cycle_start = false;
|
|
|
|
bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end
|
2010-03-03 00:26:48 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-02 21:46:51 +01:00
|
|
|
|
2011-01-03 00:36:33 +01:00
|
|
|
if (current_block != NULL) {
|
2011-09-24 15:46:41 +02:00
|
|
|
// Execute step displacement profile by bresenham line algorithm
|
2011-01-03 00:36:33 +01:00
|
|
|
out_bits = current_block->direction_bits;
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_x += current_block->steps_x;
|
|
|
|
if (st.counter_x > 0) {
|
2010-03-02 21:46:51 +01:00
|
|
|
out_bits |= (1<<X_STEP_BIT);
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_x -= st.event_count;
|
2012-01-06 18:10:41 +01:00
|
|
|
if (out_bits & (1<<X_DIRECTION_BIT)) { sys.position[X_AXIS]--; }
|
|
|
|
else { sys.position[X_AXIS]++; }
|
2010-03-02 21:46:51 +01:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_y += current_block->steps_y;
|
|
|
|
if (st.counter_y > 0) {
|
2010-03-02 21:46:51 +01:00
|
|
|
out_bits |= (1<<Y_STEP_BIT);
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_y -= st.event_count;
|
2012-01-06 18:10:41 +01:00
|
|
|
if (out_bits & (1<<Y_DIRECTION_BIT)) { sys.position[Y_AXIS]--; }
|
|
|
|
else { sys.position[Y_AXIS]++; }
|
2010-03-02 21:46:51 +01:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_z += current_block->steps_z;
|
|
|
|
if (st.counter_z > 0) {
|
2010-03-02 21:46:51 +01:00
|
|
|
out_bits |= (1<<Z_STEP_BIT);
|
2011-12-09 02:47:48 +01:00
|
|
|
st.counter_z -= st.event_count;
|
2012-01-06 18:10:41 +01:00
|
|
|
if (out_bits & (1<<Z_DIRECTION_BIT)) { sys.position[Z_AXIS]--; }
|
|
|
|
else { sys.position[Z_AXIS]++; }
|
2010-03-02 21:46:51 +01:00
|
|
|
}
|
2011-09-24 15:46:41 +02:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
st.step_events_completed++; // Iterate step events
|
2011-09-24 15:46:41 +02:00
|
|
|
|
|
|
|
// While in block steps, check for de/ac-celeration events and execute them accordingly.
|
2011-12-09 02:47:48 +01:00
|
|
|
if (st.step_events_completed < current_block->step_event_count) {
|
2012-01-06 18:10:41 +01:00
|
|
|
if (sys.feed_hold) {
|
2011-12-09 02:47:48 +01:00
|
|
|
// Check for and execute feed hold by enforcing a steady deceleration from the moment of
|
|
|
|
// execution. The rate of deceleration is limited by rate_delta and will never decelerate
|
|
|
|
// faster or slower than in normal operation. If the distance required for the feed hold
|
|
|
|
// deceleration spans more than one block, the initial rate of the following blocks are not
|
|
|
|
// updated and deceleration is continued according to their corresponding rate_delta.
|
|
|
|
// NOTE: The trapezoid tick cycle counter is not updated intentionally. This ensures that
|
|
|
|
// the deceleration is smooth regardless of where the feed hold is initiated and if the
|
|
|
|
// deceleration distance spans multiple blocks.
|
|
|
|
if ( iterate_trapezoid_cycle_counter() ) {
|
|
|
|
// If deceleration complete, set system flags and shutdown steppers.
|
|
|
|
if (st.trapezoid_adjusted_rate <= current_block->rate_delta) {
|
|
|
|
// Just go idle. Do not NULL current block. The bresenham algorithm variables must
|
2012-01-06 18:10:41 +01:00
|
|
|
// remain intact to ensure the stepper path is exactly the same. Feed hold is still
|
|
|
|
// active and is released after the buffer has been reinitialized.
|
2011-12-09 02:47:48 +01:00
|
|
|
st_go_idle();
|
2012-01-06 18:10:41 +01:00
|
|
|
sys.cycle_start = false;
|
|
|
|
bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program that feed hold is complete.
|
2011-12-09 02:47:48 +01:00
|
|
|
} else {
|
|
|
|
st.trapezoid_adjusted_rate -= current_block->rate_delta;
|
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
|
|
|
}
|
2011-09-24 15:46:41 +02:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// The trapezoid generator always checks step event location to ensure de/ac-celerations are
|
|
|
|
// executed and terminated at exactly the right time. This helps prevent over/under-shooting
|
|
|
|
// the target position and speed.
|
|
|
|
// NOTE: By increasing the ACCELERATION_TICKS_PER_SECOND in config.h, the resolution of the
|
|
|
|
// discrete velocity changes increase and accuracy can increase as well to a point. Numerical
|
|
|
|
// round-off errors can effect this, if set too high. This is important to note if a user has
|
|
|
|
// very high acceleration and/or feedrate requirements for their machine.
|
|
|
|
if (st.step_events_completed < current_block->accelerate_until) {
|
|
|
|
// Iterate cycle counter and check if speeds need to be increased.
|
|
|
|
if ( iterate_trapezoid_cycle_counter() ) {
|
|
|
|
st.trapezoid_adjusted_rate += current_block->rate_delta;
|
|
|
|
if (st.trapezoid_adjusted_rate >= current_block->nominal_rate) {
|
|
|
|
// Reached nominal rate a little early. Cruise at nominal rate until decelerate_after.
|
|
|
|
st.trapezoid_adjusted_rate = current_block->nominal_rate;
|
2011-09-26 03:24:29 +02:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
|
|
|
}
|
|
|
|
} else if (st.step_events_completed >= current_block->decelerate_after) {
|
|
|
|
// Reset trapezoid tick cycle counter to make sure that the deceleration is performed the
|
|
|
|
// same every time. Reset to CYCLES_PER_ACCELERATION_TICK/2 to follow the midpoint rule for
|
|
|
|
// an accurate approximation of the deceleration curve.
|
|
|
|
if (st.step_events_completed == current_block-> decelerate_after) {
|
|
|
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2;
|
|
|
|
} else {
|
|
|
|
// Iterate cycle counter and check if speeds need to be reduced.
|
|
|
|
if ( iterate_trapezoid_cycle_counter() ) {
|
|
|
|
// NOTE: We will only do a full speed reduction if the result is more than the minimum safe
|
|
|
|
// rate, initialized in trapezoid reset as 1.5 x rate_delta. Otherwise, reduce the speed by
|
|
|
|
// half increments until finished. The half increments are guaranteed not to exceed the
|
|
|
|
// CNC acceleration limits, because they will never be greater than rate_delta. This catches
|
|
|
|
// small errors that might leave steps hanging after the last trapezoid tick or a very slow
|
|
|
|
// step rate at the end of a full stop deceleration in certain situations. The half rate
|
|
|
|
// reductions should only be called once or twice per block and create a nice smooth
|
|
|
|
// end deceleration.
|
|
|
|
if (st.trapezoid_adjusted_rate > st.min_safe_rate) {
|
|
|
|
st.trapezoid_adjusted_rate -= current_block->rate_delta;
|
|
|
|
} else {
|
|
|
|
st.trapezoid_adjusted_rate >>= 1; // Bit shift divide by 2
|
|
|
|
}
|
|
|
|
if (st.trapezoid_adjusted_rate < current_block->final_rate) {
|
|
|
|
// Reached final rate a little early. Cruise to end of block at final rate.
|
|
|
|
st.trapezoid_adjusted_rate = current_block->final_rate;
|
|
|
|
}
|
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
2011-09-26 03:24:29 +02:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No accelerations. Make sure we cruise exactly at the nominal rate.
|
|
|
|
if (st.trapezoid_adjusted_rate != current_block->nominal_rate) {
|
|
|
|
st.trapezoid_adjusted_rate = current_block->nominal_rate;
|
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
2011-09-24 15:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
2011-09-24 15:46:41 +02:00
|
|
|
} else {
|
|
|
|
// If current block is finished, reset pointer
|
2011-01-03 00:36:33 +01:00
|
|
|
current_block = NULL;
|
2011-02-06 23:52:12 +01:00
|
|
|
plan_discard_current_block();
|
2009-02-08 20:40:24 +01:00
|
|
|
}
|
2011-10-12 04:51:04 +02:00
|
|
|
}
|
2012-10-01 03:57:10 +02:00
|
|
|
out_bits ^= settings.invert_mask; // Apply step and direction invert mask
|
2011-12-09 02:47:48 +01:00
|
|
|
busy = false;
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:25:12 +01:00
|
|
|
// This interrupt is set up by ISR_TIMER1_COMPAREA when it sets the motor port bits. It resets
|
2010-03-07 20:29:18 +01:00
|
|
|
// the motor port after a short period (settings.pulse_microseconds) completing one step cycle.
|
2012-10-08 23:57:58 +02:00
|
|
|
// NOTE: Interrupt collisions between the serial and stepper interrupts can cause delays by
|
|
|
|
// a few microseconds, if they execute right before one another. Not a big deal, but can
|
|
|
|
// cause issues at high step rates if another high frequency asynchronous interrupt is
|
|
|
|
// added to Grbl.
|
2011-12-09 02:47:48 +01:00
|
|
|
ISR(TIMER2_OVF_vect)
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
2011-12-09 02:47:48 +01:00
|
|
|
// Reset stepping pins (leave the direction pins)
|
2010-03-07 20:29:18 +01:00
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK);
|
2012-01-16 02:25:12 +01:00
|
|
|
TCCR2B = 0; // Disable Timer2 to prevent re-entering this interrupt when it's not needed.
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2012-09-22 01:55:02 +02:00
|
|
|
#ifdef STEP_PULSE_DELAY
|
2012-06-27 05:48:42 +02:00
|
|
|
// This interrupt is used only when STEP_PULSE_DELAY is enabled. Here, the step pulse is
|
|
|
|
// initiated after the STEP_PULSE_DELAY time period has elapsed. The ISR TIMER2_OVF interrupt
|
|
|
|
// will then trigger after the appropriate settings.pulse_microseconds, as in normal operation.
|
|
|
|
// The new timing between direction, step pulse, and step complete events are setup in the
|
|
|
|
// st_wake_up() routine.
|
|
|
|
ISR(TIMER2_COMPA_vect)
|
|
|
|
{
|
|
|
|
STEPPING_PORT = step_bits; // Begin step pulse.
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Reset and clear stepper subsystem variables
|
|
|
|
void st_reset()
|
|
|
|
{
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
set_step_events_per_minute(MINIMUM_STEPS_PER_MINUTE);
|
|
|
|
current_block = NULL;
|
|
|
|
busy = false;
|
|
|
|
}
|
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
// Initialize and start the stepper motor subsystem
|
|
|
|
void st_init()
|
|
|
|
{
|
2011-12-09 02:47:48 +01:00
|
|
|
// Configure directions of interface pins
|
|
|
|
STEPPING_DDR |= STEPPING_MASK;
|
2010-03-07 20:29:18 +01:00
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask;
|
2011-05-31 13:08:42 +02:00
|
|
|
STEPPERS_DISABLE_DDR |= 1<<STEPPERS_DISABLE_BIT;
|
2012-06-27 05:48:42 +02:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// waveform generation = 0100 = CTC
|
|
|
|
TCCR1B &= ~(1<<WGM13);
|
|
|
|
TCCR1B |= (1<<WGM12);
|
|
|
|
TCCR1A &= ~(1<<WGM11);
|
|
|
|
TCCR1A &= ~(1<<WGM10);
|
|
|
|
|
|
|
|
// output mode = 00 (disconnected)
|
|
|
|
TCCR1A &= ~(3<<COM1A0);
|
|
|
|
TCCR1A &= ~(3<<COM1B0);
|
2009-01-28 23:48:21 +01:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Configure Timer 2
|
2012-01-16 02:25:12 +01:00
|
|
|
TCCR2A = 0; // Normal operation
|
|
|
|
TCCR2B = 0; // Disable timer until needed.
|
2012-10-08 23:57:58 +02:00
|
|
|
TIMSK2 |= (1<<TOIE2); // Enable Timer2 Overflow interrupt
|
2012-09-22 01:55:02 +02:00
|
|
|
#ifdef STEP_PULSE_DELAY
|
2012-06-27 05:48:42 +02:00
|
|
|
TIMSK2 |= (1<<OCIE2A); // Enable Timer2 Compare Match A interrupt
|
|
|
|
#endif
|
2012-10-08 23:57:58 +02:00
|
|
|
|
2011-05-31 13:08:42 +02:00
|
|
|
// Start in the idle state
|
|
|
|
st_go_idle();
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-04 21:18:55 +01:00
|
|
|
// Configures the prescaler and ceiling of timer 1 to produce the given rate as accurately as possible.
|
2011-01-03 00:36:33 +01:00
|
|
|
// Returns the actual number of cycles per interrupt
|
2011-02-19 23:03:10 +01:00
|
|
|
static uint32_t config_step_timer(uint32_t cycles)
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
|
|
|
uint16_t ceiling;
|
2012-09-22 01:55:02 +02:00
|
|
|
uint8_t prescaler;
|
2011-01-03 00:36:33 +01:00
|
|
|
uint32_t actual_cycles;
|
2011-12-09 02:47:48 +01:00
|
|
|
if (cycles <= 0xffffL) {
|
|
|
|
ceiling = cycles;
|
2012-09-22 01:55:02 +02:00
|
|
|
prescaler = 1; // prescaler: 0
|
2011-01-03 00:36:33 +01:00
|
|
|
actual_cycles = ceiling;
|
2011-12-09 02:47:48 +01:00
|
|
|
} else if (cycles <= 0x7ffffL) {
|
2011-01-03 00:36:33 +01:00
|
|
|
ceiling = cycles >> 3;
|
2012-09-22 01:55:02 +02:00
|
|
|
prescaler = 2; // prescaler: 8
|
2011-01-22 23:29:02 +01:00
|
|
|
actual_cycles = ceiling * 8L;
|
2011-12-09 02:47:48 +01:00
|
|
|
} else if (cycles <= 0x3fffffL) {
|
|
|
|
ceiling = cycles >> 6;
|
2012-09-22 01:55:02 +02:00
|
|
|
prescaler = 3; // prescaler: 64
|
2011-01-22 23:29:02 +01:00
|
|
|
actual_cycles = ceiling * 64L;
|
2011-12-09 02:47:48 +01:00
|
|
|
} else if (cycles <= 0xffffffL) {
|
|
|
|
ceiling = (cycles >> 8);
|
2012-09-22 01:55:02 +02:00
|
|
|
prescaler = 4; // prescaler: 256
|
2011-01-22 23:29:02 +01:00
|
|
|
actual_cycles = ceiling * 256L;
|
2011-12-09 02:47:48 +01:00
|
|
|
} else if (cycles <= 0x3ffffffL) {
|
|
|
|
ceiling = (cycles >> 10);
|
2012-09-22 01:55:02 +02:00
|
|
|
prescaler = 5; // prescaler: 1024
|
2011-01-22 23:29:02 +01:00
|
|
|
actual_cycles = ceiling * 1024L;
|
2011-12-09 02:47:48 +01:00
|
|
|
} else {
|
|
|
|
// Okay, that was slower than we actually go. Just set the slowest speed
|
|
|
|
ceiling = 0xffff;
|
2012-10-01 03:57:10 +02:00
|
|
|
prescaler = 5;
|
2011-01-03 00:36:33 +01:00
|
|
|
actual_cycles = 0xffff * 1024;
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
// Set prescaler
|
2012-09-22 01:55:02 +02:00
|
|
|
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (prescaler<<CS10);
|
2009-01-28 23:48:21 +01:00
|
|
|
// Set ceiling
|
|
|
|
OCR1A = ceiling;
|
2011-01-03 00:36:33 +01:00
|
|
|
return(actual_cycles);
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
static void set_step_events_per_minute(uint32_t steps_per_minute)
|
|
|
|
{
|
2011-01-22 23:29:02 +01:00
|
|
|
if (steps_per_minute < MINIMUM_STEPS_PER_MINUTE) { steps_per_minute = MINIMUM_STEPS_PER_MINUTE; }
|
2011-12-09 02:47:48 +01:00
|
|
|
st.cycles_per_step_event = config_step_timer((TICKS_PER_MICROSECOND*1000000*60)/steps_per_minute);
|
2010-06-28 23:29:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Planner external interface to start stepper interrupt and execute the blocks in queue. Called
|
2012-01-06 18:10:41 +01:00
|
|
|
// by the main program functions: planner auto-start and run-time command execution.
|
2011-12-09 02:47:48 +01:00
|
|
|
void st_cycle_start()
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
2012-01-06 18:10:41 +01:00
|
|
|
if (!sys.cycle_start) {
|
|
|
|
if (!sys.feed_hold) {
|
|
|
|
sys.cycle_start = true;
|
2011-12-09 02:47:48 +01:00
|
|
|
st_wake_up();
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
2011-10-12 04:51:04 +02:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Execute a feed hold with deceleration, only during cycle. Called by main program.
|
|
|
|
void st_feed_hold()
|
|
|
|
{
|
2012-01-06 18:10:41 +01:00
|
|
|
if (!sys.feed_hold) {
|
|
|
|
if (sys.cycle_start) {
|
|
|
|
sys.auto_start = false; // Disable planner auto start upon feed hold.
|
|
|
|
sys.feed_hold = true;
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
2011-10-12 04:51:04 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
|
|
|
|
// Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by
|
|
|
|
// runtime command execution in the main program, ensuring that the planner re-plans safely.
|
|
|
|
// NOTE: Bresenham algorithm variables are still maintained through both the planner and stepper
|
|
|
|
// cycle reinitializations. The stepper path should continue exactly as if nothing has happened.
|
|
|
|
// Only the planner de/ac-celerations profiles and stepper rates have been updated.
|
|
|
|
void st_cycle_reinitialize()
|
|
|
|
{
|
2012-01-06 18:10:41 +01:00
|
|
|
if (current_block != NULL) {
|
|
|
|
// Replan buffer from the feed hold stop location.
|
|
|
|
plan_cycle_reinitialize(current_block->step_event_count - st.step_events_completed);
|
|
|
|
// Update initial rate and timers after feed hold.
|
|
|
|
st.trapezoid_adjusted_rate = 0; // Resumes from rest
|
|
|
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
|
|
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2; // Start halfway for midpoint rule.
|
|
|
|
st.step_events_completed = 0;
|
|
|
|
}
|
|
|
|
sys.feed_hold = false; // Release feed hold. Cycle is ready to re-start.
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|