Skip to content
Snippets Groups Projects
Commit 0fa5164c authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Bugfixes from lab

parent 3b940c26
No related branches found
No related tags found
No related merge requests found
...@@ -5,4 +5,3 @@ Polynomials ...@@ -5,4 +5,3 @@ Polynomials
ProgressMeter ProgressMeter
JLD JLD
Documenter Documenter
LabProcesses
...@@ -3,7 +3,7 @@ if "LabProcesses" ∉ keys(installed_packages) ...@@ -3,7 +3,7 @@ if "LabProcesses" ∉ keys(installed_packages)
Pkg.clone("https://gitlab.control.lth.se/processes/LabProcesses.jl.git") Pkg.clone("https://gitlab.control.lth.se/processes/LabProcesses.jl.git")
end end
__precompile__() #__precompile__()
""" """
This module contains a controller for the Ball and Beam. It connects to the This module contains a controller for the Ball and Beam. It connects to the
...@@ -26,8 +26,9 @@ using LabProcesses, Plots, Polynomials, ControlSystems, ProgressMeter ...@@ -26,8 +26,9 @@ using LabProcesses, Plots, Polynomials, ControlSystems, ProgressMeter
Perform fra-experiemnt For a single frequency `ω`. Called from inside `fra` Perform fra-experiemnt For a single frequency `ω`. Called from inside `fra`
""" """
function run_experiment(P::BallAndBeamType, ω, duration, settling_time, amplitude, bias) function run_experiment(P::BallAndBeamType, ω, duration, settling_time, amplitude, bias)
h = sampletime(P)
data = zeros(0:h:duration) data = zeros(0:h:duration)
initialize(P) LabProcesses.initialize(P)
for t = 0:h:settling_time for t = 0:h:settling_time
@periodically h begin @periodically h begin
u = amplitude*sin(ω*t) u = amplitude*sin(ω*t)
...@@ -41,16 +42,16 @@ function run_experiment(P::BallAndBeamType, ω, duration, settling_time, amplitu ...@@ -41,16 +42,16 @@ function run_experiment(P::BallAndBeamType, ω, duration, settling_time, amplitu
control(P, u) control(P, u)
end end
end end
finalize(P) LabProcesses.finalize(P)
data data
end end
""" """
∫ = integrate(fun::Function, data::Vector, ω::Real) ∫ = integrate(fun::Function, data::Vector, ω::Real,h)
Calculate sin or cos channel, call like this `integrate(sin, data, ω)` for sin-channel Calculate sin or cos channel, call like this `integrate(sin, data, ω,h)` for sin-channel
integration integration
""" """
integrate(fun,data,ω) = h*sum(fun(ω*(i-1)*h).*data[i] for i = 1:length(data)) integrate(fun,data,ω,h) = h*sum(fun(ω*(i-1)*h).*data[i] for i = 1:length(data))
""" """
fra(Ω::AbstractVector; kwargs...) fra(Ω::AbstractVector; kwargs...)
...@@ -66,7 +67,7 @@ function fra(P::BallAndBeamType, Ω::AbstractVector; ...@@ -66,7 +67,7 @@ function fra(P::BallAndBeamType, Ω::AbstractVector;
settling_time = 2, settling_time = 2,
nbr_of_periods = 10, nbr_of_periods = 10,
amplitude = 1) amplitude = 1)
h = sampletime(P)
G = zeros(Complex, size(Ω)) # Transfer function G = zeros(Complex, size(Ω)) # Transfer function
@showprogress 1 "Running experiments" for (i,ω) = enumerate(Ω) @showprogress 1 "Running experiments" for (i,ω) = enumerate(Ω)
...@@ -74,8 +75,8 @@ function fra(P::BallAndBeamType, Ω::AbstractVector; ...@@ -74,8 +75,8 @@ function fra(P::BallAndBeamType, Ω::AbstractVector;
settling_time_i = ceil(settling_time/T)*T # Settling time even number of periods settling_time_i = ceil(settling_time/T)*T # Settling time even number of periods
duration = nbr_of_periods*T duration = nbr_of_periods*T
data = run_experiment(P,ω,duration,settling_time_i, amplitude,bias) data = run_experiment(P,ω,duration,settling_time_i, amplitude,bias)
sin_channel = integrate(sin, data, ω) sin_channel = integrate(sin, data, ω, h)
cos_channel = integrate(cos, data, ω) cos_channel = integrate(cos, data, ω, h)
G[i] = 2/(amplitude*duration)*Complex(sin_channel, cos_channel) G[i] = 2/(amplitude*duration)*Complex(sin_channel, cos_channel)
end end
Number[Ω G] Number[Ω G]
...@@ -92,7 +93,7 @@ end ...@@ -92,7 +93,7 @@ end
function bopl!(G; kwargs...) function bopl!(G; kwargs...)
plot!(G[:,1].*(180/π), abs.(G[:,2]); ylabel="Magnitude", title="Bode plot", xscale=:log10, yscale=:log10, subplot=1, kwargs...) plot!(G[:,1].*(180/π), abs.(G[:,2]); ylabel="Magnitude", title="Bode plot", xscale=:log10, yscale=:log10, subplot=1, kwargs...)
plot!(G[:,1].*(180/π), angle.(G[:,2]); xlabel="Frequency [1/s]", ylabel="Phase", xscale=:log10, subplot=2, kwargs...) plot!(G[:,1].*(180/π), angle.(G[:,2]).*(180/π); xlabel="Frequency [1/s]", ylabel="Phase", xscale=:log10, subplot=2, kwargs...)
plot!(link=:x) plot!(link=:x)
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment