> For the complete documentation index, see [llms.txt](https://yams.yassrobotics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-drive-config.md). Agents can also search and read this documentation through its [MCP server](https://yams.yassrobotics.com/~gitbook/mcp).

# SwerveDriveConfig

`SwerveDriveConfig` describes the whole drive base. Anything about a single wheel is on that module's [`SwerveModuleConfig`](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-module-config.md). The [Swerve Drive](https://yams.yassrobotics.com/documentation/understanding/swerve-drive.md) page shows a complete drive.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-f67121634d933a51b196ae295120235e8967f67d%2Fmech-swerve-map.png?alt=media" alt="A grid of six groups of SwerveDriveConfig options: the modules, the gyro, speed limits, auto-align controllers, driving corrections, and start and telemetry, each listing its methods."><figcaption><p>The option groups on a SwerveDriveConfig.</p></figcaption></figure>

Every option on this page is used on the real robot and in simulation unless it says otherwise. Options with a `withSim...` twin use that value in simulation instead, when it is set.

## The modules

The modules are given to the constructor, or with `withModules`. The drive's kinematics are built from [each module's location](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-module-config.md#withlocation).

```java
SwerveDriveConfig config = new SwerveDriveConfig(this, frontLeft, frontRight, backLeft, backRight);
```

### `withModuleStateOptimization`

Turns [state optimization](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-module-config.md#withoptimization) on or off for every module at once, overriding each module's own setting. It can be changed while the drive is running.

```java
drive.getConfig().withModuleStateOptimization(false);
```

## The gyro

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-e7eda2d94c53f18b633a963bba80743f8f5f7728%2Fmech-swerve-gyro.png?alt=media" alt="Three panels showing a robot turned 30 degrees counter-clockwise. With withGyro alone the heading reads plus 30 degrees. With withGyroInverted the same turn reads minus 30 degrees. With withGyroOffset of 90 degrees the heading reads 90 degrees different."><figcaption><p>The gyro options change how the same turn is read.</p></figcaption></figure>

| Option                                        | What it does                                                                                                                                                         |
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `withGyro(Supplier<Rotation3d>)`              | Supplies the robot's attitude. Its yaw is the heading.                                                                                                               |
| `withGyroInverted(boolean)`                   | Flips the heading, for a gyro that reads clockwise as positive.                                                                                                      |
| `withGyroOffset(Angle)`                       | Adds a fixed angle to the heading.                                                                                                                                   |
| `withGyroHeadingAxis(GyroAxis)`               | Chooses which axis is the heading, for a gyro mounted on its side.                                                                                                   |
| `withGyroVelocity(Supplier<AngularVelocity>)` | Supplies the turn rate, used by [`withGyroAngularVelocityScaleFactor`](#withgyroangularvelocityscalefactor). Without it the turn rate is estimated from the heading. |

```java
config.withGyro(pigeon::getRotation3d)
      .withGyroVelocity(() -> pigeon.getAngularVelocityZWorld().getValue());
```

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-c4cb94eececc5e0d196a99a6f23101e015b34e01%2Fgyro-axes.png?alt=media" alt="A gyro drawn as a box lying flat and standing on each side, with its X, Y and Z axes"><figcaption><p>A gyro lying flat and standing on each side.</p></figcaption></figure>

[How do I use a gyro mounted on its side?](https://yams.yassrobotics.com/documentation/how-to-motor-controllers-and-sensors/how-do-i-use-a-gyro-mounted-on-its-side.md) covers `withGyroHeadingAxis`.

In simulation the heading comes from the simulated drive, not from the gyro supplier.

## Speed limits

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-7e7838e7a354aa6357a496166e5d753bdbbcc860%2Fmech-swerve-desaturation.png?alt=media" alt="Two bar charts of four wheel speeds. As asked for, two wheels are above the 4.5 meters per second maximum. With withMaximumModuleSpeed all four are slowed by the same factor so the fastest is at the maximum, which keeps the direction of travel."><figcaption><p>Wheel speeds before and after they are limited.</p></figcaption></figure>

```java
config.withMaximumModuleSpeed(MetersPerSecond.of(4.5))
      .withMaximumChassisSpeed(MetersPerSecond.of(4.5), DegreesPerSecond.of(540));
```

| Option                    | What it does                                                                                                        |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `withMaximumModuleSpeed`  | The fastest a wheel can go. When a move needs more, every wheel is slowed by the same factor.                       |
| `withMaximumChassisSpeed` | The fastest the robot should drive and turn. With both set, the slowdown also keeps the mix of driving and turning. |

Without `withMaximumModuleSpeed` nothing is limited, and wheels asked to go too fast each fall short by a different amount, which bends the robot's path.

`withMaximumChassisSpeed` is also the starting value for a [`SwerveInputStream`](https://yams.yassrobotics.com/documentation/reference/swerve-input-stream.md#velocity-limits)'s maximum velocities.

## Auto-align controllers

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-40db8b761d3786862b6f66d351af9882280480f2%2Fmech-swerve-controllers.png?alt=media" alt="A field with a robot and a target pose, marking the distance left and the angle left. withTranslationController takes meters from the target and outputs how fast to drive there. withRotationController takes radians from the target heading and outputs how fast to turn, and wraps at 180 degrees for you."><figcaption><p>The translation and rotation controllers.</p></figcaption></figure>

```java
config.withTranslationController(new PIDController(4, 0, 0))
      .withRotationController(new PIDController(5, 0, 0));
```

They are used by [auto-align](https://yams.yassrobotics.com/documentation/understanding/swerve-drive.md#auto-align) and by the heading, aim and translation-only modes of a [`SwerveInputStream`](https://yams.yassrobotics.com/documentation/reference/swerve-input-stream/swerve-input-stream-aim.md#modes-are-chosen-automatically), which throw a `SwerveDriveConfigurationException` if no rotation controller is set. Normal stick driving does not use them.

`withSimTranslationController` and `withSimRotationController` give different gains for simulation only. [Tune a swerve drive](https://yams.yassrobotics.com/documentation/how-to-tuning/live-tuning/swerve-drive.md#tune-auto-align) shows how to find the values.

## Driving corrections

### `withDiscretizationTime`

Corrects the path of a robot that is driving and turning at the same time.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-1f2eb74c90541199aa2cfd31b2e025f03f4b049c%2Fmech-swerve-discretization.png?alt=media" alt="Two panels of a robot asked to drive in a straight line while spinning. Without the option its path drifts off to one side. With withDiscretizationTime of 20 milliseconds it stays on the line."><figcaption><p>A straight line while spinning, without and with discretization.</p></figcaption></figure>

```java
config.withDiscretizationTime(Milliseconds.of(20)); // the robot's loop time
```

`withSimDiscretizationTime` sets a different value for simulation.

### `withGyroAngularVelocityScaleFactor`

A second correction for the same drift, using how fast the gyro says the robot is turning. It is a number from 0 to 1, found by experiment: drive in a straight line while spinning and adjust it until the drift is smallest.

```java
config.withGyroVelocity(() -> pigeon.getAngularVelocityZWorld().getValue())
      .withGyroAngularVelocityScaleFactor(0.1);
```

It needs a gyro: without `withGyroVelocity(...)` it reads the gyro set with `withGyro(...)`, and throws an `IllegalStateException` if that is not set either. `withSimGyroAngularVelocityScaleFactor` sets a different value for simulation.

{% hint style="info" %}
This option has no graphic of its own: it corrects the same drift shown above for `withDiscretizationTime`.
{% endhint %}

### `withAntiTipping`

Drives the robot back under itself when it starts to tip.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-1bb94d53a66c4ef8ddb865c2a4fbb166012912af%2Fmech-swerve-anti-tipping.png?alt=media" alt="A robot with a tall mechanism tilted past the threshold. A correction arrow drives it toward the side it is tipping to. The three numbers are the tipping threshold, the tilt at which it starts correcting; kP, the correction speed for a given tilt; and the maximum correction speed."><figcaption><p>The correction drives toward the side the robot is falling to.</p></figcaption></figure>

```java
config.withAntiTipping(MetersPerSecond.of(2), Degrees.of(8), MetersPerSecond.of(1.5));
//                     kP                     threshold      maximum correction
```

Set the threshold a few degrees above the most the robot tilts in normal driving. It reads pitch and roll from the gyro, so it needs [`withGyro`](#the-gyro).

### `withCenterOfRotation`

The point the robot spins around. The default is the center of the robot.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-2690d359da2ed8201e68652761a9503f7e3f0df7%2Fswerve-center-of-rotation.gif?alt=media" alt="Animation of two robots spinning with the same input. The first spins around its own center. The second spins around its front left corner"><figcaption><p>The same turn around the center and around a corner.</p></figcaption></figure>

```java
config.withCenterOfRotation(Meters.of(0.3), Meters.of(0.3)); // forward, left
```

It can be changed while driving: see [Center of Rotation](https://yams.yassrobotics.com/documentation/reference/swerve-input-stream.md#center-of-rotation).

## Start and telemetry

### `withStartingPose`

Where on the field the robot starts. The default is `new Pose2d()`. An autonomous routine or a vision measurement usually replaces it.

```java
config.withStartingPose(new Pose2d(2, 4, Rotation2d.fromDegrees(180)));
```

### `withTelemetry`

Names the drive and chooses what it publishes. The default name is `swerve`.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-627f3f23380b876348e0529d5e987b6b58706de1%2Ftelemetry-swerve-tree.png?alt=media" alt="The NetworkTables tree for a swerve drive: pose, gyro, chassis velocities, module states and each module&#x27;s motors"><figcaption><p>What a swerve drive publishes.</p></figcaption></figure>

```java
config.withTelemetry("swerve", TelemetryVerbosity.HIGH);
```

[Telemetry](https://yams.yassrobotics.com/documentation/understanding/telemetry.md#swerve-drive-telemetry) lists what each level publishes, and [Tune a swerve drive](https://yams.yassrobotics.com/documentation/how-to-tuning/live-tuning/swerve-drive.md) needs `HIGH`.

## Code reference

* `SwerveDriveConfig`
* `SwerveDrive`


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-drive-config.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
