Class LQRConfig
java.lang.Object
yams.core.math.LQRConfig
LQR Configuration for
LQRController
The LQR cost matrices determine how the regulator balances state error against control effort:
- Q (state cost, via qelms) penalizes deviation from the desired state (position, velocity). Smaller values tolerate larger state errors; larger values drive the controller to correct errors more aggressively.
- R (control effort cost, via relms) penalizes the voltage applied to the motor. Smaller values allow higher voltages (more aggressive); larger values keep the output gentler. The default of 12 V matches the approximate maximum battery voltage.
Example Arm LQR configuration
import static org.wpilib.units.Units.*;
LQRConfig config = new LQRConfig(
DCMotor.getNEO(1),
MechanismGearing.ofReduction(60.0),
KilogramSquareMeters.of(0.25))
.withArm(
Radians.of(0.01), // qelms position: 0.01 rad error tolerance
RadiansPerSecond.of(0.5), // qelms velocity: 0.5 rad/s error tolerance
Radians.of(0.05), // model position std dev
RadiansPerSecond.of(0.5), // model velocity std dev
Radians.of(0.01)) // encoder position std dev
.withControlEffort(Volts.of(12))
.withMaxVoltage(Volts.of(12))
.withAggressiveness(10);
LQRController armController = new LQRController(config);
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionLQRConfig(org.wpilib.math.system.DCMotor motor, MechanismGearing gearing, org.wpilib.units.measure.MomentOfInertia moi) Create a new LQR Configuration. -
Method Summary
Modifier and TypeMethodDescriptionorg.wpilib.math.estimator.KalmanFilter<?, ?, ?> getKalmanFilter(org.wpilib.math.system.LinearSystem<?, ?, ?> plant) Get theKalmanFilterfor the LQR.org.wpilib.math.system.LinearSystemLoop<?, ?, ?> getLoop()Get theLinearSystemLoopwith the currently configured LQR.org.wpilib.math.system.LinearSystemLoop<?, ?, ?> getLoop(org.wpilib.math.system.LinearSystem<?, ?, ?> plant, org.wpilib.math.controller.LinearQuadraticRegulator<?, ?, ?> controller, org.wpilib.math.estimator.KalmanFilter<?, ?, ?> observer) Get theLinearSystemLoop.org.wpilib.units.measure.TimeGet the loop time for the LQR.org.wpilib.math.controller.LinearQuadraticRegulator<?, ?, ?> getRegulator(org.wpilib.math.system.LinearSystem<?, ?, ?> plant) Get theLinearQuadraticRegulator.org.wpilib.math.system.LinearSystem<?, ?, ?> Get theLinearSystemfor the LQR.getType()Get theLQRConfig.LQRTypeof the LQR.withAggressiveness(double aggressiveness) Aggressiveness of the LQR, how fast it will attempt to achieve the desired state.withArm(org.wpilib.units.measure.Angle qelmsPosition, org.wpilib.units.measure.AngularVelocity qelmsVelocity, org.wpilib.units.measure.Angle modelPositionTrust, org.wpilib.units.measure.AngularVelocity modelVelocityTrust, org.wpilib.units.measure.Angle encoderPositionTrust) Construct an Arm LQR Configuration.withControlEffort(org.wpilib.units.measure.Voltage effort) Set the control effort for the LQR.withElevator(org.wpilib.units.measure.Distance qelmsPosition, org.wpilib.units.measure.LinearVelocity qelmsVelocity, org.wpilib.units.measure.Distance modelPositionTrust, org.wpilib.units.measure.LinearVelocity modelVelocityTrust, org.wpilib.units.measure.Distance encoderPositionTrust, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance drumRadius) Construct an Elevator LQR Configuration.withFlyWheel(org.wpilib.units.measure.AngularVelocity qelms, org.wpilib.units.measure.AngularVelocity modelTrust, org.wpilib.units.measure.AngularVelocity encoderTrust) Construct a Flywheel LQR Configuration.withMaxVoltage(org.wpilib.units.measure.Voltage voltage) Set the maximum voltage for theLinearSystemLoop.withMeasurementDelay(org.wpilib.units.measure.Time delay) Set the measurement delay of the LQR.withRelms(org.wpilib.units.measure.Voltage relms) Set the control effort for the LQR.
-
Constructor Details
-
LQRConfig
public LQRConfig(org.wpilib.math.system.DCMotor motor, MechanismGearing gearing, org.wpilib.units.measure.MomentOfInertia moi) Create a new LQR Configuration.- Parameters:
motor-DCMotorfor theLinearQuadraticRegulator.gearing-MechanismGearingfor theLinearQuadraticRegulator.moi-MomentOfInertiafor theLinearQuadraticRegulator.
-
-
Method Details
-
getType
Get theLQRConfig.LQRTypeof the LQR.- Returns:
LQRConfig.LQRTypeof the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-
getPeriod
public org.wpilib.units.measure.Time getPeriod()Get the loop time for the LQR.- Returns:
Timefor the loop time.
-
withRelms
Set the control effort for the LQR.- Parameters:
relms- Control effort (voltage) tolerance. Decrease this to more heavily penalize control effort, or make the controller less aggressive. 12 is a good starting point because that is the (approximate) maximum voltage of a battery.- Returns:
LQRConfigfor chaining.
-
withControlEffort
Set the control effort for the LQR.- Parameters:
effort- Control effort (voltage) tolerance. Decrease this to more heavily penalize control effort, or make the controller less aggressive. 12 is a good starting point because that is the (approximate) maximum voltage of a battery.- Returns:
LQRConfigfor chaining.
-
withMaxVoltage
-
withMeasurementDelay
-
withAggressiveness
-
withFlyWheel
public LQRConfig withFlyWheel(org.wpilib.units.measure.AngularVelocity qelms, org.wpilib.units.measure.AngularVelocity modelTrust, org.wpilib.units.measure.AngularVelocity encoderTrust) Construct a Flywheel LQR Configuration.- Parameters:
qelms- Velocity error tolerance. Decrease this to more heavily penalize state excursion, or make the controller behave more starting point because that is the (approximate) maximum voltage of a battery.modelTrust- Standard deviation of the model, represented inAngularVelocity.encoderTrust- Standard deviation of the encoder, represented inAngularVelocity.- Returns:
LQRConfigfor chaining.
-
withElevator
public LQRConfig withElevator(org.wpilib.units.measure.Distance qelmsPosition, org.wpilib.units.measure.LinearVelocity qelmsVelocity, org.wpilib.units.measure.Distance modelPositionTrust, org.wpilib.units.measure.LinearVelocity modelVelocityTrust, org.wpilib.units.measure.Distance encoderPositionTrust, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance drumRadius) Construct an Elevator LQR Configuration.- Parameters:
qelmsPosition- Position error tolerance, in meters. Decrease this to more heavily penalize state excursion, or make the controller behave more aggressively. This can be tuned to balance the position and velocity errors.qelmsVelocity- Velocity error tolerance, in meters per second. Decrease this to more heavily penalize state excursion, or make the controller behave more aggressively. This can be tuned to balance the position and velocity errors.modelPositionTrust- Standard deviation of the model position, represented inDistance.modelVelocityTrust- Standard deviation of the model velocity, represented inLinearVelocity.encoderPositionTrust- Standard deviation of the encoder position, represented inDistance.mass- Mass of the elevator, represented inMass.drumRadius- Radius of the elevator drum, represented inDistance.- Returns:
LQRConfigfor chaining.
-
withArm
public LQRConfig withArm(org.wpilib.units.measure.Angle qelmsPosition, org.wpilib.units.measure.AngularVelocity qelmsVelocity, org.wpilib.units.measure.Angle modelPositionTrust, org.wpilib.units.measure.AngularVelocity modelVelocityTrust, org.wpilib.units.measure.Angle encoderPositionTrust) Construct an Arm LQR Configuration.- Parameters:
qelmsPosition- Position error tolerance, in rotations. Decrease this to more heavily penalize state excursion, or make the controller behave more aggressively. This can be tuned to balance the position and velocity errors.qelmsVelocity- Velocity error tolerance, in rotations per second. Decrease this to more heavily penalize state excursion, or make the controller behave more aggressively. This can be tuned to balance the position and velocity errors.modelPositionTrust- Standard deviation of the model position, represented inAngle.modelVelocityTrust- Standard deviation of the model velocity, represented inAngularVelocity.encoderPositionTrust- Standard deviation of the encoder position, represented inAngle.- Returns:
LQRConfigfor chaining.
-
getSystem
public org.wpilib.math.system.LinearSystem<?,?, getSystem()?> Get theLinearSystemfor the LQR. withModels- Returns:
LinearSystemfor the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-
getKalmanFilter
public org.wpilib.math.estimator.KalmanFilter<?,?, getKalmanFilter?> (org.wpilib.math.system.LinearSystem<?, ?, ?> plant) Get theKalmanFilterfor the LQR.- Parameters:
plant-LinearSystemfor the LQR, fetched fromgetSystem().- Returns:
KalmanFilterfor the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-
getRegulator
public org.wpilib.math.controller.LinearQuadraticRegulator<?,?, getRegulator?> (org.wpilib.math.system.LinearSystem<?, ?, ?> plant) Get theLinearQuadraticRegulator.- Parameters:
plant-LinearSystemfor the LQR, fetched fromgetSystem().- Returns:
LinearQuadraticRegulatorfor the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-
getLoop
public org.wpilib.math.system.LinearSystemLoop<?,?, getLoop?> (org.wpilib.math.system.LinearSystem<?, ?, ?> plant, org.wpilib.math.controller.LinearQuadraticRegulator<?, ?, ?> controller, org.wpilib.math.estimator.KalmanFilter<?, ?, ?> observer) Get theLinearSystemLoop.- Parameters:
plant-LinearSystemfor the LQR, fetched fromgetSystem()controller-LinearQuadraticRegulatorfor the LQR, fetched fromgetRegulator(LinearSystem)observer-KalmanFilterfor the LQR, fetched fromgetKalmanFilter(LinearSystem)- Returns:
LinearSystemLoopfor the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-
getLoop
public org.wpilib.math.system.LinearSystemLoop<?,?, getLoop()?> Get theLinearSystemLoopwith the currently configured LQR.- Returns:
LinearSystemLoopfor the LQR.- Throws:
NoSuchElementException- if no mechanism type has been configured viawithFlyWheel(AngularVelocity, AngularVelocity, AngularVelocity),withArm(Angle, AngularVelocity, Angle, AngularVelocity, Angle), orwithElevator(Distance, LinearVelocity, Distance, LinearVelocity, Distance, Mass, Distance).
-