From e503289c20bbf8465e36cc71c135988963253367 Mon Sep 17 00:00:00 2001 From: jasondaming Date: Tue, 7 Oct 2025 15:49:12 -0500 Subject: [PATCH 1/2] Replace deprecated feedforward.calculate(velocity, acceleration) Update ProfiledPIDController documentation to use the recommended feedforward.calculate(currentVelocity, nextVelocity) overload instead of the deprecated (velocity, acceleration) version. This eliminates manual acceleration calculations and uses the newer API that calculates acceleration internally from velocity change. Fixes #2963 --- .../controllers/profiled-pidcontroller.rst | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst b/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst index 84e60773bc..55963e5418 100644 --- a/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst +++ b/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst @@ -86,55 +86,44 @@ The returned setpoint might then be used as in the following example: ```java 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); motor.setVoltage( pidVal - + feedforward.calculate(controller.getSetpoint().velocity, acceleration)); + + feedforward.calculate(lastSpeed, controller.getSetpoint().velocity)); lastSpeed = controller.getSetpoint().velocity; - lastTime = Timer.getFPGATimestamp(); } ``` ```c++ 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); motor.SetVoltage( pidVal + - feedforward.Calculate(controller.GetSetpoint().velocity, acceleration)); + feedforward.Calculate(lastSpeed, controller.GetSetpoint().velocity)); lastSpeed = controller.GetSetpoint().velocity; - lastTime = frc2::Timer::GetFPGATimestamp(); } ``` ```python - from wpilib import Timer from wpilib.controller import ProfiledPIDController from wpilib.controller import SimpleMotorFeedforward def __init__(self): # Assuming encoder, motor, controller are already defined self.lastSpeed = 0 - self.lastTime = Timer.getFPGATimestamp() # Assuming feedforward is a SimpleMotorFeedforward object self.feedforward = SimpleMotorFeedforward(ks=0.0, kv=0.0, ka=0.0) def goToPosition(self, goalPosition: float): pidVal = self.controller.calculate(self.encoder.getDistance(), goalPosition) - acceleration = (self.controller.getSetpoint().velocity - self.lastSpeed) / (Timer.getFPGATimestamp() - self.lastTime) self.motor.setVoltage( pidVal - + self.feedforward.calculate(self.controller.getSetpoint().velocity, acceleration)) - self.lastSpeed = controller.getSetpoint().velocity - self.lastTime = Timer.getFPGATimestamp() + + self.feedforward.calculate(self.lastSpeed, self.controller.getSetpoint().velocity)) + self.lastSpeed = self.controller.getSetpoint().velocity ``` ## Complete Usage Example From c80e6e884722228347e59a6ee5ba94f2996f7906 Mon Sep 17 00:00:00 2001 From: jasondaming Date: Tue, 7 Oct 2025 21:36:17 -0500 Subject: [PATCH 2/2] Convert ProfiledPIDController feedforward example to use snippets Replace inline code blocks with RLIs to snippets from allwpilib. Java and C++ examples now reference compilable snippet code that will be tested in every allwpilib build. Python remains as inline code block since Python snippets are maintained in robotpy repository. Related: wpilibsuite/allwpilib#8280 --- .../controllers/profiled-pidcontroller.rst | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst b/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst index 55963e5418..6fbc57aba1 100644 --- a/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst +++ b/source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst @@ -84,47 +84,31 @@ The returned setpoint might then be used as in the following example: .. tab-set-code:: - ```java - double lastSpeed = 0; - // Controls a simple motor's position using a SimpleMotorFeedforward - // and a ProfiledPIDController - public void goToPosition(double goalPosition) { - double pidVal = controller.calculate(encoder.getDistance(), goalPosition); - motor.setVoltage( - pidVal - + feedforward.calculate(lastSpeed, controller.getSetpoint().velocity)); - lastSpeed = controller.getSetpoint().velocity; - } - ``` - - ```c++ - units::meters_per_second_t lastSpeed = 0_mps; - // 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); - motor.SetVoltage( - pidVal + - feedforward.Calculate(lastSpeed, controller.GetSetpoint().velocity)); - lastSpeed = controller.GetSetpoint().velocity; - } - ``` - - ```python - from wpilib.controller import ProfiledPIDController - from wpilib.controller import SimpleMotorFeedforward - def __init__(self): - # Assuming encoder, motor, controller are already defined - self.lastSpeed = 0 - # Assuming feedforward is a SimpleMotorFeedforward object - self.feedforward = SimpleMotorFeedforward(ks=0.0, kv=0.0, ka=0.0) - def goToPosition(self, goalPosition: float): - pidVal = self.controller.calculate(self.encoder.getDistance(), goalPosition) - self.motor.setVoltage( - pidVal - + self.feedforward.calculate(self.lastSpeed, self.controller.getSetpoint().velocity)) - self.lastSpeed = self.controller.getSetpoint().velocity - ``` + .. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/17344d8891d0121dabac876730496828554a03bc/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/profiledpidfeedforward/Robot.java + :language: java + :lines: 33-39 + :lineno-match: + + .. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/17344d8891d0121dabac876730496828554a03bc/wpilibcExamples/src/main/cpp/snippets/ProfiledPIDFeedforward/cpp/Robot.cpp + :language: c++ + :lines: 23-30 + :lineno-match: + + .. code-block:: python + + from wpilib.controller import ProfiledPIDController + from wpilib.controller import SimpleMotorFeedforward + def __init__(self): + # Assuming encoder, motor, controller are already defined + self.lastSpeed = 0 + # Assuming feedforward is a SimpleMotorFeedforward object + self.feedforward = SimpleMotorFeedforward(ks=0.0, kv=0.0, ka=0.0) + def goToPosition(self, goalPosition: float): + pidVal = self.controller.calculate(self.encoder.getDistance(), goalPosition) + self.motor.setVoltage( + pidVal + + self.feedforward.calculate(self.lastSpeed, self.controller.getSetpoint().velocity)) + self.lastSpeed = self.controller.getSetpoint().velocity ## Complete Usage Example