# Variable Constraints #

Constraints specify the maximum velocities and accelerations allowed in trajectories generated by Road Runner. Typically, one set of constraints is set based on the robot’s capabilities and used for all trajectories. This works most of the time, though there may be certain motions that should be executed at a slower speed (e.g., paths close to obstacles).

## Per-segment Constraints #

One way to accomplish a slowdown is by overriding the base constraints over a specific segment. Let’s say you have the trajectory

```
builder
.splineTo(new Vector2d(0.0, 48.0), 0.0)
.splineTo(new Vector2d(48.0, 0.0), -Math.PI / 2)
.splineTo(new Vector2d(0.0, -48.0), -Math.PI)
.splineTo(new Vector2d(-48.0, 0.0), Math.PI / 2)
.build();
```

with the base constraints

```
VelConstraint baseVelConstraint = new MinVelConstraint(Arrays.asList(
new TranslationalVelConstraint(50.0),
new AngularVelConstraint(Math.PI / 2)
));
AccelConstraint baseAccelConstraint = new ProfileAccelConstraint(-10.0, 25.0);
```

The resulting path will look like

Each of the path-related builder functions (e.g., `splineTo()`

) take velocity
and acceleration constraints as optional overrides. Here’s a new variation of
the path from before with a modified trajectory:

```
builder
.splineTo(new Vector2d(0.0, 48.0), 0.0)
.splineTo(new Vector2d(48.0, 0.0), -Math.PI / 2,
// only override velocity constraint
new TranslationalVelConstraint(20.0))
.splineTo(new Vector2d(0.0, -48.0), -Math.PI,
// skip velocity constraint and only override acceleration constraint
null,
new ProfileAccelConstraint(-10.0, 10.0))
// revert back to the base constraints
.splineTo(new Vector2d(-48.0, 0.0), Math.PI / 2)
.build();
```

## Custom Constraints #

Alternatively, you can write your own base constraints that give maximum robot velocity/acceleration depending on the robot’s pose, path derivatives, current path, and current displacement. This velocity constraint slows the robot in a particular region of the field:

```
VelConstraint baseVelConstraint = (robotPose, _path, _disp) -> {
if (robotPose.position.x.value() > 24.0) {
return 20.0;
} else {
return 50.0;
}
};
```

With the same path as the last section, you get the trajectory