Skip to content

Commit d59132b

Browse files
feat: add feedforward controller (#170)
* add feedforward controller * Update doc/controllers/Feedforward2Dof.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent d4d5912 commit d59132b

12 files changed

Lines changed: 276 additions & 175 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Refer to the documentation to quickly integrate and utilize the library's signal
1818
|--------------------------------------------------------------------|----------------------------------------------------------------------|
1919
| [Analysis](doc/analysis/README.md) | FFT, Power Spectral Density, DCT, Window Functions, Signal Detectors |
2020
| [Control Analysis](doc/control_analysis/README.md) | Frequency Response, Root Locus |
21-
| [Controllers](doc/controllers/README.md) | Bang-Bang/Hysteresis, PID, LQR, MPC, Saturation, Rate Limiter, Slew-Limited Saturation |
21+
| [Controllers](doc/controllers/README.md) | Bang-Bang/Hysteresis, PID, LQR, MPC, Saturation, Rate Limiter, Slew-Limited Saturation, Feedforward/2-DOF |
2222
| [Dynamics](doc/dynamics/README.md) | Euler-Lagrange, Newton-Euler, Recursive Newton-Euler, ABA |
2323
| [Estimators](doc/estimators/README.md) | Linear Regression, Yule-Walker (offline), Recursive Least Squares (online) |
2424
| [Filters](doc/filters/README.md) | Kalman, Extended Kalman, Unscented Kalman, FIR, IIR, Exponential Moving Average, Moving Average, Median Filter |

‎ROADMAP.md‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Difficulty legend:
2727

2828
| # | Component | Target module | Difficulty |
2929
|----|------------------------------------------------------|---------------------------|------------|
30-
| 7 | Feedforward / 2-DOF controller | `controllers` | ★★☆☆☆ |
3130
| 8 | Alpha-beta / alpha-beta-gamma filter | `filters/active` | ★★☆☆☆ |
3231
| 9 | Complementary filter | `filters/active` | ★★☆☆☆ |
3332
| 10 | Gain-scheduled controller | `controllers` | ★★☆☆☆ |
@@ -101,12 +100,6 @@ Difficulty legend:
101100

102101
## Tier 2 — Easy ★★☆☆☆
103102

104-
### 7. Feedforward / 2-DOF controller
105-
- **What:** Adds a reference/disturbance feedforward path to an existing feedback loop (two-degree-of-freedom structure).
106-
- **Embedded value:** Improves tracking and disturbance rejection without sacrificing feedback stability margins.
107-
- **Algorithm / paper:** Åström & Murray, *Feedback Systems*, Ch. 12 (Feedforward and 2-DOF design).
108-
- **Reuses:** Existing controller interfaces; composable wrapper.
109-
110103
### 8. Alpha-beta / alpha-beta-gamma filter
111104
- **What:** Fixed-gain steady-state tracker for position/velocity(/acceleration) states.
112105
- **Embedded value:** Delivers most of the benefit of a Kalman filter at a fraction of the cost — no online covariance propagation.

‎doc/controllers/Feedforward2Dof.md‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Feedforward / 2-DOF Controller
2+
3+
## Overview & Motivation
4+
5+
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.
6+
7+
## Mathematical Theory
8+
9+
### Signal Flow
10+
11+
Let $r$ be the reference, $y$ the plant output, and $u$ the actuator command. The 2-DOF law is:
12+
13+
$$u = \text{sat}\!\left(u_{ff}(r) + u_{fb}(r - y),\; u_{min},\; u_{max}\right)$$
14+
15+
where:
16+
- $u_{ff}(r)$ is the feedforward map evaluated on the reference alone (open-loop),
17+
- $u_{fb}(e) = u_{fb}(r - y)$ is the feedback law driven by the tracking error,
18+
- $\text{sat}(\cdot)$ clips the combined command to the actuator range $[u_{min}, u_{max}]$.
19+
20+
### Feedforward Design
21+
22+
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.
23+
24+
### Feedback Design (Independence)
25+
26+
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.
27+
28+
### Saturation
29+
30+
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.
31+
32+
## Complexity Analysis
33+
34+
| Case | Time | Space | Notes |
35+
|---------|--------|--------|----------------------------------------------|
36+
| Best | $O(1)$ | $O(1)$ | Wrapper adds one addition and one clamp |
37+
| Average | $O(1)$ | $O(1)$ | Cost dominated by the injected components |
38+
| Worst | $O(1)$ | $O(1)$ | State lives entirely inside injected objects |
39+
40+
## Step-by-Step Walkthrough
41+
42+
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]$:
43+
44+
1. $u_{ff} = K_{ff} \cdot r = 0.6 \times 0.5 = 0.3$
45+
2. $e = r - y = 0.5 - 0.1 = 0.4$
46+
3. $u_{fb} = K_p \cdot e = 0.5 \times 0.4 = 0.2$
47+
4. $u_{raw} = u_{ff} + u_{fb} = 0.3 + 0.2 = 0.5$
48+
5. $u = \text{sat}(0.5, -1, 1) = 0.5$
49+
50+
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$.
51+
52+
## Pitfalls & Edge Cases
53+
54+
- **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.
55+
- **Feedforward does not aid disturbance rejection.** If $u_{ff}$ is tuned for tracking and a disturbance enters the plant, only $u_{fb}$ responds.
56+
- **Inverse-plant feedforward for non-minimum-phase systems** is non-causal; use a truncated or approximate inverse.
57+
- **Sign convention.** The error passed to feedback is $r - y$, not $y - r$. Swapping signs destabilises positive-gain plants.
58+
59+
## Variants & Generalizations
60+
61+
- **Gain-only feedforward:** $u_{ff} = K_{ff} \cdot r$ — the simplest form, requiring only a scalar.
62+
- **Filter-based feedforward:** a low-pass or derivative filter applied to $r$ before multiplication; smooths the command for actuators with bandwidth limits.
63+
- **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.
64+
- **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.
65+
66+
## Applications
67+
68+
- Motion control: hitting aggressive position or velocity trajectories while maintaining stability against load disturbances.
69+
- Process control: tracking ramp set-points in temperature or flow loops where a simple P or PI controller lags behind.
70+
- Robotics: feed-forward gravity/inertia compensation combined with joint-level PD feedback.
71+
- Power electronics: duty-cycle feedforward in DC-DC converters to cancel input voltage disturbances before the voltage-mode feedback acts.
72+
73+
## Connections to Other Algorithms
74+
75+
- **PID / PidIncremental** is a natural choice for the injected feedback law. The incremental (velocity) form avoids integrator wind-up issues at start-up.
76+
- **SaturationRateLimiter** can replace or augment the output clamp to add slew-rate limiting on the command.
77+
- **Gain-scheduled controller** extends the 2-DOF concept by varying feedforward and feedback gains across operating points.
78+
- **LQR / LQG** provides an optimal state-feedback law for the feedback path; the feedforward term then handles reference shaping independently.
79+
80+
## References & Further Reading
81+
82+
- 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).
83+
- G. F. Franklin, J. D. Powell, and A. Emami-Naeini, *Feedback Control of Dynamic Systems*, 8th ed., Pearson, 2019, Ch. 4.

