Class LQRController
java.lang.Object
yams.core.math.LQRController
Linear Quadratic Regulator (Regulator is a type of controller)
An LQR computes optimal control inputs (voltages) that minimize a quadratic cost function
combining state error and control effort. It requires a linearized plant model, which is built
automatically from the physical parameters supplied via LQRConfig. Three mechanism types
are supported: ARM, ELEVATOR, and FLYWHEEL.
On each robot loop iteration, call the appropriate calculate() overload with the
current sensor measurement and the desired setpoint. The controller internally runs a
LinearSystemLoop that fuses a
LinearQuadraticRegulator with a
KalmanFilter observer.
Example construct from LQRConfig and calculate arm voltage
import static org.wpilib.units.Units.*;
// Build configuration (see LQRConfig for full parameter details)
LQRConfig config = new LQRConfig(
DCMotor.getNEO(1),
MechanismGearing.ofReduction(60.0),
KilogramSquareMeters.of(0.25))
.withArm(
Radians.of(0.01),
RadiansPerSecond.of(0.5),
Radians.of(0.05),
RadiansPerSecond.of(0.5),
Radians.of(0.01));
LQRController controller = new LQRController(config);
// In robotInit / subsystem initialization, reset to the current sensor state:
controller.reset(encoder.getAngle(), encoder.getAngularVelocity());
// In periodic, calculate and apply voltage:
Voltage output = controller.calculate(
encoder.getAngle(), // measured position
Radians.of(Math.PI / 4), // position setpoint
RadiansPerSecond.of(0)); // velocity setpoint
motor.setVoltage(output.in(Volts));
-
Constructor Summary
ConstructorsConstructorDescriptionLQRController(LQRConfig config) Create a LQR Controller.LQRController(LQRConfig.LQRType type, org.wpilib.math.system.LinearSystemLoop<?, ?, ?> loop, org.wpilib.units.measure.Time period) Create a LQR Controller. -
Method Summary
Modifier and TypeMethodDescriptionorg.wpilib.units.measure.Voltagecalculate(org.wpilib.units.measure.Angle measured, org.wpilib.units.measure.Angle position, org.wpilib.units.measure.AngularVelocity velocity) Calculate the voltage for an Arm LQRorg.wpilib.units.measure.Voltagecalculate(org.wpilib.units.measure.AngularVelocity measured, org.wpilib.units.measure.AngularVelocity velocity) Calculate the voltage for a Flywheel LQRorg.wpilib.units.measure.Voltagecalculate(org.wpilib.units.measure.Distance measured, org.wpilib.units.measure.Distance position, org.wpilib.units.measure.LinearVelocity velocity) Calculate the voltage for a Elevator LQRorg.wpilib.units.measure.Voltagecalculate(org.wpilib.units.measure.LinearVelocity measured, org.wpilib.units.measure.LinearVelocity velocity) Calculate the voltage for a Flywheel LQRGet theLQRConfigfor the LQR.getType()Get the type of LQR.voidreset(org.wpilib.units.measure.Angle angle, org.wpilib.units.measure.AngularVelocity velocity) Reset the Arm or Flywheel LQR.voidreset(org.wpilib.units.measure.Distance distance, org.wpilib.units.measure.LinearVelocity velocity) Reset the Elevator LQR.voidupdateConfig(LQRConfig config) Update LQR based off config.
-
Constructor Details
-
LQRController
public LQRController(LQRConfig.LQRType type, org.wpilib.math.system.LinearSystemLoop<?, ?, ?> loop, org.wpilib.units.measure.Time period) Create a LQR Controller.- Parameters:
type- LQR Type.loop-LinearSystemLoopwhich can be derived fromLQRConfigperiod- Loop time.
-
LQRController
Create a LQR Controller.- Parameters:
config-LQRConfigto create the controller from.- Throws:
NoSuchElementException- ifconfighas no mechanism type configured (none ofwithFlyWheel,withArm, orwithElevatorwas called).
-
-
Method Details
-
updateConfig
Update LQR based off config.- Parameters:
config-LQRConfig- Throws:
NoSuchElementException- ifconfighas no mechanism type configured (none ofwithFlyWheel,withArm, orwithElevatorwas called).
-
reset
public void reset(org.wpilib.units.measure.Angle angle, org.wpilib.units.measure.AngularVelocity velocity) Reset the Arm or Flywheel LQR.- Parameters:
angle- Current angle.velocity- Current velocity.
-
reset
public void reset(org.wpilib.units.measure.Distance distance, org.wpilib.units.measure.LinearVelocity velocity) Reset the Elevator LQR.- Parameters:
distance- Current distance.velocity- Current velocity.
-
calculate
public org.wpilib.units.measure.Voltage calculate(org.wpilib.units.measure.Angle measured, org.wpilib.units.measure.Angle position, org.wpilib.units.measure.AngularVelocity velocity) Calculate the voltage for an Arm LQR- Parameters:
measured- Measured angle from the arm.position-Anglesetpoint to go to.velocity-AngularVelocitysetpoint to go to.- Returns:
Voltageto apply to the arm.
-
calculate
public org.wpilib.units.measure.Voltage calculate(org.wpilib.units.measure.Distance measured, org.wpilib.units.measure.Distance position, org.wpilib.units.measure.LinearVelocity velocity) Calculate the voltage for a Elevator LQR- Parameters:
measured- Measured distance of the elevator.position- Setpoint distance of the elevator from the motion profile.velocity- Setpoint velocity of the elevator from the motion profile.- Returns:
Voltageto apply to the elevator.
-
calculate
public org.wpilib.units.measure.Voltage calculate(org.wpilib.units.measure.AngularVelocity measured, org.wpilib.units.measure.AngularVelocity velocity) Calculate the voltage for a Flywheel LQR- Parameters:
measured- Measured velocity of the flywheel.velocity- Setpoint velocity of the flywheel.- Returns:
Voltageto apply to the flywheel.
-
calculate
public org.wpilib.units.measure.Voltage calculate(org.wpilib.units.measure.LinearVelocity measured, org.wpilib.units.measure.LinearVelocity velocity) Calculate the voltage for a Flywheel LQR- Parameters:
measured- Measured velocity of the flywheel.velocity- Setpoint velocity of the flywheel.- Returns:
Voltageto apply to the flywheel.
-
getType
-
getConfig
-