spindel.py
5.23 KB
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
#!/usr/bin/python
'''
Generate Spindle Gcode
Copyright (C) 2014 Hubert Berezowski
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
from optparse import OptionParser
def main(argv):
parser = OptionParser(usage="usage: %prog -f fraeser -r radius [options]",
version="%prog 0.1")
parser.add_option("-f", "--fraseserdurchmesser"
, dest="fraeser"
, type="float"
, default=False
, help="Durchmesser in mm des gewaehlten Fraesers"
)
parser.add_option("-r", "--fraseradius"
, dest="radius"
, type="float"
, default=False
, help="Radius in mm des zu fraesenden Kreises, beruecksichtigt den Fraeserradius"
)
parser.add_option("-v", "--versatz"
, dest="versatz"
, type="float"
, help="Versatz in mm, den Der Fraeser bei jedem durchlauf in das Material eintaucht"
)
parser.add_option("-x", "--offset_x"
, dest="x"
, type="float"
, default=0,
help="offset "
)
parser.add_option("-y", "--offset_y"
, dest="y"
, type="float"
, default=0
, help="offset"
)
parser.add_option("-s", "--startradius"
, dest="startradius"
, type="float"
, default=0
, help="Start at radius <>"
)
parser.add_option("-d", "--direction"
, dest="direction"
, type="str"
, default='CCW'
, help="Rotate direction, 'CW', default: 'CCW'"
)
(options, args) = parser.parse_args()
if not options.fraeser:
parser.error('Fraeserdurchmesser nicht angegeben')
if not options.radius:
parser.error('Radius nicht angegeben')
spindel = Spindel(fraeser = options.fraeser
, radius = options.radius
, versatz = options.versatz if options.versatz else options.fraeser/2
, x = options.x
, y = options.y
, startradius = options.startradius
, direction = options.direction
)
spindel.head(spindel.direction)
spindel.makeSpindel(spindel.position)
class Spindel:
"""Super Duper Spindel Gcode Generator"""
### SETUP
def __init__(self, fraeser=0, radius=0, versatz=None, x=0, y=0, startradius=0, direction='CCW' ):
self.tool = {}
self.target = {}
self.setTool(fraeser, versatz)
self.setTarget(radius,x,y)
self.position = {
'radius' : startradius,
'x' : startradius,
'y' : 0
}
self.direction = direction
self.validateInput()
def validateInput(self):
if self.tool['versatz'] > self.tool['fraeser']/2:
sys.stderr.write('''
Achtung !\n
Es wird mit mehr als dem Fraeserradius gefraest\n
Fraeserradius: %s\nVersatz: %s
''' %(self.tool['fraeser'], self.tool['versatz']))
if self.tool['versatz'] > self.tool['fraeser']:
raise Exception('''
Der Versatz ist groesser als der Fraeserdurchmesser\n
Fraeserradius: %s\nVersatz: %s
''' %(self.tool['fraeser'], self.tool['versatz']))
def setTool(self, fraeser, versatz=None):
self.tool = {
'fraeser' : float(fraeser),
'versatz' : float(versatz) if versatz else fraeser/2.
}
def setTarget(self, radius, x, y):
self.target = {
'radius' : float(radius - self.tool['fraeser']/2),
'x' : float(x),
'y' : float(y)
}
def printPosition(self, position):
print 'Y%s X%s R%s' %(
position['y'] + self.target['y'],
position['x'] + self.target['x'],
position['radius']
)
return position
### GENERATE
def head(self, direction):
print 'G21' #MM mode (G71 on older controls)
print 'G0 Y%s X%s' %(
self.position['y'] + self.target['y'],
self.position['x'] + self.target['x']
)
if direction == 'CW':
print 'G02' #Circular move CW
else:
print 'G03 F100' #Circular move CCW
def increaseArc(self, position, versatz):
diameter = (position['x']*2) + (0 if position['x'] > 0 else -versatz)
position['x'] -= diameter
position['radius'] = abs(diameter/2)
self.printPosition(position)
return position
def makeSpindel(self, position):
if abs(position['radius']) <= self.target['radius'] - self.tool['versatz']:
self.makeSpindel(
self.increaseArc(position, self.tool['versatz'])
)
else:
self.closeSpindel(position)
def closeSpindel(self, position):
#print(self.target['radius'] - position['radius']);
# print('// closing radius: %s') %(self.target['radius'] - position['radius'])
self.increaseArc(position, self.target['radius'] - position['radius'])
# make a nice border
self.increaseArc(position, self.target['radius'] - position['radius'])
self.increaseArc(position, self.target['radius'] - position['radius'])
print 'M00' #Program stop
print 'M01' #Optional program stop
print 'M02' #End of program (no rewind or return to start of program)
#
# recenter the tool
# print 'Y%s X%s' %(
# self.target['x'],
# self.target['y'],
# )
if __name__ == "__main__":
main(sys.argv[1:])