@@ -3,31 +3,109 @@ using ModelingToolkitStandardLibrary.Mechanical.MultiBody2D
3
3
using ModelingToolkitStandardLibrary. Mechanical. Translational
4
4
using DifferentialEquations
5
5
# using Setfield
6
+ using Test
6
7
7
8
@parameters t
8
9
9
- @named link = Link (; m = 1 , l = 10 , I = π * 8.33 , g = - 9.807 )
10
+ @named link1 = Link (; m = 1 , l = 10 , I = 84 , g = - 9.807 )
11
+ @named link2 = Link (; m = 1 , l = 10 , I = 84 , g = - 9.807 , x1_0= 10 )
10
12
@named cart = Mass (; m = 1 , s_0 = 0 )
11
13
# @named force = SineForce(;amp=3e3, freq=15)
12
- # @named fixed = Fixed()
14
+ @named fixed = Fixed ()
13
15
# @named m1 = Mass(;m=0.5)
14
16
# @named m2 = Mass(;m=0.5)
15
17
16
18
eqs = [
17
- connect (link . TX1, cart. flange), # , force.flange)
18
- # connect(link .TY1, fixed.flange)
19
- # connect(link .TX2, m1.flange )
20
- # connect(link .TY2, m2.flange )
19
+ connect (link1 . TX1, cart. flange) # , force.flange)
20
+ connect (link1 . TY1, fixed. flange)
21
+ connect (link1 . TX2, link2 . TX1 )
22
+ connect (link1 . TY2, link2 . TY1 )
21
23
]
22
24
23
- @named model = ODESystem (eqs, t, [], []; systems = [link, cart])
25
+ @named model = ODESystem (eqs, t, [], []; systems = [link1, link2, cart, fixed ])
24
26
27
+ # TODO : This gives an error
25
28
sys = structural_simplify (model)
26
- # sys = expand_connections(model)
27
- # @set! sys.eqs = [(typeof(eq.lhs) != ModelingToolkit.Connection) & !ModelingToolkit._iszero(eq.lhs) & !ModelingToolkit.isdifferential(eq.lhs) ? 0 ~ eq.rhs - eq.lhs : eq for eq in sys.eqs]
28
29
29
- prob = ODEProblem (sys, [], (0.0 , 5 ), []; jac = true )
30
+ # The below code does work...
31
+ #=
32
+ sys = expand_connections(model)
33
+ @set! sys.eqs = [(typeof(eq.lhs) != ModelingToolkit.Connection) & !ModelingToolkit._iszero(eq.lhs) & !ModelingToolkit.isdifferential(eq.lhs) ? 0 ~ eq.rhs - eq.lhs : eq for eq in sys.eqs]
34
+
35
+ prob = ODEProblem(sys, [], (0.0, 20), []; jac = true)
30
36
NEWTON = NLNewton(check_div = false, always_new = true, max_iter = 100, relax = 4 // 10)
31
- sol = solve (prob, ImplicitEuler (nlsolve = NEWTON), dt = 0.0001 , adaptive = false )
32
- # sol = solve(prob)
37
+ sol = solve(prob, ImplicitEuler(nlsolve = NEWTON), dt = 0.001, adaptive = false)
38
+
39
+ @test sol[cart.s][end] ≈ 4.767 atol=1e-3
40
+
33
41
plot(sol, idxs = [cart.s])
42
+ =#
43
+
44
+
45
+ #=
46
+ using CairoMakie
47
+ f = Figure()
48
+ a = Axis(f[1,1],xlabel="time [s]", ylabel="cart x pos. [m]")
49
+ lines!(a, sol.t, sol[cart.s])
50
+ f
51
+
52
+
53
+
54
+ function plot_link(sol, sys, tmax)
55
+ tm = Observable(0.0)
56
+ idx = Dict(reverse.(enumerate(states(sys))))
57
+
58
+ fig = Figure()
59
+ a = Axis(fig[1,1], aspect=DataAspect(), )
60
+ hidedecorations!(a)
61
+ s = @lift(sol($tm))
62
+
63
+ m1x1 = @lift($s[idx[link1.x1]])
64
+ m1x2 = @lift($s[idx[link1.x2]])
65
+ m2x1 = @lift($s[idx[link2.x1]])
66
+ m2x2 = @lift($s[idx[link2.x2]])
67
+
68
+ m1y1 = @lift($s[idx[link1.y1]])
69
+ m1y2 = @lift($s[idx[link1.y2]])
70
+ m2y1 = @lift($s[idx[link2.y1]])
71
+ m2y2 = @lift($s[idx[link2.y2]])
72
+
73
+ sz1 = 0.5
74
+ # lines!(a, [-sz1, sz1, sz1, -sz1, -sz1], @lift([$m1x1, $m1x1, $m1x1+sz1*2, $m1x1+sz1*2, $m1x1]))
75
+
76
+
77
+ lines!(a, @lift([$m1x1, $m1x2]), @lift([$m1y1, $m1y2]), linewidth=10, color=:blue)
78
+ lines!(a, @lift([$m2x1, $m2x2]), @lift([$m2y1, $m2y2]), linewidth=10, color=:red)
79
+
80
+ CairoMakie.ylims!(a, -40, 20)
81
+ CairoMakie.xlims!(a, -20, 40)
82
+
83
+ # a = Axis(fig[1, 1], xlabel="time [s]", ylabel="position [m]")
84
+ # lines!(a, sol.t, sol[2,:])
85
+ # lines!(a, sol.t, sol[4,:])
86
+
87
+ # scatter!(a, tm, m1x)
88
+ # scatter!(a, tm, m2x)
89
+ # ylims!(a, -60, 30)
90
+
91
+ framerate = 30
92
+ timestamps = range(0, tmax, step=1/framerate)
93
+
94
+
95
+ record(fig, "links.mp4", timestamps;
96
+ framerate = framerate) do t
97
+ tm[] = t
98
+ end
99
+
100
+ #=
101
+ CairoMakie.Makie.Record(fig, timestamps; framerate=framerate) do t
102
+ tm[] = t
103
+ end
104
+ =#
105
+
106
+ nothing
107
+ end
108
+
109
+ plot_link(sol, sys, 20)
110
+
111
+ =#
0 commit comments