Combining Motion Profiling and PID Control with ProfiledPIDController


For a guide on implementing the ProfiledPIDController class in the command-based framework framework, see Combining Motion Profiling and PID in Command-Based.

In the previous article, we saw how to use the TrapezoidProfile class to create and use a trapezoidal motion profile. The example code from that article demonstrates manually composing the TrapezoidProfile class with the external PID control feature of a ”smart“ motor controller.

This combination of functionality (a motion profile for generating setpoints combined with a PID controller for following them) is extremely common. To facilitate this, WPILib comes with a ProfiledPIDController class (Java, C++) that does most of the work of combining these two functionalities. The API of the ProfiledPIDController is very similar to that of the PIDController, allowing users to add motion profiling to a PID-controlled mechanism with very few changes to their code.

Using the ProfiledPIDController class


In C++, the ProfiledPIDController class is templated on the unit type used for distance measurements, which may be angular or linear. The passed-in values must have units consistent with the distance units, or a compile-time error will be thrown. For more information on C++ units, see The C++ Units Library.


Much of the functionality of ProfiledPIDController is effectively identical to that of PIDController. Accordingly, this article will only cover features that are substantially-changed to accommodate the motion profiling functionality. For information on standard PIDController features, see בקרת PID עם WPILib.

Constructing a ProfiledPIDController


C++ is often able to infer the type of the inner classes, and thus a simple initializer list (without the class name) can be sent as a parameter. The full class name is included in the example below for clarity.

Creating a ProfiledPIDController is nearly identical to creating a PIDController. The only difference is the need to supply a set of trapezoidal profile constraints, which will be automatically forwarded to the internally-generated TrapezoidProfile instances:

// Creates a ProfiledPIDController
// Max velocity is 5 meters per second
// Max acceleration is 10 meters per second
ProfiledPIDController controller = new ProfiledPIDController(
  kP, kI, kD,
  new TrapezoidProfile.Constraints(5, 10));
// Creates a ProfiledPIDController
// Max velocity is 5 meters per second
// Max acceleration is 10 meters per second
frc::ProfiledPIDController<units::meters> controller(
  kP, kI, kD,
  frc::TrapezoidProfile<units::meters>::Constraints{5_mps, 10_mps_sq});

Goal vs Setpoint

A major difference between a standard PIDController and a ProfiledPIDController is that the actual setpoint of the control loop is not directly specified by the user. Rather, the user specifies a goal position or state, and the setpoint for the controller is computed automatically from the generated motion profile between the current state and the goal. So, while the user-side call looks mostly identical:

// Calculates the output of the PID algorithm based on the sensor reading
// and sends it to a motor
motor.set(controller.calculate(encoder.getDistance(), goal));
// Calculates the output of the PID algorithm based on the sensor reading
// and sends it to a motor
motor.Set(controller.Calculate(encoder.GetDistance(), goal));

The specified goal value (which can be either a position value or a TrapezoidProfile.State, if nonzero velocity is desired) is not necessarily the current setpoint of the loop - rather, it is the eventual setpoint once the generated profile terminates.

Getting/Using the Setpoint

Since the ProfiledPIDController goal differs from the setpoint, is if often desirable to poll the current setpoint of the controller (for instance, to get values to use with feedforward). This can be done with the getSetpoint() method.

The returned setpoint might then be used as in the following example:

double lastSpeed = 0;
double lastTime = Timer.getFPGATimestamp();

// Controls a simple motor's position using a SimpleMotorFeedforward
// and a ProfiledPIDController
public void goToPosition(double goalPosition) {
  double pidVal = controller.calculate(encoder.getDistance(), goalPosition);
  double acceleration = (controller.getSetpoint().velocity - lastSpeed) / (Timer.getFPGATimestamp() - lastTime);
      + feedforward.calculate(controller.getSetpoint().velocity, acceleration));
  lastSpeed = controller.getSetpoint().velocity;
  lastTime = Timer.getFPGATimestamp();
units::meters_per_second_t lastSpeed = 0_mps;
units::second_t lastTime = frc2::Timer::GetFPGATimestamp();

// Controls a simple motor's position using a SimpleMotorFeedforward
// and a ProfiledPIDController
void GoToPosition(units::meter_t goalPosition) {
  auto pidVal = controller.Calculate(units::meter_t{encoder.GetDistance()}, goalPosition);
  auto acceleration = (controller.GetSetpoint().velocity - lastSpeed) /
      (frc2::Timer::GetFPGATimestamp() - lastTime);
       pidVal +
      feedforward.Calculate(controller.GetSetpoint().velocity, acceleration));
  lastSpeed = controller.GetSetpoint().velocity;
  lastTime = frc2::Timer::GetFPGATimestamp();

Complete Usage Example

