Class ExponentialProfilePIDController
PIDController or
ProfiledPIDController, but uses an ExponentialProfile
This controller combines an ExponentialProfile motion profile with a
PIDController to achieve smooth, continuous position control. On each iteration the
profile generates a feasible intermediate setpoint (position + velocity) that the PID then
tracks, preventing large step demands from commanding unreachable states. The exponential profile
shape respects the plant's natural first-order dynamics, making it well-suited for flywheels,
arms, and elevators.
Use the static factory helpers (createArmConstraints(Voltage, DCMotor, MomentOfInertia, MechanismGearing),
createElevatorConstraints(Voltage, DCMotor, Mass, Distance, MechanismGearing),
createFlywheelConstraints(Voltage, DCMotor, MomentOfInertia, MechanismGearing)) to derive physically accurate
ExponentialProfile.Constraints directly from motor and mechanism parameters.
Example arm position control
import static org.wpilib.units.Units.*;
// Derive constraints from the physical arm model
ExponentialProfile.Constraints armConstraints =
ExponentialProfilePIDController.createArmConstraints(
Volts.of(12),
DCMotor.getNEO(1),
KilogramSquareMeters.of(0.25),
MechanismGearing.ofReduction(60.0));
// kP=1.0, kI=0.0, kD=0.0 tune these for your mechanism
ExponentialProfilePIDController armPID =
new ExponentialProfilePIDController(1.0, 0.0, 0.0, armConstraints);
// In robotInit, seed the profile at the current sensor state:
armPID.reset(encoder.getPosition(), 0.0);
// In periodic, calculate output (positions in rotations):
double output = armPID.calculate(
encoder.getPosition(), // measured position (rotations)
targetPosition); // desired position (rotations)
motor.set(output);
-
Constructor Summary
ConstructorsConstructorDescriptionExponentialProfilePIDController(double kP, double kI, double kD, org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Constructor.ExponentialProfilePIDController(org.wpilib.math.controller.PIDController controller, org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if the error is within the tolerance of the setpoint.doublecalculate(double measurementPosition, double setpointPosition) Calculate the feedback, assuming no setpoint velocity.doublecalculate(double measurementPosition, double setpointVelocity, double setpointPosition) Calculate the feedback, assuming previous state velocity.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateArmConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance length, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an arm.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateArmConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.MomentOfInertia moi, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an arm.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.units.measure.AngularVelocity maxVelocity, org.wpilib.units.measure.AngularAcceleration maxAcceleration) Create a generic constraints object.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateElevatorConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance drumRadius, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an elevator.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateFlywheelConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance radius, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor a flywheel.static org.wpilib.math.trajectory.ExponentialProfile.ConstraintscreateFlywheelConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.MomentOfInertia moi, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor a flywheel.voidenableContinuousInput(double minimumInput, double maximumInput) Enables continuous input.Optional<org.wpilib.math.trajectory.ExponentialProfile.Constraints> Get the constraints.org.wpilib.units.measure.AngleGet the current angle.org.wpilib.math.trajectory.ExponentialProfile.StateGet the current state of theExponentialProfile.org.wpilib.units.measure.AngularVelocityGet the current velocity fromExponentialProfile.doublegetD()Get the Differential coefficient.doublegetI()Get the Integral coefficient.org.wpilib.units.measure.AngularAccelerationgetKa()Get the acceleration gain kAorg.wpilib.units.measure.AngularVelocitygetKv()Get the velocity gain as constant.org.wpilib.units.measure.AngleGet the next angle from theExponentialProfileOptional<org.wpilib.math.trajectory.ExponentialProfile.State> Get the next state of theExponentialProfile.org.wpilib.units.measure.AngularVelocityGet the next velocity fromExponentialProfiledoublegetP()Get the Proportional coefficient.doubleReturns the error tolerance of this controller.doubleGet the setpoint.voidreset(double position, double velocity) Reset the PID and profile with the given position and velocity as the measured position and velocity.voidreset(org.wpilib.math.trajectory.ExponentialProfile.State measurement) Reset the controller, set the next setpoint to empty.voidsetConstraints(org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Sets the constraints for theExponentialProfile.voidsetD(double kD) Sets the Differential coefficient of the PID controller gain.voidsetI(double kI) Sets the Integral coefficient of the PID controller gain.voidsetP(double kP) Sets the Proportional coefficient of the PID controller gain.voidsetTolerance(double tolerance) Sets the error which is considered tolerable for use with atSetpoint().
-
Constructor Details
-
ExponentialProfilePIDController
public ExponentialProfilePIDController(org.wpilib.math.controller.PIDController controller, org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Constructor.- Parameters:
controller- The wrapped PID controller.constraints- The wrapped profile constraints.
-
ExponentialProfilePIDController
public ExponentialProfilePIDController(double kP, double kI, double kD, org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Constructor.- Parameters:
kP- kP value for thePIDControllerkI- kI value for thePIDControllerkD- kD value for thePIDControllerconstraints-ExponentialProfile.Constraintsfor theExponentialProfile
-
-
Method Details
-
createElevatorConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createElevatorConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance drumRadius, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an elevator.- Parameters:
maxVolts- Maximum input voltage for profile generation.motor-DCMotorof the elevator.mass-Massof the elevator carriage.drumRadius-Distanceof the elevator drum radius.gearing-MechanismGearingof the elevator from the drum to the rotor.- Returns:
ExponentialProfile.Constraints
-
createArmConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createArmConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.MomentOfInertia moi, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an arm.- Parameters:
maxVolts- Maximum input voltage for profile generation.motor-DCMotorof the arm.moi-MomentOfInertiaof the arm.gearing-MechanismGearingof the arm from the rotor to the drum.gearing.getMechanismToRotorRatio()- Returns:
ExponentialProfile.Constraints
-
createArmConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createArmConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance length, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor an arm.- Parameters:
maxVolts- Maximum input voltage for profile generation.motor-DCMotorof the arm.mass-Massof the arm.length-Distanceof the arm length.gearing-MechanismGearingof the arm from the rotor to the drum.gearing.getMechanismToRotorRatio()- Returns:
ExponentialProfile.Constraints
-
createFlywheelConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createFlywheelConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.MomentOfInertia moi, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor a flywheel.- Parameters:
maxVolts- Maximum input voltage for profile generation.motor-DCMotorof the flywheel.moi-MomentOfInertiaof the flywheel.gearing-MechanismGearingof the flywheel from the rotor to the drum.- Returns:
ExponentialProfile.Constraints
-
createFlywheelConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createFlywheelConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.math.system.DCMotor motor, org.wpilib.units.measure.Mass mass, org.wpilib.units.measure.Distance radius, MechanismGearing gearing) Get theExponentialProfile.Constraintsfor a flywheel.- Parameters:
maxVolts- Maximum input voltage for profile generation.motor-DCMotorof the flywheel.mass-Massof the flywheel.radius-Distanceof the flywheel radius.gearing-MechanismGearingof the flywheel from the rotor to the drum.- Returns:
ExponentialProfile.Constraints
-
createConstraints
public static org.wpilib.math.trajectory.ExponentialProfile.Constraints createConstraints(org.wpilib.units.measure.Voltage maxVolts, org.wpilib.units.measure.AngularVelocity maxVelocity, org.wpilib.units.measure.AngularAcceleration maxAcceleration) Create a generic constraints object.- Parameters:
maxVolts- Maximum input voltage for profile generation.maxVelocity- Maximum velocity.maxAcceleration- Maximum acceleration.- Returns:
ExponentialProfile.Constraints
-
getKv
public org.wpilib.units.measure.AngularVelocity getKv()Get the velocity gain as constant.- Returns:
- kV with (-A/B)
- Throws:
IllegalStateException- if the profile constraints arenull(never set or set tonull).
-
getKa
public org.wpilib.units.measure.AngularAcceleration getKa()Get the acceleration gain kA- Returns:
- kA interpreted as (1.0/B)
- Throws:
IllegalStateException- if the profile constraints arenull(never set or set tonull).
-
reset
public void reset(org.wpilib.math.trajectory.ExponentialProfile.State measurement) Reset the controller, set the next setpoint to empty.- Parameters:
measurement- Measurement in Rotations, and Rotations per Second.
-
reset
public void reset(double position, double velocity) Reset the PID and profile with the given position and velocity as the measured position and velocity.- Parameters:
position- Measured positionvelocity- Measured velocity.
-
getConstraints
Get the constraints.- Returns:
ExponentialProfile.Constraints
-
setConstraints
public void setConstraints(org.wpilib.math.trajectory.ExponentialProfile.Constraints constraints) Sets the constraints for theExponentialProfile.- Parameters:
constraints- The constraints for theExponentialProfile.
-
setTolerance
public void setTolerance(double tolerance) Sets the error which is considered tolerable for use with atSetpoint().- Parameters:
tolerance- – Error which is tolerable.
-
atSetpoint
public boolean atSetpoint()Returns true if the error is within the tolerance of the setpoint. The error tolerance defaults to 0.05, and the error derivative tolerance defaults to ∞. This will return false until at least one input value has been computed.- Returns:
- Whether the error is within the acceptable bounds
-
getSetpoint
public double getSetpoint()Get the setpoint.- Returns:
- setpoint.
-
getCurrentState
public org.wpilib.math.trajectory.ExponentialProfile.State getCurrentState()Get the current state of theExponentialProfile.- Returns:
ExponentialProfile.Stategiven by theExponentialProfile.
-
getCurrentAngle
public org.wpilib.units.measure.Angle getCurrentAngle()Get the current angle.- Returns:
AnglefromExponentialProfile
-
getNextAngle
public org.wpilib.units.measure.Angle getNextAngle()Get the next angle from theExponentialProfile- Returns:
AnglefromExponentialProfile- Throws:
NoSuchElementException- if no next state exists becausecalculate(double, double, double)has not been called since construction or the lastreset(State).
-
getCurrentVelocitySetpoint
public org.wpilib.units.measure.AngularVelocity getCurrentVelocitySetpoint()Get the current velocity fromExponentialProfile.- Returns:
AngularVelocityfromExponentialProfile
-
getNextVelocitySetpoint
public org.wpilib.units.measure.AngularVelocity getNextVelocitySetpoint()Get the next velocity fromExponentialProfile- Returns:
- Next
AngularVelocityfromExponentialProfile - Throws:
NoSuchElementException- if no next state exists becausecalculate(double, double, double)has not been called since construction or the lastreset(State).
-
getNextState
Get the next state of theExponentialProfile.- Returns:
ExponentialProfile.Stategiven by theExponentialProfile.
-
calculate
public double calculate(double measurementPosition, double setpointVelocity, double setpointPosition) Calculate the feedback, assuming previous state velocity.- Parameters:
measurementPosition- Measurement position to set as the current state..setpointVelocity- Setpoint velocity.setpointPosition- Setpoint position.- Returns:
- Profile calculation
-
calculate
public double calculate(double measurementPosition, double setpointPosition) Calculate the feedback, assuming no setpoint velocity.- Parameters:
measurementPosition- Measurement position to set as the current state.setpointPosition- Setpoint position.- Returns:
- Profile calculation where setpoint velocity is 0.
-
getPositionTolerance
public double getPositionTolerance()Returns the error tolerance of this controller. Defaults to 0.05.- Returns:
- the error tolerance of the controller
-
getP
public double getP()Get the Proportional coefficient.- Returns:
- proportional coefficient
-
setP
public void setP(double kP) Sets the Proportional coefficient of the PID controller gain.- Parameters:
kP- The proportional coefficient. Must be >= 0.
-
getI
public double getI()Get the Integral coefficient.- Returns:
- integral coefficient
-
setI
public void setI(double kI) Sets the Integral coefficient of the PID controller gain.- Parameters:
kI- The integral coefficient. Must be >= 0.
-
getD
public double getD()Get the Differential coefficient.- Returns:
- differential coefficient
-
setD
public void setD(double kD) Sets the Differential coefficient of the PID controller gain.- Parameters:
kD- The differential coefficient. Must be >= 0.
-
enableContinuousInput
public void enableContinuousInput(double minimumInput, double maximumInput) Enables continuous input.Rather then using the max and min input range as constraints, it considers them to be the same point and automatically calculates the shortest route to the setpoint.
- Parameters:
minimumInput- The minimum value expected from the input.maximumInput- The maximum value expected from the input.
-