-
Notifications
You must be signed in to change notification settings - Fork 44
/
state_manager.py
324 lines (273 loc) · 11.9 KB
/
state_manager.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
import enabler_connection
import dynamics_model
import threading
import time
import datetime
import math
class StateManager(object):
def __init__(self):
self.stopped = False
self.connection = enabler_connection.EnablerConnection()
self.dynamics_model = dynamics_model.DynamicsModel()
self.headlamp = False
self.highbeams = False
self.wipers = False
self.door_status = {'driver':False, 'passenger':False,
'left_rear':False, 'right_rear':False}
self.start_send_loop(self.send_local_loop, "Send-Local-Thread")
self.data = []
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'steering_wheel_angle',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.1,
'rounding': 1})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'torque_at_transmission',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.1,
'rounding': 1})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'engine_speed',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.1,
'rounding': 1})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'vehicle_speed',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.01,
'rounding': 2})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'accelerator_pedal_position',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.1,
'rounding': 1})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'parking_brake_status',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': True,
'last_value': False,
'precision': 0})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'brake_pedal_status',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': True,
'last_value': False,
'precision': 0})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'transmission_gear_position',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': True,
'last_value': 'first',
'precision': 0})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'gear_lever_position',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': True,
'last_value': 'drive',
'precision': 0})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'odometer',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.002,
'rounding': 3})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'ignition_status',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': True,
'last_value': 'run',
'precision': 0})
period = datetime.timedelta(microseconds = 1000000/2) #2Hz
self.data.append({'name':'fuel_level',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 1,
'rounding': 0})
period = datetime.timedelta(microseconds = 1000000/10) #10Hz
self.data.append({'name':'fuel_consumed_since_restart',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.000001,
'rounding': 6})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'latitude',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.00001,
'rounding': 5})
period = datetime.timedelta(microseconds = 1000000/1) #1Hz
self.data.append({'name':'longitude',
'period': period,
'deadline': datetime.datetime.now() + period,
'fast_update': False,
'precision': 0.00001,
'rounding': 5})
self.start_send_loop(self.send_dynamics_loop, "Send-Dynamic-Thread")
print('State Manager initialized')
# Properties -------------------
@property
def steering_wheel_angle(self):
return self.dynamics_model.steering_wheel_angle
@steering_wheel_angle.setter
def steering_wheel_angle(self, value):
self.dynamics_model.steering_wheel_angle = value
@property
def accelerator_pedal_position(self):
return self.dynamics_model.accelerator
@accelerator_pedal_position.setter
def accelerator_pedal_position(self, value):
self.dynamics_model.accelerator = value
@property
def parking_brake_status(self):
return self.dynamics_model.parking_brake_status
@parking_brake_status.setter
def parking_brake_status(self, value):
self.dynamics_model.parking_brake_status = value
@property
def manual_trans_status(self):
return self.dynamics_model.manual_trans_status
@manual_trans_status.setter
def manual_trans_status(self, value):
self.dynamics_model.manual_trans_status = value
@property
def brake_pedal_position(self):
return self.dynamics_model.brake
@brake_pedal_position.setter
def brake_pedal_position(self, value):
self.dynamics_model.brake = value
@property
def ignition_status(self):
return self.dynamics_model.ignition_status
@ignition_status.setter
def ignition_status(self, value):
self.dynamics_model.ignition_status = value
@property
def gear_lever_position(self):
return self.dynamics_model.gear_lever_position
@gear_lever_position.setter
def gear_lever_position(self, value):
self.dynamics_model.gear_lever_position = value
@property
def headlamp_status(self):
return self.headlamp
@headlamp_status.setter
def headlamp_status(self, value):
if value != self.headlamp:
self.connection.send_measurement("headlamp_status", value)
self.headlamp = value
@property
def high_beam_status(self):
return self.highbeams
@high_beam_status.setter
def high_beam_status(self, value):
if value != self.highbeams:
self.connection.send_measurement("high_beam_status", value)
self.highbeams = value
@property
def windshield_wiper_status(self):
return self.wipers
@windshield_wiper_status.setter
def windshield_wiper_status(self, value):
if value != self.wipers:
self.connection.send_measurement("windshield_wiper_status", value)
self.wipers = value
@property
def local_ip(self):
return self.connection.local_ip
@property
def dynamics_data(self):
return self.dynamics_model.data
@property
def latitude(self):
return self.dynamics_model.snapshot['latitude']
@latitude.setter
def latitude(self, value):
self.dynamics_model.latitude = value
@property
def longitude(self):
return self.dynamics_model.snapshot['longitude']
@longitude.setter
def longitude(self, value):
self.dynamics_model.longitude = value
# Sending Data ------------------
def start_send_loop(self, function, thread_name):
t = threading.Thread(target=self.send_loop, args=[function],
name=thread_name)
t.setDaemon(True)
t.start()
def send_loop(self, function):
while True:
if not self.stopped:
function()
else:
time.sleep(0.5)
def update_signal(self, signal, snapshot):
value = snapshot[signal['name']]
if signal['fast_update']:
signal['last_value'] = value
if signal['precision'] != 0:
value = value - (value % signal['precision'])
value = round(value, signal['rounding'])
self.connection.send_measurement(signal['name'],value)
def send_dynamics_loop(self):
snapshot = self.dynamics_model.snapshot
now = datetime.datetime.now()
for signal in self.data:
if now > signal['deadline']:
self.update_signal(signal, snapshot)
signal['deadline'] = now + signal['period']
elif signal['fast_update']:
if snapshot[signal['name']] != signal['last_value']:
self.update_signal(signal, snapshot)
time.sleep(0.01)
def send_local_loop(self):
self.connection.send_measurement("headlamp_status",
self.headlamp)
self.connection.send_measurement("high_beam_status",
self.highbeams)
self.connection.send_measurement("windshield_wiper_status",
self.wipers)
for door in self.door_status:
self.connection.send_measurement("door_status", door, self.door_status[door])
time.sleep(1.0)
def pause(self):
self.stopped = True
self.dynamics_model.stopped = True
def resume(self):
self.dynamics_model.stopped = False
self.stopped = False
def update_once(self):
self.connection.send_measurement("steering_wheel_angle",
self.steering_wheel_angle)
def send_callback(self, data_name, value, event=None):
self.connection.send_measurement(data_name, value, event)
def send_json(self, payload):
self.connection.send_json(payload)
def update_door(self, door, value):
self.door_status[door] = value
self.connection.send_measurement("door_status", door, value)
def received_messages(self):
return self.connection.received_messages()
def upshift(self):
self.dynamics_model.upshift();
def downshift(self):
self.dynamics_model.downshift();