Retime a path to a specific durationΒΆ

toppra allows user to parametrize a path to a specific duration while still satisfy constraints. We will see how to do this in this example. First, import necessary libraries.

 9 import toppra as ta
10 import toppra.constraint as constraint
11 import toppra.algorithm as algo
12 import numpy as np
13 import matplotlib.pyplot as plt
14 import time
15
16 ta.setup_logging("INFO")

Parameters

20 N_samples = 5
21 SEED = 9
22 dof = 7

Random waypoints used to obtain a random geometric path. Here, we use spline interpolation.

27 np.random.seed(SEED)
28 way_pts = np.random.randn(N_samples, dof)
29 path = ta.SplineInterpolator(np.linspace(0, 1, 5), way_pts)

Create velocity bounds, then velocity constraint object

33 vlim_ = np.random.rand(dof) * 20
34 vlim = np.vstack((-vlim_, vlim_)).T

Create acceleration bounds, then acceleration constraint object

38 alim_ = np.random.rand(dof) * 2
39 alim = np.vstack((-alim_, alim_)).T
40 pc_vel = constraint.JointVelocityConstraint(vlim)
41 pc_acc = constraint.JointAccelerationConstraint(
42     alim, discretization_scheme=constraint.DiscretizationType.Interpolation)

Setup a parametrization instance

46 instance = algo.TOPPRAsd([pc_vel, pc_acc], path)
47 instance.set_desired_duration(60)

Out:

INFO [algorithm.py : 104] No gridpoint specified. Automatically choose a gridpoint with 290 points
INFO [reachability_algorithm.py : 65] Solver wrapper not supplied. Choose solver wrapper automatically!
INFO [reachability_algorithm.py : 75] Select solver seidel

Retime the trajectory, only this step is necessary.

51 jnt_traj = instance.compute_trajectory(0, 0)
52 ts_sample = np.linspace(0, jnt_traj.get_duration(), 100)
53 qs_sample = jnt_traj(ts_sample, 2)

Out:

 INFO [desired_duration_algorithm.py : 158] Desired duration 60.000000 sec is achievable. Continue computing.
 INFO [algorithm.py : 191] Successfully parametrize path. Duration: 60.000, previously 1.000)
 INFO [algorithm.py : 193] Finish parametrization in 0.048 secs
/home/circleci/repo/toppra/utils.py:23: DeprecationWarning: Call to deprecated function get_duration in module toppra.interpolator.
  warnings.warn(

Output

57 plt.plot(ts_sample, qs_sample)
58 plt.xlabel("Time (s)")
59 plt.ylabel("Joint acceleration (rad/s^2)")
60 plt.show()
plot kinematics duration

Compute the feasible sets and the controllable sets for viewing. Note that these steps are not necessary.

65 X = instance.compute_feasible_sets()
66 K = instance.compute_controllable_sets(0, 0)
67 _, sd_vec, _ = instance.compute_parameterization(0, 0)
68 X = np.sqrt(X)
69 K = np.sqrt(K)
70 plt.plot(X[:, 0], c='green', label="Feasible sets")
71 plt.plot(X[:, 1], c='green')
72 plt.plot(K[:, 0], '--', c='red', label="Controllable sets")
73 plt.plot(K[:, 1], '--', c='red')
74 plt.plot(sd_vec, label="Velocity profile")
75 plt.title("Path-position path-velocity plot")
76 plt.xlabel("Path position")
77 plt.ylabel("Path velocity square")
78 plt.legend()
79 plt.tight_layout()
80 plt.show()
Path-position path-velocity plot

Out:

INFO [desired_duration_algorithm.py : 158] Desired duration 60.000000 sec is achievable. Continue computing.

Total running time of the script: ( 0 minutes 0.458 seconds)

Gallery generated by Sphinx-Gallery