Skip to content

Commit 47151aa

Browse files
authored
Accept controller returning scalar (#319)
1 parent 17c5867 commit 47151aa

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/timeresp.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ function lsim(sys::StateSpace, u::Function, t::AbstractVector;
143143
x0::VecOrMat=zeros(sys.nx), method::Symbol=:cont)
144144
ny, nu = size(sys)
145145
nx = sys.nx
146+
u0 = u(x0,1)
146147
if length(x0) != nx
147148
error("size(x0) must match the number of states of sys")
148-
elseif size(u(x0,1)) != (nu,) && size(u(x0,1)) != (nu,1)
149+
elseif !(u0 isa Number && nu == 1) && (size(u0) != (nu,) && size(u0) != (nu,1))
149150
error("return value of u must be of size nu")
150151
end
151152
T = promote_type(Float64, eltype(x0))
@@ -207,7 +208,7 @@ function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::Function, t,
207208

208209
for i=1:iters
209210
x[:,i] = x0
210-
uout[:,i] = u(x0,t[i])
211+
uout[:,i] .= u(x0,t[i])
211212
x0 = A * x0 + B * uout[:,i]
212213
end
213214
return transpose(x), transpose(uout)

test/test_timeresp.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ sysd = c2d(sys, 0.1)[1]
3333
sysdfb = ss(sysd.A-sysd.B*L, sysd.B, sysd.C, sysd.D, 0.1)
3434
#Simulate without input
3535
yd, td, xd = lsim(sysdfb, zeros(501), t, x0=x0)
36-
3736
@test y yd
3837
@test x xd
3938

39+
# Test that the discrete lsim accepts u function that returns scalar
40+
L = lqr(sysd,Q,R)
41+
u(x,i) = -L*x
42+
yd, td, xd = lsim(sysd, u, t, x0=x0)
43+
@test norm(y - yd)/norm(y) < 0.05 # Since the cost matrices are not discretized, these will differ a bit
44+
@test norm(x - xd)/norm(x) < 0.05
4045

4146
#Test step and impulse Continuous
4247
t0 = 0:0.05:2

0 commit comments

Comments
 (0)