Skip to content

Commit 3b0bf64

Browse files
Add SpringDamper component
1 parent 3a9340b commit 3b0bf64

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

src/Mechanical/PlanarMechanics/PlanarMechanics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ D = Differential(t)
1616
export Frame, FrameResolve, PartialTwoFrames, ZeroPosition
1717
include("utils.jl")
1818

19-
export Fixed, Body, FixedTranslation
19+
export Fixed, Body, FixedTranslation, SpringDamper
2020
include("components.jl")
2121

2222
export Revolute, Prismatic

src/Mechanical/PlanarMechanics/components.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,66 @@ A fixed translation between two components (rigid rod)
150150
frame_a.j + frame_b.j + r0[1] * frame_b.fy - r0[2] * frame_b.fx ~ 0
151151
end
152152
end
153+
154+
"""
155+
156+
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Parts/SpringDamper.mo#L154C20-L154C20
157+
"""
158+
@mtkmodel SpringDamper begin
159+
@extend frame_a, frame_b = partial_frames = PartialTwoFrames()
160+
161+
@parameters begin
162+
c_x = 1, [description = "Spring constant in x dir"]
163+
c_y = 1, [description = "Spring constant in y dir"]
164+
c_phi = 1.0e5, [description = "Spring constant in phi dir"]
165+
d_x = 1, [description = "Damping constant in x dir"]
166+
d_y = 1, [description = "Damping constant in y dir"]
167+
d_phi = 1, [description = "Damping constant in phi dir"]
168+
s_relx0 = 0, [description = "Unstretched spring length"]
169+
s_rely0 = 0, [description = "Unstretched spring length"]
170+
phi_rel0 = 0, [description = "Unstretched spring length"]
171+
s_small = 1.e-10,
172+
[
173+
description = "Prevent zero-division if distance between frame_a and frame_b is zero",
174+
]
175+
end
176+
177+
@variables begin
178+
v_relx(t)
179+
v_rely(t)
180+
ω_rel(t) = 0
181+
s_relx(t)
182+
s_rely(t)
183+
phi_rel(t) = 0
184+
f_x(t)
185+
f_y(t)
186+
j(t)
187+
end
188+
189+
begin
190+
r_rel_0 = [s_relx, s_rely, 0]
191+
l = sqrt(r_rel_0' * r_rel_0)
192+
e_rel_0 = r_rel_0 / max(l, s_small)
193+
end
194+
195+
@equations begin
196+
s_relx ~ frame_b.x - frame_a.x
197+
s_rely ~ frame_b.y - frame_a.y
198+
phi_rel ~ frame_b.phi - frame_a.phi
199+
v_relx ~ D(s_relx)
200+
v_rely ~ D(s_rely)
201+
ω_rel ~ D(phi_rel)
202+
203+
j ~ c_phi * (phi_rel - phi_rel0) + d_phi * ω_rel
204+
frame_a.j ~ -j
205+
frame_b.j ~ j
206+
f_x ~ c_x * (s_relx - s_relx0) + d_x * v_relx
207+
f_y ~ c_y * (s_rely - s_rely0) + d_y * v_rely
208+
frame_a.fx ~ -f_x
209+
frame_b.fx ~ f_x
210+
frame_a.fy ~ -f_y
211+
frame_b.fy ~ f_y
212+
213+
# lossPower ~ d_x * v_relx * v_relx + d_y * v_rely * v_rely
214+
end
215+
end

test/Mechanical/planar_mechanics.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,40 @@ end
207207
end
208208

209209
@testset "Measure Demo" begin end
210+
211+
@testset "SpringDamper" begin
212+
# https://github.com/dzimmer/PlanarMechanics/blob/master/PlanarMechanics/Examples/SpringDamperDemo.mo
213+
@named spring_damper = SpringDamper(;
214+
s_relx0 = 0,
215+
d_y = 1,
216+
s_rely0 = 0,
217+
d_phi = 0,
218+
c_y = 5,
219+
c_x = 5,
220+
d_x = 1,
221+
c_phi = 0)
222+
@named body = Body(; j = 0.1, m = 0.5, rx = 1, ry = 1)
223+
@named fixed = Fixed()
224+
@named fixed_translation = FixedTranslation(; rx = -1, ry = 0)
225+
226+
connections = [
227+
connect(fixed.frame, fixed_translation.frame_a),
228+
connect(fixed_translation.frame_b, spring_damper.frame_a),
229+
connect(spring_damper.frame_b, body.frame),
230+
]
231+
@named model = ODESystem(connections,
232+
t,
233+
[],
234+
[],
235+
systems = [
236+
spring_damper,
237+
body,
238+
fixed,
239+
fixed_translation,
240+
])
241+
sys = structural_simplify(model)
242+
unset_vars = setdiff(states(sys), keys(ModelingToolkit.defaults(sys)))
243+
prob = ODEProblem(sys, unset_vars .=> 0.0, (0, 5), []; jac = true)
244+
sol = solve(prob, Rodas5P())
245+
@test SciMLBase.successful_retcode(sol)
246+
end

0 commit comments

Comments
 (0)