Source code for arena.MotionPlanner

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 plot_planner_inputs(self, figure=None): """ Plot the corridors and start/end poses of the motion planner. :param figure: Matplotlib figure to plot on. If ``None``, a new figure is created. :type figure: matplotlib.figure.Figure, optional :returns: The resulting matplotlib figure. :rtype: matplotlib.figure.Figure """ return plot_planner_inputs(self, figure)
[docs] def print_planner_inputs(self): """ Return a formatted string representation of the planner inputs. """ # Build corridor definitions corridor_defs = [ f"corridor{i} = Corridor.CorridorWorld({c.width}, {c.height}, {c.center}, {c.tilt})" for i, c in enumerate(self.corridor_list, start=1) ] corridors_str = "\n".join(corridor_defs) # Build corridor list line corridor_names = ", ".join(f"corridor{i}" for i in range(1, len(self.corridor_list) + 1)) corridor_list_str = f"corridor_list = [{corridor_names}]" # Format start/end poses start_pose_str = f"start_pose = [{', '.join(map(str, self.start_pose))}]" end_pose_str = f"end_pose = [{', '.join(map(str, self.end_pose))}]" # Format vehicle definition v = self.vehicle vehicle_str = ( f"vehicle = Unicycle(width={v.width}, length={v.length}, v_max={v.v_max}, " f"v_min=0, omega_max={v.omega_max}, omega_min={-v.omega_max})" ) # Join all parts with newlines return "\n".join([corridors_str, corridor_list_str, start_pose_str, end_pose_str, vehicle_str])
[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