-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxiDrawBehaviorClass.py
324 lines (309 loc) · 12.3 KB
/
AxiDrawBehaviorClass.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
import sys
from pyaxidraw import axidraw
import time
import numpy as np
class AxiDrawBehavior:
def __int__(self):
self.ad = axidraw.AxiDraw() # AxiDraw Initialize class
self.DXL_IDS = [1, 2] # define motors ID
self.M1 = axidraw.Motor(self.DXL_IDS) # Initialize motors
def homing(self):
self.ad.interactive()
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
self.ad.penup()
self.ad.moveto(0, 0) # Pen-up return home
self.M1.Homeing()
self.ad.disconnect() # Close serial port to AxiDraw
def draw_image(self, svg_image, mode='easy'):
self.homing()
try:
self.M1.Enable_Torque()
self.ad.interactive() # Enter interactive mode
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
self.ad.plot_setup(svg_image)
self.ad.options.pen_pos_up = 72 # set pen-up position
self.ad.options.pen_pos_down = 12
if mode == 'easy':
self.ad.options.speed_pendown = 8
self.ad.options.speed_penup = 80
elif mode == 'medium':
self.ad.options.speed_pendown = 8
self.ad.options.speed_penup = 50
elif mode == 'hard':
self.ad.options.speed_pendown = 6
self.ad.options.speed_penup = 25
self.ad.plot_run()
self.ad.disconnect() # Close serial port to AxiDraw
except:
print("Cant plot the image")
def SpendTime(self):
self.ad.interactive() # Enter interactive mode
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
self.ad.moveto(3, 0) # robot go to middle
t_start = time.time()
t_end = 0
temp1 = list(range(90, 270, 2))
temp2 = temp1[::-1]
pose = temp1 + [270] + temp2
self.M1.Enable_Torque()
self.M1.Write_Pos(180, 1)
self.M1.Write_Pos(90, 2)
while (t_end - t_start) <= 10:
for i in pose:
self.M1.Write_Pos(round(i), 2)
# time.sleep(0.25)
if not self.M1.inRange(self.M1.Read_Pos(2), 2):
raise Exception("Out of range, Homing failed, Try moving the arm and homing again")
t_end = time.time()
self.M1.Write_Pos(180, 2)
self.ad.moveto(0, 0) # robot return home
self.ad.disconnect() # Close serial port to AxiDraw
def interference1(self):
self.M1.Homeing()
self.M1.Pointing()
self.ad.interactive() # Enter interactive mode
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
flag = 0
while flag < 5:
self.ad.penup()
self.ad.pendown()
flag = flag + 1
self.M1.Homeing()
self.ad.disconnect() # Close serial port to AxiDraw
def interference2(self):
self.M1.Homeing()
self.M1.Write_Pos(298, 1)
if not self.M1.inRange(self.M1.Read_Pos(1), 1):
raise Exception("Out of range, pointing failed, Try moving the arm and try again")
flag = 0
# temp1 = list(range(156, 188, 2))
# temp2 = temp1[::-1]
# pose = temp1 + [188] + temp2
self.M1.Write_Pos(140, 2)
while flag < 10:
# for i in pose:
# M1.Write_Pos(round(i), 2)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
self.M1.Write_Pos(188, 2)
time.sleep(0.5)
self.M1.Write_Pos(140, 2)
time.sleep(0.5)
flag = flag + 1
self.homing()
def YourTurn(self):
self.ad.interactive()
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
self.ad.penup()
self.ad.moveto(3, 0) # Pen-up return home
self.M1.Pointing()
time.sleep(2)
self.M1.Homeing()
# ad.interactive() # Enter interactive mode
flag = 0
while flag < 5:
self.ad.penup()
time.sleep(0.5)
self.ad.pendown()
flag = flag + 1
self.M1.Homeing()
self.M1.Pointing()
time.sleep(2)
self.ad.disconnect() # Close serial port to AxiDraw
self.homing()
def Observe(self):
self.M1.Homeing()
try:
self.ad.interactive() # Enter interactive mode
connected = self.ad.connect() # Open serial port to AxiDraw
if not connected:
sys.exit() # end script
self.ad.moveto(0, 1) # Absolute pen-up move, to (0 inch, 1 inch)
t_start = time.time()
t_end = 0
temp1 = list(range(90, 270, 2))
temp2 = temp1[::-1]
pose = temp1 + [270] + temp2
self.M1.Enable_Torque()
self.M1.Write_Pos(180, 1)
self.M1.Write_Pos(180, 2)
while (t_end - t_start) <= 3:
for i in pose:
self.M1.Write_Pos(round(i), 1)
# time.sleep(0.25)
if not self.M1.inRange(self.M1.Read_Pos(2), 2):
raise Exception("Out of range, Homing failed, Try moving the arm and homing again")
t_end = time.time()
self.M1.Write_Pos(180, 1)
self.ad.moveto(4, 3) # Absolute pen-up move, to (0 inch, 1 inch)
t_start = time.time()
t_end = 0
temp1 = list(range(90, 270, 2))
temp2 = temp1[::-1]
pose = temp1 + [270] + temp2
self.M1.Enable_Torque()
self.M1.Write_Pos(180, 1)
self.M1.Write_Pos(180, 2)
while (t_end - t_start) <= 3:
for i in pose:
self.M1.Write_Pos(round(i), 1)
# time.sleep(0.25)
if not self.M1.inRange(self.M1.Read_Pos(2), 2):
raise Exception("Out of range, Homing failed, Try moving the arm and homing again")
t_end = time.time()
self.M1.Write_Pos(180, 1)
self.ad.moveto(0, 0)
self.ad.disconnect() # Close serial port to AxiDraw
except:
print("action stopped")
#
# def bowing():
# try:
# ad.interactive() # Enter interactive mode
# connected = ad.connect() # Open serial port to AxiDraw
# if not connected:
# sys.exit() # end script
# ad.moveto(0, 0) # Absolute pen-up move, to (0 inch, 1 inch)
# M1.Write_Pos(90, 2)
# M1.Write_Pos(50, 1)
# ad.disconnect() # Close serial port to AxiDraw
# except:
# print("action stopped")
#
#
# def signature():
# signature = 'signature.svg'
# draw_image(signature)
#
#
# def waving():
# M1.Homeing()
# try:
# ad.interactive() # Enter interactive mode
# connected = ad.connect() # Open serial port to AxiDraw
# if not connected:
# sys.exit() # end script
# t_start = time.time()
# t_end = 0
# temp1 = list(range(90, 270, 2))
# temp2 = temp1[::-1]
# pose = temp1 + [270] + temp2
# M1.Enable_Torque()
# M1.Write_Pos(180, 1)
# M1.Write_Pos(90, 2)
# flag = True
# while (t_end - t_start) <= 30:
# if flag:
# ad.moveto(4, 3) # Absolute pen-up move, to (0 inch, 1 inch)
# flag = False
# else:
# ad.moveto(0, 3) # Absolute pen-up move, to (0 inch, 1 inch)
# flag = True
# for i in pose:
# M1.Write_Pos(round(i), 2)
# # time.sleep(0.25)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, Homing failed, Try moving the arm and homing again")
# t_end = time.time()
# M1.Write_Pos(180, 2)
# ad.disconnect() # Close serial port to AxiDraw
# homing()
# except:
# print("action stopped")
#
#
# def point_on(pose):
# M1.Enable_Torque()
# M1.Write_Pos(pose[0], 1)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# M1.Write_Pos(pose[1], 2)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
#
#
# def point_on_paper():
# ad.interactive() # Enter interactive mode
# connected = ad.connect() # Open serial port to AxiDraw
# if not connected:
# sys.exit() # end script
# ad.moveto(6, 0) # Absolute pen-up move, to (0 inch, 1 inch)
# M1.Enable_Torque()
# M1.Write_Pos(218, 1)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# M1.Write_Pos(228, 2)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# flag = 0
# while flag < 5:
# ad.penup()
# time.sleep(0.5)
# ad.pendown()
# flag = flag + 1
# ad.disconnect() # Close serial port to AxiDraw
# homing()
#
#
# def point_on_postcards():
# ad.interactive() # Enter interactive mode
# connected = ad.connect() # Open serial port to AxiDraw
# if not connected:
# sys.exit() # end script
# M1.Enable_Torque()
# M1.Write_Pos(148, 1)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# M1.Write_Pos(230, 2)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# ad.moveto(6, 4) # Absolute pen-up move, to (0 inch, 1 inch)
# flag = 0
# while flag < 5:
# ad.penup()
# time.sleep(0.5)
# ad.pendown()
# flag = flag + 1
# ad.moveto(0, 0) # Absolute pen-up move, to (0 inch, 1 inch)
# ad.disconnect() # Close serial port to AxiDraw
# homing()
#
#
# def point_on_sit():
# ad.interactive() # Enter interactive mode
# connected = ad.connect() # Open serial port to AxiDraw
# if not connected:
# sys.exit() # end script
# M1.Enable_Torque()
# ad.moveto(0, 4) # Absolute pen-up move, to (0 inch, 1 inch)
# M1.Pointing()
# time.sleep(1)
# M1.Write_Pos(270, 1)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# M1.Write_Pos(115, 2)
# if not M1.inRange(M1.Read_Pos(2), 2):
# raise Exception("Out of range, pointing failed, Try moving the arm and try again")
# flag = 0
# while flag < 5:
# ad.penup()
# time.sleep(0.5)
# ad.pendown()
# flag = flag + 1
# ad.disconnect() # Close serial port to AxiDraw
# homing()
if __name__ == '__main__':
a = AxiDrawBehavior()
a.homing()
a.M1.Disable_Torque()
a.ad.disconnect() # Close serial port to AxiDraw