File size: 6,497 Bytes
c1eaa40 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
from re import A
from typing import Dict, List, Tuple
import warnings
import numpy as np
import numpy.typing as npt
from shapely.geometry import Point
from nuplan.common.maps.abstract_map import AbstractMap
from nuplan.common.actor_state.state_representation import StateSE2
from nuplan.common.maps.maps_datatypes import SemanticMapLayer
from nuplan.common.maps.abstract_map_objects import (
LaneGraphEdgeMapObject,
RoadBlockGraphEdgeMapObject,
)
from navsim.planning.simulation.planner.pdm_planner.utils.route_utils import (
route_roadblock_correction,
)
from navsim.planning.simulation.planner.pdm_planner.utils.graph_search.dijkstra import (
Dijkstra,
)
from navsim.planning.simulation.planner.pdm_planner.utils.pdm_path import PDMPath
from navsim.planning.simulation.planner.pdm_planner.utils.pdm_geometry_utils import (
convert_absolute_to_relative_se2_array,
)
from navsim.planning.simulation.planner.pdm_planner.utils.pdm_enums import (
SE2Index,
)
# shapely runtime warning
warnings.filterwarnings("ignore", category=RuntimeWarning)
def get_driving_command(
ego_pose: StateSE2,
map_api: AbstractMap,
route_roadblock_ids: List[str],
distance: float = 20,
lateral_offset: float = 2,
) -> npt.NDArray[np.int]:
"""
Creates the one-hot (left, forward, right)driving command for the ego vehicle
:param ego_pose: (x, y, heading) object for global ego pose
:param map_api: nuPlan's map interface
:param route_roadblock_ids: roadblock ids of route
:param distance: longitudinal distance to interpolate on th centerline, defaults to 10
:param lateral_offset: lateral offset for left/right threshold, to defaults to 2
:return: numpy one-hot array of driving_command
"""
driving_command = np.zeros((3,), dtype=int) # one-hot: (left, forward, right)
# If no route available, go straight
if len(route_roadblock_ids) == 0:
driving_command[1] = 1
return driving_command
# Apply route correction on route_roadblock_ids
route_roadblock_dict, _ = get_route_dicts(map_api, route_roadblock_ids)
corrected_route_roadblock_ids = route_roadblock_correction(
ego_pose, map_api, route_roadblock_dict
)
route_roadblock_dict, route_lane_dict = get_route_dicts(map_api, corrected_route_roadblock_ids)
# Find the nearest lane, graph search, and centerline extraction
current_lane = get_current_lane(ego_pose, route_lane_dict)
discrete_centerline = get_discrete_centerline(
current_lane, route_roadblock_dict, route_lane_dict
)
centerline = PDMPath(discrete_centerline)
# Interpolate target distance on centerline
current_progress = centerline.project(Point(*ego_pose.array))
target_progress = current_progress + distance
current_pose_array, target_pose_array = centerline.interpolate(
[current_progress, target_progress], as_array=True
)
target_pose_array = convert_absolute_to_relative_se2_array(
StateSE2(*current_pose_array), target_pose_array[None,...]
)[0]
# Threshold for driving command
if target_pose_array[SE2Index.Y] >= lateral_offset:
driving_command[0] = 1
elif target_pose_array[SE2Index.Y] <= -lateral_offset:
driving_command[2] = 1
else:
driving_command[1] = 1
# delete some variables for memory management
del route_roadblock_dict, route_lane_dict, _, centerline
return driving_command
def get_route_dicts(
map_api: AbstractMap, route_roadblock_ids: List[str]
) -> Tuple[Dict[str, RoadBlockGraphEdgeMapObject], Dict[str, LaneGraphEdgeMapObject]]:
"""
Loads the roadblock and lane dicts
:param map_api: nuPlan's map interface
:param route_roadblock_ids: roadblock ids of route
:return: tuple of roadblock and lane dicts
"""
# remove repeated ids while remaining order in list
route_roadblock_ids = list(dict.fromkeys(route_roadblock_ids))
route_roadblock_dict: Dict[str, RoadBlockGraphEdgeMapObject] = {}
route_lane_dict: Dict[str, LaneGraphEdgeMapObject] = {}
for id_ in route_roadblock_ids:
block = map_api.get_map_object(id_, SemanticMapLayer.ROADBLOCK)
block = block or map_api.get_map_object(id_, SemanticMapLayer.ROADBLOCK_CONNECTOR)
route_roadblock_dict[block.id] = block
for lane in block.interior_edges:
route_lane_dict[lane.id] = lane
return route_roadblock_dict, route_lane_dict
def get_current_lane(
ego_pose: StateSE2, route_lane_dict: Dict[str, LaneGraphEdgeMapObject]
) -> LaneGraphEdgeMapObject:
"""
Find current lane, either if intersection with ego pose, or by distance.
:param ego_pose: (x, y, heading) object for global ego pose
:param route_lane_dict: Dictionary of roadblock ids and objects
:return: Lane object
"""
closest_distance = np.inf
starting_lane = None
for edge in route_lane_dict.values():
if edge.contains_point(ego_pose):
starting_lane = edge
break
distance = edge.polygon.distance(Point(*ego_pose.array))
if distance < closest_distance:
starting_lane = edge
closest_distance = distance
return starting_lane
def get_discrete_centerline(
current_lane: LaneGraphEdgeMapObject,
route_roadblock_dict: Dict[str, RoadBlockGraphEdgeMapObject],
route_lane_dict: Dict[str, LaneGraphEdgeMapObject],
search_depth: int = 30,
) -> List[StateSE2]:
"""
Given the current lane, apply graph search, and extract centerline.
:param current_lane: Lane object closest to ego
:param route_roadblock_dict: Dictionary of roadblock ids and objects
:param route_lane_dict: Dictionary of lane ids and objects
:param search_depth: max search depth of Dijkstra, defaults to 30
:return: List of (x, y, heading) objects
"""
roadblocks = list(route_roadblock_dict.values())
roadblock_ids = list(route_roadblock_dict.keys())
# find current roadblock index
start_idx = np.argmax(np.array(roadblock_ids) == current_lane.get_roadblock_id())
roadblock_window = roadblocks[start_idx : start_idx + search_depth]
graph_search = Dijkstra(current_lane, list(route_lane_dict.keys()))
route_plan, _ = graph_search.search(roadblock_window[-1])
centerline_discrete_path: List[StateSE2] = []
for lane in route_plan:
centerline_discrete_path.extend(lane.baseline_path.discrete_path)
return centerline_discrete_path
|