Fixed the initial state of weather.py

This commit is contained in:
Marc Garcia Puig 2020-01-20 14:34:13 +01:00 committed by bernat
parent a52e97f3c3
commit f57aaef687
1 changed files with 74 additions and 75 deletions

View File

@ -1,8 +1,13 @@
#!/usr/bin/env python
"""
Script to control weather parameters in simulations
"""
import glob
import os
import sys
import argparse
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
@ -14,12 +19,66 @@ except IndexError:
import carla
import argparse
SUN_PRESETS = {
'day': (90.0, 220.0),
'night': (-90.0, 220.0),
'sunset': (181.0, 100.0)}
WEATHER_PRESETS = {
'clear': [10.0, 0.0, 0.0, 5.0, 0.0, 50.0, 0.0],
'overcast': [80.0, 0.0, 0.0, 50.0, 0.0, 50.0, 10.0],
'rain': [100.0, 80.0, 90.0, 100.0, 20.0, 50.0, 100.0]}
def apply_sun_presets(args, weather):
"""Uses sun presets to set the sun position"""
if args.sun is not None:
if args.sun in SUN_PRESETS:
weather.sun_altitude_angle = SUN_PRESETS[args.sun][0]
weather.sun_azimuth_angle = SUN_PRESETS[args.sun][1]
else:
print("[ERROR]: Command [--sun | -s] '" + args.sun + "' not known")
sys.exit(1)
def apply_weather_presets(args, weather):
"""Uses weather presets to set the weather parameters"""
if args.weather is not None:
if args.weather in WEATHER_PRESETS:
weather.cloudiness = WEATHER_PRESETS[args.weather][0]
weather.precipitation = WEATHER_PRESETS[args.weather][1]
weather.precipitation_deposits = WEATHER_PRESETS[args.weather][2]
weather.wind_intensity = WEATHER_PRESETS[args.weather][3]
else:
print("[ERROR]: Command [--weather | -w] '" + args.weather + "' not known")
sys.exit(1)
def apply_weather_values(args, weather):
"""Set weather values individually"""
if args.azimuth is not None:
weather.sun_azimuth_angle = args.azimuth
if args.altitude is not None:
weather.sun_altitude_angle = args.altitude
if args.clouds is not None:
weather.cloudiness = args.clouds
if args.rain is not None:
weather.precipitation = args.rain
if args.puddles is not None:
weather.precipitation_deposits = args.puddles
if args.wind is not None:
weather.wind_intensity = args.wind
if args.fog is not None:
weather.fog_density = args.fog
if args.fogdist is not None:
weather.fog_distance = args.fogdist
if args.wetness is not None:
weather.wetness = args.wetness
def main():
argparser = argparse.ArgumentParser(
description="CARLA weather example")
description=__doc__)
argparser.add_argument(
'--host',
metavar='H',
@ -35,12 +94,12 @@ def main():
'--sun',
default=None,
type=str,
help='Sun position presets [day | night | sunset]')
help='Sun position presets [' + ' | '.join([i for i in SUN_PRESETS]) + ']')
argparser.add_argument(
'--weather',
default=None,
type=str,
help='Weather condition presets [clear | overcast | rain]')
help='Weather condition presets [' + ' | '.join([i for i in WEATHER_PRESETS]) + ']')
argparser.add_argument(
'--altitude', '-alt',
metavar='A',
@ -97,7 +156,11 @@ def main():
help='Wetness')
args = argparser.parse_args()
if not (args.sun or args.weather or args.altitude or args.azimuth or args.clouds or args.rain or args.puddles or args.wind or args.fog or args.fogdist or args.wetness):
# since all the arguments are None by default
# (except for the first 2, host and port)
# we can check if all the arguments have been provided
arg_values = [v for _, v in args.__dict__.items()][2:]
if all(i is (None and False) for i in arg_values):
argparser.error('No arguments provided.')
client = carla.Client(args.host, args.port)
@ -106,78 +169,14 @@ def main():
weather = world.get_weather()
#### PRESETS ###
if args.sun is not None:
## Sun Position ##
if args.sun == 'day':
weather.sun_altitude_angle = 90
weather.sun_azimuth_angle = 220.0
# apply presets
apply_sun_presets(args, weather)
apply_weather_presets(args, weather)
elif args.sun == 'night':
weather.sun_altitude_angle = -90
weather.sun_azimuth_angle = 220.0
# apply weather values individually
apply_weather_values(args, weather)
elif args.sun == 'sunset':
weather.sun_altitude_angle = 181
weather.sun_azimuth_angle = 100
else:
print("[ERROR]: Command [--sun | -s] '" + args.sun + "' not known")
sys.exit(1)
## Weather ##
if args.weather is not None:
if args.weather == 'clear':
weather.cloudiness = 10.0
weather.precipitation = 0.0
weather.precipitation_deposits = 0.0
weather.wind_intensity = 5.0
elif args.weather == 'overcast':
weather.cloudiness = 80.0
weather.precipitation = 0.0
weather.precipitation_deposits = 0.0
weather.wind_intensity = 50.0
elif args.weather == 'rain':
weather.cloudiness = 100.0
weather.precipitation = 80.0
weather.precipitation_deposits = 90.0
weather.wind_intensity = 100.0
weather.fog_density = 20.0
weather.fog_distance = 50.0
weather.wetness = 100.0
else:
print("[ERROR]: Command [--weather | -w] '" + args.weather + "' not known")
sys.exit(1)
if args.azimuth is not None:
weather.sun_azimuth_angle = args.azimuth
if args.altitude is not None:
weather.sun_altitude_angle = args.altitude
if args.clouds is not None:
weather.cloudiness = args.clouds
if args.rain is not None:
weather.precipitation = args.rain
if args.puddles is not None:
weather.precipitation_deposits = args.puddles
if args.wind is not None:
weather.wind_intensity = args.wind
if args.fog is not None:
weather.fog_density = args.fog
if args.fogdist is not None:
weather.fog_distance = args.fogdist
if args.wetness is not None:
weather.wetness = args.wetness
world.set_weather(weather)
if __name__ == '__main__':