2009-01-25 00:48:56 +01:00
|
|
|
/*
|
2011-01-14 16:45:18 +01:00
|
|
|
motion_control.c - high level interface for issuing motion commands
|
2009-01-25 00:48:56 +01:00
|
|
|
Part of Grbl
|
|
|
|
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
2009-01-25 00:48:56 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-09 15:47:51 +01:00
|
|
|
|
|
|
|
#include "wiring_serial.h"
|
2009-02-03 09:56:45 +01:00
|
|
|
|
2009-02-08 21:22:54 +01:00
|
|
|
int32_t position[3]; // The current position of the tool in absolute steps
|
2009-01-25 00:48:56 +01:00
|
|
|
|
|
|
|
void mc_init()
|
|
|
|
{
|
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)
|
|
|
|
{
|
2010-03-03 17:52:56 +01:00
|
|
|
st_synchronize();
|
|
|
|
_delay_ms(milliseconds);
|
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-02-11 00:37:33 +01:00
|
|
|
// unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in
|
|
|
|
// 1/feed_rate minutes.
|
2011-01-14 12:10:18 +01:00
|
|
|
void mc_line(double x, double y, double z, double feed_rate, int invert_feed_rate)
|
2009-02-03 09:56:45 +01:00
|
|
|
{
|
2010-03-03 00:26:48 +01:00
|
|
|
uint8_t axis; // loop variable
|
2010-03-02 21:46:51 +01:00
|
|
|
int32_t target[3]; // The target position in absolute steps
|
2010-03-03 00:26:48 +01:00
|
|
|
int32_t steps[3]; // The target line in relative steps
|
2009-02-08 22:08:27 +01:00
|
|
|
|
2010-03-07 20:29:18 +01:00
|
|
|
target[X_AXIS] = lround(x*settings.steps_per_mm[0]);
|
|
|
|
target[Y_AXIS] = lround(y*settings.steps_per_mm[1]);
|
|
|
|
target[Z_AXIS] = lround(z*settings.steps_per_mm[2]);
|
2010-03-03 00:26:48 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
for(axis = X_AXIS; axis <= Z_AXIS; axis++) {
|
2010-03-03 00:26:48 +01:00
|
|
|
steps[axis] = target[axis]-position[axis];
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2011-01-03 00:36:33 +01:00
|
|
|
// Ask old Phytagoras to estimate how many mm our next move is going to take us
|
|
|
|
double millimeters_of_travel = sqrt(
|
|
|
|
square(steps[X_AXIS]/settings.steps_per_mm[0]) +
|
|
|
|
square(steps[Y_AXIS]/settings.steps_per_mm[1]) +
|
|
|
|
square(steps[Z_AXIS]/settings.steps_per_mm[2]));
|
2010-03-03 00:26:48 +01:00
|
|
|
if (invert_feed_rate) {
|
2011-01-03 00:36:33 +01:00
|
|
|
st_buffer_line(steps[X_AXIS], steps[Y_AXIS], steps[Z_AXIS], lround(ONE_MINUTE_OF_MICROSECONDS/feed_rate),
|
|
|
|
millimeters_of_travel);
|
2010-03-03 00:26:48 +01:00
|
|
|
} else {
|
|
|
|
st_buffer_line(steps[X_AXIS], steps[Y_AXIS], steps[Z_AXIS],
|
2010-06-28 23:29:58 +02:00
|
|
|
lround((millimeters_of_travel/feed_rate)*1000000), millimeters_of_travel);
|
2010-03-03 00:26:48 +01:00
|
|
|
}
|
|
|
|
memcpy(position, target, sizeof(target)); // position[] = target[]
|
2009-01-25 00:48:56 +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
|
2009-02-11 00:37:33 +01:00
|
|
|
// circle in millimeters. axis_1 and axis_2 selects the circle plane in tool space. Stick the remaining
|
|
|
|
// axis in axis_l which will be the axis for linear travel if you are tracing a helical motion.
|
2010-03-03 17:52:56 +01:00
|
|
|
|
|
|
|
// The arc is approximated by generating a huge number of tiny, linear segments. The length of each
|
2010-06-28 23:29:58 +02:00
|
|
|
// segment is configured in settings.mm_per_arc_segment.
|
2009-02-11 00:37:33 +01:00
|
|
|
void mc_arc(double theta, double angular_travel, double radius, double linear_travel, int axis_1, int axis_2,
|
|
|
|
int axis_linear, double feed_rate, int invert_feed_rate)
|
2010-03-03 01:39:44 +01:00
|
|
|
{
|
|
|
|
double millimeters_of_travel = hypot(angular_travel*radius, labs(linear_travel));
|
|
|
|
if (millimeters_of_travel == 0.0) { return; }
|
2010-03-07 20:29:18 +01:00
|
|
|
uint16_t segments = ceil(millimeters_of_travel/settings.mm_per_arc_segment);
|
2010-03-03 17:52:56 +01:00
|
|
|
// Multiply inverse feed_rate to compensate for the fact that this movement is approximated
|
|
|
|
// by a number of discrete segments. The inverse feed_rate should be correct for the sum of
|
|
|
|
// all segments.
|
2010-03-03 01:39:44 +01:00
|
|
|
if (invert_feed_rate) { feed_rate *= segments; }
|
2010-03-03 17:52:56 +01:00
|
|
|
// The angular motion for each segment
|
2010-03-03 01:39:44 +01:00
|
|
|
double theta_per_segment = angular_travel/segments;
|
2010-03-03 17:52:56 +01:00
|
|
|
// The linear motion for each segment
|
2010-03-03 01:39:44 +01:00
|
|
|
double linear_per_segment = linear_travel/segments;
|
2010-03-03 17:52:56 +01:00
|
|
|
// Compute the center of this circle
|
2010-03-07 20:29:18 +01:00
|
|
|
double center_x = (position[axis_1]/settings.steps_per_mm[axis_1])-sin(theta)*radius;
|
|
|
|
double center_y = (position[axis_2]/settings.steps_per_mm[axis_2])-cos(theta)*radius;
|
2010-03-03 17:52:56 +01:00
|
|
|
// a vector to track the end point of each segment
|
2010-03-03 01:39:44 +01:00
|
|
|
double target[3];
|
|
|
|
int i;
|
2010-03-03 17:52:56 +01:00
|
|
|
// Initialize the linear axis
|
|
|
|
target[axis_linear] = position[axis_linear]/Z_STEPS_PER_MM;
|
2010-03-03 01:39:44 +01:00
|
|
|
for (i=0; i<=segments; i++) {
|
|
|
|
target[axis_linear] += linear_per_segment;
|
|
|
|
theta += theta_per_segment;
|
|
|
|
target[axis_1] = center_x+sin(theta)*radius;
|
|
|
|
target[axis_2] = center_y+cos(theta)*radius;
|
2011-01-14 12:10:18 +01:00
|
|
|
mc_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], feed_rate, invert_feed_rate);
|
2010-03-03 01:39:44 +01:00
|
|
|
}
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
void mc_go_home()
|
|
|
|
{
|
|
|
|
st_go_home();
|
2009-02-08 21:22:54 +01:00
|
|
|
clear_vector(position); // By definition this is location [0, 0, 0]
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|