utilities.jl 894 Bytes
Newer Older
1
export periodically, init_sysfilter, sysfilter!
Fredrik Bagge Carlson's avatar
Update  
Fredrik Bagge Carlson committed
2
3
4
5
6
7
8
9
10
11
12
13
14

"""
	@periodically(h, body)
Ensures that the body is run with an interval of `h >= 0.001` seconds.
"""
macro periodically(h, body)
	quote
		local start_time = time()
		$(esc(body))
		local execution_time = time()-start_time
		sleep(max(0,$h-execution_time))
	end
end
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

"""
	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