Skip to content

Commit

Permalink
add_output for named ss
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jan 6, 2025
1 parent f8f9037 commit 9e6a979
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/named_systems2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ end
"""
measure(s::NamedStateSpace, names)
Return a system with specified states as measurement outputs.
Return a system with specified state variables as measurement outputs.
See also [`add_output`](@ref).
"""
function measure(s::NamedStateSpace, names)
inds = names2indices(names, s.x)
Expand Down Expand Up @@ -840,6 +842,27 @@ function CS.append(systems::NamedStateSpace...; kwargs...)
end


"""
add_output(sys::NamedStateSpace, C2::AbstractArray, D2 = 0; y)
Add outputs to `sys` corresponding to the output matrix `C2` and the feedthrough matrix `D2` to the system `sys`.
# Arguments:
- `y`: The names used for the new outputs. If not provided, the names will be generated automatically.
See also [`measure`](@ref) for a simpler way to output state variables.
"""
function CS.add_output(sys::NamedStateSpace, C2::AbstractArray, D2=0; y = [Symbol("y_$i") for i in (1:size(C2, 1)) .+ sys.ny])
T = promote_type(CS.numeric_type(sys), eltype(C2), eltype(D2))
A,B,C,D = ssdata(sys)
D3 = D2 == 0 ? zeros(T, size(C2, 1), sys.nu) : D2
x = sys.x
u = sys.u
y = [sys.y; y]
named_ss(ss(A, B, [C; C2], [D; D3]), sys.timeevol; x, u, y)
end


function CS.minreal(sys::NamedStateSpace, args...; kwargs...)
msys = minreal(sys.sys, args...; kwargs...)
named_ss(msys; sys.u, sys.y, sys.name)
Expand Down
4 changes: 4 additions & 0 deletions test/test_named_systems2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ end
G = measure(s1, :x)
@test G.C == ones(1, 1)
@test G.y == [:x]

@test add_output(s1, [0.2]) isa NamedStateSpace
@test add_output(s1, [0.2], y=[:hej]).y[2] === :hej

end

G1 = ss(1,1,1,0)
Expand Down

0 comments on commit 9e6a979

Please sign in to comment.