import warnings
from .Vehicle import Unicycle, Bicycle
from .helpers.helper_functions import Timer
from .helpers.plot_helpers import plot_planner_inputs
from .helpers.corridor_functions import (
compute_trajectory_multiple_corridors,
check_inputs_analytical_planner,
compute_end_pose,
compute_start_pose,
compute_trajectory_unicycle_two_corridors,
shrink_corridor_list,
pose_from_shrunken_corridor_relative_frame,
compute_minimum_widths
)
from .helpers.corridor_functions_oo import (
compute_trajectory_bicycle_two_corridors,
)
[docs]
class MotionPlanner:
"""Analytically compute trajectories within corridors for different kinds of vehicles.
"""
[docs]
def __init__(self,
vehicle,
corridor_list,
start_pose = None,
end_pose = None,
relative_start_pose = None,
relative_end_pose = None,
waypoints = None):
"""Constructor.
:param vehicle: Vehicle model used for planning.
:type vehicle: Vehicle
:param corridor_list: Ordered sequence of corridors.
:type corridor_list: list[CorridorWorld]
:param start_pose: Initial pose included in the first corridor. If not provided, it is computed automatically
at a distance of 1.3 times the vehicle length from the midpoint of the back edge of the first corridor.
:type start_pose: list[float] | numpy.ndarray
:param end_pose: Goal pose included in the last corridor. If not provided, it is computed automatically
at a distance of 1.3 times the vehicle length from the midpoint of the front edge of the last corridor.
:type end_pose: list[float] | numpy.ndarray
:param waypoints: List of waypoints to make the trajectory pass through (used only if provided).
:type waypoints: list[list[float]] | numpy.ndarray
"""
# Initialize core attributes
self.vehicle = vehicle
self.corridor_list = corridor_list
self.waypoints = waypoints
self.shrunken_corridor_list = shrink_corridor_list(
self.corridor_list,
self.vehicle.width * 0.5)
# Compute start and end poses if not provided
if start_pose is None and relative_start_pose is None:
margin = vehicle.length * 1.3
self.start_pose = compute_start_pose(self.corridor_list[0], self.vehicle, margin)
elif start_pose is not None:
self.start_pose = start_pose
elif relative_start_pose is not None:
self.start_pose = pose_from_shrunken_corridor_relative_frame(
relative_start_pose,
self.shrunken_corridor_list[0],
)
if end_pose is None and relative_end_pose is None:
margin = vehicle.length * 1.3
self.end_pose = compute_end_pose(self.corridor_list[-1], self.vehicle, margin)
elif end_pose is not None:
self.end_pose = end_pose
elif relative_end_pose is not None:
self.end_pose = pose_from_shrunken_corridor_relative_frame(
relative_end_pose,
self.shrunken_corridor_list[-1],
)
if isinstance(self.vehicle, Unicycle) or isinstance(self.vehicle, Bicycle):
# Compute minimum widths of corridors
self.min_corridor_widths = compute_minimum_widths(self)
# Validate inputs
(
self.inputs_check,
self.warn_msgs,
self.intermediate_circle_centers,
) = check_inputs_analytical_planner(self)
vehicle.update(state=self.start_pose)
# Initialize computation stats
self.comp_time_analytical_sol = 0.0
[docs]
def update(self, **kwargs):
"""Update attributes of the MotionPlanner instance.
Keyword arguments correspond to existing attributes of the object.
Attributes not found in the instance are ignored with a warning.
Example:
planner.update(start_pose=new_start, end_pose=new_end)
"""
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
self.inputs_check = check_inputs_analytical_planner(self)
else:
warnings.warn(f"Ignoring unknown attribute '{key}' in update().")
[docs]
def compute_trajectory_analytical(self, obstacle_center = None, obstacle_radius = None):
"""
Compute the time-optimal trajectory from the start pose to the end pose.
Uses the analytical planner to generate a trajectory constrained within
the specified corridors or waypoints.
"""
# Create copies of attributes to avoid modifying the original data
corridors = self.corridor_list.copy()
vehicle = self.vehicle.copy()
start_pose = self.start_pose.copy()
end_pose = self.end_pose.copy()
waypoints = self.waypoints.copy() if self.waypoints is not None else None
# Analytical planner for Unicycle vehicles - two corridors or multiple corridors
if isinstance(vehicle, Unicycle) and waypoints is None:
if self.inputs_check is False:
raise ValueError(
"Invalid inputs for analytical motion planner.\n"
"Check warning messages for details:\n"
+ "\n".join(self.warn_msgs)
)
else:
if len(corridors) == 2:
with Timer() as timer:
trajectory, _ = compute_trajectory_unicycle_two_corridors(
corridors[0], corridors[1], start_pose, end_pose, vehicle
)
else:
with Timer() as timer:
_, trajectory = compute_trajectory_multiple_corridors(
corridors, start_pose, end_pose, vehicle, obstacle_center, obstacle_radius
)
self.comp_time_analytical_sol = timer()
# Analytical planner for Bicycle vehicles – two corridors or multiple corridors
elif isinstance(vehicle, Bicycle) and waypoints is None:
if self.inputs_check is False:
raise ValueError(
"Invalid inputs for analytical motion planner.\n"
"Check warning messages for details:\n"
+ "\n".join(self.warn_msgs)
)
else:
if len(corridors) == 2:
with Timer() as timer:
trajectory = compute_trajectory_bicycle_two_corridors(
corridors[0], corridors[1], start_pose, end_pose, vehicle
)
else:
with Timer() as timer:
trajectory, _ = compute_trajectory_multiple_corridors(
corridors, start_pose, end_pose, vehicle, obstacle_center, obstacle_radius
)
self.comp_time_analytical_sol = timer()
## TODO: Waypoints
## TODO: One corridor
return trajectory
def __str__(self):
return 'MotionPlanner object'
# def compute_trajectory_analytical_one_corridor(self):
# corridor_list_copy = self.corridor_list.copy()
# vehicle_copy = self.vehicle.copy()
# start_pose_copy = self.start_pose.copy()
# trajectory = compute_trajectory_unicycle_one_corridor(corridor_list_copy[0], start_pose_copy, vehicle_copy)
# return trajectory