-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavData.py
121 lines (108 loc) · 3.07 KB
/
navData.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
import logging
import math
import json
ordered_list = [
(2,1), # 0
(2,2), # 45
(1,2), # 90
(0,2), # 135
(0,1), # 180
(0,0), # 225
(1,0), # 270
(2,0), # 315
]
ordered_navigation = [
(1,0), # 0
(1,1), # 45
(0,1), # 90
(-1,1), # 135
(-1,0), # 180
(-1,-1), # 225
(0,-1), # 270
(1,-1), # 315
]
# START Class Map ------------------------------------------------
class NavigationData:
def __init__(self):
self.size = 3
self.values = [[None for j in range(self.size)] for i in range(self.size)]
def get_data(self):
return self.values
def save_data(self, filename):
data = {
"values": self.values,
"size": self.size,
}
data2save = json.JSONEncoder().encode(data)
with open(filename, 'w') as json_file:
json.dump(data2save, json_file)
json_file.close()
def load_data(self, filename):
with open(filename) as json_data:
filedata = json.load(json_data)
data2load = json.JSONDecoder().decode(filedata)
self.values = data2load["values"]
self.size = data2load["size"]
def get_value(self, x, y):
i, j = self.dir2self((x, y))
if i < self.size and j < self.size and not(i==1 and j==1):
return self.values[i][j]
else:
return None
def set_value(self, x, y, value):
i, j = self.dir2self((x, y))
if i < self.size and j < self.size and not(i==1 and j==1):
self.values[i][j] = value
else:
logging.error("Bad navData x,y values: " + str((x, y)))
def get_iterative(self):
return ordered_navigation
def build_info(self, datafield, data, previous=None):
if previous == None:
previous = {
"blocked": None,
"limit_pos": None,
"pmap": None,
"known": None,
"known_dir": None,
"max_potential": None,
"max_potential_point": None,
"block_size": None,
"dist_to_goal": None,
}
if datafield == "blocked":
previous["blocked"] = data
if datafield == "limit_pos":
previous["limit_pos"] = data
if datafield == "pmap":
previous["pmap"] = data
if datafield == "known":
previous["known"] = data
if datafield == "known_dir":
previous["known_dir"] = data
if datafield == "max_potential":
previous["max_potential"] = data
if datafield == "max_potential_point":
previous["max_potential_point"] = data
if datafield == "block_size":
previous["block_size"] = data
if datafield == "dist_to_goal":
previous["dist_to_goal"] = data
return previous
def print(self):
logging.debug("navData:")
#for j in range(self.size):
# for i in range(self.size):
# if not(i==1 and j==1):
# logging.debug("\tx: " + str(i-1) + " | y: " + str(j-1) + " | dir: " + str(math.degrees(self.pangles(math.atan2(j-1,i-1)))) + " | data: " + str(self.values[i][j]))
for i,j in ordered_list:
logging.debug("\tx: " + str(i-1) + " | y: " + str(j-1) + " | dir: " + str(math.degrees(self.pangles(math.atan2(j-1,i-1)))) + " | data: " + str(self.values[i][j]))
# Using positive angles is more human readable
def pangles(self, ang):
if ang >= 0:
return ang
else:
return (2 * math.pi + ang)
# Internally we go from 0 to 2, but values go from -1 to 1
def dir2self(self, dirval):
return dirval[0]+1, dirval[1]+1