]> rtime.felk.cvut.cz Git - hubacji1/py-carla.git/blob - launch.py
931a870dd7a13fd50fda581ab383de9e39b5cb55
[hubacji1/py-carla.git] / launch.py
1 #!/usr/bin/env python3.7
2 import carla
3
4 client = carla.Client("localhost", 2000)
5 client.set_timeout(9999)
6 world = client.get_world()
7 settings = world.get_settings()
8
9
10 def set_world(new_world="Town04"):
11     global world
12     client.load_world(new_world)
13     world = client.get_world()
14
15
16 def set_simulation():
17     global settings
18     settings = world.get_settings()
19     settings.synchronous_mode = True
20     settings.fixed_delta_seconds = 0.02
21     settings.max_substeps = 10
22     world.apply_settings(settings)
23
24
25 def focus_on(actor):
26     world.get_spectator().set_transform(actor.get_transform())
27
28
29 def destroy(*actors):
30     for a in actors:
31         a.destroy()
32
33
34 def spawn_vehicle(name, where):
35     if not isinstance(where, carla.Transform):
36         where = world.get_map().get_spawn_points()[where]
37     bp = world.get_blueprint_library().find("vehicle.tesla.model3")
38     bp.set_attribute("role_name", name)
39     if name == "target":
40         bp.set_attribute("color", "255,0,0")
41     return world.spawn_actor(bp, where)
42
43
44 def spawn_ego(target):
45     tt = target.get_transform()
46     et = carla.Transform(
47             tt.location - 20 * tt.get_forward_vector(),
48             tt.rotation)
49     return spawn_vehicle("ego", et)
50
51
52 def spawn_target():
53     return spawn_vehicle("target", 1)
54
55
56 def main_collision():
57     set_world()
58     t = spawn_target()
59     world.wait_for_tick()
60     e = spawn_ego(t)
61     world.wait_for_tick()
62     focus_on(e)
63     e.set_target_velocity(carla.Vector3D(10, 0, 0))
64
65
66 def main_simulation():
67     set_world()
68     set_simulation()
69     t = spawn_target()
70     world.tick()
71     e = spawn_ego(t)
72     world.tick()
73     focus_on(e)
74     t.set_target_velocity(carla.Vector3D(10, 0, 0))
75
76     c = carla.VehicleControl()
77     c.throttle = 1
78
79     i = 0
80     while i * settings.fixed_delta_seconds < 4:
81         print("step {} time {} ego speed {} m/s".format(
82                 i, i * 0.02, e.get_velocity().length()))
83         if i == 10:
84             focus_on(e)
85         i += 1
86         world.tick()
87
88     destroy(e, t)
89
90
91 if __name__ == "__main__":
92     try:
93         main_collision()
94     except KeyboardInterrupt:
95         pass