Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
controllers::PIDController Class Reference

This class encapsulates the fundamental principles of Proportional-Integral-Derivative (PID) control with an additional feedforward component. It provides a versatile control mechanism for various systems, enabling precise control of processes by adjusting an output in response to the error between a desired setpoint and a measured process variable. More...

#include <PIDController.h>

Public Member Functions

 PIDController (const double dKp, const double dKi, const double dKd, const double dKff=0.0)
 Construct a new PIDController::PIDController object.
 
double Calculate (const double dActual, const double dSetpoint)
 Calculate the next control output given the current actual and a setpoint.
 
double Calculate (const double dActual)
 Calculate the next control output given the current actual and using the previously set setpoint.
 
void EnableContinuousInput (const double dMinimumInput, const double dMaximumInput)
 Enables continuous wraparound of PID controller input. Use for setpoints that exists on a circle. For example, if using degrees for setpoint and actual calling this method with 0, 360 will treat 0 and 360 as the same number. This prevents the controller from going the long way around the circle when the setpoint and actual are near the wraparound point.
 
void DisableContinuousInput ()
 Disable continuous input wraparound of the PID controller.
 
void Reset ()
 Resets the controller. This erases the integral term buildup, and removes derivative term on the next iteration.
 
void SetProportional (const double dKp)
 Configure the proportional gain parameter. This quickly responds to changes in setpoint, and provides most of the initial driving force to make corrections. Some systems can be used with only a P gain, and many can be operated with only PI. For position based controllers, Proportional gain the first parameter to tune, with integral gain second. For rate controlled systems, Proportional is often the second after feedforward.
 
void SetIntegral (const double dKi)
 Configure the integral gain parameter. This is used for overcoming disturbances, and ensuring that the controller always minimizes error, when the control actual is close to the setpoint. Typically tuned second for "Position" based modes, and third for "Rate" or continuous based modes.
 
void SetDerivative (const double dKd)
 Configure the derivative gain parameter. This acts as a brake or dampener on the control effort. The more the controller tries to change the value, the more it counteracts the effort. Typically this Parameter is tuned last and only used to reduce overshoot in systems. When using derivative gain, you can usually get a more responsive controller by over-tuning proportional gain so that small oscillations are visible and then bringing derivative gain up until the oscillations disappear.
 
void SetFeedforward (const double dKff)
 Configure the feedforward gain parameter. This is excellent for velocity, rate, and other continuous control modes where you can expect a rough output value based solely on the setpoint. This should not be used in "position" based control modes.
 
void SetPID (const double dKp, const double dKi, const double dKd)
 Mutator for PID gains of the controller.
 
void SetPID (const double dKp, const double dKi, const double dKd, const double dKff)
 Mutator for PID and Feedforward gains of the controller.
 
void SetSetpoint (const double dSetpoint)
 Mutator for the setpoint for the PID controller.
 
void SetMaxSetpointDifference (const double dMaxSetpointDifference)
 Mutator for the maximum allowable distance of the setpoint from the actual value. This doesn't limit the setable value of the setpoint. It only limits the value that the controller is given specify the error from the setpoint.
 
void SetMaxIntegralEffort (const double dMaxIEffort)
 Mutator for the max value of the integral term during PID calculation. This is useful for preventing integral wind-up runaways.
 
void SetTolerance (const double dTolerance)
 Mutator for the tolerance of the controller. This is used to determine if the.
 
void SetOutputLimits (const double dMinEffort, const double dMaxEffort)
 Mutator for setting the minimum and maximum allowable values of the control output from the controller.
 
void SetOutputLimits (const double dMaxMin)
 Mutator for setting the minimum and maximum allowable values of the control output from the controller.
 
void SetOutputRampRate (const double dOutputRampRate)
 Mutator for the maximum rate that the output can change per iteration.
 
void SetOutputFilter (const double dStrength)
 Set a filter on the output to reduce sharp oscillations. 0.1 is likely a good starting point.
 
void SetDirection (const bool bReversed=false)
 Set the operating direction of the PID controller. Set true to reverse PID output.
 
double GetProportional () const
 Accessor for proportional gain of the PID controller.
 
double GetIntegral () const
 Accessor for integral gain of the PID controller.
 
double GetDerivative () const
 Accessor for derivative gain of the PID controller.
 
