/* stepper.c - stepper motor interface Part of Grbl Copyright (c) 2009 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 . */ /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith and Philipp Tiefenbacher. The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis */ #include "stepper.h" #include "config.h" #include #include #include #include "nuts_bolts.h" #include "acceleration.h" #include #include "wiring_serial.h" // Pick a suitable line-buffer size #ifdef __AVR_ATmega328P__ #define LINE_BUFFER_SIZE 40 // Atmega 328 has one full kilobyte of extra RAM! #else #define LINE_BUFFER_SIZE 10 #endif <<<<<<< Updated upstream struct Line { uint32_t steps_x, steps_y, steps_z; int32_t maximum_steps; uint8_t direction_bits; double average_millimeters_per_step_event; uin32_t ideal_rate; // in step-events/minute uin32_t exit_rate; uin32_t brake_point; // the point where braking starts measured in step-events from end point uint32_t rate; // in cpu-ticks pr. step }; struct Line line_buffer[LINE_BUFFER_SIZE]; // A buffer for step instructions volatile int line_buffer_head = 0; volatile int line_buffer_tail = 0; volatile int moving = FALSE; // Variables used by SIG_OUTPUT_COMPARE1A uint8_t out_bits; // The next stepping-bits to be output struct Line *current_line; // A pointer to the line currently being traced volatile int32_t counter_x, counter_y, counter_z; // counter variables for the bresenham line tracer uint32_t iterations; // The number of iterations left to complete the current_line volatile int busy; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler. void set_step_events_per_minute(uint32_t steps_per_minute); uint32_t mm_per_minute_to_step_events_pr_minute(struct Line* line, double mm_per_minute) { return(mm_per_minute/line->average_millimeters_per_step_event); } void update_accelleration_plan() { // Store the current int initial_buffer_tail = line_buffer_tail; } ======= #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<>>>>>> Stashed changes // Add a new linear movement to the buffer. steps_x, _y and _z is the signed, relative motion in // steps. Microseconds specify how many microseconds the move should take to perform. To aid accelleration // calculation the caller must also provide the physical length of the line in millimeters. void st_buffer_line(int32_t steps_x, int32_t steps_y, int32_t steps_z, uint32_t microseconds, double millimeters) { // Calculate the buffer head after we push this byte int next_buffer_head = (line_buffer_head + 1) % LINE_BUFFER_SIZE; // If the buffer is full: good! That means we are well ahead of the robot. <<<<<<< Updated upstream // Nap until there is room in the buffer. while(line_buffer_tail == next_buffer_head) { sleep_mode(); } // Setup line record struct Line *line = &line_buffer[line_buffer_head]; line->steps_x = labs(steps_x); line->steps_y = labs(steps_y); line->steps_z = labs(steps_z); line->maximum_steps = max(line->steps_x, max(line->steps_y, line->steps_z)); // Bail if this is a zero-length line if (line->maximum_steps == 0) { return; }; line->rate = (TICKS_PER_MICROSECOND*microseconds)/line->maximum_steps; ======= // Rest here until there is room in the buffer. while(block_buffer_tail == next_buffer_head) { sleep_mode(); } // Prepare to set up new block struct Block *block = &block_buffer[block_buffer_head]; // Number of steps for each axis block->steps_x = labs(steps_x); block->steps_y = labs(steps_y); block->steps_z = labs(steps_z); block->maximum_steps = max(block->steps_x, max(block->steps_y, block->steps_z)); // Bail if this is a zero-length block if (block->maximum_steps == 0) { return; }; // Rate in steps/second for each axis double rate_multiplier = 60.0*1000000.0/microseconds; block->rate_x = round(block->steps_x*rate_multiplier); block->rate_y = round(block->steps_y*rate_multiplier); block->rate_z = round(block->steps_z*rate_multiplier); block->rate = microseconds/block->maximum_steps; // Compute direction bits for this block >>>>>>> Stashed changes uint8_t direction_bits = 0; if (steps_x < 0) { direction_bits |= (1<direction_bits = direction_bits; line->average_millimeters_per_step_event = millimeters/line->maximum_steps // Move buffer head <<<<<<< Updated upstream line_buffer_head = next_buffer_head; // enable stepper interrupt TIMSK1 |= (1<>>>>>> Stashed changes #ifdef TIMER1_COMPA_vect SIGNAL(TIMER1_COMPA_vect) #else SIGNAL(SIG_OUTPUT_COMPARE1A) #endif { if(busy){ return; } // The busy-flag is used to avoid reentering this interrupt PORTD |= (1<<3); // Set the direction pins a cuple of nanoseconds before we step the steppers STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK); // Then pulse the stepping pins STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | out_bits; // Reset step pulse reset timer so that The Stepper Port Reset Interrupt can reset the signal after // exactly settings.pulse_microseconds microseconds. TCNT2 = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND)/8); busy = TRUE; sei(); // Re enable interrupts (normally disabled while inside an interrupt handler) // We re-enable interrupts in order for SIG_OVERFLOW2 to be able to be triggered // at exactly the right time even if we occasionally spend a lot of time inside this handler. // If there is no current line, attempt to pop one from the buffer if (current_line == NULL) { PORTD &= ~(1<<4); // Anything in the buffer? if (line_buffer_head != line_buffer_tail) { PORTD ^= (1<<5); // Retrieve a new line and get ready to step it current_line = &line_buffer[line_buffer_tail]; config_step_timer(current_line->rate); counter_x = -(current_line->maximum_steps >> 1); counter_y = counter_x; counter_z = counter_x; iterations = current_line->maximum_steps; moving = TRUE; } else { // disable this interrupt until there is something to handle moving = FALSE; TIMSK1 &= ~(1<direction_bits; counter_x += current_line->steps_x; if (counter_x > 0) { out_bits |= (1<maximum_steps; } counter_y += current_line->steps_y; if (counter_y > 0) { out_bits |= (1<maximum_steps; } counter_z += current_line->steps_z; if (counter_z > 0) { out_bits |= (1<maximum_steps; } // If current line is finished, reset pointer iterations -= 1; if (iterations <= 0) { current_line = NULL; // move the line buffer tail to the next instruction line_buffer_tail = (line_buffer_tail + 1) % LINE_BUFFER_SIZE; } } else { out_bits = 0; } out_bits ^= settings.invert_mask; busy=FALSE; PORTD &= ~(1<<3); } // This interrupt is set up by SIG_OUTPUT_COMPARE1A when it sets the motor port bits. It resets // the motor port after a short period (settings.pulse_microseconds) completing one step cycle. #ifdef TIMER2_OVF_vect SIGNAL(TIMER2_OVF_vect) #else SIGNAL(SIG_OVERFLOW2) #endif { // reset stepping pins (leave the direction pins) STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK); } // Initialize and start the stepper motor subsystem void st_init() { // Configure directions of interface pins STEPPING_DDR |= STEPPING_MASK; STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask; LIMIT_DDR &= ~(LIMIT_MASK); STEPPERS_ENABLE_DDR |= 1<> 3; prescaler = 1; // prescaler: 8 } else if (ticks <= 0x3fffffL) { ceiling = ticks >> 6; prescaler = 2; // prescaler: 64 } else if (ticks <= 0xffffffL) { ceiling = (ticks >> 8); prescaler = 3; // prescaler: 256 } else if (ticks <= 0x3ffffffL) { ceiling = (ticks >> 10); prescaler = 4; // prescaler: 1024 } else { // Okay, that was slower than we actually go. Just set the slowest speed ceiling = 0xffff; prescaler = 4; } // Set prescaler TCCR1B = (TCCR1B & ~(0x07<