ucla-mobility / OpenCDA

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

Spawn a new CAV at a certain simulation time step #166

Closed thuns001 closed 2 years ago

thuns001 commented 2 years ago

I was wondering if it is possible to generate a new single CAV on the on-ramp particularly for the scenario "platoon_joining_2lanefree_cosim". I tried to spawn a single cav on the on-ramp but when it reached to the merging area, about the same time as a mainline platoon (and it should perform a cut-in merge). It did not merge into the platoon.

Please advise if OpenCDA allows us to do this. My intent is to have the simulation run longer with more CAVs. (Spawning multiple CAVs at the simulation start is possible but is limited by space of link.)

Thank you, Thod

DerrickXuNu commented 2 years ago

Hi Thod,

Can you share your new yaml for this setting?

thuns001 commented 2 years ago

Hi Derrick,

Please find the python file and yaml file as attached. I modified a little bit on the python file to spawn a new cav on the on-ramp. (though with my little experience in coding.). Thanks for your quick response.

platoon_joining_2lanefree_cosim.zip

DerrickXuNu commented 2 years ago

Hi Thod,

Thanks for your file. I will look into it today and get back to you soon.

DerrickXuNu commented 2 years ago

Are you trying to let 2 cavs join the same platoon or each cav joins a different platoon?

thuns001 commented 2 years ago

Each cav joins in a different platoon. Right now I only spawned one cav to see if it’s possible to do so in OpenCDA.

Thanks

DerrickXuNu commented 2 years ago

First, OpenCDA does support different Cavs joining different platoons, but you need to give a good start position for both so they can meet each other at the merging area. Also, I saw your YAML file, the platoon member has a too large distance gap at the beginning. Make each member in the platoon around 10 meters distance. After you pay attention to the points I mentioned and that issue still happens, you could record a video with the log output together and send to me.

thuns001 commented 2 years ago

Sure I will do.

However the issue I have here is that the new one single cav that was spawned at simulation step 15 second, it did not join the platoon. Can you please also check if we are allowed to spawn a new vehicle at a certain time step. If so, what have I missed in the python file.

Thank you.

DerrickXuNu commented 2 years ago

I see. I haven't tried that before. Can you show a video with log output?

thuns001 commented 2 years ago

Hi Derrick:

Please find the attached files below for the video recording and log file.

vdo_log_OpenCDA.zip

thuns001 commented 2 years ago

Hi Derrick,

I just wonder if you have a chance looking at the files attached above. We really hope that spawning a new cav vehicle at any time step is possible and they can cooperate with the other cavs in the network.

thank you.

DerrickXuNu commented 2 years ago

Hi thuns,

I have just watched the video and have several questions. 1) For the cavs that are created at the beginning, did they join the platoon successfully? 2) Can you share the code snippet that generates the new cav?

thuns001 commented 2 years ago
  1. Yes the CAVs that spawned at the beginning of the simulation, they joined the platoon successfully.
  2. Here is the code snippet from the python scenario file that I modified and added some of the code.

import os import carla

import opencda.scenario_testing.utils.cosim_api as sim_api import opencda.scenario_testing.utils.customized_map_api as map_api from opencda.core.common.cav_world import CavWorld from opencda.scenario_testing.evaluations.evaluate_manager import \ EvaluationManager from opencda.scenario_testing.utils.yaml_utils import load_yaml

############################ Thod import traci from opencda.scenario_testing.utils.sim_api import ScenarioManager from opencda.core.common.vehicle_manager import VehicleManager import weakref from opencda.core.application.platooning.fsm import FSM from opencda.core.application.platooning.platooning_manager import \ PlatooningManager ################################

def run_scenario(opt, config_yaml):

#################### Added by Thod ######################################################## client = carla.Client('localhost', 2000) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() #################################################################################

try:
    scenario_params = load_yaml(config_yaml)

    # create CAV world
    cav_world = CavWorld(opt.apply_ml)

    # sumo conifg file path
    current_path = os.path.dirname(os.path.realpath(__file__))
    xodr_path = os.path.join(current_path,
                             '../assets/2lane_freeway_simplified/'
                             '2lane_freeway_simplified.xodr')
    sumo_cfg = os.path.join(current_path,
                            '../assets/2lane_freeway_simplified')

    # create co-simulation scenario manager
    scenario_manager = \
        sim_api.CoScenarioManager(scenario_params,
                                  opt.apply_ml,
                                  opt.version,
                                  xodr_path=xodr_path,
                                  cav_world=cav_world,
                                  sumo_file_parent_path=sumo_cfg)

    # create platoon members
    platoon_list = \
        scenario_manager.create_platoon_manager(
            map_helper=map_api.spawn_helper_2lanefree,
            data_dump=False)

    single_cav_list = \
        scenario_manager.create_vehicle_manager(application=['platooning'],
                                                map_helper=map_api.
                                                spawn_helper_2lanefree)

    # create evaluation manager
    eval_manager = \
        EvaluationManager(scenario_manager.cav_world,
                          script_name='single_2lanefree_cosim',
                          current_time=scenario_params['current_time'])

    spectator = scenario_manager.world.get_spectator()
    spectator_vehicle = platoon_list[0].vehicle_manager_list[1].vehicle

    while True:
        # simulation tick
        scenario_manager.tick()
        transform = spectator_vehicle.get_transform()
        spectator.set_transform(carla.Transform(transform.location +
                                                carla.Location(z=80),
                                                carla.Rotation(pitch=-90)))

        ################### Edited by Thod to generate a new signle CAV on the ramp. ###########################
        simsec = traci.simulation.getTime()
        if simsec == 15:
            for i, cav_config in enumerate(scenario_params['scenario']['single_cav_list']):
                # Only the first single CAV is re-spawned. 
                if i == 0: 
                    spawn_transform = map_api.spawn_helper_2lanefree('0.9.12', *cav_config['spawn_special'])
                    bp_lib = world.get_blueprint_library()
                    vehicle_bp = bp_lib.find('vehicle.tesla.model3')
                    vehicle = world.spawn_actor(vehicle_bp, spawn_transform)

                    # create vehicle manager for each cav
                    vehicle_manager = VehicleManager(vehicle, cav_config, ['platooning'], carla_map, cav_world, current_time=scenario_params['current_time'],data_dumping=False)

                    scenario_manager.tick()

                    vehicle_manager.v2x_manager.set_platoon(None)

                    destination = carla.Location(x=cav_config['destination'][0],y=cav_config['destination'][1],z=cav_config['destination'][2])

                    vehicle_manager.update_info()
                    vehicle_manager.set_destination(vehicle_manager.vehicle.get_location(), destination, clean=True)

                    single_cav_list.append(vehicle_manager)

