-
Notifications
You must be signed in to change notification settings - Fork 0
/
STL_CARLA_example.py
96 lines (77 loc) · 2.48 KB
/
STL_CARLA_example.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
import glob
import os
import re
import sys
import matplotlib.pyplot as plt
from srunner.metrics.tools.metrics_log import MetricsLog
import STL
try:
sys.path.append(glob.glob('carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
def main():
# Client creation
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
file = os.path.dirname(__file__) + "/records/FollowLeadingVehicle_2.log"
info = client.show_recorder_file_info(file, True)
frames = int(re.search(r"Frames: (\d+)",
info).group(1))
log = MetricsLog(info)
ego_id = log.get_ego_vehicle_id()
adv_id = log.get_actor_ids_with_role_name("scenario")[0]
client.replay_file(file, 0, 0, ego_id)
world.tick()
actor_list = world.get_actors()
vehicle = actor_list.find(ego_id)
vehicle2 = actor_list.find(adv_id)
x = STL.parse('(x<20)')
y = STL.parse('<->[0,1] y')
phi = x.once(lo=0, hi=5) and y
robustness = []
b_robustness = []
sig = {"x": [], "y": []}
time = []
points = frames * 2
time_period = frames * 0.05
for i in range(points):
if (vehicle.get_location() == carla.Location(0, 0, 0)
or vehicle2.get_location() ==
carla.Location(0, 0, 0)):
break
dist = vehicle.get_location().distance(
vehicle2.get_location())
sig["x"].append(dist)
sig["y"].append(vehicle.is_at_traffic_light())
time.append(i * 0.05)
val = STL.evaluate(phi, sig, time, t=i, points=points,
time_period=time_period)
val = 1 if val == float('inf') else val
val = 0 if val == float('-inf') else val
robustness.append(val)
b_robustness.append(1 if val >= 0 else 0)
world.tick()
vehicles = world.get_actors().filter('vehicle.*')
sensors = world.get_actors().filter('sensors.*')
for a in vehicles:
a.destroy()
for a in sensors:
a.destroy()
plt.plot(time, sig["x"], time, robustness, "r--")
plt.show()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndone.')