Skip to content

Commit 9e6a979

Browse files
committed
add_output for named ss
1 parent f8f9037 commit 9e6a979

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/named_systems2.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ end
363363
"""
364364
measure(s::NamedStateSpace, names)
365365
366-
Return a system with specified states as measurement outputs.
366+
Return a system with specified state variables as measurement outputs.
367+
368+
See also [`add_output`](@ref).
367369
"""
368370
function measure(s::NamedStateSpace, names)
369371
inds = names2indices(names, s.x)
@@ -840,6 +842,27 @@ function CS.append(systems::NamedStateSpace...; kwargs...)
840842
end
841843

842844

845+
"""
846+
add_output(sys::NamedStateSpace, C2::AbstractArray, D2 = 0; y)
847+
848+
Add outputs to `sys` corresponding to the output matrix `C2` and the feedthrough matrix `D2` to the system `sys`.
849+
850+
# Arguments:
851+
- `y`: The names used for the new outputs. If not provided, the names will be generated automatically.
852+
853+
See also [`measure`](@ref) for a simpler way to output state variables.
854+
"""
855+
function CS.add_output(sys::NamedStateSpace, C2::AbstractArray, D2=0; y = [Symbol("y_$i") for i in (1:size(C2, 1)) .+ sys.ny])
856+
T = promote_type(CS.numeric_type(sys), eltype(C2), eltype(D2))
857+
A,B,C,D = ssdata(sys)
858+
D3 = D2 == 0 ? zeros(T, size(C2, 1), sys.nu) : D2
859+
x = sys.x
860+
u = sys.u
861+
y = [sys.y; y]
862+
named_ss(ss(A, B, [C; C2], [D; D3]), sys.timeevol; x, u, y)
863+
end
864+
865+
843866
function CS.minreal(sys::NamedStateSpace, args...; kwargs...)
844867
msys = minreal(sys.sys, args...; kwargs...)
845868
named_ss(msys; sys.u, sys.y, sys.name)

test/test_named_systems2.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ end
139139
G = measure(s1, :x)
140140
@test G.C == ones(1, 1)
141141
@test G.y == [:x]
142+
143+
@test add_output(s1, [0.2]) isa NamedStateSpace
144+
@test add_output(s1, [0.2], y=[:hej]).y[2] === :hej
145+
142146
end
143147

144148
G1 = ss(1,1,1,0)

0 commit comments

Comments
 (0)