GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
control.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_CONTROL_HPP
5#define GREM_CORE_CONTROL_HPP
6
8
9namespace grem {
10
18template <typename T>
20public:
24 constexpr PDController() = default;
25
31 constexpr PDController(T error)
32 : error(error) {}
33
37 constexpr void reset() {
38 error = {};
39 }
40
46 constexpr void reset(T newError) {
47 error = newError;
48 }
49
64 template <typename Coefficient>
65 [[nodiscard]] constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient derivativeGain) {
66 const T currentError = newSignalError;
67 const T changeInError = currentError - error;
68 const auto result = currentError * proportionalGain + changeInError * derivativeGain;
69 error = currentError;
70 return result;
71 }
72
73private:
74 T error{};
75};
76
84template <typename T>
86public:
90 constexpr PIController() = default;
91
98 : integral(integral) {}
99
103 constexpr void reset() {
104 integral = {};
105 }
106
112 constexpr void reset(T newIntegral) {
113 integral = newIntegral;
114 }
115
130 template <typename Coefficient>
131 [[nodiscard]] constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient integralGain) {
132 const T currentError = newSignalError;
133 const T accumulatedError = integral + currentError;
134 const auto result = currentError * proportionalGain + accumulatedError * integralGain;
135 integral = accumulatedError;
136 return result;
137 }
138
139private:
140 T integral{};
141};
142
150template <typename T>
152public:
156 constexpr PIDController() = default;
157
164 constexpr PIDController(T error, T integral)
165 : error(error)
166 , integral(integral) {}
167
171 constexpr void reset() {
172 error = {};
173 integral = {};
174 }
175
182 constexpr void reset(T newError, T newIntegral) {
183 error = newError;
184 integral = newIntegral;
185 }
186
192 constexpr void resetError(T newError = {}) {
193 error = newError;
194 }
195
201 constexpr void resetIntegral(T newIntegral = {}) {
202 integral = newIntegral;
203 }
204
222 template <typename Coefficient>
223 [[nodiscard]] constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient integralGain, Coefficient derivativeGain) {
224 const T currentError = newSignalError;
225 const T accumulatedError = integral + currentError;
226 const T changeInError = currentError - error;
227 const auto result = currentError * proportionalGain + accumulatedError * integralGain + changeInError * derivativeGain;
228 error = currentError;
229 integral = accumulatedError;
230 return result;
231 }
232
233private:
234 T error{};
235 T integral{};
236};
237
238} // namespace grem
239
240#endif
constexpr PDController(T error)
Construct a PD controller with specific initial values.
Definition control.hpp:31
constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient derivativeGain)
Update the controller state and get the next control variable value to use in order to steer the proc...
Definition control.hpp:65
constexpr PDController()=default
Default-construct a PD controller.
constexpr void reset(T newError)
Reset the PD controller to a specific value.
Definition control.hpp:46
constexpr void reset()
Reset the PD controller to the default state.
Definition control.hpp:37
constexpr void reset(T newIntegral)
Reset the PI controller to a specific value.
Definition control.hpp:112
constexpr PIController(T integral)
Construct a PI controller with specific initial values.
Definition control.hpp:97
constexpr void reset()
Reset the PI controller to the default state.
Definition control.hpp:103
constexpr PIController()=default
Default-construct a PI controller.
constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient integralGain)
Update the controller state and get the next control variable value to use in order to steer the proc...
Definition control.hpp:131
constexpr PIDController(T error, T integral)
Construct a PID controller with specific initial values.
Definition control.hpp:164
constexpr void reset(T newError, T newIntegral)
Reset the PID controller to specific values.
Definition control.hpp:182
constexpr void reset()
Reset the PID controller to the default state.
Definition control.hpp:171
constexpr PIDController()=default
Default-construct a PID controller.
constexpr void resetIntegral(T newIntegral={})
Reset the PID controller's accumulated integral to a specific value.
Definition control.hpp:201
constexpr auto update(T newSignalError, Coefficient proportionalGain, Coefficient integralGain, Coefficient derivativeGain)
Update the controller state and get the next control variable value to use in order to steer the proc...
Definition control.hpp:223
constexpr void resetError(T newError={})
Reset the PID controller's latest error to a specific value.
Definition control.hpp:192
Definition concepts.hpp:67
Definition aliases.hpp:9