# trapveltraj

Generate trajectories with trapezoidal velocity profiles

## Syntax

## Description

`[`

generates a trajectory through a given set of input waypoints that follow a trapezoidal
velocity profile. The function outputs positions, velocities, and accelerations at the given
time samples, `q`

,`qd`

,`qdd`

,`tSamples`

,`pp`

] = trapveltraj(`wayPoints`

,`numSamples`

)`tSamples`

, based on the specified number of samples,
`numSamples`

. The function also returns the piecewise polynomial
`pp`

form of the polynomial trajectory with respect to time.

`[`

specifies additional parameters using `q`

,`qd`

,`qdd`

,`tSamples`

,`pp`

] = trapveltraj(`wayPoints`

,`numSamples`

,`Name,Value`

)`Name,Value`

pair arguments.

## Examples

### Compute Trapezoidal Velocity Trajectory for 2-D Planar Motion

Use the `trapveltraj`

function with a given set of 2-D *xy* waypoints.

wpts = [0 45 15 90 45; 90 45 -45 15 90];

Compute the trajectory for a given number of samples (501). The function outputs the trajectory positions (`q`

), velocity (`qd`

), acceleration (`qdd`

), time vector (`tvec`

), and polynomial coefficients (`pp`

) of the polynomial that achieves the waypoints using trapezoidal velocities.

[q,qd,qdd,tvec,pp] = trapveltraj(wpts,501);

Plot the trajectories for the *x- *and *y*-positions and the trapezoial velocity profile between each waypoint.

subplot(2,1,1) plot(tvec, q) xlabel('t') ylabel('Positions') legend('X','Y') subplot(2,1,2) plot(tvec, qd) xlabel('t') ylabel('Velocities') legend('X','Y')

You can also verify the actual positions in the 2-D plane. Plot the separate rows of the `q`

vector and the waypoints as *x-* and *y-*positions.

figure plot(q(1,:),q(2,:),'-b',wpts(1,:),wpts(2,:),'or')

## Input Arguments

`wayPoints`

— Waypoints for trajectory

*n*-by-*p* matrix

Points for waypoints of trajectory, specified as an
*n*-by-*p* matrix, where *n* is the
dimension of the trajectory and *p* is the number of waypoints.

**Example: **`[1 4 4 3 -2 0; 0 1 2 4 3 1]`

**Data Types: **`single`

| `double`

`numSamples`

— Number of samples in output trajectory

positive integer

Number of samples in output trajectory, specified as a positive integer.

**Data Types: **`single`

| `double`

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

**Example: **`'PeakVelocity',5`

**Note**

Due to the nature of the trapezoidal velocity profile, you can only set at most two of the following parameters.

`PeakVelocity`

— Peak velocity of the velocity profile

scalar | *n*-element vector | *n*-by-(*p*–1) matrix

Peak velocity of the profile segment, specified as the comma-separated pair
consisting of `'PeakVelocity'`

and a scalar, vector, or matrix. This
peak velocity is the highest velocity achieved during the trapezoidal velocity
profile.

A scalar value is applied to all elements of the trajectory and between all
waypoints. An *n*-element vector is applied to each element of the
trajectory between all waypoints. An *n*-by-(*p*–1)
matrix is applied to each element of the trajectory for each waypoint.

**Data Types: **`single`

| `double`

`Acceleration`

— Acceleration of velocity profile

scalar | *n*-element vector | *n*-by-(*p*–1) matrix

Acceleration of the velocity profile, specified as the comma-separated pair
consisting of `'Acceleration'`

and a scalar, vector, or matrix. This
acceleration defines the constant acceleration from zero velocity to the
`PeakVelocity`

value.

A scalar value is applied to all elements of the trajectory and between all
waypoints. An *n*-element vector is applied to each element of the
trajectory between all waypoints. An *n*-by-(*p*–1)
matrix is applied to each element of the trajectory for each waypoint.

**Data Types: **`single`

| `double`

`EndTime`

— Duration of each trajectory segment

scalar | *n*-element vector | *n*-by-(*p*–1) matrix

Duration of each of the *p*–1 trajectory segments, specified as
the comma-separated pair consisting of `'EndTime'`

