-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathatomic_scenario_behavior.py
571 lines (433 loc) · 17.8 KB
/
atomic_scenario_behavior.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/usr/bin/env python
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""
This module provides all atomic scenario behaviors required to realize
complex, realistic scenarios such as "follow a leading vehicle", "lane change",
etc.
The atomic behaviors are implemented with py_trees.
"""
import py_trees
import carla
from ScenarioManager.carla_data_provider import CarlaDataProvider
EPSILON = 0.001
def calculate_distance(location, other_location):
"""
Method to calculate the distance between to locations
Note: It uses the direct distance between the current location and the
target location to estimate the time to arrival.
To be accurate, it would have to use the distance along the
(shortest) route between the two locations.
"""
return location.distance(other_location)
class AtomicBehavior(py_trees.behaviour.Behaviour):
"""
Base class for all atomic behaviors used to setup a scenario
Important parameters:
- name: Name of the atomic behavior
"""
def __init__(self, name):
super(AtomicBehavior, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self.name = name
def setup(self, unused_timeout=15):
self.logger.debug("%s.setup()" % (self.__class__.__name__))
return True
def initialise(self):
self.logger.debug("%s.initialise()" % (self.__class__.__name__))
def terminate(self, new_status):
self.logger.debug("%s.terminate()[%s->%s]" % (
self.__class__.__name__, self.status, new_status))
class InTriggerRegion(AtomicBehavior):
"""
This class contains the trigger region (condition) of a scenario
"""
def __init__(self, vehicle, min_x, max_x, min_y,
max_y, name="TriggerRegion"):
"""
Setup trigger region (rectangle provided by
[min_x,min_y] and [max_x,max_y]
"""
super(InTriggerRegion, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._vehicle = vehicle
self._min_x = min_x
self._max_x = max_x
self._min_y = min_y
self._max_y = max_y
def update(self):
"""
Check if the _vehicle location is within trigger region
"""
new_status = py_trees.common.Status.RUNNING
location = CarlaDataProvider.get_location(self._vehicle)
if location is None:
return new_status
not_in_region = (location.x < self._min_x or location.x > self._max_x) or (
location.y < self._min_y or location.y > self._max_y)
if not not_in_region:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class InTriggerDistanceToVehicle(AtomicBehavior):
"""
This class contains the trigger distance (condition) between to vehicles
of a scenario
"""
def __init__(self, other_vehicle, ego_vehicle, distance,
name="TriggerDistanceToVehicle"):
"""
Setup trigger distance
"""
super(InTriggerDistanceToVehicle, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._other_vehicle = other_vehicle
self._ego_vehicle = ego_vehicle
self._distance = distance
def update(self):
"""
Check if the ego _vehicle is within trigger distance to other _vehicle
"""
new_status = py_trees.common.Status.RUNNING
ego_location = CarlaDataProvider.get_location(self._ego_vehicle)
other_location = CarlaDataProvider.get_location(self._other_vehicle)
if ego_location is None or other_location is None:
return new_status
if calculate_distance(ego_location, other_location) < self._distance:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class InTriggerDistanceToLocation(AtomicBehavior):
"""
This class contains the trigger (condition) for a distance to a fixed
location of a scenario
"""
def __init__(self, vehicle, target_location, distance,
name="InTriggerDistanceToLocation"):
"""
Setup trigger distance
"""
super(InTriggerDistanceToLocation, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._target_location = target_location
self._vehicle = vehicle
self._distance = distance
def update(self):
"""
Check if the _vehicle is within trigger distance to the target location
"""
new_status = py_trees.common.Status.RUNNING
location = CarlaDataProvider.get_location(self._vehicle)
if location is None:
return new_status
if calculate_distance(location, self._target_location) < self._distance:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class TriggerVelocity(AtomicBehavior):
"""
This class contains the trigger velocity (condition) of a scenario
"""
def __init__(self, vehicle, target_velocity, name="TriggerVelocity"):
"""
Setup trigger velocity
"""
super(TriggerVelocity, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._vehicle = vehicle
self._target_velocity = target_velocity
def update(self):
"""
Check if the _vehicle has the trigger velocity
"""
new_status = py_trees.common.Status.RUNNING
delta_velocity = CarlaDataProvider.get_velocity(
self._vehicle) - self._target_velocity
if delta_velocity < EPSILON:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class InTimeToArrivalToLocation(AtomicBehavior):
"""
This class contains a check if a vehicle arrives within a given time
at a given location.
"""
_max_time_to_arrival = float('inf') # time to arrival in seconds
def __init__(self, vehicle, time, location, name="TimeToArrival"):
"""
Setup parameters
"""
super(InTimeToArrivalToLocation, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._vehicle = vehicle
self._time = time
self._target_location = location
def update(self):
"""
Check if the _vehicle can arrive at target_location within time
"""
new_status = py_trees.common.Status.RUNNING
current_location = CarlaDataProvider.get_location(self._vehicle)
if current_location is None:
return new_status
distance = calculate_distance(current_location, self._target_location)
velocity = CarlaDataProvider.get_velocity(self._vehicle)
# if velocity is too small, simply use a large time to arrival
time_to_arrival = self._max_time_to_arrival
if velocity > EPSILON:
time_to_arrival = distance / velocity
if time_to_arrival < self._time:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class InTimeToArrivalToVehicle(AtomicBehavior):
"""
This class contains a check if a vehicle arrives within a given time
at another vehicle.
"""
_max_time_to_arrival = float('inf') # time to arrival in seconds
def __init__(self, other_vehicle, ego_vehicle, time, name="TimeToArrival"):
"""
Setup parameters
"""
super(InTimeToArrivalToVehicle, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._other_vehicle = other_vehicle
self._ego_vehicle = ego_vehicle
self._time = time
def update(self):
"""
Check if the ego _vehicle can arrive at other vehicle within time
"""
new_status = py_trees.common.Status.RUNNING
current_location = CarlaDataProvider.get_location(self._ego_vehicle)
target_location = CarlaDataProvider.get_location(self._other_vehicle)
if current_location is None or target_location is None:
return new_status
distance = calculate_distance(current_location, target_location)
current_velocity = CarlaDataProvider.get_velocity(self._ego_vehicle)
other_velocity = CarlaDataProvider.get_velocity(self._other_vehicle)
# if velocity is too small, simply use a large time to arrival
time_to_arrival = self._max_time_to_arrival
if current_velocity > other_velocity:
time_to_arrival = 2 * distance / \
(current_velocity - other_velocity)
if time_to_arrival < self._time:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class AccelerateToVelocity(AtomicBehavior):
"""
This class contains an atomic acceleration behavior. The controlled
traffic participant will accelerate with _throttle_value_ until reaching
a given _target_velocity_
"""
def __init__(self, vehicle, throttle_value,
target_velocity, name="Acceleration"):
"""
Setup parameters including acceleration value (via throttle_value)
and target velocity
"""
super(AccelerateToVelocity, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._control = carla.VehicleControl()
self._vehicle = vehicle
self._throttle_value = throttle_value
self._target_velocity = target_velocity
def update(self):
"""
Set throttle to throttle_value, as long as velocity is < target_velocity
"""
new_status = py_trees.common.Status.RUNNING
if CarlaDataProvider.get_velocity(self._vehicle) < self._target_velocity:
self._control.throttle = self._throttle_value
else:
new_status = py_trees.common.Status.SUCCESS
self._control.throttle = 0
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
self._vehicle.apply_control(self._control)
return new_status
class KeepVelocity(AtomicBehavior):
"""
This class contains an atomic behavior to keep the provided velocity.
The controlled traffic participant will accelerate as fast as possible
until reaching a given _target_velocity_, which is then maintained for
as long as this behavior is active.
Note: In parallel to this behavior a termination behavior has to be used
to keep the velocity either for a certain duration, or for a certain
distance, etc.
"""
def __init__(self, vehicle, target_velocity, name="KeepVelocity"):
"""
Setup parameters including acceleration value (via throttle_value)
and target velocity
"""
super(KeepVelocity, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._control = carla.VehicleControl()
self._vehicle = vehicle
self._target_velocity = target_velocity
def update(self):
"""
Set throttle to throttle_value, as long as velocity is < target_velocity
"""
new_status = py_trees.common.Status.RUNNING
if CarlaDataProvider.get_velocity(self._vehicle) < self._target_velocity:
self._control.throttle = 1.0
else:
self._control.throttle = 0.0
self._vehicle.apply_control(self._control)
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
def terminate(self, new_status):
"""
On termination of this behavior, the throttle should be set back to 0.,
to avoid further acceleration.
"""
self._control.throttle = 0.0
self._vehicle.apply_control(self._control)
super(KeepVelocity, self).terminate(new_status)
class DriveDistance(AtomicBehavior):
"""
This class contains an atomic behavior to drive a certain distance.
"""
def __init__(self, vehicle, distance, name="DriveDistance"):
"""
Setup parameters
"""
super(DriveDistance, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._target_distance = distance
self._distance = 0
self._location = None
self._vehicle = vehicle
def initialise(self):
self._location = CarlaDataProvider.get_location(self._vehicle)
super(DriveDistance, self).initialise()
def update(self):
"""
Check driven distance
"""
new_status = py_trees.common.Status.RUNNING
new_location = CarlaDataProvider.get_location(self._vehicle)
self._distance += calculate_distance(self._location, new_location)
self._location = new_location
if self._distance > self._target_distance:
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class UseAutoPilot(AtomicBehavior):
"""
This class contains an atomic behavior to use the auto pilot.
Note: In parallel to this behavior a termination behavior has to be used
to terminate this behavior after a certain duration, or after a
certain distance, etc.
"""
def __init__(self, vehicle, name="UseAutoPilot"):
"""
Setup parameters
"""
super(UseAutoPilot, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._vehicle = vehicle
def update(self):
"""
Activate autopilot
"""
new_status = py_trees.common.Status.RUNNING
self._vehicle.set_autopilot(True)
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
def terminate(self, new_status):
"""
Deactivate autopilot
"""
self._vehicle.set_autopilot(False)
super(UseAutoPilot, self).terminate(new_status)
class StopVehicle(AtomicBehavior):
"""
This class contains an atomic stopping behavior. The controlled traffic
participant will decelerate with _bake_value_ until reaching a full stop.
"""
def __init__(self, vehicle, brake_value, name="Stopping"):
"""
Setup _vehicle and maximum braking value
"""
super(StopVehicle, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._control = carla.VehicleControl()
self._vehicle = vehicle
self._brake_value = brake_value
def update(self):
"""
Set brake to brake_value until reaching full stop
"""
new_status = py_trees.common.Status.RUNNING
if CarlaDataProvider.get_velocity(self._vehicle) > EPSILON:
self._control.brake = self._brake_value
else:
new_status = py_trees.common.Status.SUCCESS
self._control.brake = 0
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
self._vehicle.apply_control(self._control)
return new_status
class SteerVehicle(AtomicBehavior):
"""
This class contains an atomic steer behavior.
To set the steer value of the vehicle.
"""
def __init__(self, vehicle, steer_value, name="Steering"):
"""
Setup vehicle and maximum steer value
"""
super(SteerVehicle, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self.control = carla.VehicleControl()
self.vehicle = vehicle
self.steer_value = steer_value
def update(self):
"""
Set steer to steer_value until reaching full stop
"""
self.control.steer = self.steer_value
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
self.vehicle.apply_control(self.control)
return new_status
class SteerVehicle(AtomicBehavior):
"""
This class contains an atomic steer behavior.
To set the steer value of the vehicle.
"""
def __init__(self, vehicle, steer_value, name="Steering"):
"""
Setup vehicle and maximum steer value
"""
super(SteerVehicle, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self.control = carla.VehicleControl()
self.vehicle = vehicle
self.steer_value = steer_value
def update(self):
"""
Set steer to steer_value until reaching full stop
"""
self.control.steer = self.steer_value
new_status = py_trees.common.Status.SUCCESS
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
self.vehicle.apply_control(self.control)
return new_status