Skip to content

Commit 87b07ad

Browse files
authored
Add Translational library similar to MSL (#151)
* Add Translational library similar to MSL * add tests * fix tests * add partial and constant torque soruces * rm tau from InternalSupport state variables * format * add docs and tests for constant torque
1 parent a8428c8 commit 87b07ad

File tree

10 files changed

+487
-22
lines changed

10 files changed

+487
-22
lines changed

src/Mechanical/Mechanical.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using ModelingToolkit
88
include("Rotational/Rotational.jl")
99
include("Translational/Translational.jl")
1010
include("TranslationalPosition/TranslationalPosition.jl")
11+
include("TranslationalModelica/TranslationalModelica.jl")
1112
include("MultiBody2D/MultiBody2D.jl")
1213

1314
end

src/Mechanical/Rotational/Rotational.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include("utils.jl")
1515
export Fixed, Inertia, Spring, Damper, IdealGear, RotationalFriction
1616
include("components.jl")
1717

18-
export Torque, Speed
18+
export Torque, ConstantTorque, Speed
1919
include("sources.jl")
2020

2121
export AngleSensor, SpeedSensor, TorqueSensor, RelSpeedSensor

src/Mechanical/Rotational/sources.jl

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
2+
function PartialTorque(; name, use_support = false)
3+
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support = use_support)
4+
@unpack flange, phi_support = partial_element
5+
@variables phi(t) [
6+
description = "Angle of flange with respect to support (= flange.phi - support.phi)",
7+
]
8+
eqs = [phi ~ flange.phi - phi_support]
9+
return extend(ODESystem(eqs, t; name = name), partial_element)
10+
end
11+
112
"""
213
Torque(; name, use_support=false)
314
415
Input signal acting as external torque on a flange
516
617
# States:
718
8-
- `phi_support(t)`: [`rad`] Absolute angle of support flange"
19+
- `phi_support(t)`: [`rad`] Absolute angle of support flange
920
1021
# Connectors:
1122
@@ -25,7 +36,41 @@ Input signal acting as external torque on a flange
2536
end
2637

2738
"""
28-
Speed(; name, use_support=false, exact=false, f_crit=50)
39+
ConstantTorque(; name, tau_constant, use_support = false)
40+
41+
Constant torque source
42+
43+
# State variables:
44+
45+
- `phi_support(t)`: [`rad`] Absolute angle of support flange, only available if `use_support = true`
46+
- `tau`: Accelerating torque acting at flange (= -flange.tau)
47+
- `w`: Angular velocity of flange with respect to support (= der(phi))
48+
49+
# Connectors:
50+
- `flange` [Flange](@ref)
51+
52+
# Arguments:
53+
- `tau_constant`: The constant torque applied by the source
54+
- `use_support`: Whether or not an internal support flange is added, defaults to false.
55+
"""
56+
function ConstantTorque(; name, tau_constant, use_support = false)
57+
@named partial_element = PartialTorque(; use_support)
58+
@unpack flange, phi = partial_element
59+
@parameters tau_constant=tau_constant [
60+
description = "Constant torque (if negative, torque is acting as load in positive direction of rotation)",
61+
]
62+
@variables tau(t) [description = "Accelerating torque acting at flange (= -flange.tau)"]
63+
@variables w(t) [
64+
description = "Angular velocity of flange with respect to support (= der(phi))",
65+
]
66+
eqs = [w ~ D(phi)
67+
tau ~ -flange.tau
68+
tau ~ tau_constant]
69+
return extend(ODESystem(eqs, t; name = name), partial_element)
70+
end
71+
72+
"""
73+
Speed(; name, use_support=false, exact=false, f_crit=50)
2974
3075
Forced movement of a flange according to a reference angular velocity signal
3176

