当前位置:   article > 正文

carla与sumo联合仿真_carla sumo联合仿真

carla sumo联合仿真

一、基础使用

sumo在联合仿真中的作用就是代替了traffic manager,联合仿真的思路就是在两个软件都有pythonAPI(carla、traci)的情况下,carla有变化就改sumo,sumo有变化就改carla。总的来说,这两个软件不是从源码层次联合的,更像carla后来单独写的联合脚本,分别调用两个软件的api。

1、运行carla仿真器,选择相同地图
./CarlaUE4.sh,
2、运行sumo仿真器、启动联合仿真脚本
python3 run_synchronization.py examples/Town04.sumocfg  --sumo-gui
  • 1
  • 2
  • 3
  • 4

二、synchronization_loop()

synchronization_loop函数是run_synchronization.py整个文件的核心,目的就是初始化sumo和carla两个模拟器

def synchronization_loop(args):
    """
    Entry point for sumo-carla co-simulation.
    """
    sumo_simulation = SumoSimulation(args.sumo_cfg_file, args.step_length, args.sumo_host,                               
                                     args.sumo_port, args.sumo_gui, args.client_order)                                   #sumo仿真器
    carla_simulation = CarlaSimulation(args.carla_host, args.carla_port, args.step_length)                               #carla仿真器

    synchronization = SimulationSynchronization(sumo_simulation, carla_simulation, args.tls_manager,
                                                args.sync_vehicle_color, args.sync_vehicle_lights)                       #同步桥
    try:
        while True:
            start = time.time()

            synchronization.tick()                           #同步更新,关键

            end = time.time()
            elapsed = end - start
            if elapsed < args.step_length: 
                time.sleep(args.step_length - elapsed)       #停止同步时间

    except KeyboardInterrupt:
        logging.info('Cancelled by user.')

    finally:
        logging.info('Cleaning synchronization')

        synchronization.close()
  • 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

三、tick()

tick()是synchronization_loop()的核心,下面是sumo->carla的代码,之后还有carla->sumo的代码。基本逻辑都相同,分为仿真更新、新角色生成、旧角色删除、更新现有角色位置、更新红绿灯状态五步。

        # -----------------
        # sumo-->carla sync
        # -----------------
        self.sumo.tick()                                   #更新上次仿真的结果

        # Spawning new sumo actors in carla (i.e, not controlled by carla).
        sumo_spawned_actors = self.sumo.spawned_actors - set(self.carla2sumo_ids.values())              #找到sumo中有,而carla中没有的生成
        for sumo_actor_id in sumo_spawned_actors:                                                                                         
            self.sumo.subscribe(sumo_actor_id)
            sumo_actor = self.sumo.get_actor(sumo_actor_id)

            carla_blueprint = BridgeHelper.get_carla_blueprint(sumo_actor, self.sync_vehicle_color)
            if carla_blueprint is not None:
                carla_transform = BridgeHelper.get_carla_transform(sumo_actor.transform,
                                                                   sumo_actor.extent)

                carla_actor_id = self.carla.spawn_actor(carla_blueprint, carla_transform)
                if carla_actor_id != INVALID_ACTOR_ID:
                    self.sumo2carla_ids[sumo_actor_id] = carla_actor_id
            else:
                self.sumo.unsubscribe(sumo_actor_id)

        # Destroying sumo arrived actors in carla.                                                      #摧毁sumo中已经结束任务的车辆
        for sumo_actor_id in self.sumo.destroyed_actors:
            if sumo_actor_id in self.sumo2carla_ids:
                self.carla.destroy_actor(self.sumo2carla_ids.pop(sumo_actor_id))

        # Updating sumo actors in carla.                                                                 #在carla中更新sumo的人物位置
        for sumo_actor_id in self.sumo2carla_ids:
            carla_actor_id = self.sumo2carla_ids[sumo_actor_id]

            sumo_actor = self.sumo.get_actor(sumo_actor_id)
            carla_actor = self.carla.get_actor(carla_actor_id)

            carla_transform = BridgeHelper.get_carla_transform(sumo_actor.transform,
                                                               sumo_actor.extent)
            if self.sync_vehicle_lights:
                carla_lights = BridgeHelper.get_carla_lights_state(carla_actor.get_light_state(),
                                                                   sumo_actor.signals)
            else:
                carla_lights = None

            self.carla.synchronize_vehicle(carla_actor_id, carla_transform, carla_lights)

        # Updates traffic lights in carla based on sumo information.                                     #在carla中更新sumo的红绿灯
        if self.tls_manager == 'sumo':
            common_landmarks = self.sumo.traffic_light_ids & self.carla.traffic_light_ids
            for landmark_id in common_landmarks:
                sumo_tl_state = self.sumo.get_traffic_light_state(landmark_id)
                carla_tl_state = BridgeHelper.get_carla_traffic_light_state(sumo_tl_state)

                self.carla.synchronize_traffic_light(landmark_id, carla_tl_state)
  • 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
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号