-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoccupancy_algorithm.py
More file actions
executable file
·308 lines (272 loc) · 12.3 KB
/
Copy pathoccupancy_algorithm.py
File metadata and controls
executable file
·308 lines (272 loc) · 12.3 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
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
#!/usr/bin/env python3
'''
@Author : Abdul-Razak Adam
This class generates a 2-D array representation of the environment of the robot. The size of the grid, the current position
and the direction of the robot in relation to the map is specified.
Algorithm
At each free cell:
Get the free neighbours of the cell, Priority is given to the left neighbours hence we visit the left neighbour, then
the forward then the right neighbours. Update the map with detected obstacles.
if atleast a neighbour is free, and it is not visited, move to neighbouring cell and update the map.
if not neighbour, then backtrack
if after backtracking an no neighbour then end.
'''
from cell import Cell
from robot import Robot
import time
'''
height :: height of the grid
width :: width of the grid
start_position :: start position eg (2,0)
start_direction ::starting direction of the robot eg 'N', 'E', 'S' or 'W'
speed :: speed of movement of the robot
cell_size :: size of the size in cm
'''
class GenerateMap(object):
def __init__(self, height, width, start_position, start_direction, speed, cell_size):
self.WIDTH = width
self.HEIGHT = height
self.direction = start_direction
self.current_position = start_position
self.SPEED = speed
self.CELL_SIZE = cell_size
self.generate_map()
#cretae a robot with motors in port B and C and sensors in 4,3 and 2
self.robot = Robot('B', 'C', '4','3','2')
#generate a map with cell
def generate_map(self):
self.map = [[0 for x in range(self.WIDTH)] for y in range(self.HEIGHT)]
for i in range(self.HEIGHT):
for r in range(self.WIDTH):
self.map[i][r] = Cell(i, r)
#This method get the average readings of a specified sensor catering for errors
def readUSsensorValues(self, sensor):
d1 = self.robot.getSensorReading(sensor)
time.sleep(0.1)
d2 = self.robot.getSensorReading(sensor)
time.sleep(0.1)
d3 = self.robot.getSensorReading(sensor)
time.sleep(0.1)
ave = (d1 + d2 + d3) / 3
return ave <= (self.CELL_SIZE - 10)
#Update obstacles in the map as they are encountered from a cell with the robot facing a certain direction
def mark_obstacles(self, cell, dir):
h = cell[0]
w = cell[1]
if dir == 'N':
if self.readUSsensorValues('front') and (h - 1) >= 0:
self.map[h - 1][w].empty = False
if self.readUSsensorValues('right') and (w + 1) < self.WIDTH:
self.map[h][w + 1].empty = False
if self.readUSsensorValues('left') and (w - 1) >= 0:
self.map[h][w - 1].empty = False
elif dir == 'E':
if self.readUSsensorValues('front') and (w + 1) < self.WIDTH:
self.map[h][w + 1].empty = False
if self.readUSsensorValues('right') and (h + 1) < self.HEIGHT:
self.map[h + 1][w].empty = False
if self.readUSsensorValues('left') and (h - 1) >= 0:
self.map[h - 1][w].empty = False
elif dir == 'S':
if self.readUSsensorValues('front') and (h + 1) < self.HEIGHT:
self.map[h + 1][w].empty = False
if self.readUSsensorValues('right') and (w - 1) >= 0:
self.map[h][w - 1].empty = False
if self.readUSsensorValues('left') and (w + 1) < self.WIDTH:
self.map[h][w + 1].empty = False
elif dir == 'W':
if self.readUSsensorValues('front') and (w - 1) >= 0:
self.map[h][w - 1].empty = False
if self.readUSsensorValues('right') and (h - 1) >= 0:
self.map[h - 1][w].empty = False
if self.readUSsensorValues('left') and (h + 1) < self.HEIGHT:
self.map[h + 1][w].empty = False
#for a given cell and direction, this method returns all the free neigbours/ cell that are not visited
def getNeighborCells(self, cell, dir):
h = cell[0]
w = cell[1]
neigbours = []
if dir == 'N':
if (((h - 1) >= 0) and (not self.readUSsensorValues('front'))):
if (not self.map[h - 1][w].sensed):
neigbours.append((h - 1, w, 'N'))
if(((w + 1) < self.WIDTH) and (not self.readUSsensorValues('right'))):
if (not self.map[h][w + 1].sensed):
neigbours.append((h, w + 1, 'E'))
if(((w - 1) >= 0) and (not self.readUSsensorValues('left'))):
if (not self.map[h][w - 1].sensed):
neigbours.append((h, w - 1, 'W'))
elif dir == 'E':
if (((w + 1) < self.WIDTH) and (not self.readUSsensorValues('front'))):
if (not self.map[h][w + 1].sensed):
neigbours.append((h, w + 1, 'E'))
if(((h + 1) < self.HEIGHT) and (not self.readUSsensorValues('right'))):
if (not self.map[h + 1][w].sensed):
neigbours.append((h + 1, w, 'S'))
if(((h - 1) >= 0) and (not self.readUSsensorValues('left'))):
if (not self.map[h - 1][w].sensed):
neigbours.append((h - 1, w, 'N'))
elif dir == 'S':
if (((h + 1) < self.HEIGHT) and (not self.readUSsensorValues('front'))):
if (not self.map[h + 1][w].sensed):
neigbours.append((h + 1, w, 'S'))
if(((w - 1) >= 0) and (not self.readUSsensorValues('right'))):
if (not self.map[h][w - 1].sensed):
neigbours.append((h, w - 1, 'W'))
if(((w + 1) < self.WIDTH) and (not self.readUSsensorValues('left'))):
if (not self.map[h][w + 1].sensed):
neigbours.append((h, w + 1, 'E'))
elif dir == 'W':
if(((w - 1) >= 0) and (not self.readUSsensorValues('front'))):
if (not self.map[h][w - 1].sensed):
neigbours.append((h, w - 1, 'W'))
if (((h - 1) >= 0) and (not self.readUSsensorValues('right'))):
if (not self.map[h - 1][w].sensed):
neigbours.append((h - 1, w, 'N'))
if(((h + 1) < self.HEIGHT) and (not self.readUSsensorValues('left'))):
if (not self.map[h + 1][w].sensed):
neigbours.append((h + 1, w, 'S'))
return neigbours
#this method prints the representation of the map of the robot with
# 'FREE' for free cell
# 'NOOO' for occupied cell
def print_map(self):
for i in range(self.HEIGHT):
for f in range(self.WIDTH):
free = self.map[i][f].empty
if free:
print('FREE', end=' | ') # if free
else:
print('NOOO', end=' | ') #if occupied
print()
#This method marks a cell as visited/sensed
def markCellSensed(self, cell):
self.map[cell[0]][cell[1]].sensed = True
#display the map indicating visited an unvisited cells
def sensed_map(self):
for i in range(self.HEIGHT):
for r in range(self.WIDTH):
sensed = self.map[i][r].sensed
if sensed:
print("SENSED", end=' | ')
else:
print("NOOOON", end=' | ')
print()
'''
This function check the position and direction of the robot, compute it neighbours and make the necessary movement required
'''
def move(self, current_dir, goto_direction):
if (current_dir == 'N'):
if goto_direction == 'N':
#go straight
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
elif goto_direction == 'E':
#turn right
self.robot.turnRight(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'E'
elif goto_direction == 'W':
#turn left
self.robot.turnLeft(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'W'
elif current_dir == 'E':
if goto_direction == 'E':
#go straight
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
elif goto_direction == 'S':
#turn right
self.robot.turnRight(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'S'
elif goto_direction == 'N':
#turn left
self.robot.turnLeft(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'N'
elif current_dir == 'S':
if goto_direction == 'S':
#go straight
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
elif goto_direction == 'W':
#turn right
self.robot.turnRight(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'W'
elif goto_direction == 'E':
#turn left
self.robot.turnLeft(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'E'
elif current_dir == 'W':
if goto_direction == 'W':
#go straight
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
elif goto_direction == 'N':
#turn right
self.robot.turnRight(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'N'
elif goto_direction == 'S':
#turn left
self.robot.turnLeft(9.5, self.SPEED)
self.robot.moveStraight(self.CELL_SIZE, self.SPEED)
self.direction = 'S'
#This methods generates and draw the representation by the robot
def createMap(self):
start_cell = self.current_position
while (not self.robot.TOUCH_SENSOR.value()):
self.markCellSensed(self.current_position)
self.mark_obstacles(self.current_position, self.direction)
neighbours = self.getNeighborCells(self.current_position, self.direction)
if (len(neighbours) > 0):
neighbour = neighbours[0]
#move & update cell
cell = (neighbour[0], neighbour[1])
dir = neighbour[2]
#move
self.move(self.direction, dir)
time.sleep(2)
self.markCellSensed(cell)
self.current_position = cell
else:
print('Backtrack')
backtrack = False
h = self.current_position[0]
w = self.current_position[1]
if (self.direction == 'N'):
if (h + 1) < self.HEIGHT:
temp = (h + 1, w)
dir = 'S'
backtrack = True
elif(self.direction == 'E'):
if (w - 1) >= 0:
temp = (h, w - 1)
backtrack = True
dir = 'W'
elif (self.direction == 'S'):
if (h - 1) >= 0:
temp = (h - 1, w)
backtrack = True
dir = 'N'
elif (self.direction == 'W'):
if (w + 1) < self.WIDTH:
temp = (h, w + 1)
backtrack = True
dir = 'E'
if backtrack and self.map[temp[0]][temp[1]].empty:
self.robot.moveBackward(self.CELL_SIZE, self.SPEED)
#turnRight(19.2, SPEED) #turn 180
#moveStraight(CELL_SIZE, SPEED)
self.current_position = temp
#direction = dir
time.sleep(2)
else:
print('Thank you')
break
print('....................')
print('....................')
self.print_map()
#sensed_map()
print("Current Position " + str(self.current_position))
time.sleep(1)