diff --git a/src/controllers.jl b/src/controllers.jl index 1e5f2fa65980d37f2affc6372b289ce805803fb9..945f7f8233f980328dd9bb577e35fd143f51f43d 100644 --- a/src/controllers.jl +++ b/src/controllers.jl @@ -36,9 +36,9 @@ function run_control_2DOF(P::AbstractProcess,sysFB, sysFF=nothing; duration = 10 y[:,i] = measure(P) r[:,i] = reference(t) u[:,i] = calc_control(i) # y,r must be updated before u - control(P, [clamp.(u[j,i], input_range(P)[j]...) for j=1:nu]) + control(P, [clamp.(u[j,i], inputrange(P)[j]...) for j=1:nu]) end end finalize(P) - y,u,r + y',u',r' end diff --git a/src/interface_implementations/ballandbeam.jl b/src/interface_implementations/ballandbeam.jl index fe1ed04c3c7dd295e846146d49e8b12d522aa72d..19f5361a12867c311c7a752b3c6d9b212cc6585e 100644 --- a/src/interface_implementations/ballandbeam.jl +++ b/src/interface_implementations/ballandbeam.jl @@ -1,6 +1,18 @@ # Interface implementation Ball And Beam ==================================================== -export BallAndBeam, BallAndBeamSimulator, BallAndBeamType +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)) struct BallAndBeam <: PhysicalProcess h::Float64 @@ -14,22 +26,32 @@ struct BallAndBeamSimulator <: SimulatedProcess end BallAndBeamSimulator() = BallAndBeamSimulator(0.01, zeros(4)) -const BallAndBeamType = Union{BallAndBeam, BallAndBeamSimulator} + +const BeamType = Union{Beam, BeamSimulator} +const BallAndBeamType = Union{BallAndBeam, BallAndBeamSimulator} +const BeamOrBallAndBeam = Union{BeamType, BallAndBeamType} + + +num_outputs(p::BeamType) = 1 num_outputs(p::BallAndBeamType) = 2 -num_inputs(p::BallAndBeamType) = 1 +num_inputs(p::BeamOrBallAndBeam)= 1 +outputrange(p::Beam) = [(-10,10)] outputrange(p::BallAndBeamType) = [(-10,10),(-1,1)] # Beam angle, Ball position -inputrange(p::BallAndBeamType) = [(-10,10)] +inputrange(p::BeamOrBallAndBeam)= [(-10,10)] +isstable(p::Beam) = true isstable(p::BallAndBeamType) = false -isasstable(p::BallAndBeamType) = false -sampletime(p::BallAndBeamType) = p.h -bias(p::BallAndBeam) = p.bias +isasstable(p::BeamOrBallAndBeam) = false +sampletime(p::BeamOrBallAndBeam) = p.h +bias(p::BeamOrBallAndBeam) = p.bias bias(p::BallAndBeamSimulator) = 0 -control(p::BallAndBeam, u) = ccall((:comedi_write, comedipath),Int32, +control(p::BeamOrBallAndBeam, u) = ccall((:comedi_write, comedipath),Int32, (Int32,Int32,Int32,Int32),0,1,1,num2io(u[1]+p.bias)) -measure(p::BallAndBeam) = io2num(ccall((:comedi_read,comedipath), Int32, +measure(p::Beam) = io2num(ccall((:comedi_read,comedipath), Int32, (Int32,Int32,Int32), 0,0,0)) +measure(p::BallAndBeam) = [io2num(ccall((:comedi_read,comedipath), Int32, +(Int32,Int32,Int32), 0,0,i)) for i = 0:1] control(p::BallAndBeamSimulator, u) = error("Not yet implemented") measure(p::BallAndBeamSimulator) = [p.state[1], p.state[3]] @@ -39,7 +61,7 @@ const conversion = 65535/20 io2num(x) = x/conversion -10 # Converts from io to float num2io(x) = round(Int32,x*100 + 2048) # Converts from regular number to io -initialize(p::BallAndBeam) = ccall((:comedi_start, comedipath),Int32,(Int32,), 0) -finalize(p::BallAndBeam) = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0)) +initialize(p::BeamOrBallAndBeam) = ccall((:comedi_start, comedipath),Int32,(Int32,), 0) +finalize(p::BeamOrBallAndBeam) = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0)) initialize(p::BallAndBeamSimulator) = nothing finalize(p::BallAndBeamSimulator) = nothing