-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.py
222 lines (170 loc) · 5.76 KB
/
assignment.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
from __future__ import print_function
import time
import math
from sr.robot import *
a_th = 2.0
""" float: Threshold for the control of the orientation"""
d_th = 0.4
""" float: Threshold for the control of the linear distance"""
R = Robot()
token_to_find = "silver"
""" string: Indicstion of which box should the robot look for first"""
ordered_boxes=[]
""" list: list of boxes used by the robot"""
direction = 1
code_to_find = 0
finished = False
def drive(speed, seconds):
"""
Function for setting a linear velocity
Args: speed (int): the speed of the wheels
seconds (int): the time interval
"""
R.motors[0].m0.power = speed
R.motors[0].m1.power = speed
time.sleep(seconds)
R.motors[0].m0.power = 0
R.motors[0].m1.power = 0
def turn(speed, seconds):
"""
Function for setting an angular velocity
Args: speed (int): the speed of the wheels
seconds (int): the time interval
"""
R.motors[0].m0.power = speed
R.motors[0].m1.power = -speed
time.sleep(seconds)
R.motors[0].m0.power = 0
R.motors[0].m1.power = 0
def find_token(color, code):
"""
Function to find the closest token
Returns:
dist (float): distance of the closest token (-1 if no token is detected)
rot_y (float): angle between the robot and the token (-1 if no token is detected)
code (float): id number of the token (-1 if no token is detected)
"""
if(color == "silver"):
m = MARKER_TOKEN_SILVER
elif(color == "gold"):
m = MARKER_TOKEN_GOLD
dist = 100
for token in R.see():
if(code == 0):
if token.dist < dist and token.info.marker_type == m and (token.info.code, token.info.marker_type) not in ordered_boxes:
dist = token.dist
return_code = token.info.code
rot_y = token.rot_y
else:
if token.dist < dist and token.info.marker_type == m and (token.info.code, token.info.marker_type) not in ordered_boxes and code == token.info.code:
dist = token.dist
return_code = token.info.code
rot_y = token.rot_y
if(dist == 100):
return -1, -1, -1
else:
return dist, rot_y, return_code
def find_closest_gold():
"""
Function to find the closest golden token among all.
- If the robot find a token closest than the prevoius one and the next one (due to the distribution of the world), it returns that token because is the closest among all
- If the robot has seen all the tokens, it returns understanding that the last token is the closest.
Returns:
final_code (float): id number of the token (-1 if no token is detected)
"""
arr_gold = []
codes = []
not_all = True
c = 0
for cod,col in ordered_boxes:
if(col == 'gold-token'):
c += 1
tokens_seen = 0
while(not_all):
dist, rot_y, code = find_token("gold", 0)
if(len(arr_gold) >= 2 and dist > arr_gold[-1][0] and arr_gold[-1][0] < arr_gold[-2][0] and code != arr_gold[-1][1]):
not_all = False
elif(tokens_seen + c == 6):
not_all = False
elif(dist != -1 and code not in codes):
arr_gold.append((dist, code))
codes.append(code)
tokens_seen += 1
turn(20, 0.1)
print("Looking closest gold token...")
min_dist = 100
final_code = 0
for dist, code in arr_gold:
if(dist < min_dist):
min_dist = dist
final_code = code
print("Closest gold token: ", final_code)
return final_code
def get_rotation_vel(rot_y):
"""
Function that calculates the robot rotation velocity proportional to the angle
Returns:
turn_vel (float): velocity of turning
"""
turn_vel = math.exp(0.3*abs(rot_y)) # Proportional velocity to the angle
if(turn_vel > 15): # Stablish a limit
turn_vel = 15
if(turn_vel <= 0): # Stablish a limit
turn_vel = 0.1
return turn_vel
def get_velocity(dist):
"""
Function that calculates the robot linear velocity proportional to the distance
Returns:
velocity (float): linear velocity
"""
if(dist <= 0.7):
velocity = 10
else:
velocity = dist*100 #Proportional velocity to the distance
if(velocity > 300): # Stablish a limit
velocity = 300
if(velocity <= 0): # Stablish a limit
velocity = 10
return velocity
while(not finished):
if(len(ordered_boxes) == 12):
finished = True
dist, rot_y, code = find_token(token_to_find, code_to_find)
print("Distance: ", dist, " Angle: ", rot_y, " Code: ", code)
if(dist == -1 or rot_y == -1):
print("Turning...")
turn(direction*20, 0.1)
elif(dist <= d_th):
print("Got it!")
if(token_to_find == "gold"):
R.release()
print("Silver token released!")
token_to_find = "silver"
d_th = 0.4
drive(-30, 1)
ordered_boxes.append((code, MARKER_TOKEN_GOLD))
code_to_find = 0
direction = 1
else:
R.grab()
print("Silver token grabbed!")
token_to_find = "gold"
d_th = 0.6
ordered_boxes.append((code, MARKER_TOKEN_SILVER))
code_to_find = find_closest_gold()
direction = -1
elif(rot_y <= a_th and rot_y >= -a_th):
print("Centered! Going forward...")
velocity = get_velocity(dist)
drive(velocity,0.1)
elif(rot_y > a_th):
print("Turning to the right...")
turn_vel = get_rotation_vel(rot_y)
turn(1*turn_vel, 0.1)
elif(rot_y < -a_th):
print("Turning to the left...")
turn_vel = get_rotation_vel(rot_y)
turn(-1*turn_vel, 0.1)
print("FINISHED")
exit()