double GetFeedforward () const
 Accessor for feedforward gain of the PID controller.
 
double GetSetpoint () const
 Accessor for the current setpoint that the controller is trying to reach.
 
double GetMaxSetpointDifference () const
 Accessor for the maximum allowable difference the setpoint can be from the actual. Doesn't limit the setpoint, just the max error. 0 = disabled.
 
double GetMaxIntegralEffort () const
 Accessor for the maximum contribution of the integral term within the controller. 0 = disabled.
 
double GetOutputRampRate () const
 Accessor the the maximum allowed output ramp rate of the PID controller.
 
double GetOutputFilter () const
 Accessor for the exponential rolling sum filter strength. Used to reduce sharp output oscillations or jumps.
 
double GetContinuousInputEnabled () const
 
bool GetReversed () const
 Accessor for if the controller outputs are set to be negated or reversed.
 

Private Member Functions

void CheckGainSigns ()
 To operate correctly, all PID parameters require the same sign.
 

Private Attributes

double m_dKp
 
double m_dKi
 
double m_dKd
 
double m_dKff
 
double m_dSetpoint
 
double m_dErrorSum
 
double m_dMaxIError
 
double m_dMaxIEffort
 
double m_dMinEffort
 
double m_dMaxEffort
 
double m_dLastError
 
double m_dOutputRampRate
 
double m_dLastControlOutput
 
double m_dTolerance
 
double m_dOutputFilter
 
double m_dMaxSetpointDifference
 
double m_dMinimumContinuousInput
 
double m_dMaximumContinuousInput
 
bool m_bControllerIsContinuous
 
bool m_bFirstCalculation
 
bool m_bReversed
 

Detailed Description

This class encapsulates the fundamental principles of Proportional-Integral-Derivative (PID) control with an additional feedforward component. It provides a versatile control mechanism for various systems, enabling precise control of processes by adjusting an output in response to the error between a desired setpoint and a measured process variable.

The key features of this class include:

  • Proportional Control: The proportional gain (Kp) component adjusts the control output in proportion to the current error.
  • Integral Control: The integral gain (Ki) component accumulates past errors to eliminate steady-state error.
  • Derivative Control: The derivative gain (Kd) component anticipates future error by considering the rate of change of the error.
  • Feedforward Control: The feedforward gain (Kff) allows for predictive control, improving response time and minimizing overshoot. See https://blog.opticontrols.com/archives/297 for a better explanation on feedforward controller.

Additionally, this PID controller offers an optional wraparound feature. When enabled, it can be used for systems with non-linear, circular, or cyclic setpoints, effectively managing control in such scenarios.

This class is a valuable tool for control engineering, robotics, and automation, allowing you to fine-tune the control behavior of various processes and systems.

For specific details on class attributes and methods, please refer to the class documentation.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-16

Constructor & Destructor Documentation

◆ PIDController()

controllers::PIDController::PIDController ( const double  dKp,
const double  dKi,
const double  dKd,
const double  dKff = 0.0 
)

Construct a new PIDController::PIDController object.

Parameters
dKp- The proportional gain (Kp) component adjusts the control output in proportion to the current error.
dKi- The integral gain (Ki) component accumulates past errors to eliminate steady-state error.
dKd- The derivative gain (Kd) component anticipates future error by considering the rate of change of the error.
dKff- The feedforward gain (Kff) allows for predictive control, improving response time and minimizing overshoot.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
40 {
41 // Initialize member variable.
42 m_dKp = dKp;
43 m_dKi = dKi;
44 m_dKd = dKd;
45 m_dKff = dKff;
46 m_dSetpoint = 0.0;
47 m_dErrorSum = 0.0;
48 m_dMaxIError = 0.0;
49 m_dMaxIEffort = 0.0;
50 m_dMinEffort = 0.0;
51 m_dMaxEffort = 0.0;
52 m_dLastError = 0.0;
53 m_dOutputRampRate = 0.0;
54 m_dLastControlOutput = 0.0;
55 m_dOutputFilter = 0.0;
56 m_dMaxSetpointDifference = 0.0;
57 m_dMinimumContinuousInput = 0.0;
58 m_dMaximumContinuousInput = 360.0;
59 m_bControllerIsContinuous = false;
60 m_bFirstCalculation = true;
61 m_bReversed = false;
62
63 // Check gain values for sign.
64 this->CheckGainSigns();
65 }
void CheckGainSigns()
To operate correctly, all PID parameters require the same sign.
Definition PIDController.cpp:710
Here is the call graph for this function:

