-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoveRobot.py
executable file
·274 lines (254 loc) · 9.68 KB
/
moveRobot.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
import Globals as G
import time
import httplib
class moveRobot:
"""Packages the translated RFID's received from Wall through Parser as minibot script in the form of
"<<<<SCRIPT, code>>>>" and sends it to minibot. In addition, records the current position of the bot during
its movement as class variables. """
reset_flag = False
time_step = 0
start_dir = 1
robotX = 0
robotY = 0
direction = start_dir
GoalX = 0
GoalY = 0
dimX = 0
dimY = 0
startX = 0
startY = 0
OBS = []
dead_pirates = []
attack_range = 2
# conn = httplib.HTTPConnection("192.168.4.71:8080")
def __init__(self):
self.startX = 3
self.startY = 1
self.robotX = self.startX
self.robotY = self.startY
self.GoalX = 3
self.GoalY = 4
self.dimX = 5
self.dimY = 5
def moveRobot(self, code):
"""returns one line of the SCRIPT string and a boolean representing whether the target goal is reached"""
goal_reached = False
s = ""
# TODO change "dummy" to actual power level
# TODO "dummy" refers to the power needed for minibot to move 1 grid length OR turn
MOVE_POWER = 50
TURN_POWER = 10
# TODO Figure out how long it takes to turn 90 degrees
TURN_TIME = 3
if code == "Forward":
if self.direction == G.SOUTH:
self.robotX += 1
elif self.direction == G.EAST:
self.robotY += 1
elif self.direction == G.NORTH:
self.robotX -= 1
elif self.direction == G.WEST:
self.robotY -= 1
s += "bot.move_forward {}\n".format(MOVE_POWER)
time = self.calcTravelTime(1, MOVE_POWER)
s += "bot.wait {}\n".format(time) + "\n"
if code == "Backward":
if self.direction == G.SOUTH:
self.robotX -= 1
elif self.direction == G.EAST:
self.robotY -= 1
elif self.direction == G.NORTH:
self.robotX += 1
elif self.direction == G.WEST:
self.robotY += 1
s += "bot.move_backward {}\n".format(MOVE_POWER)
time = self.calcTravelTime(1, MOVE_POWER)
s += "bot.wait {}\n".format(time) + "\n"
if code == "TurnLeft":
self.direction = (self.direction + 1) % 4
s += "bot.move_counter_clockwise {}\n".format(TURN_POWER)
s += "bot.wait {}\n".format(TURN_TIME) + "\n"
if code == "TurnRight":
self.direction = (self.direction + 3) % 4
s += "bot.move_clockwise {}\n".format(TURN_POWER)
s += "bot.wait {}\n".format(TURN_TIME) + "\n"
if code == "Attack":
in_range = []
if self.direction == G.SOUTH:
for dist in range(self.attack_range):
in_range.append([self.robotX + (dist + 1), self.robotY])
elif self.direction == G.NORTH:
for dist in range(self.attack_range):
in_range.append([self.robotX - (dist + 1), self.robotY])
elif self.direction == G.EAST:
for dist in range(self.attack_range):
in_range.append([self.robotX, self.robotY + (dist + 1)])
elif self.direction == G.WEST:
for dist in range(self.attack_range):
in_range.append([self.robotX, self.robotY - (dist + 1)])
for i in range(len(in_range)):
x = in_range[i][0]
y = in_range[i][1]
check, obs = self.check_obstacles(x, y)
if self.checkBounds(x, y):
# if a possible block is out of bounds, then the following blocks in the same direction will also
# be out of bounds, so no need to continue checking.
break
elif check:
self.OBS.remove(obs)
self.dead_pirates = []
self.dead_pirates.append([x, y])
# TODO does "attack" translate to minibot movement???
break
if self.robotX == self.GoalX and self.robotY == self.GoalY:
goal_reached = True
# print("HERE"+s)
return s, goal_reached
def checkBounds(self, x, y):
"""checks whether the current position of the robot is out of bounds in the map/maze
if the robot is out of bounds, then it resets the position of the robot at its last position in bound
returns True if the robot is out of bounds, and False if it is not."""
out_of_bounds = False
if x >= self.dimX:
out_of_bounds = True
x = self.dimX-1
elif x < 0:
out_of_bounds = True
x = 0
if y >= self.dimY:
out_of_bounds = True
y = self.dimY-1
elif y < 0:
out_of_bounds = True
y = 0
return out_of_bounds
def check_obstacles(self, x, y):
"""checks whether the current position of the robot is on an obstacle in the map/maze
returns True if the robot is on an obstacle, and False if it is not."""
for i in range(len(self.OBS)):
temp = self.OBS[i]
if temp.location[0] == x and temp.location[1] == y:
return True, temp
return False, None
def run(self, code, obs, ded_obs):
"""returns the finalized SCRIPT string to send to minibot"""
s = ""
list = code.split("\n")
length = len(list)
for i in range(0, length):
if not self.reset_flag:
code = list[i]
self.time_step += 1
self.move_obs()
temp, goal = self.moveRobot(code)
if not len(self.dead_pirates) == 0:
ded_obs.append(self.dead_pirates[0])
obs = self.OBS
if self.checkBounds(self.robotX, self.robotY):
break
obs1, obs2 = self.check_obstacles(self.robotX, self.robotY)
if temp != "":
s += temp
print temp
if obs1:
break
time.sleep(1)
else:
s += self.reset()
break
conn = httplib.HTTPConnection("192.168.4.71:8080")
conn.request("POST", "/script", s)
return s
def check_dir(self):
"""sets the direction to NORTH"""
if self.direction == G.NORTH:
return ""
elif self.direction == G.SOUTH:
return "TurnRight\nTurnRight\n"
elif self.direction == G.EAST:
return "TurnLeft\n"
elif self.direction == G.WEST:
return "TurnRight\n"
def rerun(self, code):
"""executing specifically reset()"""
s = ""
list = code.split("\n")
length = len(list)
for i in range(0, length):
code = list[i]
temp, goal = self.moveRobot(code)
if temp != "":
s += temp
# time.sleep(1)
return s
def revert_dir(self, dir):
"""reverts direction back to starting direction, starting at direction NORTH"""
# assuming everything starts facing NORTH
if dir == G.NORTH:
return ""
elif dir == G.SOUTH:
return "TurnRight\nTurnRight\n"
elif dir == G.EAST:
return "TurnRight\n"
else:
return "TurnLeft\n"
def reset(self):
"""returns the string to send to minibot for it to revert to its starting point"""
distX = self.robotX - self.startX
distY = self.robotY - self.startY
s = ""
if distX == 0 and distY == 0:
return
if distX > 0:
# go north
s += self.check_dir()
for i in range(distX):
s += "Forward\n"
s += self.revert_dir(self.start_dir)
elif distX < 0:
# go south
s += self.check_dir()
for i in range(distX):
s += "Backward\n"
s += self.revert_dir(self.start_dir)
if distY > 0:
# go west
s += self.check_dir() + "TurnLeft\n"
for i in range(distY):
s += "Forward\n"
s += "TurnRight\n"
s += self.revert_dir(self.start_dir)
elif distY < 0:
# go east
s += self.check_dir() + "TurnRight\n"
for i in range(distY):
s += "Forward\n"
s += "TurnLeft\n"
s += self.revert_dir(self.start_dir)
temp = self.rerun(s)
conn = httplib.HTTPConnection("192.168.4.71:8080")
conn.request("POST", "/script", temp)
return temp
def move_obs(self):
"""Moves the obstacles according to designated path"""
for i in range(len(self.OBS)):
temp_obs = self.OBS[i]
if not temp_obs.movable:
continue
path = temp_obs.path
movement_serial = self.time_step % len(path)
self.OBS[i].location = path[movement_serial]
def check_goal(self):
"""checks whether goal is reached by comparing goal to current location. (used in GUI)"""
if self.robotX == self.GoalX and self.robotY == self.GoalY:
return True
return False
def calcTravelTime(self, distance, power):
"""Returns the amount of [time] Minibot needs to move to go one unit of [distance]"""
time = 2
# TODO Calculate travel time based off wheels of Minibot
return time
# p = Parser()
# codeblock = p.runCode(p.translateRFID("rfidFOR.txt"))
# mr = moveRobot()
# print mr.run(codeblock)