-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathsimulator.py
316 lines (272 loc) · 11.6 KB
/
simulator.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Simulator interface for CARLA."""
try:
import carla
except ImportError as e:
raise ModuleNotFoundError('CARLA scenarios require the "carla" Python package') from e
import math
import os
import warnings
import scenic.core.errors as errors
if errors.verbosityLevel == 0: # suppress pygame advertisement at zero verbosity
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
import pygame
from scenic.core.simulators import SimulationCreationError
from scenic.domains.driving.simulators import DrivingSimulation, DrivingSimulator
from scenic.simulators.carla.blueprints import oldBlueprintNames
import scenic.simulators.carla.utils.utils as utils
import scenic.simulators.carla.utils.visuals as visuals
from scenic.syntax.veneer import verbosePrint
class CarlaSimulator(DrivingSimulator):
"""Implementation of `Simulator` for CARLA."""
def __init__(
self,
carla_map,
map_path,
address="127.0.0.1",
port=2000,
timeout=10,
render=True,
record="",
timestep=0.1,
traffic_manager_port=None,
):
super().__init__()
verbosePrint(f"Connecting to CARLA on port {port}")
self.client = carla.Client(address, port)
self.client.set_timeout(timeout) # limits networking operations (seconds)
if carla_map is not None:
try:
self.world = self.client.load_world(carla_map)
except Exception as e:
raise RuntimeError(f"CARLA could not load world '{carla_map}'") from e
else:
if str(map_path).endswith(".xodr"):
with open(map_path) as odr_file:
self.world = self.client.generate_opendrive_world(odr_file.read())
else:
raise RuntimeError("CARLA only supports OpenDrive maps")
self.timestep = timestep
if traffic_manager_port is None:
traffic_manager_port = port + 6000
self.tm = self.client.get_trafficmanager(traffic_manager_port)
self.tm.set_synchronous_mode(True)
# Set to synchronous with fixed timestep
settings = self.world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = timestep # NOTE: Should not exceed 0.1
self.world.apply_settings(settings)
verbosePrint("Map loaded in simulator.")
self.render = render # visualization mode ON/OFF
self.record = record # whether to use the carla recorder
self.scenario_number = 0 # Number of the scenario executed
def createSimulation(self, scene, *, timestep, **kwargs):
if timestep is not None and timestep != self.timestep:
raise RuntimeError(
"cannot customize timestep for individual CARLA simulations; "
"set timestep when creating the CarlaSimulator instead"
)
self.scenario_number += 1
return CarlaSimulation(
scene,
self.client,
self.tm,
self.render,
self.record,
self.scenario_number,
timestep=self.timestep,
**kwargs,
)
def destroy(self):
super().destroy()
settings = self.world.get_settings()
settings.synchronous_mode = False
settings.fixed_delta_seconds = None
self.world.apply_settings(settings)
self.tm.set_synchronous_mode(False)
class CarlaSimulation(DrivingSimulation):
def __init__(self, scene, client, tm, render, record, scenario_number, **kwargs):
self.client = client
self.world = self.client.get_world()
self.map = self.world.get_map()
self.blueprintLib = self.world.get_blueprint_library()
self.tm = tm
self.render = render
self.record = record
self.scenario_number = scenario_number
self.cameraManager = None
super().__init__(scene, **kwargs)
def setup(self):
weather = self.scene.params.get("weather")
if weather is not None:
if isinstance(weather, str):
self.world.set_weather(getattr(carla.WeatherParameters, weather))
elif isinstance(weather, dict):
self.world.set_weather(carla.WeatherParameters(**weather))
# Setup HUD
if self.render:
self.displayDim = (1280, 720)
self.displayClock = pygame.time.Clock()
self.camTransform = 0
pygame.init()
pygame.font.init()
self.hud = visuals.HUD(*self.displayDim)
self.display = pygame.display.set_mode(
self.displayDim, pygame.HWSURFACE | pygame.DOUBLEBUF
)
self.cameraManager = None
if self.record:
if not os.path.exists(self.record):
os.mkdir(self.record)
name = "{}/scenario{}.log".format(self.record, self.scenario_number)
# Carla is looking for an absolute path, so convert it if necessary.
name = os.path.abspath(name)
self.client.start_recorder(name)
# Create objects.
super().setup()
# Set up camera manager and collision sensor for ego
if self.render:
camIndex = 0
camPosIndex = 0
egoActor = self.objects[0].carlaActor
self.cameraManager = visuals.CameraManager(self.world, egoActor, self.hud)
self.cameraManager._transform_index = camPosIndex
self.cameraManager.set_sensor(camIndex)
self.cameraManager.set_transform(self.camTransform)
self.world.tick() ## allowing manualgearshift to take effect # TODO still need this?
for obj in self.objects:
if isinstance(obj.carlaActor, carla.Vehicle):
obj.carlaActor.apply_control(
carla.VehicleControl(manual_gear_shift=False)
)
self.world.tick()
for obj in self.objects:
if obj.speed is not None and obj.speed != 0:
raise RuntimeError(
f"object {obj} cannot have a nonzero initial speed "
"(this is not yet possible in CARLA)"
)
def createObjectInSimulator(self, obj):
# Extract blueprint
try:
blueprint = self.blueprintLib.find(obj.blueprint)
except IndexError as e:
found = False
if obj.blueprint in oldBlueprintNames:
for oldName in oldBlueprintNames[obj.blueprint]:
try:
blueprint = self.blueprintLib.find(oldName)
found = True
warnings.warn(
f"CARLA blueprint {obj.blueprint} not found; "
f"using older version {oldName}"
)
obj.blueprint = oldName
break
except IndexError:
continue
if not found:
raise SimulationCreationError(
f"Unable to find blueprint {obj.blueprint}" f" for object {obj}"
) from e
if obj.rolename is not None:
blueprint.set_attribute("role_name", obj.rolename)
# set walker as not invincible
if blueprint.has_attribute("is_invincible"):
blueprint.set_attribute("is_invincible", "False")
# Set up transform
loc = utils.scenicToCarlaLocation(
obj.position, world=self.world, blueprint=obj.blueprint
)
rot = utils.scenicToCarlaRotation(obj.orientation)
transform = carla.Transform(loc, rot)
# Color, cannot be set for Pedestrians
if blueprint.has_attribute("color") and obj.color is not None:
c = obj.color
c_str = f"{int(c.r*255)},{int(c.g*255)},{int(c.b*255)}"
blueprint.set_attribute("color", c_str)
# Create Carla actor
carlaActor = self.world.try_spawn_actor(blueprint, transform)
if carlaActor is None:
raise SimulationCreationError(f"Unable to spawn object {obj}")
obj.carlaActor = carlaActor
carlaActor.set_simulate_physics(obj.physics)
if isinstance(carlaActor, carla.Vehicle):
# TODO should get dimensions at compile time, not simulation time
extent = carlaActor.bounding_box.extent
ex, ey, ez = extent.x, extent.y, extent.z
# Ensure each extent is positive to work around CARLA issue #5841
obj.width = ey * 2 if ey > 0 else obj.width
obj.length = ex * 2 if ex > 0 else obj.length
obj.height = ez * 2 if ez > 0 else obj.height
carlaActor.apply_control(carla.VehicleControl(manual_gear_shift=True, gear=1))
elif isinstance(carlaActor, carla.Walker):
carlaActor.apply_control(carla.WalkerControl())
# spawn walker controller
controller_bp = self.blueprintLib.find("controller.ai.walker")
controller = self.world.try_spawn_actor(
controller_bp, carla.Transform(), carlaActor
)
if controller is None:
raise SimulationCreationError(
f"Unable to spawn carla controller for object {obj}"
)
obj.carlaController = controller
return carlaActor
def executeActions(self, allActions):
super().executeActions(allActions)
# Apply control updates which were accumulated while executing the actions
for obj in self.agents:
ctrl = obj._control
if ctrl is not None:
obj.carlaActor.apply_control(ctrl)
obj._control = None
def step(self):
# Run simulation for one timestep
self.world.tick()
# Render simulation
if self.render:
self.cameraManager.render(self.display)
pygame.display.flip()
def getProperties(self, obj, properties):
# Extract Carla properties
carlaActor = obj.carlaActor
currTransform = carlaActor.get_transform()
currLoc = currTransform.location
currRot = currTransform.rotation
currVel = carlaActor.get_velocity()
currAngVel = carlaActor.get_angular_velocity()
# Prepare Scenic object properties
position = utils.carlaToScenicPosition(currLoc)
velocity = utils.carlaToScenicPosition(currVel)
speed = math.hypot(*velocity)
angularSpeed = utils.carlaToScenicAngularSpeed(currAngVel)
angularVelocity = utils.carlaToScenicAngularVel(currAngVel)
globalOrientation = utils.carlaToScenicOrientation(currRot)
yaw, pitch, roll = obj.parentOrientation.localAnglesFor(globalOrientation)
elevation = utils.carlaToScenicElevation(currLoc)
values = dict(
position=position,
velocity=velocity,
speed=speed,
angularSpeed=angularSpeed,
angularVelocity=angularVelocity,
yaw=yaw,
pitch=pitch,
roll=roll,
elevation=elevation,
)
return values
def destroy(self):
for obj in self.objects:
if obj.carlaActor is not None:
if isinstance(obj.carlaActor, carla.Vehicle):
obj.carlaActor.set_autopilot(False, self.tm.get_port())
if isinstance(obj.carlaActor, carla.Walker):
obj.carlaController.stop()
obj.carlaController.destroy()
obj.carlaActor.destroy()
if self.render and self.cameraManager:
self.cameraManager.destroy_sensor()
self.client.stop_recorder()
self.world.tick()
super().destroy()