Member Function Documentation

◆ Calculate() [1/2]

double controllers::PIDController::Calculate ( const double  dActual,
const double  dSetpoint 
)

Calculate the next control output given the current actual and a setpoint.

Parameters
dActual- The current actual position of the system.
dSetpoint- The desired setpoint of the controller.
Returns
double - The resultant PID controller output.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
78 {
79 // Create instance variables.
80 double dPTermOutput;
81 double dITermOutput;
82 double dDTermOutput;
83 double dFFTermOutput;
84 double dOutput;
85
86 // Update setpoint member variable.
87 m_dSetpoint = dSetpoint;
88
89 // Apply the sliding setpoint range adjust if necessary.
90 if (m_dMaxSetpointDifference != 0)
91 {
92 // Clamp current setpoint value within a certain range of the current actual.
93 m_dSetpoint = numops::Clamp(m_dSetpoint, dActual - m_dMaxSetpointDifference, dActual + m_dMaxSetpointDifference);
94 }
95
96 // Determine error from setpoint.
97 double dError = dSetpoint - dActual;
98 // Check if the controller is within the tolerance.
99 if (numops::Bounded(dError, -m_dTolerance, m_dTolerance))
100 {
101 // Reset the error sum.
102 dError = 0.0;
103 }
104
105 // Check if the input, and therefor controller is continuous.
106 if (m_bControllerIsContinuous)
107 {
108 // Calculate error bounds.
109 double dErrorBound = (m_dMaximumContinuousInput - m_dMinimumContinuousInput) / 2.0;
110 // Wrap heading.
111 dError = numops::InputAngleModulus(dError, -dErrorBound, dErrorBound);
112 }
113
114 // Calculate feedforward term.
115 dFFTermOutput = m_dKff * dSetpoint;
116 // Calculate proportional term.
117 dPTermOutput = m_dKp * dError;
118
119 // Check if this is the first time doing a calculation.
120 if (m_bFirstCalculation)
121 {
122 // Assume the process variable hold same as previous.
123 m_dLastError = dError;
124 // Assume the last output is the current time-independent output.
125 m_dLastControlOutput = dPTermOutput + dFFTermOutput;
126
127 // Set first calculation toggle to false.
128 m_bFirstCalculation = false;
129 }
130
131 // Calculate derivative term.
132 // Note, derivative is actually negative and "slows" the system if it's doing
133 // the correct thing. Small gain values help prevent output spikes and overshoot.
134 dDTermOutput = -m_dKd * (dError - m_dLastError);
135 m_dLastError = dError;
136
137 // Calculate integral term.
138 // The integral term is more complex. There's several things to factor in to make it easier to deal with.
139 // 1. m_dMaxIEffort restricts the amount of output contributed by the integral term.
140 // 2. prevent windup by not increasing errorSum if we're already running against our max Ioutput
141 // 3. prevent windup by not increasing errorSum if output is output=maxOutput
142 dITermOutput = m_dKi * m_dErrorSum;
143 // Apply integral term limits.
144 if (m_dMaxIEffort != 0)
145 {
146 // Clamp current integral term output.
147 dITermOutput = numops::Clamp(dITermOutput, -m_dMaxIEffort, m_dMaxIEffort);
148 }
149
150 // Here's PID!
151 dOutput = dFFTermOutput + dPTermOutput + dITermOutput + dDTermOutput;
152 // Check if error is at not at limits.
153 if (m_dMinEffort != m_dMaxEffort && !numops::Bounded(dOutput, m_dMinEffort, m_dMaxEffort))
154 {
155 // Reset the error to a sane level. Settings to the current error ensures a smooth transition when the P term
156 // decreases enough for the I term to start acting upon the controller properly.
157 m_dErrorSum = dError;
158 }
159 // Check if the error is within the allowed ramp rate compared to the last output.
160 else if (m_dOutputRampRate != 0 && !numops::Bounded(dOutput, m_dLastControlOutput - m_dOutputRampRate, m_dLastControlOutput + m_dOutputRampRate))
161 {
162 // Reset the error to a sane level. Settings to the current error ensures a smooth transition when the P term
163 // decreases enough for the I term to start acting upon the controller properly.
164 m_dErrorSum = dError;
165 }
166 // Check if integral term output should be limited.
167 else if (m_dMaxIEffort != 0)
168 {
169 // In addition to output limiting directly, we also want to prevent integral term wind-up. Restrict directly.
170 m_dErrorSum = numops::Clamp(m_dErrorSum + dError, -m_dMaxIError, m_dMaxIError);
171 }
172 else
173 {
174 // Accumulate error.
175 m_dErrorSum += dError;
176 }
177
178 // Restrict output to our specified ramp limits.
179 if (m_dOutputRampRate != 0)
180 {
181 // Clamp output to ramp rate.
182 dOutput = numops::Clamp(dOutput, m_dLastControlOutput - m_dOutputRampRate, m_dLastControlOutput + m_dOutputRampRate);
183 }
184 // Restrict output to our specified effort limits.
185 if (m_dMinEffort != m_dMaxEffort)
186 {
187 // Clamp output to effort limits.
188 dOutput = numops::Clamp(dOutput, m_dMinEffort, m_dMaxEffort);
189 }
190 // Reduce jitters and output spikes with an exponential rolling sum filter.
191 if (m_dOutputFilter != 0)
192 {
193 // Filter output effort.
194 dOutput = (m_dLastControlOutput * m_dOutputFilter) + (dOutput * (1 - m_dOutputFilter));
195 }
196
197 // Update last output member variable.
198 m_dLastControlOutput = dOutput;
199
200 // Return the PID controller calculated output effort.
201 return dOutput;
202 }
constexpr T Clamp(T tValue, T tMin, T tMax)
Clamps a given value from going above or below a given threshold.
Definition NumberOperations.hpp:81
constexpr T InputAngleModulus(T tValue, T tMinValue, T tMaxValue)
Calculates the modulus of an input angle.
Definition NumberOperations.hpp:165
bool Bounded(T tValue, T tMin, T tMax, const bool bInclusive=true)
Checks if a given value is between the given maximum and minimum ranges.
Definition NumberOperations.hpp:101
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Calculate() [2/2]

