-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
244 lines (212 loc) · 8.2 KB
/
main.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
import argparse
import time
import multiprocessing
import os
import functools
import logging
import carla
from sensors.apollo_control import listen_and_apply_control
from sensors.utils import setup_sensors
from pygame_viewer import view_game
from utils import is_actor_exist, load_json, get_vehicle_by_role_name
from scenario_runner import scenario_run
from srunner.tools.scenario_parser \
import ScenarioConfigurationParser as SrCfgP
from dreamview_api import Connection as DreamviewConn
def destroy_all_sensors(world):
sensor_list = world.get_actors().filter("*sensor*")
"""Destroys all actors"""
for actor in sensor_list:
if actor is not None:
actor.destroy()
def logging_wrapper(func):
@functools.wraps(func)
def wrapper(log_dir: str, name: str, *args, **kwargs):
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
timestr = time.strftime("%Y%m%d-%H%M%S")
logfilepath = os.path.join(log_dir, f"{name}.{timestr}.log")
if os.path.isfile(logfilepath):
os.remove(logfilepath)
logging.basicConfig(
filename=logfilepath,
level=logging.INFO,
format='%(asctime)s %(message)s')
func(*args, **kwargs)
return wrapper
@logging_wrapper
def run_pygame(*args, **kwargs):
return view_game(*args, **kwargs)
@logging_wrapper
def run_control(*args, **kwargs):
return listen_and_apply_control(*args, **kwargs)
@logging_wrapper
def run_sensors(*args, **kwargs):
return setup_sensors(*args, **kwargs)
@logging_wrapper
def run_scenario(*args, **kwargs):
return scenario_run(*args, **kwargs)
def get_args():
argparser = argparse.ArgumentParser(
description='Run scenarios which run in carla, '
'and the AD statck is Apollo')
argparser.add_argument(
'--carla-host',
default='127.0.0.1',
help='carla server ip addr (default: 127.0.0.1)')
argparser.add_argument(
'--carla-port',
default=2000,
type=int,
help='carla port to connect to (default: 2000)')
argparser.add_argument(
'--apollo-host',
default='127.0.0.1',
help='apollo server ip addr (default: 127.0.0.1)')
argparser.add_argument(
'--apollo-port',
default=9090,
type=int,
help='apollo port to connect to (default: 9090)')
argparser.add_argument(
'--dreamview-port',
default=8888,
type=int,
help='apollo dreamview port (default: 8888)')
argparser.add_argument(
'--adc',
default='hero',
help='ego vehicle role name')
argparser.add_argument(
'--show',
action='store_true',
help='display adc in pygame')
argparser.add_argument(
'--timeout',
type=float,
default=20.0,
help='Set the CARLA client timeout value in seconds')
argparser.add_argument(
'--sensor-config',
default="sensor_configs/apollo_600_modular_testing.json",
help='specify sensor config file path')
argparser.add_argument(
'--log-dir',
default="./logs",
help='where to store log files')
argparser.add_argument('--configFile',
default='./scenario_configs/'
'743_borrow_lane_cfg.json',
help='Provide a scenario configuration file '
'(*.json)')
args = argparser.parse_args()
# communication with apollo and carla args
ac_args = argparse.Namespace()
ac_args.carla_host = args.carla_host
ac_args.carla_port = args.carla_port
ac_args.apollo_host = args.apollo_host
ac_args.apollo_port = args.apollo_port
ac_args.dreamview_port = args.dreamview_port
ac_args.adc = args.adc
ac_args.show = args.show
ac_args.timeout = args.timeout
ac_args.sensor_config = args.sensor_config
ac_args.log_dir = args.log_dir
# sr_host_keys = ['host', 'port', 'timeout']
sr_host_dict = dict()
sr_host_dict.update({'host': args.carla_host})
sr_host_dict.update({'port': args.carla_port})
sr_host_dict.update({'timeout': args.timeout})
scenario_configurations = []
sr_config = load_json(args.configFile)
sr_config.update({'list': False})
scenario_configurations = \
SrCfgP.parse_scenario_configuration_with_customer_param(
sr_config['scenario'],
sr_config['configFile'])
sr_config.update(
{'scenario_configurations': scenario_configurations})
if len(scenario_configurations) > 0:
ac_args.dst_x = scenario_configurations[0].destination.x
ac_args.dst_y = scenario_configurations[0].destination.y
ac_args.dst_z = scenario_configurations[0].destination.z
# scenario runner args
sr_args = {**sr_host_dict, **sr_config}
return ac_args, argparse.Namespace(**sr_args)
def main(ac_args: argparse.Namespace, sr_args: argparse.Namespace):
# logging.basicConfig(level=logging.INFO)
apollo_host = ac_args.apollo_host
apollo_port = ac_args.apollo_port
dreamview_port = ac_args.dreamview_port
carla_host = ac_args.carla_host
carla_port = ac_args.carla_port
ego_role_name = ac_args.adc
show = ac_args.show
carla_timeout = ac_args.timeout
sensor_config = load_json(ac_args.sensor_config)
log_dir = ac_args.log_dir
dst_x = ac_args.dst_x
dst_y = ac_args.dst_y
dst_z = ac_args.dst_z
sim_world = None
child_pid_file = open("pids.txt", "w")
try:
client = carla.Client(carla_host, carla_port)
client.set_timeout(carla_timeout)
sim_world = client.get_world()
sim_map = sim_world.get_map()
settings = sim_world.get_settings()
settings.synchronous_mode = False
settings.fixed_delta_seconds = 0.02
sim_world.apply_settings(settings)
scenario_runner = multiprocessing.Process(
target=run_scenario,
args=(log_dir, "scenario_runner", sr_args))
scenario_runner.start()
child_pid_file.write(f"scenario_runner pid: {scenario_runner.pid}\n")
player, _ = get_vehicle_by_role_name(
__name__, sim_world, ego_role_name)
dreamview_conn = DreamviewConn(apollo_host, dreamview_port)
start_waypoint = sim_map.get_waypoint(player.get_location())
end_waypoint = sim_map.get_waypoint(
carla.Location(x=dst_x, y=dst_y, z=dst_z))
dreamview_conn.set_destination(start_waypoint, end_waypoint)
if show:
viewer = multiprocessing.Process(
target=run_pygame,
args=(log_dir, "view_game", ego_role_name, carla_host,
carla_port, 1080, 720))
viewer.start()
child_pid_file.write(f"viewer pid: {viewer.pid}\n")
control_sensor = multiprocessing.Process(
target=run_control,
args=(log_dir, "apollo_control", ego_role_name,
carla_host, carla_port, apollo_host,
apollo_port))
control_sensor.start()
child_pid_file.write(f"control_sensor pid: {control_sensor.pid}\n")
sensors_config = multiprocessing.Process(
target=run_sensors,
args=(log_dir, "carla_sensors",
ego_role_name, carla_host,
carla_port, apollo_host, apollo_port,
sensor_config))
sensors_config.start()
child_pid_file.write(f"sensors_config pid: {sensors_config.pid}\n")
child_pid_file.close()
while True:
if not is_actor_exist(sim_world, role_name=ego_role_name):
break
time.sleep(1)
finally:
if not child_pid_file.closed:
child_pid_file.close()
if sim_world is not None:
settings = sim_world.get_settings()
settings.synchronous_mode = False
settings.fixed_delta_seconds = None
sim_world.apply_settings(settings)
destroy_all_sensors(sim_world)
if __name__ == "__main__":
ac_args, sr_args = get_args()
main(ac_args, sr_args)