Skip to content

Add Translational library similar to MSL #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Mechanical/Mechanical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using ModelingToolkit
include("Rotational/Rotational.jl")
include("Translational/Translational.jl")
include("TranslationalPosition/TranslationalPosition.jl")
include("TranslationalModelica/TranslationalModelica.jl")
include("MultiBody2D/MultiBody2D.jl")

end
2 changes: 1 addition & 1 deletion src/Mechanical/Rotational/Rotational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include("utils.jl")
export Fixed, Inertia, Spring, Damper, IdealGear, RotationalFriction
include("components.jl")

export Torque, Speed
export Torque, ConstantTorque, Speed
include("sources.jl")

export AngleSensor, SpeedSensor, TorqueSensor, RelSpeedSensor
Expand Down
49 changes: 47 additions & 2 deletions src/Mechanical/Rotational/sources.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@

function PartialTorque(; name, use_support = false)
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support = use_support)
@unpack flange, phi_support = partial_element
@variables phi(t) [
description = "Angle of flange with respect to support (= flange.phi - support.phi)",
]
eqs = [phi ~ flange.phi - phi_support]
return extend(ODESystem(eqs, t; name = name), partial_element)
end

"""
Torque(; name, use_support=false)

Input signal acting as external torque on a flange

# States:

- `phi_support(t)`: [`rad`] Absolute angle of support flange"
- `phi_support(t)`: [`rad`] Absolute angle of support flange

# Connectors:

Expand All @@ -25,7 +36,41 @@ Input signal acting as external torque on a flange
end

"""
Speed(; name, use_support=false, exact=false, f_crit=50)
ConstantTorque(; name, tau_constant, use_support = false)

Constant torque source

# State variables:

- `phi_support(t)`: [`rad`] Absolute angle of support flange, only available if `use_support = true`
- `tau`: Accelerating torque acting at flange (= -flange.tau)
- `w`: Angular velocity of flange with respect to support (= der(phi))

# Connectors:
- `flange` [Flange](@ref)

# Arguments:
- `tau_constant`: The constant torque applied by the source
- `use_support`: Whether or not an internal support flange is added, defaults to false.
"""
function ConstantTorque(; name, tau_constant, use_support = false)
@named partial_element = PartialTorque(; use_support)
@unpack flange, phi = partial_element
@parameters tau_constant=tau_constant [
description = "Constant torque (if negative, torque is acting as load in positive direction of rotation)",
]
@variables tau(t) [description = "Accelerating torque acting at flange (= -flange.tau)"]
@variables w(t) [
description = "Angular velocity of flange with respect to support (= der(phi))",
]
eqs = [w ~ D(phi)
tau ~ -flange.tau
tau ~ tau_constant]
return extend(ODESystem(eqs, t; name = name), partial_element)
end

"""
Speed(; name, use_support=false, exact=false, f_crit=50)

Forced movement of a flange according to a reference angular velocity signal

Expand Down
29 changes: 25 additions & 4 deletions src/Mechanical/Rotational/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ODESystem(Equation[], t, sts, [], name = name, defaults = Dict(phi => 0.0, tau => 0.0))
end
Base.@doc """
Flange(;name)
Support(;name)

1-dim. rotational flange of a shaft.

Expand All @@ -14,10 +14,31 @@ Base.@doc """
""" Flange

@connector function Support(; name)
sts = @variables(phi(t), [description = "Rotation angle of support $name"],
tau(t), [connect = Flow, description = "Cut torque in support $name"],)
ODESystem(Equation[], t, sts, [], name = name, defaults = Dict(phi => 0.0, tau => 0.0))
@named flange = Flange()
extend(ODESystem(Equation[], t, [], [], name = name), flange)
end

# Base.@doc """
# InternalSupport(;name, tau)

# 1-dim. rotational flange of a shaft.

# - `tau`: External support torque (must be computed via torque balance in model where InternalSupport is used; = flange.tau)

# # States:
# - `phi(t)`: [`rad`] Absolute rotation angle of flange
# - `tau(t)`: [`N.m`] Cut torque in the flange
# """ Flange

# @connector function InternalSupport(; name, tau)
# @named flange = Flange()
# @variables phi(t)=0 [description = "Rotation angle of support $name"]
# # tau(t), [connect = Flow, description = "Cut torque in support $name"],)
# equations = [flange.tau ~ tau
# flange.phi ~ phi]
# ODESystem(equations, t, [phi], [], name = name, systems = [flange]) # NOTE: tau not included since it belongs elsewhere
# end

