-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharc.go
240 lines (215 loc) · 6.38 KB
/
arc.go
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
package gcode
import (
"errors"
"fmt"
"math"
)
func hypot(pos1, pos2 Position) float64 {
return math.Hypot(pos1.X-pos2.X, pos1.Y-pos2.Y)
}
func radiusCenter(curPos, endPos Position, radius float64, clockwise bool) (Position, error) {
if curPos.X == endPos.X && curPos.Y == endPos.Y {
return Position{}, errors.New("expected endpoint different than current with radius")
}
dist := hypot(curPos, endPos)
delta := dist - math.Abs(radius)*2
if delta > minimumDelta {
return Position{}, errors.New("radius too small")
} else if delta > 0.0 {
dist = math.Abs(radius) * 2
}
theta := math.Atan2(endPos.Y-curPos.Y, endPos.X-curPos.X)
if (clockwise && radius > 0.0) || (!clockwise && radius < 0.0) {
theta -= (math.Pi / 2.0)
} else {
theta += (math.Pi / 2.0)
}
offset := math.Abs(radius) * math.Cos(math.Asin(dist/(math.Abs(radius)*2)))
return Position{
X: ((curPos.X + endPos.X) / 2) + offset*math.Cos(theta),
Y: ((curPos.Y + endPos.Y) / 2) + offset*math.Sin(theta),
}, nil
}
// arcTo expects the positions to be mapped to the XYZ plane, with Z being the axis of rotation
// and the arc drawn in the XY plane
func arcTo(curPos, endPos, centerPos Position, radius float64, turns uint, clockwise bool,
linearTo func(pos Position) error) error {
if radius != 0.0 {
if centerPos.X != curPos.X || centerPos.Y != curPos.Y {
return errors.New("both center point and radius specified for arc")
}
var err error
centerPos, err = radiusCenter(curPos, endPos, radius, clockwise)
if err != nil {
return err
}
radius = math.Abs(radius)
} else if centerPos.X != curPos.X || centerPos.Y != curPos.Y {
radius = hypot(curPos, centerPos)
// XXX: warn if hypot(endPos, centerPos) is significantly different than radius
} else {
return errors.New("expected center point or radius for arc")
}
normal := endPos.Z - curPos.Z
if math.Abs(normal) < minimumDelta {
normal = 0.0
if turns > 2 {
turns = 2
}
}
x := curPos.X - centerPos.X
y := curPos.Y - centerPos.Y
angle := math.Atan2(y, x)
if angle < 0.0 {
angle += math.Pi * 2
}
endAngle := math.Atan2(endPos.Y-centerPos.Y, endPos.X-centerPos.X)
if endAngle < 0.0 {
endAngle += math.Pi * 2
}
var angleDir float64
if clockwise {
angleDir = -1.0
} else {
angleDir = 1.0
}
angleTotal := float64(turns-1) * math.Pi * 2
if angle == endAngle {
angleTotal += math.Pi * 2
} else if angle < endAngle {
if clockwise {
angleTotal += math.Pi*2 - (endAngle - angle)
} else {
angleTotal += endAngle - angle
}
} else {
if clockwise {
angleTotal += angle - endAngle
} else {
angleTotal += math.Pi*2 - (angle - endAngle)
}
}
travelTotal := math.Hypot(angleTotal*radius, math.Abs(normal))
numSteps := math.Floor(travelTotal / 0.1)
stepAngle := angleTotal / numSteps
stepNormal := normal / numSteps
for step := float64(1.0); step < numSteps; step += 1.0 {
err := linearTo(
Position{
X: centerPos.X + radius*math.Cos(angle+step*stepAngle*angleDir),
Y: centerPos.Y + radius*math.Sin(angle+step*stepAngle*angleDir),
Z: curPos.Z + step*stepNormal,
})
if err != nil {
return err
}
}
return linearTo(endPos)
}
func (eng *engine) toArcPlane(pos Position) Position {
switch eng.arcPlane {
case XYPlane:
return pos
case ZXPlane:
return Position{X: pos.Z, Y: pos.X, Z: pos.Y}
case YZPlane:
return Position{X: pos.Y, Y: pos.Z, Z: pos.X}
default:
panic(fmt.Sprintf("unexpected arcPlane: %d", eng.arcPlane))
}
}
func (eng *engine) fromArcPlane(pos Position) Position {
switch eng.arcPlane {
case XYPlane:
return pos
case ZXPlane:
return Position{X: pos.Y, Y: pos.Z, Z: pos.X}
case YZPlane:
return Position{X: pos.Z, Y: pos.X, Z: pos.Y}
default:
panic(fmt.Sprintf("unexpected arcPlane: %d", eng.arcPlane))
}
}
func (eng *engine) arcTo(codes []Code, useMachine bool) ([]Code, error) {
if useMachine {
return nil, errors.New("G53 not allowed with arcs")
}
var err error
var args []arg
args, codes, err = parseArgs(codes, fArg|iArg|jArg|kArg|pArg|rArg|xArg|yArg|zArg)
if err != nil {
return nil, err
}
endPos := eng.curPos
centerPos := eng.curPos
var radius float64
turns := uint(1)
for _, arg := range args {
switch arg.letter {
case 'F':
err = eng.setFeed(float64(arg.num) * eng.units)
if err != nil {
return nil, err
}
case 'I':
if eng.arcPlane == YZPlane && !arg.num.Equal(0.0) {
return nil, errors.New("unexpected I for arc in YZ plane")
}
centerPos.X = eng.toMachineX(float64(arg.num)*eng.units, eng.absoluteArcMode)
case 'J':
if eng.arcPlane == ZXPlane && !arg.num.Equal(0.0) {
return nil, errors.New("unexpected J for arc in ZX plane")
}
centerPos.Y = eng.toMachineY(float64(arg.num)*eng.units, eng.absoluteArcMode)
case 'K':
if eng.arcPlane == XYPlane && !arg.num.Equal(0.0) {
return nil, errors.New("unexpected K for arc in XY plane")
}
centerPos.Z = eng.toMachineZ(float64(arg.num)*eng.units, eng.absoluteArcMode)
case 'P':
num, ok := arg.num.AsInteger()
if !ok || num < 1 {
return nil, fmt.Errorf("expected a positive number of turns: P%s", arg.num)
}
turns = uint(num)
case 'R':
radius = float64(arg.num) * eng.units
case 'X':
endPos.X = eng.toMachineX(float64(arg.num)*eng.units, eng.absoluteMode)
case 'Y':
endPos.Y = eng.toMachineY(float64(arg.num)*eng.units, eng.absoluteMode)
case 'Z':
endPos.Z = eng.toMachineZ(float64(arg.num)*eng.units, eng.absoluteMode)
}
}
if radius == 0.0 && eng.absoluteArcMode {
switch eng.arcPlane {
case XYPlane:
if !hasArg(args, 'I') || !hasArg(args, 'J') {
return nil, errors.New("I and J are required for XY plane in absolute arc mode")
}
case ZXPlane:
if !hasArg(args, 'I') || !hasArg(args, 'K') {
return nil, errors.New("I and K are required for ZX plane in absolute arc mode")
}
case YZPlane:
if !hasArg(args, 'J') || !hasArg(args, 'K') {
return nil, errors.New("J and K are required for YZ plane in absolute arc mode")
}
default:
panic(fmt.Sprintf("unexpected arcPlane: %d", eng.arcPlane))
}
}
if eng.moveMode != clockwiseArcMove && eng.moveMode != counterClockwiseArcMove {
panic(fmt.Sprintf("unexpected moveMode: %d", eng.moveMode))
}
err = arcTo(eng.toArcPlane(eng.curPos), eng.toArcPlane(endPos), eng.toArcPlane(centerPos),
radius, turns, eng.moveMode == clockwiseArcMove,
func(pos Position) error {
return eng.linearTo(eng.fromArcPlane(pos))
})
if err != nil {
return nil, err
}
return codes, nil
}