From 99d658ff752efbca7e3ce3fbd7318b3e9b1aabb9 Mon Sep 17 00:00:00 2001
From: Armin <armin.co@hs-bochum.de>
Date: Wed, 10 Mar 2021 13:30:15 +0100
Subject: [PATCH] Integrated manual control into env

---
 carla_environment.py        | 12 ++++++---
 run_scripts/manual_carla.py | 50 +++++++++++++++++++------------------
 run_scripts/start_carla.sh  |  8 ++++++
 3 files changed, 42 insertions(+), 28 deletions(-)
 create mode 100755 run_scripts/start_carla.sh

diff --git a/carla_environment.py b/carla_environment.py
index 8854f52..0d50303 100644
--- a/carla_environment.py
+++ b/carla_environment.py
@@ -185,7 +185,6 @@ class World:
             position = transform
             self.player = self.world.try_spawn_actor(blueprint, position)
             start_location = self.player.get_location()
-            print(str(start_location))
             self.collision_sensor = CollisionSensor(self.world, self.player)
             self.obstacle_sensor = ObstacleSensor(self.world, self.player)
     
@@ -290,12 +289,12 @@ class CarlaEnvironment:
     client = None
     camera = None
     allow_render = False
+    manual = False
 
-    def __init__(self, host="127.0.0.1", port=2000, render=False):
+    def __init__(self, host="127.0.0.1", port=2000, render=False, manual=False):
         pygame.init()
         self.client = carla.Client(host, port)
-        self.client.set_timeout(5.0)
-        time.sleep(1.0)
+        self.client.set_timeout(3.0)
         self.client.load_world('Town07')
         time.sleep(1.0)
         self.world = World(self.client.get_world())
@@ -303,6 +302,9 @@ class CarlaEnvironment:
             self.allow_render = True
             self.camera = Camera(self.world, camera_type='semantic_segmentation')
             # self.camera = Camera(self.world, camera_type='rgb')
+        if manual:
+            self.manual = False
+            self.cntrl = Controller()
     
     def reset(self):
         self.world.reset()
@@ -315,6 +317,8 @@ class CarlaEnvironment:
     def step(self, action, render=False):
         if render:
             self.render()
+        if self.manual:
+            action = self.cntrl.get_action()
         return self.world.step(action)
     
     def __del__(self):
diff --git a/run_scripts/manual_carla.py b/run_scripts/manual_carla.py
index 7b77e09..0460491 100644
--- a/run_scripts/manual_carla.py
+++ b/run_scripts/manual_carla.py
@@ -2,41 +2,43 @@ import main
 import environment_wrapper as ew
 import gym
 import copy
-from agents import QAgent as QAgent
+from agents import QAgent, DQAgent
 from carla_environment import CarlaEnvironment
 
 c = ew.Config()
 
-c.name = 'Base'
+c.name = '2'
 c.render = True
 c.env_type = 'Carla'
-c.net_layout = [256, 128]
+c.net_layout = [256, 128, 128]
 c.eps_decay = 0.9995
 c.learn_rate= 0.001
-c.run_episodes = 20
+c.run_episodes = 100
 c.save_to = 'test/'
+c.load_from = 'Carla_1_256__128__128_0.9995_0.001_1'
+c.load_mem = True
 
 
-t = copy.deepcopy(c)
-t.render = True
-t.net_layout = [1024, 1024, 256, 32]
-t.eps_decay = 0.9993
-t.learn_rate = 0.0005
-t.force_cpu = False
-t.load_mem = True
-t.load_ann = False
-t.save_to = 'test/'
-t.load_from = 'Carla_CarlaOffline_1024__1024__256__32_0.9993_0.0005_50'
-t.name = 'Offline'
-t.learn_offline = True
-t.learn_online = True
-t.run_episodes = 500
-t.offline_epochs = 100
-t.learn_iterations = 100
-t.offline_validate_every_x_iteration = -1
+# t = copy.deepcopy(c)
+# t.render = True
+# t.net_layout = [1024, 1024, 256, 32]
+# t.eps_decay = 0.9993
+# t.learn_rate = 0.0005
+# t.force_cpu = False
+# t.load_mem = True
+# t.load_ann = False
+# t.save_to = 'test/'
+# t.load_from = 'Carla_CarlaOffline_1024__1024__256__32_0.9993_0.0005_50'
+# t.name = 'Offline'
+# t.learn_offline = True
+# t.learn_online = True
+# t.run_episodes = 500
+# t.offline_epochs = 100
+# t.learn_iterations = 100
+# t.offline_validate_every_x_iteration = -1
 
-configuration = t
-configuration.env = CarlaEnvironment(render=configuration.render)
+configuration = c
+configuration.env = CarlaEnvironment(render=configuration.render, manual=True)
 configuration.conf_to_name()
-configuration.agent = QAgent(configuration)
+configuration.agent = DQAgent(configuration)
 main.run(configuration)
diff --git a/run_scripts/start_carla.sh b/run_scripts/start_carla.sh
new file mode 100755
index 0000000..bc6da0a
--- /dev/null
+++ b/run_scripts/start_carla.sh
@@ -0,0 +1,8 @@
+# Start CARLA
+#
+# You may have to change the path to the CarlaUE4.sh
+# The simulator could be installed with your package manager: 
+# apt install carla-simulator
+#
+
+DISPLAY= /opt/carla-simulator/CarlaUE4.sh -benchmark -fps=15 -quality-level=Low -opengl
\ No newline at end of file
-- 
GitLab