1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/********************************************************************
* Description: interp_arc.cc
*
* Derived from a work by Thomas Kramer
*
* Author:
* License: GPL Version 2
* System: Linux
*
* Copyright (c) 2004 All rights reserved.
*
* Last change:
********************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libintl.h>
#include "rs274ngc.hh"
#include "rs274ngc_return.hh"
#include "rs274ngc_interp.hh"
#include "interp_internal.hh"
#define _(s) gettext(s)
char Interp::arc_axis1(int plane) {
switch(plane) {
case CANON_PLANE_XY: return 'X';
case CANON_PLANE_XZ: return 'Z';
case CANON_PLANE_YZ: return 'Y';
default: return '!';
}
}
char Interp::arc_axis2(int plane) {
switch(plane) {
case CANON_PLANE_XY: return 'Y';
case CANON_PLANE_XZ: return 'X';
case CANON_PLANE_YZ: return 'Z';
default: return '!';
}
}
/***********************************************************************/
/*! arc_data_comp_ijk
Returned Value: int
If any of the following errors occur, this returns the error code shown.
Otherwise, it returns INTERP_OK.
1. The two calculable values of the radius differ by more than
tolerance: NCE_RADIUS_TO_END_OF_ARC_DIFFERS_FROM_RADIUS_TO_START
2. move is not G_2 or G_3: NCE_BUG_CODE_NOT_G2_OR_G3
Side effects:
This finds and sets the values of center_x, center_y, and turn.
Called by: convert_arc_comp1
This finds the center coordinates and number of full or partial turns
counterclockwise of a helical or circular arc in ijk-format in the XY
plane. The center is computed easily from the current point and center
offsets, which are given. It is checked that the end point lies one
tool radius from the arc.
*/
int Interp::arc_data_comp_ijk(int move, //!<either G_2 (cw arc) or G_3 (ccw arc)
int plane, //!<active plane
int side, //!<either RIGHT or LEFT
double tool_radius, //!<radius of the tool
double current_x, //!<first coordinate of current point
double current_y, //!<second coordinate of current point
double end_x, //!<first coordinate of arc end point
double end_y, //!<second coordinate of arc end point
int ij_absolute, //!<how to interpret i/j numbers
double i_number, //!<first coordinate of center (abs or incr)
double j_number, //!<second coordinate of center (abs or incr)
int p_number,
double *center_x, //!<pointer to first coordinate of center of arc
double *center_y, //!<pointer to second coordinate of center of arc
int *turn, //!<pointer to number of full or partial circles CCW
double tolerance) //!<tolerance of differing radii
{
double arc_radius;
double radius2;
double min_radius = 0.1 * tolerance; /* smaller than this is treated as zero */
char a = arc_axis1(plane), b = arc_axis2(plane);
if ( ij_absolute ) {
*center_x = (i_number);
*center_y = (j_number);
} else {
*center_x = (current_x + i_number);
*center_y = (current_y + j_number);
}
arc_radius = hypot((*center_x - current_x), (*center_y - current_y));
radius2 = hypot((*center_x - end_x), (*center_y - end_y));
CHKS(((arc_radius < min_radius) || (radius2 < min_radius)), _("Zero radius arc"));
double abs_err = fabs(arc_radius - radius2);
double rel_err = abs_err / std::max(arc_radius, radius2);
CHKS(abs_err > 100*tolerance || (abs_err > tolerance && (rel_err > .001)),
_("Radius to end of arc differs from radius to start: "
"start=(%c%.4f,%c%.4f) center=(%c%.4f,%c%.4f) end=(%c%.4f,%c%.4f) r1=%.4f r2=%.4f abs_err=%.4g rel_err=%.4f%%"),
a, current_x, b, current_y,
a, *center_x, b, *center_y,
a, end_x, b, end_y, arc_radius, radius2,
abs_err, rel_err*100);
CHKS(((arc_radius <= tool_radius) && (((side == LEFT) && (move == G_3)) ||
((side == RIGHT) && (move == G_2)))),
NCE_TOOL_RADIUS_NOT_LESS_THAN_ARC_RADIUS_WITH_COMP);
/* This catches an arc too small for the tool, also */
if (move == G_2)
*turn = -1 * p_number;
else if (move == G_3)
*turn = 1 * p_number;
else
ERS(NCE_BUG_CODE_NOT_G2_OR_G3);
return INTERP_OK;
}
/****************************************************************************/
/*! arc_data_comp_r
Returned Value: int
If any of the following errors occur, this returns the error code shown.
Otherwise, it returns INTERP_OK.
1. The arc radius is too small to reach the end point:
NCE_RADIUS_TOO_SMALL_TO_REACH_END_POINT
2. The arc radius is not greater than the tool_radius, but should be:
NCE_TOOL_RADIUS_NOT_LESS_THAN_ARC_RADIUS_WITH_COMP
3. An imaginary value for offset would be found, which should never
happen if the theory is correct: NCE_BUG_IN_TOOL_RADIUS_COMP
Side effects:
This finds and sets the values of center_x, center_y, and turn.
Called by: convert_arc_comp1
This finds the center coordinates and number of full or partial turns
counterclockwise of a helical or circular arc (call it arc1) in
r-format in the XY plane. Arc2 is constructed so that it is tangent
to a circle whose radius is tool_radius and whose center is at the
point (current_x, current_y) and passes through the point (end_x,
end_y). Arc1 has the same center as arc2. The radius of arc1 is one
tool radius larger or smaller than the radius of arc2.
If the value of the big_radius argument is negative, that means [NCMS,
page 21] that an arc larger than a semicircle is to be made.
Otherwise, an arc of a semicircle or less is made.
The algorithm implemented here is to construct a line L from the
current point to the end point, and a perpendicular to it from the
center of the arc which intersects L at point P. Since the distance
from the end point to the center and the distance from the current
point to the center are known, two equations for the length of the
perpendicular can be written. The right sides of the equations can be
set equal to one another and the resulting equation solved for the
length of the line from the current point to P. Then the location of
P, the length of the perpendicular, the angle of the perpendicular,
and the location of the center, can be found in turn.
This needs to be better documented, with figures. There are eight
possible arcs, since there are three binary possibilities: (1) tool
inside or outside arc, (2) clockwise or counterclockwise (3) two
positions for each arc (of the given radius) tangent to the tool
outline and through the end point. All eight are calculated below,
since theta, radius2, and turn may each have two values.
To see two positions for each arc, imagine the arc is a hoop, the
tool is a cylindrical pin, and the arc may rotate around the end point.
The rotation covers all possible positions of the arc. It is easy to
see the hoop is constrained by the pin at two different angles, whether
the pin is inside or outside the hoop.
*/
int Interp::arc_data_comp_r(int move, //!< either G_2 (cw arc) or G_3 (ccw arc)
int plane,
int side, //!< either RIGHT or LEFT
double tool_radius, //!< radius of the tool
double current_x, //!< first coordinate of current point
double current_y, //!< second coordinate of current point
double end_x, //!< first coordinate of arc end point
double end_y, //!< second coordinate of arc end point
double big_radius, //!< radius of arc
int p_number,
double *center_x, //!< pointer to first coordinate of center of arc
double *center_y, //!< pointer to second coordinate of center of arc
int *turn, //!< pointer to number of full or partial circles CCW
double tolerance) //!< tolerance of differing radii
{
double abs_radius; // absolute value of big_radius
abs_radius = fabs(big_radius);
CHKS(((abs_radius <= tool_radius) && (((side == LEFT) && (move == G_3)) ||
((side == RIGHT) && (move == G_2)))),
NCE_TOOL_RADIUS_NOT_LESS_THAN_ARC_RADIUS_WITH_COMP);
return arc_data_r(move, plane, current_x, current_y, end_x, end_y, big_radius, p_number,
center_x, center_y, turn, tolerance);
}
/****************************************************************************/
/*! arc_data_ijk
Returned Value: int
If any of the following errors occur, this returns the error code shown.
Otherwise, it returns INTERP_OK.
1. The two calculable values of the radius differ by more than
tolerance: NCE_RADIUS_TO_END_OF_ARC_DIFFERS_FROM_RADIUS_TO_START
2. The move code is not G_2 or G_3: NCE_BUG_CODE_NOT_G2_OR_G3
3. Either of the two calculable values of the radius is zero:
NCE_ZERO_RADIUS_ARC
Side effects:
This finds and sets the values of center_x, center_y, and turn.
Called by:
convert_arc2
convert_arc_comp2
This finds the center coordinates and number of full or partial turns
counterclockwise of a helical or circular arc in ijk-format. This
function is used by convert_arc2 for all three planes, so "x" and
"y" really mean "first_coordinate" and "second_coordinate" wherever
they are used here as suffixes of variable names. The i and j prefixes
are handled similarly.
*/
int Interp::arc_data_ijk(int move, //!< either G_2 (cw arc) or G_3 (ccw arc)
int plane,
double current_x, //!< first coordinate of current point
double current_y, //!< second coordinate of current point
double end_x, //!< first coordinate of arc end point
double end_y, //!< second coordinate of arc end point
int ij_absolute, //!<how to interpret i/j numbers
double i_number, //!<first coordinate of center (abs or incr)
double j_number, //!<second coordinate of center (abs or incr)
int p_number,
double *center_x, //!< pointer to first coordinate of center of arc
double *center_y, //!< pointer to second coordinate of center of arc
int *turn, //!< pointer to no. of full or partial circles CCW
double tolerance) //!< tolerance of differing radii
{
double radius; /* radius to current point */
double radius2; /* radius to end point */
double min_radius = 0.1 * tolerance; /* smaller than this is treated as zero */
char a = arc_axis1(plane), b = arc_axis2(plane);
if ( ij_absolute ) {
*center_x = (i_number);
*center_y = (j_number);
} else {
*center_x = (current_x + i_number);
*center_y = (current_y + j_number);
}
radius = hypot((*center_x - current_x), (*center_y - current_y));
radius2 = hypot((*center_x - end_x), (*center_y - end_y));
CHKS(((radius < min_radius) || (radius2 < min_radius)), NCE_ZERO_RADIUS_ARC);
double abs_err = fabs(radius - radius2);
double rel_err = abs_err / std::max(radius, radius2);
CHKS(abs_err > 100*tolerance || (abs_err > tolerance && (rel_err > .001)),
_("Radius to end of arc differs from radius to start: "
"start=(%c%.4f,%c%.4f) center=(%c%.4f,%c%.4f) end=(%c%.4f,%c%.4f) r1=%.4f r2=%.4f abs_err=%.4g rel_err=%.4f%%"),
a, current_x, b, current_y,
a, *center_x, b, *center_y,
a, end_x, b, end_y, radius, radius2,
abs_err, rel_err*100);
if (move == G_2)
*turn = -1 * p_number;
else if (move == G_3)
*turn = 1 * p_number;
else
ERS(NCE_BUG_CODE_NOT_G2_OR_G3);
return INTERP_OK;
}
/****************************************************************************/
/*! arc_data_r
Returned Value: int
If any of the following errors occur, this returns the error shown.
Otherwise, it returns INTERP_OK.
1. The radius is too small to reach the end point:
NCE_ARC_RADIUS_TOO_SMALL_TO_REACH_END_POINT
2. The current point is the same as the end point of the arc
(so that it is not possible to locate the center of the circle):
NCE_CURRENT_POINT_SAME_AS_END_POINT_OF_ARC
Side effects:
This finds and sets the values of center_x, center_y, and turn.
Called by:
convert_arc2
convert_arc_comp2
This finds the center coordinates and number of full or partial turns
counterclockwise of a helical or circular arc in the r format. This
function is used by convert_arc2 for all three planes, so "x" and
"y" really mean "first_coordinate" and "second_coordinate" wherever
they are used here as suffixes of variable names.
If the value of the radius argument is negative, that means [NCMS,
page 21] that an arc larger than a semicircle is to be made.
Otherwise, an arc of a semicircle or less is made.
The algorithm used here is based on finding the midpoint M of the line
L between the current point and the end point of the arc. The center
of the arc lies on a line through M perpendicular to L.
*/
int Interp::arc_data_r(int move, //!< either G_2 (cw arc) or G_3 (ccw arc)
int plane,
double current_x, //!< first coordinate of current point
double current_y, //!< second coordinate of current point
double end_x, //!< first coordinate of arc end point
double end_y, //!< second coordinate of arc end point
double radius, //!< radius of arc
int p_number,
double *center_x, //!< pointer to first coordinate of center of arc
double *center_y, //!< pointer to second coordinate of center of arc
int *turn, //!< pointer to number of full or partial circles CCW
double tolerance) //!< tolerance of differing radii
{
double abs_radius; /* absolute value of given radius */
double half_length; /* distance from M to end point */
double mid_x; /* first coordinate of M */
double mid_y; /* second coordinate of M */
double offset; /* distance from M to center */
double theta; /* angle of line from M to center */
double turn2; /* absolute value of half of turn */
CHKS(((end_x == current_x) && (end_y == current_y)),
NCE_CURRENT_POINT_SAME_AS_END_POINT_OF_ARC);
abs_radius = fabs(radius);
mid_x = (end_x + current_x) / 2.0;
mid_y = (end_y + current_y) / 2.0;
half_length = hypot((mid_x - end_x), (mid_y - end_y));
CHKS(((half_length - abs_radius) > tolerance),
NCE_ARC_RADIUS_TOO_SMALL_TO_REACH_END_POINT);
if ((half_length / abs_radius) > (1 - TINY))
half_length = abs_radius; /* allow a small error for semicircle */
/* check needed before calling asin */
if (((move == G_2) && (radius > 0)) || ((move == G_3) && (radius < 0)))
theta = atan2((end_y - current_y), (end_x - current_x)) - M_PI_2l;
else
theta = atan2((end_y - current_y), (end_x - current_x)) + M_PI_2l;
turn2 = asin(half_length / abs_radius);
offset = abs_radius * cos(turn2);
*center_x = mid_x + (offset * cos(theta));
*center_y = mid_y + (offset * sin(theta));
*turn = (move == G_2) ? -1 * p_number : 1 * p_number;
return INTERP_OK;
}
|