From db9482cd59137d25a8c6803479fb9f232cb2f131 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Sat, 8 Mar 2025 11:27:32 -0500 Subject: [PATCH 1/3] doc: minor correction --- src/controller/nonlinmpc.jl | 2 +- src/estimator/mhe/construct.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controller/nonlinmpc.jl b/src/controller/nonlinmpc.jl index 0c7cee9b..269925d8 100644 --- a/src/controller/nonlinmpc.jl +++ b/src/controller/nonlinmpc.jl @@ -525,7 +525,7 @@ Also return vectors with the nonlinear inequality constraint functions `gfuncs`, `∇gfuncs!`, for the associated gradients. Lastly, also return vectors with the nonlinear equality constraint functions `geqfuncs` and gradients `∇geqfuncs!`. -This method is really indicated and I'm not proud of it. That's because of 3 elements: +This method is really intricate and I'm not proud of it. That's because of 3 elements: - These functions are used inside the nonlinear optimization, so they must be type-stable and as efficient as possible. diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index b59279f2..95318138 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -1275,7 +1275,7 @@ Return the nonlinear objective `Jfunc` function, and `∇Jfunc!`, to compute its Also return vectors with the nonlinear inequality constraint functions `gfuncs`, and `∇gfuncs!`, for the associated gradients. -This method is really indicated and I'm not proud of it. That's because of 3 elements: +This method is really intricate and I'm not proud of it. That's because of 3 elements: - These functions are used inside the nonlinear optimization, so they must be type-stable and as efficient as possible. From 54a52aac55ad6421f5f58a31f3ced3662e00f82d Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 10 Mar 2025 18:31:03 -0400 Subject: [PATCH 2/3] changed: `preparestate!` for `SimModel` now receives no additional arg. The old method with `ym` and `d` arguments still works but it's now deprecated. The old method was not making sense since the two arguments were simply ignored. Moreover, it's now consistent with the `updatestate!` function, that is, it receive no measurement arguments for `SimModel`. --- src/sim_model.jl | 10 ++++++++-- test/1_test_sim_model.jl | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sim_model.jl b/src/sim_model.jl index 5a8ec6eb..27c8ed6e 100644 --- a/src/sim_model.jl +++ b/src/sim_model.jl @@ -208,11 +208,17 @@ function initstate!(model::SimModel, u, d=model.buffer.empty) end @doc raw""" - preparestate!(model::SimModel, _ , _ ) -> x + preparestate!(model::SimModel) -> x Do nothing for [`SimModel`](@ref) and return the current model state ``\mathbf{x}(k)``. """ -function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty) +function preparestate!(model::SimModel) + x = model.buffer.x + x .= model.x0 .+ model.xop + return x +end + +@deprecate function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty) x = model.buffer.x x .= model.x0 .+ model.xop return x diff --git a/test/1_test_sim_model.jl b/test/1_test_sim_model.jl index d21eb66b..5d5a77a5 100644 --- a/test/1_test_sim_model.jl +++ b/test/1_test_sim_model.jl @@ -107,7 +107,8 @@ end @test evaloutput(linmodel1, Float64[]) ≈ linmodel1(Float64[]) ≈ [50,30] x = initstate!(linmodel1, [10, 60]) @test evaloutput(linmodel1) ≈ [50 + 19.0, 30 + 7.4] - @test preparestate!(linmodel1, [10, 60]) ≈ x + @test preparestate!(linmodel1) ≈ x # new method + @test preparestate!(linmodel1, [10, 60]) ≈ x # deprecated method @test updatestate!(linmodel1, [10, 60]) ≈ x linmodel2 = LinModel(append(tf(1, [1, 0]), tf(2, [10, 1])), 1.0) x = initstate!(linmodel2, [10, 3]) From cf6096c4fc10bb162219be5a49dbd013b64a5ae3 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 10 Mar 2025 18:52:48 -0400 Subject: [PATCH 3/3] debug: `x_noise` argument in `sim!` now works --- src/plot_sim.jl | 11 ++++++----- src/sim_model.jl | 11 +++++++---- test/4_test_plot_sim.jl | 3 +++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/plot_sim.jl b/src/plot_sim.jl index c524ba5a..8157fbd6 100644 --- a/src/plot_sim.jl +++ b/src/plot_sim.jl @@ -286,7 +286,8 @@ function sim_closedloop!( d = lastd + d_step + d_noise.*randn(plant.nd) y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny) ym = y[estim.i_ym] - preparestate!(est_mpc, ym, d) + x = preparestate!(plant) + x̂ = preparestate!(est_mpc, ym, d) u = sim_getu!(est_mpc, u_ry, d, ru) ud = u + u_step + u_noise.*randn(plant.nu) Y_data[:, i] .= y @@ -296,10 +297,10 @@ function sim_closedloop!( Ud_data[:, i] .= ud Ru_data[:, i] .= ru D_data[:, i] .= d - X_data[:, i] .= plant.x0 .+ plant.xop - X̂_data[:, i] .= estim.x̂0 .+ estim.x̂op - x = updatestate!(plant, ud, d); - x[:] += x_noise.*randn(plant.nx) + X_data[:, i] .= x + X̂_data[:, i] .= x̂ + updatestate!(plant, ud, d); + plant.x0 .+= x_noise.*randn(plant.nx) updatestate!(est_mpc, u, ym, d) end res = SimResult( diff --git a/src/sim_model.jl b/src/sim_model.jl index 27c8ed6e..aaad33f8 100644 --- a/src/sim_model.jl +++ b/src/sim_model.jl @@ -218,10 +218,13 @@ function preparestate!(model::SimModel) return x end -@deprecate function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty) - x = model.buffer.x - x .= model.x0 .+ model.xop - return x +function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty) + Base.depwarn( + "The method preparestate!(model::SimModel, u, d=[]) is deprecated. Use "* + " preparestate!(model::SimModel) instead.", + :preparestate!, + ) + return preparestate!(model) end @doc raw""" diff --git a/test/4_test_plot_sim.jl b/test/4_test_plot_sim.jl index c20f3a1b..e419edac 100644 --- a/test/4_test_plot_sim.jl +++ b/test/4_test_plot_sim.jl @@ -65,6 +65,9 @@ end @test res_man.D_data ≈ res.D_data @test res_man.X_data ≈ res.X_data @test res_man.X̂_data ≈ res.X̂_data + + res2 = sim!(estim, 15, x_noise=[0.1, 0.2, 0.3, 0.4]) + @test !(res2.X_data ≈ res.X_data) end @testitem "StateEstimator Plots" setup=[SetupMPCtests] begin