/*
limits.c - code pertaining to limit-switches and performing the homing cycle
Part of Grbl
Copyright (c) 2009-2011 Simen Svale Skogsrud
Copyright (c) 2012 Sungeun K. Jeon
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 .
*/
#include
#include
#include
#include "stepper.h"
#include "settings.h"
#include "nuts_bolts.h"
#include "config.h"
#include "spindle_control.h"
#include "motion_control.h"
#include "planner.h"
#include "protocol.h"
#include "limits.h"
#define MICROSECONDS_PER_ACCELERATION_TICK (1000000/ACCELERATION_TICKS_PER_SECOND)
void limits_init()
{
LIMIT_DDR &= ~(LIMIT_MASK); // Set as input pins
LIMIT_PORT |= (LIMIT_MASK); // Enable internal pull-up resistors. Normal high operation.
if bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE) {
MCUCR = (1< 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;
uint8_t limit_state;
for(;;) {
// Reset out bits. Both direction and step pins appropriately inverted and set.
out_bits = out_bits0;
// Get limit pin 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 (x_axis) {
counter_x += steps[X_AXIS];
if (counter_x > 0) {
if (limit_state & (1< 0) {
if (limit_state & (1< 0) {
if (limit_state & (1< 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;
}
}
}
}
void limits_go_home()
{
STEPPERS_DISABLE_PORT &= ~(1< 0) {
// Re-approach all switches to re-engage them.
homing_cycle(true, true, true, true, false, settings.homing_feed_rate);
delay_ms(settings.homing_debounce_delay);
}
}
st_go_idle(); // Call main stepper shutdown routine.
}