# PVT Sequence Generation

This guide covers the [`generate_velocities`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocities), [`generate_positions`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatepositions) and [`generate_velocities_and_times`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocitiesandtimes) functions of the [`PvtSequence`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md) class.
If you are new to PVT, please refer to [our introductory guide](https://software.zaber.com/motion-library/docs/guides/device_features/pvt.md) on the basics of setting up sequences and loading points.

PVT is a very useful feature of Zaber devices. It allows for precise control of timing and movement over continuous paths
with many points, but path planning can involve solving for some unknowns. For some applications, a user may know the specific
time and position for a point in a sequence, but not the velocity. In others, the user may know the times and velocities but
not the positions. There may also be situations where the user knows only the positions. In any case, finding the best values
to specify the desired motion requires either some fortuitous guess-work or a fairly comprehensive understanding of the math behind PVT.

This sequence generation API aims to simplify PVT path-planning and solve for these unknowns in a sensible way.

## Sequence Generation API

### PVT Sequence Data

All of the sequence generation functions return an array of [`PvtSequenceItem`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequenceitem.md) objects, which
can either be saved as a CSV file using [`save_sequence_data`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#savesequencedata), or used directly with the
PvtSequence [`submit_sequence_data`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#submitsequencedata) method. Typically most of the
items in the array are of type [`PvtPoint`](https://software.zaber.com/motion-library/api/py/ascii/pvtpoint.md), but they are held in or represented
by (depending on programming language) [`PvtSequenceItem`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequenceitem.md) because you can also
interleave non-point PVT actions such as output port operations in the array, and they will be preserved.

For example:

**Python:**

```py
# Save as CSV
PvtSequence.save_sequence_data(generated, "my_pvt_sequence.csv")

# Add points (sequence must be in live or store mode)
pvt_sequence.submit_sequence_data(generated)
pvt_sequence.wait_until_idle()
```

**C#:**

```cs
// Save as CSV
PvtSequence.SaveSequenceData(generated, "my_pvt_sequence.csv");

// Add points (sequence must be in live or store mode)
pvtSequence.SubmitSequenceData(generated);
pvtSequence.WaitUntilIdle();
```

**C++:**

```cpp
// Save as CSV
PvtSequence::saveSequenceData(generated, "my_pvt_sequence.csv");

// Add points (sequence must be in live or store mode)
pvtSequence.submitSequenceData(generated);
pvtSequence.waitUntilIdle();
```

Points input to the sequence generation functions are a different type, [`PvtPartialPoint`](https://software.zaber.com/motion-library/api/py/ascii/pvtpartialpoint.md), which
allows for missing elements (or missing columns in the CSV format). These can be interleaved with the same non-point PVT action types if needed.

### Notes on Times

For the sake the consistency of the trajectory and path plots, the example inputs for [`generate_velocities`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocities) and [`generate_positions`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatepositions) both have initial times set to 0.
The [`generate_velocities_and_times`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocitiesandtimes) function will also return an initial time of zero.

If the initial point in a sequence has its time set to zero, the library treats it as a special "start position". Start positions
must have an absolute position and zero velocity, and the device must already be at the start position when the sequence is submitted.
The start position will not be submitted to the device as part of the PVT sequence.

If that behavior is undesirable, you must set the time of the first point to something greater than zero, giving the device
enough time to move to the first point.

Here's an example showing how to move a two-axis device to the start position:

**Python:**

```py
start = cast(PvtPoint, generated[0])
device.get_axis(1).move_absolute(start.positions[0].value, cast(LengthUnits, start.positions[0].unit))
device.get_axis(2).move_absolute(start.positions[1].value, cast(LengthUnits, start.positions[1].unit))
```

**C#:**

```cs
start = generated[0].AsPvtPoint;
device.GetAxis(1).MoveAbsolute(start.Positions[0].Value, start.Positions[0].Unit ?? Units.Native);
device.GetAxis(2).MoveAbsolute(start.Positions[1].Value, start.Positions[1].Unit ?? Units.Native);
```

**C++:**

```cpp
start = std::get<PvtPoint>(generated[0]);
device.getAxis(1).moveAbsolute(start.positions[0].value, start.positions[0].unit.value_or(Units::NATIVE));
device.getAxis(2).moveAbsolute(start.positions[1].value, start.positions[1].unit.value_or(Units::NATIVE));
```

Generally, the Zaber Motion Library PVT functions expect the point times to be relative. That is, relative
to the previous point. The opposite condition, called absolute time, is when each point's time value is relative
to the beginning of the sequence. There are functions to convert times between relative and absolute, for example:

**Python:**

```py
points = PvtSequence.convert_time_absolute_to_relative_partial(points)
```

**C#:**

```cs
points = PvtSequence.ConvertTimeAbsoluteToRelativePartial(points);
```

**C++:**

```cpp
points = PvtSequence::convertTimeAbsoluteToRelativePartial(points);
```

## Generate Velocities ([`generate_velocities`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocities))

This function works by generating velocities such that acceleration is continuous over the entire sequence. It will also set the
start and end velocities to zero unless otherwise specified (see `With Partially Defined Velocities` section below).

In this example we are generating the velocities for a 2-dimensional spiral path. The inputs to the sequence generation functions
are always arrays of [`PvtPartialPoint`](https://software.zaber.com/motion-library/api/py/ascii/pvtpartialpoint.md), which allows the positions and velocities to be empty
arrays or to contain null measurements, and allows the time measurement to be null. Only some combinations of missing
measurements are supported by the sequence generation functions.

**Python:**

```py
points: list[PvtPartialSequenceItem] = [
    PvtPartialPoint(positions=[Measurement(14.5, "mm"), Measurement(7.5, "mm")], velocities=[], time=Measurement(0, "s")),
    PvtPartialPoint(positions=[Measurement(15.943, "mm"), Measurement(6.666, "mm")], velocities=[], time=Measurement(3.333, "s")),
    PvtPartialPoint(positions=[Measurement(11.613, "mm"), Measurement(5.833, "mm")], velocities=[], time=Measurement(6.667, "s")),
    PvtPartialPoint(positions=[Measurement(14.5, "mm"), Measurement(12.5, "mm")], velocities=[], time=Measurement(10, "s")),
    PvtPartialPoint(positions=[Measurement(20.274, "mm"), Measurement(4.167, "mm")], velocities=[], time=Measurement(13.333, "s")),
    PvtPartialPoint(positions=[Measurement(7.283, "mm"), Measurement(3.333, "mm")], velocities=[], time=Measurement(16.667, "s")),
    PvtPartialPoint(positions=[Measurement(22, "mm"), Measurement(15, "mm")], velocities=[], time=Measurement(20, "s")),
]
```

**C#:**

```cs
PvtPartialSequenceItem[] points = [
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 14.5, Unit = Units.Length_Millimetres },
            new Measurement { Value = 7.5, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 0, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 15.943, Unit = Units.Length_Millimetres },
            new Measurement { Value = 6.666, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 3.333, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 11.613, Unit = Units.Length_Millimetres },
            new Measurement { Value = 5.833, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 6.667, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 14.5, Unit = Units.Length_Millimetres },
            new Measurement { Value = 12.5, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 10, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 20.274, Unit = Units.Length_Millimetres },
            new Measurement { Value = 4.167, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 13.333, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 7.283, Unit = Units.Length_Millimetres },
            new Measurement { Value = 3.333, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 16.667, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 22, Unit = Units.Length_Millimetres },
            new Measurement { Value = 15, Unit = Units.Length_Millimetres },
        ],
        Time = new Measurement { Value = 20, Unit = Units.Time_Seconds },
    },
];
```

**C++:**

```cpp
std::vector<PvtPartialSequenceItem> points = {
    PvtPartialPoint(
        {Measurement(14.5, Units::LENGTH_MILLIMETRES), Measurement(7.5, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(0, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(15.943, Units::LENGTH_MILLIMETRES), Measurement(6.666, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(3.333, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(11.613, Units::LENGTH_MILLIMETRES), Measurement(5.833, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(6.667, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(14.5, Units::LENGTH_MILLIMETRES), Measurement(12.5, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(10, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(20.274, Units::LENGTH_MILLIMETRES), Measurement(4.167, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(13.333, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(7.283, Units::LENGTH_MILLIMETRES), Measurement(3.333, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(16.667, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {Measurement(22, Units::LENGTH_MILLIMETRES), Measurement(15, Units::LENGTH_MILLIMETRES)},
        {},
        Measurement(20, Units::TIME_SECONDS),
        std::nullopt
    ),
};
```

Then make the call to generate the velocities for the sequence. Note that while you can specify the data with either relative or absolute time,
the sequence generation functions expect input times to be relative. Additionally, the time sequences in the output data are always relative.
This is because [`submit_sequence_data`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#submitsequencedata) expects times to be relative. This example includes
a call to convert to relative time:

**Python:**

```py
points = PvtSequence.convert_time_absolute_to_relative_partial(points)
generated = PvtSequence.generate_velocities(points)
```

**C#:**

```cs
points = PvtSequence.ConvertTimeAbsoluteToRelativePartial(points);
var generated = PvtSequence.GenerateVelocities(points);
```

**C++:**

```cpp
points = PvtSequence::convertTimeAbsoluteToRelativePartial(points);
std::vector<PvtSequenceItem> generated = PvtSequence::generateVelocities(points);
```

![Path and Trajectory with Generated Velocities](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_velocities_path_and_trajectory.png)

Note that there is no guarantee that the generated sequence will adhere to your axes' velocity and acceleration constraints,
so it is important to provide 'reasonable' times which allow for enough travel time from point to point.

### With Partially Defined Velocities

Optionally, you can pass in a partially-defined sequence of velocities. In this case the function will generate velocities with
continuous acceleration for all of the undefined segments of the path.

**Python:**

```py
cast(PvtPartialPoint, points[3]).velocities = [Measurement(5, "mm/s"), Measurement(0, "mm/s")]
generated = PvtSequence.generate_velocities(points)
```

**C#:**

```cs
points[3].AsPvtPartialPoint.Velocities = [
    new Measurement { Value = 5, Unit = Units.Velocity_MillimetresPerSecond },
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
];
generated = PvtSequence.GenerateVelocities(points);
```

**C++:**

```cpp
std::get<PvtPartialPoint>(points[3]).velocities = {
    Measurement(5, Units::VELOCITY_MILLIMETRES_PER_SECOND),
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
};
generated = PvtSequence::generateVelocities(points);
```

Since the start and end velocities are set to zero if undefined, the above velocity example is equivalent to:

**Python:**

```py
cast(PvtPartialPoint, points[0]).velocities = [Measurement(0, "mm/s"), Measurement(0, "mm/s")]
cast(PvtPartialPoint, points[3]).velocities = [Measurement(5, "mm/s"), Measurement(0, "mm/s")]
cast(PvtPartialPoint, points[6]).velocities = [Measurement(0, "mm/s"), Measurement(0, "mm/s")]
generated = PvtSequence.generate_velocities(points)
```

**C#:**

```cs
points[0].AsPvtPartialPoint.Velocities = [
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
];
points[3].AsPvtPartialPoint.Velocities = [
    new Measurement { Value = 5, Unit = Units.Velocity_MillimetresPerSecond },
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
];
points[6].AsPvtPartialPoint.Velocities = [
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
    new Measurement { Value = 0, Unit = Units.Velocity_MillimetresPerSecond },
];
generated = PvtSequence.GenerateVelocities(points);
```

**C++:**

```cpp
std::get<PvtPartialPoint>(points[0]).velocities = {
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
};
std::get<PvtPartialPoint>(points[3]).velocities = {
    Measurement(5, Units::VELOCITY_MILLIMETRES_PER_SECOND),
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
};
std::get<PvtPartialPoint>(points[6]).velocities = {
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
    Measurement(0, Units::VELOCITY_MILLIMETRES_PER_SECOND),
};
generated = PvtSequence::generateVelocities(points);
```

The call to generate velocities produces the following path:

![Path and Trajectory with Partially Generated Velocities](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_velocities_partial_path_and_trajectory.png)

Notice that acceleration is not continuous between the path segments on either side of the defined point. This is because
the function actually splits the path into two sections and solves each separately, so while the continuity constraint applies
over the first and second sub-paths, it isn't enforced over the entire path.

## Generate Positions ([`generate_positions`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatepositions))

Like [`generate_velocities`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocities), this function also enforces that acceleration is
continuous from point to point. Additionally, it uses the constraint that acceleration is 0 at the start point.

In this example we are generating the positions for a 1-dimensional rotary path with velocities and times:

**Python:**

```py
velocities: list[PvtPartialSequenceItem] = [
    PvtPartialPoint(positions=[], velocities=[Measurement(0, "°/s")], time=Measurement(0, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(180, "°/s")], time=Measurement(3, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(-180, "°/s")], time=Measurement(6, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(180, "°/s")], time=Measurement(6, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(360, "°/s")], time=Measurement(3, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(0, "°/s")], time=Measurement(6, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(-180, "°/s")], time=Measurement(3, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(-360, "°/s")], time=Measurement(3, "s")),
    PvtPartialPoint(positions=[], velocities=[Measurement(0, "°/s")], time=Measurement(6, "s")),
]
```

**C#:**

```cs
PvtPartialSequenceItem[] velocities = [
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 0, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 0, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 180, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 3, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = -180, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 6, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 180, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 6, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 360, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 3, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 0, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 6, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = -180, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 3, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = -360, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 3, Unit = Units.Time_Seconds },
    },
    new PvtPartialPoint
    {
        Velocities = [new Measurement { Value = 0, Unit = Units.AngularVelocity_DegreesPerSecond }],
        Time = new Measurement { Value = 6, Unit = Units.Time_Seconds },
    },
];
```

**C++:**

```cpp
std::vector<PvtPartialSequenceItem> velocities = {
    PvtPartialPoint(
        {},
        {Measurement(0, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(0, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(180, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(3, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(-180, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(6, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(180, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(6, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(360, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(3, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(0, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(6, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(-180, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(3, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(-360, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(3, Units::TIME_SECONDS),
        std::nullopt
    ),
    PvtPartialPoint(
        {},
        {Measurement(0, Units::ANGULAR_VELOCITY_DEGREES_PER_SECOND)},
        Measurement(6, Units::TIME_SECONDS),
        std::nullopt
    ),
};
```

Then, the call to generate positions produces the following trajectory. Notice that in this case we have specified
times to be relative (the default value for this parameter is true).

**Python:**

```py
generated = PvtSequence.generate_positions(velocities)
```

**C#:**

```cs
generated = PvtSequence.GeneratePositions(velocities);
```

**C++:**

```cpp
generated = PvtSequence::generatePositions(velocities);
```

![Trajectory with Generated Positions](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_positions_rotary_trajectory.png)

## Generate Velocities and Times ([`generate_velocities_and_times`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocitiesandtimes))

This function works differently than [`generate_velocities`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatevelocities) and [`generate_positions`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md#generatepositions).
It first fits a geometric spline over the position sequence and then calculates the velocity and time information by
traversing the spline using a trapezoidal motion profile. While it will produce a smooth trajectory, it does not
guarantee that velocity will be continuous over the entire path. It is also only capable of generating trajectories
for single axis linear and rotary trajectories, and multi-axis linear trajectories where the directions of all axes are orthogonal (ie. Cartesian systems).

In this example, we are generating the same 2-dimensional spiral path using only position information.

**Python:**

```py
positions: list[PvtPartialSequenceItem] = [
    PvtPartialPoint(positions=[Measurement(14.5, "mm"), Measurement(7.5, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(15.943, "mm"), Measurement(6.666, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(11.613, "mm"), Measurement(5.833, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(14.5, "mm"), Measurement(12.5, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(20.274, "mm"), Measurement(4.167, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(7.283, "mm"), Measurement(3.333, "mm")], velocities=[]),
    PvtPartialPoint(positions=[Measurement(22, "mm"), Measurement(15, "mm")], velocities=[]),
]
```

**C#:**

```cs
PvtPartialSequenceItem[] positions = [
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 14.5, Unit = Units.Length_Millimetres },
            new Measurement { Value = 7.5, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 15.943, Unit = Units.Length_Millimetres },
            new Measurement { Value = 6.666, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 11.613, Unit = Units.Length_Millimetres },
            new Measurement { Value = 5.833, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 14.5, Unit = Units.Length_Millimetres },
            new Measurement { Value = 12.5, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 20.274, Unit = Units.Length_Millimetres },
            new Measurement { Value = 4.167, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 7.283, Unit = Units.Length_Millimetres },
            new Measurement { Value = 3.333, Unit = Units.Length_Millimetres },
        ],
    },
    new PvtPartialPoint
    {
        Positions = [
            new Measurement { Value = 22, Unit = Units.Length_Millimetres },
            new Measurement { Value = 15, Unit = Units.Length_Millimetres },
        ],
    },
];
```

**C++:**

```cpp
std::vector<PvtPartialSequenceItem> positions = {
    PvtPartialPoint(
        {Measurement(14.5, Units::LENGTH_MILLIMETRES), Measurement(7.5, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(15.943, Units::LENGTH_MILLIMETRES), Measurement(6.666, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(11.613, Units::LENGTH_MILLIMETRES), Measurement(5.833, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(14.5, Units::LENGTH_MILLIMETRES), Measurement(12.5, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(20.274, Units::LENGTH_MILLIMETRES), Measurement(4.167, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(7.283, Units::LENGTH_MILLIMETRES), Measurement(3.333, Units::LENGTH_MILLIMETRES)},
        {}
    ),
    PvtPartialPoint(
        {Measurement(22, Units::LENGTH_MILLIMETRES), Measurement(15, Units::LENGTH_MILLIMETRES)},
        {}
    ),
};
```

Then we generate the velocities and times for the sequence with a target speed of 10mm/s and target acceleration of 5mm/s².

**Python:**

```py
target_speed = Measurement(10, "mm/s")
target_accel = Measurement(5, "mm/s²")
generated = PvtSequence.generate_velocities_and_times(positions, target_speed, target_accel)
```

**C#:**

```cs
var targetSpeed = new Measurement { Value = 10, Unit = Units.Velocity_MillimetresPerSecond };
var targetAccel = new Measurement { Value = 5, Unit = Units.Acceleration_MillimetresPerSecondSquared };
generated = PvtSequence.GenerateVelocitiesAndTimes(positions, targetSpeed, targetAccel);
```

**C++:**

```cpp
Measurement targetSpeed(10, Units::VELOCITY_MILLIMETRES_PER_SECOND);
Measurement targetAccel(5, Units::ACCELERATION_MILLIMETRES_PER_SECOND_SQUARED);
generated = PvtSequence::generateVelocitiesAndTimes(positions, targetSpeed, targetAccel);
```

![Path and Trajectory with Generated Velocities and Times](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_velocities_and_times_path_and_trajectory.png)

Notice that acceleration and velocity don't adhere perfectly to our target values. The following section on resampling
provides a means of keeping the trajectory closer to the specified velocity and acceleration targets.

### With Resampling

Resampling allows you to specify the number of sample points along the geometric spline which is fitted to the input
positions. For PVT paths with few positions, this can help the function to build a trajectory which adheres better to
the velocity and acceleration targets. For PVT paths with many positions, this can help to reduce the size of the final
generated sequence.

We will be upsampling the path from above using the same target speed and acceleration values and a resample value of 30,
so that there will be 30 PVT points in the returned sequence data object.

**Python:**

```py
generated = PvtSequence.generate_velocities_and_times(positions, target_speed, target_accel, 30)
```

**C#:**

```cs
generated = PvtSequence.GenerateVelocitiesAndTimes(positions, targetSpeed, targetAccel, 30);
```

**C++:**

```cpp
generated = PvtSequence::generateVelocitiesAndTimes(positions, targetSpeed, targetAccel, 30);
```

![Path and Trajectory with Generated Velocities and Times Resampled](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_velocities_and_times_resampled_path_and_trajectory.png)

Notice that after the initial curve in the path, the trajectory stays much closer to the target speed and acceleration
than in the previous example.

It is important to note that resampled paths may not pass through the exact positions in the input sequence. This is
because the input positions likely won't be included in the generated trajectory. The following plot illustrates this
clearly, with the red x's representing the original input positions:

![Path with Generated Velocities and Times Resampled with Original Positions](https://software.zaber.com/dev-portal/motion-library/docs/assets/images/pvt_generate_velocities_and_times_resampled_with_points_path_and_trajectory.png)

If for your application you require your device to pass through the input positions exactly, then resampling may not be
the tool for you. That said, a higher resample value will likely reduce the amount of deviation.

Also note that resampling does not work on sequences that contain non-point actions, because their relative timing would be
lost.

## Further Reading

- [Mastering Motion: Position-Velocity-Time Control for Smooth and Precise Path Planning](https://www.zaber.com/articles/motion-planning-with-position-velocity-time)
- [PVT Sequence Generation (with helper functions for visualization)](https://www.zaber.com/articles/git-motion_pvt_sequence_generation)

## Resources

- [ASCII Protocol Manual - PVT (firmware version 7.33)](https://www.zaber.com/protocol-manual#topic_command_pvt)
- API Reference
  * [`PvtSequence`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequence.md)
  * [`PvtSequenceItem`](https://software.zaber.com/motion-library/api/py/ascii/pvtsequenceitem.md)
  * [`PvtPartialSequenceItem`](https://software.zaber.com/motion-library/api/py/ascii/pvtpartialsequenceitem.md)
  * [`MeasurementSequence`](https://software.zaber.com/motion-library/api/py/ascii/measurementsequence.md)
