-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuArmFunctions.py
More file actions
269 lines (227 loc) · 7.92 KB
/
uArmFunctions.py
File metadata and controls
269 lines (227 loc) · 7.92 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
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
import serial
import time
def openUArm(uArmPort):
'''Open the uArm serial port
Typically port is /dev/ttyACM0'''
uArm = serial.Serial(uArmPort,115200, timeout=2)
time.sleep(2) #pause for a bit
#listen to the port for the uArm boot-up sequence, and print it
print(wait4Response(uArm,10))
time.sleep(2) #pause for a bit
print(getResponse(uArm))
return uArm
def wait4Response(ser, timeOut):
""" Waits for a 'line' response from the serial port
Returns the string response
If no response after timeOut, returns and empty string
"""
startTime = time.time()
Response = ""
while Response == "" and time.time()- startTime < timeOut:
if ser.inWaiting() > 0:
#time.sleep(1)
Response = ser.readline().decode("utf-8")
return Response
def getResponse(ser):
""" Returns any data from the serial input buffer
No waiting, no timeout
"""
serData=""
while ser.inWaiting()>0:
serData = serData + ser.read(1).decode("utf-8")
return serData
#==============================================================
# NEW FUNCTION - this works pretty well. Shouldn't need fixing
#==============================================================
def sendGCode(uArm, gCode):
'''Sends some g-code to the robot and waits for the robot to respond
To make this really good, it would be good to check if the robot says "OK" or whether there
is an error, and return either "TRUE" or an error number'''
print("Sending: " + gCode)
uArm.write(("\r" + gCode + "\r").encode("utf-8"))
print("Received: ", wait4Response(uArm, 20)) #wait for response
print(getResponse(uArm)) #soak up any extra chars
def goHome(uArm):
'''Puts the uArm into the home position'''
sendGCode(uArm, "G0 X150 Y0 Z0 F20000") #pen up (if not already up)
def drawBoard(uArm):
'''Draws the board'''
sendGCode(uArm, "G0 X150 Y0 Z10")
sendGCode(uArm, "G0 X150 Y25 Z10")
sendGCode(uArm, "G0 X150 Y25 Z0")
sendGCode(uArm, "G0 X225 Y25 Z0")
sendGCode(uArm, "G0 X225 Y25 Z10")
sendGCode(uArm, "G0 X200 Y0 Z10")
sendGCode(uArm, "G0 X200 Y0 Z0")
sendGCode(uArm, "G0 X200 Y75 Z0")
sendGCode(uArm, "G0 X200 Y075 Z10")
sendGCode(uArm, "G0 X225 Y50 Z10")
sendGCode(uArm, "G0 X225 Y50 Z0")
sendGCode(uArm, "G0 X150 Y50 Z0")
sendGCode(uArm, "G0 X150 Y50 Z10")
sendGCode(uArm, "G0 X175 Y75 Z10")
sendGCode(uArm, "G0 X175 Y75 Z0")
sendGCode(uArm, "G0 X175 Y0 Z0")
sendGCode(uArm, "G0 X175 Y0 Z10")
sendGCode(uArm, "G0 X150 Y0 Z10")
sendGCode(uArm, "G0 X150 Y0 Z10")
def drawNought(uArm, whichSquare):
'''Draws a nought at the position on the board specified by "whichSquare")
eg: drawNought(1) draws a nought in square 1
drawNought(2) draws a nought in square 2
squares are as follows:
0|1|2
-----
3|4|5
-----
6|7|8 '''
#work out starting position based on which square:
if whichSquare == 0:
x=150
y=0
elif whichSquare == 1:
x=150
y=25
elif whichSquare == 2:
x=150
y=50
elif whichSquare == 3:
x=175
y=0
elif whichSquare == 4:
x=175
y=25
elif whichSquare == 5:
x=175
y=50
elif whichSquare == 6:
x=200
y=0
elif whichSquare == 7:
x=200
y=25
elif whichSquare == 8:
x=200
y=50
sendGCode(uArm, "G0 Z50") #pen up (if not already up)
sendGCode(uArm, "G0 X" + str(x) + " Y"+ str(y)) #got to the starting location
sendGCode(uArm, "G91") #switch to relative move mode
sendGCode(uArm, "G0 X5 Y10")
sendGCode(uArm, "G90") #switch back to absolute mode
sendGCode(uArm, "G0 Z0") #pen down
sendGCode(uArm, "G91") #switch to relative move mode
#draw the nought
sendGCode(uArm, "G0 Y5")
sendGCode(uArm, "G0 X5 Y5")
sendGCode(uArm, "G0 X5 ")
sendGCode(uArm, "G0 X5 Y-5")
sendGCode(uArm, "G0 Y-5")
sendGCode(uArm, "G0 X-5 Y-5")
sendGCode(uArm, "G0 X-5")
sendGCode(uArm, "G0 X-5 Y5")
sendGCode(uArm, "G90") #switch back to absolute mode
sendGCode(uArm, "G0 Z50") #pen up
def drawCross(uArm, whichSquare):
'''Draws a cross at the position on the board specified by "whichSquare")
eg: drawCross(1) draws a nought in square 1
drawCross(2) draws a nought in square 2
squares are as follows:
0|1|2
-----
3|4|5
-----
6|7|8 '''
#work out starting position based on which square:
#(These are wrong... needs work!)
if whichSquare == 0:
x=150
y=0
elif whichSquare == 1:
x=150
y=25
elif whichSquare == 2:
x=150
y=50
elif whichSquare == 3:
x=175
y=0
elif whichSquare == 4:
x=175
y=25
elif whichSquare == 5:
x=175
y=50
elif whichSquare == 6:
x=200
y=0
elif whichSquare == 7:
x=200
y=25
elif whichSquare == 8:
x=200
y=50
sendGCode(uArm, "G0 Z50") #pen up (if not already up)
sendGCode(uArm, "G0 X" + str(x) + " Y"+ str(y)) #go to the starting location
sendGCode(uArm, "G91") #switch to relative move mode
sendGCode(uArm, "G0 X5 Y5")
sendGCode(uArm, "G90") #switch back to absolute mode
sendGCode(uArm, "G0 Z0") #pen down
sendGCode(uArm, "G91") #switch to relative move mode
#draw the cross
sendGCode(uArm, "G0 X15 Y15")
sendGCode(uArm, "G0 Z20") #pen up
sendGCode(uArm, "G0 X-15")
sendGCode(uArm, "G0 Z-20") #pen down
sendGCode(uArm, "G0 X15 Y-15")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G90") #switch back to absolute mode
sendGCode(uArm, "G0 Z50") #pen up
def drawWinLine(uArm, winLine):
'''Draws a line through the winning positions
Parameters: uArm - the robot arm to do the drawing
winLine - a string representing the positions to draw through
eg: "012" or "248" or "147"
'''
sendGCode(uArm, "G0 Z50") #pen up (if not already up)
if winLine == "012":
#put some code to draw a line through "012"
sendGCode(uArm, "G0 X162.5 Y0")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X162.5 Y75")
elif winLine == "258":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X150 Y62.5")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X225 Y62.5")
elif winLine == "345":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X187.5 Y0")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X187.5 Y75")
elif winLine == "678":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X212.5 Y0")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X212.5 Y75")
elif winLine == "147":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X150 Y37.5")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X225 Y37.5")
elif winLine == "036":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X150 Y12.5")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X225 Y12.5")
elif winLine == "246":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X150 Y75")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X225 Y0")
elif winLine == "048":
#puts some code to draw a line through "345"
sendGCode(uArm, "G0 X150 Y0")
sendGCode(uArm, "G0 Z0")
sendGCode(uArm, "G0 X225 Y75")
#need code for the following winLines: 012, 345, 678, 036, 147, 258, 048, 246
sendGCode(uArm, "G0 Z50") #pen up