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

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
export Beam, BeamSimulator, BeamType, BallAndBeam, BallAndBeamSimulator, BeamOrBallAndBeam

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
16
17
18

struct BallAndBeam <: PhysicalProcess
    h::Float64
19
    bias::Float64
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
20
end
21
BallAndBeam() = BallAndBeam(0.01, 0.0)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
22
23
24

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

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
29
30
31
32
33
34
35

const BeamType = Union{Beam, BeamSimulator}
const BallAndBeamType   = Union{BallAndBeam, BallAndBeamSimulator}
const BeamOrBallAndBeam = Union{BeamType, BallAndBeamType}


num_outputs(p::BeamType)        = 1
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
36
num_outputs(p::BallAndBeamType) = 2
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
37
38
num_inputs(p::BeamOrBallAndBeam)= 1
outputrange(p::Beam)            = [(-10,10)]
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
39
outputrange(p::BallAndBeamType) = [(-10,10),(-1,1)] # Beam angle, Ball position
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
40
41
inputrange(p::BeamOrBallAndBeam)= [(-10,10)]
isstable(p::Beam)               = true
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
42
isstable(p::BallAndBeamType)    = false
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
43
44
45
isasstable(p::BeamOrBallAndBeam)  = false
sampletime(p::BeamOrBallAndBeam)  = p.h
bias(p::BeamOrBallAndBeam)            = p.bias
46
47
bias(p::BallAndBeamSimulator)   = 0

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
48

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
49
control(p::BeamOrBallAndBeam, u) = ccall((:comedi_write, comedipath),Int32,
50
                                        (Int32,Int32,Int32,Int32),0,1,1,num2io(u[1]+p.bias))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
51
measure(p::Beam) = io2num(ccall((:comedi_read,comedipath), Int32,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
52
                                        (Int32,Int32,Int32), 0,0,0))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
53
54
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
55
56

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

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

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
64
65
initialize(p::BeamOrBallAndBeam)  = ccall((:comedi_start, comedipath),Int32,(Int32,), 0)
finalize(p::BeamOrBallAndBeam)    = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
66
67
initialize(p::BallAndBeamSimulator)  = nothing
finalize(p::BallAndBeamSimulator)    = nothing