-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_diagram.hpp
More file actions
337 lines (304 loc) · 9.35 KB
/
block_diagram.hpp
File metadata and controls
337 lines (304 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @file block_diagram.hpp
* @brief Block diagram algebra and system interconnections
*
* Provides tools for building complex control systems from basic blocks
*/
#ifndef CPPPLOT_CONTROL_BLOCK_DIAGRAM_HPP
#define CPPPLOT_CONTROL_BLOCK_DIAGRAM_HPP
#include "transfer_function.hpp"
#include "state_space.hpp"
#include <vector>
#include <string>
#include <memory>
#include <functional>
namespace cppplot {
namespace control {
// ============================================================
// BASIC CONNECTIONS
// ============================================================
// Note: series() and parallel() are defined in transfer_function.hpp
/**
* @brief Negative feedback connection
*
* r -->(+)---[G]--->y
* ^ |
* |---[H]<--|
*
* Closed-loop: T = G / (1 + GH)
*/
inline TransferFunction nfeedback(const TransferFunction& G, const TransferFunction& H) {
return feedback(G, H, -1);
}
/**
* @brief Positive feedback connection
* Closed-loop: T = G / (1 - GH)
*/
inline TransferFunction pfeedback(const TransferFunction& G, const TransferFunction& H) {
return feedback(G, H, +1);
}
// ============================================================
// STANDARD CONTROL LOOPS
// ============================================================
/**
* @brief Standard feedback control loop
*
* r -->(+)---[C]---[G]--->y
* ^ |
* |------[H]<----|
*
* @param G Plant transfer function
* @param C Controller transfer function
* @param H Feedback transfer function (default: unity)
* @return Closed-loop transfer function from r to y
*/
inline TransferFunction closed_loop(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction L = C * G; // Loop transfer function
return feedback(L, H);
}
/**
* @brief Standard PID control loop
*
* @param G Plant
* @param Kp Proportional gain
* @param Ki Integral gain
* @param Kd Derivative gain
* @return Closed-loop transfer function
*/
inline TransferFunction pid_loop(
const TransferFunction& G,
double Kp, double Ki, double Kd
) {
// PID: G(s) = Kp + Ki/s + Kd*s = (Kd*s^2 + Kp*s + Ki) / s
TransferFunction C({Kd, Kp, Ki}, {1, 0});
return closed_loop(G, C);
}
/**
* @brief Two degree-of-freedom controller structure
*
* r -->[Cr]-->(+)---[Cy]---[G]--->y
* ^ |
* |-------[H]<----|
*
* @param G Plant
* @param Cr Reference prefilter
* @param Cy Feedback controller
* @param H Feedback (default: unity)
* @return Closed-loop from r to y
*/
inline TransferFunction two_dof_loop(
const TransferFunction& G,
const TransferFunction& Cr,
const TransferFunction& Cy,
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction inner_loop = feedback(Cy * G, H);
return Cr * inner_loop;
}
// ============================================================
// CASCADE CONTROL
// ============================================================
/**
* @brief Cascade (nested) control structure
*
* r -->[C1]-->(+)-->[C2]-->[G2]-->[G1]--->y
* ^ | |
* |------[H2]<---| |
* | |
* |-------[H1]<---------|
*
* @return Closed-loop transfer function
*/
inline TransferFunction cascade_control(
const TransferFunction& G1, // Outer plant
const TransferFunction& G2, // Inner plant
const TransferFunction& C1, // Outer controller
const TransferFunction& C2, // Inner controller
const TransferFunction& H1 = TransferFunction({1}, {1}),
const TransferFunction& H2 = TransferFunction({1}, {1})
) {
// Inner loop first
TransferFunction inner = feedback(C2 * G2, H2);
// Outer loop
return feedback(C1 * inner * G1, H1);
}
// ============================================================
// FEEDFORWARD CONTROL
// ============================================================
/**
* @brief Feedback with feedforward structure
*
* d ----->[Gd]---.
* v
* r -->[Gff]->(+)---[C]-->(+)---[G]--->y
* | ^ |
* | |-------[H]<-------|
* | ^
* `--------->[Cff]----------'
*
* Simplified: r -> y with feedforward compensation
*/
inline TransferFunction feedforward_loop(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& Cff, // Feedforward controller
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction fb_loop = feedback(C * G, H);
TransferFunction ff_path = Cff * G;
// Total: ff_path + fb_loop (assuming they add at output)
return ff_path + fb_loop;
}
// ============================================================
// DISTURBANCE REJECTION
// ============================================================
/**
* @brief Calculate disturbance-to-output transfer function
*
* d
* |
* v
* r -->[C]-->(+)---[G]--->y
* ^ |
* |------[H]<----|
*
* G_yd = G / (1 + GCH) = G * S where S is sensitivity
*/
inline TransferFunction disturbance_to_output(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction one({1}, {1});
TransferFunction S = feedback(one, C * G * H); // Sensitivity
return G * S;
}
/**
* @brief Calculate noise-to-output transfer function
*
* r -->[C]---[G]-->(+)--->y
* ^ ^
* | n (noise)
* |--[H]<----|
*
* G_yn = -GCH / (1 + GCH) = -T (for noise after output)
*/
inline TransferFunction noise_to_output(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction T = feedback(G * C * H, TransferFunction({1}, {1}));
return T * (-1);
}
// ============================================================
// STABILITY ANALYSIS OF LOOPS
// ============================================================
/**
* @brief Check closed-loop stability
*/
inline bool is_closed_loop_stable(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& H = TransferFunction({1}, {1})
) {
TransferFunction T = closed_loop(G, C, H);
return T.isStable();
}
/**
* @brief Get loop transfer function
*/
inline TransferFunction loop_transfer(
const TransferFunction& G,
const TransferFunction& C,
const TransferFunction& H = TransferFunction({1}, {1})
) {
return G * C * H;
}
// ============================================================
// SPECIAL CONFIGURATIONS
// ============================================================
/**
* @brief Smith predictor for time-delay compensation
*
* For plant G(s) = G0(s) * e^(-Ls)
* Smith predictor uses model G0m and delay estimate Lm
*
* @param G0 Plant without delay (model)
* @param C Controller designed for G0
* @param L Delay estimate (used conceptually - actual delay not implemented)
* @return Equivalent closed-loop without delay effect
*/
inline TransferFunction smith_predictor_ideal(
const TransferFunction& G0,
const TransferFunction& C
) {
// Ideal case: closed-loop behaves as if no delay
// T = CG0 / (1 + CG0)
return feedback(C * G0, TransferFunction({1}, {1}));
}
/**
* @brief Internal Model Control (IMC) structure
*
* IMC controller: Cimc such that closed-loop is stable
* For perfect model: T = G * Cimc
*
* Convert IMC to classical: C = Cimc / (1 - G*Cimc)
*/
inline TransferFunction imc_to_classical(
const TransferFunction& G_model,
const TransferFunction& Cimc
) {
TransferFunction one({1}, {1});
TransferFunction denom = one - G_model * Cimc;
// C = Cimc / (1 - G*Cimc)
return TransferFunction(Cimc.num * denom.den, Cimc.den * denom.num);
}
/**
* @brief Design IMC controller for first-order plant
* G(s) = K / (tau*s + 1)
* Cimc(s) = (tau*s + 1) / (K * (lambda*s + 1))
*
* @param K Plant gain
* @param tau Plant time constant
* @param lambda Desired closed-loop time constant
*/
inline TransferFunction imc_first_order(double K, double tau, double lambda) {
// Cimc = (tau*s + 1) / (K * (lambda*s + 1))
return TransferFunction({tau, 1}, {K * lambda, K});
}
// ============================================================
// LOOP SHAPING HELPERS
// ============================================================
/**
* @brief Calculate required controller gain for desired crossover
*
* Find K such that |K * G(jwc)| = 1
*/
inline double gain_for_crossover(
const TransferFunction& G,
double wc
) {
double Gwc = G.mag(wc);
if (Gwc < 1e-15) return std::numeric_limits<double>::infinity();
return 1.0 / Gwc;
}
/**
* @brief Calculate required phase lead for desired phase margin
*/
inline double required_phase_lead(
const TransferFunction& G,
double wc,
double desired_PM
) {
double current_phase = G.phase_deg(wc);
double current_PM = 180 + current_phase;
return desired_PM - current_PM;
}
} // namespace control
} // namespace cppplot
#endif // CPPPLOT_CONTROL_BLOCK_DIAGRAM_HPP