-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (169 loc) · 5.94 KB
/
main.py
File metadata and controls
194 lines (169 loc) · 5.94 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
import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.misc import imresize
import separation
import findLegs
import create
import webcamTakePicture as webcam
ERROR = 3
#ChairLeg: 47-56 cm
def separationCal(leg1, leg2):
leg1_x = leg1[0]
leg1_y = leg1[1]
leg2_x = leg2[0]
leg2_y = leg2[1]
return np.sqrt(np.square(leg1_x-leg2_x) + np.square(leg1_y-leg2_y))
def updateLegMap(chairLegMap, chair_dis, chair_angle, robot_x, robot_y, robot_angle):
chair_x = chair_dis * np.sin((robot_angle + chair_angle)/180 * np.pi) + robot_x
chair_y = chair_dis * np.cos((robot_angle + chair_angle)/180 * np.pi) + robot_y
found = False
for i in range(0,len(chairLegMap)):
if chair_x >= chairLegMap[i][0] - ERROR and chair_x <= chairLegMap[i][0] + ERROR and chair_y >= chairLegMap[i][1] - ERROR and chair_y <= chairLegMap[i][1] + ERROR:
found = True
break
if not found:
chairLegMap.append((chair_x, chair_y))
return chairLegMap
def moveTo(aim_x, aim_y, robot_x, robot_y, robot_angle):
d_aim_x = aim_x - robot_x
d_aim_y = aim_y - robot_y
angle = 0
distanceToGo = np.sqrt(np.square(d_aim_x) + np.square(d_aim_y))
if aim_x > 0:
angle = np.arccos(d_aim_y / distanceToGo) / np.pi * 180 - robot_angle
else:
angle = -np.arccos(d_aim_y / distanceToGo) / np.pi * 180 - robot_angle
if angle > 180:
angle -= 360
elif angle < -180:
angle += 360
return distanceToGo, angle
pass
def moveBetweenLegsShort(leg1, leg2, robot_x, robot_y, robot_angle):
leg1_x = leg1[0]
leg1_y = leg1[1]
leg2_x = leg2[0]
leg2_y = leg2[1]
mid_x = (leg1_x + leg2_x) / 2
mid_y = (leg1_y + leg2_y) / 2
return moveTo(mid_x, mid_y, robot_x, robot_y, robot_angle)
def scatterPlot(array):
xs = []
ys = []
for i in range(0,len(array)):
xs.append(array[i][0])
ys.append(array[i][1])
# plt.plot(0,0,'go')
plt.plot(xs,ys,'ro')
# plt.axis([-200,200,-200,200])
# plt.show()
pass
def imageProcessing(chairLegMap):
img = webcam.takePicture()
legs = findLegs.findLegs(img)
if legs is not "none":
for j in range (0,len(legs)):
x = legs[j][0]
y = legs[j][1]
# cv2.circle(img, (int(x), int(y)), 5, (0,225,225), -1)
# cv2.imshow('circle',img)
# cv2.waitKey(0)
distance, angle = separation.convert(x,y)
# print ('angle, distance: ', angle, distance)
if angle > 20 or distance == 0:
continue
chairLegMap = updateLegMap(chairLegMap, distance, angle, currentConfigTranslationX, currentConfigTranslationY, currentConfigDegrees)
return chairLegMap
def exe():
robot = create.TetheredDriveApp()
robot.connect()
chairLegMap = []
robotConfigs = []
currentConfigDegrees = 0
currentConfigTranslationX = 0
currentConfigTranslationY = 0
for i in range(0,9):
img = webcam.takePicture()
legs = findLegs.findLegs(img)
if legs is not "none":
for j in range (0,len(legs)):
x = legs[j][0]
y = legs[j][1]
cv2.circle(img, (int(x), int(y)), 5, (0,225,225), -1)
cv2.imshow('circle',img)
cv2.waitKey(0)
distance, angle = separation.convert(x,y)
print ('angle, distance: ', angle, distance)
if angle > 20 or distance == 0:
continue
chairLegMap = updateLegMap(chairLegMap, distance, angle, currentConfigTranslationX, currentConfigTranslationY, currentConfigDegrees)
robot.testDrive()
currentConfigDegrees += 40
#We have returned to original rotational config
if currentConfigDegrees == 360:
currentConfigDegrees = 0
# After the robot find all the chair legs possible at this position, we have a map of the legs, it
# will try to go to the middle of the two legs.
if len(chairLegMap) < 2:
print("THERE IS ONLY ONE LEG!!")
else:
park = False
for i in range(0, len(chairLegMap)-1):
if park:
break
for j in range(i,len(chairLegMap)):
if park:
break
separationBtwLegs = separationCal(chairLegMap[i], chairLegMap[j])
if separationBtwLegs < 56 and separationBtwLegs > 46:
distanceToGo, angleToTurn = moveBetweenLegsShort(chairLegMap[i], chairLegMap[j], currentConfigTranslationX, currentConfigTranslationY, currentConfigDegrees)
if angleToTurn < 0:
robot.rotate(np.abs(angleToTurn),'left')
else:
robot.rotate(angleToTurn, 'right')
safe = True
while safe and distanceToGo > 0:
# take a picture before it move forward
img = webcam.takePicture()
legs = findLegs.findLegs(img)
if legs is not "none":
for j in range (0,len(legs)):
x = legs[j][0]
y = legs[j][1]
cv2.circle(img, (int(x), int(y)), 5, (0,225,225), -1)
cv2.imshow('circle',img)
cv2.waitKey(0)
distance, angle = separation.convert(x,y)
# print ('angle, distance: ', angle, distance)
if angle > 20 or distance == 0:
continue
if distance < 55:
robot.rotate(40, 'right')
currentConfigDegrees += 40
safe = False
break
else:
if distanceToGo >= 30:
d = 30
else:
d = distanceToGo
park = True
robot.move(d, 'forward')
distanceToGo -= d
currentConfigTranslationX = d * np.sin((currentConfigDegrees + angleToTurn)/180 * np.pi) + currentConfigTranslationX
currentConfigTranslationY = d * np.cos((currentConfigDegrees + angleToTurn)/180 * np.pi) + currentConfigTranslationY
print('PARK? ', park)
# distanceToGo, angleToTurn = moveBetweenLegsShort(chairLegMap[2], chairLegMap[4], currentConfigTranslationX, currentConfigTranslationY, currentConfigDegrees)
# currentConfigDegrees += angleToTurn
# robot.move(distanceToGo, 'forward')
# currentConfigTranslationX = (chairLegMap[2][0] + chairLegMap[4][0]) / 2
# currentConfigTranslationY = (chairLegMap[2][1] + chairLegMap[4][1]) / 2
# print('PARK AT: ', currentConfigTranslationX, currentConfigTranslationY, currentConfigDegrees)
scatterPlot(chairLegMap);
plt.plot(0,0,'go')
plt.plot(currentConfigTranslationX, currentConfigTranslationY,'bo')
plt.axis([-200,200,-200,200])
plt.show()
cv2.destroyAllWindows()
exe()