controllers.jl 1.28 KB
Newer Older
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
	y,u,r = run_control(process, sysFB[, sysFF]; duration = 10, reference(t) = sign(sin(2π*t)))
Perform control experiemnt where the feedback and feedforward controllers are given by
`sysFB` and `sysFF`, both of type `StateSpace`. See [`fbdesign`](@ref) [`ffdesign`](@ref).

`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(P::AbstractProcess,sysFB, sysFF=nothing; duration = 10, reference = t->sign(sin(2π*t)))
	h 		= sampletime(P)
	y       = zeros(0:h:duration)
	u       = zeros(0:h:duration)
	r       = zeros(0:h:duration)

	sysFB   = minreal(sysFB)
	stateFB = init_sysfilter(sysFB)
	if sysFF != nothing
		sysFF   = minreal(sysFF)
		stateFF = init_sysfilter(sysFF)
	end

	function control(i)
		rf = sysFF == nothing ? r[i] : sysfilter!(stateFF, sysFF, r[i])
		e  = rf-y[i]
		ui = sysfilter!(stateFB, sysFB, e)
	end

	initialize(P)
	for (i,t) = enumerate(0:h:duration)
		@periodically h begin
			y[i]       = measure(P)
			r[i]       = reference(t)
			u[i]       = control(i) # y,r must be updated before u
			control(P, u[i])
		end
	end
	control(P, 0)
	finalize(P)
	y,u,r
end