> 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/api-reference/c++-reference/swerve/swerve-module.md).

# SwerveModule

**Namespace:** `yams::mechanisms::swerve`\
**Header:** `#include <yams/mechanisms/swerve/SwerveModule.hpp>`\
**Java equivalent:** [Java SwerveModule](https://yams.yassrobotics.com/api-reference/java-reference/swerve/swerve-module.md)\
**Doxygen:** [`SwerveModule`](https://doxygen.yassrobotics.com/yams/classyams_1_1mechanisms_1_1swerve_1_1SwerveModule.html)

`SwerveModule` represents one swerve module: a drive motor and an azimuth motor. You construct one per corner from a `SwerveModuleConfig` (which must outlive the module) and pass pointers to them into `SwerveDriveConfig::WithModules(...)`; `SwerveDrive<N>` then drives, simulates and publishes them.

***

## Constructor

```cpp
explicit SwerveModule(config::SwerveModuleConfig* config)
```

***

## State

| Method                       | Signature                                                 | Description                                                                              |
| ---------------------------- | --------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `GetState`                   | `wpi::math::SwerveModuleVelocity GetState() const`        | Current drive velocity and azimuth angle.                                                |
| `GetPosition`                | `wpi::math::SwerveModulePosition GetPosition() const`     | Integrated drive distance and azimuth angle.                                             |
| `GetName`                    | `std::string GetName() const`                             | Module name from config.                                                                 |
| `GetConfig`                  | `const config::SwerveModuleConfig& GetConfig() const`     | Module configuration.                                                                    |
| `GetRawAbsoluteEncoderAngle` | `wpi::units::degree_t GetRawAbsoluteEncoderAngle() const` | Absolute encoder angle with no offset applied: the raw reading captured at construction. |

***

## Control

| Method                 | Signature                                                                      | Description                                                                                                                                                                                      |
| ---------------------- | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `SetSwerveModuleState` | `void SetSwerveModuleState(wpi::math::SwerveModuleVelocity state)`             | Commands drive speed and azimuth angle.                                                                                                                                                          |
| `SetAzimuthAngle`      | `wpi::math::SwerveModuleVelocity SetAzimuthAngle(wpi::math::Rotation2d angle)` | Turns the wheel to `angle` with zero drive speed, skipping the velocity deadband, and returns the commanded state. Used by `LockPose()` and `SetAzimuthAngles(...)`.                             |
| `GetCouplingVelocity`  | `wpi::units::meters_per_second_t GetCouplingVelocity() const`                  | The wheel velocity the drive encoder reads because the azimuth is turning, which a drive held still must be commanded to.                                                                        |
| `SetupTelemetry`       | `void SetupTelemetry(const std::string& mechName)`                             | Wires up telemetry under `Mechanisms/<mechName>/modules/<moduleName>`. Called by `SwerveDrive<N>`; see below.                                                                                    |
| `SeedAzimuthEncoder`   | `void SeedAzimuthEncoder()`                                                    | Reads the absolute encoder and seeds the azimuth motor's relative encoder (skipped in simulation and when the azimuth uses an external feedback encoder). The constructor already calls it once. |

***

## Motor access

| Method                      | Signature                                                                   | Description                   |
| --------------------------- | --------------------------------------------------------------------------- | ----------------------------- |
| `GetDriveMotorController`   | `motorcontrollers::SmartMotorController* GetDriveMotorController() const`   | Pointer to the drive motor.   |
| `GetAzimuthMotorController` | `motorcontrollers::SmartMotorController* GetAzimuthMotorController() const` | Pointer to the azimuth motor. |

***

## Lifecycle

| Method            | Signature                | Description                              |
| ----------------- | ------------------------ | ---------------------------------------- |
| `UpdateTelemetry` | `void UpdateTelemetry()` | Publishes module state to NetworkTables. |
| `SimIterate`      | `void SimIterate()`      | Advances module simulation.              |

***

## Telemetry & DataLog

`SetupTelemetry(const std::string& mechName)` wires up this module's telemetry under `Mechanisms/<mechName>/modules/<moduleName>`; `SwerveDrive<N>` calls it automatically for every module during its own construction, so you don't normally call it yourself. `UpdateTelemetry()` then publishes this module's `SwerveModuleVelocity` and absolute encoder angle to NetworkTables at the verbosity from `SwerveModuleConfig::GetTelemetryVerbosity()`, or from an explicit `telemetry::SwerveModuleTelemetryConfig` passed via `SwerveModuleConfig::WithTelemetry(name, SwerveModuleTelemetryConfig)`; see [SwerveModuleConfig](https://yams.yassrobotics.com/api-reference/c++-reference/swerve/swerve-module-config.md#datalog-telemetry) for how to additionally record fields to a WPILib DataLog. For field-level control (or a DataLog name) on the drive/azimuth motors themselves, configure each motor's own `SmartMotorControllerConfig::WithTelemetry(name, SmartMotorControllerTelemetryConfig)`.

{% hint style="warning" %}
`SwerveModuleConfig` has no `WithDataLogName(const std::string&)` method: `SetupTelemetry(...)` only consults the `SwerveModuleTelemetryConfig`'s own, separate DataLog name. Use `SwerveModuleConfig::WithTelemetry(name, telemetry::SwerveModuleTelemetryConfig{}.WithDataLogName(...))` instead; see [SwerveModuleConfig](https://yams.yassrobotics.com/api-reference/c++-reference/swerve/swerve-module-config.md#datalog-telemetry).
{% endhint %}

***

## See also

* [Java SwerveModule](https://yams.yassrobotics.com/api-reference/java-reference/swerve/swerve-module.md)
* [SwerveModuleConfig](https://yams.yassrobotics.com/api-reference/c++-reference/swerve/swerve-module-config.md)
* [SwerveDrive\<N>](https://yams.yassrobotics.com/api-reference/c++-reference/swerve/swerve-drive.md)


---

# 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/api-reference/c++-reference/swerve/swerve-module.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.