src/Mechanical/Rotational/utils.jl

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ODESystem(Equation[], t, sts, [], name = name, defaults = Dict(phi => 0.0, tau => 0.0))
55
end
66
Base.@doc """
7-
Flange(;name)
7+
Support(;name)
88
99
1-dim. rotational flange of a shaft.
1010
@@ -14,10 +14,31 @@ Base.@doc """
1414
""" Flange
1515

1616
@connector function Support(; name)
17-
sts = @variables(phi(t), [description = "Rotation angle of support $name"],
18-
tau(t), [connect = Flow, description = "Cut torque in support $name"],)
19-
ODESystem(Equation[], t, sts, [], name = name, defaults = Dict(phi => 0.0, tau => 0.0))
17+
@named flange = Flange()
18+
extend(ODESystem(Equation[], t, [], [], name = name), flange)
2019
end
20+
21+
# Base.@doc """
22+
# InternalSupport(;name, tau)
23+
24+
# 1-dim. rotational flange of a shaft.
25+
26+
# - `tau`: External support torque (must be computed via torque balance in model where InternalSupport is used; = flange.tau)
27+
28+
# # States:
29+
# - `phi(t)`: [`rad`] Absolute rotation angle of flange
30+
# - `tau(t)`: [`N.m`] Cut torque in the flange
31+
# """ Flange
32+
33+
# @connector function InternalSupport(; name, tau)
34+
# @named flange = Flange()
35+
# @variables phi(t)=0 [description = "Rotation angle of support $name"]
36+
# # tau(t), [connect = Flow, description = "Cut torque in support $name"],)
37+
# equations = [flange.tau ~ tau
38+
# flange.phi ~ phi]
39+
# ODESystem(equations, t, [phi], [], name = name, systems = [flange]) # NOTE: tau not included since it belongs elsewhere
40+
# end
41+
2142
Base.@doc """
2243
Support(;name)
2344
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Library to model 1-dimensional, translational mechanical components.
3+
"""
4+
module TranslationalModelica
5+
6+
using ModelingToolkit, Symbolics, IfElse
7+
using ...Blocks: RealInput, RealOutput
8+
9+
@parameters t
10+
D = Differential(t)
11+
12+
export Flange
13+
include("utils.jl")
14+
15+
export Fixed, Mass, Spring, Damper, IdealGear
16+
include("components.jl")
17+
18+
export Force
19+
include("sources.jl")
20+
21+
end
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
Fixed(;name, s0=0.0)
3+
4+
Flange fixed in housing at a given position.
5+
6+
# Parameters:
7+
8+
- `s0`: [m] Fixed offset position of housing
9+
10+
# Connectors:
11+
12+
- `flange: 1-dim. translational flange`
13+
"""
14+
function Fixed(; name, s0 = 0.0)
15+
pars = @parameters s0 = s0
16+
vars = []
17+
18+
@named flange = Flange()
19+
20+
eqs = [flange.s ~ s0]
21+
22+
return compose(ODESystem(eqs, t, vars, pars; name = name, defaults = [flange.s => s0]),
23+
flange)
24+
end
25+
26+
"""
27+
Mass(; name, m, s0 = 0.0, v0 = 0.0)
28+
29+
Sliding mass with inertia
30+
31+
# Parameters:
32+
33+
- `m`: [kg] Mass of sliding mass
34+
- `s0`: [m] Initial value of absolute position of sliding mass
35+
- `v0`: [m/s] Initial value of absolute linear velocity of sliding mass
36+
37+
# States:
38+
39+
- `s`: [m] Absolute position of sliding mass
40+
- `v`: [m/s] Absolute linear velocity of sliding mass (= D(s))
41+
42+
# Connectors:
43+
44+
- `flange: 1-dim. translational flange of mass`
45+
"""
46+
function Mass(m; name, s0 = 0.0, v0 = 0.0)
47+
@named pr = PartialRigid(; L = 0, s0)
48+
@unpack flange_a, flange_b, s = pr
49+
@parameters m=m [description = "Mass of sliding mass [kg]"]
50+
@variables v(t)=v0 [description = "Absolute linear velocity of sliding mass [m/s]"]
51+
@variables a(t)=0 [description = "Absolute linear acceleration of sliding mass [m/s^2]"]
52+
eqs = [v ~ D(s)
53+
a ~ D(v)
54+
m * a ~ flange_a.f + flange_b.f]
55+
return extend(ODESystem(eqs, t; name), pr)
56+
end
57+
58+
"""
59+
Spring(c; name, s_rel0=0)
60+
61+
Linear 1D translational spring
62+
63+
# Parameters:
64+
65+
- `c`: [N/m] Spring constant
66+
- `s_rel0`: Unstretched spring length
67+
68+
# Connectors:
69+
70+
- `flange_a: 1-dim. translational flange on one side of spring`
71+
- `flange_b: 1-dim. translational flange on opposite side of spring` #default function
72+
"""
73+
function Spring(c; name, s_rel0 = 0)
74+
@named pc = PartialCompliant()
75+
@unpack flange_a, flange_b, s_rel, f = pc
76+
@parameters c=c [description = "Spring constant [N/m]"]
77+
@parameters s_rel0=s_rel0 [description = "Unstretched spring length [m]"]
78+
79+
eqs = [f ~ c * (s_rel - s_rel0)]
80+
return extend(ODESystem(eqs, t; name), pc)
81+
end
82+
83+
"""
84+
Damper(d; name)
85+
86+
Linear 1D translational damper
87+
88+
# Parameters:
89+
90+
- `d`: [N.s/m] Damping constant
91+
92+
# Connectors:
93+
94+
- `flange_a: 1-dim. translational flange on one side of damper`
95+
- `flange_b: 1-dim. translational flange on opposite side of damper`
96+
"""
97+
function Damper(d; name)
98+
@named pc = PartialCompliantWithRelativeStates()
99+
@unpack flange_a, flange_b, v_rel, f = pc
100+
@parameters d=d [description = "Damping constant [Ns/m]"]
101+
@variables lossPower(t)=0 [description = "Power dissipated by the damper [W]"]
102+
eqs = [f ~ d * v_rel; lossPower ~ f * v_rel]
103+
return extend(ODESystem(eqs, t; name), pc)
104+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Force(;name)
3+
4+
Input signal acting as external force on a flange
5+
"""
6+
function Force(; name, use_support = false)
7+
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support = use_support)
8+
@unpack flange = partial_element
9+
@named f = RealInput() # Accelerating force acting at flange (= -flange.tau)
10+
eqs = [flange.f ~ -f.u]
11+
return extend(ODESystem(eqs, t, [], []; name = name, systems = [f]), partial_element)
12+
end

0 commit comments

Comments
 (0)