> 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/math/lqr-controller.md).

# LQRController

**Namespace:** `yams::math`\
**Header:** `#include <yams/math/LQRController.hpp>`\
**Java equivalent:** [Java LQRController](https://yams.yassrobotics.com/api-reference/java-reference/math/lqr-controller.md)\
**Doxygen:** [`LQRController`](https://doxygen.yassrobotics.com/yams/classyams_1_1math_1_1LQRController.html)

`LQRController` wraps a `LinearSystemLoop` and exposes a unified `Calculate` interface for arm, elevator, and flywheel mechanisms.

***

## Constructor

```cpp
explicit LQRController(LQRConfig config)
```

***

## Methods

### Configuration

| Method         | Signature                                           | Description                                                                                                                                                                                                                                                 |
| -------------- | --------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `UpdateConfig` | `void UpdateConfig(LQRConfig config)`               | Replace the internal system loop at runtime                                                                                                                                                                                                                 |
| `Retune`       | `void Retune(LQRConfig config)`                     | Rebuild 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 controller's thread runs `Calculate`. Throws `std::invalid_argument` for a config of a different `LQRType` |
| `GetType`      | `LQRConfig::LQRType GetType() const`                | Returns the configured mechanism type                                                                                                                                                                                                                       |
| `GetConfig`    | `const std::optional<LQRConfig>& GetConfig() const` | Returns the config if constructed from `LQRConfig`, otherwise empty                                                                                                                                                                                         |

### Reset

| Method  | Signature                                                                            | Description                                         |
| ------- | ------------------------------------------------------------------------------------ | --------------------------------------------------- |
| `Reset` | `void Reset(wpi::units::radian_t angle, wpi::units::radians_per_second_t velocity)`  | Reset the Kalman observer state for arm or flywheel |
| `Reset` | `void Reset(wpi::units::meter_t distance, wpi::units::meters_per_second_t velocity)` | Reset the Kalman observer state for elevator        |

### Calculate

| Overload           | Signature                                                                                                                               | Description                                                  |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| Arm                | `wpi::units::volt_t Calculate(wpi::units::radian_t measured, wpi::units::radian_t position, wpi::units::radians_per_second_t velocity)` | Compute output voltage for arm control                       |
| Elevator           | `wpi::units::volt_t Calculate(wpi::units::meter_t measured, wpi::units::meter_t position, wpi::units::meters_per_second_t velocity)`    | Compute output voltage for elevator control                  |
| Flywheel (angular) | `wpi::units::volt_t Calculate(wpi::units::radians_per_second_t measured, wpi::units::radians_per_second_t velocity)`                    | Compute output voltage for flywheel angular velocity control |
| Flywheel (linear)  | `wpi::units::volt_t Calculate(wpi::units::meters_per_second_t measured, wpi::units::meters_per_second_t velocity)`                      | Compute output voltage for flywheel surface speed control    |

***

## Example

```cpp
yams::math::LQRConfig cfg;
cfg.WithType(yams::math::LQRConfig::LQRType::ARM)
   .WithArmSystem(wpi::math::DCMotor::Falcon500(1), 0.5, 50.0)
   .WithQElems({0.01745, 0.08727})
   .WithRElems({12.0})
   .WithStateStdDevs({0.015, 0.17})
   .WithMeasurementStdDevs({0.01})
   .WithMaxVoltage(12_V);

yams::math::LQRController controller{cfg};

// In Periodic():
wpi::units::radian_t measured = GetArmAngle();
wpi::units::volt_t output = controller.Calculate(
    measured,
    targetAngle,
    targetVelocity
);
motor.SetVoltage(output);
```

{% hint style="info" %}
Call `Reset` whenever the mechanism is re-enabled or its state is known (e.g., at initialization) to prevent the Kalman filter from tracking stale state.
{% endhint %}

***

## Live tuning

A [SmartMotorController](https://yams.yassrobotics.com/api-reference/c++-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/c++-reference/motor-controllers/smart-motor-controller-telemetry-config.md#lqr-fields). While the `Live Tuning` command runs, a changed value is set on a copy of `GetConfig()` and passed to `Retune`. `SmartMotorController::GetLQRController()` returns the controller, or `nullptr` without one.


---

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