A more complete example of ProfiledPIDController usage is provided in the ElevatorProfilePID example project (Java, C++):

 5package edu.wpi.first.wpilibj.examples.elevatorprofiledpid;
 7import edu.wpi.first.math.controller.ElevatorFeedforward;
 8import edu.wpi.first.math.controller.ProfiledPIDController;
 9import edu.wpi.first.math.trajectory.TrapezoidProfile;
10import edu.wpi.first.wpilibj.Encoder;
11import edu.wpi.first.wpilibj.Joystick;
12import edu.wpi.first.wpilibj.TimedRobot;
13import edu.wpi.first.wpilibj.motorcontrol.MotorController;
14import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
17public class Robot extends TimedRobot {
18  private static double kDt = 0.02;
19  private static double kMaxVelocity = 1.75;
20  private static double kMaxAcceleration = 0.75;
21  private static double kP = 1.3;
22  private static double kI = 0.0;
23  private static double kD = 0.7;
24  private static double kS = 1.1;
25  private static double kG = 1.2;
26  private static double kV = 1.3;
28  private final Joystick m_joystick = new Joystick(1);
29  private final Encoder m_encoder = new Encoder(1, 2);
30  private final MotorController m_motor = new PWMSparkMax(1);
32  // Create a PID controller whose setpoint's change is subject to maximum
33  // velocity and acceleration constraints.
34  private final TrapezoidProfile.Constraints m_constraints =
35      new TrapezoidProfile.Constraints(kMaxVelocity, kMaxAcceleration);
36  private final ProfiledPIDController m_controller =
37      new ProfiledPIDController(kP, kI, kD, m_constraints, kDt);
38  private final ElevatorFeedforward m_feedforward = new ElevatorFeedforward(kS, kG, kV);
40  @Override
41  public void robotInit() {
42    m_encoder.setDistancePerPulse(1.0 / 360.0 * 2.0 * Math.PI * 1.5);
43  }
45  @Override
46  public void teleopPeriodic() {
47    if (m_joystick.getRawButtonPressed(2)) {
48      m_controller.setGoal(5);
49    } else if (m_joystick.getRawButtonPressed(3)) {
50      m_controller.setGoal(0);
51    }
53    // Run controller and update motor output
54    m_motor.setVoltage(
55        m_controller.calculate(m_encoder.getDistance())
56            + m_feedforward.calculate(m_controller.getSetpoint().velocity));
57  }
 5#include <numbers>
 7#include <frc/Encoder.h>
 8#include <frc/Joystick.h>
 9#include <frc/TimedRobot.h>
10#include <frc/controller/ElevatorFeedforward.h>
11#include <frc/controller/ProfiledPIDController.h>
12#include <frc/motorcontrol/PWMSparkMax.h>
13#include <frc/trajectory/TrapezoidProfile.h>
14#include <units/acceleration.h>
15#include <units/length.h>
16#include <units/time.h>
17#include <units/velocity.h>
18#include <units/voltage.h>
20class Robot : public frc::TimedRobot {
21 public:
22  static constexpr units::second_t kDt = 20_ms;
24  Robot() {
25    m_encoder.SetDistancePerPulse(1.0 / 360.0 * 2.0 * std::numbers::pi * 1.5);
26  }
28  void TeleopPeriodic() override {
29    if (m_joystick.GetRawButtonPressed(2)) {
30      m_controller.SetGoal(5_m);
31    } else if (m_joystick.GetRawButtonPressed(3)) {
32      m_controller.SetGoal(0_m);
33    }
35    // Run controller and update motor output
36    m_motor.SetVoltage(
37        units::volt_t{
38            m_controller.Calculate(units::meter_t{m_encoder.GetDistance()})} +
39        m_feedforward.Calculate(m_controller.GetSetpoint().velocity));
40  }
42 private:
43  static constexpr units::meters_per_second_t kMaxVelocity = 1.75_mps;
44  static constexpr units::meters_per_second_squared_t kMaxAcceleration =
45      0.75_mps_sq;
46  static constexpr double kP = 1.3;
47  static constexpr double kI = 0.0;
48  static constexpr double kD = 0.7;
49  static constexpr units::volt_t kS = 1.1_V;
50  static constexpr units::volt_t kG = 1.2_V;
51  static constexpr auto kV = 1.3_V / 1_mps;
53  frc::Joystick m_joystick{1};
54  frc::Encoder m_encoder{1, 2};
55  frc::PWMSparkMax m_motor{1};
57  // Create a PID controller whose setpoint's change is subject to maximum
58  // velocity and acceleration constraints.
59  frc::TrapezoidProfile<units::meters>::Constraints m_constraints{
60      kMaxVelocity, kMaxAcceleration};
61  frc::ProfiledPIDController<units::meters> m_controller{kP, kI, kD,
62                                                         m_constraints, kDt};
63  frc::ElevatorFeedforward m_feedforward{kS, kG, kV};
67int main() {
68  return frc::StartRobot<Robot>();