Skip to content

Commit

Permalink
Merge pull request #9 from SimonEnsemble/animate
Browse files Browse the repository at this point in the history
Animate
  • Loading branch information
SimonEnsemble authored Feb 18, 2020
2 parents 975ba4e + 2d3e54d commit f0c1f10
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/src/viz.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ PyPlot.matplotlib.style.use(normpath(joinpath(pathof(Controlz), "..", "hipster.m
nyquist_diagram
bode_plot
root_locus
mk_gif
```
2 changes: 1 addition & 1 deletion src/Controlz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export
# sim.jl
simulate, interpolate,
# viz.jl
viz_response, viz_poles_and_zeros, nyquist_diagram, root_locus, bode_plot,
viz_response, viz_poles_and_zeros, nyquist_diagram, root_locus, bode_plot, mk_gif,
# controlz.jl
PController, PIController, PIDController
end
83 changes: 81 additions & 2 deletions src/viz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ end
"""
viz_response(t, y,
plot_title="", plot_xlabel="time, t",
plot_ylabel="output, y(t)")
plot_ylabel="output, y(t)",
savename=nothing)
plot `y` vs. `t` to visualize the response of a system to an input. typically `t` and `y` are outputs of [`simulate`](@ref).
note that PyPlot.jl (matplotlib) commands can be invoked after `viz_response` to make further changes to the figure panel.
e.g. `xlim([0, 1])` can be applied after `viz_response`.
# Arguments
* `t::Array{Float64}`: array of times
* `y::Array{Float64}`: array of values of response variables at the corresponding times in `t`
* `plot_title::Union{String, LaTeXString}`: title of plot
* `plot_xlabel::Union{String, LaTeXString}`: x-label
* `plot_ylabel::Union{String, LaTeXString}`: y-label
* `savename::Union{Nothing, String}`: filename to save as a figure in .png format (dpi 250).
# Example
```
Expand All @@ -28,6 +36,7 @@ function viz_response(t::Array{Float64}, y::Array{Float64};
plot_title::Union{String, LaTeXString}="",
plot_xlabel::Union{String, LaTeXString}=L"time, $t$",
plot_ylabel::Union{String, LaTeXString}=L"output, $y(t)$",
savename::Union{Nothing, String}=nothing
)

figure()
Expand All @@ -36,7 +45,13 @@ function viz_response(t::Array{Float64}, y::Array{Float64};
ylabel(plot_ylabel)
title(plot_title)
draw_axes()
tight_layout()
if ! isnothing(savename)
tight_layout()
if ! occursin(".png", savename)
savename *= ".png"
end
savefig(savename, dpi=250, format="png")
end
return nothing
end

Expand Down Expand Up @@ -137,3 +152,67 @@ function bode_plot(g::TransferFunction; log10_ω_min::Float64=-4.0, log10_ω_max
tight_layout()
return nothing
end

@doc raw"""
mk_gif(t, y, plot_title="", plot_xlabel="time, t",
plot_ylabel="output, y(t)",
savename="response")
make a .gif of the process response.
`t` and `y` are outputs of [`simulate`](@ref).
accepts same arguments as [`viz_response`](@ref).
ImageMagick must be installed to create the .gif.
the .gif is saved as a file `savename`.
# Arguments
* `t::Array{Float64}`: array of times
* `y::Array{Float64}`: array of values of response variables at the corresponding times in `t`
* `plot_title::Union{String, LaTeXString}`: title of plot
* `plot_xlabel::Union{String, LaTeXString}`: x-label
* `plot_ylabel::Union{String, LaTeXString}`: y-label
* `savename::String`: filename to save as a .gif. .gif extension automatically appended if not provided.
"""
function mk_gif(t::Array{Float64}, y::Array{Float64};
plot_title::Union{String, LaTeXString}="",
plot_xlabel::Union{String, LaTeXString}=L"time, $t$",
plot_ylabel::Union{String, LaTeXString}=L"output, $y(t)$",
savename::String="response.gif"
)
if length(t) > 999
error("too many points and thus images; reduce the number of points")
end

# let matplotlib determine x, y lims:
plot(t, y)
xmin, xmax, ymin, ymax = axis()
close()

# name to save image i as.
step_to_image(i::Int) = @sprintf("__y_of_t_snapshot_%03d.png", i)

# save series of images
for i = 2:length(t)
viz_response(t[1:i], y[1:i], plot_title=plot_title, plot_xlabel=plot_xlabel,
plot_ylabel=plot_ylabel)
xlim([xmin, xmax])
ylim([ymin, ymax])
tight_layout()
savefig(step_to_image(i), format="png", dpi=100)
close()
end

if ! occursin(".gif", savename)
savename *= ".gif"
end
try
run(`convert -delay 20 -loop 0 __y_of_t_snapshot_\*.png $savename`)
@info "see " * savename
catch
@warn "You must install ImageMagick for `convert` to run. Exiting..."
end

# clean up
for i = 2:length(t)
rm(step_to_image(i))
end
end

0 comments on commit f0c1f10

Please sign in to comment.