-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagWorldDemov1.py
181 lines (143 loc) · 5.58 KB
/
TagWorldDemov1.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
import socket
import time
class TagWorld():
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(('127.0.0.1', 5700))
self.sock.settimeout(1)
def runSim(self):
self.sock.send(str.encode(str('start')))
def endSim(self):
self.sock.send(str.encode(str('quit')))
def getAdjacent(self, position):
msg = "get_tags_adjacent," + str(position[0]+1) + "," + str(position[1]+1)
self.sock.send(str.encode(str(msg)))
msg = self.sock.recv(2048)
data = msg.decode('utf-8')
data = data.split(",")
data = [float(each) for each in data]
return data
def UpdateUncertainity(self, mode, radius):
msg = "updateUncertainity," + str(mode) + "," + str(radius)
self.sock.send(str.encode(str(msg)))
def UpdateSurfstatus(self, mode):
msg = "ascendordescend," + str(mode)
self.sock.send(str.encode(str(msg)))
# initial_position and final_position is a grid cell of format [x,y]
# template to move from one cell to another
def move_cell(self, initial_position, final_destination):
msg = "moveTo," + str(final_destination[0]+1) + "," + str(final_destination[1]+1)
self.sock.send(str.encode(str(msg)))
# make the movement from initial_position to the final destination
# return acknowledgment once it moved to the destination grid cell
return True # boolean value; true if reached
# position = [x, y] # x and y are the coordinates of the grid cell
def in_cell(self, position):
msg = "inCell," + str(position[0]) + "," + str(position[1])
self.sock.send(str.encode(str(msg)))
msg = self.sock.recv(2048)
return msg.decode('utf-8')
def get_cell(self):
try:
msg = "getCell"
self.sock.send(str.encode(str(msg)))
msg = self.sock.recv(2048)
position = msg.decode('utf-8')
# convert to midca coordinates
# for example [2,2] is [1,1] in our representation
if position:
position = position.split(",")
position = [str(int(pos)-1) for pos in position]
position = ",".join(position)
return position
except Exception as e:
print e
# position = [x, y] # x and y are the coordinates of the grid cell
def search_and_get_tags2(self, position):
# search the location (grid cell [x,y])
if self.searchComplete() == 'True':
return self.get_tags() # no_of_tags
else:
self.search(position)
def search_and_get_tags(self, position):
# search the location (grid cell [x,y])
self.search(position)
complete = 'False'
while complete == 'False':
complete = self.searchComplete()
# collect tags
print(complete)
return self.get_tags() # no_of_tags
# position = [x, y] # x and y are the coordinates of the grid cell
def search(self, position):
msg = "search," + str(position[0]+1) + "," + str(position[1]+1)
self.sock.send(str.encode(str(msg)))
def send(self, position):
msg = str(position[0]+1) + "," + str(position[1]+1)
self.sock.send(str.encode(str(msg)))
# search the location (grid cell [x,y])
# display the movement of the agent in the cell
def searchComplete(self):
self.sock.send(str.encode('searchComplete'))
msg = self.sock.recv(2048)
return msg.decode('utf-8')
def get_tags(self, position):
try:
msg = "get_tags," + str(position[0]+1) + "," + str(position[1]+1)
self.sock.send(str.encode(str(msg)))
# collect tags
msg = self.sock.recv(2048)
return int(msg.decode('utf-8'))
except:
return False
def get_measurement(self, position=None):
try:
msg = "get_measurement"
self.sock.send(str.encode(str(msg)))
# collect tags
msg = self.sock.recv(2048)
return msg.decode('utf-8')
except:
return False
def get_cell_poisson_rate(self, pos=None):
msg = "cell_lambda"
if type(pos) != type(None):
msg += "," + str(pos[0]+1) + "," + str(pos[1]+1)
self.sock.send(str.encode(str(msg)))
# collect tags
msg = self.sock.recv(1024)
return float(msg.decode('utf-8'))
def simtime(self):
try:
msg = "time"
self.sock.send(str.encode(str(msg)))
msg = self.sock.recv(2048)
time = msg.decode('utf-8')
return float(time)
except Exception as e:
print e
def close(self):
self.sock.shutdown(1)
self.sock.close()
if __name__ == "__main__":
""""
tag = TagWorld()
tag.runSim()
tag = TagWorld()
tag.move_cell([0,0], [1,3])
"""
tag = TagWorld()
tag.runSim()
tag = TagWorld()
tag.search([0,1])
tag = TagWorld()
print (tag.get_measurement())
#print (tag.simtime())
#tag.search([0, 3])
#print tag.get_cell_poisson_rate()
#print tag.get_tags()
#tag.send([3,3])
#print (msg)
#tag.UpdateUncertainity("High", 100)
#tag.UpdateUncertainity("Med", 50)
#tag.UpdateSurfstatus("ascend")