-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
add utilities and tests for disturbance modeling #3314
Open
baggepinnen
wants to merge
1
commit into
master
Choose a base branch
from
gen_control
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+265
−3
Open
Changes from all commits
Commits
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 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 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 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,215 @@ | ||
#= | ||
This file implements and tests a typical workflow for state estimation with disturbance models | ||
The primary subject of the tests is the analysis-point features and the | ||
analysis-point specific method for `generate_control_function`. | ||
=# | ||
using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, Test | ||
using ModelingToolkitStandardLibrary.Mechanical.Rotational | ||
using ModelingToolkitStandardLibrary.Blocks | ||
using ModelingToolkit: connect | ||
# using Plots | ||
|
||
using ModelingToolkit: t_nounits as t, D_nounits as D | ||
|
||
indexof(sym, syms) = findfirst(isequal(sym), syms) | ||
|
||
## Build the system model ====================================================== | ||
@mtkmodel SystemModel begin | ||
@parameters begin | ||
m1 = 1 | ||
m2 = 1 | ||
k = 10 # Spring stiffness | ||
c = 3 # Damping coefficient | ||
end | ||
@components begin | ||
inertia1 = Inertia(; J = m1, phi = 0, w = 0) | ||
inertia2 = Inertia(; J = m2, phi = 0, w = 0) | ||
spring = Spring(; c = k) | ||
damper = Damper(; d = c) | ||
torque = Torque(use_support = false) | ||
end | ||
@equations begin | ||
connect(torque.flange, inertia1.flange_a) | ||
connect(inertia1.flange_b, spring.flange_a, damper.flange_a) | ||
connect(inertia2.flange_a, spring.flange_b, damper.flange_b) | ||
end | ||
end | ||
|
||
@mtkmodel ModelWithInputs begin | ||
@components begin | ||
input_signal = Blocks.Sine(frequency = 1, amplitude = 1) | ||
disturbance_signal1 = Blocks.Constant(k = 0) | ||
disturbance_signal2 = Blocks.Constant(k = 0) | ||
disturbance_torque1 = Torque(use_support = false) | ||
disturbance_torque2 = Torque(use_support = false) | ||
system_model = SystemModel() | ||
end | ||
@equations begin | ||
connect(input_signal.output, :u, system_model.torque.tau) | ||
connect(disturbance_signal1.output, :d1, disturbance_torque1.tau) | ||
connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) | ||
connect(disturbance_torque1.flange, system_model.inertia1.flange_b) | ||
connect(disturbance_torque2.flange, system_model.inertia2.flange_b) | ||
end | ||
end | ||
|
||
@named model = ModelWithInputs() # Model with load disturbance | ||
ssys = structural_simplify(model) | ||
prob = ODEProblem(ssys, [], (0.0, 10.0)) | ||
sol = solve(prob, Tsit5()) | ||
plot(sol) | ||
|
||
## | ||
using ControlSystemsBase | ||
# cmodel = complete(model) | ||
# P = cmodel.system_model | ||
# lsys = named_ss( | ||
# model, [:u, :d1], [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) | ||
|
||
## | ||
# If we now want to add a disturbance model, we cannot do that since we have already connected a constant to the disturbance input. We have also already used the name `d` for an analysis point, but that might not be an issue since we create an outer model and get a new namespace. | ||
|
||
s = tf("s") | ||
dist(; name) = ODESystem(1 / s; name) | ||
|
||
@mtkmodel SystemModelWithDisturbanceModel begin | ||
@components begin | ||
input_signal = Blocks.Sine(frequency = 1, amplitude = 1) | ||
disturbance_signal1 = Blocks.Constant(k = 0) | ||
disturbance_signal2 = Blocks.Constant(k = 0) | ||
disturbance_torque1 = Torque(use_support = false) | ||
disturbance_torque2 = Torque(use_support = false) | ||
disturbance_model = dist() | ||
system_model = SystemModel() | ||
end | ||
@equations begin | ||
connect(input_signal.output, :u, system_model.torque.tau) | ||
connect(disturbance_signal1.output, :d1, disturbance_model.input) | ||
connect(disturbance_model.output, disturbance_torque1.tau) | ||
connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) | ||
connect(disturbance_torque1.flange, system_model.inertia1.flange_b) | ||
connect(disturbance_torque2.flange, system_model.inertia2.flange_b) | ||
end | ||
end | ||
|
||
@named model_with_disturbance = SystemModelWithDisturbanceModel() | ||
# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here | ||
# lsys2 = named_ss(model_with_disturbance, [:u, :d1], | ||
# [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) | ||
ssys = structural_simplify(model_with_disturbance) | ||
prob = ODEProblem(ssys, [], (0.0, 10.0)) | ||
sol = solve(prob, Tsit5()) | ||
@test SciMLBase.successful_retcode(sol) | ||
# plot(sol) | ||
|
||
## | ||
# Now we only have an integrating disturbance affecting inertia1, what if we want both integrating and direct Gaussian? We'd need a "PI controller" disturbancemodel. If we add the disturbance model (s+1)/s we get the integrating and non-integrating noises being correlated which is fine, it reduces the dimensions of the sigma point by 1. | ||
|
||
dist3(; name) = ODESystem(ss(1 + 10 / s, balance = false); name) | ||
|
||
@mtkmodel SystemModelWithDisturbanceModel begin | ||
@components begin | ||
input_signal = Blocks.Sine(frequency = 1, amplitude = 1) | ||
disturbance_signal1 = Blocks.Constant(k = 0) | ||
disturbance_signal2 = Blocks.Constant(k = 0) | ||
disturbance_torque1 = Torque(use_support = false) | ||
disturbance_torque2 = Torque(use_support = false) | ||
disturbance_model = dist3() | ||
system_model = SystemModel() | ||
|
||
y = Blocks.Add() | ||
angle_sensor = AngleSensor() | ||
output_disturbance = Blocks.Constant(k = 0) | ||
end | ||
@equations begin | ||
connect(input_signal.output, :u, system_model.torque.tau) | ||
connect(disturbance_signal1.output, :d1, disturbance_model.input) | ||
connect(disturbance_model.output, disturbance_torque1.tau) | ||
connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) | ||
connect(disturbance_torque1.flange, system_model.inertia1.flange_b) | ||
connect(disturbance_torque2.flange, system_model.inertia2.flange_b) | ||
|
||
connect(system_model.inertia1.flange_b, angle_sensor.flange) | ||
connect(angle_sensor.phi, y.input1) | ||
connect(output_disturbance.output, :dy, y.input2) | ||
end | ||
end | ||
|
||
@named model_with_disturbance = SystemModelWithDisturbanceModel() | ||
# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here | ||
# lsys3 = named_ss(model_with_disturbance, [:u, :d1], | ||
# [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) | ||
ssys = structural_simplify(model_with_disturbance) | ||
prob = ODEProblem(ssys, [], (0.0, 10.0)) | ||
sol = solve(prob, Tsit5()) | ||
@test SciMLBase.successful_retcode(sol) | ||
# plot(sol) | ||
|
||
## Generate function for an augmented Unscented Kalman Filter ===================== | ||
# temp = open_loop(model_with_disturbance, :d) | ||
outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] | ||
(f_oop1, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( | ||
model_with_disturbance, [:u], [:d1, :d2, :dy], split = false) | ||
|
||
(f_oop2, f_ip2), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( | ||
model_with_disturbance, [:u], [:d1, :d2, :dy], | ||
disturbance_argument = true, split = false) | ||
|
||
measurement = ModelingToolkit.build_explicit_observed_function( | ||
io_sys, outputs, inputs = ModelingToolkit.inputs(io_sys)[1:1]) | ||
measurement2 = ModelingToolkit.build_explicit_observed_function( | ||
io_sys, [io_sys.y.output.u], inputs = ModelingToolkit.inputs(io_sys)[1:1], | ||
disturbance_inputs = ModelingToolkit.inputs(io_sys)[2:end], | ||
disturbance_argument = true) | ||
|
||
op = ModelingToolkit.inputs(io_sys) .=> 0 | ||
x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) | ||
x = zeros(5) | ||
u = zeros(1) | ||
d = zeros(3) | ||
@test f_oop2(x, u, p, t, d) == zeros(5) | ||
@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] | ||
@test measurement2(x, u, p, 0.0, d) == [0] | ||
|
||
# Add to the integrating disturbance input | ||
d = [1, 0, 0] | ||
@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity | ||
@test measurement2(x, u, p, 0.0, d) == [0] | ||
|
||
d = [0, 1, 0] | ||
@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity | ||
@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] | ||
@test measurement2(x, u, p, 0.0, d) == [0] | ||
|
||
d = [0, 0, 1] | ||
@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing | ||
@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] | ||
@test measurement2(x, u, p, 0.0, d) == [1] # We have now disturbed the output | ||
|
||
## Further downstream tests that the functions generated above actually have the properties required to use for state estimation | ||
# | ||
# using LowLevelParticleFilters, SeeToDee | ||
# Ts = 0.001 | ||
# discrete_dynamics = SeeToDee.Rk4(f_oop2, Ts) | ||
# nx = length(x_sym) | ||
# nu = 1 | ||
# nw = 2 | ||
# ny = length(outputs) | ||
# R1 = Diagonal([1e-5, 1e-5]) | ||
# R2 = 0.1 * I(ny) | ||
# op = ModelingToolkit.inputs(io_sys) .=> 0 | ||
# x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) | ||
# d0 = LowLevelParticleFilters.SimpleMvNormal(x0, 10.0I(nx)) | ||
# measurement_model = UKFMeasurementModel{Float64, false, false}(measurement, R2; nx, ny) | ||
# kf = UnscentedKalmanFilter{false, false, true, false}( | ||
# discrete_dynamics, measurement_model, R1, d0; nu, Ts, p) | ||
|
||
# tvec = 0:Ts:sol.t[end] | ||
# u = vcat.(Array(sol(tvec, idxs = P.torque.tau.u))) | ||
# y = collect.(eachcol(Array(sol(tvec, idxs = outputs)) .+ 1e-2 .* randn.())) | ||
|
||
# inds = 1:5805 | ||
# res = forward_trajectory(kf, u, y) | ||
|
||
# plot(res, size = (1000, 1000)); | ||
# plot!(sol, idxs = x_sym, sp = (1:nx)', l = :dash); |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