#################################################################################################

        for platoon in platoon_list:
            platoon.update_information()
            platoon.run_step()

        for i, single_cav in enumerate(single_cav_list):
            if single_cav.v2x_manager.in_platoon():
                single_cav_list.pop(i)
            single_cav.update_info()
            control = single_cav.run_step()
            single_cav.vehicle.apply_control(control)

finally:
    eval_manager.evaluate()
    scenario_manager.close()

    for platoon in platoon_list:
        platoon.destroy()

    for v in single_cav_list:
        v.destroy()
DerrickXuNu commented 2 years ago

HI, I think respown the created vehicle may have some potential issues, as it was already in a platoon before. Have you tried created a totally new cav that is not created before?

thuns001 commented 2 years ago

I just tried spawning a new on-ramp cav from a different config yaml file. But the new CAV still did not merge into the platoon.

        scenario_params2 = load_yaml('/home/carma3/OpenCDA/opencda/scenario_testing/config_yaml/platoon_joining_2lanefree_cosim_spawnnewcavs.yaml')
        simsec = traci.simulation.getTime()
        if simsec == 15:
            for i, cav_config in enumerate(scenario_params2['scenario']['single_cav_list']):
                spawn_transform = map_api.spawn_helper_2lanefree('0.9.12', *cav_config['spawn_special'])
                bp_lib = world.get_blueprint_library()
                vehicle_bp = bp_lib.find('vehicle.tesla.model3')
                vehicle = world.spawn_actor(vehicle_bp, spawn_transform)

                # create vehicle manager for each cav
                vehicle_manager = VehicleManager(vehicle, cav_config, ['platooning'], carla_map, cav_world, current_time=scenario_params2['current_time'],data_dumping=False)

                scenario_manager.tick()

                vehicle_manager.v2x_manager.set_platoon(None)

                destination = carla.Location(x=cav_config['destination'][0],y=cav_config['destination'][1],z=cav_config['destination'][2])

                vehicle_manager.update_info()
                vehicle_manager.set_destination(vehicle_manager.vehicle.get_location(), destination, clean=True)

                single_cav_list.append(vehicle_manager)
thuns001 commented 2 years ago

I also have another question about how can we edit the opendrive file if we want to expand the lengths of the freeway and ramp.

Thank you,

DerrickXuNu commented 2 years ago

I just tried spawning a new on-ramp cav from a different config yaml file. But the new CAV still did not merge into the platoon.

        scenario_params2 = load_yaml('/home/carma3/OpenCDA/opencda/scenario_testing/config_yaml/platoon_joining_2lanefree_cosim_spawnnewcavs.yaml')
        simsec = traci.simulation.getTime()
        if simsec == 15:
            for i, cav_config in enumerate(scenario_params2['scenario']['single_cav_list']):
                spawn_transform = map_api.spawn_helper_2lanefree('0.9.12', *cav_config['spawn_special'])
                bp_lib = world.get_blueprint_library()
                vehicle_bp = bp_lib.find('vehicle.tesla.model3')
                vehicle = world.spawn_actor(vehicle_bp, spawn_transform)

                # create vehicle manager for each cav
                vehicle_manager = VehicleManager(vehicle, cav_config, ['platooning'], carla_map, cav_world, current_time=scenario_params2['current_time'],data_dumping=False)

                scenario_manager.tick()

                vehicle_manager.v2x_manager.set_platoon(None)

                destination = carla.Location(x=cav_config['destination'][0],y=cav_config['destination'][1],z=cav_config['destination'][2])

                vehicle_manager.update_info()
                vehicle_manager.set_destination(vehicle_manager.vehicle.get_location(), destination, clean=True)

                single_cav_list.append(vehicle_manager)

i saw you append a new cav in for i, cav_config in enumerate(scenario_params2['scenario']['single_cav_list']): , it will actually create multiple cavs in stead of one Also, can you make sure your created vehicle is visible in CAVWorld?

thuns001 commented 2 years ago

Hi Derrick,

My bad I forgot to mention that I loaded a different config yaml file, which there's only one signle CAV in the list.

Also, can you please show me how can I double-check if the newly created CAV is visible in the CAVWorld? Thank you.