double controllers::PIDController::Calculate ( const double  dActual)

Calculate the next control output given the current actual and using the previously set setpoint.

Parameters
dActual- The current actual position of the system.
Returns
double - The resultant PID controller output.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
215 {
216 // Calculate and return the output from the PIDController using the same setpoint.
217 return this->Calculate(dActual, m_dSetpoint);
218 }
double Calculate(const double dActual, const double dSetpoint)
Calculate the next control output given the current actual and a setpoint.
Definition PIDController.cpp:77
Here is the call graph for this function:

◆ EnableContinuousInput()

void controllers::PIDController::EnableContinuousInput ( const double  dMinimumInput,
const double  dMaximumInput 
)

Enables continuous wraparound of PID controller input. Use for setpoints that exists on a circle. For example, if using degrees for setpoint and actual calling this method with 0, 360 will treat 0 and 360 as the same number. This prevents the controller from going the long way around the circle when the setpoint and actual are near the wraparound point.

Parameters
dMinimumInput- Minimum input before wraparound.
dMaximumInput- Maximum input before wraparound.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
234 {
235 // Assign member variables.
236 m_dMinimumContinuousInput = dMinimumInput;
237 m_dMaximumContinuousInput = dMaximumInput;
238
239 // Enable continuous input.
240 m_bControllerIsContinuous = true;
241 }

◆ DisableContinuousInput()

void controllers::PIDController::DisableContinuousInput ( )

Disable continuous input wraparound of the PID controller.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
251 {
252 // Disable continuous input.
253 m_bControllerIsContinuous = false;
254 }

◆ Reset()

void controllers::PIDController::Reset ( )

Resets the controller. This erases the integral term buildup, and removes derivative term on the next iteration.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
265 {
266 // Reset necessary values to reset the controller.
267 m_bFirstCalculation = true;
268 m_dErrorSum = 0.0;
269 }

◆ SetProportional()

void controllers::PIDController::SetProportional ( const double  dKp)

Configure the proportional gain parameter. This quickly responds to changes in setpoint, and provides most of the initial driving force to make corrections. Some systems can be used with only a P gain, and many can be operated with only PI. For position based controllers, Proportional gain the first parameter to tune, with integral gain second. For rate controlled systems, Proportional is often the second after feedforward.

