README.md 4.61 KB
Newer Older
1
# LabProcesses
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
2
This package contains an (programming- as well as connection-) interface to serve
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
3
as a base for the implementation of lab-process software. The first example of
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
4
5
6
7
8
9
10
11
an implementaiton of this interface is for the ball-and-beam process, which is
used in Lab1 FRTN35: frequency response analysis of the beam. The lab is implemented
in [BallAndBeam.jl](https://gitlab.control.lth.se/processes/BallAndBeam.jl), a
package that makes use of `LabProcesses.jl` to handle the communication with
the lab process and/or a simulated version thereof. This way, the code written
for frequency response analysis of the beam can be run on another process
implementing the same interface (or a simulated version) by changeing a single
line of code :)
12

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
13
## Installation
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
14
15
16
17
1. Start julia by typing `julia` in a terminal, make sure the printed info says it's
`v0.6+` running. If not, visit [julialang.org](https://julialang.org/downloads/)
to get the latest release.
2. Install LabProcesses.jl using command `Pkg.clone("https://gitlab.control.lth.se/processes/LabProcesses.jl.git")` Lots of packages will now be installed, this will take some time. If this is your first time using Julia, you might have to run `julia> Pkg.init()` before you install any packages.
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
18
19

## How to implement a new process
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
20
1. Locate the file [interface.jl](https://gitlab.control.lth.se/processes/LabProcesses.jl/blob/master/src/interface.jl). (Alternatively, you can copy all definitions from [/interface_implementations/ballandbeam.jl](https://gitlab.control.lth.se/processes/LabProcesses.jl/blob/master/src/interface_implementations/ballandbeam.jl) instead. Maybe it's easier to work from an existing implementaiton.)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
21
22
2. Copy all function definitions.
3. Create a new file under `/interface_implementations` where you paste all the
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
23
copied definitions and implement them. See [/interface_implementations/ballandbeam.jl](https://gitlab.control.lth.se/processes/LabProcesses.jl/blob/master/src/interface_implementations/ballandbeam.jl) for an example.
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
24
4. Above all function implementations you must define the process type, e.g,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
25
26
27
    ```julia
    struct BallAndBeam <: PhysicalProcess
        h::Float64
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
28
        bias::Float64
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
29
    end
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
30
    BallAndBeam() = BallAndBeam(0.01, 0.0) # Constructor with default value of sample time
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
31
    ```
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
32
Make sure you inherit from `PhysicalProcess` or `SimulatedProcess` as appropriate.
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
33
34
35
36
37
38
This type must contains fields that hold information about everything that is
relevant to a particular instance of the process. Different ballandbeam-process
have different biases, hence this must be stored. A simulated process would have
to keep track of its state etc. in order to implement the measure and control
methods. See [Types in julia documentation](https://docs.julialang.org/en/stable/manual/types/#Composite-Types-1)
for additional info regarding user defined types and (constructors)[https://docs.julialang.org/en/stable/manual/constructors/].
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
39
40
41
42
43
44
5. Documentation of all interface functions is available in the file [interface_documentation.jl](https://gitlab.control.lth.se/processes/LabProcesses.jl/blob/master/src/interface_documentation.jl)

## Control a process
The interface `AbstractProcess` defines the functions `control(P, u)` and `measure(P)`.
These functions can be used to implement your own control loops. A common loop
with a feedback controller and a feedforward filter on the reference is implemented
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
45
46
in the function [`run_control_2DOF`](@ref), where the user can supply $G_1$ and $G_4$
in the diagram below, with the process $P=G_2$.
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
47
![block diagram](docs/feedback4.png)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
48
49
50
51
52
53
54

The macro `@periodically` might come in handy if you want to implement your own loop.
Consider the following example, in which the loop body will be run periodically
with a sample time of `h` seconds.
```julia
for (i,t) = enumerate(0:h:duration)
    @periodically h begin
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
55
56
57
        y[i] = measure(P)
        r[i] = reference(t)
        u[i] = control(i)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
58
59
60
61
62
63
64
        control(P, u[i])
    end
end
```

Often one finds the need to implement a stateful controller, i.e., a function
that has a memory or state. To this end, the function [`sysfilter`](@ref) is
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
65
66
67
provided. 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)`.
Usage is demonstrated below
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
68
69
70
```julia
stateC = init_sysfilter(C)
function control(i)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
71
72
    e = r[i]-y[i]
    u = sysfilter!(stateC, C, e)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
73
74
end
```
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
75
`C` must here be represented by a [`StateSpace`](http://juliacontrol.github.io/ControlSystems.jl/latest/lib/constructors/#ControlSystems.ss) type from [`ControlSystems.jl`](https://github.com/JuliaControl/ControlSystems.jl).
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
76
`TransferFunction` types can easily be converted to a `StateSpace` by `Gss = ss(Gtf)`.