-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.py
More file actions
235 lines (183 loc) · 6.83 KB
/
assignment.py
File metadata and controls
235 lines (183 loc) · 6.83 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
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
#-------------IMPORT LIBRARIES----------------------
from __future__ import print_function
import time
#-------------IMPORT CLASS ROBOT--------------------
from sr.robot import *
#-------------DEFINING GOLBAL VARIABLES-------------
""" float: Threshold for the control of the linear distance"""
a_th = 2.3
""" float: Threshold for the control of the orientation"""
d_th = 0.4
""" instance of the class Robot"""
R = Robot()
""" int: Maximum frontal distance that the robot must keep from golden tokens"""
gold_th=1
""" float: Threshold for the activation of the grab routine"""
silver_th=1.5
#-------------DEFINING FUNCTIONS-------------
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_silver_token():
"""
Function to find the closest silver token
Returns:
dist (float): distance of the closest silver token (-1 if no silver token is detected)
rot_y (float): angle between the robot and the silver token (-1 if no silver token is detected)
"""
dist=3
for token in R.see():
if token.dist < dist and token.info.marker_type is MARKER_TOKEN_SILVER and -70<token.rot_y<70:
#the field of view for detectig silver token is restricted to an angle of -70, 70 degrees.
#In this way the robot can detect silver token just in front of it.
#This is useful for avoid the robot detecting silver token behind it.
dist=token.dist
rot_y=token.rot_y
if dist==3:
return -1, -1
else:
return dist, rot_y
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def find_golden_token():
"""
Function to find the closest golden token
Returns:
dist (float): distance of the closest golden token (-1 if no golden token is detected)
"""
dist=100
for token in R.see():
if token.dist < dist and token.info.marker_type is MARKER_TOKEN_GOLD and -40<token.rot_y<40:
#the field of view for detectig gold token is restricted to an angle of -40, 40 degrees.
#In this way the robot can detect gold token just in front of it.
dist=token.dist
if dist==100:
return -1
else:
return dist
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def find_golden_token_left():
"""
Function to compute the closest golden token distace on the left of the robot.
Returns:
dist (float): distance of the closest golden token on the left of the robot (-1 if no golden token is detected on the left of the robot)
"""
dist=100
for token in R.see():
if token.dist < dist and token.info.marker_type is MARKER_TOKEN_GOLD and -105<token.rot_y<-75:
#The (-105, -75) angle span is useful for detecting gold token on the left
dist=token.dist
if dist==100:
return -1
else:
return dist
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def find_golden_token_right():
"""
Function to compute the closest golden token distace on the right of the robot.
Returns:
dist (float): distance of the closest golden token on the right of the robot (-1 if no golden token is detected on the right of the robot)
"""
dist=100
for token in R.see():
if token.dist < dist and token.info.marker_type is MARKER_TOKEN_GOLD and 75<token.rot_y<105:
#The (75, 105) angle span is useful for detecting gold token on the right
dist=token.dist
if dist==100:
return -1
else:
return dist
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def grab_routine(rot_silver, dist_silver):
"""
Function to control the routine for grabbing silver tokens
Arguments:
rot_silver (float): angle between the robot and the closest silver token
dist_silver (float): distance from the closest silver token
No returns.
"""
if dist_silver <= d_th:
print("Found it!")
if R.grab():
print("Grabbed!")
turn(20, 3)
drive(15, 0.8)
R.release()
drive(-15,0.8)
turn(-20,3)
elif -a_th<=rot_silver<=a_th:
drive(40, 0.5)
print("Correct angle")
elif rot_silver < -a_th:
print("Left a bit...")
turn(-5, 0.3)
elif rot_silver > a_th:
print("Right a bit...")
turn(+5, 0.3)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def turn_method(left_dist, right_dist, dist_gold):
"""
Function that implements the turn decision method
Arguments:
left_dist (float): distance of the closest gloden token on the left of the robot
right_dist (float): distance of the closest gloden token on the right of the robot
No returns.
"""
print("Where's the wall?")
if left_dist>1.2*right_dist:
print("The wall is on the right at a distance of: "+ str(right_dist))
while dist_gold<gold_th: #until there are no more gold tokens in front of the robot
dist_gold=find_golden_token()
turn(-10, 0.1)
print("I'm turning left")
elif right_dist>1.2*left_dist:
print("The wall is on the left at a distance of: "+ str(left_dist))
while dist_gold<gold_th:
dist_gold=find_golden_token()
turn(10, 0.1)
print("I'm turning right")
else:
drive(15,0.5)
print("Left and right distances are similar, i'll go straight")
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def main():
while 1:
#Updating variables value for every while cycle
dist_silver, rot_silver = find_silver_token()
dist_gold=find_golden_token()
left_dist=find_golden_token_left()
right_dist=find_golden_token_right()
#Check if gold token are detected, if no gold token are detected go straight ahead.
if (dist_gold>gold_th and dist_silver>silver_th) or (dist_gold>gold_th and dist_silver==-1):
print("I'll go straight ahead")
drive(70,0.5)
#If gold token are detected, then check where the wall is
elif dist_gold<gold_th and dist_gold!=-1:
#The robot decides where to turn
turn_method(left_dist, right_dist, dist_gold)
if dist_silver<silver_th and dist_silver!=-1:
#If any silver token closer than silver_th is detected, the grab routine will start
print("Silver is close")
grab_routine(rot_silver, dist_silver)
#-----------------main() CALL-------------------
main()