Skip to content

Commit 732e651

Browse files
author
Fabian Oboril
committed
OSC Controllers
1 parent 1fc508e commit 732e651

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed

srunner/scenarioconfigs/openscenario_configuration.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,30 @@ def _set_actor_information(self):
242242
value = prop.get('value')
243243
args[key] = value
244244

245+
actor_controller = []
246+
for controller in obj.iter("ObjectController"):
247+
actor_controller.append(controller)
248+
245249
for catalog_reference in obj.iter("CatalogReference"):
246250
entry = OpenScenarioParser.get_catalog_entry(self.catalogs, catalog_reference)
247251
if entry.tag == "Vehicle":
248-
self._extract_vehicle_information(entry, rolename, entry, args)
252+
self._extract_vehicle_information(entry, rolename, entry, args, actor_controller)
249253
elif entry.tag == "Pedestrian":
250-
self._extract_pedestrian_information(entry, rolename, entry, args)
254+
self._extract_pedestrian_information(entry, rolename, entry, args, actor_controller)
251255
elif entry.tag == "MiscObject":
252-
self._extract_misc_information(entry, rolename, entry, args)
256+
self._extract_misc_information(entry, rolename, entry, args, actor_controller)
253257
else:
254258
self.logger.debug(
255259
" A CatalogReference specifies a reference that is not an Entity. Skipping...")
256260

257261
for vehicle in obj.iter("Vehicle"):
258-
self._extract_vehicle_information(obj, rolename, vehicle, args)
262+
self._extract_vehicle_information(obj, rolename, vehicle, args, actor_controller)
259263

260264
for pedestrian in obj.iter("Pedestrian"):
261-
self._extract_pedestrian_information(obj, rolename, pedestrian, args)
265+
self._extract_pedestrian_information(obj, rolename, pedestrian, args, actor_controller)
262266

263267
for misc in obj.iter("MiscObject"):
264-
self._extract_misc_information(obj, rolename, misc, args)
268+
self._extract_misc_information(obj, rolename, misc, args, actor_controller)
265269

266270
# Set transform for all actors
267271
# This has to be done in a multi-stage loop to resolve relative position settings
@@ -285,7 +289,7 @@ def _set_actor_information(self):
285289
if actor.transform is None:
286290
all_actor_transforms_set = False
287291

288-
def _extract_vehicle_information(self, obj, rolename, vehicle, args):
292+
def _extract_vehicle_information(self, obj, rolename, vehicle, args, actor_controller):
289293
"""
290294
Helper function to _set_actor_information for getting vehicle information from XML tree
291295
"""
@@ -301,25 +305,26 @@ def _extract_vehicle_information(self, obj, rolename, vehicle, args):
301305

302306
speed = self._get_actor_speed(rolename)
303307
new_actor = ActorConfigurationData(
304-
model, None, rolename, speed, color=color, category=category, args=args)
308+
model, None, rolename, speed, color=color, category=category, args=args, controller=actor_controller)
305309

306310
if ego_vehicle:
307311
self.ego_vehicles.append(new_actor)
308312
else:
309313
self.other_actors.append(new_actor)
310314

311-
def _extract_pedestrian_information(self, obj, rolename, pedestrian, args):
315+
def _extract_pedestrian_information(self, obj, rolename, pedestrian, args, actor_controller):
312316
"""
313317
Helper function to _set_actor_information for getting pedestrian information from XML tree
314318
"""
315319
model = pedestrian.attrib.get('model', "walker.*")
316320

317321
speed = self._get_actor_speed(rolename)
318-
new_actor = ActorConfigurationData(model, None, rolename, speed, category="pedestrian", args=args)
322+
new_actor = ActorConfigurationData(model, None, rolename, speed,
323+
category="pedestrian", args=args, controller=actor_controller)
319324

320325
self.other_actors.append(new_actor)
321326

322-
def _extract_misc_information(self, obj, rolename, misc, args):
327+
def _extract_misc_information(self, obj, rolename, misc, args, actor_controller):
323328
"""
324329
Helper function to _set_actor_information for getting vehicle information from XML tree
325330
"""
@@ -330,7 +335,8 @@ def _extract_misc_information(self, obj, rolename, misc, args):
330335
model = "static.prop.chainbarrier"
331336
else:
332337
model = misc.attrib.get('name')
333-
new_actor = ActorConfigurationData(model, None, rolename, category="misc", args=args)
338+
new_actor = ActorConfigurationData(model, None, rolename, category="misc",
339+
args=args, controller=actor_controller)
334340

