@@ -6,6 +6,7 @@ struct SimResult{O<:Union{SimModel, StateEstimator, PredictiveController}}
6
6
Ŷ_data :: Matrix{Float64} # estimated outputs
7
7
U_data :: Matrix{Float64} # manipulated inputs
8
8
Ud_data:: Matrix{Float64} # manipulated inputs including load disturbances
9
+ Ru_data:: Matrix{Float64} # manipulated input setpoints
9
10
D_data :: Matrix{Float64} # measured disturbances
10
11
X_data :: Matrix{Float64} # plant states
11
12
X̂_data :: Matrix{Float64} # estimated states
@@ -55,7 +56,9 @@ function sim!(
55
56
updatestate! (plant, u, d);
56
57
end
57
58
return SimResult (
58
- T_data, Y_data, U_data, Y_data, U_data, U_data, D_data, X_data, X_data, plant
59
+ T_data, Y_data, U_data, Y_data,
60
+ U_data, U_data, U_data, D_data, X_data, X_data,
61
+ plant
59
62
)
60
63
end
61
64
68
71
<keyword arguments>
69
72
) -> res
70
73
71
- Closed-loop simulation of `estim` estimator for `N` time steps, default to input bumps.
74
+ Closed-loop simulation of `estim` estimator for `N` steps, default to input bumps.
72
75
73
76
See Arguments for the available options. The noises are provided as standard deviations σ
74
77
vectors. The simulated sensor and process noises of `plant` are specified by `y_noise` and
@@ -118,14 +121,15 @@ end
118
121
mpc::PredictiveController,
119
122
N::Int,
120
123
ry = mpc.estim.model.yop .+ 1,
121
- d = mpc.estim.model.dop;
124
+ d = mpc.estim.model.dop,
125
+ ru = mpc.estim.model.uop;
122
126
<keyword arguments>
123
127
) -> res
124
128
125
- Closed-loop simulation of `mpc` controller for `N` time steps, default to setpoint bumps.
129
+ Closed-loop simulation of `mpc` controller for `N` steps, default to output setpoint bumps.
126
130
127
- The output setpoint `` \m athbf{r_y}`` is held constant at `ry`. The keyword arguments are
128
- identical to [`sim!(::StateEstimator, ::Int)`](@ref).
131
+ The output and manipulated input setpoints are held constant at `ry` and `ru`, respectively.
132
+ The keyword arguments are identical to [`sim!(::StateEstimator, ::Int)`](@ref).
129
133
130
134
# Examples
131
135
```julia-repl
@@ -143,10 +147,11 @@ function sim!(
143
147
mpc:: PredictiveController ,
144
148
N:: Int ,
145
149
ry:: Vector = mpc. estim. model. yop .+ 1 ,
146
- d :: Vector = mpc. estim. model. dop;
150
+ d :: Vector = mpc. estim. model. dop,
151
+ ru:: Vector = mpc. estim. model. uop;
147
152
kwargs...
148
153
)
149
- return sim_closedloop! (mpc, mpc. estim, N, ry, d; kwargs... )
154
+ return sim_closedloop! (mpc, mpc. estim, N, ry, d, ru ; kwargs... )
150
155
end
151
156
152
157
" Quick simulation function for `StateEstimator` and `PredictiveController` instances."
@@ -155,7 +160,8 @@ function sim_closedloop!(
155
160
estim:: StateEstimator ,
156
161
N:: Int ,
157
162
u_ry:: Vector ,
158
- d:: Vector ;
163
+ d :: Vector ,
164
+ ru :: Vector = estim. model. uop;
159
165
plant:: SimModel = estim. model,
160
166
u_step :: Vector = zeros (plant. nu),
161
167
u_noise:: Vector = zeros (plant. nu),
@@ -177,6 +183,7 @@ function sim_closedloop!(
177
183
U_Ry_data = Matrix {Float64} (undef, length (u_ry), N)
178
184
U_data = Matrix {Float64} (undef, plant. nu, N)
179
185
Ud_data = Matrix {Float64} (undef, plant. nu, N)
186
+ Ru_data = Matrix {Float64} (undef, plant. nu, N)
180
187
D_data = Matrix {Float64} (undef, plant. nd, N)
181
188
X_data = Matrix {Float64} (undef, plant. nx, N)
182
189
X̂_data = Matrix {Float64} (undef, estim. nx̂, N)
@@ -188,13 +195,14 @@ function sim_closedloop!(
188
195
d = lastd + d_step + d_noise.* randn (plant. nd)
189
196
y = evaloutput (plant, d) + y_step + y_noise.* randn (plant. ny)
190
197
ym = y[estim. i_ym]
191
- u = sim_getu! (est_mpc, u_ry, d, ym)
198
+ u = sim_getu! (est_mpc, u_ry, d, ru, ym)
192
199
ud = u + u_step + u_noise.* randn (plant. nu)
193
200
Y_data[:, i] = y
194
201
Ŷ_data[:, i] = evalŷ (estim, ym, d)
195
202
U_Ry_data[:, i] = u_ry
196
203
U_data[:, i] = u
197
204
Ud_data[:, i] = ud
205
+ Ru_data[:, i] = ru
198
206
D_data[:, i] = d
199
207
X_data[:, i] = plant. x
200
208
X̂_data[:, i] = estim. x̂
@@ -204,17 +212,19 @@ function sim_closedloop!(
204
212
end
205
213
res = SimResult (
206
214
T_data, Y_data, U_Ry_data, Ŷ_data,
207
- U_data, Ud_data, D_data, X_data, X̂_data,
215
+ U_data, Ud_data, Ru_data, D_data, X_data, X̂_data,
208
216
est_mpc
209
217
)
210
218
setstate! (plant, old_x0)
211
219
return res
212
220
end
213
221
214
- " Keep manipulated input `u` unchanged for state estimator simulation."
215
- sim_getu! (:: StateEstimator , u, _ , _ ) = u
216
222
" Compute new `u` for predictive controller simulation."
217
- sim_getu! (mpc:: PredictiveController , ry, d, ym) = moveinput! (mpc, ry, d; ym)
223
+ function sim_getu! (mpc:: PredictiveController , ry, d, ru, ym)
224
+ return moveinput! (mpc, ry, d; R̂u= repeat (ru, mpc. Hp), ym)
225
+ end
226
+ " Keep manipulated input `u` unchanged for state estimator simulation."
227
+ sim_getu! (:: StateEstimator , u, _ , _ , _ ) = u
218
228
219
229
" Plots.jl recipe for `SimResult` objects constructed with `SimModel` objects."
220
230
@recipe function plot (
0 commit comments