Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ufechner7 committed Jul 28, 2024
1 parent 66b9d32 commit ecbad46
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 59 deletions.
2 changes: 1 addition & 1 deletion examples/basic_1p.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using KiteViewers, KiteUtils
viewer=Viewer3D(true);
viewer::Viewer3D = Viewer3D(true);
segments=6
state=demo_state(segments+1)
update_system(viewer, state)
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_4p.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using KiteViewers, KiteUtils
viewer=Viewer3D(true);
viewer::Viewer3D = Viewer3D(true);
segments=6
state=demo_state_4p(segments+1)
update_system(viewer, state, kite_scale=0.25)
Expand Down
6 changes: 1 addition & 5 deletions examples/depower.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ end

using KiteViewers, KiteModels, KitePodModels, Rotations

# example program that shows
# a. how to create a video
# b. how to create a performance plot (simulation speed vs time)

kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

Expand All @@ -24,7 +20,7 @@ SAVE_PNG = false
PLOT_PERFORMANCE = false
# end of user parameter section #

if ! @isdefined time_vec; const time_vec = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
time_vec::Vector{Float64} = zeros(div(STEPS, TIME_LAPSE_RATIO))
viewer::Viewer3D = Viewer3D(SHOW_KITE)

# ffmpeg -r:v 20 -i "video%06d.png" -codec:v libx264 -preset veryslow -pix_fmt yuv420p -crf 10 -an "video.mp4"
Expand Down
15 changes: 7 additions & 8 deletions examples/depower_bench_video.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ using KiteViewers, KiteModels, KitePodModels, Rotations
# example program that shows
# a. how to create a video
# b. how to create a performance plot (simulation speed vs time)
const Model = KPS4

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

# the following values can be changed to match your interest
dt = 0.05
TIME = 50
dt::Float64 = 0.05
TIME = 35
TIME_LAPSE_RATIO = 10
STEPS = Int64(round(TIME/dt))
STATISTIC = false
Expand All @@ -25,7 +24,7 @@ SAVE_PNG = false
PLOT_PERFORMANCE = false
# end of user parameter section #

if ! @isdefined time_vec; const time_vec = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
time_vec::Vector{Float64} = zeros(div(STEPS, TIME_LAPSE_RATIO))
viewer::Viewer3D = Viewer3D(SHOW_KITE)

# ffmpeg -r:v 20 -i "video%06d.png" -codec:v libx264 -preset veryslow -pix_fmt yuv420p -crf 10 -an "video.mp4"
Expand All @@ -47,7 +46,7 @@ function simulate(integrator, steps; log=false)
elseif i == 640
set_depower_steering(kps4.kcu, 0.35, 0.0)
end
KiteModels.next_step!(kps4, integrator, dt=dt)
KiteModels.next_step!(kps4, integrator; set_speed=0, dt=dt)
reltime = i*dt
if mod(i, TIME_LAPSE_RATIO) == 0 || i == steps
if SHOW_VIEWER update_system2(kps4) end
Expand All @@ -66,7 +65,7 @@ function simulate(integrator, steps; log=false)
(integrator.p.iter - start) / steps
end

integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)

av_steps = simulate(integrator, STEPS, log=SAVE_PNG)
if PLOT_PERFORMANCE
Expand Down
24 changes: 12 additions & 12 deletions examples/depower_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@ end

using KiteViewers, KiteModels, KitePodModels, Rotations

const Model = KPS4

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

# the following values can be changed to match your interest
dt::Float64 = 0.05
const TIME = 50
const TIME_LAPSE_RATIO = 5
const STEPS = Int64(round(TIME/dt))
TIME = 35
TIME_LAPSE_RATIO = 5
STEPS = Int64(round(TIME/dt))
STATISTIC = false
SHOW_VIEWER = true
SHOW_KITE = true
PLOT_PERFORMANCE = true
# end of user parameter section #

if Model==KPS3 SHOW_KITE = true end

if ! @isdefined time_vec_gc; const time_vec_gc = zeros(STEPS); end
if ! @isdefined time_vec_sim; const time_vec_sim = zeros(STEPS); end
if ! @isdefined time_vec_tot; const time_vec_tot = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
viewer::Viewer3D = Viewer3D(SHOW_KITE)

function simulate(integrator, steps)
println("Start simulation for $steps steps")
start = integrator.p.iter
start_time_ns = time_ns()
j=0; k=0
KiteViewers.clear_viewer(viewer)
GC.gc()
GC.enable(false)
max_time = 0
for i in 1:steps
if i == 300
Expand Down Expand Up @@ -68,21 +67,22 @@ function simulate(integrator, steps)
end
time_tot = end_time_ns - start_time_ns
start_time_ns = time_ns()
time_vec_tot[div(i, TIME_LAPSE_RATIO)] = time_tot/1e9/dt*1000*dt
time_vec_tot[div(i, TIME_LAPSE_RATIO)] = time_tot/1e9*1000 # calculate time in ms
end

time_vec_gc[i]=t_gc/dt*100.0
time_vec_sim[i]=t_sim/dt*100.0
if viewer.stop break end
# if viewer.stop break end
end
misses=j/k * 100
println("\nMissed the deadline for $(round(misses, digits=2)) %. Max time: $(round((max_time*1e-6), digits=1)) ms")
(integrator.p.iter - start) / steps
end

function play()
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)
simulate(integrator, STEPS)
GC.enable(true)
end

on(viewer.btn_PLAY.clicks) do c
Expand Down
11 changes: 4 additions & 7 deletions examples/joystick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ end

using KiteViewers, KiteModels, KitePodModels, Joysticks

