ballandbeam.jl 3.12 KB
Newer Older
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
1
2
# Interface implementation Ball And Beam ====================================================

3
4
5
6
7
8
9
10
11
12
# The ball and beam can be used in two modes, either just the Beam, in which case there is a
# single output (measurement signal) or the BallAndBeam, in which case there are two.
# There are a few union types defined for convenience, these are
# AbstractBeam = Union{Beam, BeamSimulator}
# AbstractBallAndBeam   = Union{BallAndBeam, BallAndBeamSimulator}
# AbstractBeamOrBallAndBeam = All types
# Although not Abstract per se, the names AbstractBeam etc. were chosen since this reflects
# their usage in dispatch.

export Beam, BeamSimulator, AbstractBeam, BallAndBeam, BallAndBeamSimulator, AbstractBeamOrBallAndBeam
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
13
14
15
16
17
18
19
20
21
22
23
24

struct Beam <: PhysicalProcess
    h::Float64
    bias::Float64
end
Beam() = Beam(0.01, 0.0)

struct BeamSimulator <: SimulatedProcess
    h::Float64
    state::Vector{Float64} # angle,ω
end
BeamSimulator() = BeamSimulator(0.01, zeros(2))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
25
26
27

struct BallAndBeam <: PhysicalProcess
    h::Float64
28
    bias::Float64
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
29
end
30
BallAndBeam() = BallAndBeam(0.01, 0.0)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
31
32
33

struct BallAndBeamSimulator <: SimulatedProcess
    h::Float64
34
    state::Vector{Float64} # pos,vel,angle,ω
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
35
end
36
BallAndBeamSimulator() = BallAndBeamSimulator(0.01, zeros(4))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
37

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
38

39
40
41
const AbstractBeam              = Union{Beam, BeamSimulator}
const AbstractBallAndBeam       = Union{BallAndBeam, BallAndBeamSimulator}
const AbstractBeamOrBallAndBeam = Union{AbstractBeam, AbstractBallAndBeam}
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
42
43


44
45
46
47
48
49
50
51
52
53
54
55
num_outputs(p::AbstractBeam)             = 1
num_outputs(p::AbstractBallAndBeam)      = 2
num_inputs(p::AbstractBeamOrBallAndBeam) = 1
outputrange(p::Beam)                     = [(-10,10)]
outputrange(p::AbstractBallAndBeam)      = [(-10,10),(-1,1)] # Beam angle, Ball position
inputrange(p::AbstractBeamOrBallAndBeam) = [(-10,10)]
isstable(p::Beam)                        = true
isstable(p::AbstractBallAndBeam)         = false
isasstable(p::AbstractBeamOrBallAndBeam) = false
sampletime(p::AbstractBeamOrBallAndBeam) = p.h
bias(p::AbstractBeamOrBallAndBeam)       = p.bias
bias(p::BallAndBeamSimulator)            = 0
56

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
57

58
control(p::AbstractBeamOrBallAndBeam, u) = ccall((:comedi_write, comedipath),Int32,
59
                                        (Int32,Int32,Int32,Int32),0,1,1,num2io(u[1]+p.bias))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
60
measure(p::Beam) = io2num(ccall((:comedi_read,comedipath), Int32,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
61
                                        (Int32,Int32,Int32), 0,0,0))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
62
63
measure(p::BallAndBeam) = [io2num(ccall((:comedi_read,comedipath), Int32,
(Int32,Int32,Int32), 0,0,i)) for i = 0:1]
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
64
65

control(p::BallAndBeamSimulator, u)  = error("Not yet implemented")
66
measure(p::BallAndBeamSimulator)     = [p.state[1], p.state[3]]
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
67

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
68
const comedipath = Pkg.dir("LabProcesses","c","comedi_bridge.so")
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
69
70
const conversion = 65535/20
io2num(x) = x/conversion -10            # Converts from io to float
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
71
num2io(x) = round(Int32,x*100 + 2048)   # Converts from regular number to io
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
72

73
74
75
76
initialize(p::AbstractBeamOrBallAndBeam) = ccall((:comedi_start, comedipath),Int32,(Int32,), 0)
finalize(p::AbstractBeamOrBallAndBeam)   = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0))
initialize(p::BallAndBeamSimulator)      = nothing
finalize(p::BallAndBeamSimulator)        = nothing