-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathatomic_scenario_criteria.py
371 lines (278 loc) · 10.7 KB
/
atomic_scenario_criteria.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
#!/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 evaluation criteria required to analyze if a
scenario was completed successfully or failed.
The atomic criteria are implemented with py_trees.
"""
import weakref
import py_trees
import carla
from ScenarioManager.carla_data_provider import CarlaDataProvider
from ScenarioManager.timer import GameTime
class Criterion(py_trees.behaviour.Behaviour):
"""
Base class for all criteria used to evaluate a scenario for success/failure
Important parameters (PUBLIC):
- name: Name of the criterion
- expected_value: Result in case of success (e.g. max_speed, zero collisions, ...)
- actual_value: Actual result after running the scenario
- test_status: Used to access the result of the criterion
"""
def __init__(self, name, vehicle, expected_value):
super(Criterion, self).__init__(name)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
self._terminate_on_failure = False
self.name = name
self.vehicle = vehicle
self.test_status = "INIT"
self.expected_value = expected_value
self.actual_value = 0
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 MaxVelocityTest(Criterion):
"""
This class contains an atomic test for maximum velocity.
"""
def __init__(self, vehicle, max_velocity_allowed,
name="CheckMaximumVelocity"):
"""
Setup vehicle and maximum allowed velovity
"""
super(MaxVelocityTest, self).__init__(
name, vehicle, max_velocity_allowed)
def update(self):
"""
Check velocity
"""
new_status = py_trees.common.Status.RUNNING
if self.vehicle is None:
return new_status
velocity = CarlaDataProvider.get_velocity(self.vehicle)
self.actual_value = max(velocity, self.actual_value)
if velocity > self.expected_value:
self.test_status = "FAILURE"
else:
self.test_status = "SUCCESS"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class DrivenDistanceTest(Criterion):
"""
This class contains an atomic test to check the driven distance
"""
def __init__(self, vehicle, distance, name="CheckDrivenDistance"):
"""
Setup vehicle
"""
super(DrivenDistanceTest, self).__init__(name, vehicle, distance)
self._last_location = None
def initialise(self):
self._last_location = CarlaDataProvider.get_location(self.vehicle)
super(DrivenDistanceTest, self).initialise()
def update(self):
"""
Check distance
"""
new_status = py_trees.common.Status.RUNNING
if self.vehicle is None:
return new_status
location = CarlaDataProvider.get_location(self.vehicle)
if location is None:
return new_status
if self._last_location is None:
self._last_location = location
return new_status
self.actual_value += location.distance(self._last_location)
self._last_location = location
if self.actual_value > self.expected_value:
self.test_status = "SUCCESS"
else:
self.test_status = "RUNNING"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class AverageVelocityTest(Criterion):
"""
This class contains an atomic test for average velocity.
"""
def __init__(self, vehicle, avg_velocity, name="CheckAverageVelocity"):
"""
Setup vehicle and average velovity expected
"""
super(AverageVelocityTest, self).__init__(name, vehicle, avg_velocity)
self._last_location = None
self._distance = 0.0
def initialise(self):
self._last_location = CarlaDataProvider.get_location(self.vehicle)
super(AverageVelocityTest, self).initialise()
def update(self):
"""
Check velocity
"""
new_status = py_trees.common.Status.RUNNING
if self.vehicle is None:
return new_status
location = CarlaDataProvider.get_location(self.vehicle)
if location is None:
return new_status
if self._last_location is None:
self._last_location = location
return new_status
self._distance += location.distance(self._last_location)
self._last_location = location
elapsed_time = GameTime.get_time()
if elapsed_time > 0.0:
self.actual_value = self._distance / elapsed_time
if self.actual_value > self.expected_value:
self.test_status = "SUCCESS"
else:
self.test_status = "RUNNING"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
class CollisionTest(Criterion):
"""
This class contains an atomic test for collisions.
"""
def __init__(self, vehicle, name="CheckCollisions"):
"""
Construction with sensor setup
"""
super(CollisionTest, self).__init__(name, vehicle, 0)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
world = self.vehicle.get_world()
blueprint = world.get_blueprint_library().find('sensor.other.collision')
self._collision_sensor = world.spawn_actor(
blueprint, carla.Transform(), attach_to=self.vehicle)
self._collision_sensor.listen(
lambda event: self._count_collisions(weakref.ref(self), event))
def update(self):
"""
Check collision count
"""
new_status = py_trees.common.Status.RUNNING
if self.actual_value > 0:
self.test_status = "FAILURE"
else:
self.test_status = "SUCCESS"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
def terminate(self, new_status):
"""
Cleanup sensor
"""
if self._collision_sensor is not None:
self._collision_sensor.destroy()
self._collision_sensor = None
super(CollisionTest, self).terminate(new_status)
@staticmethod
def _count_collisions(weak_self, event):
"""
Callback to update collision count
"""
self = weak_self()
if not self:
return
self.actual_value += 1
class KeepLaneTest(Criterion):
"""
This class contains an atomic test for keeping lane.
"""
def __init__(self, vehicle, name="CheckKeepLane"):
"""
Construction with sensor setup
"""
super(KeepLaneTest, self).__init__(name, vehicle, 0)
self.logger.debug("%s.__init__()" % (self.__class__.__name__))
world = self.vehicle.get_world()
blueprint = world.get_blueprint_library().find(
'sensor.other.lane_detector')
self._lane_sensor = world.spawn_actor(
blueprint, carla.Transform(), attach_to=self.vehicle)
self._lane_sensor.listen(
lambda event: self._count_lane_invasion(weakref.ref(self), event))
def update(self):
"""
Check lane invasion count
"""
new_status = py_trees.common.Status.RUNNING
if self.actual_value > 0:
self.test_status = "FAILURE"
else:
self.test_status = "SUCCESS"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status
def terminate(self, new_status):
"""
Cleanup sensor
"""
if self._lane_sensor is not None:
self._lane_sensor.destroy()
self._lane_sensor = None
super(KeepLaneTest, self).terminate(new_status)
@staticmethod
def _count_lane_invasion(weak_self, event):
"""
Callback to update lane invasion count
"""
self = weak_self()
if not self:
return
self.actual_value += 1
class ReachedRegionTest(Criterion):
"""
This class contains the reached region test
"""
def __init__(self, vehicle, min_x, max_x, min_y,
max_y, name="ReachedRegionTest"):
"""
Setup trigger region (rectangle provided by
[min_x,min_y] and [max_x,max_y]
"""
super(ReachedRegionTest, self).__init__(name, vehicle, 0)
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:
self.test_status = "FAILURE"
else:
self.test_status = "SUCCESS"
if self._terminate_on_failure and (self.test_status == "FAILURE"):
new_status = py_trees.common.Status.FAILURE
self.logger.debug("%s.update()[%s->%s]" %
(self.__class__.__name__, self.status, new_status))
return new_status