YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
LQRConfig.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Yet Another Software Suite
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
6#include <optional>
7#include <variant>
8#include <vector>
9#include <wpi/math/controller/LinearQuadraticRegulator.hpp>
10#include <wpi/math/estimator/KalmanFilter.hpp>
11#include <wpi/math/system/DCMotor.hpp>
12#include <wpi/math/system/LinearSystem.hpp>
13#include <wpi/math/system/LinearSystemLoop.hpp>
14#include <wpi/math/system/Models.hpp>
15#include <wpi/units/time.hpp>
16#include <wpi/units/voltage.hpp>
17
18namespace yams::math {
19
27class LQRConfig {
28 public:
30 enum class LQRType {
31 FLYWHEEL,
32 ARM,
33 ELEVATOR,
34 };
35
37 using Loop1 = wpi::math::LinearSystemLoop<1, 1, 1>;
39 using Loop2 = wpi::math::LinearSystemLoop<2, 1, 1>;
40
48
57 LQRConfig& WithFlywheelSystem(const wpi::math::DCMotor& motor, double momentOfInertia,
58 double gearing);
59
68 LQRConfig& WithArmSystem(const wpi::math::DCMotor& motor, double momentOfInertia, double gearing);
69
79 LQRConfig& WithElevatorSystem(const wpi::math::DCMotor& motor, double mass, double drumRadius,
80 double gearing);
81
90 LQRConfig& WithQElems(std::initializer_list<double> qElems);
91
101 LQRConfig& WithRElems(std::initializer_list<double> rElems);
102
109 LQRConfig& WithStateStdDevs(std::initializer_list<double> stateStdDevs);
110
117 LQRConfig& WithMeasurementStdDevs(std::initializer_list<double> measStdDevs);
118
125 LQRConfig& WithMaxVoltage(wpi::units::volt_t maxVoltage);
126
133 LQRConfig& WithPeriod(wpi::units::second_t period);
134
141
147 wpi::units::second_t GetPeriod() const;
148
154 wpi::units::volt_t GetMaxVoltage() const;
155
161 std::variant<wpi::math::LinearSystem<1, 1, 1>, wpi::math::LinearSystem<2, 1, 1>> GetSystem()
162 const;
163
169 std::variant<Loop1, Loop2> GetLoop() const;
170
171 private:
172 friend class LQRController;
173
174 std::optional<LQRType> m_type;
175 std::optional<wpi::math::LinearSystem<1, 1, 1>> m_flywheelPlant;
176 std::optional<wpi::math::LinearSystem<2, 1, 1>> m_armElevatorPlant;
177
178 std::vector<double> m_qElems;
179 std::vector<double> m_rElems;
180 std::vector<double> m_stateStdDevs;
181 std::vector<double> m_measStdDevs;
182
183 wpi::units::volt_t m_maxVoltage{wpi::units::volt_t{12.0}};
184 wpi::units::second_t m_period{wpi::units::second_t{0.020}};
185};
186
187} // namespace yams::math
Definition LQRConfig.hpp:27
LQRConfig & WithStateStdDevs(std::initializer_list< double > stateStdDevs)
LQRConfig & WithElevatorSystem(const wpi::math::DCMotor &motor, double mass, double drumRadius, double gearing)
LQRConfig & WithMaxVoltage(wpi::units::volt_t maxVoltage)
LQRConfig & WithQElems(std::initializer_list< double > qElems)
LQRConfig & WithArmSystem(const wpi::math::DCMotor &motor, double momentOfInertia, double gearing)
wpi::math::LinearSystemLoop< 2, 1, 1 > Loop2
Definition LQRConfig.hpp:39
LQRConfig & WithType(LQRType type)
wpi::units::second_t GetPeriod() const
LQRType
Definition LQRConfig.hpp:30
@ ELEVATOR
Elevator (2-state position+velocity model).
@ FLYWHEEL
Flywheel (1-state velocity model).
@ ARM
Single-jointed arm (2-state position+velocity model).
LQRConfig & WithRElems(std::initializer_list< double > rElems)
wpi::units::volt_t GetMaxVoltage() const
std::variant< wpi::math::LinearSystem< 1, 1, 1 >, wpi::math::LinearSystem< 2, 1, 1 > > GetSystem() const
LQRConfig & WithMeasurementStdDevs(std::initializer_list< double > measStdDevs)
std::variant< Loop1, Loop2 > GetLoop() const
LQRConfig & WithPeriod(wpi::units::second_t period)
LQRType GetType() const
wpi::math::LinearSystemLoop< 1, 1, 1 > Loop1
Definition LQRConfig.hpp:37
LQRConfig & WithFlywheelSystem(const wpi::math::DCMotor &motor, double momentOfInertia, double gearing)
Definition LQRController.hpp:35
Definition DerivativeTimeFilter.hpp:10