In [2]:
%pylab inline
import seaborn
In [3]:
from plotting import plot_trajectory
In [4]:
def cubic_trajectory(current_position, target_position,
current_velocity, target_velocity,
duration_in_seconds):
trajectories = []
t = duration_in_seconds
xs = linspace(0,t)
for qi, qf, dqi, dqf in zip(current_position, target_position,
current_velocity, target_velocity):
A = np.array([[0.0,0.0,0.0,1.0],
[t**3, t**2, t, 1.],
[0.0, 0.0, 1.0, 0.0],
[3.0 * t**2, 2*t, 1.0, 0.0]])
b = np.array([qi, qf, dqi, dqf])
x = np.linalg.solve(A,b)
qs = np.polyval(x, xs)
dqs = np.polyval([3. * x[0], 2. * x[1], x[2]], xs)
ddqs = np.polyval([6. * x[0], 2. * x[1]], xs)
trajectories.append((qs, dqs, ddqs))
return trajectories
In [5]:
qi = [0,np.pi]
qf = [np.pi,0]
dqi = [0,0]
dqf = [0,0]
trajectories = cubic_trajectory(qi, qf, dqi, dqf, 50)
In [6]:
plot_trajectory(trajectories[0], iscubic=True)
In [8]:
def quintic_trajectory(current_position, target_position, current_velocity,
target_velocity, current_acceleration,
target_acceleration, duration_in_seconds):
trajectories = []
t = duration_in_seconds
xs = linspace(0, t)
for qi, qf, dqi, dqf, ddqi, ddqf in zip(current_position, target_position,
current_velocity, target_velocity,
current_acceleration, target_acceleration):
A = np.array(
[[0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[t**5, t**4, t**3, t**2, t, 1.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
[5. * t**4, 4. * t**3, 3. * t**2, 2. * t, 1., 0.0],
[0.0, 0.0, 0.0, 2.0, 0.0, 0.0],
[20. * t**3, 12. * t**2, 6. * t, 2., 0.0, 0.0]])
b = np.array([qi, qf, dqi, dqf, ddqi, ddqf])
x = np.linalg.solve(A, b)
qs = np.polyval(x, xs)
dqs = np.polyval([5. * x[0], 4. * x[1], 3. * x[2], 2. * x[3], x[4]], xs)
ddqs = np.polyval([20. * x[0], 12. * x[1], 6. * x[2], 2. * x[3]], xs)
trajectories.append((qs, dqs, ddqs))
return trajectories
In [9]:
qi = [0,np.pi]
qf = [np.pi,0]
dqi = [0,0]
dqf = [0,0]
ddqi = [0,0]
ddqf = [0,0]
trajectories = quintic_trajectory(qi, qf, dqi, dqf, ddqi, ddqf, 50)
In [10]:
plot_trajectory(trajectories[0], iscubic=False)