Skip to content

Commit 8cf86a9

Browse files
committed
Enables PIDController Feed Forward term to be calculated by the end user
1 parent 8dd6a17 commit 8cf86a9

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

WPILib/PIDController.cs

+52-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public enum ToleranceType
5353
private double m_minimumInput = 0.0; // minimum input - limit setpoint to this
5454
private bool m_continuous = false; // do the endpoints wrap around? eg. Absolute encoder
5555
private bool m_enabled = false; //is the pid controller enabled
56-
private double m_prevInput = 0.0; // the prior sensor input (used to compute velocity)
56+
private double m_prevError = 0.0; // the prior sensor error (used to compute velocity)
5757
private double m_totalError = 0.0; //the sum of the errors for use in the integral calc
5858
private double m_setpoint = 0.0;
5959
private double m_error = 0.0;
@@ -73,6 +73,9 @@ public enum ToleranceType
7373
private readonly Queue<double> m_buf;
7474
private double m_bufTotal = 0.0;
7575

76+
private double m_prevSetpoint = 0.0;
77+
private Timer m_setpointTimer;
78+
7679
private double m_tolerance = 0.05;
7780

7881
private Notifier m_controlLoop;
@@ -100,6 +103,8 @@ public PIDController(double kp, double ki, double kd, double kf,
100103

101104
CalculateCallback = Calculate;
102105
m_controlLoop = new Notifier(CalculateCallback);
106+
m_setpointTimer = new Timer();
107+
m_setpointTimer.Start();
103108

104109
m_P = kp;
105110
m_I = ki;
@@ -252,7 +257,7 @@ protected virtual void Calculate()
252257
m_totalError = m_maximumOutput / m_P;
253258
}
254259
}
255-
m_result = m_P * m_totalError + m_D * m_error + m_setpoint * m_F;
260+
m_result = m_P * m_totalError + m_D * m_error + CalculateFeedForward();
256261
}
257262
else
258263
{
@@ -275,11 +280,12 @@ protected virtual void Calculate()
275280
m_totalError = m_maximumOutput / m_I;
276281
}
277282
}
283+
m_result = m_P * m_error + m_I * m_totalError + m_D * (m_error - m_prevError) + CalculateFeedForward();
278284
}
279285

280-
m_result = m_P * m_error + m_I * m_totalError + m_D * (m_prevInput - input) + m_setpoint * m_F;
281286

282-
m_prevInput = input;
287+
288+
m_prevError = m_error; ;
283289

284290
if (m_result > m_maximumOutput)
285291
{
@@ -304,6 +310,38 @@ protected virtual void Calculate()
304310
pidOutput.PidWrite(result);
305311
}
306312

313+
/// <summary>
314+
/// Calculate the feed forward term.
315+
/// </summary>
316+
/// <remarks>
317+
///Both of the provided feed forward calculations are velocity feed forwards.
318+
/// If a different feed forward calculation is desired, the user can override
319+
/// this function and provide his or her own.This function does no
320+
/// synchronization because the PIDController class only calls it in
321+
/// synchronized code, so be careful if calling it oneself.
322+
/// <para></para>
323+
/// If a velocity PID controller is being used, the F term should be set to 1
324+
/// over the maximum setpoint for the output.If a position PID controller is
325+
/// being used, the F term should be set to 1 over the maximum speed for the
326+
/// output measured in setpoint units per this controller's update period (see
327+
/// the default period in this class's constructor).
328+
/// </remarks>
329+
/// <returns>The calculated Feed Forward Value</returns>
330+
protected virtual double CalculateFeedForward()
331+
{
332+
if (PIDInput.PIDSourceType == PIDSourceType.Rate)
333+
{
334+
return m_F * Setpoint;
335+
}
336+
else
337+
{
338+
double temp = m_F * GetDeltaSetpoint();
339+
m_prevSetpoint = m_setpoint;
340+
m_setpointTimer.Reset();
341+
return temp;
342+
}
343+
}
344+
307345
///<inheritdoc/>
308346
public void SetPID(double p, double i, double d)
309347
{
@@ -497,6 +535,15 @@ public double Setpoint
497535
}
498536
}
499537

538+
/// <summary>
539+
/// Retunrs the change in setpoing over time of the PIDController.
540+
/// </summary>
541+
/// <returns>The change in setpoint over time.</returns>
542+
public double GetDeltaSetpoint()
543+
{
544+
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get();
545+
}
546+
500547
/// <summary>
501548
/// Returns the current difference of the input from the setpoint.
502549
/// </summary>
@@ -644,7 +691,7 @@ public void Reset()
644691
lock (m_lockObject)
645692
{
646693
Disable();
647-
m_prevInput = 0;
694+
m_prevError = 0;
648695
m_totalError = 0;
649696
m_result = 0;
650697
}

0 commit comments

Comments
 (0)