‎doc/controllers/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Feedback control algorithms for regulating dynamic systems in real time.
1212
| [LQG Controller](Lqg.md) | Linear Quadratic Gaussian — output-feedback optimal control via LQR + Kalman Filter |
1313
| [MPC Controller](Mpc.md) | Model Predictive Controller — receding-horizon optimal control with constraint handling |
1414
| [Linear Time-Invariant Model](LinearTimeInvariant.md) | Discrete-time state-space plant model (A, B, C, D) shared across controllers and filters |
15+
| [Feedforward / 2-DOF Controller](Feedforward2Dof.md) | Feedforward + feedback wrapper for decoupled reference tracking and disturbance rejection |

‎numerical/controllers/implementations/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ target_link_libraries(numerical.controllers.implementations ${NUMERICAL_VISIBILI
1313

1414
target_sources(numerical.controllers.implementations PRIVATE
1515
BangBangHysteresis.hpp
16+
Feedforward2Dof.hpp
1617
Lqg.hpp
1718
Lqr.hpp
1819
Mpc.hpp
@@ -22,6 +23,7 @@ target_sources(numerical.controllers.implementations PRIVATE
2223

2324
numerical_add_coverage_sources(numerical.controllers.implementations
2425
BangBangHysteresis.cpp
26+
Feedforward2Dof.cpp
2527
Lqg.cpp
2628
Lqr.cpp
2729
Mpc.cpp
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "numerical/controllers/implementations/Feedforward2Dof.hpp"
2+
3+
namespace controllers
4+
{
5+
template class Feedforward2Dof<float>;
6+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#if defined(__GNUC__) || defined(__clang__)
4+
#pragma GCC optimize("O3", "fast-math")
5+
#endif
6+
7+
#include "numerical/controllers/implementations/SaturationRateLimiter.hpp"
8+
#include "numerical/math/CompilerOptimizations.hpp"
9+
#include <type_traits>
10+
11+
namespace controllers
12+
{
13+
template<typename T>
14+
class Feedforward
15+
{
16+
public:
17+
static_assert(std::is_floating_point_v<T>, "Feedforward supports floating-point types");
18+
19+
virtual ~Feedforward() = default;
20+
virtual T Evaluate(T reference) const = 0;
21+
};
22+
23+
template<typename T>
24+
class FeedbackLaw
25+
{
26+
public:
27+
static_assert(std::is_floating_point_v<T>, "FeedbackLaw supports floating-point types");
28+
29+
virtual ~FeedbackLaw() = default;
30+
virtual T Process(T error) = 0;
31+
virtual void Reset() = 0;
32+
};
33+
34+
template<typename T>
35+
class Feedforward2Dof
36+
{
37+
public:
38+
static_assert(std::is_floating_point_v<T>, "Feedforward2Dof supports floating-point types");
39+
40+
Feedforward2Dof(Feedforward<T>& ff, FeedbackLaw<T>& fb, Saturation<T> clamp);
41+
42+
OPTIMIZE_FOR_SPEED T Compute(T reference, T measurement);
43+
void Reset();
44+
45+
private:
46+
Feedforward<T>& feedforward;
47+
FeedbackLaw<T>& feedback;
48+
Saturation<T> outputClamp;
49+
};
50+
51+
template<typename T>
52+
Feedforward2Dof<T>::Feedforward2Dof(Feedforward<T>& ff, FeedbackLaw<T>& fb, Saturation<T> clamp)
53+
: feedforward{ ff }
54+
, feedback{ fb }
55+
, outputClamp{ clamp }
56+
{
57+
}
58+
59+
template<typename T>
60+
OPTIMIZE_FOR_SPEED T Feedforward2Dof<T>::Compute(T reference, T measurement)
61+
{
62+
T uFeedforward{ feedforward.Evaluate(reference) };
63+
T error{ reference - measurement };
64+
T uFeedback{ feedback.Process(error) };
65+
return outputClamp.Clamp(uFeedforward + uFeedback);
66+
}
67+
68+
template<typename T>
69+
void Feedforward2Dof<T>::Reset()
70+
{
71+
feedback.Reset();
72+
}
73+
74+
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
75+
extern template class Feedforward2Dof<float>;
76+
#endif
77+
}

‎numerical/controllers/implementations/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_link_libraries(numerical.controllers_test PUBLIC
1010

1111
target_sources(numerical.controllers_test PRIVATE
1212
TestBangBangHysteresis.cpp
13+
TestFeedforward2Dof.cpp
1314
TestLqg.cpp
1415
TestLqr.cpp
1516
TestMpc.cpp
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "numerical/controllers/implementations/Feedforward2Dof.hpp"
2+
#include "numerical/math/Tolerance.hpp"
3+
#include "gmock/gmock.h"
4+
#include "gtest/gtest.h"
5+
6+
namespace
7+
{
8+
class MockFeedforward
9+
: public controllers::Feedforward<float>
10+
{
11+
public:
12+
MOCK_METHOD(float, Evaluate, (float), (const, override));
13+
};
14+
15+
class MockFeedbackLaw
16+
: public controllers::FeedbackLaw<float>
17+
{
18+
public:
19+
MOCK_METHOD(float, Process, (float), (override));
20+
MOCK_METHOD(void, Reset, (), (override));
21+
};
22+
23+
class TestFeedforward2Dof
24+
: public ::testing::Test
25+
{
26+
public:
27+
testing::StrictMock<MockFeedforward> ff;
28+
testing::StrictMock<MockFeedbackLaw> fb;
29+
controllers::Saturation<float> clamp{ -1.0f, 1.0f };
30+
controllers::Feedforward2Dof<float> controller{ ff, fb, clamp };
31+
};
32+
}
33+
34+
TEST_F(TestFeedforward2Dof, sums_feedforward_and_feedback)
35+
{
36+
EXPECT_CALL(ff, Evaluate(0.5f)).WillOnce(testing::Return(0.3f));
37+
EXPECT_CALL(fb, Process(0.4f)).WillOnce(testing::Return(0.2f));
38+
39+
float result{ controller.Compute(0.5f, 0.1f) };
40+
41+
EXPECT_NEAR(result, 0.5f, math::Tolerance<float>());
42+
}
43+
44+
TEST_F(TestFeedforward2Dof, passes_correct_error_to_feedback)
45+
{
46+
EXPECT_CALL(ff, Evaluate(0.4f)).WillOnce(testing::Return(0.0f));
47+
EXPECT_CALL(fb, Process(0.3f)).WillOnce(testing::Return(0.3f));
48+
49+
controller.Compute(0.4f, 0.1f);
50+
}
51+
52+
TEST_F(TestFeedforward2Dof, passes_reference_to_feedforward)
53+
{
54+
EXPECT_CALL(ff, Evaluate(0.6f)).WillOnce(testing::Return(0.0f));
55+
EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.0f));
56+
57+
controller.Compute(0.6f, 0.0f);
58+
}
59+
60+
TEST_F(TestFeedforward2Dof, zero_feedforward_reduces_to_feedback)
61+
{
62+
EXPECT_CALL(ff, Evaluate(testing::_)).WillOnce(testing::Return(0.0f));
63+
EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.4f));
64+
65+
float result{ controller.Compute(0.5f, 0.1f) };
66+
67+
EXPECT_NEAR(result, 0.4f, math::Tolerance<float>());
68+
}
69+
70+
TEST_F(TestFeedforward2Dof, output_is_clamped)
71+
{
72+
EXPECT_CALL(ff, Evaluate(testing::_)).WillOnce(testing::Return(0.9f));
73+
EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(0.9f));
74+
75+
float result{ controller.Compute(1.0f, 0.0f) };
76+
77+
EXPECT_NEAR(result, 1.0f, math::Tolerance<float>());
78+
}
79+
80+
TEST_F(TestFeedforward2Dof, perfect_feedforward_zero_error)
81+
{
82+
EXPECT_CALL(ff, Evaluate(0.7f)).WillOnce(testing::Return(0.7f));
83+
EXPECT_CALL(fb, Process(0.0f)).WillOnce(testing::Return(0.0f));
84+
85+
float result{ controller.Compute(0.7f, 0.7f) };
86+
87+
EXPECT_NEAR(result, 0.7f, math::Tolerance<float>());
88+
}
89+
90+
TEST_F(TestFeedforward2Dof, reset_delegates_to_feedback)
91+
{
92+
EXPECT_CALL(fb, Reset()).Times(1);
93+
94+
controller.Reset();
95+
}
96+
97+
TEST_F(TestFeedforward2Dof, negative_reference_handled)
98+
{
99+
EXPECT_CALL(ff, Evaluate(-0.3f)).WillOnce(testing::Return(-0.2f));
100+
EXPECT_CALL(fb, Process(testing::_)).WillOnce(testing::Return(-0.1f));
101+
102+
float result{ controller.Compute(-0.3f, 0.0f) };
103+
104+
EXPECT_NEAR(result, -0.3f, math::Tolerance<float>());
105+
}

‎roadmap/controllers/Feedforward2Dof/explanation.md‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)