Repository navigation
feat: add feedforward controller #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gabrielfrasantos
merged 3 commits into
main
from
feature/add-feedforward-2-dof-controller
Jul 26, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Feedforward / 2-DOF Controller | ||
|
|
||
| ## Overview & Motivation | ||
|
|
||
| A single feedback loop must balance two competing demands: fast reference tracking and robust rejection of disturbances. Tightening the feedback gains for faster tracking reduces stability margins; relaxing them for robustness slows the response. The two-degree-of-freedom (2-DOF) structure resolves this conflict by separating the two tasks into independent paths. A feedforward path shapes how the output follows the reference, while the feedback path corrects errors and rejects disturbances. Because the feedforward term is open-loop, it can be tuned for tracking without affecting the closed-loop stability margins at all. | ||
|
|
||
| ## Mathematical Theory | ||
|
|
||
| ### Signal Flow | ||
|
|
||
| Let $r$ be the reference, $y$ the plant output, and $u$ the actuator command. The 2-DOF law is: | ||
|
|
||
| $$u = \text{sat}\!\left(u_{ff}(r) + u_{fb}(r - y),\; u_{min},\; u_{max}\right)$$ | ||
|
|
||
| where: | ||
| - $u_{ff}(r)$ is the feedforward map evaluated on the reference alone (open-loop), | ||
| - $u_{fb}(e) = u_{fb}(r - y)$ is the feedback law driven by the tracking error, | ||
| - $\text{sat}(\cdot)$ clips the combined command to the actuator range $[u_{min}, u_{max}]$. | ||
|
|
||
| ### Feedforward Design | ||
|
|
||
| The ideal feedforward is an inverse of the plant model. For a linear plant $P(z)$, the ideal map is $u_{ff}(r) = P^{-1}(z)\, r$, yielding perfect steady-state tracking. A simpler approximation is a scalar gain $K_{ff}$ tuned to match the DC plant gain: $u_{ff}(r) = K_{ff} \cdot r$. Non-minimum-phase plants require a causal approximation since the exact inverse is non-causal. | ||
|
|
||
| ### Feedback Design (Independence) | ||
|
|
||
| The closed-loop characteristic equation depends only on the feedback law and the plant; $u_{ff}$ does not appear in it. Stability margins, bandwidth, and disturbance rejection are therefore set entirely by $u_{fb}$. This decoupling is the defining property of the 2-DOF structure. | ||
|
|
||
| ### Saturation | ||
|
|
||
| The output clamp $\text{sat}(u, u_{min}, u_{max})$ acts on the combined command to protect the actuator. When the sum saturates, both the feedforward and feedback contributions are implicitly reduced. | ||
|
|
||
| ## Complexity Analysis | ||
|
|
||
| | Case | Time | Space | Notes | | ||
| |---------|--------|--------|----------------------------------------------| | ||
| | Best | $O(1)$ | $O(1)$ | Wrapper adds one addition and one clamp | | ||
| | Average | $O(1)$ | $O(1)$ | Cost dominated by the injected components | | ||
| | Worst | $O(1)$ | $O(1)$ | State lives entirely inside injected objects | | ||
|
|
||
| ## Step-by-Step Walkthrough | ||
|
|
||
| Given $r = 0.5$, $y = 0.1$, feedforward gain $K_{ff} = 0.6$, and a proportional feedback with gain $K_p = 0.5$, clamped to $[-1, 1]$: | ||
|
|
||
| 1. $u_{ff} = K_{ff} \cdot r = 0.6 \times 0.5 = 0.3$ | ||
| 2. $e = r - y = 0.5 - 0.1 = 0.4$ | ||
| 3. $u_{fb} = K_p \cdot e = 0.5 \times 0.4 = 0.2$ | ||
| 4. $u_{raw} = u_{ff} + u_{fb} = 0.3 + 0.2 = 0.5$ | ||
| 5. $u = \text{sat}(0.5, -1, 1) = 0.5$ | ||
|
|
||
| If the system were already tracking perfectly ($y = r = 0.5$), then $e = 0$, $u_{fb} = 0$, and the feedforward alone supplies the command: $u = K_{ff} \cdot 0.5 = 0.3$. | ||
|
|
||
| ## Pitfalls & Edge Cases | ||
|
|
||
| - **Saturation interacts with both terms.** When the actuator saturates, the feedback integrator (if used) can wind up. Anti-windup logic in the feedback law is recommended. | ||
| - **Feedforward does not aid disturbance rejection.** If $u_{ff}$ is tuned for tracking and a disturbance enters the plant, only $u_{fb}$ responds. | ||
| - **Inverse-plant feedforward for non-minimum-phase systems** is non-causal; use a truncated or approximate inverse. | ||
| - **Sign convention.** The error passed to feedback is $r - y$, not $y - r$. Swapping signs destabilises positive-gain plants. | ||
|
|
||
| ## Variants & Generalizations | ||
|
|
||
| - **Gain-only feedforward:** $u_{ff} = K_{ff} \cdot r$ — the simplest form, requiring only a scalar. | ||
| - **Filter-based feedforward:** a low-pass or derivative filter applied to $r$ before multiplication; smooths the command for actuators with bandwidth limits. | ||
| - **Inverse-plant feedforward:** uses a model of the plant to produce the exact command required; maximises tracking performance at the cost of model accuracy sensitivity. | ||
| - **Model-reference 2-DOF:** the feedforward block is an explicit reference model; the feedback corrects deviations from the model response rather than from the raw reference. | ||
|
|
||
| ## Applications | ||
|
|
||
| - Motion control: hitting aggressive position or velocity trajectories while maintaining stability against load disturbances. | ||
| - Process control: tracking ramp set-points in temperature or flow loops where a simple P or PI controller lags behind. | ||
| - Robotics: feed-forward gravity/inertia compensation combined with joint-level PD feedback. | ||
| - Power electronics: duty-cycle feedforward in DC-DC converters to cancel input voltage disturbances before the voltage-mode feedback acts. | ||
|
|
||
| ## Connections to Other Algorithms | ||
|
|
||
| - **PID / PidIncremental** is a natural choice for the injected feedback law. The incremental (velocity) form avoids integrator wind-up issues at start-up. | ||
| - **SaturationRateLimiter** can replace or augment the output clamp to add slew-rate limiting on the command. | ||
| - **Gain-scheduled controller** extends the 2-DOF concept by varying feedforward and feedback gains across operating points. | ||
| - **LQR / LQG** provides an optimal state-feedback law for the feedback path; the feedforward term then handles reference shaping independently. | ||
|
|
||
| ## References & Further Reading | ||
|
|
||
| - K. J. Åström and R. M. Murray, *Feedback Systems: An Introduction for Scientists and Engineers*, Princeton University Press, 2008, Ch. 12 (Feedforward and 2-DOF Design). | ||
| - G. F. Franklin, J. D. Powell, and A. Emami-Naeini, *Feedback Control of Dynamic Systems*, 8th ed., Pearson, 2019, Ch. 4. |
|
gabrielfrasantos marked this conversation as resolved.
Resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #include "numerical/controllers/implementations/Feedforward2Dof.hpp" | ||
|
|
||
| namespace controllers | ||
| { | ||
| template class Feedforward2Dof<float>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #pragma once | ||
|
|
||
| #if defined(__GNUC__) || defined(__clang__) | ||
| #pragma GCC optimize("O3", "fast-math") | ||
| #endif | ||
|
|
||
| #include "numerical/controllers/implementations/SaturationRateLimiter.hpp" | ||
| #include "numerical/math/CompilerOptimizations.hpp" | ||
| #include <type_traits> | ||
|
|
||
| namespace controllers | ||
| { | ||
| template<typename T> | ||
| class Feedforward | ||
| { | ||
| public: | ||
| static_assert(std::is_floating_point_v<T>, "Feedforward supports floating-point types"); | ||
|
|
||
| virtual ~Feedforward() = default; | ||
| virtual T Evaluate(T reference) const = 0; | ||
| }; | ||
|
|
||
| template<typename T> | ||
| class FeedbackLaw | ||
| { | ||
| public: | ||
| static_assert(std::is_floating_point_v<T>, "FeedbackLaw supports floating-point types"); | ||
|
|
||
| virtual ~FeedbackLaw() = default; | ||
| virtual T Process(T error) = 0; | ||
| virtual void Reset() = 0; | ||
| }; | ||
|
|
||
| template<typename T> | ||
| class Feedforward2Dof | ||
| { | ||
| public: | ||
| static_assert(std::is_floating_point_v<T>, "Feedforward2Dof supports floating-point types"); | ||
|
|
||
| Feedforward2Dof(Feedforward<T>& ff, FeedbackLaw<T>& fb, Saturation<T> clamp); | ||
|
|
||
| OPTIMIZE_FOR_SPEED T Compute(T reference, T measurement); | ||
| void Reset(); | ||
|
|
||
| private: | ||
| Feedforward<T>& feedforward; | ||
| FeedbackLaw<T>& feedback; | ||
| Saturation<T> outputClamp; | ||
| }; | ||
|
|
||
| template<typename T> | ||
| Feedforward2Dof<T>::Feedforward2Dof(Feedforward<T>& ff, FeedbackLaw<T>& fb, Saturation<T> clamp) | ||
| : feedforward{ ff } | ||
| , feedback{ fb } | ||
| , outputClamp{ clamp } | ||
| { | ||
| } | ||
|
|
||
| template<typename T> | ||
| OPTIMIZE_FOR_SPEED T Feedforward2Dof<T>::Compute(T reference, T measurement) | ||
| { | ||
| T uFeedforward{ feedforward.Evaluate(reference) }; | ||
| T error{ reference - measurement }; | ||
| T uFeedback{ feedback.Process(error) }; | ||
| return outputClamp.Clamp(uFeedforward + uFeedback); | ||
| } | ||
|
|
||
| template<typename T> | ||
| void Feedforward2Dof<T>::Reset() | ||
| { | ||
| feedback.Reset(); | ||
| } | ||
|
|
||
| #ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD | ||
| extern template class Feedforward2Dof<float>; | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
numerical/controllers/implementations/test/TestFeedforward2Dof.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #include "numerical/controllers/implementations/Feedforward2Dof.hpp" | ||
| #include "numerical/math/Tolerance.hpp" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace | ||
| { | ||
| class MockFeedforward | ||
| : public controllers::Feedforward<float> | ||
| { | ||
| public: | ||
| MOCK_METHOD(float, Evaluate, (float), (const, override)); | ||
| }; | ||
|
|
||
| class MockFeedbackLaw | ||
| : public controllers::FeedbackLaw<float> | ||
| { | ||
| public: | ||
| MOCK_METHOD(float, Process, (float), (override)); | ||
| MOCK_METHOD(void, Reset, (), (override)); | ||
| }; | ||
|
|
||
| class TestFeedforward2Dof | ||
| : public ::testing::Test | ||
| { | ||
| public: | ||
| testing::StrictMock<MockFeedforward> ff; | ||
| testing::StrictMock<MockFeedbackLaw> fb; | ||
| controllers::Saturation<float> clamp{ -1.0f, 1.0f }; | ||
| controllers::Feedforward2Dof<float> controller{ ff, fb, clamp }; | ||
| }; | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, sums_feedforward_and_feedback) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(0.5f)).WillOnce(testing::Return(0.3f)); | ||
| EXPECT_CALL(fb, Process(0.4f)).WillOnce(testing::Return(0.2f)); | ||
|
|
||
| float result{ controller.Compute(0.5f, 0.1f) }; | ||
|
|
||
| EXPECT_NEAR(result, 0.5f, math::Tolerance<float>()); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, passes_correct_error_to_feedback) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(0.4f)).WillOnce(testing::Return(0.0f)); | ||
| EXPECT_CALL(fb, Process(0.3f)).WillOnce(testing::Return(0.3f)); | ||
|
|
||
| controller.Compute(0.4f, 0.1f); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, passes_reference_to_feedforward) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(0.6f)).WillOnce(testing::Return(0.0f)); | ||
| EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.0f)); | ||
|
|
||
| controller.Compute(0.6f, 0.0f); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, zero_feedforward_reduces_to_feedback) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(testing::_)).WillOnce(testing::Return(0.0f)); | ||
| EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.4f)); | ||
|
|
||
| float result{ controller.Compute(0.5f, 0.1f) }; | ||
|
|
||
| EXPECT_NEAR(result, 0.4f, math::Tolerance<float>()); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, output_is_clamped) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(testing::_)).WillOnce(testing::Return(0.9f)); | ||
| EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.9f)); | ||
|
|
||
| float result{ controller.Compute(1.0f, 0.0f) }; | ||
|
|
||
| EXPECT_NEAR(result, 1.0f, math::Tolerance<float>()); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, perfect_feedforward_zero_error) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(0.7f)).WillOnce(testing::Return(0.7f)); | ||
| EXPECT_CALL(fb, Process(0.0f)).WillOnce(testing::Return(0.0f)); | ||
|
|
||
| float result{ controller.Compute(0.7f, 0.7f) }; | ||
|
|
||
| EXPECT_NEAR(result, 0.7f, math::Tolerance<float>()); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, reset_delegates_to_feedback) | ||
| { | ||
| EXPECT_CALL(fb, Reset()).Times(1); | ||
|
|
||
| controller.Reset(); | ||
| } | ||
|
|
||
| TEST_F(TestFeedforward2Dof, negative_reference_handled) | ||
| { | ||
| EXPECT_CALL(ff, Evaluate(-0.3f)).WillOnce(testing::Return(-0.2f)); | ||
| EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(-0.1f)); | ||
|
|
||
| float result{ controller.Compute(-0.3f, 0.0f) }; | ||
|
|
||
| EXPECT_NEAR(result, -0.3f, math::Tolerance<float>()); | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.