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

# LQRController

**Package:** `yams.core.math`\
**Javadoc:** [`LQRController`](https://javadocs.yassrobotics.com/yams/yams/core/math/LQRController.html)

`LQRController` wraps a `LinearSystemLoop` and provides type-safe `calculate()` overloads for different mechanism types.

## Constructors

```java
LQRController(LQRConfig config)   // Preferred
LQRController(LQRType type, LinearSystemLoop<?, ?, ?> loop, Time period)
```

## Methods

| Method                                              | Returns | Description                                                                                                                                                                                                                           |
| --------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reset(Angle angle, AngularVelocity velocity)`      | `void`  | Resets the observer for arm or flywheel systems. Call on enable.                                                                                                                                                                      |
| `reset(Distance distance, LinearVelocity velocity)` | `void`  | Resets the observer for elevator systems.                                                                                                                                                                                             |
| `updateConfig(LQRConfig config)`                    | `void`  | Replaces the underlying system loop at runtime.                                                                                                                                                                                       |
| `retune(LQRConfig config)`                          | `void`  | Rebuilds the loop from a config whose values changed, keeping the state estimate, so the output does not jump. Safe to call while the closed loop runs. Throws `IllegalArgumentException` if the config is for a different `LQRType`. |

## Calculate overloads

| Method                                                                     | For Mechanism                   | Returns   |
| -------------------------------------------------------------------------- | ------------------------------- | --------- |
| `calculate(Angle measured, Angle position, AngularVelocity velocity)`      | Arm                             | `Voltage` |
| `calculate(Distance measured, Distance position, LinearVelocity velocity)` | Elevator                        | `Voltage` |
| `calculate(AngularVelocity measured, AngularVelocity velocity)`            | Flywheel                        | `Voltage` |
| `calculate(LinearVelocity measured, LinearVelocity velocity)`              | Flywheel (linear surface speed) | `Voltage` |

## Getters

| Method        | Returns               | Description                                                                                     |
| ------------- | --------------------- | ----------------------------------------------------------------------------------------------- |
| `getType()`   | `LQRType`             | The mechanism type this controller was configured for.                                          |
| `getConfig()` | `Optional<LQRConfig>` | The `LQRConfig` used to construct this controller, or empty if constructed from raw components. |

## Usage with SmartMotorControllerConfig

Pass the controller to `SmartMotorControllerConfig.withClosedLoopController(LQRController)` (this clears any configured PID). `withSimClosedLoopController(LQRController)` sets one for simulation only.

```java
LQRConfig lqrCfg = new LQRConfig(DCMotor.getNEO(1), gearing, moi)
    .withArm(Degrees.of(5), DegreesPerSecond.of(10),
             Degrees.of(1), DegreesPerSecond.of(5), Degrees.of(0.5))
    .withControlEffort(Volts.of(12));

SmartMotorControllerConfig motorConfig = new SmartMotorControllerConfig(this)
    .withClosedLoopController(new LQRController(lqrCfg));
```

## Live tuning

A [SmartMotorController](https://yams.yassrobotics.com/api-reference/java-reference/motor-controllers/smart-motor-controller.md) with an LQR publishes its values at `TelemetryVerbosity.HIGH` in place of kP, kI and kD, see [LQR fields](https://yams.yassrobotics.com/api-reference/java-reference/motor-controllers/smart-motor-controller-telemetry-config.md#lqr-fields). While the `Live Tuning` command runs, a changed value is set on the controller's `LQRConfig` and passed to `retune`.

## See also

* [LQRConfig](https://yams.yassrobotics.com/api-reference/java-reference/math/lqr-config.md)
* [C++ LQRController](https://yams.yassrobotics.com/api-reference/c++-reference/math/lqr-controller.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/java-reference/math/lqr-controller.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.
