Fixing exception on tcp, adding verbose

This commit is contained in:
felipecode 2018-01-22 10:43:12 +01:00
parent cec548c964
commit f0eccca7ad
3 changed files with 43 additions and 22 deletions

View File

@ -7,21 +7,19 @@
# For a copy, see <https://opensource.org/licenses/MIT>.
from builtins import input as input_data
import csv
import datetime
import math
import os
import abc
import logging
from builtins import input as input_data
from carla.client import VehicleControl
def sldist(c1, c2):
return math.sqrt((c2[0] - c1[0])**2 + (c2[1] - c1[1])**2)
@ -38,11 +36,15 @@ class Benchmark(object):
city_name,
name_to_save,
continue_experiment=False,
save_images=False):
save_images=False,
verbose=False):
self.__metaclass__ = abc.ABCMeta
self._city_name = city_name
self._verbose = verbose
self._base_name = name_to_save
self._dict_stats = {'exp_id': -1,
@ -104,8 +106,11 @@ class Benchmark(object):
measurements, sensor_data = carla.read_data()
control = agent.run_step(measurements, sensor_data, target)
print('STEER ', control.steer, 'GAS ',
control.throttle, 'Brake ', control.brake)
logging.info("Controller is Inputting:")
logging.info('Steer = %f Throttle = %f Brake = %f ' %
(control.steer, control.throttle, control.brake))
carla.send_control(control)
# measure distance to target
@ -124,9 +129,11 @@ class Benchmark(object):
distance = sldist([curr_x, curr_y],
[target.location.x, target.location.y])
print(
logging.info('Status:')
logging.info(
'[d=%f] c_x = %f, c_y = %f ---> t_x = %f, t_y = %f' %
(float(distance), curr_x, curr_y, target.location.x, target.location.y))
(float(distance), curr_x, curr_y, target.location.x,
target.location.y))
if distance < 200.0:
success = True
@ -157,7 +164,7 @@ class Benchmark(object):
else:
(start_task, start_pose) = self._get_pose_and_task(self._line_on_file)
print(' START ')
logging.info(' START ')
for experiment in self._experiments[start_task:]:
@ -172,12 +179,15 @@ class Benchmark(object):
carla.start_episode(start_point)
print('======== !!!! ==========')
print(' Start Position ', start_point,
' End Position ', end_point)
logging.info('======== !!!! ==========')
logging.info(' Start Position %d End Position %d ' %
(start_point, end_point))
path_distance = agent.get_distance(
positions[start_point], positions[end_point])
euclidean_distance = \
sldist([positions[start_point].location.x, positions[start_point].location.y],
[positions[end_point].location.x, positions[end_point].location.y])
time_out = self._calculate_time_out(path_distance)
# running the agent
@ -191,16 +201,16 @@ class Benchmark(object):
# compute stats for the experiment
self._write_summary_results(
experiment, pose, rep, path_distance,
experiment, pose, rep, euclidean_distance,
remaining_distance, final_time, time_out, result)
self._write_details_results(experiment, rep, reward_vec)
if(result > 0):
print('+++++ Target achieved in %f seconds! +++++' %
logging.info('+++++ Target achieved in %f seconds! +++++' %
final_time)
else:
print('----- Timeout! -----')
logging.info('----- Timeout! -----')
return self.get_all_statistics()
def _write_summary_results(self, experiment, pose, rep,

View File

@ -41,7 +41,7 @@ class TCPClient(object):
self._socket.settimeout(self._timeout)
logging.debug('%sconnected', self._logprefix)
return
except OSError as exception:
except socket.error as exception:
error = exception
logging.debug('%sconnection attempt %d: %s', self._logprefix, attempt, error)
time.sleep(1)
@ -65,7 +65,7 @@ class TCPClient(object):
header = struct.pack('<L', len(message))
try:
self._socket.sendall(header + message)
except OSError as exception:
except socket.error as exception:
self._reraise_exception_as_tcp_error('failed to write data', exception)
def read(self):
@ -85,7 +85,7 @@ class TCPClient(object):
while length > 0:
try:
data = self._socket.recv(length)
except OSError as exception:
except socket.error as exception:
self._reraise_exception_as_tcp_error('failed to read data', exception)
if not data:
raise TCPConnectionError(self._logprefix + 'connection closed')

View File

@ -35,6 +35,11 @@ if __name__ == '__main__':
argparser.add_argument(
'-v', '--verbose',
action='store_true',
dest='verbose',
help='print some extra status information')
argparser.add_argument(
'-db', '--debug',
action='store_true',
dest='debug',
help='print debug information')
argparser.add_argument(
@ -58,12 +63,17 @@ if __name__ == '__main__':
'-n', '--log_name',
metavar='T',
default='test',
help='The name of the log file to be created by the scripts'
help='The name of the log file to be created by the benchmark'
)
args = argparser.parse_args()
if args.debug:
log_level = logging.DEBUG
elif args.verbose:
log_level = logging.INFO
else:
log_level = logging.WARNING
log_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
logging.info('listening to server %s:%s', args.host, args.port)
@ -71,7 +81,8 @@ if __name__ == '__main__':
while True:
try:
with make_carla_client(args.host, args.port) as client:
corl = CoRL2017(city_name=args.city_name, name_to_save=args.log_name)
corl = CoRL2017(city_name=args.city_name, name_to_save=args.log_name,
verbose=args.verbose)
agent = Manual(args.city_name)
results = corl.benchmark_agent(agent, client)
corl.plot_summary_test()