controllers.jl 1.25 KB
Newer Older
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
1
2
export run_control_2DOF

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
3
4
5
"""
	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
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
6
`sysFB` and `sysFF`, both of type `StateSpace`.
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
7
8
9
10
11

`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.
"""
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
12
function run_control_2DOF(P::AbstractProcess,sysFB, sysFF=nothing; duration = 10, reference = t->sign(sin(2π*t)))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
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
	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
	finalize(P)
	y,u,r
end