motion_control.cpp

Sat, 07 Nov 2015 13:23:07 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:23:07 +0100
changeset 0
2c8ba1964db7
child 1
b584642d4f58
permissions
-rw-r--r--

Initial code from reprappro Marlin repository

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 motion_control.c - high level interface for issuing motion commands
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 Part of Grbl
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 Copyright (c) 2009-2011 Simen Svale Skogsrud
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 Copyright (c) 2011 Sungeun K. Jeon
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 Grbl is free software: you can redistribute it and/or modify
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 Grbl is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 GNU General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 along with Grbl. If not, see <http://www.gnu.org/licenses/>.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23 #include "stepper.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 #include "planner.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 // The arc is approximated by generating a huge number of tiny, linear segments. The length of each
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 // segment is configured in settings.mm_per_arc_segment.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 void mc_arc(float *position, float *target, float *offset, uint8_t axis_0, uint8_t axis_1,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 uint8_t axis_linear, float feed_rate, float radius, uint8_t isclockwise, uint8_t extruder)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 // int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 // plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 float center_axis0 = position[axis_0] + offset[axis_0];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 float center_axis1 = position[axis_1] + offset[axis_1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 float linear_travel = target[axis_linear] - position[axis_linear];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 float extruder_travel = target[E_AXIS] - position[E_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 float r_axis0 = -offset[axis_0]; // Radius vector from center to current location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 float r_axis1 = -offset[axis_1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 float rt_axis0 = target[axis_0] - center_axis0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 float rt_axis1 = target[axis_1] - center_axis1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 // CCW angle between position and target from circle center. Only one atan2() trig computation required.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 float angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 if (angular_travel < 0) { angular_travel += 2*M_PI; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 if (isclockwise) { angular_travel -= 2*M_PI; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 float millimeters_of_travel = hypot(angular_travel*radius, fabs(linear_travel));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 if (millimeters_of_travel < 0.001) { return; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 uint16_t segments = floor(millimeters_of_travel/MM_PER_ARC_SEGMENT);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 if(segments == 0) segments = 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53 // Multiply inverse feed_rate to compensate for the fact that this movement is approximated
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 // by a number of discrete segments. The inverse feed_rate should be correct for the sum of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 // all segments.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 if (invert_feed_rate) { feed_rate *= segments; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 float theta_per_segment = angular_travel/segments;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 float linear_per_segment = linear_travel/segments;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 float extruder_per_segment = extruder_travel/segments;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 r_T = [cos(phi) -sin(phi);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 sin(phi) cos(phi] * r ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 For arc generation, the center of the circle is the axis of rotation and the radius vector is
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 defined from the circle center to the initial position. Each line segment is formed by successive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 vector rotations. This requires only two cos() and sin() computations to form the rotation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71 all double numbers are single precision on the Arduino. (True double precision will not have
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 round off issues for CNC applications.) Single precision error can accumulate to be greater than
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 tool precision in some cases. Therefore, arc path correction is implemented.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 Small angle approximation may be used to reduce computation overhead further. This approximation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 holds for everything, but very small circles and large mm_per_arc_segment values. In other words,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80 issue for CNC machines with the single precision Arduino calculations.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 This approximation also allows mc_arc to immediately insert a line segment into the planner
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85 This is important when there are successive arc motions.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 // Vector rotation matrix values
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 float cos_T = 1-0.5*theta_per_segment*theta_per_segment; // Small angle approximation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 float sin_T = theta_per_segment;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 float arc_target[4];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 float sin_Ti;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 float cos_Ti;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 float r_axisi;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 uint16_t i;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 int8_t count = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 // Initialize the linear axis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 arc_target[axis_linear] = position[axis_linear];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 // Initialize the extruder axis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102 arc_target[E_AXIS] = position[E_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 for (i = 1; i<segments; i++) { // Increment (segments-1)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 if (count < N_ARC_CORRECTION) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 // Apply vector rotation matrix
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 r_axisi = r_axis0*sin_T + r_axis1*cos_T;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 r_axis0 = r_axis0*cos_T - r_axis1*sin_T;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 r_axis1 = r_axisi;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 count++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 cos_Ti = cos(i*theta_per_segment);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 sin_Ti = sin(i*theta_per_segment);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 r_axis0 = -offset[axis_0]*cos_Ti + offset[axis_1]*sin_Ti;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 r_axis1 = -offset[axis_0]*sin_Ti - offset[axis_1]*cos_Ti;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 count = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 // Update arc_target location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 arc_target[axis_0] = center_axis0 + r_axis0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 arc_target[axis_1] = center_axis1 + r_axis1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 arc_target[axis_linear] += linear_per_segment;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 arc_target[E_AXIS] += extruder_per_segment;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 if (min_software_endstops) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 if (arc_target[X_AXIS] < X_HOME_POS) arc_target[X_AXIS] = X_HOME_POS;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 if (arc_target[Y_AXIS] < Y_HOME_POS) arc_target[Y_AXIS] = Y_HOME_POS;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 if (arc_target[Z_AXIS] < Z_HOME_POS) arc_target[Z_AXIS] = Z_HOME_POS;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 if (max_software_endstops) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 if (arc_target[X_AXIS] > max_length[X_AXIS]) arc_target[X_AXIS] = max_length[X_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 if (arc_target[Y_AXIS] > max_length[Y_AXIS]) arc_target[Y_AXIS] = max_length[Y_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 if (arc_target[Z_AXIS] > max_length[Z_AXIS]) arc_target[Z_AXIS] = max_length[Z_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 plan_buffer_line(arc_target[X_AXIS], arc_target[Y_AXIS], arc_target[Z_AXIS], arc_target[E_AXIS], feed_rate, extruder);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 // Ensure last segment arrives at target location.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feed_rate, extruder);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 // plan_set_acceleration_manager_enabled(acceleration_manager_was_enabled);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147

mercurial