> 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-module-config.md). Agents can also search and read this documentation through its [MCP server](https://yams.yassrobotics.com/~gitbook/mcp).

# SwerveModuleConfig

`SwerveModuleConfig` describes one swerve module: its two motors, where it is on the robot, and how its wheel is steered. The [Swerve Module](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md) page shows a complete module.

Every option on this page is used on the real robot and in simulation.

## The two motors

A module is built from a drive `SmartMotorController` and an azimuth `SmartMotorController`, each with its own [`SmartMotorControllerConfig`](https://yams.yassrobotics.com/documentation/reference/editor/config.md).

```java
SwerveModuleConfig config = new SwerveModuleConfig(driveMotor, azimuthMotor);
```

The drive motor's config needs the wheel size, with [`withWheelRadius` or `withWheelDiameter`](https://yams.yassrobotics.com/documentation/reference/editor/config/gearing-and-units.md#withmechanismcircumference). `SwerveModuleConfig.withWheelRadius` and `withWheelDiameter` set the same thing from here, but the motor config is the better place for it. The azimuth motor's config usually has [`withContinuousWrapping`](https://yams.yassrobotics.com/documentation/reference/editor/config/closed-loop-control.md#withcontinuouswrapping), so that the wheel takes the short way round from 170 degrees to -170.

## Directions and inversion

Every direction on a swerve module follows WPILib's convention: angles are counter-clockwise positive, seen from above, and a positive wheel speed drives the robot forward when the wheel points forward. There is no inversion option on `SwerveModuleConfig` itself: each direction is set where its sensor or motor is configured.

| What             | Must be                                                               | Set it with                                                                                                                                                                                     |
| ---------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Azimuth motor    | A positive command turns the wheel counter-clockwise, seen from above | `withMotorInverted(...)` on the azimuth motor's `SmartMotorControllerConfig`                                                                                                                    |
| Drive motor      | A positive speed rolls the wheel forward when it points forward       | `withMotorInverted(...)` on the drive motor's `SmartMotorControllerConfig`                                                                                                                      |
| Absolute encoder | Its reading goes up as the wheel turns counter-clockwise              | The encoder's own direction setting, such as a `CANcoder`'s sensor direction in Phoenix Tuner X, or `withExternalEncoderInverted(...)` for an encoder passed with `withAbsoluteEncoder(Object)` |
| Gyro             | The heading goes up as the robot turns counter-clockwise              | [`withGyroInverted`](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-drive-config.md#the-gyro) on the `SwerveDriveConfig`                                        |

A wheel that points the right way but rolls backward is usually not inverted: its absolute encoder offset is 180 degrees off. [Step 5: On the real robot](https://yams.yassrobotics.com/documentation/tutorials/swerve-drive/real-robot.md#check-the-directions) checks each direction in turn.

## `withLocation`

Where the module is, measured from 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-21ed773ea125aee961a5c8a5f965de9813292141%2Fmech-module-location.png?alt=media" alt="A top view of a robot with the center at zero, plus X pointing forward and plus Y pointing left. The front left module is at 0.3, 0.3; front right at 0.3, minus 0.3; back left at minus 0.3, 0.3; back right at minus 0.3, minus 0.3, in meters."><figcaption><p>Module locations: X is forward and Y is left.</p></figcaption></figure>

```java
config.withLocation(Meters.of(0.3), Meters.of(0.3));  // front, left
config.withLocation(Meters.of(0.42), Degrees.of(45)); // distance and angle from the center
```

`withDistanceFromCenterOfRotation(front, left)` is the same as the first form. Every module needs a location: a missing or wrong one gives wrong kinematics, not an error. See [Module Location Drives the Kinematics](https://yams.yassrobotics.com/documentation/understanding/swerve-drive.md#module-location-drives-the-kinematics).

## The absolute encoder

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-07248350cb5eba533b1f2c5fd94d3df7d4e25704%2Fmech-module-absolute-encoder.png?alt=media" alt="Two panels. Zero must be with the wheel pointing forward and its bevel gear facing the robot&#x27;s left. To find the offset, point every wheel forward with bevels to the left, read each absolute encoder, and use that reading, here 64 degrees, as that module&#x27;s offset."><figcaption><p>Zero is the wheel pointing forward with the bevel to the left.</p></figcaption></figure>

The absolute encoder tells the module which way its wheel points when the robot turns on. Set it with one of the two forms of `withAbsoluteEncoder`, then give it an offset.

### `withAbsoluteEncoder(Supplier<Angle>)`

An absolute encoder from **any vendor**, read by your code. The module reads it once at startup and sets the azimuth motor's own encoder from it.

```java
config.withAbsoluteEncoder(canCoder.getAbsolutePosition().asSupplier());
```

A `DoubleSupplier` in rotations works too. This is the form the [Swerve Drive tutorial](https://yams.yassrobotics.com/documentation/tutorials/swerve-drive.md) uses, with a `CANcoder` on a `SparkMax` azimuth.

### `withAbsoluteEncoder(Object)`

An absolute encoder from the **same vendor** as the azimuth motor, read by the motor controller itself, such as a `CANcoder` on a `TalonFX`. The azimuth motor's closed loop then runs on the absolute encoder.

```java
config.withAbsoluteEncoder(new CANcoder(11, new CANBus(CANPort.CAN_S0)));
```

{% hint style="warning" %}
This form only accepts an encoder the azimuth motor controller can read itself: see the [encoders each controller accepts](https://yams.yassrobotics.com/documentation/reference/editor/config/encoders.md#withexternalencoder). The azimuth motor's `SmartMotorControllerConfig` must also have `withUseExternalFeedbackEncoder(true)`: without it, `new SwerveModule(...)` throws a `SmartMotorControllerConfigurationException`. For any other encoder use the `Supplier<Angle>` form.
{% endhint %}

[Absolute Encoder Wiring](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md#absolute-encoder-wiring) covers both forms in detail.

### `withAbsoluteEncoderOffset`

The absolute encoder's reading when the wheel points straight forward with its bevel gear to the robot's left. YAMS subtracts it, so the module reads 0 there.

```java
config.withAbsoluteEncoderOffset(Degrees.of(64));
```

**The rule:** the offset is the raw reading itself, with the same sign. Point the wheel forward, bevel gear left, read `NT:/Mechanisms/<drive name>/modules/<module name>/encoder` (the raw reading in degrees, with no offset applied), and pass that number. Every module has its own. If a module then drives backward while the others drive forward, its bevel gear faced right when you read it: add 180 degrees. [Step 5: On the real robot](https://yams.yassrobotics.com/documentation/tutorials/swerve-drive/real-robot.md#set-the-absolute-encoder-offsets) walks through it.

### `withAbsoluteEncoderGearing`

For an absolute encoder that does not turn once per turn of the wheel's steering. Most modules mount it 1:1 and do not need this.

```java
config.withAbsoluteEncoderGearing(new GearBox(new double[]{1.5}));
```

## `withOptimization`

Lets a wheel reverse its drive instead of turning more than 90°. It is on by default.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-f5044619b00d77bf829be85e1d3ce70cd3c87981%2Fmech-module-optimization.png?alt=media" alt="Two panels of a wheel asked to point 160 degrees away from where it is. Without optimization it turns 160 degrees and drives forwards, which is slow. With optimization, the default, it turns 20 degrees the other way and drives backwards, and never turns more than 90 degrees."><figcaption><p>Turning 160° without optimization, 20° with it.</p></figcaption></figure>

```java
config.withOptimization(true);
```

[`SwerveDriveConfig.withModuleStateOptimization`](https://yams.yassrobotics.com/documentation/reference/mechanism-configs/swerve-drive-config.md#withmodulestateoptimization) overrides it on every module at once. [The Flip-Decision Feedback Loop](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md#the-flip-decision-feedback-loop) explains how a wheel avoids flipping back and forth.

## `withCosineCompensation`

Slows a wheel's drive while it is still turning to its angle. It is off by default.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-09de28558ece0813ac402cb1a5bbad633c954206%2Fmech-module-cosine.png?alt=media" alt="A wheel pointing 40 degrees away from where it should, where full speed would push the robot sideways. A graph of drive speed against angle error: without the option the speed is always full, with it the speed is multiplied by the cosine of the error, 77 percent at 40 degrees."><figcaption><p>Drive speed scaled by the cosine of the angle error.</p></figcaption></figure>

```java
config.withCosineCompensation(true);
```

## `withVelocityDeadband`

A wheel asked to go slower than this stops and holds its angle. The default is 0.1 m/s.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-6bc6f47053991731658bd435d087157f1ad8b773%2Fswerve-velocity-deadband.gif?alt=media" alt="Animation of a wheel whose requested speed sweeps up and down. Inside the velocity deadband the commanded speed is zero and the wheel is stopped"><figcaption><p>Requested and commanded wheel speed with the default deadband.</p></figcaption></figure>

```java
config.withVelocityDeadband(MetersPerSecond.of(0.05));
```

[Velocity Deadband](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md#velocity-deadband) explains why it exists and what it costs when the robot is meant to creep.

## `withCouplingRatio`

Compensates for steering also rolling the wheel.

<figure><img src="https://1354553280-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZM0CFmYiQzcrY4zDcTtZ%2Fuploads%2Fgit-blob-18d7b396c2a0a0ce85a960949036611a4c94a71f%2Fmech-module-coupling.png?alt=media" alt="Inside a swerve module, turning the azimuth also turns the wheel through the drive gearing, even with the drive motor stopped. withCouplingRatio takes how many drive motor rotations one azimuth rotation causes. With it set, YAMS adds that motion to the drive setpoint and removes it from the measured wheel speed and distance."><figcaption><p>Turning the azimuth turns the wheel through the drive gearing.</p></figcaption></figure>

```java
config.withCouplingRatio(3.5); // drive motor rotations per azimuth rotation
```

It depends on the module's gearing, and some modules do not need it. See [Steer to Drive Coupling](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md#steer-to-drive-coupling).

## `withTelemetry`

Names the module and chooses what it publishes. The name appears under the drive's `modules` table.

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

[Module Telemetry](https://yams.yassrobotics.com/documentation/understanding/swerve-drive/swerve-module.md#module-telemetry) lists the keys, and [tuning the swerve modules](https://yams.yassrobotics.com/documentation/how-to-tuning/live-tuning/swerve-drive.md#tune-the-swerve-modules) uses them.

## Code reference

* `SwerveModuleConfig`
* `SwerveModule`


---

# 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-module-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.
