-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSystemControl.py
314 lines (297 loc) · 11.7 KB
/
SystemControl.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import time
import Globals as G
# import a4988
class SystemControl:
"""Receives the translated RFID's from Wall through Parser and calls the 2D system movements accordingly.
In addition, records the current position of the bot during its movement as class variables. """
reset_flag = False
time_step = 0
start_dir = 1
direction = start_dir
startX = 3
startY = 1
robotX = startX
robotY = startY
GoalX = 3
GoalY = 4
dimX = 5
OBS = []
dead_pirates = []
attack_range = 2
def __init__(self):
self.direction = 1
self.startX = 3
self.startY = 1
self.robotX = self.startX
self.robotY = self.startY
self.GoalX = 3
self.GoalY = 4
self.dimX = 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
out = False
on_obstacle = False
if code == "Forward":
if self.direction == G.SOUTH:
self.robotX += 1
# a4988.moveVerticalDown(500)
elif self.direction == G.EAST:
self.robotY += 1
# a4988.moveVerticalUp(500)
elif self.direction == G.NORTH:
self.robotX -= 1
# a4988.moveVerticalUp(500)
elif self.direction == G.WEST:
self.robotY -= 1
# self.moveHorizontalDown(500)
check, obs = self.check_obstacles(self.robotX, self.robotY)
if self.checkBounds(self.robotX, self.robotY):
out = True
return goal_reached, out, on_obstacle
if check:
on_obstacle = True
return goal_reached, out, on_obstacle
# a4988.moveVerticalUp(10)
if code == "Backward":
if self.direction == G.SOUTH:
self.robotX -= 1
# a4988.moveHorizontalDown(500)
elif self.direction == G.EAST:
self.robotY -= 1
# self.moveVerticalDown(500)
elif self.direction == G.NORTH:
self.robotX += 1
# a4988.moveHorizontalUp(500)
elif self.direction == G.WEST:
self.robotY += 1
# a4988.moveVerticalUp(500)
check, obs = self.check_obstacles(self.robotX, self.robotY)
if self.checkBounds(self.robotX, self.robotY):
out = True
return goal_reached, out, on_obstacle
if check:
on_obstacle = True
return goal_reached, out, on_obstacle
# self.moveBackward()
if code == "TurnLeft":
self.direction = (self.direction + 1) % 4
# self.turnLeft()
if code == "TurnRight":
self.direction = (self.direction + 3) % 4
# self.turnRight()
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])
break
if self.robotX == self.GoalX and self.robotY == self.GoalY:
goal_reached = True
return goal_reached, out, on_obstacle
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 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 robot from its current location to the 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)
self.rerun(s)
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.dimX:
out_of_bounds = True
y = self.dimX - 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 rerun(self, code):
"""executing specifically reset()"""
action_list = code.split("\n")
length = len(action_list)
for i in range(0, length-1):
code = action_list[i]
self.moveRobot(code)
print("robotX")
print(self.robotX)
print("robotY")
print(self.robotY)
# TODO sleep time probably needs to correlate to 2D system move time.
time.sleep(2)
def run(self, code, obs, ded_obs):
"""runs the actions on the 2D system"""
action_list = code.split("\n")
length = len(action_list)
goal = False
for i in range(0, length-1):
code = action_list[i]
self.time_step += 1
self.move_obs()
goal, out, on_obstacle = self.moveRobot(code)
print("robotX")
print(self.robotX)
print("robotY")
print(self.robotY)
obs = self.OBS
if not len(self.dead_pirates) == 0:
ded_obs.append(self.dead_pirates[0])
# TODO sleep time probably needs to correlate to 2D system move time.
time.sleep(2)
if out:
print("OUT OF BOUNDS")
return False
if on_obstacle:
print("INVALID, ON OBSTACLE")
return False
if self.reset_flag:
print("RESET")
return False
return goal
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_random(self, pseudo_x, pseudo_y):
# """checks whether a single random move of the robot is feasible, ie not out of bounds and not overlapping"""
# # check whether it is out of bounds or overlapping with another obstacle, which are not allowed
# allowed = False
# if pseudo_x < 0 or pseudo_y < 0 or pseudo_x >= self.dimX or pseudo_y >= self.dimX:
# # check if it is out of bounds
# return allowed
# else:
# # check if it is overlapping with the obstacles
# for i in range(len(self.OBS)):
# temp = self.OBS[i]
# if temp.location[0] == [pseudo_x] and temp.location[1] == [pseudo_y]:
# return False
# allowed = True
# return allowed
#
# def move_obs_random(self):
# """moves the obstacle randomly"""
# # possible movements: north, south, east, west, attack
# for i in range(len(self.OBS)):
# if not self.OBS[i].movable:
# continue
# allowed = False
# while not allowed:
# index = randint(1, 4)
# if index == 1:
# # move north
# temp_x = self.OBS[i].location[0] - 1
# temp_y = self.OBS[i].location[1]
# if self.check_random(temp_x, temp_y):
# self.OBS[i].location[0] = temp_x
# allowed = True
# elif index == 2:
# # move south
# temp_x = self.OBS[i].location[0] + 1
# temp_y = self.OBS[i].location[1]
# if self.check_random(temp_x, temp_y):
# self.OBS[i].location[0] = temp_x
# allowed = True
# elif index == 3:
# # move east
# temp_x = self.OBS[i].location[0]
# temp_y = self.OBS[i].location[1] + 1
# if self.check_random(temp_x, temp_y):
# self.OBS[i].location[1] = temp_y
# allowed = True
# elif index == 4:
# # move west
# temp_x = self.OBS[i].location[0]
# temp_y = self.OBS[i].location[1] - 1
# if self.check_random(temp_x, temp_y):
# self.OBS[i].location[1] = temp_y
# allowed = True