From dac13f1a96afe03e990ed5682d1f17d0441a7bc6 Mon Sep 17 00:00:00 2001
From: Fredrik Bagge Carlson <cont-frb@ulund.org>
Date: Sun, 20 Aug 2017 21:15:09 +0200
Subject: [PATCH] Add missing sysfilter functions

---
 REQUIRE            |  1 +
 src/controllers.jl |  1 -
 src/utilities.jl   | 22 +++++++++++++++++++++-
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/REQUIRE b/REQUIRE
index 137767a..9ed6efe 100644
--- a/REQUIRE
+++ b/REQUIRE
@@ -1 +1,2 @@
 julia 0.6
+ControlSystems
diff --git a/src/controllers.jl b/src/controllers.jl
index c9cd617..80b00da 100644
--- a/src/controllers.jl
+++ b/src/controllers.jl
@@ -8,7 +8,6 @@ Perform control experiemnt where the feedback and feedforward controllers are gi
 `reference` is a reference generating function that accepts a scalar `t` (time in seconds) and outputs a scalar `r`, default is `reference(t) = sign(sin(2π*t))`.
 
 The outputs `y,u,r` are the beam angle, control signal and reference respectively.
-
 """
 function run_control_2DOF(P::AbstractProcess,sysFB, sysFF=nothing; duration = 10, reference = t->sign(sin(2π*t)))
 	h 		= sampletime(P)
diff --git a/src/utilities.jl b/src/utilities.jl
index 86be3b8..a8cb718 100644
--- a/src/utilities.jl
+++ b/src/utilities.jl
@@ -1,4 +1,4 @@
-export periodically
+export periodically, init_sysfilter, sysfilter!
 
 """
 	@periodically(h, body)
@@ -12,3 +12,23 @@ macro periodically(h, body)
 		sleep(max(0,$h-execution_time))
 	end
 end
+
+"""
+	state = init_sysfilter(sys::StateSpace)
+Use together with [`sysfilter!`](@ref)
+"""
+function init_sysfilter(sys::StateSpace)
+ 	zeros(sys.nx,1)
+end
+
+"""
+	output = sysfilter!(state, sys::StateSpace, input)
+Returns the filtered output `y` in `y = Cx+Du, x'=Ax+Bu`
+
+This function is used to implement control loops where a signal is filtered through a
+dynamical system, i.e., `U(z) = C(z)E(z)`. Initialize `state` using [`init_sysfilter`](@ref).
+"""
+function sysfilter!(state, sys::StateSpace, input)
+	state .= sys.A*state + sys.B*input
+	output = sys.C*state + sys.D*input
+end
-- 
GitLab