-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
215 lines (133 loc) · 6.09 KB
/
Copy pathcontroller.py
File metadata and controls
215 lines (133 loc) · 6.09 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
import pandas as pd
import numpy as np
import time
class Status:
def __init__(self,*args, **kwargs):
self._last = {}
self._current = {}
self._new = {}
# initialize the parameter. The initial values are put under "last status"
for item in args:
if isinstance(item, tuple) and len(item) == 2:
self._last[item[0]] = item[1]
else:
self._last[item] = None
def update(self, attr, val):
"""
Update the value of an attribute. The updated values are stored in "new" attribute.
"""
self._new[attr] = val
def compute_current_param(self):
"""
Use information in "new" section and "last" section to compute the "current" status.
User can define his own approach to estimate the current status.
"""
pass
def rotate(self):
"""
Rotate function will rotate the status. More specifically, current becomes last.
"""
self._last = self._current
@property
def last(self):
return self._last
@property
def current(self):
return self._current
@property
def new(self):
return self._new
class RawDataHandler:
def __init__(self, name=None, parser=None, record_size=100):
assert name is not None and parser is not None
self._name = name
self._records = []
self._record_size = record_size
self._columns = parser
def update(self, msg):
if len(self._records) == self._record_size:
self._records.pop(0)
# parse the msg
self._records.append(list(msg))
@property
def data(self):
df = pd.DataFrame(self._records, columns=self._columns)
df['DataHandlerName'] = self._name
return df
T_FACTOR = 100
CN_DISTANCE = 'distance'
CN_STATUS = 'status'
CN_POS = 'pos'
def format_timestamp(ts,factor=T_FACTOR):
return (ts * factor).astype(np.int64)
def create_distance_map(df_radar_base, df_distance_sensor):
"""
Create the distance map. The map represents the environment in front of the robot. It is the association between position of
radar (in degree) and the distance detected in that position.
Args:
df_radar_base: data sent from distance radar component.
df_distance_sensor: data sent from the distance radar sensor component.
Return:
A pd.Series. The index is the position of the radar base and the value is the distance deteced.
Note:
The map is discrete. The value of index in the retured Series is integer.
"""
df_sensor = df_distance_sensor
df_base = df_radar_base
df_sensor['timestamp'] = format_timestamp(df_sensor['timestamp'])
df_base['timestamp'] = format_timestamp(df_base['timestamp'])
# print('+++++ sensor_min: {}, sensor_max: {}, base_min: {}, base_max: {}'.format(
# df_sensor['timestamp'].min(), df_sensor['timestamp'].max(),
# df_base['timestamp'].min(), df_base['timestamp'].max()))
df = pd.merge(df_sensor, df_base, on='timestamp', how='outer', suffixes=('_sensor','_base')).sort_values('timestamp')
# smooth the distance.
# window = int(df.shape[0] / df['pos'].notnull().sum())
# df['avg_distance'] = df['distance'].rolling(window=window, min_periods=1).mean()
# df['distance'] = df['distance'].combine_first(df['avg_distance'])
# df = df.drop('avg_distance', axis=1)
# clean the data
df['distance'] = df['distance'].fillna(method='ffill')
df['status'] = df['status'].fillna(method='ffill')
df['pos'] = df['pos'].fillna(method='ffill')
df_work = df[(df['pos'].notnull()) & (df['distance'].notnull())]
# select useful information
df_work = df_work[['pos','timestamp','distance', 'status']]
# create the distance map
df_work['pos_bin'] = df_work['pos'].astype(int)
df_work = df_work.groupby(by=['pos_bin','timestamp'], as_index=False)['distance'].mean()
df_work = df_work.sort_values(['pos_bin','timestamp'])
# ts_distance_map = df_work.groupby(by='pos_bin')['distance'].mean().sort_index()
# ts_distance_map = df_work.groupby(by='pos_bin')['distance'].agg(lambda x: x.values[-1])
ts_distance_map = df_work.groupby(by='pos_bin').apply(lambda x: x.loc[ (x['timestamp'] == x['timestamp'].max()), 'distance'].mean())
return ts_distance_map
def retrieve_data(Q, data_handler):
while not Q.empty():
msg = Q.get()
data_handler.update(msg)
class Controller:
def __init__(self, radar_base=None, radar_distance_sensor=None, engine=None):
assert isinstance(radar_base, ContinuousComponentWrapper)
assert isinstance(radar_distance_sensor, ContinuousComponentWrapper)
assert isinstance(engine, Engine)
self._radar_base = radar_base
self._radar_distance_sensor = radar_distance_sensor
self._engine = engine
self._components = ['radar_base','radar_distance_sensor','engine']
self._comp_output_Q = {}
self._comp_output_Q['radar_base'] = self._radar_base.output_Q
self._comp_output_Q['radar_distance_sensor'] = selef._radar_distance_sensor.output_Q
self._data_handler = {}
self._data_handler['radar_base'] = RawDataHandler(name=self._radar_base.name, parser=self._radar_base.FORMAT, record_size=500)
self._data_handler['radar_distance_sensor'] = RawDataHandler(name=self._radar_distance_sensor.name,parser=self._radar_distance_sensor.FORMAT, record_size=500)
def run(self):
"""
The controller will analyze the data received from different components, make decisions to adjustment the status of
the robot and send back commands to each component.
"""
while True:
# retrieve data from different component
for item in self._components:
retrieve_data(self._comp_output_Q[item], self._data_handler[item])
# craete the distance map
ts_distance_map = create_distance_map(df_radar_base, df_radar_distance_sensor)
# analyze