Repository navigation
Expand file tree
/
Copy pathold_code.py
More file actions
205 lines (178 loc) · 6.69 KB
/
Copy pathold_code.py
File metadata and controls
205 lines (178 loc) · 6.69 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
import itertools
import time
import pypot.dynamixel
import math
import cv2
import numpy as np
import serial
import cv2.cv as cv
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
mot1 = 1
mot2 = 4
mot3 = 3
#PuertoSerie = serial.Serial('COM22', 9600)
dxl_io = pypot.dynamixel.DxlIO('/dev/ttyUSB0')
found_ids = dxl_io.scan([mot1,mot2,mot3])
print('Found ids:', found_ids)
ids = found_ids[:3]
dxl_io.enable_torque(ids)
speed = dict(zip(ids, itertools.repeat(400)))
dxl_io.set_moving_speed(speed)
dxl_io.set_max_torque({mot1: 1000})
dxl_io.set_max_torque({mot2: 1000})
dxl_io.set_max_torque({mot3: 1000})
def posicion(puntos):
xo = puntos[0]
yo = puntos[1]
zo = puntos[2]
Y1 = (0.0)
R = (203.0)
r = (100.0)
X0 = xo
Y0 = yo
Z0 = zo
AA = (((X0) ** 2 + (Y0) ** 2 + (Z0) ** 2 - (Y1) ** 2 - (R) ** 2 + (r) ** 2)) / (2 * Z0)
BB = (Y1 - Y0) / Z0
aa = (1 + (BB) ** 2)
bb = (2 * AA * BB - 2 * Y1)
cc = ((AA) ** 2 + (Y1) ** 2 - (r) ** 2)
X = 0
Y = ((-bb + math.sqrt(((bb) ** 2) - 4 * aa * cc))) / (2 * aa)
Z = BB * Y + AA
ALFA = (180.0 / math.pi) * math.atan(Z / (Y - Y1))
X0 = xo * math.cos(math.pi * 2 / 3.0) - yo * (math.sin(2 * math.pi / 3.0))
Y0 = xo * math.sin(math.pi * 2 / 3.0) + yo * (math.cos(120 * math.pi / 180.0))
Z0 = zo
AA = (((X0) ** 2 + (Y0) ** 2 + (Z0) ** 2 - (Y1) ** 2 - (R) ** 2 + (r) ** 2)) / (2 * Z0)
BB = (Y1 - Y0) / Z0
aa = (1 + (BB) ** 2)
bb = (2 * AA * BB - 2 * Y1)
cc = ((AA) ** 2 + (Y1) ** 2 - (r) ** 2)
X2 = 0.0
Y2 = ((-bb + math.sqrt(((bb) ** 2) - 4 * aa * cc))) / (2 * aa)
Z2 = BB * Y2 + AA
BETA = (180.0 / math.pi) * math.atan(Z2 / (Y2 - Y1))
X0 = xo * math.cos(math.pi * 2 / 3.0) + yo * (math.sin(2 * math.pi / 3.0))
Y0 = -xo * math.sin(math.pi * 2 / 3.0) + yo * (math.cos(2 * math.pi / 3.0))
Z0 = zo
AA = (((X0) ** 2 + (Y0) ** 2 + (Z0) ** 2 - (Y1) ** 2 - (R) ** 2 + (r) ** 2)) / (2 * Z0)
BB = (Y1 - Y0) / Z0
aa = (1 + (BB) ** 2)
bb = (2 * AA * BB - 2 * Y1)
cc = ((AA) ** 2 + (Y1) ** 2 - (r) ** 2)
X3 = 0.0
Y3 = ((-bb + math.sqrt(((bb) ** 2) - 4 * aa * cc))) / (2 * aa)
Z3 = BB * Y3 + AA
GAMMA = (180.0 / math.pi) * math.atan(Z3 / (Y3 - Y1))
"""print "EMPEZANDO" """
pres1 = dxl_io.get_present_position((mot1, ))[0]
pres2 = dxl_io.get_present_position((mot2, ))[0]
pres3 = dxl_io.get_present_position((mot3, ))[0]
dxl_io.set_goal_position({mot1: ALFA})
dxl_io.set_goal_position({mot2: BETA})
dxl_io.set_goal_position({mot3: GAMMA})
pres1 = dxl_io.get_present_position((mot1, ))[0]
pres2 = dxl_io.get_present_position((mot2, ))[0]
pres3 = dxl_io.get_present_position((mot3, ))[0]
#exit()
time.sleep(0.5)
for vuelta in [1,2]:
posicion([0, 0, 120])
posicion([150, -60, 120])
time.sleep(4)
captura = cv2.VideoCapture(0)
_, frame = captura.read()
maxr = np.max(frame[:,:,0])
maxg = np.max(frame[:,:,1])
maxb = np.max(frame[:,:,2])
minr = np.min(frame[:,:,0])
ming = np.min(frame[:,:,1])
minb = np.min(frame[:,:,2])
size = np.shape(frame)
img = np.zeros(size, 'uint8')
img[:,:,0] = np.uint8(255.*(frame[:,:,0]-minr)/(maxr-minr))
img[:,:,1] = np.uint8(255.*(frame[:,:,1]-ming)/(maxg-ming))
img[:,:,2] = np.uint8(255.*(frame[:,:,2]-minb)/(maxb-minb))
#img = cv2.medianBlur(img,5)
cmig2= img
cimg2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(cimg2,cv.CV_HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=5,maxRadius=80)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv[:,:,1] = 255
hsv[:,:,2] = 255
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
try:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
px = img[i[1],i[0]]
#print 'draw the outer circle' """
cv2.circle(cimg2,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(cimg2,(0,0),10,(0,255,0),2)
cv2.circle(cimg2,(100,0),10,(0,255,0),2)
#print 'draw the center of the circle'
cv2.circle(cimg2,(i[0],i[1]),2,(0,0,255),3)
x6 = -(i[0] - 320) * 230/640
y6 = -(i[1] - 240) * 170/480
print 'selecciono circulos'
print px, x6,y6
"""print Math.sqrt(px[1])
print Math.sqrt(255-px[0])
print Math.sqrt(px[2])"""
if (math.sqrt((px[1])**2 + (px[0])**2 + (255-px[2])**2))<50:
posicion([x6,y6,120])
posicion([x6,y6,200])
posicion([x6,y6,210])
posicion([x6,y6,218])
#PuertoSerie.write('1')
GPIO.output(17,True)
time.sleep(1)
posicion([x6,y6,120])
posicion([0,-195,100])
posicion([0,-195,135])
#PuertoSerie.write('0')
GPIO.output(17,False)
time.sleep(1)
print x6, y6, 'rojo'
if (math.sqrt((px[1])**2 + (255-px[0])**2 + (px[2])**2))<190:
posicion([x6,y6,120])
posicion([x6,y6,200])
posicion([x6,y6,210])
posicion([x6,y6,218])
#PuertoSerie.write('1')
GPIO.output(17,True)
time.sleep(1)
posicion([x6,y6,120])
posicion([-173,100,100])
posicion([-173,100,135])
#PuertoSerie.write('0')
GPIO.output(17,False)
time.sleep(1)
print x6, y6, 'azul'
if (math.sqrt((255-px[1])**2 + (px[0])**2 + (px[2])**2))<250:
posicion([x6,y6,120])
posicion([x6,y6,200])
posicion([x6,y6,210])
posicion([x6,y6,218])
#PuertoSerie.write('1')
GPIO.output(17,True)
time.sleep(1)
posicion([x6,y6,120])
posicion([173,100,100])
posicion([173,100,135])
#PuertoSerie.write('0')
GPIO.output(17,False)
time.sleep(1)
print x6, y6, 'verde'
except:
pass
#cv2.imshow('detected circles',img)
#cv2.imshow('detected circles2',cimg2)
print"ACABANDO"
print vuelta
posicion([0, 0, 120])
captura.release()
print "again"
#cv2.waitKey(0)
print "yata"