and a scalar,
vector, or matrix.

A scalar value is applied to all elements of the trajectory and between all
waypoints. An *n*-element vector is applied to each element of the
trajectory between all waypoints. An *n*-by-(*p*–1)
matrix is applied to each element of the trajectory for each waypoint.

**Data Types: **`single`

| `double`

`AccelTime`

— Duration of acceleration phase of velocity profile

scalar | *n*-element vector | *n*-by-(*p*–1) matrix

Duration of acceleration phase of velocity profile, specified as the
comma-separated pair consisting of `'AccelTime'`

and a scalar,
vector, or matrix.

*n*-element vector is applied to each element of the
trajectory between all waypoints. An *n*-by-(*p*–1)
matrix is applied to each element of the trajectory for each waypoint.

**Data Types: **`single`

| `double`

## Output Arguments

`q`

— Positions of trajectory

*n*-by-*m* matrix

Positions of the trajectory at the given time samples in
`tSamples`

, returned as
*n*-by-*m* matrix, where *n* is
the dimension of the trajectory, and *m* is equal to
`numSamples`

.

**Data Types: **`single`

| `double`

`qd`

— Velocities of trajectory

*n*-by-*m* matrix

Velocities of the trajectory at the given time samples in
`tSamples`

, returned as
*n*-by-*m* matrix, where *n* is
the dimension of the trajectory, and *m* is equal to
`numSamples`

.

**Data Types: **`single`

| `double`

`qdd`

— Accelerations of trajectory

*n*-by-*m* matrix

Accelerations of the trajectory at the given time samples in
`tSamples`

, returned as
*n*-by-*m* matrix, where *n* is
the dimension of the trajectory, and *m* is equal to
`numSamples`

.

**Data Types: **`single`

| `double`

`pp`

— Piecewise polynomials

cell array or structures

Piecewise polynomials, returned as a cell array of structures that defines the
polynomial for each section of the piecewise trajectory. If all the elements of the
trajectory share the same breaks, the cell array is a single piecewise polynomial
structure. Otherwise, the cell array has *n* elements, which correspond
to each of the different trajectory elements (dimensions). Each structure contains the fields:

`form`

:`'pp'`

.`breaks`

:*p*-element vector of times when the piecewise trajectory changes forms.*p*is the number of waypoints.`coefs`

:*n*(*p*–1)-by-`order`

matrix for the coefficients for the polynomials.*n*(*p*–1) is the dimension of the trajectory times the number of`pieces`

. Each set of*n*rows defines the coefficients for the polynomial that described each variable trajectory.`pieces`

:*p*–1. The number of breaks minus 1.`order`

: Degree of the polynomial + 1. For example, cubic polynomials have an order of 4.`dim`

:*n*. The dimension of the control point positions.

You can build your own piecewise polynomials using `mkpp`

, or evaluate the polynomial at specified times using `ppval`

.

`pp`

— Piecewise-polynomial

structure

Piecewise-polynomial, returned as a structure that defines the polynomial for each
section of the piecewise trajectory. You can build your own piecewise polynomials using
`mkpp`

, or evaluate the polynomial at
specified times using `ppval`

. The structure contains the fields:

`form`

:`'pp'`

.`breaks`

:*p*-element vector of times when the piecewise trajectory changes forms.*p*is the number of waypoints.`coefs`

:*n*(*p*–1)-by-`order`

matrix for the coefficients for the polynomials.*n*(*p*–1) is the dimension of the trajectory times the number of`pieces`

. Each set of*n*rows defines the coefficients for the polynomial that described each variable trajectory.`pieces`

:*p*–1. The number of breaks minus 1.`order`

: Degree of the polynomial + 1. For example, cubic polynomials have an order of 4.`dim`

:*n*. The dimension of the control point positions.

## References

[1] Lynch, Kevin M., and Frank C.
Park. *Modern Robotics: Mechanics, Planning and Control*. Cambridge:
Cambridge University Press, 2017.

[2] Spong, Mark W., Seth Hutchinson,
and M. Vidyasagar. *Robot Modeling and Control*. John Wiley
& Sons, 2006.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

## Version History

**Introduced in R2019a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)