]> rtime.felk.cvut.cz Git - hubacji1/py-carla.git/commitdiff
Add simple launch script without control
authorJiri Vlasak <jiri.vlasak.2@cvut.cz>
Wed, 1 Jun 2022 15:46:34 +0000 (17:46 +0200)
committerJiri Vlasak <jiri.vlasak.2@cvut.cz>
Wed, 1 Jun 2022 15:46:36 +0000 (17:46 +0200)
launch.py [new file with mode: 0755]

diff --git a/launch.py b/launch.py
new file mode 100755 (executable)
index 0000000..931a870
--- /dev/null
+++ b/launch.py
@@ -0,0 +1,95 @@
+#!/usr/bin/env python3.7
+import carla
+
+client = carla.Client("localhost", 2000)
+client.set_timeout(9999)
+world = client.get_world()
+settings = world.get_settings()
+
+
+def set_world(new_world="Town04"):
+    global world
+    client.load_world(new_world)
+    world = client.get_world()
+
+
+def set_simulation():
+    global settings
+    settings = world.get_settings()
+    settings.synchronous_mode = True
+    settings.fixed_delta_seconds = 0.02
+    settings.max_substeps = 10
+    world.apply_settings(settings)
+
+
+def focus_on(actor):
+    world.get_spectator().set_transform(actor.get_transform())
+
+
+def destroy(*actors):
+    for a in actors:
+        a.destroy()
+
+
+def spawn_vehicle(name, where):
+    if not isinstance(where, carla.Transform):
+        where = world.get_map().get_spawn_points()[where]
+    bp = world.get_blueprint_library().find("vehicle.tesla.model3")
+    bp.set_attribute("role_name", name)
+    if name == "target":
+        bp.set_attribute("color", "255,0,0")
+    return world.spawn_actor(bp, where)
+
+
+def spawn_ego(target):
+    tt = target.get_transform()
+    et = carla.Transform(
+            tt.location - 20 * tt.get_forward_vector(),
+            tt.rotation)
+    return spawn_vehicle("ego", et)
+
+
+def spawn_target():
+    return spawn_vehicle("target", 1)
+
+
+def main_collision():
+    set_world()
+    t = spawn_target()
+    world.wait_for_tick()
+    e = spawn_ego(t)
+    world.wait_for_tick()
+    focus_on(e)
+    e.set_target_velocity(carla.Vector3D(10, 0, 0))
+
+
+def main_simulation():
+    set_world()
+    set_simulation()
+    t = spawn_target()
+    world.tick()
+    e = spawn_ego(t)
+    world.tick()
+    focus_on(e)
+    t.set_target_velocity(carla.Vector3D(10, 0, 0))
+
+    c = carla.VehicleControl()
+    c.throttle = 1
+
+    i = 0
+    while i * settings.fixed_delta_seconds < 4:
+        print("step {} time {} ego speed {} m/s".format(
+                i, i * 0.02, e.get_velocity().length()))
+        if i == 10:
+            focus_on(e)
+        i += 1
+        world.tick()
+
+    destroy(e, t)
+
+
+if __name__ == "__main__":
+    try:
+        main_collision()
+    except KeyboardInterrupt:
+        pass