Carla模块介绍之BehaviorAgent类

1.BehaviorAgent:

        BehaviorAgent实现一个代理,该代理导航场景以到达给定的目标目的地,通过计算到它的最短可能路径。该代理可以正确遵循交通标志、速度限制、,交通信号灯,同时也考虑到附近的车辆。换车道可以通过分析周围环境做出决策,例如超车或避免尾随。可以添加到这些行为,代理还可以与前面的汽车保持安全距离通过跟踪碰撞的瞬时时间并将其保持在一定范围内。最后,从谨慎的角度出发,你可以在代理中编码不同的行为和控制。

2. 模块位于以及引用为

from carla.agents.navigation.behavior_agent import BehaviorAgent

agent = BehaviorAgent(ego_vehicle, behavior="cautious")

goal_spawn_points = world.get_map().get_spawn_points()[27]

#random.shuffle(goal_spawn_points)

if goal_spawn_points.location != agent.vehicle.get_location():

    destination = goal_spawn_points.location

else:

    destination = goal_spawn_points.location

agent.set_destination(agent.vehicle.get_location(), destination, clean=True)

while True:
    
    agent.update_information()

    world.tick()

    if len(agent._local_planner.waypoints_queue)<1:

        print('======== Success, Arrivied at Target Point!')

        break

    spectator = world.get_spectator()

    transform = ego_vehicle.get_transform()

    spectator.set_transform(carla.Transform(transform.location ++carla.Location(z=40),carla.Rotation(pitch=-90)))

    speed_limit = ego_vehicle.get_speed_limit()

    agent.get_local_planner().set_speed(speed_limit)

    control = agent.run_step(debug=True)

    ego_vehicle.apply_control(control)

3.重要代码片段解读(原代码见附件)

(1)类中有三个参数,要控制的参与者、是否忽略红绿灯、行为模式(有三个模式,挑normal介绍),可以看到normal模式下面是Cautions()对象,三个模式之间只是对速度等一些信息进行了限制,具体可以看Normal()类。

def __init__(self, vehicle, ignore_traffic_light=False, behavior='normal'):

Carla模块介绍之BehaviorAgent类_第1张图片

(2)重点,自动驾驶中的全局路径规划,LocalPlanner(),后续再研究。

self._local_planner = LocalPlanner(self)

(3) BehaviorAgent中共有如下几个函数

a:   update_information(self):

        这个函数更新了关于控制物体的信息。有当前速度,速度限制,并把速度限制、目标点传给LocalPlanner(),并且更新控制物体的动作,自车的航向角(航向角由LocalPlanner()规划获取),交通灯的状态。

 def update_information(self):
        """
        This method updates the information regarding the ego
        vehicle based on the surrounding world.
        """

b:    set_destination(self, start_location, end_location, clean=False):

        该函数主要依据起点和终点创建一个全局路径,并把全局路径传递给LocalPlanner()

 def set_destination(self, start_location, end_location, clean=False):
        """
        This method creates a list of waypoints from agent's position to destination location
        based on the route returned by the global router.

            :param start_location: initial position
            :param end_location: final position
            :param clean: boolean to clean the waypoint queue
        """

c:   reroute(self, spawn_points):

        该函数是自动驾驶里面的重规划函数吧,接近全局路径终点,但是没有到终点就可以调用该函数。

 def reroute(self, spawn_points):
        """
        This method implements re-routing for vehicles approaching its destination.
        It finds a new target and computes another path to reach it.

            :param spawn_points: list of possible destinations for the agent
        """

d:  _trace_route(self, start_waypoint, end_waypoint):

        依据起点和终点生成一个全局路径,利用GlobalRoutePlannerDAO,它的主要作用就是提取carla 地图的拓朴结构,并且在set_destination函数中,把这个全局路径赋LocalBehavior

def _trace_route(self, start_waypoint, end_waypoint):
        """
        This method sets up a global router and returns the
        optimal route from start_waypoint to end_waypoint.

            :param start_waypoint: initial position
            :param end_waypoint: final position
        """

e:traffic_light_manager(self, waypoint):

        该函数是交通灯管理,根据要控制物体的位置(路口或者直线),返回交通灯的状态。个人理解应该是返回交通灯状态,便于下发指令给控制物体行为。

def traffic_light_manager(self, waypoint):
        """
        This method is in charge of behaviors for red lights and stops.

        WARNING: What follows is a proxy to avoid having a car brake after running a yellow light.
        This happens because the car is still under the influence of the semaphore,
        even after passing it. So, the semaphore id is temporarely saved to
        ignore it and go around this issue, until the car is near a new one.

            :param waypoint: current waypoint of the agent
        """

f: _overtake(self, location, waypoint, vehicle_list):

        超车动作,输入有控制物体位置,当前路段,周围车辆列表。该地方判断了从左边超车还是右边超车,其判断思维可以学习一下,大概考虑到是否可行驶,绕行安全等因素。

  def _overtake(self, location, waypoint, vehicle_list):
        """
        This method is in charge of overtaking behaviors.

            :param location: current location of the agent
            :param waypoint: current waypoint of the agent
            :param vehicle_list: list of all the nearby vehicles
        """

g: _tailgating(self, location, waypoint, vehicle_list):

        跟车行为,可以结合超车行为看一下里面具体逻辑

def _tailgating(self, location, waypoint, vehicle_list):
        """
        This method is in charge of tailgating behaviors.

            :param location: current location of the agent
            :param waypoint: current waypoint of the agent
            :param vehicle_list: list of all the nearby vehicles
        """

h:collision_and_car_avoid_manager(self, location, waypoint):

        碰撞行为的检测。以及检测到碰撞之后要采取的行为

def collision_and_car_avoid_manager(self, location, waypoint):
        """
        This module is in charge of warning in case of a collision
        and managing possible overtaking or tailgating chances.

            :param location: current location of the agent
            :param waypoint: current waypoint of the agent
            :return vehicle_state: True if there is a vehicle nearby, False if not
            :return vehicle: nearby vehicle
            :return distance: distance to nearby vehicle
        """

i:  pedestrian_avoid_manager(self, location, waypoint):

        行人碰撞检测

def pedestrian_avoid_manager(self, location, waypoint):
        """
        This module is in charge of warning in case of a collision
        with any pedestrian.

            :param location: current location of the agent
            :param waypoint: current waypoint of the agent
            :return vehicle_state: True if there is a walker nearby, False if not
            :return vehicle: nearby walker
            :return distance: distance to nearby walker
        """

j:car_following_manager(self, vehicle, distance, debug=False):

        跟车行为管理


    def car_following_manager(self, vehicle, distance, debug=False):
        """
        Module in charge of car-following behaviors when there's
        someone in front of us.

            :param vehicle: car to follow
            :param distance: distance from vehicle
            :param debug: boolean for debugging
            :return control: carla.VehicleControl
        """

k:run_step(self, debug=False):

        车辆控制,结合以上函数的像跟车等等函数的输出,计算出自车的行为,速度限度,如果需要除行走,跟车以外的控制,调用LocalPlanner里面的run_step函数。

 def run_step(self, debug=False):
        """
        Execute one step of navigation.

            :param debug: boolean for debugging
            :return control: carla.VehicleControl
        """

4.结论

        感觉这个类主要还是像大脑一样,执行什么动作,具体的局部路径规划应该在LocalPlanner里面,还有地图拓扑结构的获取。所以之后我觉得更深入了解一下以上两个函数,至于这个类,我觉得了解他是一个大脑就行,依据外界信息决策了控制者的行为,感觉有点自动驾驶中决策的味道。

你可能感兴趣的:(carla,python)