Skip to content

Commit be93225

Browse files
adds DC motor with friction
1 parent a8bb688 commit be93225

File tree

11 files changed

+264
-37
lines changed

11 files changed

+264
-37
lines changed

src/Electrical/Analog/ideal_components.jl

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ See [OnePort](@ref)
2626
- `n` Negative pin
2727
2828
# Parameters:
29-
- `R`: [`Ω`] Resistance
29+
- `R`: [`Ohm`] Resistance
3030
"""
3131
function Resistor(; name, R)
3232
@named oneport = OnePort()
@@ -70,8 +70,7 @@ end
7070
Creates an ideal capacitor.
7171
7272
# States:
73-
- `v(t)`: [`V`]
74-
The voltage across the capacitor, given by `D(v) ~ p.i / C`
73+
- `v(t)`: [`V`] The voltage across the capacitor, given by `D(v) ~ p.i / C`
7574
7675
# Connectors:
7776
- `p` Positive pin
@@ -158,5 +157,79 @@ function Short(; name)
158157
@named oneport = OnePort()
159158
@unpack v, i = oneport
160159
eqs = [v ~ 0]
161-
extend(ODESystem(eqs, t, [], []; name = name), oneport)
160+
extend(ODESystem(eqs, t, [], []; name=name), oneport)
161+
end
162+
163+
"""
164+
HeatingResistor(;name, R_ref=1.0, T_ref=300.15, alpha=0)
165+
166+
Temperature dependent electrical resistor
167+
168+
# States
169+
- See [OnePort](@ref)
170+
- `R(t)`: [`Ohm`] Temperature dependent resistance `R ~ R_ref*(1 + alpha*(heat_port.T(t) - T_ref))`
171+
172+
# Connectors
173+
- `p` Positive pin
174+
- `n` Negative pin
175+
176+
# Parameters:
177+
- `R_ref`: [`Ω`] Reference resistance
178+
- `T_ref`: [K] Reference temperature
179+
"""
180+
function HeatingResistor(;name, R_ref=1.0, T_ref=300.15, alpha=0)
181+
@named oneport = OnePort()
182+
@unpack v, i = oneport
183+
@named heat_port = HeatPort()
184+
pars = @parameters begin
185+
R_ref=R_ref
186+
T_ref=T_ref
187+
alpha=alpha
188+
end
189+
@variables R
190+
eqs = [
191+
R ~ R_ref*(1 + alpha*(heat_port.T - T_ref))
192+
heat_port.Q_flow ~ -v * i # -LossPower
193+
v ~ i * R
194+
]
195+
extend(ODESystem(eqs, t, [R], pars; name=name, systems=[heat_port]), oneport)
196+
end
197+
198+
"""
199+
EMF(;name, k)
200+
201+
Electromotoric force (electric/mechanic transformer)
202+
203+
# States
204+
- `v(t)`: [`V`] The voltage across component `p.v - n.v`
205+
- `i(t)`: [`A`] The current passing through positive pin
206+
- `phi`: [`rad`] Rotation angle (=flange.phi - support.phi)
207+
- `w`: [`rad/s`] Angular velocity (= der(phi))
208+
209+
# Connectors
210+
- `p` [Pin](@ref) Positive pin
211+
- `n` [Pin](@ref) Negative pin
212+
- `flange` [Flange](@ref) Shaft of EMF shaft
213+
- `support` [Support](@ref) Support/housing of emf shaft
214+
215+
# Parameters:
216+
- `k`: [`N⋅m/A`] Transformation coefficient
217+
"""
218+
function EMF(;name, k)
219+
@named p = Pin()
220+
@named n = Pin()
221+
@named flange = Flange()
222+
@named support = Support()
223+
@parameters k=k
224+
@variables v(t)=0.0 i(t)=0.0 phi(t)=0.0 w(t)=0.0
225+
eqs = [
226+
v ~ p.v - n.v
227+
0 ~ p.i + n.i
228+
i ~ p.i
229+
phi ~ flange.phi - support.phi
230+
D(phi) ~ w
231+
k*w ~ v
232+
flange.tau ~ -k*i
233+
]
234+
ODESystem(eqs, t, [v, i, phi, w], [k]; name=name, systems=[p, n, flange, support])
162235
end

src/Electrical/Electrical.jl

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ module Electrical
66

77
using ModelingToolkit, Symbolics, IfElse
88
using OffsetArrays
9+
using ..Thermal: HeatPort
10+
using ..Mechanical.Rotational: Flange, Support
11+
using ..Blocks: RealInput, RealOutput
912

1013
@parameters t
1114
D = Differential(t)
1215

13-
using ..Blocks: RealInput, RealOutput
14-
16+
export Pin, OnePort
1517
include("utils.jl")
18+
19+
export Capacitor, Ground, Inductor, Resistor, Short, IdealOpAmp, EMF
1620
include("Analog/ideal_components.jl")
21+
22+
export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor
1723
include("Analog/sensors.jl")
24+
25+
export Voltage, Current
1826
include("Analog/sources.jl")
27+
1928
# include("Digital/components.jl")
2029
# include("Digital/gates.jl")
2130
# include("Digital/tables.jl")
@@ -26,22 +35,4 @@ include("Analog/sources.jl")
2635
# - machines
2736
# - multi-phase
2837

29-
export #Interface
30-
Pin,
31-
# Analog Components
32-
Capacitor, Ground, Inductor, Resistor, Conductor,
33-
Short, IdealOpAmp,
34-
# Analog Sensors
35-
CurrentSensor, PotentialSensor, VoltageSensor,
36-
PowerSensor, MultiSensor,
37-
# Analog Sources
38-
Voltage, Current
39-
40-
# # Digital Gates
41-
# And, Or, Not, Xor, Nand, Nor, Xnor,
42-
# # Digital components
43-
# HalfAdder, FullAdder, MUX, DEMUX, Encoder, Decoder,
44-
# # Digital Sources
45-
# DigitalPin, Pulse, PulseDiff
46-
4738
end

src/Mechanical/Rotational/components.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ end
3939
function Inertia(; name, J, phi_start = 0.0, w_start = 0.0, a_start = 0.0)
4040
@named flange_a = Flange()
4141
@named flange_b = Flange()
42-
@parameters J = J
42+
J > 0 || throw(ArgumentError("Expected `J` to be positive"))
43+
@parameters J=J
4344
sts = @variables begin
4445
phi(t) = phi_start
4546
w(t) = w_start
@@ -73,6 +74,7 @@ Linear 1D rotational spring
7374
function Spring(; name, c, phi_rel0 = 0.0)
7475
@named partial_comp = PartialCompliant()
7576
@unpack phi_rel, tau = partial_comp
77+
c > 0 || throw(ArgumentError("Expected `c` to be positive"))
7678
pars = @parameters begin
7779
c = c
7880
phi_rel0 = phi_rel0
@@ -102,9 +104,10 @@ Linear 1D rotational damper
102104
function Damper(; name, d)
103105
@named partial_comp = PartialCompliantWithRelativeStates()
104106
@unpack w_rel, tau = partial_comp
105-
pars = @parameters d = d
106-
eqs = [tau ~ d * w_rel]
107-
extend(ODESystem(eqs, t, [], pars; name = name), partial_comp)
107+
d > 0 || throw(ArgumentError("Expected `d` to be positive"))
108+
pars = @parameters d=d
109+
eqs = [tau ~ d*w_rel]
110+
extend(ODESystem(eqs, t, [], pars; name=name), partial_comp)
108111
end
109112

110113
"""
@@ -130,7 +133,8 @@ This element characterizes any type of gear box which is fixed in the ground and
130133
function IdealGear(; name, ratio, use_support = false)
131134
@named partial_element = PartialElementaryTwoFlangesAndSupport2(use_support = use_support)
132135
@unpack phi_support, flange_a, flange_b = partial_element
133-
@parameters ratio = ratio
136+
ratio > 0 || throw(ArgumentError("Expected `ratio` to be positive"))
137+
@parameters ratio=ratio
134138
sts = @variables phi_a(t)=0.0 phi_b(t)=0.0
135139
eqs = [phi_a ~ flange_a.phi - phi_support
136140
phi_b ~ flange_b.phi - phi_support

src/Mechanical/Rotational/sources.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Torque(;name)
2+
Torque(; name, use_support=false)
33
44
Input signal acting as external torque on a flange
55

src/Mechanical/Rotational/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Partial model for a component with one rotational 1-dim. shaft flange and a supp
118118
function PartialElementaryOneFlangeAndSupport2(; name, use_support = false)
119119
@named flange = Flange()
120120
sys = [flange]
121-
@variables phi_support(t)
121+
@variables phi_support(t)=0.0
122122
if use_support
123123
@named support = Support()
124124
eqs = [support.phi ~ phi_support

src/ModelingToolkitStandardLibrary.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module ModelingToolkitStandardLibrary
22

33
include("Blocks/Blocks.jl")
44
include("Mechanical/Mechanical.jl")
5+
include("Thermal/Thermal.jl")
56
include("Electrical/Electrical.jl")
67
include("Magnetic/Magnetic.jl")
7-
include("Thermal/Thermal.jl")
88

99
end

test/Blocks/sources.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ end
102102
sys = structural_simplify(iosys)
103103
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 10.0))
104104
sol = solve(prob, Rodas4())
105-
106105
@test sol.retcode == :Success
107106
@test sol[src.output.u]cosine.(sol.t, frequency, amplitude, phase, offset, start_time) atol=1e-3
108107

