diff --git a/CARLA.sublime-project b/CARLA.sublime-project index e5ffd445d..b5f335ef3 100644 --- a/CARLA.sublime-project +++ b/CARLA.sublime-project @@ -42,7 +42,8 @@ "settings": { "tab_size": 2, - "translate_tabs_to_spaces": true + "translate_tabs_to_spaces": true, + "trim_trailing_white_space_on_save": true }, "build_systems": [ diff --git a/PythonClient/carla/client.py b/PythonClient/carla/client.py index 9cc462f74..9158ce569 100644 --- a/PythonClient/carla/client.py +++ b/PythonClient/carla/client.py @@ -8,6 +8,7 @@ import os import struct +import time from contextlib import contextmanager @@ -37,7 +38,6 @@ class CarlaClient(object): 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() @@ -50,34 +50,11 @@ class CarlaClient(object): def connected(self): return self._world_client.connected() - 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. - - - """ - # Disconnect agent clients. - self._stream_client.disconnect() - self._control_client.disconnect() - # Send new episode request. - pb_message = carla_protocol.RequestNewEpisode() - pb_message.ini_file = str(carla_settings) - self._world_client.write(pb_message.SerializeToString()) - # Read scene description. - data = self._world_client.read() - if not data: - raise RuntimeError('failed to read data from server') - pb_message = carla_protocol.SceneDescription() - 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_settings(self, carla_settings): - """ Abstraction to new episode request. carla_settings object must be convertible to - a str holding a CarlaSettings.ini. Loads new settings to the server. + """ + Load new settings and request a new episode based on these settings to + the server. carla_settings object must be convertible to a str holding a + CarlaSettings.ini. Returns a protobuf object holding the scene description. """ @@ -86,18 +63,19 @@ class CarlaClient(object): def start_episode(self, player_start_index): - """Start the new episode at the player start given by the + """ + Start the new episode at the player start given by the player_start_index. The list of player starts is retrieved by - load_settings(). + "load_settings". Requests a new episode based on the last settings + loaded by "load_settings". This function waits until the server answers with an EpisodeReady. """ - - if self._current_settings == None: + if self._current_settings is None: raise RuntimeError('no settings loaded, cannot start episode') # if no new settings are loaded, request new episode with previous - if not self._is_episode_requested: + if not self._is_episode_requested: self._request_new_episode(self._current_settings) try: @@ -120,9 +98,10 @@ class CarlaClient(object): self._is_episode_requested = False def read_measurements(self): - """Read measuremnts of current frame. The episode must be started. - Return the protobuf object with the measurements followed by the raw - data with the images. + """ + Read measuremnts of current frame. The episode must be started. Return + the protobuf object with the measurements followed by the raw data with + the images. """ # Read measurements. data = self._stream_client.read() @@ -147,6 +126,30 @@ class CarlaClient(object): pb_message.reverse = kwargs.get('reverse', False) self._control_client.write(pb_message.SerializeToString()) + 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. + """ + # Disconnect agent clients. + self._stream_client.disconnect() + self._control_client.disconnect() + # Send new episode request. + pb_message = carla_protocol.RequestNewEpisode() + pb_message.ini_file = str(carla_settings) + self._world_client.write(pb_message.SerializeToString()) + # Read scene description. + data = self._world_client.read() + if not data: + raise RuntimeError('failed to read data from server') + pb_message = carla_protocol.SceneDescription() + 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 + class CarlaImage(object): @staticmethod diff --git a/PythonClient/test/console.py b/PythonClient/test/console.py index 8e9dd0c16..7229d00d4 100644 --- a/PythonClient/test/console.py +++ b/PythonClient/test/console.py @@ -128,7 +128,7 @@ class CarlaClientConsole(cmd.Cmd): self.control = _Control() if not self.client.connected(): self.client.connect() - self.client.request_new_episode(self.settings) + self.client.load_settings(self.settings) self.client.start_episode(0) logging.info('new episode started') except Exception as exception: diff --git a/PythonClient/test/test_client.py b/PythonClient/test/test_client.py index ed1c1d929..47176015b 100755 --- a/PythonClient/test/test_client.py +++ b/PythonClient/test/test_client.py @@ -45,7 +45,7 @@ def run_carla_client(args): logging.debug('sending CarlaSettings:\n%s', settings) logging.info('new episode requested') - scene = client.request_new_episode(settings) + scene = client.load_settings(settings) number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) diff --git a/PythonClient/test/test_suite.py b/PythonClient/test/test_suite.py index 08f1e7658..b1efbd4d9 100755 --- a/PythonClient/test/test_suite.py +++ b/PythonClient/test/test_suite.py @@ -23,6 +23,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '.')) import carla +from carla.tcp import TCPConnectionError from carla.util import StopWatch from unit_tests import CarlaServerTest @@ -99,14 +100,20 @@ def run_test(test, args): logging.error('exception instantiating %r: %s', test.name, exception) return False log_test(RUN, test.name) - try: - timer = StopWatch() - result = test.run() - timer.stop() - except Exception as exception: - timer.stop() - logging.error('exception: %s', exception) - result = False + while True: + try: + timer = StopWatch() + result = test.run() + timer.stop() + break + except TCPConnectionError as error: + logging.error(error) + time.sleep(1) + except Exception as exception: + timer.stop() + logging.exception('exception: %s', exception) + result = False + break log_test(OK if result else FAILED, '%s (%d ms)', test.name, timer.milliseconds()) return result @@ -118,9 +125,6 @@ def do_the_tests(args): failed = [] log_test(SEP0, 'Running %d tests.', len(tests)) for test in tests: - if succeeded or failed: - logging.info('waiting for the server to be ready again') - time.sleep(7) if run_test(test, args): succeeded.append(test) else: diff --git a/PythonClient/test/unit_tests/Basic.py b/PythonClient/test/unit_tests/Basic.py index 2b05d8632..29d841ab8 100644 --- a/PythonClient/test/unit_tests/Basic.py +++ b/PythonClient/test/unit_tests/Basic.py @@ -25,7 +25,7 @@ class _BasicTestBase(unit_tests.CarlaServerTest): carla_settings.randomize_weather() logging.debug('sending CarlaSettings:\n%s', carla_settings) logging.info('new episode requested') - scene = client.request_new_episode(carla_settings) + scene = client.load_settings(carla_settings) number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) logging.info(