ucla-mobility / OpenCDA

A generalized framework for prototyping full-stack cooperative driving automation applications under CARLA+SUMO.
Other
988 stars 197 forks source link

[UCDavis] Add the OpenScenario module for unified autonomouos driving test #196

Closed gaodechen closed 1 year ago

gaodechen commented 1 year ago

The commit adds a new module for OpenCDA that supports running unified autonomouos driving test with custom autonomous driving algorithms and pre-defined scenarios.

Specifically, the program involves two pipeline executed concurrently: an ego vehicle algorithm runner where autonomous driving algorithms are adopted, and a ScenarioRunner pipeline that tests against preset scenarios.

To test the new module:

python opencda.py -t openscenario_carla -v 0.9.14

Changes

Prerequisites

Therefore, we can execute the following commands to set up ScenarioRunner environmental variables:

export SCENARIO_RUNNER_ROOT="/to/scenario_runner/installation/path"   # Change to your own installation path
export PYTHONPATH="$SCENARIO_RUNNER_ROOT:$PYTHONPATH"        # Add to Python API path to import
touch "$SCENARIO_RUNNER_ROOT/__init__.py"
ju40268 commented 1 year ago

Hi @gaodechen -

Thanks for the wrapping the PR with the detailed description πŸ˜„ I was able to follow the set up guidelines and have the scenario runner imported. When I am trying to run the scenario by

opencda-latest) ztu@lambda-quad:~/OpenCDA$ python opencda.py -t openscenario_carla -v 0.9.12
OpenCDA Version: 0.1.3
Process Process-1:
Traceback (most recent call last):
  File "/home/ztu/miniconda3/envs/opencda-latest/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/home/ztu/miniconda3/envs/opencda-latest/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ztu/OpenCDA/opencda/scenario_testing/openscenario_carla.py", line 25, in exec_scenario_runner
    scenario_runner = sr.ScenarioRunner(scenario_params.scenario)
AttributeError: module 'scenario_runner' has no attribute 'ScenarioRunner'
Waiting for the ego vehicle...
Waiting for the ego vehicle...
Waiting for the ego vehicle...

with the status on waiting for ego vehicle for a long time. I have my Carla running on the side in another thread. We have our Carla set up with 0.9.12

Did I miss any steps or is that expected? Thanks!

gaodechen commented 1 year ago

Hi @gaodechen -

Thanks for the wrapping the PR with the detailed description πŸ˜„ I was able to follow the set up guidelines and have the scenario runner imported. When I am trying to run the scenario by

opencda-latest) ztu@lambda-quad:~/OpenCDA$ python opencda.py -t openscenario_carla -v 0.9.12
OpenCDA Version: 0.1.3
Process Process-1:
Traceback (most recent call last):
  File "/home/ztu/miniconda3/envs/opencda-latest/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/home/ztu/miniconda3/envs/opencda-latest/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ztu/OpenCDA/opencda/scenario_testing/openscenario_carla.py", line 25, in exec_scenario_runner
    scenario_runner = sr.ScenarioRunner(scenario_params.scenario)
AttributeError: module 'scenario_runner' has no attribute 'ScenarioRunner'
Waiting for the ego vehicle...
Waiting for the ego vehicle...
Waiting for the ego vehicle...

with the status on waiting for ego vehicle for a long time. I have my Carla running on the side in another thread. We have our Carla set up with 0.9.12

Did I miss any steps or is that expected? Thanks!

Thank you @ju40268 for reviewing!

Could you check if there is an empty __init__.py under the ScenarioRunner folder? The file turns the ScenarioRunner folder into a regular Python package that can be imported by other Python codes.

Please try python -c 'import scenario_runner' once __init__.py is added to the folder and see if it can be imported then.

gaodechen commented 1 year ago

@ju40268

The path settings look good. Could you remove the __pycache__ folder and try importing again?

Or check out

>> import scenario_runner as sr
>> dir(sr)
['CarlaDataProvider', 'LooseVersion', 'OpenScenario', 'OpenScenarioConfiguration', 'RawTextHelpFormatter', 'RouteParser', 'RouteScenario', 'ScenarioConfigurationParser', 'ScenarioManager', 'ScenarioRunner', 'VERSION', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'argparse', 'carla', 'datetime', 'glob', 'importlib', 'inspect', 'json', 'main', 'os', 'pkg_resources', 'print_function', 'signal', 'sys', 'time', 'traceback']

and see if the classes were imported

ju40268 commented 1 year ago

I was able to run the code πŸŽ‰ seeing some scenarios showing up on my side. I'll have a detailed readme section on how to install the scenario running combing with our projects.

gaodechen commented 1 year ago

I was able to run the code πŸŽ‰ seeing some scenarios showing up on my side. I'll have a detailed readme section on how to install the scenario running combing with our projects.

Thank you for testing and reviewing our code!

DerrickXuNu commented 1 year ago