Parameters
dKp- The proportional gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
284 {
285 // Assign member variables.
286 m_dKp = dKp;
287
288 // Check signs of gains.
289 this->CheckGainSigns();
290 }
Here is the call graph for this function:

◆ SetIntegral()

void controllers::PIDController::SetIntegral ( const double  dKi)

Configure the integral gain parameter. This is used for overcoming disturbances, and ensuring that the controller always minimizes error, when the control actual is close to the setpoint. Typically tuned second for "Position" based modes, and third for "Rate" or continuous based modes.

Parameters
dKi- The integral gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
303 {
304 // Check if the current I gain is not zero and we need to scale our current error.
305 if (m_dKi != 0)
306 {
307 // Scale the current error sum, to match the new integral gain.
308 m_dErrorSum = m_dErrorSum * (m_dKi / dKi);
309 }
310 // Check if max integral term effort is enabled.
311 if (m_dMaxIEffort != 0)
312 {
313 // Update max error from new integral and max effort.
314 m_dMaxIError = m_dMaxIEffort / dKi;
315 }
316
317 // Assign integral gain member variable.
318 m_dKi = dKi;
319
320 // Check the signs of the PID gains.
321 this->CheckGainSigns();
322 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetDerivative()

void controllers::PIDController::SetDerivative ( const double  dKd)

Configure the derivative gain parameter. This acts as a brake or dampener on the control effort. The more the controller tries to change the value, the more it counteracts the effort. Typically this Parameter is tuned last and only used to reduce overshoot in systems. When using derivative gain, you can usually get a more responsive controller by over-tuning proportional gain so that small oscillations are visible and then bringing derivative gain up until the oscillations disappear.

Parameters
dKd- The derivative gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
337 {
338 // Assign member variables.
339 m_dKd = dKd;
340
341 // Check signs of gains.
342 this->CheckGainSigns();
343 }
Here is the call graph for this function:

◆ SetFeedforward()

void controllers::PIDController::SetFeedforward ( const double  dKff)

Configure the feedforward gain parameter. This is excellent for velocity, rate, and other continuous control modes where you can expect a rough output value based solely on the setpoint. This should not be used in "position" based control modes.

Parameters
dKff- The feedforward gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
356 {
357 // Assign member variables.
358 m_dKff = dKff;
359
360 // Check signs of gains.
361 this->CheckGainSigns();
362 }
Here is the call graph for this function:

◆ SetPID() [1/2]

void controllers::PIDController::SetPID ( const double  dKp,
const double  dKi,
const double  dKd 
)

Mutator for PID gains of the controller.

Parameters
dKp- The proportional gain for the controller to use for calculation.
dKi- The integral gain for the controller to use for calculation.
dKd- The derivative gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
375 {
376 // Update member variables.
377 m_dKp = dKp;
378 m_dKd = dKd;
379
380 // Call SetIntegral since is scales the accumulated error and checks signs.
381 this->SetIntegral(dKi);
382 }
void SetIntegral(const double dKi)
Configure the integral gain parameter. This is used for overcoming disturbances, and ensuring that th...
Definition PIDController.cpp:302
Here is the call graph for this function:

◆ SetPID() [2/2]

void controllers::PIDController::SetPID ( const double  dKp,
const double  dKi,
const double  dKd,
const double  dKff 
)

Mutator for PID and Feedforward gains of the controller.

Parameters
dKp- The proportional gain for the controller to use for calculation.
dKi- The integral gain for the controller to use for calculation.
dKd- The derivative gain for the controller to use for calculation.
dKff- The feedforward gain for the controller to use for calculation.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
396 {
397 // Update member variables.
398 m_dKp = dKp;
399 m_dKd = dKd;
400 m_dKff = dKff;
401
402 // Call SetIntegral since is scales the accumulated error and checks signs.
403 this->SetIntegral(dKi);
404 }
Here is the call graph for this function:

◆ SetSetpoint()

void controllers::PIDController::SetSetpoint ( const double  dSetpoint)

Mutator for the setpoint for the PID controller.

Parameters
dSetpoint- The setpoint that the controller should aim to get the actual to.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
415 {
416 // Assign member variable.
417 m_dSetpoint = dSetpoint;
418 }

◆ SetMaxSetpointDifference()

void controllers::PIDController::SetMaxSetpointDifference ( const double  dMaxSetpointDifference)

Mutator for the maximum allowable distance of the setpoint from the actual value. This doesn't limit the setable value of the setpoint. It only limits the value that the controller is given specify the error from the setpoint.

Parameters
dMaxSetpointDifference- The allowable distance of the setpoint from the actual.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
431 {
432 // Assign member variables.
433 m_dMaxSetpointDifference = dMaxSetpointDifference;
434 }

◆ SetMaxIntegralEffort()

void controllers::PIDController::SetMaxIntegralEffort ( const double  dMaxIEffort)

Mutator for the max value of the integral term during PID calculation. This is useful for preventing integral wind-up runaways.

Parameters
dMaxIEffort- The max allowable value of the integral term.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
446 {
447 // Assign member variable.
448 m_dMaxIEffort = std::fabs(dMaxIEffort);
449 // Update max error from new integral and max effort.
450 m_dMaxIError = m_dMaxIEffort / m_dKi;
451 }
Here is the caller graph for this function:

◆ SetTolerance()

void controllers::PIDController::SetTolerance ( const double  dTolerance)

Mutator for the tolerance of the controller. This is used to determine if the.

Parameters
dTolerance- The allowable error from the setpoint.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-02-28
462 {
463 // Assign member variable.
464 m_dTolerance = dTolerance;
465 }

◆ SetOutputLimits() [1/2]

void controllers::PIDController::SetOutputLimits ( const double  dMinEffort,
const double  dMaxEffort 
)

Mutator for setting the minimum and maximum allowable values of the control output from the controller.

Parameters
dMinEffort- The minimum allowable output from the PID controller.
dMaxEffort- The maximum allowable output from the PID controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
478 {
479 // Check if the maximum is greater than the minimum.
480 if (dMaxEffort > dMinEffort)
481 {
482 // Assign member variables.
483 m_dMinEffort = dMinEffort;
484 m_dMaxEffort = dMaxEffort;
485
486 // Ensure the bounds of the integral term are within the bounds of the allowable output range.
487 if (m_dMaxIEffort == 0 || m_dMaxIEffort > (dMaxEffort - dMinEffort))
488 {
489 // Set new integral max output.
490 this->SetMaxIntegralEffort(dMaxEffort - dMinEffort);
491 }
492 }
493 }
void SetMaxIntegralEffort(const double dMaxIEffort)
Mutator for the max value of the integral term during PID calculation. This is useful for preventing ...
Definition PIDController.cpp:445
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetOutputLimits() [2/2]

void controllers::PIDController::SetOutputLimits ( const double  dMinMax)

Mutator for setting the minimum and maximum allowable values of the control output from the controller.

Parameters
dMinMax- The overall output range of the controller. Must be positive.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
505 {
506 // Set output limits with the same max and min.
507 this->SetOutputLimits(-dMinMax, dMinMax);
508 }
void SetOutputLimits(const double dMinEffort, const double dMaxEffort)
Mutator for setting the minimum and maximum allowable values of the control output from the controlle...
Definition PIDController.cpp:477
Here is the call graph for this function:

◆ SetOutputRampRate()

void controllers::PIDController::SetOutputRampRate ( const double  dOutputRampRate)

Mutator for the maximum rate that the output can change per iteration.

Parameters
dOutputRampRate- The maximum ramp rate of the output for the controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
519 {
520 // Assign member variable.
521 m_dOutputRampRate = dOutputRampRate;
522 }

◆ SetOutputFilter()

void controllers::PIDController::SetOutputFilter ( const double  dStrength)

Set a filter on the output to reduce sharp oscillations. 0.1 is likely a good starting point.

Parameters
dStrength- The output filtering strength.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
534 {
535 // Check if the strength is valid.
536 if (dStrength == 0 || numops::Bounded(dStrength, 0.0, 1.0))
537 {
538 // Assign member variable.
539 m_dOutputFilter = dStrength;
540 }
541 }
Here is the call graph for this function:

◆ SetDirection()

void controllers::PIDController::SetDirection ( const bool  bReversed = false)

Set the operating direction of the PID controller. Set true to reverse PID output.

Parameters
bReversed- Whether or not eh PID output should be negated.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
553 {
554 // Assign member variable.
555 m_bReversed = bReversed;
556 }

◆ GetProportional()

double controllers::PIDController::GetProportional ( ) const

Accessor for proportional gain of the PID controller.

Returns
double - dKp proportional gain of the controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
567 {
568 // Return the member variable.
569 return m_dKp;
570 }

◆ GetIntegral()

double controllers::PIDController::GetIntegral ( ) const

Accessor for integral gain of the PID controller.

Returns
double - dKi integral gain of the controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
581 {
582 // Return the member variable.
583 return m_dKi;
584 }

◆ GetDerivative()

double controllers::PIDController::GetDerivative ( ) const

Accessor for derivative gain of the PID controller.

Returns
double - dKd derivative gain of the controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
595 {
596 // Return the member variable.
597 return m_dKd;
598 }

◆ GetFeedforward()

double controllers::PIDController::GetFeedforward ( ) const

Accessor for feedforward gain of the PID controller.

Returns
double - dKff feedforward gain of the controller.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
609 {
610 // Return the member variable.
611 return m_dKff;
612 }

◆ GetSetpoint()

double controllers::PIDController::GetSetpoint ( ) const

Accessor for the current setpoint that the controller is trying to reach.

Returns
double - The current setpoint.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
623 {
624 // Return the member variable.
625 return m_dSetpoint;
626 }

◆ GetMaxSetpointDifference()

double controllers::PIDController::GetMaxSetpointDifference ( ) const

Accessor for the maximum allowable difference the setpoint can be from the actual. Doesn't limit the setpoint, just the max error. 0 = disabled.

Returns
double - The maximum setpoint difference from the actual.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
639 {
640 // Return the member variable.
641 return m_dMaxSetpointDifference;
642 }

◆ GetMaxIntegralEffort()

double controllers::PIDController::GetMaxIntegralEffort ( ) const

Accessor for the maximum contribution of the integral term within the controller. 0 = disabled.

Returns
double - The maximum allowed integral value.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
654 {
655 // Return the member variable.
656 return m_dMaxIEffort;
657 }

◆ GetOutputRampRate()

double controllers::PIDController::GetOutputRampRate ( ) const

Accessor the the maximum allowed output ramp rate of the PID controller.

Returns
double - The max ramp rate of the controller output.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
668 {
669 // Return the member variable.
670 return m_dOutputRampRate;
671 }

◆ GetOutputFilter()

double controllers::PIDController::GetOutputFilter ( ) const

Accessor for the exponential rolling sum filter strength. Used to reduce sharp output oscillations or jumps.

Returns
double - Strength of the output filtering.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
683 {
684 // Return the member variable.
685 return m_dOutputFilter;
686 }

◆ GetReversed()

bool controllers::PIDController::GetReversed ( ) const

Accessor for if the controller outputs are set to be negated or reversed.

Returns
true - The output is negated.
false - The output is not negated.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-19
698 {
699 // Return the member variable.
700 return m_bReversed;
701 }

◆ CheckGainSigns()

void controllers::PIDController::CheckGainSigns ( )
private

To operate correctly, all PID parameters require the same sign.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-17
711 {
712 // Check if the gains are supposed to be reversed.
713 if (m_bReversed)
714 {
715 // All values should be below zero.
716 if (m_dKp > 0)
717 {
718 // Flip sign sign for proportional gain.
719 m_dKi *= -1;
720 }
721 if (m_dKi > 0)
722 {
723 // Flip sign for integral gain.
724 m_dKi *= -1;
725 }
726 if (m_dKd > 0)
727 {
728 // Flip sign for derivative gain.
729 m_dKd *= -1;
730 }
731 if (m_dKff > 0)
732 {
733 // Flip sign for feedforward gain.
734 m_dKff *= -1;
735 }
736 }
737 else
738 {
739 // All values should be above zero.
740 if (m_dKp < 0)
741 {
742 // Flip sign sign for proportional gain.
743 m_dKi *= -1;
744 }
745 if (m_dKi < 0)
746 {
747 // Flip sign for integral gain.
748 m_dKi *= -1;
749 }
750 if (m_dKd < 0)
751 {
752 // Flip sign for derivative gain.
753 m_dKd *= -1;
754 }
755 if (m_dKff < 0)
756 {
757 // Flip sign for feedforward gain.
758 m_dKff *= -1;
759 }
760 }
761 }
Here is the caller graph for this function:

The documentation for this class was generated from the following files: