-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSetter_sensors.py
151 lines (129 loc) · 5.18 KB
/
Setter_sensors.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
# ======================================================================================================================
# author: Xincong YANG
# date: 12 Oct. 2017
# email: [email protected]
# name: Setter_sensors
# ======================================================================================================================
from __future__ import division, print_function, absolute_import
import numpy as np
import csv
# sample config
SENSOR_TYPE1 = {'type': 'FL 2.8', 'min_rho': 2, 'max_rho': 4, 'theta': 70 * np.pi / 180,
'isOmini': 0, 'isThrough': 0, 'cost': 40}
SENSOR_TYPE2 = {'type': 'FL 2.8', 'min_rho': 2, 'max_rho': 4, 'theta': np.pi * 2,
'isOmini': 1, 'isThrough': 0, 'cost': 40}
TYPES = [SENSOR_TYPE1, SENSOR_TYPE2]
LOCATIONS = np.array([[1, 2], [2, 3]])
ALPHA_NUM = 8
class Sensor(object):
def __init__(self, sensor_type):
self.type = sensor_type['type']
self.min_rho = float(sensor_type['min_rho'])
self.max_rho = float(sensor_type['max_rho'])
self.theta = float(sensor_type['theta'])
self.isOmini = int(sensor_type['isOmini'])
self.isThrough = int(sensor_type['isThrough'])
self.cost = float(sensor_type['cost'])
assert self.isOmini == int(self.theta == np.pi * 2), "Please input correct sensor configuration"
self.FOD = [self.min_rho, self.max_rho]
def place(self, *args):
self.x = float(args[0])
self.y = float(args[1])
# if sensor is directional, orientation is required
if not self.isOmini:
self.alpha = float(args[2])
else:
self.alpha = 0
self.FOV = [self.alpha - self.theta / 2, self.alpha + self.theta / 2]
@property
def get_config(self):
config = {}
try:
# global parameters
config['type'] = self.type
config['min_rho'] = self.min_rho
config['max_rho'] = self.max_rho
config['theta'] = self.theta
config['isOmini'] = self.isOmini
config['isThrough'] = self.isThrough
config['cost'] = self.cost
# local parameters
config['x'] = self.x
config['y'] = self.y
config['alpha'] = self.alpha
return config
except:
raise Exception("Please place the sensor first!")
@property
def get_FOV(self):
return self.FOV
@property
def get_FOD(self):
return self.FOD
class Sensors(object):
def __init__(self):
self.sensors = []
def generate_configs(self, types, locations, alpha_num):
sensors = []
alphas = np.array([i * 2 * np.pi / alpha_num for i in range(alpha_num)])
for location in locations:
for type in types:
# if sensor is Omini
if int(type['isOmini']):
sensor = Sensor(type)
sensor.place(location[0], location[1])
sensors.append(sensor)
else:
for alpha in alphas:
sensor = Sensor(type)
sensor.place(location[0], location[1], alpha)
sensors.append(sensor)
self.sensors = sensors
print("==========>>> Generate %d sensor candidates <<<==========" % len(sensors))
def save_configs(self, configs_file='data/configs.csv'):
# the keys of config_file
fieldnames = ['type', 'min_rho', 'max_rho', 'theta', 'isOmini', 'isThrough', 'cost', 'x', 'y', 'alpha']
with open(configs_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for sensor in self.sensors:
dict = sensor.get_config
writer.writerow(dict)
print("==========>>> Save config file: {} <<<========== ".format(configs_file))
def load_configs(self, configs_file='data/configs.csv'):
sensors = []
with open(configs_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# initialize a sensor
sensor = Sensor(row)
# place the sensor
sensor.place(row['x'], row['y'], row['alpha'])
sensors.append(sensor)
self.sensors = sensors
print("==========>>> Load config file: {} <<<==========".format(configs_file))
def update(self, mask):
sensors = []
for i, sensor in enumerate(self.sensors):
if mask[i]:
sensors.append(sensor)
self.sensors = sensors
@property
def get_sensors(self):
return self.sensors
@property
def get_locations(self):
xs = [sensor.x for sensor in self.sensors]
ys = [sensor.y for sensor in self.sensors]
return np.array(xs), np.array(ys)
@property
def get_num(self):
return len(self.sensors)
@property
def get_costs(self):
costs = np.array([sensor.cost for sensor in self.sensors])
return costs
if __name__ == '__main__':
sensors = Sensors()
sensors.generate_configs(TYPES, LOCATIONS, ALPHA_NUM)
sensors.save_configs()