-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
config.py
executable file
·256 lines (214 loc) · 7.67 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""
Configure and inspect an instance of CARLA Simulator.
For further details, visit
https://carla.readthedocs.io/en/latest/configuring_the_simulation/
"""
import glob
import os
import sys
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import argparse
import datetime
import re
import socket
import textwrap
def get_ip(host):
if host in ['localhost', '127.0.0.1']:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.connect(('10.255.255.255', 1))
host = sock.getsockname()[0]
except RuntimeError:
pass
finally:
sock.close()
return host
def find_weather_presets():
presets = [x for x in dir(carla.WeatherParameters) if re.match('[A-Z].+', x)]
return [(getattr(carla.WeatherParameters, x), x) for x in presets]
def list_options(client):
maps = [m.replace('/Game/Carla/Maps/', '') for m in client.get_available_maps()]
indent = 4 * ' '
def wrap(text):
return '\n'.join(textwrap.wrap(text, initial_indent=indent, subsequent_indent=indent))
print('weather presets:\n')
print(wrap(', '.join(x for _, x in find_weather_presets())) + '.\n')
print('available maps:\n')
print(wrap(', '.join(sorted(maps))) + '.\n')
def list_blueprints(world, bp_filter):
blueprint_library = world.get_blueprint_library()
blueprints = [bp.id for bp in blueprint_library.filter(bp_filter)]
print('available blueprints (filter %r):\n' % bp_filter)
for bp in sorted(blueprints):
print(' ' + bp)
print('')
def inspect(args, client):
address = '%s:%d' % (get_ip(args.host), args.port)
world = client.get_world()
elapsed_time = world.get_snapshot().timestamp.elapsed_seconds
elapsed_time = datetime.timedelta(seconds=int(elapsed_time))
actors = world.get_actors()
s = world.get_settings()
weather = 'Custom'
current_weather = world.get_weather()
for preset, name in find_weather_presets():
if current_weather == preset:
weather = name
if s.fixed_delta_seconds is None:
frame_rate = 'variable'
else:
frame_rate = '%.2f ms (%d FPS)' % (
1000.0 * s.fixed_delta_seconds,
1.0 / s.fixed_delta_seconds)
print('-' * 34)
print('address:% 26s' % address)
print('version:% 26s\n' % client.get_server_version())
print('map: % 22s' % world.get_map().name)
print('weather: % 22s\n' % weather)
print('time: % 22s\n' % elapsed_time)
print('frame rate: % 22s' % frame_rate)
print('rendering: % 22s' % ('disabled' if s.no_rendering_mode else 'enabled'))
print('sync mode: % 22s\n' % ('disabled' if not s.synchronous_mode else 'enabled'))
print('actors: % 22d' % len(actors))
print(' * spectator:% 20d' % len(actors.filter('spectator')))
print(' * static: % 20d' % len(actors.filter('static.*')))
print(' * traffic: % 20d' % len(actors.filter('traffic.*')))
print(' * vehicles: % 20d' % len(actors.filter('vehicle.*')))
print(' * walkers: % 20d' % len(actors.filter('walker.*')))
print('-' * 34)
def main():
argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--host',
metavar='H',
default='localhost',
help='IP of the host CARLA Simulator (default: localhost)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port of CARLA Simulator (default: 2000)')
argparser.add_argument(
'-d', '--default',
action='store_true',
help='set default settings')
argparser.add_argument(
'-m', '--map',
help='load a new map, use --list to see available maps')
argparser.add_argument(
'-r', '--reload-map',
action='store_true',
help='reload current map')
argparser.add_argument(
'--delta-seconds',
metavar='S',
type=float,
help='set fixed delta seconds, zero for variable frame rate')
argparser.add_argument(
'--fps',
metavar='N',
type=float,
help='set fixed FPS, zero for variable FPS (similar to --delta-seconds)')
argparser.add_argument(
'--rendering',
action='store_true',
help='enable rendering')
argparser.add_argument(
'--no-rendering',
action='store_true',
help='disable rendering')
argparser.add_argument(
'--no-sync',
action='store_true',
help='disable synchronous mode')
argparser.add_argument(
'--weather',
help='set weather preset, use --list to see available presets')
argparser.add_argument(
'-i', '--inspect',
action='store_true',
help='inspect simulation')
argparser.add_argument(
'-l', '--list',
action='store_true',
help='list available options')
argparser.add_argument(
'-b', '--list-blueprints',
metavar='FILTER',
help='list available blueprints matching FILTER (use \'*\' to list them all)')
if len(sys.argv) < 2:
argparser.print_help()
return
args = argparser.parse_args()
client = carla.Client(args.host, args.port, worker_threads=1)
client.set_timeout(10.0)
if args.default:
args.rendering = True
args.delta_seconds = 0.0
args.weather = 'Default'
args.no_sync = True
if args.map is not None:
print('load map %r.' % args.map)
world = client.load_world(args.map)
elif args.reload_map:
print('reload map.')
world = client.reload_world()
else:
world = client.get_world()
settings = world.get_settings()
if args.no_rendering:
print('disable rendering.')
settings.no_rendering_mode = True
elif args.rendering:
print('enable rendering.')
settings.no_rendering_mode = False
if args.no_sync:
print('disable synchronous mode.')
settings.synchronous_mode = False
if args.delta_seconds is not None:
settings.fixed_delta_seconds = args.delta_seconds
elif args.fps is not None:
settings.fixed_delta_seconds = (1.0 / args.fps) if args.fps > 0.0 else 0.0
if args.delta_seconds is not None or args.fps is not None:
if settings.fixed_delta_seconds > 0.0:
print('set fixed frame rate %.2f milliseconds (%d FPS)' % (
1000.0 * settings.fixed_delta_seconds,
1.0 / settings.fixed_delta_seconds))
else:
print('set variable frame rate.')
settings.fixed_delta_seconds = None
world.apply_settings(settings)
if args.weather is not None:
if not hasattr(carla.WeatherParameters, args.weather):
print('ERROR: weather preset %r not found.' % args.weather)
else:
print('set weather preset %r.' % args.weather)
world.set_weather(getattr(carla.WeatherParameters, args.weather))
if args.inspect:
inspect(args, client)
if args.list:
list_options(client)
if args.list_blueprints:
list_blueprints(world, args.list_blueprints)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')
except RuntimeError as e:
print(e)