2009-01-25 00:48:56 +01:00
|
|
|
/*
|
|
|
|
motion_control.c - cartesian robot controller.
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-29 09:58:29 +01:00
|
|
|
/* The structure of this module was inspired by the Arduino GCode_Interpreter by Mike Ellery. The arc
|
|
|
|
interpolator written from the information provided in the Wikipedia article 'Midpoint circle algorithm'
|
|
|
|
and the lecture 'Circle Drawing Algorithms' by Leonard McMillan.
|
|
|
|
|
|
|
|
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
|
|
|
|
http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html
|
|
|
|
*/
|
2009-01-25 00:48:56 +01:00
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "motion_control.h"
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nuts_bolts.h"
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "stepper.h"
|
2009-02-03 09:56:45 +01:00
|
|
|
|
2009-02-08 12:24:52 +01:00
|
|
|
#define ONE_MINUTE_OF_MICROSECONDS 60000000.0
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
volatile int8_t mode; // The current operation mode
|
2009-02-08 21:22:54 +01:00
|
|
|
int32_t position[3]; // The current position of the tool in absolute steps
|
|
|
|
uint8_t direction_bits; // The direction bits to be used with any upcoming step-instruction
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2009-01-29 23:12:06 +01:00
|
|
|
void set_stepper_directions(int8_t *direction);
|
2009-02-08 21:22:54 +01:00
|
|
|
inline void step_steppers(uint8_t bits);
|
2009-01-25 00:48:56 +01:00
|
|
|
inline void step_axis(uint8_t axis);
|
2009-02-01 11:58:21 +01:00
|
|
|
void prepare_linear_motion(uint32_t x, uint32_t y, uint32_t z, float feed_rate, int invert_feed_rate);
|
2009-01-25 00:48:56 +01:00
|
|
|
|
|
|
|
void mc_init()
|
|
|
|
{
|
2009-02-08 22:08:27 +01:00
|
|
|
mode = MC_MODE_AT_REST;
|
2009-02-08 21:22:54 +01:00
|
|
|
clear_vector(position);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mc_dwell(uint32_t milliseconds)
|
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
mode = MC_MODE_DWELL;
|
|
|
|
st_synchronize();
|
|
|
|
_delay_ms(milliseconds);
|
|
|
|
mode = MC_MODE_AT_REST;
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
// Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second
|
2009-01-30 11:05:10 +01:00
|
|
|
// unless invert_feed_rate is true. Then the feed_rate states the number of seconds for the whole movement.
|
2009-02-08 21:22:54 +01:00
|
|
|
void mc_line(double x, double y, double z, float feed_rate, int invert_feed_rate)
|
2009-02-03 09:56:45 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
// Flags to keep track of which axes to step
|
|
|
|
uint8_t step_bits;
|
|
|
|
uint8_t axis; // loop variable
|
|
|
|
int8_t direction[3]; // The direction of travel along each axis (-1, 0 or 1)
|
|
|
|
int32_t target[3], // The target position in absolute steps
|
|
|
|
step_count[3], // Absolute steps of travel along each axis
|
|
|
|
counter[3], // A counter used in the bresenham algorithm for line plotting
|
|
|
|
maximum_steps; // The larges absolute step-count of any axis
|
2009-02-04 14:01:24 +01:00
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
target[X_AXIS] = x*X_STEPS_PER_MM;
|
|
|
|
target[Y_AXIS] = y*Y_STEPS_PER_MM;
|
|
|
|
target[Z_AXIS] = z*Z_STEPS_PER_MM;
|
2009-01-25 00:48:56 +01:00
|
|
|
// Determine direction and travel magnitude for each axis
|
|
|
|
for(axis = X_AXIS; axis <= Z_AXIS; axis++) {
|
2009-02-08 21:22:54 +01:00
|
|
|
step_count[axis] = abs(target[axis] - position[axis]);
|
|
|
|
direction[axis] = signof(target[axis] - position[axis]);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
// Find the magnitude of the axis with the longest travel
|
2009-02-08 21:22:54 +01:00
|
|
|
maximum_steps = max(step_count[Z_AXIS],
|
|
|
|
max(step_count[X_AXIS], step_count[Y_AXIS]));
|
2009-02-03 09:56:45 +01:00
|
|
|
// Nothing to do?
|
2009-02-08 22:08:27 +01:00
|
|
|
if (maximum_steps == 0) { return; }
|
2009-01-25 00:48:56 +01:00
|
|
|
// Set up a neat counter for each axis
|
|
|
|
for(axis = X_AXIS; axis <= Z_AXIS; axis++) {
|
2009-02-08 21:22:54 +01:00
|
|
|
counter[axis] = -maximum_steps/2;
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
// Set our direction pins
|
2009-02-08 21:22:54 +01:00
|
|
|
set_stepper_directions(direction);
|
2009-01-25 00:48:56 +01:00
|
|
|
// Calculate the microseconds we need to wait between each step to achieve the desired feed rate
|
|
|
|
if (invert_feed_rate) {
|
2009-02-08 21:22:54 +01:00
|
|
|
st_buffer_pace((feed_rate*1000000)/maximum_steps);
|
2009-01-25 00:48:56 +01:00
|
|
|
} else {
|
2009-02-04 14:01:24 +01:00
|
|
|
// Ask old Phytagoras to estimate how many mm our next move is going to take us:
|
|
|
|
double millimeters_to_travel =
|
2009-02-08 21:22:54 +01:00
|
|
|
sqrt(pow(X_STEPS_PER_MM*step_count[X_AXIS],2) +
|
|
|
|
pow(Y_STEPS_PER_MM*step_count[Y_AXIS],2) +
|
|
|
|
pow(Z_STEPS_PER_MM*step_count[Z_AXIS],2));
|
2009-02-04 14:01:24 +01:00
|
|
|
// Calculate the microseconds between steps that we should wait in order to travel the
|
|
|
|
// designated amount of millimeters in the amount of steps we are going to generate
|
2009-02-08 21:22:54 +01:00
|
|
|
st_buffer_pace(((millimeters_to_travel * ONE_MINUTE_OF_MICROSECONDS) / feed_rate) / maximum_steps);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
// Execution
|
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
mode = MC_MODE_LINEAR;
|
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
while(mode) {
|
2009-02-03 09:56:45 +01:00
|
|
|
// Trace the line
|
2009-02-08 21:22:54 +01:00
|
|
|
step_bits = 0;
|
2009-02-03 09:56:45 +01:00
|
|
|
for(axis = X_AXIS; axis <= Z_AXIS; axis++) {
|
2009-02-08 21:22:54 +01:00
|
|
|
if (target[axis] != position[axis])
|
2009-02-03 09:56:45 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
counter[axis] += step_count[axis];
|
|
|
|
if (counter[axis] > 0)
|
2009-02-03 09:56:45 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
step_bits |= st_bit_for_stepper(axis);
|
|
|
|
counter[axis] -= maximum_steps;
|
|
|
|
position[axis] += direction[axis];
|
2009-02-03 09:56:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 21:22:54 +01:00
|
|
|
if (step_bits) {
|
|
|
|
step_steppers(step_bits);
|
2009-02-03 09:56:45 +01:00
|
|
|
} else {
|
2009-02-08 21:22:54 +01:00
|
|
|
mode = MC_MODE_AT_REST;
|
2009-02-03 09:56:45 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
// Execute an arc. theta == start angle, angular_travel == number of radians to go along the arc,
|
2009-01-29 23:12:06 +01:00
|
|
|
// positive angular_travel means clockwise, negative means counterclockwise. Radius == the radius of the
|
|
|
|
// circle in millimeters. axis_1 and axis_2 selects the plane in tool space.
|
2009-01-30 11:05:10 +01:00
|
|
|
// ISSUE: The arc interpolator assumes all axes have the same steps/mm as the X axis.
|
|
|
|
void mc_arc(double theta, double angular_travel, double radius, int axis_1, int axis_2, double feed_rate)
|
2009-01-29 23:12:06 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
uint32_t start_x, start_y;
|
|
|
|
uint32_t diagonal_error;
|
|
|
|
|
|
|
|
int8_t direction[3]; // The direction of travel along each axis (-1, 0 or 1)
|
|
|
|
int8_t angular_direction; // 1 = clockwise, -1 = anticlockwise
|
|
|
|
int32_t x, y, target_x, target_y; // current position and target position in the
|
|
|
|
// local coordinate system of the arc-generator where [0,0] is the
|
|
|
|
// center of the arc.
|
|
|
|
int target_direction_x, target_direction_y; // signof(target_x)*angular_direction precalculated for speed
|
|
|
|
int32_t error, x2, y2; // error is always == (x**2 + y**2 - radius**2),
|
|
|
|
// x2 is always 2*x, y2 is always 2*y
|
|
|
|
uint8_t axis_x, axis_y; // maps the arc axes to stepper axes
|
2009-02-08 22:08:27 +01:00
|
|
|
int8_t diagonal_bits; // A bitmask with the stepper bits for both selected axes set
|
2009-02-08 21:22:54 +01:00
|
|
|
int incomplete; // True if the arc has not reached its target yet
|
|
|
|
|
|
|
|
int dx, dy; // Trace directions
|
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
// Setup
|
|
|
|
|
2009-02-03 09:56:45 +01:00
|
|
|
uint32_t radius_steps = round(radius*X_STEPS_PER_MM);
|
2009-02-08 12:24:52 +01:00
|
|
|
if(radius_steps == 0) { return; }
|
2009-01-28 23:48:21 +01:00
|
|
|
// Determine angular direction (+1 = clockwise, -1 = counterclockwise)
|
2009-02-08 21:22:54 +01:00
|
|
|
angular_direction = signof(angular_travel);
|
2009-01-29 23:12:06 +01:00
|
|
|
// Calculate the initial position and target position in the local coordinate system of the arc
|
2009-02-08 21:22:54 +01:00
|
|
|
start_x = x = round(sin(theta)*radius_steps);
|
|
|
|
start_y = y = round(cos(theta)*radius_steps);
|
|
|
|
target_x = trunc(sin(theta+angular_travel)*radius_steps);
|
|
|
|
target_y = trunc(cos(theta+angular_travel)*radius_steps);
|
2009-01-29 23:12:06 +01:00
|
|
|
// Precalculate these values to optimize target detection
|
2009-02-08 21:22:54 +01:00
|
|
|
target_direction_x = signof(target_x)*angular_direction;
|
|
|
|
target_direction_y = signof(target_y)*angular_direction;
|
2009-01-28 23:48:21 +01:00
|
|
|
// The "error" factor is kept up to date so that it is always == (x**2+y**2-radius**2). When error
|
2009-01-29 23:12:06 +01:00
|
|
|
// <0 we are inside the arc, when it is >0 we are outside of the arc, and when it is 0 we
|
|
|
|
// are exactly on top of the arc.
|
2009-02-08 21:22:54 +01:00
|
|
|
error = x*x + y*y - radius_steps*radius_steps;
|
2009-01-28 23:48:21 +01:00
|
|
|
// Because the error-value moves in steps of (+/-)2x+1 and (+/-)2y+1 we save a couple of multiplications
|
2009-01-29 23:12:06 +01:00
|
|
|
// by keeping track of the doubles of the arc coordinates at all times.
|
2009-02-08 21:22:54 +01:00
|
|
|
x2 = 2*x;
|
|
|
|
y2 = 2*y;
|
2009-01-29 23:12:06 +01:00
|
|
|
// Set up a vector with the steppers we are going to use tracing the plane of this arc
|
2009-02-08 21:22:54 +01:00
|
|
|
diagonal_bits = st_bit_for_stepper(axis_1);
|
|
|
|
diagonal_bits |= st_bit_for_stepper(axis_2);
|
2009-01-29 23:12:06 +01:00
|
|
|
// And map the local coordinate system of the arc onto the tool axes of the selected plane
|
2009-02-08 21:22:54 +01:00
|
|
|
axis_x = axis_1;
|
|
|
|
axis_y = axis_2;
|
2009-02-08 22:08:27 +01:00
|
|
|
// The amount of steppings performed while tracing a half circle is equal to the sum of sides in a
|
|
|
|
// square inscribed in the circle. We use this to estimate the amount of steps as if this arc was a half circle:
|
2009-02-08 12:24:52 +01:00
|
|
|
uint32_t steps_in_half_circle = round(radius_steps * 4 * (1/sqrt(2)));
|
2009-02-08 22:08:27 +01:00
|
|
|
// We then calculate the millimeters of travel along the circumference of that same half circle
|
2009-02-08 12:24:52 +01:00
|
|
|
double millimeters_half_circumference = radius*M_PI;
|
2009-02-04 14:01:24 +01:00
|
|
|
// Then we calculate the microseconds between each step as if we will trace the full circle.
|
2009-02-08 22:08:27 +01:00
|
|
|
// It doesn't matter what fraction of the circle we are actually going to trace. The pace is the same.
|
|
|
|
st_buffer_pace(((millimeters_half_circumference * ONE_MINUTE_OF_MICROSECONDS) / feed_rate) / steps_in_half_circle);
|
2009-01-28 23:48:21 +01:00
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
// Execution
|
2009-02-08 22:08:27 +01:00
|
|
|
|
|
|
|
mode = MC_MODE_ARC;
|
2009-02-08 21:22:54 +01:00
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
incomplete = true;
|
2009-02-08 21:22:54 +01:00
|
|
|
while(incomplete)
|
2009-01-29 23:12:06 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
dx = (y!=0) ? signof(y) * angular_direction : -signof(x);
|
|
|
|
dy = (x!=0) ? -signof(x) * angular_direction : -signof(y);
|
2009-02-03 09:56:45 +01:00
|
|
|
// Take dx and dy which are local to the arc being generated and map them on to the
|
|
|
|
// selected tool-space-axes for the current arc.
|
2009-02-08 21:22:54 +01:00
|
|
|
direction[axis_x] = dx;
|
|
|
|
direction[axis_y] = dy;
|
|
|
|
set_stepper_directions(direction);
|
2009-02-08 22:08:27 +01:00
|
|
|
// Check which axis will be "major" for this stepping
|
2009-02-08 21:22:54 +01:00
|
|
|
if (abs(x)<abs(y)) {
|
2009-02-08 22:08:27 +01:00
|
|
|
// Step arc horizontally
|
2009-02-08 21:22:54 +01:00
|
|
|
error += 1+x2*dx;
|
2009-02-08 22:08:27 +01:00
|
|
|
x+=dx; x2 += 2*dx;
|
2009-02-08 21:22:54 +01:00
|
|
|
diagonal_error = error + 1 + y2*dy;
|
|
|
|
if(abs(error) >= abs(diagonal_error)) {
|
2009-02-08 22:08:27 +01:00
|
|
|
y += dy; y2 += 2*dy;
|
2009-02-08 21:22:54 +01:00
|
|
|
error = diagonal_error;
|
|
|
|
step_steppers(diagonal_bits); // step diagonal
|
|
|
|
} else {
|
|
|
|
step_axis(axis_x); // step straight
|
|
|
|
}
|
2009-02-03 09:56:45 +01:00
|
|
|
} else {
|
2009-02-08 22:08:27 +01:00
|
|
|
// Step arc vertically
|
2009-02-08 21:22:54 +01:00
|
|
|
error += 1+y2*dy;
|
2009-02-08 22:08:27 +01:00
|
|
|
y+=dy; y2 += 2*dy;
|
2009-02-08 21:22:54 +01:00
|
|
|
diagonal_error = error + 1 + x2*dx;
|
|
|
|
if(abs(error) >= abs(diagonal_error)) {
|
2009-02-08 22:08:27 +01:00
|
|
|
x += dx; x2 += 2*dx;
|
2009-02-08 21:22:54 +01:00
|
|
|
error = diagonal_error;
|
|
|
|
step_steppers(diagonal_bits); // step diagonal
|
|
|
|
} else {
|
|
|
|
step_axis(axis_y); // step straight
|
|
|
|
}
|
2009-02-08 22:08:27 +01:00
|
|
|
}
|
|
|
|
// Check if target has been reached. Todo: Simplify/optimize/clarify
|
2009-02-08 21:22:54 +01:00
|
|
|
if ((x * target_direction_y >=
|
|
|
|
target_x * target_direction_y) &&
|
|
|
|
(y * target_direction_x <=
|
|
|
|
target_y * target_direction_x))
|
|
|
|
{ if ((signof(x) == signof(target_x)) && (signof(y) == signof(target_y)))
|
|
|
|
{ incomplete = false; } }
|
2009-02-08 22:08:27 +01:00
|
|
|
}
|
2009-02-01 11:58:21 +01:00
|
|
|
// Update the tool position to the new actual position
|
2009-02-08 21:22:54 +01:00
|
|
|
position[axis_x] += x-start_x;
|
|
|
|
position[axis_y] += y-start_y;
|
|
|
|
mode = MC_MODE_AT_REST;
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
void mc_go_home()
|
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
mode = MC_MODE_HOME;
|
2009-01-28 23:48:21 +01:00
|
|
|
st_go_home();
|
2009-01-29 23:12:06 +01:00
|
|
|
st_synchronize();
|
2009-02-08 21:22:54 +01:00
|
|
|
clear_vector(position); // By definition this is location [0, 0, 0]
|
|
|
|
mode = MC_MODE_AT_REST;
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int mc_status()
|
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
return(mode);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 22:08:27 +01:00
|
|
|
// Set the direction bits for the stepper motors according to the provided vector.
|
2009-01-25 00:48:56 +01:00
|
|
|
// direction is an array of three 8 bit integers representing the direction of
|
|
|
|
// each motor. The values should be -1 (reverse), 0 or 1 (forward).
|
2009-01-29 23:12:06 +01:00
|
|
|
void set_stepper_directions(int8_t *direction)
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
|
|
|
/* Sorry about this convoluted code! It uses the fact that bit 7 of each direction
|
|
|
|
int is set when the direction == -1, but is 0 when direction is forward. This
|
|
|
|
way we can generate the whole direction bit-mask without doing any comparisions
|
|
|
|
or branching. Fast and compact, yet practically unreadable. Sorry sorry sorry.
|
|
|
|
*/
|
2009-02-08 21:22:54 +01:00
|
|
|
direction_bits = (
|
2009-01-30 11:26:21 +01:00
|
|
|
((direction[X_AXIS]&0x80)>>(7-X_DIRECTION_BIT)) |
|
|
|
|
((direction[Y_AXIS]&0x80)>>(7-Y_DIRECTION_BIT)) |
|
2009-02-03 09:56:45 +01:00
|
|
|
((direction[Z_AXIS]&0x80)>>(7-Z_DIRECTION_BIT)));
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step enabled steppers. Enabled should be an array of three bytes. Each byte represent one
|
|
|
|
// stepper motor in the order X, Y, Z. Set the bytes of the steppers you want to step to
|
|
|
|
// 1, and the rest to 0.
|
2009-02-08 21:22:54 +01:00
|
|
|
inline void step_steppers(uint8_t bits)
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
2009-02-08 21:22:54 +01:00
|
|
|
st_buffer_step(direction_bits | bits);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step only one motor
|
|
|
|
inline void step_axis(uint8_t axis)
|
|
|
|
{
|
2009-02-08 22:08:27 +01:00
|
|
|
st_buffer_step(direction_bits | st_bit_for_stepper(axis));
|
2009-01-30 11:05:10 +01:00
|
|
|
}
|