@@ -84,47 +84,31 @@ The returned setpoint might then be used as in the following example:
8484
8585.. tab-set-code ::
8686
87- ```java
88- double lastSpeed = 0;
89- // Controls a simple motor's position using a SimpleMotorFeedforward
90- // and a ProfiledPIDController
91- public void goToPosition(double goalPosition) {
92- double pidVal = controller.calculate(encoder.getDistance(), goalPosition);
93- motor.setVoltage(
94- pidVal
95- + feedforward.calculate(lastSpeed, controller.getSetpoint().velocity));
96- lastSpeed = controller.getSetpoint().velocity;
97- }
98- ` ``
99-
100- ```c++
101- units::meters_per_second_t lastSpeed = 0_mps;
102- // Controls a simple motor's position using a SimpleMotorFeedforward
103- // and a ProfiledPIDController
104- void GoToPosition(units::meter_t goalPosition) {
105- auto pidVal = controller.Calculate(units::meter_t{encoder.GetDistance()}, goalPosition);
106- motor.SetVoltage(
107- pidVal +
108- feedforward.Calculate(lastSpeed, controller.GetSetpoint().velocity));
109- lastSpeed = controller.GetSetpoint().velocity;
110- }
111- ` ``
112-
113- ```python
114- from wpilib.controller import ProfiledPIDController
115- from wpilib.controller import SimpleMotorFeedforward
116- def __init__(self):
117- # Assuming encoder, motor, controller are already defined
118- self.lastSpeed = 0
119- # Assuming feedforward is a SimpleMotorFeedforward object
120- self.feedforward = SimpleMotorFeedforward(ks=0.0, kv=0.0, ka=0.0)
121- def goToPosition(self, goalPosition: float):
122- pidVal = self.controller.calculate(self.encoder.getDistance(), goalPosition)
123- self.motor.setVoltage(
124- pidVal
125- + self.feedforward.calculate(self.lastSpeed, self.controller.getSetpoint().velocity))
126- self.lastSpeed = self.controller.getSetpoint().velocity
127- ` ``
87+ .. remoteliteralinclude :: https://raw.githubusercontent.com/wpilibsuite/allwpilib/17344d8891d0121dabac876730496828554a03bc/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/profiledpidfeedforward/Robot.java
88+ :language: java
89+ :lines: 33-39
90+ :lineno-match:
91+
92+ .. remoteliteralinclude :: https://raw.githubusercontent.com/wpilibsuite/allwpilib/17344d8891d0121dabac876730496828554a03bc/wpilibcExamples/src/main/cpp/snippets/ProfiledPIDFeedforward/cpp/Robot.cpp
93+ :language: c++
94+ :lines: 23-30
95+ :lineno-match:
96+
97+ .. code-block :: python
98+
99+ from wpilib.controller import ProfiledPIDController
100+ from wpilib.controller import SimpleMotorFeedforward
101+ def __init__ (self ):
102+ # Assuming encoder, motor, controller are already defined
103+ self .lastSpeed = 0
104+ # Assuming feedforward is a SimpleMotorFeedforward object
105+ self .feedforward = SimpleMotorFeedforward(ks = 0.0 , kv = 0.0 , ka = 0.0 )
106+ def goToPosition (self , goalPosition : float ):
107+ pidVal = self .controller.calculate(self .encoder.getDistance(), goalPosition)
108+ self .motor.setVoltage(
109+ pidVal
110+ + self .feedforward.calculate(self .lastSpeed, self .controller.getSetpoint().velocity))
111+ self .lastSpeed = self .controller.getSetpoint().velocity
128112
129113 ## Complete Usage Example
130114
0 commit comments