Thanks @ju40268 for reviewing this pr! Thanks @gaodechen for the useful feature! I am thinking can we make the installation a bit more easier? I feel we can add a bash script to directly do the path insert things for users

gaodechen commented 1 year ago

Thanks @ju40268 for reviewing this pr! Thanks @gaodechen for the useful feature! I am thinking can we make the installation a bit more easier? I feel we can add a bash script to directly do the path insert things for users

Agreed. Wondering if there is an installation script for OpenCDA? We can integrate ScenarioRunner scripts into that one or if you prefer a separate script for the ScenarioRunner feature?

DerrickXuNu commented 1 year ago

I think a separate bash script should be better as ScenarioRunner should be an optional feature in OpenCDA

ju40268 commented 1 year ago

I've read through the PR shortly but got basic question:

As I understand the main purpose of hooking up scenario runner with OpenCDA is that this should enable us to provide couple yaml / simple python file so that it's generating scenes, which an be inject into the OpenCDA. Later on OpenCDA could apply the rest algorithm. (ex. planning logic, apply ML models etc) From the code - it seems like it's been applying the control / scenarios with Carla directly instead of OpenCDA. ex, You have cav_world and sim_api but didn't performing further action based on this, and the major while loop is applying control of two vehicle following each other.

@DerrickXuNu Please correct me if I am wrong.

DerrickXuNu commented 1 year ago

Hi @ju40268 , thanks for your careful check. I agree with you. Scenario Runner should be a component inject to the whole OpenCDA. The current code does not use OpenCDA code functions, which is not ready for merge

gaodechen commented 1 year ago

Modifications on Apr 03

We added VehicleManager for openscenario_carla to be consistent with other OpenCDA configurations. So the ego vehicle is controlled by OpenCDA.

To support VehicleManager with the presence of ScenarioRunner, we have to create a new API create_vehicle_manager_from_scenario_runner in the second commit.

SR only supports single ego vehicle, and all the actors are initialized by the SR process. The original create_vehicle_manager API supports multiple vehicles, and initializes all the actors regardless what have been created in SR. To avoid duplicates in configurations and initialization and also support the single ego vehicle case, we add the following member function in sim_api following the same manner as create_vehicle_manager.

    def create_vehicle_manager_from_scenario_runner(self, vehicle):
        """
        Create a single CAV with a loaded ego vehicle from SR.
        Different from the create_vehicle_manager API creating Carla vehicle from scratch,
        SR creates on its own only supports 'single' vehicle.

        Parameters
        ----------
        vehicle:
            The Carla ego vehicle created by ScenarioRunner.

        Returns
        -------
        single_cav_list : list
            A list contains the singla CAV derived from the ego vehicle.
        """
        ...

We have integrated ScenarioRunner support in the first commit of this thread. We created a new scenario for an overtake action based on ScenarioRunner, where the ego vehicle needs to overtake a fast vehicle ahead and avoid collision with the background vehicles and two pedestrians.

To integrate any custom scenarios, configure the Python scripts (controlling the ego vehicle and actor behaviors) and .xml file (defining all the actors in the scenario) paths in .yaml.

scenario_runner:
  configFile: './opencda/scenario_testing/scenarios/Overtake.xml'
  # Absolute path needed here
  additionalScenario: '/home/ucdavis/OpenCDA/opencda/scenario_testing/scenarios/overtake.py'

We added a boundary check in map_manager.py since the nxt array at index 0 may not exist.

nxt = nxt.next(self.lane_sample_resolution)[0]
ju40268 commented 1 year ago

LGTM! Thanks for making this happen πŸŽ‰ πŸ˜‰ I just have two comments/questions -

  1. Could you list the steps required to add a new actor? I tried adding one in the <other_actor> section and appending apply behavior, but I didn't see it showing up.
  2. Later on, for a new scenario, we'll need to have one .py and one .xml file and additional Python code specifying their behavior, right? Would it be possible to have more for loop which avoids a similar codebase?
gaodechen commented 1 year ago

LGTM! Thanks for making this happen πŸŽ‰ πŸ˜‰ I just have two comments/questions -

  1. Could you list the steps required to add a new actor? I tried adding one in the <other_actor> section and appending apply behavior, but I didn't see it showing up.
  2. Later on, for a new scenario, we'll need to have one .py and one .xml file and additional Python code specifying their behavior, right? Would it be possible to have more for loop which avoids a similar codebase?

Thank you for the quick response, Christie!

  1. It is expected that the appended actor didn't show up. We only render and define the actor behaviors for 2 vehicles + 2 pedestrians in our overtake.py scripts, i.e., the first 4 actors in Overtake.xml. In other words, we have deterministic actors to test against here.
  2. Yes, we will need a .py script paired with a .xml to be compatible with SR. The .py defines the actor behaviors and testing criteria, and the .xml consists of all the actors to play the scenario.