VR-Engage  2.2
Loading...
Searching...
No Matches
pid.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file pid.h
7//! \brief Provides a compact and efficient PID controller implementation
8//!
9//! Small, easy to use PID implementation with advanced controller capability.
10//! Minimal usage:
11//! \code
12//! setPID(p,i,d);
13//! ...looping code...{
14//! output=getOutput(sensorvalue,target);
15//! }
16//! \endcode
17//!
18//! @see
19//! http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-direction/improving-the-beginners-pid-introduction
20
21#include "vreUtil/export.h"
22
23namespace makVre
24{
25//! \brief A compact PID (Proportional-Integral-Derivative) controller class
26//!
27//! This class implements a PID controller with additional features like
28//! feed-forward control, output ramping, setpoint limiting, and output
29//! filtering to prevent oscillations. It provides methods for tuning
30//! individual parameters and configuring various aspects of the controller's
31//! behavior.
33{
34public:
35 //! \brief Constructor with P, I, and D parameters
36 //!
37 //! \param p Proportional gain coefficient
38 //! \param i Integral gain coefficient
39 //! \param d Derivative gain coefficient
40 MiniPID(double p, double i, double d);
41
42 //! \brief Constructor with P, I, D, and F parameters
43 //!
44 //! \param p Proportional gain coefficient
45 //! \param i Integral gain coefficient
46 //! \param d Derivative gain coefficient
47 //! \param f Feed-forward gain coefficient
48 MiniPID(double p, double i, double d, double f);
49
50 //! \brief Sets the proportional gain coefficient
51 //!
52 //! This parameter responds quickly to changes in setpoint and provides most of the
53 //! initial driving force to make corrections. For position-based controllers,
54 //! this is the first parameter to tune, with I second.
55 //!
56 //! \param p New proportional gain value
57 void setP(double p);
58
59 //! \brief Sets the integral gain coefficient
60 //!
61 //! This parameter is used for overcoming disturbances and ensuring that the controller
62 //! always reaches the setpoint. Typically tuned second for position-based modes,
63 //! and third for rate-based modes.
64 //!
65 //! \param i New integral gain value
66 void setI(double i);
67
68 //! \brief Sets the derivative gain coefficient
69 //!
70 //! This parameter responds quickly to large changes in error and helps prevent
71 //! overshoot. Small values prevent the P and I terms from causing overshoot.
72 //!
73 //! \param d New derivative gain value
74 void setD(double d);
75
76 //! \brief Sets the feed-forward gain coefficient
77 //!
78 //! This parameter is excellent for velocity, rate, and other continuous control modes
79 //! where you can expect a rough output value based solely on the setpoint.
80 //! Should not be used in position-based control modes.
81 //!
82 //! \param f New feed-forward gain value
83 void setF(double f);
84
85 //! \brief Sets all three standard PID parameters at once
86 //!
87 //! \param p New proportional gain value
88 //! \param i New integral gain value
89 //! \param d New derivative gain value
90 void setPID(double p, double i, double d);
91
92 //! \brief Sets all four PID parameters including feed-forward at once
93 //!
94 //! \param p New proportional gain value
95 //! \param i New integral gain value
96 //! \param d New derivative gain value
97 //! \param f New feed-forward gain value
98 void setPID(double p, double i, double d, double f);
99
100 //! \brief Sets the maximum output value contributed by the I component
101 //!
102 //! This can be used to prevent large windup issues and make tuning simpler.
103 //!
104 //! \param maximum Maximum I component contribution (in the same units as the output)
105 void setMaxIOutput(double maximum);
106
107 //! \brief Sets symmetric output limits (from -output to +output)
108 //!
109 //! \param output Maximum output magnitude (minimum is set to -output)
110 void setOutputLimits(double output);
111
112 //! \brief Sets asymmetric output limits
113 //!
114 //! \param minimum Minimum output value
115 //! \param maximum Maximum output value
116 void setOutputLimits(double minimum, double maximum);
117
118 //! \brief Sets the direction of the PID controller
119 //!
120 //! \param reversed If true, reverses the PID output
122
123 //! \brief Sets the target setpoint for the PID calculations
124 //!
125 //! \param setpoint The desired target value
126 void setSetpoint(double setpoint);
127
128 //! \brief Resets the controller state
129 //!
130 //! Erases the integral term buildup and removes derivative gain on the next loop.
131 void reset();
132
133 //! \brief Sets the maximum rate the output can increase per cycle
134 //!
135 //! \param rate Maximum change in output per iteration
136 void setOutputRampRate(double rate);
137
138 //! \brief Sets a limit on how far the setpoint can be from the current position
139 //!
140 //! This can simplify tuning by allowing tuning over a small range to apply to a much larger range.
141 //! It limits the reactivity of the P term and restricts the impact of large D term
142 //! during large setpoint adjustments. May increase lag and I term if range is too small.
143 //!
144 //! \param range Maximum allowed setpoint distance from the current position
145 void setSetpointRange(double range);
146
147 //! \brief Sets a filter on the output to reduce sharp oscillations
148 //!
149 //! A value of 0.1 is a good starting point. Larger values smooth P and D oscillations,
150 //! but may force larger I values.
151 //!
152 //! \param strength Filter strength between 0 and 1 (0 = no filter, 1 = infinite filter)
153 void setOutputFilter(double strength);
154
155 //! \brief Calculates the PID output using the last provided setpoint and actual values
156 //!
157 //! \return Calculated output value
158 double getOutput();
159
160 //! \brief Calculates the PID output for a new actual value and the stored setpoint
161 //!
162 //! \param actual Current measured value
163 //! \return Calculated output value
164 double getOutput(double actual);
165
166 //! \brief Calculates the PID output for new actual and setpoint values
167 //!
168 //! This is the main calculation function. It computes all PID terms and applies
169 //! all configured constraints.
170 //!
171 //! \param actual Current measured value
172 //! \param setpoint Desired target value
173 //! \return Calculated output value to drive the actual to the setpoint
174 double getOutput(double actual, double setpoint);
175
176private:
177 //! \brief Clamps a value between minimum and maximum bounds
178 //!
179 //! \param value Value to clamp
180 //! \param min Minimum allowed value
181 //! \param max Maximum allowed value
182 //! \return Clamped value
183 double clamp(double value, double min, double max);
184
185 //! \brief Checks if a value is within bounds (exclusive)
186 //!
187 //! \param value Value to check
188 //! \param min Minimum bound (exclusive)
189 //! \param max Maximum bound (exclusive)
190 //! \return true if min < value < max
191 bool bounded(double value, double min, double max);
192
193 //! \brief Ensures all PID parameters have the correct sign
194 //!
195 //! The correct sign depends on the 'reversed' setting
197
198 //! \brief Initializes controller parameters to default values
199 void init();
200
201 //! \brief Proportional gain coefficient
202 double P;
203
204 //! \brief Integral gain coefficient
205 double I;
206
207 //! \brief Derivative gain coefficient
208 double D;
209
210 //! \brief Feed-forward gain coefficient
211 double F;
212
213 //! \brief Maximum output value contributed by the I component
215
216 //! \brief Maximum error value used for I term calculation
217 double maxError;
218
219 //! \brief Sum of errors for the I term
220 double errorSum;
221
222 //! \brief Maximum allowed output value
223 double maxOutput;
224
225 //! \brief Minimum allowed output value
226 double minOutput;
227
228 //! \brief Target setpoint value
229 double setpoint;
230
231 //! \brief Last measured input value (used for D term)
233
234 //! \brief Flag indicating if this is the first execution
236
237 //! \brief Flag indicating if the controller direction is reversed
239
240 //! \brief Maximum rate the output can increase per cycle
242
243 //! \brief Last calculated output value
245
246 //! \brief Output filter strength (0-1)
248
249 //! \brief Maximum allowed setpoint distance from the current position
251};
252} // namespace makVre
void checkSigns()
Ensures all PID parameters have the correct sign.
void setF(double f)
Sets the feed-forward gain coefficient.
void setD(double d)
Sets the derivative gain coefficient.
void setP(double p)
Sets the proportional gain coefficient.
double maxError
Maximum error value used for I term calculation.
Definition pid.h:217
void setOutputLimits(double output)
Sets symmetric output limits (from -output to +output)
double outputRampRate
Maximum rate the output can increase per cycle.
Definition pid.h:241
double lastOutput
Last calculated output value.
Definition pid.h:244
void setI(double i)
Sets the integral gain coefficient.
double lastActual
Last measured input value (used for D term)
Definition pid.h:232
double getOutput(double actual, double setpoint)
Calculates the PID output for new actual and setpoint values.
MiniPID(double p, double i, double d, double f)
Constructor with P, I, D, and F parameters.
double getOutput(double actual)
Calculates the PID output for a new actual value and the stored setpoint.
double F
Feed-forward gain coefficient.
Definition pid.h:211
void reset()
Resets the controller state.
void setPID(double p, double i, double d, double f)
Sets all four PID parameters including feed-forward at once.
double outputFilter
Output filter strength (0-1)
Definition pid.h:247
void init()
Initializes controller parameters to default values.
double D
Derivative gain coefficient.
Definition pid.h:208
void setPID(double p, double i, double d)
Sets all three standard PID parameters at once.
MiniPID(double p, double i, double d)
Constructor with P, I, and D parameters.
bool firstRun
Flag indicating if this is the first execution.
Definition pid.h:235
double P
Proportional gain coefficient.
Definition pid.h:202
double minOutput
Minimum allowed output value.
Definition pid.h:226
void setDirection(bool reversed)
Sets the direction of the PID controller.
double clamp(double value, double min, double max)
Clamps a value between minimum and maximum bounds.
bool bounded(double value, double min, double max)
Checks if a value is within bounds (exclusive)
double getOutput()
Calculates the PID output using the last provided setpoint and actual values.
void setSetpoint(double setpoint)
Sets the target setpoint for the PID calculations.
void setOutputRampRate(double rate)
Sets the maximum rate the output can increase per cycle.
void setOutputLimits(double minimum, double maximum)
Sets asymmetric output limits.
double setpointRange
Maximum allowed setpoint distance from the current position.
Definition pid.h:250
void setMaxIOutput(double maximum)
Sets the maximum output value contributed by the I component.
double setpoint
Target setpoint value.
Definition pid.h:229
bool reversed
Flag indicating if the controller direction is reversed.
Definition pid.h:238
double maxIOutput
Maximum output value contributed by the I component.
Definition pid.h:214
double maxOutput
Maximum allowed output value.
Definition pid.h:223
double errorSum
Sum of errors for the I term.
Definition pid.h:220
double I
Integral gain coefficient.
Definition pid.h:205
void setSetpointRange(double range)
Sets a limit on how far the setpoint can be from the current position.
void setOutputFilter(double strength)
Sets a filter on the output to reduce sharp oscillations.
T maximum(T x, T y)
Returns what ever is greater, x or y of type T.
Definition fsUtil.h:130
T minimum(T x, T y)
Returns the which ever is lesser, x or y of type T.
Definition fsUtil.h:137
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Include export definitions for this library.
Definition glsVreMessageUtil.h:49