Skip to content

Commit

Permalink
New example, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ufechner7 committed Apr 27, 2022
1 parent f45a122 commit 8b87700
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 84 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ you should see a kite on a tether.
The same example, but using the 4 point kite model:

```julia
using KiteViewers
using KiteUtils#main
viewer=Viewer3D(false);
segments=6
state=demo_state_4p(segments+1)
update_points(state.pos, segments, orient=state.orient)
Expand Down
64 changes: 0 additions & 64 deletions examples/plot.jl

This file was deleted.

88 changes: 88 additions & 0 deletions examples/steering_4p.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Pkg
if ! ("KiteModels" keys(Pkg.project().dependencies))
using TestEnv; TestEnv.activate()
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

# the following values can be changed to match your interest
dt = 0.05
TIME = 45
TIME_LAPSE_RATIO = 5
STEPS = Int64(round(TIME/dt))
STATISTIC = false
SHOW_KITE = false
# end of user parameter section #

if ! @isdefined viewer; const viewer = Viewer3D(SHOW_KITE); end

include("timers.jl")

function update_system(kps::KPS3, reltime; segments=se().segments)
scale = 0.08
pos_kite = kps.pos[end]
pos_before = kps.pos[end-1]
elevation = calc_elevation(pos_kite)
azimuth = azimuth_east(pos_kite)
force = winch_force(kps)
if SHOW_KITE
v_app = kps.v_apparent
rotation = rot(pos_kite, pos_before, v_app)
q = QuatRotation(rotation)
orient = MVector{4, Float32}(Rotations.params(q))
update_points(kps.pos, segments, scale, reltime, elevation, azimuth, force, orient=orient)
else
update_points(kps.pos, segments, scale, reltime, elevation, azimuth, force)
end
end

function update_system(kps::KPS4, reltime; segments=se().segments)
scale = 0.08
pos_kite = kps.pos[segments+1] # well, this is the position of the pod...
elevation = calc_elevation(pos_kite)
azimuth = azimuth_east(pos_kite)
force = winch_force(kps)
update_points(kps.pos, segments, scale, reltime, elevation, azimuth, force, scale_kite=3.5)
end

function simulate(integrator, steps)
start = integrator.p.iter
start_time = time()
for i in 1:steps
if i == 300
set_depower_steering(kps4.kcu, 0.25, 0.1)
elseif i == 302
set_depower_steering(kps4.kcu, 0.25, -0.1)
elseif i == 304
set_depower_steering(kps4.kcu, 0.25, 0.0)
elseif i == 350
set_depower_steering(kps4.kcu, 0.25, -0.04)
elseif i == 352
set_depower_steering(kps4.kcu, 0.25, 0.0)
end
# KitePodModels.on_timer(kcu, dt)
KiteModels.next_step!(kps4, integrator, dt=dt)
reltime = i*dt
if mod(i, TIME_LAPSE_RATIO) == 0 || i == steps
update_system(kps4, reltime; segments=se().segments)
if start_time+dt > time() + 0.002
wait_until(start_time+dt)
else
sleep(0.001)
end
start_time = time()
end
end
(integrator.p.iter - start) / steps
end

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

av_steps = simulate(integrator, STEPS)
nothing
26 changes: 13 additions & 13 deletions src/viewer3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ SOFTWARE. =#
textsize = Node(TEXT_SIZE)
textsize2 = Node(AXIS_LABEL_SIZE)
status = Node("")
p1 = Node(Vector{Point2f0}(undef, 6000)) # 5 min
p2 = Node(Vector{Point2f0}(undef, 6000)) # 5 min
p1 = Node(Vector{Point2f}(undef, 6000)) # 5 min
p2 = Node(Vector{Point2f}(undef, 6000)) # 5 min
pos_x = Node(0.0f0)

points = Vector{Point3f0}(undef, se().segments+1+4)
points = Vector{Point3f}(undef, se().segments+1+4)
quat = Node(Quaternionf0(0,0,0,1)) # orientation of the kite
kite_pos = Node(Point3f0(1,0,0)) # position of the kite
positions = Node([Point3f0(x,0,0) for x in 1:se().segments+KITE_SPRINGS]) # positions of the tether segments
part_positions = Node([Point3f0(x,0,0) for x in 1:se().segments+1+4]) # positions of the tether particles
markersizes = Node([Point3f0(1,1,1) for x in 1:se().segments+KITE_SPRINGS]) # includes the segment length
rotations = Node([Point3f0(1,0,0) for x in 1:se().segments+KITE_SPRINGS]) # unit vectors corresponding with
kite_pos = Node(Point3f(1,0,0)) # position of the kite
positions = Node([Point3f(x,0,0) for x in 1:se().segments+KITE_SPRINGS]) # positions of the tether segments
part_positions = Node([Point3f(x,0,0) for x in 1:se().segments+1+4]) # positions of the tether particles
markersizes = Node([Point3f(1,1,1) for x in 1:se().segments+KITE_SPRINGS]) # includes the segment length
rotations = Node([Point3f(1,0,0) for x in 1:se().segments+KITE_SPRINGS]) # unit vectors corresponding with
# the orientation of the segments
energy = [0.0]
end
Expand All @@ -67,7 +67,7 @@ end

function Viewer3D(show_kite=true)
KiteUtils.set_data_path(datapath)
scene, layout = layoutscene(resolution = (840, 900), backgroundcolor = RGBf0(0.7, 0.8, 1))
scene, layout = layoutscene(resolution = (840, 900), backgroundcolor = RGBf(0.7, 0.8, 1))
scene3D = LScene(scene, scenekw = (show_axis=false, limits = Rect(-7,-10.0,0, 11,10,11), resolution = (800, 800)), raw=false)
create_coordinate_system(scene3D)
cam = cameracontrols(scene3D.scene)
Expand All @@ -80,11 +80,11 @@ function Viewer3D(show_kite=true)

textsize[] = TEXT_SIZE
textsize2[] = AXIS_LABEL_SIZE
text!(scene3D, "z", position = Point3f0(0, 0, 14.6), textsize = textsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "x", position = Point3f0(17, 0,0), textsize = textsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "y", position = Point3f0( 0, 14.5, 0), textsize = textsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "z", position = Point3f(0, 0, 14.6), textsize = textsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "x", position = Point3f(17, 0,0), textsize = textsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "y", position = Point3f( 0, 14.5, 0), textsize = textsize2, align = (:center, :center), show_axis = false)

text!(scene, status, position = Point2f0( 20, 0), textsize = TEXT_SIZE, align = (:left, :bottom), show_axis = false)
text!(scene, status, position = Point2f( 20, 0), textsize = TEXT_SIZE, align = (:left, :bottom), show_axis = false)
status[]="Stopped"

layout[1, 1] = scene3D
Expand Down
14 changes: 12 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
using KiteViewers
using KiteViewers, KiteUtils
using Test

cd("..")
include("test_steering.jl")

@testset "KiteViewers.jl" begin
# Write your tests here.
segments=se().segments
pos_kite = kps4.pos[segments+1] # well, this is the position of the pod...
elevation = calc_elevation(pos_kite)
@test rad2deg(elevation) 70.31815477683752
azimuth = azimuth_east(pos_kite)
@test rad2deg(azimuth) 9.217613192065718
force = winch_force(kps4)
@test force 495.7191503651492
end
5 changes: 0 additions & 5 deletions test/test_steering.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using Pkg
if ! ("KiteModels" keys(Pkg.project().dependencies))
using TestEnv; TestEnv.activate()
end

using KiteViewers, KiteModels, KitePodModels, Rotations

# change this to KPS3 or KPS4
Expand Down

0 comments on commit 8b87700

Please sign in to comment.