Skip to content

Commit

Permalink
Good results with autopilot and GC disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ufechner7 committed Mar 16, 2024
1 parent a1fa252 commit ec494b7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
29 changes: 23 additions & 6 deletions examples/autopilot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if ! ("Plots" ∈ keys(Pkg.project().dependencies))
end
using Timers; tic()

using KiteControllers, KiteViewers, KiteModels
using KiteControllers, KiteViewers, KiteModels, StatsBase

const Model = KPS4

Expand All @@ -20,7 +20,7 @@ dt = wcs.dt

# the following values can be changed to match your interest
if ! @isdefined MAX_TIME; MAX_TIME=460; end
TIME_LAPSE_RATIO = 2
TIME_LAPSE_RATIO = 4
SHOW_KITE = true
# end of user parameter section #

Expand All @@ -33,13 +33,18 @@ if ! @isdefined viewer; const viewer = Viewer3D(SHOW_KITE); end
steps = 0
if ! @isdefined T; const T = zeros(Int64(MAX_TIME/dt)); end
if ! @isdefined DELTA_T; const DELTA_T = zeros(Int64(MAX_TIME/dt)); end
if ! @isdefined STEERING; const STEERING = zeros(Int64(MAX_TIME/dt)); end

function simulate(integrator)
start_time_ns = time_ns()
clear_viewer(viewer)
i=1
j=0; k=0
GC.gc()
GC.gc()
if Sys.total_memory()/1e9 > 24 && MAX_TIME < 500
GC.enable(false)
end
max_time = 0
t_gc_tot = 0
sys_state = SysState(kps4)
Expand All @@ -59,9 +64,11 @@ function simulate(integrator)
#
t_sim = @elapsed KiteModels.next_step!(kps4, integrator, v_ro=v_ro, dt=dt)
sys_state = SysState(kps4)
if i <= length(T)
if i <= length(T) && i > 10/dt
T[i] = dt * i
DELTA_T[i] = time_ns() - start_time_ns - 1e9*dt
# DELTA_T[i] = (time_ns() - start_time_ns - 1e9*dt)/1e6 + dt*1000
DELTA_T[i] = t_sim * 1000
STEERING[i] = sys_state.steering
end
on_new_systate(ssc, sys_state)
if mod(i, TIME_LAPSE_RATIO) == 0
Expand Down Expand Up @@ -126,6 +133,16 @@ on(viewer.btn_AUTO.clicks) do c; autopilot(); end
play()
stop(viewer)

GC.enable(true)
include("../test/plot.jl")
p1 = plot1(T, DELTA_T)

p1 = plot2(T, DELTA_T, STEERING, labels=["t_sim [ms]", "steering [-]"])
println("Mean time per timestep: $(mean(DELTA_T)) ms")
println("Maximum time per timestep: $(maximum(DELTA_T)) ms")
index=Int64(round(12/dt))
println("Maximum for t>12s : $(maximum(DELTA_T[index:end])) ms")

# GC disabled, Ryzen 7950X, 4x realtime
# Missed the deadline for 0.04 %. Max time: 166.1 ms
# Mean time per timestep: 3.0985575495652173 ms
# Maximum time per timestep: 11.14224 ms
# Maximum for t>12s : 11.14224 ms
72 changes: 37 additions & 35 deletions test/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,54 @@ using Pkg
if ! ("Plots" keys(Pkg.project().dependencies))
using TestEnv; TestEnv.activate()
end
import PyPlot;
import PyPlot as plt

function plot1(X, Y; label="", width=2, xtickfontsize=12, ytickfontsize=12, legendfontsize=12, fig="")
if fig != ""
PyPlot.figure(fig)
plt.figure(fig)
end
p = PyPlot.plot(X, Y; label)
PyPlot.grid(true)
p = plt.plot(X, Y; label)
plt.grid(true)
p
end

function plot2(X, Y1, Y2; labels=["", ""], fig="", title="")
fig_ = PyPlot.figure(fig, figsize=(8, 6))
ax1 = PyPlot.subplot(211)
PyPlot.suptitle(title, fontsize=14) # Super title
PyPlot.plot(X, Y1, label=labels[1]);
ylabel(labels[1], fontsize=14);
PyPlot.grid(true)
setp(ax1.get_xticklabels(), visible=false)
ax2 = PyPlot.subplot(212, sharex = ax1)
PyPlot.plot(X, Y2, label=labels[2])
PyPlot.grid(true)
ylabel(labels[2], fontsize=14); grid(true)
xlabel("time [s]", fontsize=14)
xlim(0, X[end])
tight_layout()
fig_ = plt.figure(fig, figsize=(8, 6))
ax1 = plt.subplot(211)
plt.suptitle(title, fontsize=14) # Super title
plt.plot(X, Y1, label=labels[1]);
plt.ylabel(labels[1], fontsize=14);
plt.grid(true)
plt.setp(ax1.get_xticklabels(), visible=false)
ax2 = plt.subplot(212, sharex = ax1)
plt.plot(X, Y2, label=labels[2])
plt.grid(true)
plt.ylabel(labels[2], fontsize=14);
plt.grid(true)
plt.xlabel("time [s]", fontsize=14)
plt.xlim(0, X[end])
plt.tight_layout()
end

function plot3(X, Y1, Y2, Y3; labels=["", "", ""], fig="", title="")
fig_ = PyPlot.figure(fig, figsize=(8, 6))
ax1 = PyPlot.subplot(311)
PyPlot.suptitle(title, fontsize=14) # Super title
PyPlot.plot(X, Y1, label=labels[1]);
fig_ = plt.figure(fig, figsize=(8, 6))
ax1 = plt.subplot(311)
plt.suptitle(title, fontsize=14) # Super title
plt.plot(X, Y1, label=labels[1]);
ylabel(labels[1], fontsize=14);
PyPlot.grid(true)
setp(ax1.get_xticklabels(), visible=false)
ax2 = PyPlot.subplot(312, sharex = ax1)
PyPlot.plot(X, Y2, label=labels[2])
PyPlot.grid(true)
plt.grid(true)
plt.setp(ax1.get_xticklabels(), visible=false)
ax2 = plt.subplot(312, sharex = ax1)
plt.plot(X, Y2, label=labels[2])
plt.grid(true)
ylabel(labels[2], fontsize=14); grid(true)
setp(ax2.get_xticklabels(), visible=false)
ax3 = PyPlot.subplot(313, sharex = ax1)
PyPlot.plot(X, Y3, label=labels[3])
PyPlot.grid(true)
ylabel(labels[3], fontsize=14); grid(true)
xlabel("time [s]", fontsize=14)
xlim(0, X[end])
tight_layout()
plt.setp(ax2.get_xticklabels(), visible=false)
ax3 = plt.subplot(313, sharex = ax1)
plt.plot(X, Y3, label=labels[3])
plt.grid(true)
plt.ylabel(labels[3], fontsize=14);
plt.grid(true)
plt.xlabel("time [s]", fontsize=14)
plt.xlim(0, X[end])
plt.tight_layout()
end

0 comments on commit ec494b7

Please sign in to comment.