335341
self.other_actors.append(new_actor)
336342

srunner/scenarioconfigs/scenario_configuration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ActorConfigurationData(object):
1919
"""
2020

2121
def __init__(self, model, transform, rolename='other', speed=0, autopilot=False,
22-
random=False, color=None, category="car", args=None):
22+
random=False, color=None, category="car", args=None, controller=None):
2323
self.model = model
2424
self.rolename = rolename
2525
self.transform = transform
@@ -29,6 +29,7 @@ def __init__(self, model, transform, rolename='other', speed=0, autopilot=False,
2929
self.color = color
3030
self.category = category
3131
self.args = args
32+
self.controller = controller
3233

3334
@staticmethod
3435
def parse_from_node(node, rolename):

srunner/scenariomanager/actorcontrols/simple_vehicle_control.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ def _set_new_velocity(self, next_location):
282282
self._actor.get_traffic_light_state() == carla.TrafficLightState.Red):
283283
target_speed = 0
284284

285-
if target_speed < current_speed:
285+
if target_speed < current_speed and math.fabs(target_speed - current_speed) > 0.01:
286+
print(target_speed, current_speed)
286287
self._actor.set_light_state(carla.VehicleLightState.Brake)
287288
if self._max_deceleration is not None:
288289
target_speed = max(target_speed, current_speed - (current_time -

srunner/scenarios/open_scenario.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,21 @@ def _create_init_behavior(self):
225225
if private.attrib.get('entityRef', None) == actor.rolename:
226226
for private_action in private.iter("PrivateAction"):
227227
for controller_action in private_action.iter('ControllerAction'):
228-
module, args = OpenScenarioParser.get_controller(
228+
module, args = OpenScenarioParser.get_controller_from_action(
229229
controller_action, self.config.catalogs)
230230
controller_atomic = ChangeActorControl(
231231
carla_actor, control_py_module=module, args=args,
232232
scenario_file_path=os.path.dirname(self.config.filename))
233233

234+
if actor.controller is not None:
235+
if controller_atomic is not None:
236+
print("Warning: A controller was already assigned to actor {}".format(actor.model))
237+
else:
238+
for controller in actor.controller:
239+
print(actor.rolename)
240+
module, args = OpenScenarioParser.get_controller(controller, self.config.catalogs)
241+
controller_atomic = ChangeActorControl(carla_actor, control_py_module=module, args=args)
242+
234243
if controller_atomic is None:
235244
controller_atomic = ChangeActorControl(carla_actor, control_py_module=None, args={})
236245

srunner/tools/openscenario_parser.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def get_weather_from_env_action(xml_tree, catalogs):
365365
return Weather(carla_weather, dtime, weather_animation)
366366

367367
@staticmethod
368-
def get_controller(xml_tree, catalogs):
368+
def get_controller_from_action(xml_tree, catalogs):
369369
"""
370370
Extract the object controller from the OSC XML or a catalog
371371
@@ -403,6 +403,38 @@ def get_controller(xml_tree, catalogs):
403403

404404
return module, args
405405

406+
@staticmethod
407+
def get_controller(xml_tree, catalogs):
408+
"""
409+
Extract the object controller from the OSC XML or a catalog
410+
411+
Args:
412+
xml_tree: Containing the controller information,
413+
or the reference to the catalog it is defined in.
414+
catalogs: XML Catalogs that could contain the EnvironmentAction
415+
416+
returns:
417+
module: Python module containing the controller implementation
418+
args: Dictonary with (key, value) parameters for the controller
419+
"""
420+
421+
properties = None
422+
if xml_tree.find('Controller') is not None:
423+
properties = xml_tree.find('Controller').find('Properties')
424+
elif xml_tree.find("CatalogReference") is not None:
425+
catalog_reference = xml_tree.find("CatalogReference")
426+
properties = OpenScenarioParser.get_catalog_entry(catalogs, catalog_reference).find('Properties')
427+
428+
module = None
429+
args = {}
430+
for prop in properties:
431+
if prop.attrib.get('name') == "module":
432+
module = prop.attrib.get('value')
433+
else:
434+
args[prop.attrib.get('name')] = prop.attrib.get('value')
435+
436+
return module, args
437+
406438
@staticmethod
407439
def get_route(xml_tree, catalogs):
408440
"""

0 commit comments

Comments
 (0)