Base.@doc """
Support(;name)

Expand Down
21 changes: 21 additions & 0 deletions src/Mechanical/TranslationalModelica/TranslationalModelica.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Library to model 1-dimensional, translational mechanical components.
"""
module TranslationalModelica

using ModelingToolkit, Symbolics, IfElse
using ...Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export Flange
include("utils.jl")

export Fixed, Mass, Spring, Damper, IdealGear
include("components.jl")

export Force
include("sources.jl")

end
104 changes: 104 additions & 0 deletions src/Mechanical/TranslationalModelica/components.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
Fixed(;name, s0=0.0)

Flange fixed in housing at a given position.

# Parameters:

- `s0`: [m] Fixed offset position of housing

# Connectors:

- `flange: 1-dim. translational flange`
"""
function Fixed(; name, s0 = 0.0)
pars = @parameters s0 = s0
vars = []

@named flange = Flange()

eqs = [flange.s ~ s0]

return compose(ODESystem(eqs, t, vars, pars; name = name, defaults = [flange.s => s0]),
flange)
end

"""
Mass(; name, m, s0 = 0.0, v0 = 0.0)

Sliding mass with inertia

# Parameters:

- `m`: [kg] Mass of sliding mass
- `s0`: [m] Initial value of absolute position of sliding mass
- `v0`: [m/s] Initial value of absolute linear velocity of sliding mass

# States:

- `s`: [m] Absolute position of sliding mass
- `v`: [m/s] Absolute linear velocity of sliding mass (= D(s))

# Connectors:

- `flange: 1-dim. translational flange of mass`
"""
function Mass(m; name, s0 = 0.0, v0 = 0.0)
@named pr = PartialRigid(; L = 0, s0)
@unpack flange_a, flange_b, s = pr
@parameters m=m [description = "Mass of sliding mass [kg]"]
@variables v(t)=v0 [description = "Absolute linear velocity of sliding mass [m/s]"]
@variables a(t)=0 [description = "Absolute linear acceleration of sliding mass [m/s^2]"]
eqs = [v ~ D(s)
a ~ D(v)
m * a ~ flange_a.f + flange_b.f]
return extend(ODESystem(eqs, t; name), pr)
end

"""
Spring(c; name, s_rel0=0)

Linear 1D translational spring

# Parameters:

- `c`: [N/m] Spring constant
- `s_rel0`: Unstretched spring length

# Connectors:

- `flange_a: 1-dim. translational flange on one side of spring`
- `flange_b: 1-dim. translational flange on opposite side of spring` #default function
"""
function Spring(c; name, s_rel0 = 0)
@named pc = PartialCompliant()
@unpack flange_a, flange_b, s_rel, f = pc
@parameters c=c [description = "Spring constant [N/m]"]
@parameters s_rel0=s_rel0 [description = "Unstretched spring length [m]"]

eqs = [f ~ c * (s_rel - s_rel0)]
return extend(ODESystem(eqs, t; name), pc)
end

"""
Damper(d; name)

Linear 1D translational damper

# Parameters:

- `d`: [N.s/m] Damping constant

# Connectors:

- `flange_a: 1-dim. translational flange on one side of damper`
- `flange_b: 1-dim. translational flange on opposite side of damper`
"""
function Damper(d; name)
@named pc = PartialCompliantWithRelativeStates()
@unpack flange_a, flange_b, v_rel, f = pc
@parameters d=d [description = "Damping constant [Ns/m]"]
@variables lossPower(t)=0 [description = "Power dissipated by the damper [W]"]
eqs = [f ~ d * v_rel; lossPower ~ f * v_rel]
return extend(ODESystem(eqs, t; name), pc)
end
12 changes: 12 additions & 0 deletions src/Mechanical/TranslationalModelica/sources.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Force(;name)

Input signal acting as external force on a flange
"""
function Force(; name, use_support = false)
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support = use_support)
@unpack flange = partial_element
@named f = RealInput() # Accelerating force acting at flange (= -flange.tau)
eqs = [flange.f ~ -f.u]
return extend(ODESystem(eqs, t, [], []; name = name, systems = [f]), partial_element)
end
Loading