Skip to content

Commit

Permalink
bump compat DataInterpolations (#68)
Browse files Browse the repository at this point in the history
* Update Project.toml

* up tests

* add IC

* up

* more up

* bump version
  • Loading branch information
baggepinnen committed Aug 1, 2024
1 parent ffbc2a7 commit ee71035
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ControlSystemsMTK"
uuid = "687d7614-c7e5-45fc-bfc3-9ee385575c88"
authors = ["Fredrik Bagge Carlson"]
version = "2.1.0"
version = "2.2.0"

[deps]
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"
Expand All @@ -16,7 +16,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[compat]
ControlSystemsBase = "1.0.1"
DataInterpolations = "3, 4"
DataInterpolations = "3, 4, 5, 6"
ModelingToolkit = "9.0"
ModelingToolkitStandardLibrary = "2"
MonteCarloMeasurements = "1.1"
Expand Down
10 changes: 5 additions & 5 deletions docs/src/batch_linearization.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ eqs = [D(x) ~ v
y.u ~ x]
@named duffing = ODESystem(eqs, t, systems=[y, u])
@named duffing = ODESystem(eqs, t, systems=[y, u], defaults=[u.u => 0])
```

## Batch linearization
Expand Down Expand Up @@ -61,7 +61,7 @@ bodeplot(P, w, legend=:bottomright) # Should look similar to the one above
```

## Controller tuning
Let's also do some controller tuning for the linearized models above. The function `batch_tune` is not really required here, but it shows how we might go about building more sophisticated tools for batch tuning. In this example, we will tune a PID controller using the function [`loopshapingPID`](@ref).
Let's also do some controller tuning for the linearized models above. The function `batch_tune` is not really required here, but it shows how we might go about building more sophisticated tools for batch tuning. In this example, we will tune a PID controller using the function [`loopshapingPID`](@ref). Note, this procedure is not limited to tuning a gain-scheduled PID controller, it should work for gain-scheduling of any LTI controller.
```@example BATCHLIN
function batch_tune(f, Ps)
f.(Ps)
Expand Down Expand Up @@ -119,7 +119,7 @@ for C in Cs
connect(Ci.output, duffing.u)
]
@named closed_loop = ODESystem(eqs, t, systems=[duffing, Ci, fb, ref, F])
prob = ODEProblem(structural_simplify(closed_loop), [], (0.0, 8.0))
prob = ODEProblem(structural_simplify(closed_loop), [F.xd => 0], (0.0, 8.0))
sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8)
plot!(sol, idxs=[duffing.y.u, duffing.u.u], layout=2, lab="")
end
Expand All @@ -133,8 +133,8 @@ eqs = [
connect(duffing.y, Cgs.scheduling_input) # Don't forget to connect the scheduling variable!
]
@named closed_loop = ODESystem(eqs, t, systems=[duffing, Cgs, fb, ref, F])
prob = ODEProblem(structural_simplify(closed_loop), [], (0.0, 8.0))
sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8)
prob = ODEProblem(structural_simplify(closed_loop), [F.xd => 0], (0.0, 8.0))
sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8, initializealg=NoInit())
plot!(sol, idxs=[duffing.y.u, duffing.u.u], l=(2, :red), lab="Gain scheduled")
plot!(sol, idxs=F.output.u, l=(1, :black, :dash, 0.5), lab="Ref")
```
Expand Down
7 changes: 4 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ m1 = 1
m2 = 1
k = 1000 # Spring stiffness
c = 10 # Damping coefficient
@named inertia1 = Inertia(; J = m1)
@named inertia2 = Inertia(; J = m2)
@named inertia1 = Inertia(; J = m1, w=0)
@named inertia2 = Inertia(; J = m2, w=0)
@named spring = Spring(; c = k)
@named damper = Damper(; d = c)
@named torque = Torque(use_support=false)
Expand All @@ -178,7 +178,7 @@ model = SystemModel() |> complete
### Numeric linearization
We can linearize this model numerically using `named_ss`, this produces a `NamedStateSpace{Continuous, Float64}`
```@example LINEAIZE_SYMBOLIC
lsys = named_ss(model, [model.torque.tau.u], [model.inertia1.phi, model.inertia2.phi])
lsys = named_ss(model, [model.torque.tau.u], [model.inertia1.phi, model.inertia2.phi], op = Dict(model.torque.tau.u => 0))
```
### Symbolic linearization
If we instead call `linearize_symbolic` and pass the jacobians into `ss`, we get a `StateSpace{Continuous, Num}`
Expand All @@ -192,6 +192,7 @@ That's pretty cool, but even nicer is to generate some code for this symbolic sy

```@example LINEAIZE_SYMBOLIC
defs = ModelingToolkit.defaults(simplified_sys)
defs = merge(Dict(unknowns(model) .=> 0), defs)
x, pars = ModelingToolkit.get_u0_p(simplified_sys, defs, defs) # Extract the default state and parameter values
fun = Symbolics.build_function(symbolic_sys, unknowns(simplified_sys), ModelingToolkit.parameters(simplified_sys);
Expand Down
14 changes: 10 additions & 4 deletions test/test_ODESystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using ControlSystemsMTK,
ControlSystemsBase, ModelingToolkit, OrdinaryDiffEq, RobustAndOptimalControl
import ModelingToolkitStandardLibrary.Blocks as Blocks
conn = ModelingToolkit.connect
connect = ModelingToolkit.connect
## Test SISO (single input, single output) system
@parameters t

Expand All @@ -22,7 +23,9 @@ fb = structural_simplify(fb0)
# @test length(unknowns(P)) == 3 # 1 + u + y
# @test length(unknowns(C)) == 4 # 2 + u + y

x0 = Pair[loopgain.P.x=>1.0]
x0 = Pair[
collect(loopgain.P.x) .=> 1.0;
]

prob = ODEProblem(fb, x0, (0.0, 10.0))
sol = solve(prob, Rodas5())
Expand Down Expand Up @@ -187,8 +190,8 @@ m2 = 1
k = 1000 # Spring stiffness
c = 10 # Damping coefficient

@named inertia1 = Inertia(; J = m1)
@named inertia2 = Inertia(; J = m2)
@named inertia1 = Inertia(; J = m1, w=0)
@named inertia2 = Inertia(; J = m2, w=0)

@named spring = Spring(; c = k)
@named damper = Damper(; d = c)
Expand Down Expand Up @@ -229,9 +232,12 @@ sys = ss((mats...,)[1:4]...)


defs = ModelingToolkit.defaults(ssys)
sympars = ModelingToolkit.parameters(ssys)
defs = merge(Dict(unknowns(model) .=> 0), defs)
_, p = ModelingToolkit.get_u0_p(ssys, defs, defs)


sympars = ModelingToolkit.parameters(ssys)

fun = Symbolics.build_function(sys, sympars; expression=Val{false}, force_SA=true)
fun(p)
@test @allocated(fun(p)) <= 256
Expand Down
2 changes: 1 addition & 1 deletion test/test_batchlin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ closed_loop_eqs = [
@named closed_loop = ODESystem(closed_loop_eqs, t, systems=[duffing, C, fb, ref, F])

ssys = structural_simplify(closed_loop)
prob = ODEProblem(ssys, [], (0.0, 8.0))
prob = ODEProblem(ssys, [F.xd => 0], (0.0, 8.0))
sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8)
# plot(sol)

Expand Down

0 comments on commit ee71035

Please sign in to comment.