> For the complete documentation index, see [llms.txt](https://docs.cb21829.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cb21829.org/ftc-java-advanced-guide/motion-profiling-and-advanced-movement.md).

# Motion profiling and advanced movement

Motion profiling gives a controller a smooth target over time instead of an abrupt position jump.

### The trapezoidal profile

A trapezoidal profile has three phases:

1. accelerate
2. cruise
3. decelerate

```java
public static double motionProfilePosition(double maxAccel, double maxVel,
                                            double distance, double elapsedTime) {
    double accelTime = maxVel / maxAccel;
    double accelDist = 0.5 * maxAccel * accelTime * accelTime;

    if (accelDist > distance / 2.0) {
        accelTime = Math.sqrt(distance / maxAccel);
        accelDist = distance / 2.0;
    }

    double cruiseDist = distance - 2 * accelDist;
    double cruiseTime = cruiseDist / maxVel;
    double decelTime  = accelTime;
    double totalTime  = accelTime + cruiseTime + decelTime;

    double t = Math.min(elapsedTime, totalTime);

    if (t < accelTime) {
        return 0.5 * maxAccel * t * t;
    } else if (t < accelTime + cruiseTime) {
        double cruiseElapsed = t - accelTime;
        return accelDist + maxVel * cruiseElapsed;
    } else {
        double decelElapsed = t - accelTime - cruiseTime;
        return accelDist + cruiseDist
                + maxVel * decelElapsed
                - 0.5 * maxAccel * decelElapsed * decelElapsed;
    }
}
```

### Combining profile, PID, and feedforward

```java
double targetPos = motionProfilePosition(MAX_ACCEL, MAX_VEL, distance, timer.seconds());

double power = pid.calculate(targetPos, motor.getCurrentPosition())
             + kV * targetVelocity
             + kA * targetAcceleration;

motor.setPower(power);
```

This is especially useful for lifts, arms, and slides.

Trapezoidal profiles are the right place to start. Use S-curves only if you still need smoother motion.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cb21829.org/ftc-java-advanced-guide/motion-profiling-and-advanced-movement.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
