Fix client tests
This commit is contained in:
parent
cebbb4bcbe
commit
b8cd04e11b
|
@ -42,7 +42,8 @@
|
||||||
"settings":
|
"settings":
|
||||||
{
|
{
|
||||||
"tab_size": 2,
|
"tab_size": 2,
|
||||||
"translate_tabs_to_spaces": true
|
"translate_tabs_to_spaces": true,
|
||||||
|
"trim_trailing_white_space_on_save": true
|
||||||
},
|
},
|
||||||
"build_systems":
|
"build_systems":
|
||||||
[
|
[
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import struct
|
import struct
|
||||||
|
import time
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
@ -37,7 +38,6 @@ class CarlaClient(object):
|
||||||
self._current_settings = None
|
self._current_settings = None
|
||||||
# Controls the state, if an episode is already started.
|
# Controls the state, if an episode is already started.
|
||||||
self._is_episode_requested = False
|
self._is_episode_requested = False
|
||||||
# Variable to control the latest episode where the it started
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self._world_client.connect()
|
self._world_client.connect()
|
||||||
|
@ -50,34 +50,11 @@ class CarlaClient(object):
|
||||||
def connected(self):
|
def connected(self):
|
||||||
return self._world_client.connected()
|
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):
|
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.
|
Returns a protobuf object holding the scene description.
|
||||||
"""
|
"""
|
||||||
|
@ -86,18 +63,19 @@ class CarlaClient(object):
|
||||||
|
|
||||||
|
|
||||||
def start_episode(self, player_start_index):
|
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
|
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.
|
This function waits until the server answers with an EpisodeReady.
|
||||||
"""
|
"""
|
||||||
|
if self._current_settings is None:
|
||||||
if self._current_settings == None:
|
|
||||||
raise RuntimeError('no settings loaded, cannot start episode')
|
raise RuntimeError('no settings loaded, cannot start episode')
|
||||||
|
|
||||||
# if no new settings are loaded, request new episode with previous
|
# 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)
|
self._request_new_episode(self._current_settings)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -120,9 +98,10 @@ class CarlaClient(object):
|
||||||
self._is_episode_requested = False
|
self._is_episode_requested = False
|
||||||
|
|
||||||
def read_measurements(self):
|
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
|
Read measuremnts of current frame. The episode must be started. Return
|
||||||
data with the images.
|
the protobuf object with the measurements followed by the raw data with
|
||||||
|
the images.
|
||||||
"""
|
"""
|
||||||
# Read measurements.
|
# Read measurements.
|
||||||
data = self._stream_client.read()
|
data = self._stream_client.read()
|
||||||
|
@ -147,6 +126,30 @@ class CarlaClient(object):
|
||||||
pb_message.reverse = kwargs.get('reverse', False)
|
pb_message.reverse = kwargs.get('reverse', False)
|
||||||
self._control_client.write(pb_message.SerializeToString())
|
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):
|
class CarlaImage(object):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -128,7 +128,7 @@ class CarlaClientConsole(cmd.Cmd):
|
||||||
self.control = _Control()
|
self.control = _Control()
|
||||||
if not self.client.connected():
|
if not self.client.connected():
|
||||||
self.client.connect()
|
self.client.connect()
|
||||||
self.client.request_new_episode(self.settings)
|
self.client.load_settings(self.settings)
|
||||||
self.client.start_episode(0)
|
self.client.start_episode(0)
|
||||||
logging.info('new episode started')
|
logging.info('new episode started')
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
|
|
|
@ -45,7 +45,7 @@ def run_carla_client(args):
|
||||||
logging.debug('sending CarlaSettings:\n%s', settings)
|
logging.debug('sending CarlaSettings:\n%s', settings)
|
||||||
logging.info('new episode requested')
|
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)
|
number_of_player_starts = len(scene.player_start_spots)
|
||||||
player_start = random.randint(0, max(0, number_of_player_starts - 1))
|
player_start = random.randint(0, max(0, number_of_player_starts - 1))
|
||||||
|
|
|
@ -23,6 +23,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '.'))
|
||||||
|
|
||||||
import carla
|
import carla
|
||||||
|
|
||||||
|
from carla.tcp import TCPConnectionError
|
||||||
from carla.util import StopWatch
|
from carla.util import StopWatch
|
||||||
|
|
||||||
from unit_tests import CarlaServerTest
|
from unit_tests import CarlaServerTest
|
||||||
|
@ -99,14 +100,20 @@ def run_test(test, args):
|
||||||
logging.error('exception instantiating %r: %s', test.name, exception)
|
logging.error('exception instantiating %r: %s', test.name, exception)
|
||||||
return False
|
return False
|
||||||
log_test(RUN, test.name)
|
log_test(RUN, test.name)
|
||||||
try:
|
while True:
|
||||||
timer = StopWatch()
|
try:
|
||||||
result = test.run()
|
timer = StopWatch()
|
||||||
timer.stop()
|
result = test.run()
|
||||||
except Exception as exception:
|
timer.stop()
|
||||||
timer.stop()
|
break
|
||||||
logging.error('exception: %s', exception)
|
except TCPConnectionError as error:
|
||||||
result = False
|
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())
|
log_test(OK if result else FAILED, '%s (%d ms)', test.name, timer.milliseconds())
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -118,9 +125,6 @@ def do_the_tests(args):
|
||||||
failed = []
|
failed = []
|
||||||
log_test(SEP0, 'Running %d tests.', len(tests))
|
log_test(SEP0, 'Running %d tests.', len(tests))
|
||||||
for test in 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):
|
if run_test(test, args):
|
||||||
succeeded.append(test)
|
succeeded.append(test)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -25,7 +25,7 @@ class _BasicTestBase(unit_tests.CarlaServerTest):
|
||||||
carla_settings.randomize_weather()
|
carla_settings.randomize_weather()
|
||||||
logging.debug('sending CarlaSettings:\n%s', carla_settings)
|
logging.debug('sending CarlaSettings:\n%s', carla_settings)
|
||||||
logging.info('new episode requested')
|
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)
|
number_of_player_starts = len(scene.player_start_spots)
|
||||||
player_start = random.randint(0, max(0, number_of_player_starts - 1))
|
player_start = random.randint(0, max(0, number_of_player_starts - 1))
|
||||||
logging.info(
|
logging.info(
|
||||||
|
|
Loading…
Reference in New Issue