-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscrewPicker.py
406 lines (343 loc) · 11.5 KB
/
screwPicker.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
398
399
import sys
from time import sleep
from random import random
import time
import math
sys.path.append("..")
from ax12 import ax12
from select import select
import RPi.GPIO as GPIO
import spidev
import os
from servoHandler import servoHandler
from switchObj import SwitchObj
import json
from ImPro import ImPro
from FCab import JsonDump
import numpy as np
# - - - - - - - - - - - - - - - -
# - - - - - GPIO SETUP - - - - -
# - - - - - - - - - - - - - - - -
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(6,GPIO.IN)
GPIO.setup(13,GPIO.IN)
GPIO.setup(19,GPIO.IN)
GPIO.setup(26,GPIO.IN)
GPIO.setup(12,GPIO.IN)
GPIO.setup(16,GPIO.IN)
GPIO.setup(20,GPIO.IN)
# - - - - - - - - - - - - - - - -
# - - - GLOBAL OBJECTS - - - - -
# - - - - - - - - - - - - - - - -
jD = JsonDump()
imageProObj = ImPro()
spi = spidev.SpiDev()
spi.open(0,0)
servos = ax12.Ax12()
switchObj = SwitchObj()
servoHandlerObj = [0]
servoHandlerObj.append(servoHandler(1, 500, servos))
servoHandlerObj.append(servoHandler(2, 500, servos))
servoHandlerObj.append(servoHandler(3, 500, servos))
servoHandlerObj.append(servoHandler(4, 500, servos))
# - - - - - - - - - - - - - - - -
# - CHECK MOVE DEMANDED FLAGS - -
# - - - - - - - - - - - - - - - -
def checkMoveDemandedFlags(servoID):
controlBit = False
for i in range(len(servoID)):
if(servoHandlerObj[servoID[i]].moveDemanded):
return True
return False
# - - - - - - - - - - - - - - - -
# - - - MOVE THEM SERVOS - - -
# - - - - - - - - - - - - - - - -
def moveThemServos(servoID, pos, speed):
if isinstance(servoID, list):
for index, servo_id in enumerate(servoID):
servoHandlerObj[servo_id].moveSmooth(pos[index], speed[index])
while checkMoveDemandedFlags(servoID):
for i in servoID:
servoHandlerObj[i].update()
servos.action()
sleep(0.1)
else:
servoHandlerObj[servoID].moveSmooth(pos, speed)
while checkMoveDemandedFlags([servoID]):
servoHandlerObj[servoID].update()
servos.action()
sleep(0.1)
# - - - - - - - - - - - - - - - -
# - - - SET TORQUE TO VAL - - -
# - - - - - - - - - - - - - - - -
def setTorqueServos(servoID, val):
for i in servoID:
servos.setTorqueStatus(i, val)
print "Torque set to ", val
# - - - - - - - - - - - - - - - -
# - - - WAIT FOR USER RESP - - -
# - - - - - - - - - - - - - - - -
def waitForUserResponce(string):
tmp = raw_input(string)
if(tmp is ""):
return True
else:
return False
# - - - - - - - - - - - - - - - -
# - - - - - - LEDS OFF - - - - -
# - - - - - - - - - - - - - - - -
def LedsOff(servoID):
for i in servoID:
servoHandlerObj[i].setLed(0)
# - - - - - - - - - - - - - - - -
# - - - - - INVERT LEDS - - - - -
# - - - - - - - - - - - - - - - -
def invertLeds(servoID):
for i in servoID:
servoHandlerObj[i].invertLed()
# - - - - - - - - - - - - - - - -
# - - - - - INPUT CHECK - - - - -
# - - - - - - - - - - - - - - - -
def checkForInput():
rlist, _, _, = select([sys.stdin], [], [], 0.1)
if rlist:
s = sys.stdin.readline()
print s
return s
else:
return "None"
# - - - - - - - - - - - - - - - -
# - - - CAUTION LED BLINK - - - -
# - - - - - - - - - - - - - - - -
def cautionLedBlink():
for i in [1,2,3,4]:
servoHandlerObj[i].setLed(0)
sleep(0.1)
for _ in range(24):
invertLeds([1,2,3,4])
sleep(0.1)
for i in [1,2,3,4]:
servoHandlerObj[i].setLed(1)
# - - - - - - - - - - - - - - - -
# - - UPDATE SERVO POS POTS - - -
# - - - - - - - - - - - - - - - -
def updateServoPosPots():
if GPIO.input(13) is 1:
moveThemServos([2], [readChannel(2)], [readChannel(0) + 1])
if GPIO.input(19) is 1:
moveThemServos([3], [readChannel(3)], [readChannel(0) + 1])
if GPIO.input(26) is 1:
moveThemServos([4], [readChannel(4)], [readChannel(0) + 1])
if GPIO.input(6) is 1:
moveThemServos([1], [readChannel(1)], [readChannel(0) + 1])
# - - - - - - - - - - - - - - - -
# - - - - SAVE SERVO POS - - - -
# - - - - - - - - - - - - - - - -
def saveServoPos(servoID):
posList = []
speedList = []
idList = []
switchObj.update()
status_ = switchObj.checkStatus()
while True:
switchObj.update()
updateServoPosPots()
for id_ in servoID:
if(id_ is 1 and GPIO.input(6) is 1):
if id_ not in idList:
idList.append(id_)
print "idList: ", idList
if(id_ is 2 and GPIO.input(13) is 1):
if id_ not in idList:
idList.append(id_)
print "idList: ", idList
if(id_ is 3 and GPIO.input(19) is 1):
if id_ not in idList:
idList.append(id_)
print "idList: ", idList
if(id_ is 4 and GPIO.input(26) is 1 and not id_ in idList):
if id_ not in idList:
idList.append(id_)
print "idList: ", idList
if(id_ is 55 and status_ is not switchObj.checkStatus() and not id_ in idList):
sleep(0.1)
GPIO.output(17, GPIO.input(12))
idList.append(id_)
print "idList: ", idList
if(GPIO.input(20) is 1 and GPIO.input(6) is 0 and GPIO.input(13) is 0 and GPIO.input(19) is 0
and GPIO.input(26) is 0):
for _ in range(8):
GPIO.output(21, 1)
sleep(0.1)
GPIO.output(21, 0)
sleep(0.1)
for id_ in idList:
if(id_ is not 55):
if id_ is 2:
speedList.append(readChannel(0))
posList.append(servoHandlerObj[id_].getPosition() + 4)
else:
speedList.append(readChannel(0))
posList.append(servoHandlerObj[id_].getPosition())
else:
posList.append(GPIO.input(12))
#imageProObj.blink()
#imageProObj.captureFrame()
return idList, posList, speedList
# - - - - - - - - - - - - - - - -
# - - - - READ CHANNEL - - - - -
# - - - - - - - - - - - - - - - -
def readChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# - - - - - - - - - - - - - - - -
# - SHUT DOWN AND WAIT FOR POKE -
# - - - - - - - - - - - - - - - -
def shutDown():
positionList = [0, 1, 2, 3, 4]
moveList = []
moveList.append([[3, 2, 1, 4], [862, 100, 100, 512], [400, 200, 200, 200]])
#get in pre shut down position
for ent in moveList:
tmp = False
print "ent: ", ent
servoID = ent[0]
pos = ent[1]
speed = ent[2]
for index, id_ in enumerate(servoID):
if id_ is 55:
GPIO.output(17, pos[index])
tmp = True
sleep(0.2)
if tmp is False:
moveThemServos(servoID, pos, speed)
setTorqueServos([1, 2, 3, 4], 0)
LedsOff([1, 2, 3, 4])
imageProObj.camera._set_led(False)
sleep(1.0)
for index, ent in enumerate(servoHandlerObj):
if ent is not 0:
positionList[index] = ent.getPosition()
while True:
sleep(0.5)
for index, ent in enumerate(servoHandlerObj):
if ent is not 0:
tmp = ent.getPosition()
if positionList[index] + 10 < tmp or positionList[index] - 10 > tmp:
print "movement!"
imageProObj.camera._set_led(True)
return True
# - - - - - - - - - - - - - - - -
# - - - PICK UP ALGORITHM - - - -
# - - - - - - - - - - - - - - - -
def pickUpAlgorithm(number):
'''Algorithm which contains all the things needed for
looking up the screws, picking them up and putting them somewhere
return True if there were objects
return False if there was no object'''
flag = False
x_offset = [204, 358, 512, 666, 820]
moveList = []
newList = []
#load some position arrays
startposY = jD.load('startposY.txt')
Ypos = jD.load('Ypos.txt')
endpos = jD.load('endpos.txt')
if number is 0:
#This is the standard position
moveList.append([[3, 2, 1], [0, 36, 203], [300, 300, 300]])
#Here we got the robot on the right startposition on the X axis
moveList.append([[4], [x_offset[number]], [300]])
#Here we write the posture for frame taking (startpos Y axis)
for ent in startposY:
moveList.append(ent)
#get in image taking position
for ent in moveList:
tmp = False
print "ent: ", ent
servoID = ent[0]
pos = ent[1]
speed = ent[2]
for index, id_ in enumerate(servoID):
if id_ is 55:
GPIO.output(17, pos[index])
tmp = True
sleep(0.2)
if tmp is False:
moveThemServos(servoID, pos, speed)
#reset moveList
moveList = []
y_vals = []
x_vals = []
#CAPTURE FRAME and GET PIXEL OBJECTS
imageProObj.blink()
imageProObj.captureFrame()
print "nr. of obj in pixel obj list: ", len(imageProObj.pixelObjList)
for ent in imageProObj.pixelObjList:
if ent.numberOfPixels > 50:
rounded_val = int(round((1.2*ent.coord_y)/10.0 - 0.5))
if rounded_val > 11:
rounded_val = 11
if (number <= 1 and rounded_val <= 2) :
print "skip"
else:
flag = True
print "rounded thing: ", rounded_val
y_vals.append(rounded_val)
tmp = np.arctan(1.0*(ent.coord_x - 24)/ent.coord_y)
print "tmp: ", tmp
x_vals.append(-int(195*tmp))
print "numberOfPixels: ", ent.numberOfPixels
print "coord_x: ", ent.coord_x
print "coord_y: ", ent.coord_y
if flag is False:
return False
#get the robot to GOALPOS on X axis!
for index, ent in enumerate(x_vals):
moveList.append([[4], [x_offset[number] + ent], [200]])
#this is to get to the GOALPOS on the Y axis!
for entry in Ypos[y_vals[index]]:
moveList.append(entry)
moveList.append([[3, 2, 1], [612, 36, 203], [300, 300, 300]])
#ENDPOS
for e in endpos:
moveList.append(e)
#standard position
moveList.append([[3, 2, 1, 4], [0, 36, 203, x_offset[number]], [300, 300, 300, 300]])
#write positiondata in list
#while GPIO.input(20) is 0:
# sleep(0.1)
# result = saveServoPos([1, 2, 3, 4, 55])
# moveList.append(result)
# newList.append(result)
# print moveList
#jD.dump(newList, 'newList.txt')
#playback
for ent in moveList:
tmp = False
print "ent: ", ent
servoID = ent[0]
pos = ent[1]
speed = ent[2]
for index, id_ in enumerate(servoID):
if id_ is 55:
GPIO.output(17, pos[index])
tmp = True
sleep(0.4)
if tmp is False:
moveThemServos(servoID, pos, speed)
return True
# - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - -
# - - - MAIN PROG START - - - - -
# - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - -
while shutDown():
cautionLedBlink()
for i in range(5):
while pickUpAlgorithm(i):
print "doing the thing"