Merge pull request #38 from carla-simulator/new_client_api_felipemod

Adding load settings and modification on settings.py (issue #32)
This commit is contained in:
nsubiron 2017-11-20 16:47:35 +01:00 committed by GitHub
commit a0fa813444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 33 deletions

View File

@ -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()
@ -46,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()
@ -67,29 +73,51 @@ 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_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.
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
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.
"""
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()
if self._current_settings == 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:
self._request_new_episode(self._current_settings)
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.

View File

@ -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(' = ', '=')

View File

@ -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_settings(settings)
# Choose one player start at random.
number_of_player_starts = len(scene.player_start_spots)

View File

@ -134,10 +134,11 @@ class CarlaGame(object):
self._on_new_episode()
def _on_new_episode(self):
print('Requesting new episode...')
scene = self.client.request_new_episode(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)
print('Starting new episode...')
self.client.start_episode(player_start)
self._timer = Timer()
self._is_on_reverse = False