-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsxs.py
executable file
·331 lines (286 loc) · 8.05 KB
/
lsxs.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# lsxs.py
#
# some convenience functions for laser alignment
#
# Copyright 2016 marcell marosvolgyi
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.import serial
import serial
import sys, getopt
import time
import math
class Dummyserial(object):
def __init__(self):
pass
def open(self):
pass
def close(self):
pass
def write(self, dummyarg):
pass
def readlines(self):
return "DCX4.995Y5.005V14.11"
class Post(object):
def __init__(self, function):
self.function = function
def __call__(self, *args, **kwargs):
mylsxs = args[0]
#lines = mylsxs.ser.readlines()
#actual postprocessor here:
#mylsxs.returnval = lines
return self.function(*args, **kwargs)
def __get__(self, instance, owner):
def wrapper(*args, **kwargs):
return self(instance, *args, **kwargs)
wrapper.__doc__ = self.function.__doc__
wrapper.__name__ = self.function.__name__
return wrapper
class Const(object):
MINX = 10
MINY = 10
MAXX = 1100
MAXY = 500
class Lsxs(object):
def __init__(self):
try:
self.ser = serial.Serial(port = "/dev/ttyO1", baudrate=57600)
except:
self.ser = Dummyserial()
self.open()
self.speed_ = 2000
self.power_ = 0.4
self.returnval = None
self.X = 0
self.Y = 0
def open(self):
self.ser.close()
self.ser.open()
def close(self):
self.ser.close()
@property
def power(self):
return self.power_
@power.setter
def power(self, p):
self.power_ = p
#add checks etc..
try:
self.command = "S{0}".format(int(p*255))
except:
print ("Wrong power setting")
@property
def speed(self):
return self.speed_
@speed.setter
def speed(self, spd):
self.speed_ = spd
self.command = "F{0}".format(self.speed)
def reset(self):
self.command = "G30"
def home(self):
self.movetoxy(Const.MINX, Const.MINY)
def movetoxy(self, x, y, laser=0):
if ((x<Const.MINX) or (x>Const.MAXX)): return -1
if ((y<Const.MINY) or (1>Const.MAXY)): return -1
self.command = "G90"
self.command = "G{0} X{1} Y{2}".format(laser, x, y)
def lr(self):
self.movetoxy(Const.MAXX, Const.MAXY)
def ll(self):
self.movetoxy(Const.MINX, Const.MAXY)
def ul(self):
self.movetoxy(Const.MINX, Const.MINY)
def ur(self):
self.movetoxy(Const.MAXX, Const.MINY)
def circle(self, r, x, y, N):
self.movetoxy(x, y)
for i in range(N):
alpha = 1.0*i/N*2.0*3.1415926535
X = r * math.cos(alpha)+x
Y = r * math.sin(alpha)+y
self.movetoxy(X, Y, laser=1)
def roundtrip(self):
self.movetoxy(Const.MINX, Const.MINY)
self.movetoxy(Const.MINX, Const.MAXY)
self.movetoxy(Const.MAXX, Const.MAXY)
self.movetoxy(Const.MAXX, Const.MINY)
self.movetoxy(Const.MINX, Const.MINY)
def laser(self, dx, dy):
#with laser on, move relative dx, dy
self.command = "G91"
self.command = "G1 X0 Y0"
self.command = "G1 X{0} Y{1}".format(dx, dy)
def leftvertical(self, direction=0):
#self.power = 0.4
dx = 0
if direction == 0:
dy = "10"
self.ul()
else:
dy = "-10"
self.ll()
self.laser(dx, dy)
#self.pulse()
self.command = "G90"
if direction == 0:
dy = "-10"
self.ll()
else:
dy = "10"
self.ul()
self.laser(dx, dy)
#self.pulse()
def leftverticalhalfway(self, direction=0):
#very ugly, later fix
#self.power = 0.4
if direction == 0:
dy = "10"
self.ul()
else:
dy = "-10"
self.ll()
self.command = "G91"
self.command = "G1 X0 Y0"
self.command = "G1 X0 Y{0}".format(dy)
#self.pulse()
self.command = "G90"
if direction == 0:
dy = "-10"
#self.ll()
self.movetoxy(Const.MINX, int(Const.MAXY/2))
else:
dy = "10"
self.ul()
self.command = "G91"
self.command = "G1 X0 Y0"
self.command = "G1 X0 Y{0}".format(dy)
#self.pulse()
def lowerhorizontal(self, direction=0):
self.power = 0.4
dy = "0"
if direction == 0:
dx = "30"
self.ll()
else:
dx = "-30"
self.lr()
self.laser(dx, dy)
#self.pulse()
self.command = "G90"
if direction == 0:
dx = "-30"
self.lr()
else:
dx = "30"
self.ll()
self.laser(dx, dy)
#self.pulse()
def rightvertical(self):
self.power = 0.4
self.ur()
self.laser(0, 10)
#self.pulse()
self.command = "G90"
self.lr()
self.laser(0, -10)
#self.pulse()
def lowerhorizontal_(self, direction =0):
self.power = 0.4
self.ll()
self.laser(10,0)
self.command = "G90"
self.lr()
self.laser(-10,0)
self.command = "G90"
def upperhorizontal(self):
self.power = 0.4
self.ul()
self.laser(10,0)
self.command = "G90"
self.ur()
self.laser(-10,0)
self.command = "G90"
def pulse(self):
pass
#NYI
#self.command="G4 P0.5"
@property
def command(self):
return "enter a gcode command"
@command.setter
#@Post
def command(self,str):
self.ser.write(str+"\r\n")
#@Post
def status(self):
self.ser.write("?\r\n")
time.sleep(1)
line = self.ser.read(self.ser.inWaiting())
print line
self.returnval = line
def state(self):
self.status()
def report(self):
s = self.returnval
try:
self.X = s.split("X")[1].split("Y")[0]
self.Y = s.split("Y")[1].split("V")[0]
except:
pass
class Handleargs(object):
#http://www.tutorialspoint.com/python/python_command_line_arguments.htm
def __init__(self, argv):
self.argv = argv
def process(self, lsxs):
try:
opts, args = getopt.getopt(self.argv,"hRlrudc:",["ifile=","ofile="])
except getopt.GetoptError:
print 'lsxs.py -c <gcode command> '
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
f=open('help.txt', 'r')
for line in f.readlines():
print line,
f.close()
sys.exit()
if opt == '-c':
#arg
lsxs.command = arg
print "executed command: {0}".format(arg)
s = raw_input("press enter>")
#sys.exit(1)
if opt == '-R':
print "resetting.."
lsxs.reset()
if opt == '-l':
lsxs.leftvertical()
if opt == '-r':
lsxs.rightvertical()
if opt == '-u':
lsxs.upperhorizontal()
if opt == '-d':
lsxs.lowerhorizontal()
if __name__=='__main__':
instance = Lsxs()
arghandler = Handleargs(sys.argv[1:])
arghandler.process(instance)
#instance.reset()
s = raw_input()
instance.close()