-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_parent.py
309 lines (270 loc) · 10.6 KB
/
pattern_parent.py
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
from typing import List, Mapping
import abc
import math
import numpy as np
from patterns.gcode_cmd import GCodeCmd
from util import subtract
from logger import log
class PatternParent:
def __init__(
self,
values: Mapping,
workHeight: float,
freeMoveHeight: float,
eFactor: float,
eFactorStart: float,
fValue: float,
overrunStart: float,
overrunEnd: float,
printOverrun: float,
startX: float,
startY: float,
rotation: float,
pause: float = 0,
cleaningX: float = None,
cleaningY: float = None,
):
self.values = values
self.workheight = workHeight
self.freemoveHeight = freeMoveHeight
self.eFactor = eFactor
self.fValue = fValue
self.startX = startX
self.startY = startY
self.overrunStart = overrunStart
self.overrunEnd = overrunEnd
self.printOverrun = printOverrun
self.pause = pause
self.cleaningX = cleaningX
self.cleaningY = cleaningY
self.currentX = 0
self.currentY = 0
self.currentE = eFactorStart
rotation = -1 * rotation * math.pi / 180
self.rotationMatrix = np.array(
[
[math.cos(rotation), math.sin(rotation) * -1],
[math.sin(rotation), math.cos(rotation)],
]
)
self.reset()
def getCmdParam(self, label: str, val: float):
return " " + label + str(round(val, 2)) if val is not None else ""
def getResult(self):
res = ""
for cmd in self.commands:
cmdString, e = cmd.toGCode(self.eFactor, self.currentE, self.fValue)
res += cmdString + "\n"
self.currentE = e
return res
def reset(self):
self.result = []
self.commands = []
def addCmd(
self,
prefix: str,
x: float = None,
y: float = None,
z: float = None,
i: float = None,
j: float = None,
p: int = None,
arcDegrees: int = None,
printing: bool = False,
moving: bool = False,
):
# x and y are both needed for rotation (even if one of them does not change)
x = x if not x is None else self.currentX
y = y if not y is None else self.currentY
# Save current location
previousX, previousY = self.rotate(self.currentX, self.currentY)
previousX += self.startX
previousY += self.startY
# Refresh current x and y as if translation and rotation were 0
self.currentX = x
self.currentY = y
# Rotate x and y
xRot, yRot = self.rotate(x, y)
xRot += self.startX
yRot += self.startY
# Rotate i and j (relative offset of the circle center)
if i is not None and j is not None:
iAbs = x + i
jAbs = y + j
iAbsNew, jAbsNew = self.rotate(iAbs, jAbs)
iAbsNew += self.startX
jAbsNew += self.startY
i = iAbsNew - xRot
j = jAbsNew - yRot
# Overrun start
if printing and len(self.commands) != 0 and self.commands[-1].z is not None:
if (
self.commands[-1].z == self.workheight
and self.overrunStart + self.printOverrun != 0
):
startVector = subtract([xRot, yRot], [previousX, previousY])
norm = np.linalg.norm(startVector)
if norm != 0:
vec = startVector / norm
back = vec * (self.overrunStart + self.printOverrun)
forward1 = vec * self.printOverrun
self.commands.insert(
-1,
GCodeCmd(
"G0",
x=previousX - back[0],
y=previousY - back[1],
previousX=previousX,
previousY=previousY,
isOverrun=False,
moving=True,
),
)
self.commands.append(
GCodeCmd(
"G0",
x=previousX - forward1[0],
y=previousY - forward1[1],
previousX=previousX - back[0],
previousY=previousY - back[1],
isOverrun=True,
moving=True,
)
)
self.commands.append(
GCodeCmd(
"G1",
x=previousX,
y=previousY,
previousX=previousX - forward1[0],
previousY=previousY - forward1[1],
isOverrun=True,
moving=True,
printing=True,
)
)
self.commands[-3].x -= back[0]
self.commands[-3].y -= back[1]
del self.commands[-5]
# Overrun end
if z is not None and len(self.commands) != 0:
if z == self.freemoveHeight and self.overrunEnd != 0:
oldCmd = self.commands[-1]
endVector = subtract(
[oldCmd.previousX, oldCmd.previousY], [oldCmd.x, oldCmd.y]
)
norm = np.linalg.norm(endVector)
if norm != 0:
vec = (endVector / norm) * self.overrunEnd
self.commands.append(
GCodeCmd(
"G0",
x=previousX - vec[0],
y=previousY - vec[1],
previousX=previousX,
previousY=previousY,
isOverrun=True,
moving=True,
)
)
xRot -= vec[0]
yRot -= vec[1]
previousX -= vec[0]
previousY -= vec[1]
self.commands.append(
GCodeCmd(
prefix,
x=xRot,
y=yRot,
z=z,
i=i,
j=j,
p=p,
arcDegrees=arcDegrees,
previousX=previousX,
previousY=previousY,
printing=printing,
moving=moving,
)
)
def setCurrentPosition(self, x=None, y=None):
self.currentX = x
self.currentY = y
def moveTo(self, x: float = None, y: float = None, z: float = None):
"""Moves to the given location without releasing material
Args:
x (float, optional): relative difference in x direction. Defaults to None.
y (float, optional): relative difference in y direction. Defaults to None.
z (float, optional): absolute z coordinate. Defaults to None.
"""
self.addCmd("G0", x, y, z, moving=True)
def printTo(self, x: float = None, y: float = None, z: float = None):
"""Moves to the given location while releasing material
Args:
x (float, optional): relative difference in x direction. Defaults to None.
y (float, optional): relative difference in y direction. Defaults to None.
z (float, optional): absolute z coordinate. Defaults to None.
"""
self.addCmd("G1", x, y, z, printing=True, moving=True)
def clockArc(
self,
x: float = None,
y: float = None,
i: float = 0.0,
j: float = 0.0,
arcDegrees=180,
):
"""Prints an arc clockwise. The center point offset as well as the location of the
nozzle after printing the arc need to be defined. Again all coordinates are relative
to the starting point of the arc (current location).
Args:
x (float, optional): Relative x coordinate of the nozzle after drawing the arc. Defaults to None.
y (float, optional): Relative y coordinate of the nozzle after drawing the arc. Defaults to None.
i (float, optional): Relative offset of the center point in x direction. Defaults to 0.0.
j (float, optional): Relative offset of the center point in y direction. Defaults to 0.0.
arcDegrees (int, optional): Number of degrees. Defaults to 180.
"""
self.addCmd(
"G02", x, y, i=i, j=j, arcDegrees=arcDegrees, printing=True, moving=True
)
def counterClockArc(
self,
x: float = None,
y: float = None,
i: float = 0.0,
j: float = 0.0,
arcDegrees=180,
):
"""Prints an arc counter-clockwise. The center point offset as well as the location of the
nozzle after printing the arc need to be defined. Again all coordinate are relative
to the starting point of the arc (current location).
Args:
x (float, optional): Relative x coordinate of the nozzle after drawing the arc. Defaults to None.
y (float, optional): Relative y coordinate of the nozzle after drawing the arc. Defaults to None.
i (float, optional): Relative offset of the center point in x direction. Defaults to 0.0.
j (float, optional): Relative offset of the center point in y direction. Defaults to 0.0.
arcDegrees (int, optional): Number of degrees. Defaults to 180.
"""
self.addCmd(
"G03", x, y, i=i, j=j, arcDegrees=arcDegrees, printing=True, moving=True
)
def workHeight(self):
"""Moves along the z-axis until work height is reached"""
self.moveTo(z=self.workheight)
def freeMoveHeight(self):
"""Moves along the z-axis until work free move height is reached"""
self.moveTo(z=self.freemoveHeight)
def rotate(self, x: float, y: float):
result = self.rotationMatrix.dot(np.array([x, y]))
return result[0], result[1]
def onFinish(self):
if self.pause != 0:
self.addCmd("G4", p=self.pause)
if self.cleaningX is not None and self.cleaningY is not None:
self.commands.append(
GCodeCmd("G0", x=self.cleaningX, y=self.cleaningY, moving=True)
)
@abc.abstractmethod
def gcode(self, startX: float, startY: float, workHeight: float):
"""Generates the gcode of the pattern"""
return