100 static constexpr bool PositionalPID = std::is_same_v<Algorithm, Positional>;
101 static constexpr bool HasDeadband = (std::is_same_v<Features, WithDeadband> || ...);
102 static constexpr bool HasIntegralLimit = (std::is_same_v<Features, WithIntegralLimit> || ...);
103 static constexpr bool HasDerivativeOnMeasurement =
104 (std::is_same_v<Features, WithDerivativeOnMeasurement> || ...);
105 static constexpr bool HasDerivativeFilter = (std::is_same_v<Features, WithDerivativeFilter> || ...);
106 static constexpr bool HasOutputFilter = (std::is_same_v<Features, WithOutputFilter> || ...);
107 static constexpr bool HasOutputLimit = (std::is_same_v<Features, WithOutputLimit> || ...);
111 ValueType prev_error{};
112 ValueType prev_prev_error{};
113 ValueType prev_ref{};
114 ValueType prev_measure{};
115 ValueType prev_derivative{};
116 ValueType prev_output{};
123 mutable ValueType
Kp;
124 mutable ValueType
Ki;
125 mutable ValueType
Kd;
136 D_RC(params.DerivativeFilterRC),
O_RC(params.OutputFilterRC)
146 ValueType
compute(ValueType ref, ValueType measure)
148 const ValueType error = ref - measure;
149 if constexpr (HasDeadband)
156 ValueType dt = deltaT.getDeltaMS();
157 ValueType P, I{}, D, output;
158 if constexpr (PositionalPID)
161 ITerm =
Ki * error * dt;
165 P =
Kp * (error - prev_error);
166 ITerm =
Ki * error * dt;
171 if constexpr (HasDerivativeOnMeasurement)
173 ValueType meas_deriv = -(measure - prev_measure) / dt;
174 prev_measure = measure;
177 else if constexpr (PositionalPID)
179 return (error - prev_error) / dt;
183 return (error - 2 * prev_error + prev_prev_error) / dt;
189 if constexpr (HasDerivativeFilter)
191 D = D * dt / (
D_RC + dt) + prev_derivative *
D_RC / (
D_RC + dt);
194 if constexpr (HasIntegralLimit)
196 ValueType temp_Iout = I + ITerm;
197 if (
const ValueType temp_Output = P + I + D; abs(temp_Output) >
MaxOutputVal)
220 if constexpr (HasOutputFilter)
222 output = output * dt / (
O_RC + dt) + prev_output *
O_RC / (
O_RC + dt);
226 if constexpr (HasOutputLimit)
233 prev_output = output;
246 prev_error = ValueType{};
247 prev_prev_error = ValueType{};
248 prev_ref = ValueType{};
249 prev_measure = ValueType{};
250 prev_derivative = ValueType{};
251 prev_output = ValueType{};