Skip to content

Commit b6cf5e0

Browse files
authored
Correct profiledpid example code (#2175)
The acceleration parameter provided to the feedforward object was being calculated with the incorrect velocity setpoint (now corrected).
1 parent 04198c3 commit b6cf5e0

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

source/docs/software/advanced-controls/controllers/profiled-pidcontroller.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ The returned setpoint might then be used as in the following example:
8080
// Controls a simple motor's position using a SimpleMotorFeedforward
8181
// and a ProfiledPIDController
8282
public void goToPosition(double goalPosition) {
83-
double acceleration = (controller.getSetpoint().velocity - lastSpeed) / (Timer.getFPGATimestamp() - lastTime)
83+
double pidVal = controller.calculate(encoder.getDistance(), goalPosition);
84+
double acceleration = (controller.getSetpoint().velocity - lastSpeed) / (Timer.getFPGATimestamp() - lastTime);
8485
motor.setVoltage(
85-
controller.calculate(encoder.getDistance(), goalPosition)
86+
pidVal
8687
+ feedforward.calculate(controller.getSetpoint().velocity, acceleration));
8788
lastSpeed = controller.getSetpoint().velocity;
8889
lastTime = Timer.getFPGATimestamp();
@@ -96,10 +97,11 @@ The returned setpoint might then be used as in the following example:
9697
// Controls a simple motor's position using a SimpleMotorFeedforward
9798
// and a ProfiledPIDController
9899
void GoToPosition(units::meter_t goalPosition) {
100+
auto pidVal = controller.Calculate(units::meter_t{encoder.GetDistance()}, goalPosition);
99101
auto acceleration = (controller.GetSetpoint().velocity - lastSpeed) /
100102
(frc2::Timer::GetFPGATimestamp() - lastTime);
101103
motor.SetVoltage(
102-
controller.Calculate(units::meter_t{encoder.GetDistance()}, goalPosition) +
104+
pidVal +
103105
feedforward.Calculate(controller.GetSetpoint().velocity, acceleration));
104106
lastSpeed = controller.GetSetpoint().velocity;
105107
lastTime = frc2::Timer::GetFPGATimestamp();

0 commit comments

Comments
 (0)