From b2feef5964de6249158c0dd59f9c397c110a7ade Mon Sep 17 00:00:00 2001 From: felipecode Date: Mon, 20 Nov 2017 15:17:18 +0100 Subject: [PATCH 1/5] load configuration --- PythonClient/carla/client.py | 24 ++++++++++++++++++++++++ PythonClient/client_example.py | 12 ++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/PythonClient/carla/client.py b/PythonClient/carla/client.py index 3be2b2157..9ac40aaf2 100644 --- a/PythonClient/carla/client.py +++ b/PythonClient/carla/client.py @@ -34,6 +34,10 @@ class CarlaClient(object): self._world_client = tcp.TCPClient(host, world_port, timeout) self._stream_client = tcp.TCPClient(host, world_port + 1, timeout) self._control_client = tcp.TCPClient(host, world_port + 2, timeout) + self._current_settings = None + # Controls the state, if an episode is already started. + self._is_episode_requested = False + # Variable to control the latest episode where the it started def connect(self): self._world_client.connect() @@ -67,7 +71,17 @@ class CarlaClient(object): pb_message.ParseFromString(data) if len(pb_message.player_start_spots) < 1: raise RuntimeError("received 0 player start spots") + self._is_episode_requested =True return pb_message + def load_carla_settings(self, carla_settings): + """ Abstraction to new episode request. carla_settings object must be convertible to + a str holding a CarlaSettings.ini. + + Returns a protobuf object holding the scene description. + """ + self._current_settings = carla_settings + return self.request_new_episode(carla_settings) + def start_episode(self, player_start_index): """Start the new episode at the player start given by the @@ -76,6 +90,14 @@ class CarlaClient(object): This function waits until the server answers with an EpisodeReady. """ + + if self._current_settings == None: + raise RuntimeError('no settings loaded, cannot start episode') + + if not self._is_episode_requested: + self.request_new_episode(self._current_settings) + + pb_message = carla_protocol.EpisodeStart() pb_message.player_start_spot_index = player_start_index self._world_client.write(pb_message.SerializeToString()) @@ -90,6 +112,8 @@ class CarlaClient(object): # We can start the agent clients now. self._stream_client.connect() self._control_client.connect() + # Set again the status for no episode requested + self._is_episode_requested = False def read_measurements(self): """Read measuremnts of current frame. The episode must be started. diff --git a/PythonClient/client_example.py b/PythonClient/client_example.py index cea25e55a..09768e7f3 100755 --- a/PythonClient/client_example.py +++ b/PythonClient/client_example.py @@ -69,13 +69,13 @@ def run_carla_client(host, port, autopilot_on, save_images_to_disk, image_filena camera1.set_position(30, 0, 130) settings.add_camera(camera1) - print('Requesting new episode...') + print('Loading the new settings ...') - # Now we request a new episode with these settings. The server - # replies with a scene description containing the available start - # spots for the player. Here instead of a CarlaSettings object we - # can also provide a CarlaSettings.ini file as string. - scene = client.request_new_episode(settings) + # Now we load these new settings. The server + # replies with a scene description containing the available start + # spots for the player. Here instead of a CarlaSettings object we + # can also provide a CarlaSettings.ini file as string. + scene = client.load_carla_settings(settings) # Choose one player start at random. number_of_player_starts = len(scene.player_start_spots) From 406f67cfe790485f38208b695a604294bcec3aeb Mon Sep 17 00:00:00 2001 From: felipecode Date: Mon, 20 Nov 2017 15:35:45 +0100 Subject: [PATCH 2/5] Carla settings python2-3 --- PythonClient/carla/settings.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/PythonClient/carla/settings.py b/PythonClient/carla/settings.py index de81fba3d..014ebc4b3 100644 --- a/PythonClient/carla/settings.py +++ b/PythonClient/carla/settings.py @@ -9,15 +9,16 @@ import io import random import sys - -try: - - from configparser import ConfigParser - -except ImportError: +import sys +is_py2 = sys.version[0] == '2' +if is_py2: from ConfigParser import RawConfigParser as ConfigParser +else: + + from configparser import ConfigParser + MAX_NUMBER_OF_WEATHER_IDS = 14 @@ -143,5 +144,6 @@ class CarlaSettings(object): text = io.StringIO() else: text = io.BytesIO() + ini.write(text) return text.getvalue().replace(' = ', '=') From 81b095b9ef07b41b3eeac0278334a8107e113619 Mon Sep 17 00:00:00 2001 From: felipecode Date: Mon, 20 Nov 2017 15:50:49 +0100 Subject: [PATCH 3/5] load settings on manual control --- PythonClient/manual_control.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PythonClient/manual_control.py b/PythonClient/manual_control.py index 0efb38441..2e700cc81 100755 --- a/PythonClient/manual_control.py +++ b/PythonClient/manual_control.py @@ -135,7 +135,7 @@ class CarlaGame(object): def _on_new_episode(self): print('Requesting new episode...') - scene = self.client.request_new_episode(make_carla_settings()) + scene = self.client.load_carla_settings(make_carla_settings()) number_of_player_starts = len(scene.player_start_spots) player_start = np.random.randint(number_of_player_starts) self.client.start_episode(player_start) From e59f5ee2eff843f38e558d5546ab431ac59aa41c Mon Sep 17 00:00:00 2001 From: felipecode Date: Mon, 20 Nov 2017 16:33:27 +0100 Subject: [PATCH 4/5] notation fixes --- PythonClient/carla/client.py | 58 ++++++++++++++++++---------------- PythonClient/client_example.py | 2 +- PythonClient/manual_control.py | 2 +- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/PythonClient/carla/client.py b/PythonClient/carla/client.py index 9ac40aaf2..9cc462f74 100644 --- a/PythonClient/carla/client.py +++ b/PythonClient/carla/client.py @@ -50,11 +50,13 @@ class CarlaClient(object): def connected(self): return self._world_client.connected() - def request_new_episode(self, carla_settings): - """Request a new episode. carla_settings object must be convertible to - a str holding a CarlaSettings.ini. + def _request_new_episode(self, carla_settings): + """Request a new episode. Internal function to + request information about a new episode episode that + is going to start. It also prepare the client for + reset by disconnecting stream and control clients. + - Returns a protobuf object holding the scene description. """ # Disconnect agent clients. self._stream_client.disconnect() @@ -73,20 +75,20 @@ class CarlaClient(object): raise RuntimeError("received 0 player start spots") self._is_episode_requested =True return pb_message - def load_carla_settings(self, carla_settings): + def load_settings(self, carla_settings): """ Abstraction to new episode request. carla_settings object must be convertible to - a str holding a CarlaSettings.ini. + a str holding a CarlaSettings.ini. Loads new settings to the server. Returns a protobuf object holding the scene description. """ self._current_settings = carla_settings - return self.request_new_episode(carla_settings) + return self._request_new_episode(carla_settings) def start_episode(self, player_start_index): """Start the new episode at the player start given by the player_start_index. The list of player starts is retrieved by - request_new_episode(). + load_settings(). This function waits until the server answers with an EpisodeReady. """ @@ -94,26 +96,28 @@ class CarlaClient(object): if self._current_settings == None: raise RuntimeError('no settings loaded, cannot start episode') - if not self._is_episode_requested: - self.request_new_episode(self._current_settings) + # if no new settings are loaded, request new episode with previous + if not self._is_episode_requested: + self._request_new_episode(self._current_settings) - - pb_message = carla_protocol.EpisodeStart() - pb_message.player_start_spot_index = player_start_index - self._world_client.write(pb_message.SerializeToString()) - # Wait for EpisodeReady. - data = self._world_client.read() - if not data: - raise RuntimeError('failed to read data from server') - pb_message = carla_protocol.EpisodeReady() - pb_message.ParseFromString(data) - if not pb_message.ready: - raise RuntimeError('cannot start episode: server failed to start episode') - # We can start the agent clients now. - self._stream_client.connect() - self._control_client.connect() - # Set again the status for no episode requested - self._is_episode_requested = False + try: + pb_message = carla_protocol.EpisodeStart() + pb_message.player_start_spot_index = player_start_index + self._world_client.write(pb_message.SerializeToString()) + # Wait for EpisodeReady. + data = self._world_client.read() + if not data: + raise RuntimeError('failed to read data from server') + pb_message = carla_protocol.EpisodeReady() + pb_message.ParseFromString(data) + if not pb_message.ready: + raise RuntimeError('cannot start episode: server failed to start episode') + # We can start the agent clients now. + self._stream_client.connect() + self._control_client.connect() + # Set again the status for no episode requested + finally: + self._is_episode_requested = False def read_measurements(self): """Read measuremnts of current frame. The episode must be started. diff --git a/PythonClient/client_example.py b/PythonClient/client_example.py index 09768e7f3..418d5cb9b 100755 --- a/PythonClient/client_example.py +++ b/PythonClient/client_example.py @@ -75,7 +75,7 @@ def run_carla_client(host, port, autopilot_on, save_images_to_disk, image_filena # replies with a scene description containing the available start # spots for the player. Here instead of a CarlaSettings object we # can also provide a CarlaSettings.ini file as string. - scene = client.load_carla_settings(settings) + scene = client.load_settings(settings) # Choose one player start at random. number_of_player_starts = len(scene.player_start_spots) diff --git a/PythonClient/manual_control.py b/PythonClient/manual_control.py index 2e700cc81..d2e95f919 100755 --- a/PythonClient/manual_control.py +++ b/PythonClient/manual_control.py @@ -135,7 +135,7 @@ class CarlaGame(object): def _on_new_episode(self): print('Requesting new episode...') - scene = self.client.load_carla_settings(make_carla_settings()) + scene = self.client.load_settings(make_carla_settings()) number_of_player_starts = len(scene.player_start_spots) player_start = np.random.randint(number_of_player_starts) self.client.start_episode(player_start) From f3464e2ed87d5d8b59cd8a0e6096f4fbe69efc27 Mon Sep 17 00:00:00 2001 From: felipecode Date: Mon, 20 Nov 2017 16:34:40 +0100 Subject: [PATCH 5/5] Small notation --- PythonClient/manual_control.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PythonClient/manual_control.py b/PythonClient/manual_control.py index d2e95f919..a4b61aa37 100755 --- a/PythonClient/manual_control.py +++ b/PythonClient/manual_control.py @@ -134,10 +134,11 @@ class CarlaGame(object): self._on_new_episode() def _on_new_episode(self): - print('Requesting new episode...') + scene = self.client.load_settings(make_carla_settings()) number_of_player_starts = len(scene.player_start_spots) player_start = np.random.randint(number_of_player_starts) + print('Starting new episode...') self.client.start_episode(player_start) self._timer = Timer() self._is_on_reverse = False