-
Notifications
You must be signed in to change notification settings - Fork 14
/
arm.py
397 lines (312 loc) · 14.8 KB
/
arm.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import sys
import os
import numpy as np
import time
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from xarm.wrapper import XArmAPI
# * pickup_piece( str -> name of the square) -> pick up a piece from the board
# * place_piece( str -> name of the square) -> place a piece on the board
# * move_piece( str -> name of the square, str -> name of the square) -> move a piece from one square to another
# * constants: size of the board, size of the squares, size of highest piece
# * (0, [87.0, -0.0, 154.199997, 180.00002, 0.0, -0.0])
# * tuple when the arm is just touching the board [x, y, z, roll, pitch, yaw]
# * (0, [163.208542, 0.025312, 135.205475, -179.992629, -3.380279, 0.091444])
# * positive z values make the arm go up
class Arm_constants:
# units in mm:
SIZE_SQUARE : float = 27.49
SIZE_BOARD : float = 250
POS_Z_HIGHEST_PIECE : float = 53.71
POS_Z_BOARD : float = 0
OFFSET_X1 : float = 0
OFFSET_Y1 : float = 0
OFFSET_X2 : float = 0
OFFSET_Y2 : float = 0
SQUARE_LOCATIONS = np.empty((8, 8), dtype=tuple)
RESERVE_LOCATIONS = np.empty((4,1), dtype=tuple)
ROBOT_COLOR: bool = False # true if robot playing as white
ARM: XArmAPI
SQUARES_IN_ROW: int = 8
# POS_Z_BASE_BOARD : int = 135
def instantiateArm():
Arm_constants.ARM = XArmAPI('192.168.1.166')
Arm_constants.ARM.motion_enable(enable=True)
Arm_constants.ARM.set_mode(0)
Arm_constants.ARM.connect()
# Arm_constants.ARM.clean_error()
# Arm_constants.ARM.clean_warn()
def deinstantiateArm():
Arm_constants.ARM.motion_enable(enable=False)
Arm_constants.ARM.disconnect()
def calibrate(unsafe = False, robot_known_white = False):
# code to calibrate the arm
# enter a known position for the arm to move relative to the board
# arm.set_position(0, 0, 0, wait=True)
print("CALIBRATION SEQUENCE STARTING...\n")
Arm_constants.ARM.close_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
if not robot_known_white:
color = input("IS THE ROBOT PLAYING AS WHITE? ENTER Y OR N\n")
while 'Y' not in color and 'N' not in color:
print("PLEASE ENTER EITHER Y OR N")
color = input("IS THE ROBOT PLAYING AS WHITE? ENTER Y OR N\n")
Arm_constants.ROBOT_COLOR = 'Y' in color
else:
Arm_constants.ROBOT_COLOR = 'W' in robot_known_white
load_cali = input("WOULD YOU LIKE TO LOAD CALIBRATION SAVE DATA?\n")
while 'Y' not in load_cali and 'N' not in load_cali:
print("PLEASE ENTER EITHER Y OR N")
load_cali = input("IS THE ROBOT PLAYING AS WHITE? ENTER Y OR N\n")
if 'Y' in load_cali:
try:
loadCalibration()
except:
print("LOAD PROCESS FAILED. BEGINNING RECALIBRATION SEQUENCE")
reCalibrate()
else:
reCalibrate()
print("CHECKING MOVEMENT FREEDOM...")
directedMode()
Arm_constants.ARM.open_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
if not unsafe:
# Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][0][0], Arm_constants.SQUARE_LOCATIONS[0][0][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
# Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[7][0][0], Arm_constants.SQUARE_LOCATIONS[7][0][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
# Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[7][7][0], Arm_constants.SQUARE_LOCATIONS[7][7][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
# Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][7][0], Arm_constants.SQUARE_LOCATIONS[0][7][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
# Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][0][0], Arm_constants.SQUARE_LOCATIONS[0][0][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][7][0], Arm_constants.SQUARE_LOCATIONS[0][7][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][0][0], Arm_constants.SQUARE_LOCATIONS[0][0][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[7][0][0], Arm_constants.SQUARE_LOCATIONS[7][0][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[7][7][0], Arm_constants.SQUARE_LOCATIONS[7][7][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[0][7][0], Arm_constants.SQUARE_LOCATIONS[0][7][1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
# code that outline board here
print("CALIBRATION COMPLETE!")
save = input("WOULD YOU LIKE TO SAVE CALIBRATION DATA? (Y/N)")
while 'Y' not in save and 'N' not in save:
print("PLEASE ENTER EITHER Y OR N")
save = input("IS THE ROBOT PLAYING AS WHITE? ENTER Y OR N\n")
if 'Y' in save:
saveCalibration()
def saveCalibration():
# print(Arm_constants.SQUARE_LOCATIONS)
# print(Arm_constants.POS_Z_BOARD)
tempTuple = (Arm_constants.SQUARE_LOCATIONS[7][7][0], Arm_constants.SQUARE_LOCATIONS[7][7][1])
Arm_constants.SQUARE_LOCATIONS[7][7] = (Arm_constants.SQUARE_LOCATIONS[7][7][0], Arm_constants.POS_Z_BOARD)
np.save('calibration_data.npy', Arm_constants.SQUARE_LOCATIONS)
Arm_constants.SQUARE_LOCATIONS[7][7] = tempTuple
def loadCalibration():
curFile = os.path.abspath(__file__)
dataFile = os.path.join(os.path.dirname(curFile), "calibration_data.npy")
# print(dataFile)
Arm_constants.SQUARE_LOCATIONS = np.load(dataFile, allow_pickle=True)
Arm_constants.POS_Z_BOARD = Arm_constants.SQUARE_LOCATIONS[7][7][1]
deltay = Arm_constants.SQUARE_LOCATIONS[7][6][1] - Arm_constants.SQUARE_LOCATIONS[7][5][1]
Arm_constants.SQUARE_LOCATIONS[7][7] = (Arm_constants.SQUARE_LOCATIONS[7][7][0], Arm_constants.SQUARE_LOCATIONS[7][6][1] + deltay)
Arm_constants.POS_Z_HIGHEST_PIECE += Arm_constants.POS_Z_BOARD
# print(Arm_constants.SQUARE_LOCATIONS)
# print(Arm_constants.POS_Z_BOARD)
def reCalibrate():
manualMode()
trash = input("MANUALLY MOVE ARM TO " + indiciesToSquare((0,0)) + " AND PRESS ENTER")
pos = Arm_constants.ARM.position
print (pos)
Arm_constants.OFFSET_X1 = pos[0]
Arm_constants.OFFSET_Y1 = pos[1]
Arm_constants.POS_Z_BOARD = pos[2]
Arm_constants.SQUARE_LOCATIONS[0][0] = (Arm_constants.OFFSET_X1, Arm_constants.OFFSET_Y1)
trash = input("MANUALLY MOVE ARM TO " + indiciesToSquare((0,7)) + " AND PRESS ENTER")
pos = Arm_constants.ARM.position
# print(pos)
Arm_constants.OFFSET_X2 = pos[0]
Arm_constants.OFFSET_Y2 = pos[1]
Arm_constants.SQUARE_LOCATIONS[0][7] = (Arm_constants.OFFSET_X2, Arm_constants.OFFSET_Y2)
if pos[2] < Arm_constants.POS_Z_BOARD:
Arm_constants.POS_Z_BOARD = pos[2]
Arm_constants.POS_Z_HIGHEST_PIECE += Arm_constants.POS_Z_BOARD
print("PERFORMING CALCULATIONS...")
deltax = Arm_constants.OFFSET_X2 - Arm_constants.OFFSET_X1
deltay = Arm_constants.OFFSET_Y2 - Arm_constants.OFFSET_Y1
deltax /= 7
deltay /= 7
for i in range(1, Arm_constants.SQUARES_IN_ROW - 1):
newX = (Arm_constants.SQUARE_LOCATIONS[0][i - 1])[0] + deltax
newY = (Arm_constants.SQUARE_LOCATIONS[0][i - 1])[1] + deltay
Arm_constants.SQUARE_LOCATIONS[0][i] = (newX, newY)
for i in range(1, Arm_constants.SQUARES_IN_ROW):
for j in range(0, Arm_constants.SQUARES_IN_ROW):
newX = (Arm_constants.SQUARE_LOCATIONS[i - 1][j])[0] + deltay
newY = (Arm_constants.SQUARE_LOCATIONS[i - 1][j])[1] + deltax
Arm_constants.SQUARE_LOCATIONS[i][j] = (newX, newY)
#set intitial loc by using delta x
#as we change y we set positon in reserve matrix
newX = Arm_constants.SQUARE_LOCATIONS[3][0][0] - 3 * deltax
newY = Arm_constants.SQUARE_LOCATIONS[0][0][1]
for i in range (0,4):
Arm_constants.RESERVE_LOCATIONS[i][0] = (newX, newY)
newY = newY + deltay
# given a string name of square in chess notation, returns a tuple of indices into the corresponding square in the data table
# returns (-1, -1) if invalid input
def squareToIndices(square : str):
x = int(square[1])
y = square[0].lower()
if x < 1 or x > 8 or ord(y) < 97 or ord(y) > 104:
return (-1, -1)
if Arm_constants.ROBOT_COLOR:
x = x - 8
x = abs(x)
y = ord(y) - 97
else:
x = x-1
y = ord(y) - 97 - 7
y = abs(y)
return (x, y)
# given a tuple representing indices for the stored table, returns the name of the square in chess notation
# returns a i9 if invalid inputs
def indiciesToSquare(indices : tuple):
x = indices[0]
y = indices[1]
if x > 7 or y > 7 or x < 0 or y < 0:
return 'i9'
if Arm_constants.ROBOT_COLOR:
x = -x
x += 8
y += 97
y = chr(y)
else:
x += 1
y = -y
y += 7
y += 97
y = chr(y)
return (str(y) + str(x))
def printStatus():
print('------------------------------------')
print('Mode: ' + str(Arm_constants.ARM.mode))
print('State: ' + str(Arm_constants.ARM.state))
print('Has Error Warn: ' + str(Arm_constants.ARM.has_err_warn))
print('Has Error: ' + str(Arm_constants.ARM.has_error))
print('Has Warn: ' + str(Arm_constants.ARM.has_warn))
print('Error Code: ' + str(Arm_constants.ARM.error_code))
print('Warn Code: ' + str(Arm_constants.ARM.warn_code))
print('------------------------------------')
def printStatusInfinite():
while True:
time.sleep(0.1)
print('------------------------------------')
print('Mode: ' + str(Arm_constants.ARM.mode))
print('State: ' + str(Arm_constants.ARM.state))
print('Has Error Warn: ' + str(Arm_constants.ARM.has_err_warn))
print('Has Error: ' + str(Arm_constants.ARM.has_error))
print('Has Warn: ' + str(Arm_constants.ARM.has_warn))
print('Error Code: ' + str(Arm_constants.ARM.error_code))
print('Warn Code: ' + str(Arm_constants.ARM.warn_code))
print('------------------------------------')
def manualMode():
Arm_constants.ARM.set_mode(2)
Arm_constants.ARM.clean_error()
Arm_constants.ARM.clean_warn()
Arm_constants.ARM.set_state(0)
def directedMode():
Arm_constants.ARM.set_mode(0)
Arm_constants.ARM.clean_error()
Arm_constants.ARM.clean_warn()
Arm_constants.ARM.set_state(0)
def pickupPiece():
Arm_constants.ARM.open_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
curPos = Arm_constants.ARM.position
Arm_constants.ARM.set_position(curPos[0], curPos[1], Arm_constants.POS_Z_BOARD, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.close_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
Arm_constants.ARM.set_position(curPos[0], curPos[1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
def dropPiece():
curPos = Arm_constants.ARM.position
Arm_constants.ARM.set_position(curPos[0], curPos[1], Arm_constants.POS_Z_BOARD, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.open_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
Arm_constants.ARM.set_position(curPos[0], curPos[1], Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
def moveToSquare(square : str):
# print(square)
indices = squareToIndices(square)
# print(indices)
Arm_constants.ARM.set_position(Arm_constants.SQUARE_LOCATIONS[indices[0]][indices[1]][0],
Arm_constants.SQUARE_LOCATIONS[indices[0]][indices[1]][1],
Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
def movePiece(square1 : str, square2 : str):
moveToSquare(square1)
pickupPiece()
moveToSquare(square2)
dropPiece()
def capturePiece(square2 : str):
moveToSquare(square2)
pickupPiece()
Arm_constants.ARM.set_servo_angle(1, 90, 100, 50, wait=True)
def rotate():
Arm_constants.ARM.set_servo_angle(1, 90, 100, 50, wait=True)
def unrotate():
Arm_constants.ARM.set_servo_angle(1, 0, 100, 50, wait=True)
def movePieceAndRotate(square1 : str, square2 : str, capture : str = ""):
unrotate()
if (capture != ""):
moveToSquare(capture)
pickupPiece()
deltax = Arm_constants.SQUARE_LOCATIONS[3][4][0] - Arm_constants.SQUARE_LOCATIONS[3][3][0]
deltay = Arm_constants.SQUARE_LOCATIONS[3][4][1] - Arm_constants.SQUARE_LOCATIONS[3][3][1]
trashx = Arm_constants.SQUARE_LOCATIONS[3][0][0] - (3 * deltax)
trashy = Arm_constants.SQUARE_LOCATIONS[3][0][1] - (3 * deltay)
Arm_constants.ARM.set_position(trashx, trashy, Arm_constants.POS_Z_HIGHEST_PIECE, 180, 0, 0, None, 100, 50, wait=True)
Arm_constants.ARM.open_lite6_gripper()
time.sleep(1)
Arm_constants.ARM.stop_lite6_gripper()
movePiece(square1, square2)
rotate()
# def capturePiece(square1 : str):
# print('hi')
# def saveCalibration(): #save POZ_Z_BOARD and SQUARE_LOCATIONS
# choice = input("Save calibration? (y/n): ")
# if choice == 'y':
# with open('calibration.dat', 'wb') as f:
# pickle.dump(Arm_constants.POS_Z_BOARD, f)
# pickle.dump(Arm_constants.SQUARE_LOCATIONS, f)
# print("Calibration saved!")
# def getSavedCalibration(): #get POZ_Z_BOARD and SQUARE_LOCATIONS
# try:
# with open('calibration.txt', 'rb') as f:
# Arm_constants.POS_Z_BOARD = pickle.load(f)
# Arm_constants.SQUARE_LOCATIONS = pickle.load(f)
# print("Calibration loaded!")
# return True
# except:
# print("No saved calibration found!")
# return False
# loadCalibration()
def testing():
instantiateArm()
calibrate()
# print(Arm_constants.SQUARE_LOCATIONS)
rotate()
command = ""
print('Now you can move pieces! First input a square that has a piece to pick up, then input a target square, and voila!~')
print('Enter \'quit\' at any time to exit!')
while 'quit' not in command.lower():
command = input("Which square should a piece be picked up from?")
square1 = command.lower()
if 'quit' in square1:
break
command = input("Which square should the piece be moved to?")
square2 = command.lower()
if 'quit' in square2:
break
movePieceAndRotate(square1, square2)
deinstantiateArm()
# testing()
# print('hello')
while 7==8:
print('DIE TANAY')
# testing()