|
17 | 17 | from agents.conf.behavior_types import Cautious, Aggressive, Normal |
18 | 18 | from agents.conf.agent_settings_backend import BehaviorAgentSettings, AgentConfig, SimpleConfig |
19 | 19 |
|
20 | | -from agents.tools.misc import get_speed, positive, is_within_distance, compute_distance |
| 20 | +from agents.tools.misc import get_speed, positive, is_within_distance, compute_distance, lanes_have_same_direction |
21 | 21 |
|
22 | 22 | class BehaviorAgent(BasicAgent): |
23 | 23 | """ |
@@ -115,44 +115,55 @@ def traffic_light_manager(self): |
115 | 115 |
|
116 | 116 | return affected |
117 | 117 |
|
| 118 | + def plan_lane_change(self, vehicle_list, order=["left", "right"], up_angle_th=180, low_angle_th=0, speed_limit_look_ahead_factor=0.5): |
| 119 | + if vehicle_list is None: |
| 120 | + vehicle_list = self._world.get_actors().filter("*vehicle*") |
| 121 | + waypoint = self.config.live_info.current_waypoint |
| 122 | + |
| 123 | + for direction in order: |
| 124 | + if direction == "right": |
| 125 | + right_turn = waypoint.right_lane_marking.lane_change |
| 126 | + can_change = (right_turn == carla.LaneChange.Right or right_turn == carla.LaneChange.Both) |
| 127 | + other_wpt = waypoint.get_right_lane() |
| 128 | + lane_offset = 1 |
| 129 | + else: |
| 130 | + left_turn = waypoint.left_lane_marking.lane_change |
| 131 | + can_change = (left_turn == carla.LaneChange.Left or left_turn == carla.LaneChange.Both) |
| 132 | + other_wpt = waypoint.get_left_lane() |
| 133 | + lane_offset = -1 |
| 134 | + if can_change and lanes_have_same_direction(waypoint, other_wpt) and other_wpt.lane_type == carla.LaneType.Driving: |
| 135 | + # Detect if right lane is free |
| 136 | + affected_by_vehicle, _, _ = self._vehicle_obstacle_detected(vehicle_list, |
| 137 | + max(self.config.distance.min_proximity_threshold, |
| 138 | + self.config.live_info.current_speed_limit * speed_limit_look_ahead_factor), |
| 139 | + up_angle_th=up_angle_th, |
| 140 | + low_angle_th=low_angle_th, |
| 141 | + lane_offset=lane_offset) |
| 142 | + if not affected_by_vehicle: |
| 143 | + print("Changing lane, moving to the %s!", direction) |
| 144 | + end_waypoint = self._local_planner.target_waypoint |
| 145 | + self.set_destination(end_waypoint.transform.location, |
| 146 | + other_wpt.transform.location) |
| 147 | + return True |
| 148 | + |
| 149 | + |
| 150 | + |
118 | 151 | def _tailgating(self, waypoint, vehicle_list): |
119 | 152 | """ |
120 | 153 | This method is in charge of tailgating behaviors. |
| 154 | + If a faster vehicle is behind the agent it will try to change the lane. |
121 | 155 |
|
122 | | - :param location: current location of the agent |
123 | 156 | :param waypoint: current waypoint of the agent |
124 | 157 | :param vehicle_list: list of all the nearby vehicles |
125 | 158 | """ |
126 | 159 |
|
127 | | - left_turn = waypoint.left_lane_marking.lane_change |
128 | | - right_turn = waypoint.right_lane_marking.lane_change |
129 | | - |
130 | | - left_wpt = waypoint.get_left_lane() |
131 | | - right_wpt = waypoint.get_right_lane() |
132 | | - |
133 | 160 | behind_vehicle_state, behind_vehicle, _ = self._vehicle_obstacle_detected(vehicle_list, max( |
134 | 161 | self.config.distance.min_proximity_threshold, self.config.live_info.current_speed_limit / 2), up_angle_th=180, low_angle_th=160) |
135 | 162 | if behind_vehicle_state and self.config.live_info.current_speed < get_speed(behind_vehicle): |
136 | | - if (right_turn == carla.LaneChange.Right or right_turn == |
137 | | - carla.LaneChange.Both) and waypoint.lane_id * right_wpt.lane_id > 0 and right_wpt.lane_type == carla.LaneType.Driving: |
138 | | - new_vehicle_state, _, _ = self._vehicle_obstacle_detected(vehicle_list, max( |
139 | | - self.config.distance.min_proximity_threshold, self.config.live_info.current_speed_limit / 2), up_angle_th=180, lane_offset=1) |
140 | | - if not new_vehicle_state: |
141 | | - print("Tailgating, moving to the right!") |
142 | | - end_waypoint = self._local_planner.target_waypoint |
143 | | - self._tailgate_counter = 200 |
144 | | - self.set_destination(end_waypoint.transform.location, |
145 | | - right_wpt.transform.location) |
146 | | - elif left_turn == carla.LaneChange.Left and waypoint.lane_id * left_wpt.lane_id > 0 and left_wpt.lane_type == carla.LaneType.Driving: |
147 | | - new_vehicle_state, _, _ = self._vehicle_obstacle_detected(vehicle_list, max( |
148 | | - self.config.distance.min_proximity_threshold, self.config.live_info.current_speed_limit / 2), up_angle_th=180, lane_offset=-1) |
149 | | - if not new_vehicle_state: |
150 | | - print("Tailgating, moving to the left!") |
151 | | - end_waypoint = self._local_planner.target_waypoint |
152 | | - self._tailgate_counter = 200 |
153 | | - self.set_destination(end_waypoint.transform.location, |
154 | | - left_wpt.transform.location) |
155 | | - |
| 163 | + changes_lane = self.plan_lane_change(vehicle_list, order=("right", "left")) |
| 164 | + if changes_lane: |
| 165 | + self._tailgate_counter = 200 |
| 166 | + |
156 | 167 | def collision_and_car_avoid_manager(self, waypoint): |
157 | 168 | """ |
158 | 169 | This module is in charge of warning in case of a collision |
|
0 commit comments