Skip to content

Commit

Permalink
a bit of tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulrhmnGhanem committed Dec 20, 2023
1 parent 3094ba3 commit 7051dbe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/Mechanical/PlanarMechanics/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Frame fixed in the planar world frame at a given position and orientation
# Connectors:
- `frame: 2-dim. Coordinate system
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Parts/Fixed.mo
"""
@mtkmodel Fixed begin
@parameters begin
Expand Down Expand Up @@ -58,12 +60,14 @@ Body component with mass and inertia
# Connectors:
- `frame`: 2-dim. Coordinate system
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Parts/Body.mo
"""
@component function Body(; name, m, j, rx = 0, ry = 0, gy = -9.807)
@component function Body(; name, m, I, rx = 0, ry = 0, gy = -9.807)
@named frame = Frame()
pars = @parameters begin
m = m
j = j
I = I
gy = gy
end

Expand Down Expand Up @@ -98,7 +102,7 @@ Body component with mass and inertia
fy ~ frame.fy,
ax ~ fx / m,
ay ~ ifelse(gy !== nothing, fy / m + gy, fy / m),
j * α ~ frame.j,
I * α ~ frame.j,
]

return compose(ODESystem(eqs, t, vars, pars; name = name),
Expand All @@ -119,6 +123,8 @@ A fixed translation between two components (rigid rod)
- `frame_a` [Frame](@ref) Coordinate system fixed to the component with one cut-force and cut-torque
- `frame_b` [Frame](@ref) Coordinate system fixed to the component with one cut-force and cut-torque
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Parts/FixedTranslation.mo
"""
@mtkmodel FixedTranslation begin
@extend frame_a, frame_b = partial_frames = PartialTwoFrames()
Expand Down Expand Up @@ -148,7 +154,7 @@ A fixed translation between two components (rigid rod)
# balancing force including lever principle
frame_a.fx + frame_b.fx ~ 0
frame_a.fy + frame_b.fy ~ 0
frame_a.j + frame_b.j + r0[1] * frame_b.fy - r0[2] * frame_b.fx ~ 0
frame_a.j + frame_b.j + r0' * [frame_b.fy, -frame_b.fx] ~ 0
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/Mechanical/PlanarMechanics/joints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A revolute joint
- `flange_a` [Flange](@ref) if `use_flange == true`
- `support` [Support](@ref) if `use_flange == true`
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Joints/Revolute.mo
"""
@component function Revolute(;
name,
Expand Down Expand Up @@ -96,6 +97,8 @@ A prismatic joint
- `fixed` [Fixed](@ref) if `use_flange == false`
- `flange_a` [Flange](@ref) if `use_flange == true`
- `support` [Support](@ref) if `use_flange == true`
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Joints/Prismatic.mo
"""
@component function Prismatic(;
name,
Expand Down
33 changes: 20 additions & 13 deletions test/Mechanical/planar_mechanics.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# using Revise
# using Plots
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkitStandardLibrary.Mechanical.PlanarMechanics

Expand All @@ -9,8 +11,8 @@ g = -9.807
@testset "Free body" begin
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/FreeBody.mo
m = 2
j = 1
@named body = Body(; m, j)
I = 1
@named body = Body(; m, I)
@named model = ODESystem(Equation[],
t,
[],
Expand All @@ -34,7 +36,7 @@ end
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/Pendulum.mo
@named ceiling = Fixed()
@named rod = FixedTranslation(rx = 1.0, ry = 0.0)
@named body = Body(m = 1, j = 0.1)
@named body = Body(m = 1, I = 0.1)
@named revolute = Revolute()

connections = [
Expand All @@ -50,7 +52,12 @@ end
systems = [body, revolute, rod, ceiling])
sys = structural_simplify(model)

@test length(states(sys)) == 7
@test_broken length(states(sys)) == 2
unset_vars = setdiff(states(sys), keys(ModelingToolkit.defaults(sys)))
prob = ODEProblem(sys, unset_vars .=> 0.0, tspan, []; jac = true)

sol = solve(prob, Rodas5P())
@test_broken SciMLBase.successful_retcode(sol)
end

@testset "Prismatic" begin
Expand All @@ -61,12 +68,12 @@ end
@testset "AbsoluteAccCentrifugal" begin
# https://github.com/dzimmer/PlanarMechanics/blob/443b007bcc1522bb172f13012e2d7a8ecc3f7a9b/PlanarMechanicsTest/Sensors.mo#L221-L332
m = 1
j = 0.1
I = 0.1
ω = 10
resolve_in_frame = :world

# components
@named body = Body(; m, j, gy = 0.0)
@named body = Body(; m, I, gy = 0.0)
@named fixed_translation = FixedTranslation(; rx = 10.0, ry = 0.0)
@named fixed = Fixed()
@named revolute = Revolute(constant_ω = ω)
Expand Down Expand Up @@ -114,11 +121,11 @@ end

@testset "Sensors (two free falling bodies)" begin
m = 1
j = 1
I = 1
resolve_in_frame = :world

@named body1 = Body(; m, j)
@named body2 = Body(; m, j)
@named body1 = Body(; m, I)
@named body2 = Body(; m, I)
@named base = Fixed()

@named abs_pos_sensor = AbsolutePosition(; resolve_in_frame)
Expand Down Expand Up @@ -209,10 +216,10 @@ end

@testset "Measure Demo" begin
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/MeasureDemo.mo
@named body = Body(; m = 1, j = 0.1)
@named body = Body(; m = 1, I = 0.1)
@named fixed_translation = FixedTranslation(; rx = 1, ry = 0)
@named fixed = Fixed()
@named body1 = Body(; m = 0.4, j = 0.02)
@named body1 = Body(; m = 0.4, I = 0.02)
@named fixed_translation1 = FixedTranslation(; rx = 0.4, ry = 0)
@named abs_pos_sensor = AbsolutePosition(; resolve_in_frame = :world)
@named rel_pos_sensor = RelativePosition(; resolve_in_frame = :world)
Expand Down Expand Up @@ -274,7 +281,7 @@ end
c_x = 5,
d_x = 1,
c_phi = 0)
@named body = Body(; j = 0.1, m = 0.5, rx = 1, ry = 1)
@named body = Body(; I = 0.1, m = 0.5, rx = 1, ry = 1)
@named fixed = Fixed()
@named fixed_translation = FixedTranslation(; rx = -1, ry = 0)

Expand Down Expand Up @@ -302,7 +309,7 @@ end

@testset "Spring and damper demo" begin
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/SpringDemo.mo
@named body = Body(; m = 0.5, j = 0.1)
@named body = Body(; m = 0.5, I = 0.1)
@named fixed = Fixed()
@named spring = Spring(; c_y = 10, s_rely0 = -0.5, c_x = 1, c_phi = 1e5)
@named damper = Damper(d = 1)
Expand Down

0 comments on commit 7051dbe

Please sign in to comment.