@@ -169,7 +168,6 @@ end
169168
sys = structural_simplify(iosys)
170169
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 10.0))
171170
sol = solve(prob, Rodas4())
172-
173171
@test sol.retcode == :Success
174172
@test sol[src.output.u]ramp.(sol.t, offset, height, duration, start_time) atol=1e-3
175173

@@ -365,7 +363,6 @@ end
365363
sys = structural_simplify(iosys)
366364
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 10.0))
367365
sol = solve(prob, Rodas4())
368-
369366
@test sol.retcode == :Success
370367
@test sol[src.output.u]exp_sine.(sol.t, amplitude, frequency, damping, phase,
371368
start_time) atol=1e-3

test/Electrical/analog.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ end
157157
systems = [resistor, capacitor, source, ground, voltage])
158158
sys = structural_simplify(model)
159159
prob = ODAEProblem(sys, [capacitor.v => 10.0], (0.0, 10.0))
160-
sol = solve(prob, Rodas5())
161-
@test sol.retcode == :Success
162160
sol = solve(prob, Tsit5())
163161
@test sol.retcode == :Success
162+
sol = solve(prob, Rodas4())
163+
@test sol.retcode == :Success
164164

165165
# Plots.plot(sol; vars=[voltage.v, capacitor.v])
166166
end

0 commit comments

Comments
 (0)