# change this to KPS3 or KPS4
const Model = KPS4

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)
if ! @isdefined js;
const js = open_joystick();
const jsaxes = JSState();
Expand Down Expand Up @@ -38,7 +35,7 @@ end
function simulate(integrator)
start = integrator.p.iter
start_time_ns = time_ns()
clear_viewer(viewer)
clear_viewer(viewer; stop=false)
i=1
j=0; k=0
GC.gc()
Expand Down Expand Up @@ -89,7 +86,7 @@ end

function play()
global steps
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5)
steps = simulate(integrator)
GC.enable(true)
end
Expand Down
12 changes: 5 additions & 7 deletions examples/reelout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ end
using KiteViewers, KiteModels, KitePodModels, Rotations

# example program that shows
# a. how to create a video
# b. how to create a performance plot (simulation speed vs time)
const Model = KPS4
# a. how to create a performance plot (simulation speed vs time)

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

# the following values can be changed to match your interest
dt::Float64 = 0.05
Expand All @@ -25,7 +23,7 @@ SAVE_PNG = false
PLOT_PERFORMANCE = true
# end of user parameter section #

if ! @isdefined time_vec; const time_vec = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
time_vec::Vector{Float64} = zeros(div(STEPS, TIME_LAPSE_RATIO))
viewer::Viewer3D = Viewer3D(SHOW_KITE)

# ffmpeg -r:v 20 -i "video%06d.png" -codec:v libx264 -preset veryslow -pix_fmt yuv420p -crf 10 -an "video.mp4"
Expand All @@ -41,7 +39,7 @@ function simulate(integrator, steps; log=false)
time_ = 0.0
v_ro = 0.0
acc = 0.1
clear_viewer(viewer)
clear_viewer(viewer; stop=false)
for i in 1:steps
iter = kps4.iter
if i > 300
Expand Down
11 changes: 4 additions & 7 deletions examples/steering_4p.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ end

using KiteViewers, KiteModels, KitePodModels, Rotations

# change this to KPS3 or KPS4
const Model = KPS4

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

# the following values can be changed to match your interest
dt::Float64 = 0.05
Expand All @@ -30,7 +27,7 @@ end
function simulate(integrator, steps)
start = integrator.p.iter
start_time_ns = time_ns()
clear_viewer(viewer)
clear_viewer(viewer; stop=false)
for i in 1:steps
if i == 300
set_depower_steering(kps4.kcu, 0.25, 0.2)
Expand All @@ -53,7 +50,7 @@ function simulate(integrator, steps)
(integrator.p.iter - start) / steps
end

integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)

av_steps = simulate(integrator, STEPS)
nothing
11 changes: 5 additions & 6 deletions examples/steering_bench_video.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ using KiteViewers, KiteModels, KitePodModels, Rotations
# example program that shows
# a. how to create a video
# b. how to create a performance plot (simulation speed vs time)
const Model = KPS4

if ! @isdefined kcu; const kcu = KCU(se()); end
if ! @isdefined kps4; const kps4 = Model(kcu); end
kcu::KCU = KCU(se())
kps4::KPS4 = KPS4(kcu)

# the following values can be changed to match your interest
dt::Float64 = 0.05
Expand All @@ -24,7 +23,7 @@ SAVE_PNG = false
PLOT_PERFORMANCE = false
# end of user parameter section #

if ! @isdefined time_vec; const time_vec = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
time_vec::Vector{Float64} = zeros(div(STEPS, TIME_LAPSE_RATIO))
viewer::Viewer3D = Viewer3D(SHOW_KITE)

# ffmpeg -r:v 20 -i "video%06d.png" -codec:v libx264 -preset veryslow -pix_fmt yuv420p -crf 10 -an "video.mp4"
Expand All @@ -39,7 +38,7 @@ function simulate(integrator, steps; log=false)
start = integrator.p.iter
start_time_ns = time_ns()
time_ = 0.0
clear_viewer(viewer)
clear_viewer(viewer; stop=false)
for i in 1:steps
iter = kps4.iter
if i == 300
Expand Down Expand Up @@ -70,7 +69,7 @@ function simulate(integrator, steps; log=false)
(integrator.p.iter - start) / steps
end

integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)

av_steps = simulate(integrator, STEPS, log=SAVE_PNG)
if PLOT_PERFORMANCE
Expand Down
2 changes: 1 addition & 1 deletion mwes/mwe_01.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PLOT_PERFORMANCE = false

SHOW_KITE = false

if ! @isdefined time_vec; const time_vec = zeros(div(STEPS, TIME_LAPSE_RATIO)); end
time_vec::Vector{Float64} = zeros(div(STEPS, TIME_LAPSE_RATIO))
viewer::Viewer3D = Viewer3D(SHOW_KITE)

# viewer=Viewer3D(true);
Expand Down
6 changes: 4 additions & 2 deletions src/viewer3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ mutable struct Viewer3D <: AKV
stop::Bool
end

function clear_viewer(kv::AKV)
function clear_viewer(kv::AKV; stop=true)
kv.step = 1
kv.energy = 0
stop(kv)
if stop
stop(kv)
end
end

function stop(kv::AKV)
Expand Down
2 changes: 1 addition & 1 deletion test/test_for_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let
(integrator.p.iter - start) / steps
end

integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)

av_steps = simulate(integrator, STEPS)
plot(1:10)
Expand Down
2 changes: 1 addition & 1 deletion test/test_steering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function simulate(integrator, steps)
(integrator.p.iter - start) / steps
end

integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.04, prn=STATISTIC)
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=STATISTIC)

av_steps = simulate(integrator, STEPS)
nothing

0 comments on commit ecbad46

Please sign in to comment.