-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
394 lines (313 loc) · 12.1 KB
/
world.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import zmq
import math
import mine_layer
import time
from pyhop import *
import threading
class Vehicle:
"""
attributes for the underwater vehicle
"""
def __init__(self, cell, speed=0, direction=90, name="ship"):
# format : cx.y
self.cell = cell
self.speed = speed
self.direction = direction
self.name = name
class Mine:
"""
attributes for the mine detected
"""
def __init__(self, x, y, label):
self.x = x
self.y = y
self.label = label
class MoosWorld:
"""
This class interfaces with moos
"""
def __init__(self, state, init_enemy=False):
# pyhop state
self.world = state
# initialize the cells of the grid with their corresponding way_points
self.grid = self.calculate_grids()
# initialize the enemy behavior as a thread
if init_enemy:
enemy_laying_mines = threading.Thread(target=self.enemy_movement)
# initialize the socket connections
self.declare_connections()
# execute the thread
enemy_laying_mines.start()
def declare_connections(self):
"""
Establish the socket connections required with moos
:return:
"""
# Zeromq connection with the moos for perception
context = zmq.Context()
self.subscriber = context.socket(zmq.SUB)
self.subscriber_mine = context.socket(zmq.SUB)
self.subscriber.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber_mine.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber_mine.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber.setsockopt(zmq.CONFLATE, 1)
self.subscriber.connect("tcp://127.0.0.1:5563")
self.subscriber_mine.connect("tcp://127.0.0.1:5564")
# Zeromq connection with the moos for action
self.publisher = context.socket(zmq.PUB)
self.publisher_mine = context.socket(zmq.PUB)
self.publisher.bind("tcp://127.0.0.1:5560")
self.publisher_mine.connect("tcp://127.0.0.1:5565")
# Zeromq connection with the fisher (enemy) for action
self.publisher_enemy = context.socket(zmq.PUB)
self.publisher_enemy.bind("tcp://127.0.0.1:5592")
# Zeromq connection with the fisher (enemy) for perception
self.subscriber_enemy = context.socket(zmq.SUB)
self.subscriber_enemy.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber_enemy.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber_enemy.connect("tcp://127.0.0.1:5590")
# Zeromq connection with the fisher1 for perception
self.subscriber_fisher1 = context.socket(zmq.SUB)
self.subscriber_fisher1.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber_fisher1.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber_fisher1.connect("tcp://127.0.0.1:5593")
# Zeromq connection with the fisher2 for perception
self.subscriber_fisher2 = context.socket(zmq.SUB)
self.subscriber_fisher2.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber_fisher2.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber_fisher2.connect("tcp://127.0.0.1:5596")
# Zeromq connection with the fisher3 for perception
self.subscriber_fisher3 = context.socket(zmq.SUB)
self.subscriber_fisher3.setsockopt(zmq.SUBSCRIBE, '')
self.subscriber_fisher3.setsockopt(zmq.RCVTIMEO, 1)
self.subscriber_fisher3.connect("tcp://127.0.0.1:5599")
def calculate_grids(self, grid_size=20, distance=20):
"""
:param grid_size: dimensions of the grid
:param distance: distance between each cell
:return: returns the way_points associated with the grid
"""
# this is the center point of the first cell in the grid
start_point = [-131, 65]
# to store the way_points associtated with the cell position
grid = []
x = start_point[0] - distance
y = start_point[1] + distance
# loop for the vertical grid i.e c00, c10, c20 ect.
for i in range(0, grid_size):
y = y - distance
x = start_point[0] - distance
# loop for the horizontal grid i.e c00, c01, c02
row = []
for j in range(0, grid_size):
x = x + distance
row.append([x, y])
grid.append(row)
return grid
def calculate_cell(self, x, y):
"""
:return: the exact cell
"""
# calculates the euclidean distance between each cell and the vehicle coordinates
# for faster processing this should be replaced with binary search algorithm
for i in range(0, len(self.grid)):
for j in range(0, len(self.grid)):
euclidean_distance = math.sqrt((self.grid[i][j][0] - x) ** 2 + (self.grid[i][j][1] - y) ** 2)
if euclidean_distance < 10:
return "c" + str(i) + "." + str(j)
def update_agent(self, verbose=1):
"""
:return: the remus state from the moos
"""
try:
remus_position = self.subscriber.recv()
except:
if verbose > 1:
print ("connection not established .... Retrying ...")
return self.update_agent()
x, y, speed, direction = remus_position.split(",")
x = float(x.split(":")[1])
y = float(y.split(":")[1])
speed = float(speed.split(":")[1])
direction = float(direction.split(":")[1])
if self.world.agent:
self.world.agent.cell = self.calculate_cell(x, y)
self.world.agent.speed = speed
self.world.agent.direction = direction
else:
cell = self.calculate_cell(x, y)
remus = Vehicle(cell, speed, direction, "remus")
self.world.agent = remus
def set_vehicle_parameters(self, vehicle_pos, name):
"""
:param vehicle_pos: The string containing all the parameters of a vehicle
:param name: the name of the vehicle
:return: updates the parameters of the world
"""
x, y, speed, direction = vehicle_pos.split(",")
x = float(x.split(":")[1])
y = float(y.split(":")[1])
speed = float(speed.split(":")[1])
direction = float(direction.split(":")[1])
if name in self.world.vessels:
self.world.vessels[name].cell = self.calculate_cell(x, y)
self.world.vessels[name].speed = speed
self.world.vessels[name].direction = direction
else:
cell = self.calculate_cell(x, y)
vehicle = Vehicle(cell, speed, direction, name)
self.world.vessels[name] = vehicle
def get_vessel(self, subscriber, name):
"""
:param subscriber: subscriber for fisher vessel
:param name: name of the vessel
:return:
"""
try:
fisher_pos = self.subscriber.recv()
self.set_vehicle_parameters(fisher_pos, name)
except:
self.get_vessel(subscriber, name)
def update_vessels(self, verbose=1):
"""
:return: updating the dictionary with vessels
"""
self.get_vessel(self.subscriber_fisher1, "fisher1")
self.get_vessel(self.subscriber_fisher2, "fisher2")
self.get_vessel(self.subscriber_fisher3, "fisher3")
self.get_vessel(self.subscriber_enemy, "fisher4")
def check_if_new_mine(self, mine):
"""
:param mine: instance of the Mine class
:return: boolean : checks if it is the new mine
"""
for each in self.world.mines:
if each.label == mine.label:
return True
return False
def remove_mine(self, mine):
"""
:param mine: instance of the mine
:return: removes the mine from the dictionary
"""
for each in self.world.mines:
if each.label == mine.label:
self.world.mines.remove(each)
self.publisher_mine.send_multipart(
[b"M", str(mine.label)])
def update_mines(self):
"""
:return: Updates the dictionary if there are any mines
"""
try:
mine_report = self.subscriber_mine.recv()
mine_x, mine_y, mine_label = mine_report.split(":")[1].split(",")
mine_x = float(mine_x.split("=")[1])
mine_y = float(mine_y.split("=")[1])
mine_label = mine_label.split("=")[1]
new_mine = Mine(mine_x, mine_y, mine_label)
if self.check_if_new_mine(new_mine):
self.world.mines.append()
except:
pass
def update_world(self):
"""
:return: the updated state from the moos
"""
# updates the state of the remus
self.update_agent()
# updates the state of all the fisher vessels
self.update_vessels()
# if detected updates the state of the mines
self.update_mines()
def apply_agent_action(self, cell):
"""
:param cell: The location of the cell remus should go to
:return:
"""
# get the grid indexes from the cell
cell = cell.replace("c", "")
cell = cell.split(".")
row_index = int(cell[0])
column_index = int(cell[1])
point = self.grid[row_index][column_index]
message = [b"M", b"point = " + str(point[0]) + "," + str(point[1]) + "# speed= 1.0"]
time.sleep(0.1)
self.publisher.send_multipart(message)
time.sleep(0.1)
self.publisher.send_multipart(message)
def apply_enemy_action(self, cell):
"""
:param cell: The location of the cell enemy should go to
:return:
"""
# get the grid indexes from the cell
cell = cell.replace("c", "")
cell = cell.split(".")
row_index = int(cell[0])
column_index = int(cell[1])
point = self.grid[row_index][column_index]
message = [b"M", b"point = " + str(point[0]) + "," + str(point[1]) + "# speed= 1.0"]
time.sleep(0.1)
self.publisher_enemy.send_multipart(message)
time.sleep(0.1)
self.publisher_enemy.send_multipart(message)
def enemy_way_point_behavior(self, way_point):
"""
:param way_point: List of way_points enemy should travel
:return:
"""
for each in way_point:
time.sleep(0.1)
self.apply_enemy_action(each)
x=0
y=0
while not (self.calculate_cell(x, y) == each):
try:
enemy_pos = self.subscriber_enemy.recv()
x, y, speed, direction = enemy_pos.split(",")
x = float(x.split(":")[1])
y = float(y.split(":")[1])
except:
pass
def enemy_movement(self):
"""
The enemy movement and mine laying activity
:return:
"""
way_point = ["c2.19", "c3.19", "c4.19", "c4.18", "c4.17", "c4.16", "c5.16", "c5.15", "c5.14",
"c5.13", "c5.12", "c5.11", "c5.10"]
# travel to all the way_points (one by one)
self.enemy_way_point_behavior(way_point)
# lay mines
cell = way_point[-1]
cell = cell.replace("c", "")
cell = cell.split(".")
layer = mine_layer.Minelayer(self.grid[int(cell[0])][int(cell[1])])
layer.send_message()
# return to its original path
while True:
way_point.reverse()
self.enemy_way_point_behavior(way_point)
def main():
state = State('state')
state.vessels = {}
state.agent = {}
state.mines = []
# True parameter runs the thread for the movement of enemy
w = MoosWorld(state, True)
w.apply_agent_action("c5.8")
'''
To update the world (This updates dictionaries)
w.update_world()
To move remus
w.apply_agent_action(cx.y)
To remove mines
w.remove_mine(mine)
To adjust the way_points of the enemy vessel
modify the list way_point from the function enemy_movement
Note.. enemy_movement is a thread that runs when you initialize the MoosWorld
'''
if __name__ == '__main__':
main()