-
Notifications
You must be signed in to change notification settings - Fork 1
/
behaviourial_fsm.py
506 lines (441 loc) · 25.9 KB
/
behaviourial_fsm.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env python3
import numpy as np
from fsm.mealy import FSM
# State machine states
FOLLOW_LANE = 'Follow Lane'
DECELERATE_TO_STOP = 'Decelerate to Stop'
# Stop speed threshold
STOP_THRESHOLD = 1.5 # m/s
# Enumerate Trafficlight State
TRAFFICLIGHT_GREEN = 0
TRAFFICLIGHT_YELLOW = 1
TRAFFICLIGHT_RED = 2
TRAFFICLIGHT_YELLOW_MIN_TIME = 1 # sec
"""In this file we implemented an FSM for the behavioural planner."""
def get_fsm():
"""Return an istance of the FSM as described in the Paragraph 2.2 of the report.
"""
# --------- Defines the action functions for the fsm -------------
def t_L00(d):
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_L01(d):
lead_vehicle = d['vehicles'][0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_V00(d):
lead_vehicle = d['vehicles'][0]
d['goal_state'] = d['waypoints'][lead_vehicle[0]]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_T00(d):
traffic_light = d['traffic_lights'][0]
d['goal_index'] = traffic_light[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_T01(d):
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = traffic_light[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_T10(d):
traffic_light = d['traffic_lights'][0]
d['goal_index'] = traffic_light[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_T11(d):
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = traffic_light[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_P00(d):
pedestrian = d['pedestrians'][0]
d['goal_index'] = pedestrian[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_P01(d):
pedestrian = d['pedestrians'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = pedestrian[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_P10(d):
pedestrian = d['pedestrians'][0]
d['goal_index'] = pedestrian[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_P11(d):
pedestrian = d['pedestrians'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = pedestrian[0]
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mtp00(d):
pedestrian = d['pedestrians'][0]
traffic_light = d['traffic_lights'][0]
d['goal_index'] = np.min((traffic_light[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mtp01(d):
pedestrian = d['pedestrians'][0]
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = np.min((traffic_light[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mtp10(d):
pedestrian = d['pedestrians'][0]
traffic_light = d['traffic_lights'][0]
d['goal_index'] = np.min((traffic_light[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mtp11(d):
pedestrian = d['pedestrians'][0]
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = np.min((traffic_light[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mtv00(d):
lead_vehicle = d['vehicles'][0]
traffic_light = d['traffic_lights'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mtv01(d):
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mtv10(d):
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mtv11(d):
traffic_light = d['traffic_lights'][0]
lead_vehicle = d['vehicles'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mvp00(d):
lead_vehicle = d['vehicles'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((pedestrian[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mvp01(d):
lead_vehicle = d['vehicles'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((pedestrian[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_Mvp10(d):
lead_vehicle = d['vehicles'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((pedestrian[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_Mvp11(d):
lead_vehicle = d['vehicles'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((pedestrian[0], lead_vehicle[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_M00(d):
lead_vehicle = d['vehicles'][0]
traffic_light = d['traffic_lights'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_M01(d):
lead_vehicle = d['vehicles'][0]
traffic_light = d['traffic_lights'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
def t_M10(d):
lead_vehicle = d['vehicles'][0]
traffic_light = d['traffic_lights'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = False
d['lead_car_state'] = None
def t_M11(d):
lead_vehicle = d['vehicles'][0]
traffic_light = d['traffic_lights'][0]
pedestrian = d['pedestrians'][0]
d['goal_index'] = np.min((traffic_light[0], lead_vehicle[0], pedestrian[0]))
d['goal_state'] = d['waypoints'][d['goal_index']]
d['goal_state'][2] = 0
d['follow_lead_vehicle'] = True
d['lead_car_state'] = [*lead_vehicle[1][0:2], lead_vehicle[2]]
# ---------- Define conditions for the transition --------------
def fl_fl_L00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
t1 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_fl_L01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_dts_V00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_dts_T00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
t1 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def fl_dts_T01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def fl_dts_P00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
t1 = d['pedestrian_presence'] and (not d['vehicle_presence']) and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_dts_P01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_dts_Mtp00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
t1 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def fl_dts_Mtp01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def fl_dts_Mtv00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def fl_dts_Mvp00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
t3 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and can_pass
return t1 or t2 or t3
def fl_dts_M00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
can_pass = (d['closed_loop_speed'] != 0) and (traffic_light[2] / d['closed_loop_speed'] < TRAFFICLIGHT_YELLOW_MIN_TIME)
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW) and (not can_pass)
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_fl_L00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
t1 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_fl_L01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_dts_V00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and (not d['traffic_light_presence'])
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_dts_T00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
t1 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = (not d['pedestrian_presence']) and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_dts_T01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_dts_P00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
t1 = d['pedestrian_presence'] and (not d['vehicle_presence']) and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_dts_P01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_dts_Mtp00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
t1 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = d['pedestrian_presence'] and (not d['vehicle_presence']) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_dts_Mtp01(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and (not lead_stopped) and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_dts_Mtv00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = (not d['pedestrian_presence']) and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
def dts_dts_Mvp00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and (not d['traffic_light_presence'])
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_GREEN)
return t1 or t2
def dts_dts_M00(d):
if d['traffic_light_presence']:
traffic_light = d['traffic_lights'][0]
if d['vehicle_presence']:
lead_stopped = d['vehicles'][0][2] <= STOP_THRESHOLD
t1 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_YELLOW)
t2 = d['pedestrian_presence'] and d['vehicle_presence'] and lead_stopped and d['traffic_light_presence'] and (traffic_light[1] == TRAFFICLIGHT_RED)
return t1 or t2
# --------- Define the FSM -----------------
fsm = FSM(True)
# Add states
fsm.add_state(FOLLOW_LANE)
fsm.add_state(DECELERATE_TO_STOP)
fsm.set_initial_state(FOLLOW_LANE)
# Add transition
fsm.add_transition(FOLLOW_LANE, fl_fl_L00, FOLLOW_LANE, action=t_L00)
fsm.add_transition(FOLLOW_LANE, fl_fl_L01, FOLLOW_LANE, action=t_L01)
fsm.add_transition(FOLLOW_LANE, fl_dts_V00, DECELERATE_TO_STOP, action=t_V00)
fsm.add_transition(FOLLOW_LANE, fl_dts_T00, DECELERATE_TO_STOP, action=t_T00)
fsm.add_transition(FOLLOW_LANE, fl_dts_T01, DECELERATE_TO_STOP, action=t_T01)
fsm.add_transition(FOLLOW_LANE, fl_dts_P00, DECELERATE_TO_STOP, action=t_P00)
fsm.add_transition(FOLLOW_LANE, fl_dts_P01, DECELERATE_TO_STOP, action=t_P01)
fsm.add_transition(FOLLOW_LANE, fl_dts_Mtp00, DECELERATE_TO_STOP, action=t_Mtp00)
fsm.add_transition(FOLLOW_LANE, fl_dts_Mtp01, DECELERATE_TO_STOP, action=t_Mtp01)
fsm.add_transition(FOLLOW_LANE, fl_dts_Mtv00, DECELERATE_TO_STOP, action=t_Mtv00)
fsm.add_transition(FOLLOW_LANE, fl_dts_Mvp00, DECELERATE_TO_STOP, action=t_Mvp00)
fsm.add_transition(FOLLOW_LANE, fl_dts_M00, DECELERATE_TO_STOP, action=t_M00)
fsm.add_transition(DECELERATE_TO_STOP, dts_fl_L00, FOLLOW_LANE, action=t_L00)
fsm.add_transition(DECELERATE_TO_STOP, dts_fl_L01, FOLLOW_LANE, action=t_L01)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_V00, DECELERATE_TO_STOP, action=t_V00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_T00, DECELERATE_TO_STOP, action=t_T00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_T01, DECELERATE_TO_STOP, action=t_T01)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_P00, DECELERATE_TO_STOP, action=t_P00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_P01, DECELERATE_TO_STOP, action=t_P01)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_Mtp00, DECELERATE_TO_STOP, action=t_Mtp00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_Mtp01, DECELERATE_TO_STOP, action=t_Mtp01)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_Mtv00, DECELERATE_TO_STOP, action=t_Mtv00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_Mvp00, DECELERATE_TO_STOP, action=t_Mvp00)
fsm.add_transition(DECELERATE_TO_STOP, dts_dts_M00, DECELERATE_TO_STOP, action=t_M00)
return fsm