Skip to content
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

Remove stale connect and use named arguments #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/complicated_feedback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ connections = [
]
w1 = [:uF]

G = ROC.connect([F, R, C, P, addP, addC], connections; w1)
G = ROC.connect([F, R, C, P, addP, addC], connections; w1=w1)


@test sminreal(G[:yF, :uF].sys) ≈ F.sys
Expand Down Expand Up @@ -75,7 +75,7 @@ Sum = named_ss(ss([I(2) -I(2)]), u=[:r1, :r2, :y1, :y2], y=:e)
systems = [G,C,Sum]
u1 = [:r1, :r2]
y1 = [:y1, :y2]
T = ROC.connect(systems; u1, y1, w1=u1)
T = ROC.connect(systems, y1 .=> u1; w1=u1)


## manual
Expand Down
57 changes: 27 additions & 30 deletions src/named_systems2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,35 +380,9 @@ end

ControlSystemsBase.feedback(s1::NamedStateSpace{T}, s2::AbstractStateSpace{T}; kwargs...) where {T <: CS.TimeEvolution} = feedback(s1, named_ss(s2); kwargs...)

function connect(systems; u1::Vector{Symbol}, y1::Vector{Symbol}, w1, z1 = (:), verbose = true, kwargs...)
full = append(systems...)
@assert length(y1) == length(u1)
@check_unique u1 "Connected inputs not unique. If you want to connect several signals to the same input, use a summation node, e.g., named_ss(ss([1 1]), u=[:u1, :u2], y=:usum)"
@check_unique full.u "system inputs"
@check_unique full.y "system outputs"

if verbose
leftover_inputs = setdiff(full.u, [u1; w1])
isempty(leftover_inputs) || @warn("The following inputs were unconnected $leftover_inputs, ignore this warning if you rely on prefix matching")
leftover_outputs = setdiff(full.y, z1 == (:) ? y1 : [y1; z1])
isempty(leftover_outputs) || @warn("The following outputs were unconnected $leftover_outputs, ignore this warning if you rely on prefix matching")
end


z2 = []
w2 = []

# Connections
y2 = (:)
u2 = (:)

fb = named_ss(ss(I(length(y1)), full.timeevol))
G = feedback(full, fb; z1, z2, w1, w2, u1, u2, y1, y2, pos_feedback=true, kwargs...)
end


"""
connect(systems, connections; w1, z1 = (:), verbose = true, kwargs...)
connect(systems, connections, w1, z1 = (:); verbose = true, kwargs...)

Create block connections using named inputs and outputs.

Expand Down Expand Up @@ -459,13 +433,36 @@ connections = [
]
w1 = [:uF] # External inputs

G = connect([F, R, C, P, addP, addC], connections; w1)
G = connect([F, R, C, P, addP, addC], connections, w1)
```

If an external input is to be connected to multiple points, use a `splitter` to split up the signal into a set of unique names which are then used in the connections.
"""
function connect(systems, pairs::AbstractVector{<:Pair}; kwargs...)
connect(systems; u1 = last.(pairs), y1 = first.(pairs), kwargs...)
function connect(systems, pairs::AbstractVector{<:Pair}; w1, z1 = (:), verbose = true, kwargs...)
full = append(systems...)
u1 = last.(pairs)
y1 = first.(pairs)
@assert length(y1) == length(u1)
@check_unique u1 "Connected inputs not unique. If you want to connect several signals to the same input, use a summation node, e.g., named_ss(ss([1 1]), u=[:u1, :u2], y=:usum)"
@check_unique full.u "system inputs"
@check_unique full.y "system outputs"

if verbose
leftover_inputs = setdiff(full.u, [u1; w1])
isempty(leftover_inputs) || @warn("The following inputs were unconnected $leftover_inputs, ignore this warning if you rely on prefix matching")
leftover_outputs = setdiff(full.y, z1 == (:) ? y1 : [y1; z1])
isempty(leftover_outputs) || @warn("The following outputs were unconnected $leftover_outputs, ignore this warning if you rely on prefix matching")
end

z2 = []
w2 = []

# Connections
y2 = (:)
u2 = (:)

fb = named_ss(ss(I(length(y1)), full.timeevol))
G = feedback(full, fb; z1, z2, w1, w2, u1, u2, y1, y2, pos_feedback=true, kwargs...)
end


Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ using Test
include("test_canonical_forms.jl")
end

@testset "complicated feedback exmaple" begin
@testset "complicated feedback example" begin
@info "Testing complicated feedback exmaple"
include("../examples/complicated_feedback.jl")
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_extendedstatespace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ S = sumblock("Ku = r - Gy", n=3)
z1 = [G.y; K.y]
w1 = :r^3
connections = [K.y .=> G.u; G.y .=> G.y; K.u .=> K.u]
Gcl2 = connect([G, K, S], connections; z1, w1)
Gcl2 = connect([G, K, S], connections; z1=z1, w1=w1)

@test linfnorm(minreal(Gcl1 - Gcl2.sys))[1] < sqrt(eps())

Expand Down
2 changes: 1 addition & 1 deletion test/test_manual_hinf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ z1 = [ # External outputs
:e, :uw, :y
];

G = connect([P,We,Wu,Wd,sumP,split_u], connections; z1, w1)
G = connect([P,We,Wu,Wd,sumP,split_u], connections; z1=z1, w1=w1)

Gsyn = partition(G, u = [:u], y = [:y]) # You can provide either u or w, and either y or z
K, γ, info = hinfsynthesize(Gsyn, γrel=1.001, interval = (0.1, 20), transform=false)
Expand Down
2 changes: 1 addition & 1 deletion test/test_named_systems2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ s2 = named_ss(G2, x = [:z], u = [:u], y=[:y2])

connections = [:y1 => :u, :y2 => :u1]
w1 = [:u2]
G = connect([s1,s2], connections; w1)
G = connect([s1,s2], connections; w1=w1)
@test G.u == w1
@test G.y == :y^2
@test G.sys == ss(ones(2,2), [1,0], I(2), 0)
Expand Down
Loading