From 127b12adc3f7226ebd18e4d4cbb617fad9e9314e Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson <cont-frb@ulund.org> Date: Tue, 22 Aug 2017 17:48:31 +0200 Subject: [PATCH] Change Union type naming to Abstract --- src/interface_implementations/ballandbeam.jl | 51 +++++++++++-------- .../eth_helicopter.jl | 35 +++++++------ 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/interface_implementations/ballandbeam.jl b/src/interface_implementations/ballandbeam.jl index 19f5361..d91092f 100644 --- a/src/interface_implementations/ballandbeam.jl +++ b/src/interface_implementations/ballandbeam.jl @@ -1,6 +1,15 @@ # Interface implementation Ball And Beam ==================================================== -export Beam, BeamSimulator, BeamType, BallAndBeam, BallAndBeamSimulator, BeamOrBallAndBeam +# 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 struct Beam <: PhysicalProcess h::Float64 @@ -27,26 +36,26 @@ end BallAndBeamSimulator() = BallAndBeamSimulator(0.01, zeros(4)) -const BeamType = Union{Beam, BeamSimulator} -const BallAndBeamType = Union{BallAndBeam, BallAndBeamSimulator} -const BeamOrBallAndBeam = Union{BeamType, BallAndBeamType} +const AbstractBeam = Union{Beam, BeamSimulator} +const AbstractBallAndBeam = Union{BallAndBeam, BallAndBeamSimulator} +const AbstractBeamOrBallAndBeam = Union{AbstractBeam, AbstractBallAndBeam} -num_outputs(p::BeamType) = 1 -num_outputs(p::BallAndBeamType) = 2 -num_inputs(p::BeamOrBallAndBeam)= 1 -outputrange(p::Beam) = [(-10,10)] -outputrange(p::BallAndBeamType) = [(-10,10),(-1,1)] # Beam angle, Ball position -inputrange(p::BeamOrBallAndBeam)= [(-10,10)] -isstable(p::Beam) = true -isstable(p::BallAndBeamType) = false -isasstable(p::BeamOrBallAndBeam) = false -sampletime(p::BeamOrBallAndBeam) = p.h -bias(p::BeamOrBallAndBeam) = p.bias -bias(p::BallAndBeamSimulator) = 0 +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 -control(p::BeamOrBallAndBeam, u) = ccall((:comedi_write, comedipath),Int32, +control(p::AbstractBeamOrBallAndBeam, u) = ccall((:comedi_write, comedipath),Int32, (Int32,Int32,Int32,Int32),0,1,1,num2io(u[1]+p.bias)) measure(p::Beam) = io2num(ccall((:comedi_read,comedipath), Int32, (Int32,Int32,Int32), 0,0,0)) @@ -61,7 +70,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::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 +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 diff --git a/src/interface_implementations/eth_helicopter.jl b/src/interface_implementations/eth_helicopter.jl index a541578..7e78568 100644 --- a/src/interface_implementations/eth_helicopter.jl +++ b/src/interface_implementations/eth_helicopter.jl @@ -1,12 +1,17 @@ # Interface implementation Ball And Beam ==================================================== -export ETHHelicopter, ETHHelicopterSimulator, ETHHelicopterType +# There is a union type defined for convenience: +# AbstractETHHelicopter = Union{ETHHelicopter, ETHHelicopterSimulator} +# Although not Abstract per se, the names AbstractETHHelicopter etc. were chosen since this +# reflects the usage in dispatch. + +export ETHHelicopter, ETHHelicopterSimulator, AbstractETHHelicopter struct ETHHelicopter <: PhysicalProcess h::Float64 bias::Float64 end -ETHHelicopter() = ETHHelicopter(0.050) +ETHHelicopter() = ETHHelicopter(0.050, 0.) struct ETHHelicopterSimulator <: SimulatedProcess h::Float64 @@ -15,15 +20,15 @@ struct ETHHelicopterSimulator <: SimulatedProcess end ETHHelicopterSimulator() = ETHHelicopterSimulator(0.01, zeros(4)) -const ETHHelicopterType = Union{ETHHelicopter, ETHHelicopterSimulator} -num_outputs(p::ETHHelicopterType) = 2 -num_inputs(p::ETHHelicopterType) = 2 -outputrange(p::ETHHelicopterType) = [(-10,10),(-10,10)] # Beam angle, Ball position -inputrange(p::ETHHelicopterType) = [(-10,10)] -isstable(p::ETHHelicopterType) = false -isasstable(p::ETHHelicopterType) = false -sampletime(p::ETHHelicopterType) = p.h -bias(p::ETHHelicopterType) = p.bias +const AbstractETHHelicopter = Union{ETHHelicopter, ETHHelicopterSimulator} +num_outputs(p::AbstractETHHelicopter) = 2 +num_inputs(p::AbstractETHHelicopter) = 2 +outputrange(p::AbstractETHHelicopter) = [(-10,10),(-10,10)] +inputrange(p::AbstractETHHelicopter) = [(-10,10),(-10,10)] +isstable(p::AbstractETHHelicopter) = false +isasstable(p::AbstractETHHelicopter) = false +sampletime(p::AbstractETHHelicopter) = p.h +bias(p::AbstractETHHelicopter) = p.bias function control(p::ETHHelicopter, u) @@ -42,7 +47,7 @@ const conversion = 65535/20 io2num(x) = x/conversion -10 # Converts from io to float num2io(x) = round(Int32,(x + 10)*conversion) # Converts from regular number to io -initialize(p::ETHHelicopter) = ccall((:comedi_start, comedipath),Int32,(Int32,), 0) -finalize(p::ETHHelicopter) = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0)) -initialize(p::ETHHelicopterSimulator) = nothing -finalize(p::ETHHelicopterSimulator) = nothing +initialize(p::ETHHelicopter) = ccall((:comedi_start, comedipath),Int32,(Int32,), 0) +finalize(p::ETHHelicopter) = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0)) +initialize(p::ETHHelicopterSimulator) = nothing +finalize(p::ETHHelicopterSimulator) = nothing -- GitLab