|
| 1 | +""" |
| 2 | +Example file demonstrating the use of linear_traj function. |
| 3 | +
|
| 4 | +This example shows: |
| 5 | +1. How to generate a linear trajectory between two points |
| 6 | +2. How to use the function with both scalar and vector positions |
| 7 | +3. Visualization of the generated trajectories |
| 8 | +""" |
| 9 | + |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +from interpolatepy.linear import linear_traj |
| 14 | + |
| 15 | + |
| 16 | +def scalar_trajectory_example() -> None: |
| 17 | + """Demonstrate linear_traj with scalar positions.""" |
| 18 | + print("Scalar Trajectory Example") |
| 19 | + print("--------------------------") |
| 20 | + |
| 21 | + # Define start and end points |
| 22 | + p0 = 0.0 |
| 23 | + p1 = 10.0 |
| 24 | + |
| 25 | + # Define time interval |
| 26 | + t0 = 0.0 |
| 27 | + t1 = 2.0 |
| 28 | + |
| 29 | + # Generate time array |
| 30 | + num_points = 100 |
| 31 | + time_array = np.linspace(t0, t1, num_points) |
| 32 | + |
| 33 | + # Calculate trajectory |
| 34 | + positions, velocities, accelerations = linear_traj(p0, p1, t0, t1, time_array) |
| 35 | + |
| 36 | + # Print some values |
| 37 | + print(f"Start position: {positions[0]}") |
| 38 | + print(f"End position: {positions[-1]}") |
| 39 | + print(f"Constant velocity: {velocities[0]}") |
| 40 | + print(f"Acceleration: {accelerations[0]}") |
| 41 | + |
| 42 | + # Plot results |
| 43 | + plt.figure(figsize=(12, 8)) |
| 44 | + |
| 45 | + # Position plot |
| 46 | + plt.subplot(3, 1, 1) |
| 47 | + plt.plot(time_array, positions, "b-", linewidth=2) |
| 48 | + plt.grid(True) |
| 49 | + plt.ylabel("Position") |
| 50 | + plt.title("Linear Trajectory - Scalar Case") |
| 51 | + |
| 52 | + # Velocity plot |
| 53 | + plt.subplot(3, 1, 2) |
| 54 | + plt.plot(time_array, velocities, "g-", linewidth=2) |
| 55 | + plt.grid(True) |
| 56 | + plt.ylabel("Velocity") |
| 57 | + |
| 58 | + # Acceleration plot |
| 59 | + plt.subplot(3, 1, 3) |
| 60 | + plt.plot(time_array, accelerations, "r-", linewidth=2) |
| 61 | + plt.grid(True) |
| 62 | + plt.ylabel("Acceleration") |
| 63 | + plt.xlabel("Time") |
| 64 | + |
| 65 | + plt.tight_layout() |
| 66 | + plt.show() |
| 67 | + |
| 68 | + |
| 69 | +def vector_trajectory_example() -> None: |
| 70 | + """Demonstrate linear_traj with vector positions (2D points).""" |
| 71 | + print("\nVector Trajectory Example (2D)") |
| 72 | + print("------------------------------") |
| 73 | + |
| 74 | + # Define start and end points (2D vectors) |
| 75 | + p0 = [0.0, 0.0] # Starting at origin |
| 76 | + p1 = [10.0, 5.0] # Ending at (10, 5) |
| 77 | + |
| 78 | + # Define time interval |
| 79 | + t0 = 0.0 |
| 80 | + t1 = 3.0 |
| 81 | + |
| 82 | + # Generate time array |
| 83 | + num_points = 100 |
| 84 | + time_array = np.linspace(t0, t1, num_points) |
| 85 | + |
| 86 | + # Calculate trajectory |
| 87 | + positions, velocities, accelerations = linear_traj(p0, p1, t0, t1, time_array) |
| 88 | + |
| 89 | + # Print some values |
| 90 | + print(f"Start position: {positions[0]}") |
| 91 | + print(f"End position: {positions[-1]}") |
| 92 | + print(f"Constant velocity: {velocities[0]}") |
| 93 | + print(f"Acceleration: {accelerations[0]}") |
| 94 | + |
| 95 | + # Extract x and y components |
| 96 | + x_positions = positions[:, 0] |
| 97 | + y_positions = positions[:, 1] |
| 98 | + x_velocities = velocities[:, 0] |
| 99 | + y_velocities = velocities[:, 1] |
| 100 | + |
| 101 | + # Plot results |
| 102 | + plt.figure(figsize=(12, 10)) |
| 103 | + |
| 104 | + # Trajectory in 2D space |
| 105 | + plt.subplot(3, 1, 1) |
| 106 | + plt.plot(x_positions, y_positions, "b-", linewidth=2) |
| 107 | + plt.plot(p0[0], p0[1], "ro", markersize=8, label="Start") |
| 108 | + plt.plot(p1[0], p1[1], "go", markersize=8, label="End") |
| 109 | + plt.grid(True) |
| 110 | + plt.xlabel("X position") |
| 111 | + plt.ylabel("Y position") |
| 112 | + plt.title("Linear Trajectory in 2D Space") |
| 113 | + plt.legend() |
| 114 | + |
| 115 | + # X and Y positions over time |
| 116 | + plt.subplot(3, 1, 2) |
| 117 | + plt.plot(time_array, x_positions, "b-", linewidth=2, label="X position") |
| 118 | + plt.plot(time_array, y_positions, "g-", linewidth=2, label="Y position") |
| 119 | + plt.grid(True) |
| 120 | + plt.ylabel("Position") |
| 121 | + plt.legend() |
| 122 | + |
| 123 | + # X and Y velocities over time |
| 124 | + plt.subplot(3, 1, 3) |
| 125 | + plt.plot(time_array, x_velocities, "b-", linewidth=2, label="X velocity") |
| 126 | + plt.plot(time_array, y_velocities, "g-", linewidth=2, label="Y velocity") |
| 127 | + plt.grid(True) |
| 128 | + plt.ylabel("Velocity") |
| 129 | + plt.xlabel("Time") |
| 130 | + plt.legend() |
| 131 | + |
| 132 | + plt.tight_layout() |
| 133 | + plt.show() |
| 134 | + |
| 135 | + |
| 136 | +def main() -> None: |
| 137 | + """Run both examples.""" |
| 138 | + print("Linear Trajectory Examples") |
| 139 | + print("=========================\n") |
| 140 | + |
| 141 | + # Run scalar example |
| 142 | + scalar_trajectory_example() |
| 143 | + |
| 144 | + # Run vector example |
| 145 | + vector_trajectory_example() |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments