diff --git a/src/controllers.jl b/src/controllers.jl index 0d404d7f643e0d63bd9e2945aa5081be40d0e752..93a2102d950955cde68ee8128b2f5ecb1f0414f6 100644 --- a/src/controllers.jl +++ b/src/controllers.jl @@ -30,9 +30,10 @@ function run_control_2DOF(P::AbstractProcess,sysFB, sysFF=nothing; duration = 10 ui + bias(P) end + simulation = isa(P, SimulatedProcess) initialize(P) for (i,t) = enumerate(0:h:duration) - @periodically h begin + @periodically h simulation begin y[:,i] = measure(P) r[:,i] = reference(t) u[:,i] = calc_control(i) # y,r must be updated before u diff --git a/src/interface_implementations/ballandbeam.jl b/src/interface_implementations/ballandbeam.jl index 05543f5067311cdb44d73111a4fcd5cd56f5f85a..8cee43f453f0c7736ca3f5b4a366ec7c7de9e027 100644 --- a/src/interface_implementations/ballandbeam.jl +++ b/src/interface_implementations/ballandbeam.jl @@ -87,5 +87,5 @@ initialize(p::AbstractBeamOrBallAndBeam) = ccall((:comedi_start, comedipath),Int finalize(p::AbstractBeamOrBallAndBeam) = (control(p,0);ccall((:comedi_stop, comedipath),Int32,(Int32,), 0)) initialize(p::BallAndBeamSimulator) = nothing finalize(p::BallAndBeamSimulator) = nothing -initialize(p::BeamSimulator) = nothing +initialize(p::BeamSimulator) = p.state .*= 0 finalize(p::BeamSimulator) = nothing diff --git a/src/utilities.jl b/src/utilities.jl index 7cff5ca0020b4e9e93c617b56c26c0b91919eb49..3dc62305c91b4e9d14359c4d1931189e04c11a19 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -13,6 +13,20 @@ macro periodically(h, body) end end +""" + @periodically(h, simulation::Bool, body) +Ensures that the body is run with an interval of `h >= 0.001` seconds. +If `simulation == false`, no sleep is done +""" +macro periodically(h, simulation, body) + quote + local start_time = time() + $(esc(body)) + local execution_time = time()-start_time + $(esc(simulation)) || sleep(max(0,$(esc(h))-execution_time)) + end +end + """ state = init_sysfilter(sys::StateSpace) Use together with [`sysfilter!`](@ref)