Revamped homing cycle. Axis limits and max travel bug fixes. Build info. Refactored config.h.
- Revamped and improved homing cycle. Now tied directly into the main planner and stepper code, which enables much faster homing seek rates. Also dropped the compiled flash size by almost 1KB, meaning 1KB more for other features. - Refactored config.h. Removed obsolete defines and configuration options. Moved lots of “advanced” options into the advanced area of the file. - Updated defaults.h with the new homing cycle. Also updated the Sherline 5400 defaults and added the ShapeOko2 defaults per user submissions. - Fixed a bug where the individual axes limits on velocity and acceleration were not working correctly. Caused by abs() returning a int, rather than a float. Corrected with fabs(). Duh. - Added build version/date to the Grbl welcome message to help indicate which version a user is operating on. - Max travel settings were not being defaulted into the settings EEPROM correctly. Fixed. - To stop a single axis during a multi-axes homing move, the stepper algorithm now has a simple axis lock mask which inhibits the desired axes from moving. Meaning, if one of the limit switches engages before the other, we stop that one axes and keep moving the other.
This commit is contained in:
181
limits.c
181
limits.c
@ -33,8 +33,6 @@
|
||||
#include "limits.h"
|
||||
#include "report.h"
|
||||
|
||||
#define MICROSECONDS_PER_ACCELERATION_TICK (1000000/ACCELERATION_TICKS_PER_SECOND)
|
||||
|
||||
|
||||
void limits_init()
|
||||
{
|
||||
@ -89,147 +87,63 @@ ISR(LIMIT_INT_vect)
|
||||
// algorithm is written here. This also lets users hack and tune this code freely for
|
||||
// their own particular needs without affecting the rest of Grbl.
|
||||
// NOTE: Only the abort runtime command can interrupt this process.
|
||||
static void homing_cycle(uint8_t cycle_mask, int8_t pos_dir, bool invert_pin, float homing_rate)
|
||||
static void homing_cycle(uint8_t cycle_mask, bool pos_dir, bool invert_pin, float homing_rate)
|
||||
{
|
||||
|
||||
/* TODO: Change homing routine to call planner instead moving at the maximum seek rates
|
||||
and (max_travel+10mm?) for each axes during the search phase. The routine should monitor
|
||||
the state of the limit pins and when a pin is triggered, it can disable that axes by
|
||||
setting the respective step_x, step_y, or step_z value in the executing planner block.
|
||||
This keeps the stepper algorithm counters from triggering the step on that particular
|
||||
axis. When all axes have been triggered, we can then disable the steppers and reset
|
||||
the stepper and planner buffers. This same method can be used for the locate cycles.
|
||||
This will also fix the slow max feedrate of the homing 'lite' stepper algorithm.
|
||||
|
||||
Need to check if setting the planner steps will require them to be volatile or not. */
|
||||
|
||||
#ifdef LIMIT_SWITCHES_ACTIVE_HIGH
|
||||
// When in an active-high switch configuration, invert_pin needs to be adjusted.
|
||||
invert_pin = !invert_pin;
|
||||
#endif
|
||||
|
||||
// Determine governing axes with finest step resolution per distance for the Bresenham
|
||||
// algorithm. This solves the issue when homing multiple axes that have different
|
||||
// resolutions without exceeding system acceleration setting. It doesn't have to be
|
||||
// perfect since homing locates machine zero, but should create for a more consistent
|
||||
// and speedy homing routine.
|
||||
// NOTE: For each axes enabled, the following calculations assume they physically move
|
||||
// an equal distance over each time step until they hit a limit switch, aka dogleg.
|
||||
uint32_t step_event_count = 0;
|
||||
uint8_t i, dist = 0;
|
||||
uint32_t steps[N_AXIS];
|
||||
clear_vector(steps);
|
||||
for (i=0; i<N_AXIS; i++) {
|
||||
if (cycle_mask & (1<<i)) {
|
||||
dist++;
|
||||
steps[i] = lround(settings.steps_per_mm[i]);
|
||||
step_event_count = max(step_event_count,steps[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// To ensure global acceleration is not exceeded, reduce the governing axes nominal rate
|
||||
// by adjusting the actual axes distance traveled per step. This is the same procedure
|
||||
// used in the main planner to account for distance traveled when moving multiple axes.
|
||||
// NOTE: When axis acceleration independence is installed, this will be updated to move
|
||||
// all axes at their maximum acceleration and rate.
|
||||
float ds = step_event_count/sqrt(dist);
|
||||
|
||||
// Compute the adjusted step rate change with each acceleration tick. (in step/min/acceleration_tick)
|
||||
uint32_t delta_rate = ceil( ds*settings.acceleration[X_AXIS]/(60*ACCELERATION_TICKS_PER_SECOND));
|
||||
|
||||
#ifdef HOMING_RATE_ADJUST
|
||||
// Adjust homing rate so a multiple axes moves all at the homing rate independently.
|
||||
homing_rate *= sqrt(dist); // Eq. only works if axes values are 1 or 0.
|
||||
#endif
|
||||
|
||||
// Nominal and initial time increment per step. Nominal should always be greater then 3
|
||||
// usec, since they are based on the same parameters as the main stepper routine. Initial
|
||||
// is based on the MINIMUM_STEPS_PER_MINUTE config. Since homing feed can be very slow,
|
||||
// disable acceleration when rates are below MINIMUM_STEPS_PER_MINUTE.
|
||||
uint32_t dt_min = lround(1000000*60/(ds*homing_rate)); // Cruising (usec/step)
|
||||
uint32_t dt = 1000000*60/MINIMUM_STEPS_PER_MINUTE; // Initial (usec/step)
|
||||
if (dt > dt_min) { dt = dt_min; } // Disable acceleration for very slow rates.
|
||||
|
||||
// Set default out_bits.
|
||||
uint8_t out_bits0 = settings.invert_mask;
|
||||
out_bits0 ^= (settings.homing_dir_mask & DIRECTION_MASK); // Apply homing direction settings
|
||||
if (!pos_dir) { out_bits0 ^= DIRECTION_MASK; } // Invert bits, if negative dir.
|
||||
|
||||
// Initialize stepping variables
|
||||
int32_t counter_x = -(step_event_count >> 1); // Bresenham counters
|
||||
int32_t counter_y = counter_x;
|
||||
int32_t counter_z = counter_x;
|
||||
uint32_t step_delay = dt-settings.pulse_microseconds; // Step delay after pulse
|
||||
uint32_t step_rate = 0; // Tracks step rate. Initialized from 0 rate. (in step/min)
|
||||
uint32_t trap_counter = MICROSECONDS_PER_ACCELERATION_TICK/2; // Acceleration trapezoid counter
|
||||
uint8_t out_bits;
|
||||
if (sys.execute & EXEC_RESET) { return; }
|
||||
uint8_t limit_state;
|
||||
for(;;) {
|
||||
#ifndef LIMIT_SWITCHES_ACTIVE_HIGH
|
||||
invert_pin = !invert_pin;
|
||||
#endif
|
||||
|
||||
// Compute target location for homing all axes. Homing axis lock will freeze non-cycle axes.
|
||||
float target[N_AXIS];
|
||||
target[X_AXIS] = settings.max_travel[X_AXIS];
|
||||
if (target[X_AXIS] < settings.max_travel[Y_AXIS]) { target[X_AXIS] = settings.max_travel[Y_AXIS]; }
|
||||
if (target[X_AXIS] < settings.max_travel[Z_AXIS]) { target[X_AXIS] = settings.max_travel[Z_AXIS]; }
|
||||
target[X_AXIS] *= 2.0;
|
||||
if (pos_dir) { target[X_AXIS] = -target[X_AXIS]; }
|
||||
target[Y_AXIS] = target[X_AXIS];
|
||||
target[Z_AXIS] = target[X_AXIS];
|
||||
homing_rate *= 1.7320; // [sqrt(N_AXIS)] Adjust so individual axes all move at homing rate.
|
||||
|
||||
// Setup homing axis locks based on cycle mask.
|
||||
uint8_t axislock = (STEPPING_MASK & ~STEP_MASK);
|
||||
if (bit_istrue(cycle_mask,bit(X_AXIS))) { axislock |= (1<<X_STEP_BIT); }
|
||||
if (bit_istrue(cycle_mask,bit(Y_AXIS))) { axislock |= (1<<Y_STEP_BIT); }
|
||||
if (bit_istrue(cycle_mask,bit(Z_AXIS))) { axislock |= (1<<Z_STEP_BIT); }
|
||||
sys.homing_axis_lock = axislock;
|
||||
|
||||
// Reset out bits. Both direction and step pins appropriately inverted and set.
|
||||
out_bits = out_bits0;
|
||||
|
||||
// Get limit pin state.
|
||||
// Perform homing cycle. Planner buffer should be empty, as required to initiate the homing cycle.
|
||||
plan_buffer_line(target, homing_rate, false); // Bypass mc_line(). Directly plan homing motion.
|
||||
st_prep_buffer(); // Prep first segment from newly planned block.
|
||||
st_wake_up(); // Initiate motion
|
||||
while (STEP_MASK & axislock) {
|
||||
// Check limit state.
|
||||
limit_state = LIMIT_PIN;
|
||||
if (invert_pin) { limit_state ^= LIMIT_MASK; } // If leaving switch, invert to move.
|
||||
|
||||
// Set step pins by Bresenham line algorithm. If limit switch reached, disable and
|
||||
// flag for completion.
|
||||
if (cycle_mask & (1<<X_AXIS)) {
|
||||
counter_x += steps[X_AXIS];
|
||||
if (counter_x > 0) {
|
||||
if (limit_state & (1<<X_LIMIT_BIT)) { out_bits ^= (1<<X_STEP_BIT); }
|
||||
else { cycle_mask &= ~(1<<X_AXIS); }
|
||||
counter_x -= step_event_count;
|
||||
}
|
||||
if (invert_pin) { limit_state ^= LIMIT_MASK; }
|
||||
if (axislock & (1<<X_STEP_BIT)) {
|
||||
if (limit_state & (1<<X_LIMIT_BIT)) { axislock &= ~(1<<X_STEP_BIT); }
|
||||
}
|
||||
if (cycle_mask & (1<<Y_AXIS)) {
|
||||
counter_y += steps[Y_AXIS];
|
||||
if (counter_y > 0) {
|
||||
if (limit_state & (1<<Y_LIMIT_BIT)) { out_bits ^= (1<<Y_STEP_BIT); }
|
||||
else { cycle_mask &= ~(1<<Y_AXIS); }
|
||||
counter_y -= step_event_count;
|
||||
}
|
||||
if (axislock & (1<<Y_STEP_BIT)) {
|
||||
if (limit_state & (1<<Y_LIMIT_BIT)) { axislock &= ~(1<<Y_STEP_BIT); }
|
||||
}
|
||||
if (cycle_mask & (1<<Z_AXIS)) {
|
||||
counter_z += steps[Z_AXIS];
|
||||
if (counter_z > 0) {
|
||||
if (limit_state & (1<<Z_LIMIT_BIT)) { out_bits ^= (1<<Z_STEP_BIT); }
|
||||
else { cycle_mask &= ~(1<<Z_AXIS); }
|
||||
counter_z -= step_event_count;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we are done or for system abort
|
||||
if (!(cycle_mask) || (sys.execute & EXEC_RESET)) { return; }
|
||||
|
||||
// Perform step.
|
||||
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (out_bits & STEP_MASK);
|
||||
delay_us(settings.pulse_microseconds);
|
||||
STEPPING_PORT = out_bits0;
|
||||
delay_us(step_delay);
|
||||
|
||||
// Track and set the next step delay, if required. This routine uses another Bresenham
|
||||
// line algorithm to follow the constant acceleration line in the velocity and time
|
||||
// domain. This is a lite version of the same routine used in the main stepper program.
|
||||
if (dt > dt_min) { // Unless cruising, check for time update.
|
||||
trap_counter += dt; // Track time passed since last update.
|
||||
if (trap_counter > MICROSECONDS_PER_ACCELERATION_TICK) {
|
||||
trap_counter -= MICROSECONDS_PER_ACCELERATION_TICK;
|
||||
step_rate += delta_rate; // Increment velocity
|
||||
dt = (1000000*60)/step_rate; // Compute new time increment
|
||||
if (dt < dt_min) {dt = dt_min;} // If target rate reached, cruise.
|
||||
step_delay = dt-settings.pulse_microseconds;
|
||||
}
|
||||
if (axislock & (1<<Z_STEP_BIT)) {
|
||||
if (limit_state & (1<<Z_LIMIT_BIT)) { axislock &= ~(1<<Z_STEP_BIT); }
|
||||
}
|
||||
sys.homing_axis_lock = axislock;
|
||||
st_prep_buffer(); // Check and prep one segment. NOTE: Should take no longer than 200us.
|
||||
if (sys.execute & EXEC_RESET) { return; }
|
||||
}
|
||||
st_go_idle(); // Disable steppers. Axes motion should already be locked.
|
||||
plan_init(); // Reset planner buffer. Ensure homing motion is cleared.
|
||||
st_reset(); // Reset step segment buffer. Ensure homing motion is cleared.
|
||||
delay_ms(settings.homing_debounce_delay);
|
||||
}
|
||||
|
||||
|
||||
void limits_go_home()
|
||||
{
|
||||
// Enable only the steppers, not the cycle. Cycle should be inactive/complete.
|
||||
st_wake_up();
|
||||
plan_init(); // Reset planner buffer before beginning homing cycles.
|
||||
|
||||
// Search to engage all axes limit switches at faster homing seek rate.
|
||||
homing_cycle(HOMING_SEARCH_CYCLE_0, true, false, settings.homing_seek_rate); // Search cycle 0
|
||||
@ -239,7 +153,6 @@ void limits_go_home()
|
||||
#ifdef HOMING_SEARCH_CYCLE_2
|
||||
homing_cycle(HOMING_SEARCH_CYCLE_2, true, false, settings.homing_seek_rate); // Search cycle 2
|
||||
#endif
|
||||
delay_ms(settings.homing_debounce_delay); // Delay to debounce signal
|
||||
|
||||
// Now in proximity of all limits. Carefully leave and approach switches in multiple cycles
|
||||
// to precisely hone in on the machine zero location. Moves at slower homing feed rate.
|
||||
@ -247,16 +160,12 @@ void limits_go_home()
|
||||
while (n_cycle--) {
|
||||
// Leave all switches to release them. After cycles complete, this is machine zero.
|
||||
homing_cycle(HOMING_LOCATE_CYCLE, false, true, settings.homing_feed_rate);
|
||||
delay_ms(settings.homing_debounce_delay);
|
||||
|
||||
|
||||
if (n_cycle > 0) {
|
||||
// Re-approach all switches to re-engage them.
|
||||
homing_cycle(HOMING_LOCATE_CYCLE, true, false, settings.homing_feed_rate);
|
||||
delay_ms(settings.homing_debounce_delay);
|
||||
}
|
||||
}
|
||||
|
||||
st_go_idle(); // Call main stepper shutdown routine.
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user