@@ -53,7 +53,7 @@ public enum ToleranceType
53
53
private double m_minimumInput = 0.0 ; // minimum input - limit setpoint to this
54
54
private bool m_continuous = false ; // do the endpoints wrap around? eg. Absolute encoder
55
55
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)
57
57
private double m_totalError = 0.0 ; //the sum of the errors for use in the integral calc
58
58
private double m_setpoint = 0.0 ;
59
59
private double m_error = 0.0 ;
@@ -73,6 +73,9 @@ public enum ToleranceType
73
73
private readonly Queue < double > m_buf ;
74
74
private double m_bufTotal = 0.0 ;
75
75
76
+ private double m_prevSetpoint = 0.0 ;
77
+ private Timer m_setpointTimer ;
78
+
76
79
private double m_tolerance = 0.05 ;
77
80
78
81
private Notifier m_controlLoop ;
@@ -100,6 +103,8 @@ public PIDController(double kp, double ki, double kd, double kf,
100
103
101
104
CalculateCallback = Calculate ;
102
105
m_controlLoop = new Notifier ( CalculateCallback ) ;
106
+ m_setpointTimer = new Timer ( ) ;
107
+ m_setpointTimer . Start ( ) ;
103
108
104
109
m_P = kp ;
105
110
m_I = ki ;
@@ -252,7 +257,7 @@ protected virtual void Calculate()
252
257
m_totalError = m_maximumOutput / m_P ;
253
258
}
254
259
}
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 ( ) ;
256
261
}
257
262
else
258
263
{
@@ -275,11 +280,12 @@ protected virtual void Calculate()
275
280
m_totalError = m_maximumOutput / m_I ;
276
281
}
277
282
}
283
+ m_result = m_P * m_error + m_I * m_totalError + m_D * ( m_error - m_prevError ) + CalculateFeedForward ( ) ;
278
284
}
279
285
280
- m_result = m_P * m_error + m_I * m_totalError + m_D * ( m_prevInput - input ) + m_setpoint * m_F ;
281
286
282
- m_prevInput = input ;
287
+
288
+ m_prevError = m_error ; ;
283
289
284
290
if ( m_result > m_maximumOutput )
285
291
{
@@ -304,6 +310,38 @@ protected virtual void Calculate()
304
310
pidOutput . PidWrite ( result ) ;
305
311
}
306
312
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
+
307
345
///<inheritdoc/>
308
346
public void SetPID ( double p , double i , double d )
309
347
{
@@ -497,6 +535,15 @@ public double Setpoint
497
535
}
498
536
}
499
537
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
+
500
547
/// <summary>
501
548
/// Returns the current difference of the input from the setpoint.
502
549
/// </summary>
@@ -644,7 +691,7 @@ public void Reset()
644
691
lock ( m_lockObject )
645
692
{
646
693
Disable ( ) ;
647
- m_prevInput = 0 ;
694
+ m_prevError = 0 ;
648
695
m_totalError = 0 ;
649
696
m_result = 0 ;
650
697
}
0 commit comments