|
| 1 | +""" |
| 2 | +Example demonstrating the usage of the TrapezoidalTrajectory class for motion planning. |
| 3 | +
|
| 4 | +This file showcases various capabilities of the trapezoidal trajectory generator, including: |
| 5 | +1. Simple point-to-point trajectories with different constraints |
| 6 | +2. Trajectories with non-zero initial and final velocities |
| 7 | +3. Multi-point trajectories through sequences of waypoints |
| 8 | +4. Time-constrained trajectories with custom timing |
| 9 | +5. Visualization of position, velocity, and acceleration profiles |
| 10 | +""" |
| 11 | + |
| 12 | +from collections.abc import Callable |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +from interpolatepy.trapezoidal import InterpolationParams |
| 18 | +from interpolatepy.trapezoidal import TrajectoryParams |
| 19 | +from interpolatepy.trapezoidal import TrapezoidalTrajectory |
| 20 | + |
| 21 | + |
| 22 | +def plot_trajectory( |
| 23 | + trajectory_func: Callable[[float], tuple[float, float, float]], |
| 24 | + duration: float, |
| 25 | + title: str = "Trapezoidal Trajectory", |
| 26 | + num_points: int = 1000, |
| 27 | +) -> None: |
| 28 | + """ |
| 29 | + Plot a trajectory showing position, velocity, and acceleration profiles. |
| 30 | +
|
| 31 | + Parameters: |
| 32 | + ----------- |
| 33 | + trajectory_func : Callable[[float], tuple[float, float, float]] |
| 34 | + Function that returns position, velocity, and acceleration at time t |
| 35 | + duration : float |
| 36 | + Total duration of the trajectory |
| 37 | + title : str |
| 38 | + Title for the plot |
| 39 | + num_points : int |
| 40 | + Number of points to sample for the plot |
| 41 | + """ |
| 42 | + # Generate time points |
| 43 | + t_values = np.linspace(0, duration, num_points) |
| 44 | + |
| 45 | + # Evaluate trajectory at each time point |
| 46 | + positions = [] |
| 47 | + velocities = [] |
| 48 | + accelerations = [] |
| 49 | + |
| 50 | + for t in t_values: |
| 51 | + pos, vel, acc = trajectory_func(t) |
| 52 | + positions.append(pos) |
| 53 | + velocities.append(vel) |
| 54 | + accelerations.append(acc) |
| 55 | + |
| 56 | + # Create figure with 3 subplots |
| 57 | + fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 8), sharex=True) |
| 58 | + fig.suptitle(title) |
| 59 | + |
| 60 | + # Plot position |
| 61 | + ax1.plot(t_values, positions, "b-") |
| 62 | + ax1.set_ylabel("Position") |
| 63 | + ax1.grid(True) |
| 64 | + |
| 65 | + # Plot velocity |
| 66 | + ax2.plot(t_values, velocities, "g-") |
| 67 | + ax2.set_ylabel("Velocity") |
| 68 | + ax2.grid(True) |
| 69 | + |
| 70 | + # Plot acceleration |
| 71 | + ax3.plot(t_values, accelerations, "r-") |
| 72 | + ax3.set_xlabel("Time") |
| 73 | + ax3.set_ylabel("Acceleration") |
| 74 | + ax3.grid(True) |
| 75 | + |
| 76 | + plt.tight_layout() |
| 77 | + plt.subplots_adjust(top=0.9) |
| 78 | + plt.show() |
| 79 | + |
| 80 | + |
| 81 | +def example_1_basic_trajectory() -> None: |
| 82 | + """Demonstrates a basic trapezoidal trajectory with zero initial and final velocities.""" |
| 83 | + print("Example 1: Basic Trapezoidal Trajectory") |
| 84 | + print("--------------------------------------") |
| 85 | + |
| 86 | + params = TrajectoryParams(q0=0.0, q1=10.0, v0=0.0, v1=0.0, amax=3.0, vmax=4.0) |
| 87 | + |
| 88 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 89 | + |
| 90 | + print(f"Duration: {duration:.2f} seconds") |
| 91 | + print(f"Position at t=0: {traj_func(0)[0]:.2f}") |
| 92 | + print(f"Position at t={duration / 2:.2f}: {traj_func(duration / 2)[0]:.2f}") |
| 93 | + print(f"Position at t={duration:.2f}: {traj_func(duration)[0]:.2f}") |
| 94 | + |
| 95 | + plot_trajectory(traj_func, duration, "Basic Trapezoidal Trajectory") |
| 96 | + |
| 97 | + |
| 98 | +def example_2_nonzero_velocities() -> None: |
| 99 | + """Demonstrates a trajectory with non-zero initial and final velocities.""" |
| 100 | + print("\nExample 2: Trajectory with Non-Zero Initial and Final Velocities") |
| 101 | + print("-------------------------------------------------------------") |
| 102 | + |
| 103 | + params = TrajectoryParams( |
| 104 | + q0=0.0, |
| 105 | + q1=10.0, |
| 106 | + v0=1.5, # Non-zero initial velocity |
| 107 | + v1=2.0, # Non-zero final velocity |
| 108 | + amax=5.0, |
| 109 | + vmax=4.0, |
| 110 | + ) |
| 111 | + |
| 112 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 113 | + |
| 114 | + print(f"Duration: {duration:.2f} seconds") |
| 115 | + print(f"Initial velocity: {traj_func(0)[1]:.2f}") |
| 116 | + print(f"Final velocity: {traj_func(duration)[1]:.2f}") |
| 117 | + |
| 118 | + plot_trajectory(traj_func, duration, "Trajectory with Non-Zero Initial and Final Velocities") |
| 119 | + |
| 120 | + |
| 121 | +def example_3_negative_displacement() -> None: |
| 122 | + """Demonstrates a trajectory with negative displacement.""" |
| 123 | + print("\nExample 3: Trajectory with Negative Displacement") |
| 124 | + print("---------------------------------------------") |
| 125 | + |
| 126 | + params = TrajectoryParams( |
| 127 | + q0=0.0, q1=-10.0, v0=1.5, v1=2.0, amax=5.0, vmax=4.0 # Negative displacement |
| 128 | + ) |
| 129 | + |
| 130 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 131 | + |
| 132 | + print(f"Duration: {duration:.2f} seconds") |
| 133 | + print(f"Initial position: {traj_func(0)[0]:.2f}") |
| 134 | + print(f"Final position: {traj_func(duration)[0]:.2f}") |
| 135 | + |
| 136 | + plot_trajectory(traj_func, duration, "Trajectory with Negative Displacement") |
| 137 | + |
| 138 | + |
| 139 | +def example_4_duration_constrained() -> None: |
| 140 | + """Demonstrates a trajectory with fixed duration.""" |
| 141 | + print("\nExample 4: Duration-Constrained Trajectory") |
| 142 | + print("----------------------------------------") |
| 143 | + |
| 144 | + params = TrajectoryParams( |
| 145 | + q0=0.0, q1=10.0, v0=0.0, v1=0.0, amax=5.0, duration=4.0 # Fixed duration of 4 seconds |
| 146 | + ) |
| 147 | + |
| 148 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 149 | + |
| 150 | + print("Specified duration: 4.00 seconds") |
| 151 | + print(f"Actual duration: {duration:.2f} seconds") |
| 152 | + |
| 153 | + plot_trajectory(traj_func, duration, "Duration-Constrained Trajectory (T=4s)") |
| 154 | + |
| 155 | + |
| 156 | +def example_5_triangular_profile() -> None: |
| 157 | + """Demonstrates a triangular profile for short displacements.""" |
| 158 | + print("\nExample 5: Triangular Profile for Short Displacement") |
| 159 | + print("-------------------------------------------------") |
| 160 | + |
| 161 | + params = TrajectoryParams( |
| 162 | + q0=0.0, |
| 163 | + q1=2.0, # Short displacement that doesn't reach vmax |
| 164 | + v0=0.0, |
| 165 | + v1=0.0, |
| 166 | + amax=3.0, |
| 167 | + vmax=5.0, |
| 168 | + ) |
| 169 | + |
| 170 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 171 | + |
| 172 | + print(f"Duration: {duration:.2f} seconds") |
| 173 | + |
| 174 | + # Sample velocity at multiple points to show it never reaches vmax |
| 175 | + times = np.linspace(0, duration, 10) |
| 176 | + max_vel = max(traj_func(t)[1] for t in times) |
| 177 | + |
| 178 | + print(f"Maximum velocity: {max_vel:.2f} (less than vmax={5.0})") |
| 179 | + |
| 180 | + plot_trajectory(traj_func, duration, "Triangular Profile for Short Displacement") |
| 181 | + |
| 182 | + |
| 183 | +def example_6_asymmetric_profile() -> None: |
| 184 | + """Demonstrates an asymmetric acceleration and deceleration profile.""" |
| 185 | + print("\nExample 6: Asymmetric Acceleration and Deceleration") |
| 186 | + print("------------------------------------------------") |
| 187 | + |
| 188 | + params = TrajectoryParams( |
| 189 | + q0=0.0, q1=15.0, v0=3.0, v1=0.0, amax=4.0, vmax=6.0 # Start already moving # End at rest |
| 190 | + ) |
| 191 | + |
| 192 | + traj_func, duration = TrapezoidalTrajectory.generate_trajectory(params) |
| 193 | + |
| 194 | + print(f"Duration: {duration:.2f} seconds") |
| 195 | + print(f"Initial velocity: {traj_func(0)[1]:.2f}") |
| 196 | + print(f"Final velocity: {traj_func(duration)[1]:.2f}") |
| 197 | + |
| 198 | + plot_trajectory(traj_func, duration, "Asymmetric Acceleration and Deceleration") |
| 199 | + |
| 200 | + |
| 201 | +def example_7_multi_point_custom_velocities() -> None: |
| 202 | + """Demonstrates a multi-point trajectory with custom intermediate velocities.""" |
| 203 | + print("\nExample 7: Multi-Point Trajectory with Custom Velocities") |
| 204 | + print("-----------------------------------------------------") |
| 205 | + |
| 206 | + points = [0.0, 8.0, 12.0, 5.0, 10.0] |
| 207 | + inter_velocities = [3.0, 0.0, -2.0] # Velocities at intermediate waypoints |
| 208 | + |
| 209 | + params = InterpolationParams( |
| 210 | + points=points, v0=0.0, vn=0.0, inter_velocities=inter_velocities, amax=4.0, vmax=5.0 |
| 211 | + ) |
| 212 | + |
| 213 | + traj_func, duration = TrapezoidalTrajectory.interpolate_waypoints(params) |
| 214 | + |
| 215 | + print(f"Waypoints: {points}") |
| 216 | + print(f"Intermediate velocities: {inter_velocities}") |
| 217 | + print(f"Total duration: {duration:.2f} seconds") |
| 218 | + |
| 219 | + plot_trajectory(traj_func, duration, "Trajectory with Custom Intermediate Velocities") |
| 220 | + |
| 221 | + |
| 222 | +def example_8_time_constrained_multi_point() -> None: |
| 223 | + """Demonstrates a multi-point trajectory with specific timing constraints.""" |
| 224 | + print("\nExample 8: Time-Constrained Multi-Point Trajectory") |
| 225 | + print("------------------------------------------------") |
| 226 | + |
| 227 | + points = [0.0, 5.0, 10.0, 7.0, 15.0] |
| 228 | + times = [0.0, 2.0, 4.0, 7.0, 10.0] # Specific times to reach each point |
| 229 | + |
| 230 | + params = InterpolationParams(points=points, v0=0.0, vn=0.0, times=times, amax=15.0) |
| 231 | + |
| 232 | + traj_func, duration = TrapezoidalTrajectory.interpolate_waypoints(params) |
| 233 | + |
| 234 | + print(f"Waypoints: {points}") |
| 235 | + print(f"Time schedule: {times}") |
| 236 | + print(f"Total duration: {duration:.2f} seconds") |
| 237 | + |
| 238 | + # Verify the trajectory passes through the points at the specified times |
| 239 | + print("\nVerifying position at specified times:") |
| 240 | + for i, t in enumerate(times): |
| 241 | + pos = traj_func(t)[0] |
| 242 | + print(f"At t={t:.1f}s: Expected={points[i]:.1f}, Actual={pos:.1f}") |
| 243 | + |
| 244 | + plot_trajectory(traj_func, duration, "Time-Constrained Multi-Point Trajectory") |
| 245 | + |
| 246 | + |
| 247 | +def example_9_oscillating_trajectory() -> None: |
| 248 | + """Demonstrates an oscillating trajectory with sign changes.""" |
| 249 | + print("\nExample 9: Oscillating Trajectory with Sign Changes") |
| 250 | + print("------------------------------------------------") |
| 251 | + |
| 252 | + oscillating_points = [0.0, 5.0, -3.0, 8.0, -2.0, 4.0, 0.0] |
| 253 | + |
| 254 | + params = InterpolationParams(points=oscillating_points, v0=0.0, vn=0.0, amax=8.0, vmax=6.0) |
| 255 | + |
| 256 | + traj_func, duration = TrapezoidalTrajectory.interpolate_waypoints(params) |
| 257 | + |
| 258 | + print(f"Oscillating waypoints: {oscillating_points}") |
| 259 | + print(f"Total duration: {duration:.2f} seconds") |
| 260 | + |
| 261 | + plot_trajectory(traj_func, duration, "Oscillating Trajectory with Sign Changes") |
| 262 | + |
| 263 | + |
| 264 | +def example_10_complex_velocity_profile() -> None: |
| 265 | + """Demonstrates a complex trajectory with custom velocity profiles.""" |
| 266 | + print("\nExample 10: Complex Trajectory with Custom Velocity Profiles") |
| 267 | + print("--------------------------------------------------------") |
| 268 | + |
| 269 | + complex_points = [0.0, 10.0, 15.0, 5.0, 8.0, 3.0] |
| 270 | + complex_velocities = [4.0, 0.0, -5.0, 2.0] |
| 271 | + |
| 272 | + params = InterpolationParams( |
| 273 | + points=complex_points, |
| 274 | + v0=0.0, |
| 275 | + vn=0.0, |
| 276 | + inter_velocities=complex_velocities, |
| 277 | + amax=6.0, |
| 278 | + vmax=8.0, |
| 279 | + ) |
| 280 | + |
| 281 | + traj_func, duration = TrapezoidalTrajectory.interpolate_waypoints(params) |
| 282 | + |
| 283 | + print(f"Complex waypoints: {complex_points}") |
| 284 | + print(f"Complex velocities: [0.0, {', '.join(str(v) for v in complex_velocities)}, 0.0]") |
| 285 | + print(f"Total duration: {duration:.2f} seconds") |
| 286 | + |
| 287 | + plot_trajectory(traj_func, duration, "Complex Trajectory with Custom Velocity Profiles") |
| 288 | + |
| 289 | + |
| 290 | +if __name__ == "__main__": |
| 291 | + print("TrapezoidalTrajectory Class - Usage Examples") |
| 292 | + print("===========================================\n") |
| 293 | + |
| 294 | + # Run all examples |
| 295 | + example_1_basic_trajectory() |
| 296 | + example_2_nonzero_velocities() |
| 297 | + example_3_negative_displacement() |
| 298 | + example_4_duration_constrained() |
| 299 | + example_5_triangular_profile() |
| 300 | + example_6_asymmetric_profile() |
| 301 | + example_7_multi_point_custom_velocities() |
| 302 | + example_8_time_constrained_multi_point() |
| 303 | + example_9_oscillating_trajectory() |
| 304 | + example_10_complex_velocity_profile() |
0 commit comments