Skip to content

Commit 99826ab

Browse files
ven-kChrisRackauckas
authored andcommitted
refactor: Mechanical/Rotational
1 parent f1459e3 commit 99826ab

File tree

5 files changed

+248
-200
lines changed

5 files changed

+248
-200
lines changed
+109-82
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Fixed(;name, phi0=0.0)
2+
Fixed(;name, phi0 = 0.0)
33
44
Flange fixed in housing at a given angle.
55
@@ -11,15 +11,20 @@ Flange fixed in housing at a given angle.
1111
1212
- `phi0`: [`rad`] Fixed offset angle of housing
1313
"""
14-
@component function Fixed(; name, phi0 = 0.0)
15-
@named flange = Flange()
16-
@parameters phi0=phi0 [description = "Fixed offset angle of flange"]
17-
eqs = [flange.phi ~ phi0]
18-
return compose(ODESystem(eqs, t, [], [phi0]; name = name), flange)
14+
@mtkmodel Fixed begin
15+
@components begin
16+
flange = Flange()
17+
end
18+
@parameters begin
19+
phi0 = 0.0, [description = "Fixed offset angle of flange"]
20+
end
21+
@equations begin
22+
flange.phi ~ phi0
23+
end
1924
end
2025

2126
"""
22-
Inertia(;name, J, phi_start=0.0, w_start=0.0, a_start=0.0)
27+
Inertia(;name, J, phi = 0.0, w = 0.0, a = 0.0)
2328
2429
1D-rotational component with inertia.
2530
@@ -37,29 +42,34 @@ end
3742
# Parameters:
3843
3944
- `J`: [`kg·m²`] Moment of inertia
40-
- `phi_start`: [`rad`] Initial value of absolute rotation angle of component
41-
- `w_start`: [`rad/s`] Initial value of absolute angular velocity of component
42-
- `a_start`: [`rad/s²`] Initial value of absolute angular acceleration of component
4345
"""
44-
@component function Inertia(; name, J, phi_start = 0.0, w_start = 0.0, a_start = 0.0)
45-
@named flange_a = Flange()
46-
@named flange_b = Flange()
47-
@symcheck J > 0 || throw(ArgumentError("Expected `J` to be positive"))
48-
@parameters J=J [description = "Moment of inertia of $name"]
49-
sts = @variables(phi(t)=phi_start, [description = "Absolute rotation angle of $name"],
50-
w(t)=w_start, [description = "Absolute angular velocity of $name"],
51-
a(t)=a_start,
52-
[description = "Absolute angular acceleration of $name"],)
53-
eqs = [phi ~ flange_a.phi
46+
@mtkmodel Inertia begin
47+
@parameters begin
48+
J, [description = "Moment of inertia"]
49+
end
50+
@components begin
51+
flange_a = Flange()
52+
flange_b = Flange()
53+
end
54+
begin
55+
@symcheck J > 0 || throw(ArgumentError("Expected `J` to be positive"))
56+
end
57+
@variables begin
58+
phi(t) = 0.0, [description = "Absolute rotation angle"]
59+
w(t) = 0.0, [description = "Absolute angular velocity"]
60+
a(t) = 0.0, [description = "Absolute angular acceleration"]
61+
end
62+
@equations begin
63+
phi ~ flange_a.phi
5464
phi ~ flange_b.phi
5565
D(phi) ~ w
5666
D(w) ~ a
57-
J * a ~ flange_a.tau + flange_b.tau]
58-
return compose(ODESystem(eqs, t, sts, [J]; name = name), flange_a, flange_b)
67+
J * a ~ flange_a.tau + flange_b.tau
68+
end
5969
end
6070

6171
"""
62-
Spring(;name, c, phi_rel0=0.0)
72+
Spring(; name, c, phi_rel0 = 0.0)
6373
6474
Linear 1D rotational spring
6575
@@ -78,19 +88,22 @@ Linear 1D rotational spring
7888
- `c`: [`N.m/rad`] Spring constant
7989
- `phi_rel0`: [`rad`] Unstretched spring angle
8090
"""
81-
@component function Spring(; name, c, phi_rel0 = 0.0)
82-
@named partial_comp = PartialCompliant()
83-
@unpack phi_rel, tau = partial_comp
84-
@symcheck c > 0 || throw(ArgumentError("Expected `c` to be positive"))
85-
pars = @parameters(c=c, [description = "Spring constant of $name"],
86-
phi_rel0=phi_rel0,
87-
[description = "Unstretched spring angle of $name"],)
88-
eqs = [tau ~ c * (phi_rel - phi_rel0)]
89-
extend(ODESystem(eqs, t, [], pars; name = name), partial_comp)
91+
@mtkmodel Spring begin
92+
@extend phi_rel, tau = partial_comp = PartialCompliant()
93+
begin
94+
@symcheck c > 0 || throw(ArgumentError("Expected `c` to be positive"))
95+
end
96+
@parameters begin
97+
c, [description = "Spring constant"]
98+
phi_rel0 = 0.0, [description = "Unstretched spring angle"]
99+
end
100+
@equations begin
101+
tau ~ c * (phi_rel - phi_rel0)
102+
end
90103
end
91104

92105
"""
93-
Damper(;name, d)
106+
Damper(; name, d)
94107
95108
Linear 1D rotational damper
96109
@@ -110,17 +123,20 @@ Linear 1D rotational damper
110123
111124
- `d`: [`N.m.s/rad`] Damping constant
112125
"""
113-
@component function Damper(; name, d)
114-
@named partial_comp = PartialCompliantWithRelativeStates()
115-
@unpack w_rel, tau = partial_comp
116-
@symcheck d > 0 || throw(ArgumentError("Expected `d` to be positive"))
117-
pars = @parameters d=d [description = "Damping constant of $name"]
118-
eqs = [tau ~ d * w_rel]
119-
extend(ODESystem(eqs, t, [], pars; name = name), partial_comp)
126+
@mtkmodel Damper begin
127+
@extend w_rel, tau = partial_comp = PartialCompliantWithRelativeStates()
128+
begin
129+
@symcheck d > 0 || throw(ArgumentError("Expected `d` to be positive"))
130+
end
131+
@parameters begin
132+
d, [description = "Damping constant"]
133+
end
134+
@equations begin
135+
tau ~ d * w_rel
136+
end
120137
end
121-
122138
"""
123-
SpringDamper(;name, d)
139+
SpringDamper(; name, d)
124140
125141
Linear 1D rotational spring and damper
126142
@@ -140,23 +156,28 @@ Linear 1D rotational spring and damper
140156
141157
- `d`: [`N.m.s/rad`] Damping constant
142158
- `c`: [`N.m/rad`] Spring constant
159+
- `phi_rel0`: [`rad`] Unstretched spring angle
143160
"""
144-
@component function SpringDamper(; name, c, d, phi_rel0 = 0.0)
145-
@named partial_comp = PartialCompliantWithRelativeStates()
146-
@unpack phi_rel, w_rel, tau = partial_comp
147-
@variables tau_c(t) [description = "Spring torque"]
148-
@variables tau_d(t) [description = "Damper torque"]
149-
@parameters d=d [description = "Damping constant"]
150-
@parameters c=c [description = "Spring constant"]
151-
@parameters phi_rel0=phi_rel0 [description = "Unstretched spring angle"]
152-
eqs = [tau_c ~ c * (phi_rel - phi_rel0)
161+
@mtkmodel SpringDamper begin
162+
@extend phi_rel, w_rel, tau = partial_comp = PartialCompliantWithRelativeStates()
163+
@variables begin
164+
tau_c(t), [description = "Spring torque"]
165+
tau_d(t), [description = "Damper torque"]
166+
end
167+
@parameters begin
168+
d, [description = "Damping constant"]
169+
c, [description = "Spring constant"]
170+
phi_rel0 = 0.0, [description = "Unstretched spring angle"]
171+
end
172+
@equations begin
173+
tau_c ~ c * (phi_rel - phi_rel0)
153174
tau_d ~ d * w_rel
154-
tau ~ tau_c + tau_d]
155-
extend(ODESystem(eqs, t; name = name), partial_comp)
175+
tau ~ tau_c + tau_d
176+
end
156177
end
157178

158179
"""
159-
IdealGear(;name, ratio, use_support=false)
180+
IdealGear(; name, ratio, use_support = false)
160181
161182
Ideal gear without inertia.
162183
@@ -178,22 +199,28 @@ This element characterizes any type of gear box which is fixed in the ground and
178199
- `ratio`: Transmission ratio (flange_a.phi/flange_b.phi)
179200
- `use_support`: If support flange enabled, otherwise implicitly grounded
180201
"""
181-
@component function IdealGear(; name, ratio, use_support = false)
182-
@named partial_element = PartialElementaryTwoFlangesAndSupport2(use_support = use_support)
183-
@unpack phi_support, flange_a, flange_b = partial_element
184-
@parameters ratio=ratio [description = "Transmission ratio of $name"]
185-
sts = @variables phi_a(t)=0.0 [
186-
description = "Relative angle between shaft a and the support of $name",
187-
] phi_b(t)=0.0 [description = "Relative angle between shaft b and the support of $name"]
188-
eqs = [phi_a ~ flange_a.phi - phi_support
202+
@mtkmodel IdealGear begin#(; name, ratio, use_support = false)
203+
@parameters begin
204+
use_support
205+
end
206+
@extend phi_support, flange_a, flange_b = partial_element = PartialElementaryTwoFlangesAndSupport2(use_support = use_support)
207+
@parameters begin
208+
ratio, [description = "Transmission ratio"]
209+
end
210+
@variables begin
211+
phi_a(t) = 0.0, [description = "Relative angle between shaft a and the support"]
212+
phi_b(t) = 0.0, [description = "Relative angle between shaft b and the support"]
213+
end
214+
@equations begin
215+
phi_a ~ flange_a.phi - phi_support
189216
phi_b ~ flange_b.phi - phi_support
190217
phi_a ~ ratio * phi_b
191-
0 ~ ratio * flange_a.tau + flange_b.tau]
192-
extend(ODESystem(eqs, t, sts, [ratio]; name = name), partial_element)
218+
0 ~ ratio * flange_a.tau + flange_b.tau
219+
end
193220
end
194221

195222
"""
196-
RotationalFriction(;name, f, tau_c, w_brk, tau_brk)
223+
RotationalFriction(; name, f, tau_c, w_brk, tau_brk)
197224
198225
Models rotational friction with Stribeck effect, Coulomb friction and viscous friction between the two flanges.
199226
The friction torque is a function of the relative angular velocity between `flange_a` and `flange_b`.
@@ -219,22 +246,22 @@ Friction model: "Armstrong, B. and C.C. de Wit, Friction Modeling and Compensati
219246
- `w_brk`: [`rad/s`] Breakaway friction velocity
220247
- `tau_brk`: [`N⋅m`] Breakaway friction torque
221248
"""
222-
@component function RotationalFriction(; name, f, tau_c, w_brk, tau_brk)
223-
@named partial_comp = PartialCompliantWithRelativeStates()
224-
@unpack w_rel, tau = partial_comp
225-
pars = @parameters(f=f, [description = "Viscous friction coefficient of $name"],
226-
tau_c=tau_c, [description = "Coulomb friction torque of $name"],
227-
w_brk=w_brk, [description = "Breakaway friction velocity of $name"],
228-
tau_brk=tau_brk,
229-
[description = "Breakaway friction torque of $name"],)
230-
231-
str_scale = sqrt(2 * exp(1)) * (tau_brk - tau_c)
232-
w_st = w_brk * sqrt(2)
233-
w_coul = w_brk / 10
234-
235-
eqs = [
249+
@mtkmodel RotationalFriction begin
250+
@extend w_rel, tau = partial_comp = PartialCompliantWithRelativeStates()
251+
@parameters begin
252+
f, [description = "Viscous friction coefficient"]
253+
tau_c, [description = "Coulomb friction torque"]
254+
w_brk, [description = "Breakaway friction velocity"]
255+
tau_brk, [description = "Breakaway friction torque"]
256+
end
257+
258+
begin
259+
str_scale = sqrt(2 * exp(1)) * (tau_brk - tau_c)
260+
w_st = w_brk * sqrt(2)
261+
w_coul = w_brk / 10
262+
end
263+
@equations begin
236264
tau ~ str_scale * (exp(-(w_rel / w_st)^2) * w_rel / w_st) +
237-
tau_c * tanh(w_rel / w_coul) + f * w_rel, # Stribeck friction + Coulomb friction + Viscous friction
238-
]
239-
extend(ODESystem(eqs, t, [], pars; name = name), partial_comp)
265+
tau_c * tanh(w_rel / w_coul) + f * w_rel # Stribeck friction + Coulomb friction + Viscous friction
266+
end
240267
end

src/Mechanical/Rotational/sensors.jl

+44-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
AngleSensor(;name)
2+
AngleSensor(; name)
33
44
Ideal sensor to measure the absolute flange angle
55
@@ -8,16 +8,19 @@ Ideal sensor to measure the absolute flange angle
88
- `flange`: [Flange](@ref) Flange of shaft from which sensor information shall be measured
99
- `phi`: [RealOutput](@ref) Absolute angle of flange
1010
"""
11-
@component function AngleSensor(; name)
12-
@named flange = Flange()
13-
@named phi = RealOutput()
14-
eqs = [phi.u ~ flange.phi
15-
flange.tau ~ 0]
16-
return ODESystem(eqs, t, [], []; name = name, systems = [flange, phi])
11+
@mtkmodel AngleSensor begin
12+
@components begin
13+
flange = Flange()
14+
phi = RealOutput()
15+
end
16+
@equations begin
17+
phi.u ~ flange.phi
18+
flange.tau ~ 0
19+
end
1720
end
1821

1922
"""
20-
SpeedSensor(;name)
23+
SpeedSensor(; name)
2124
2225
Ideal sensor to measure the absolute flange angular velocity
2326
@@ -26,12 +29,15 @@ Ideal sensor to measure the absolute flange angular velocity
2629
- `flange`: [Flange](@ref) Flange of shaft from which sensor information shall be measured
2730
- `w`: [RealOutput](@ref) Absolute angular velocity of flange
2831
"""
29-
@component function SpeedSensor(; name)
30-
@named flange = Flange()
31-
@named w = RealOutput()
32-
eqs = [D(flange.phi) ~ w.u
33-
flange.tau ~ 0]
34-
return ODESystem(eqs, t, [], []; name = name, systems = [flange, w])
32+
@mtkmodel SpeedSensor begin
33+
@components begin
34+
flange = Flange()
35+
w = RealOutput()
36+
end
37+
@equations begin
38+
D(flange.phi) ~ w.u
39+
flange.tau ~ 0
40+
end
3541
end
3642

3743
"""
@@ -45,17 +51,20 @@ Ideal sensor to measure the torque between two flanges (`= flange_a.tau`)
4551
- `flange_b`: [Flange](@ref) Left flange of shaft
4652
- `tau`: [RealOutput](@ref) Torque in flange flange_a and flange_b (`tau = flange_a.tau = -flange_b.tau`)
4753
"""
48-
@component function TorqueSensor(; name)
49-
@named flange_a = Flange()
50-
@named flange_b = Flange()
51-
@named tau = RealOutput()
52-
eqs = [flange_a.phi ~ flange_b.phi
53-
tau.u ~ flange_a.tau]
54-
return ODESystem(eqs, t, [], []; name = name, systems = [flange_a, flange_b, tau])
54+
@mtkmodel TorqueSensor begin
55+
@components begin
56+
flange_a = Flange()
57+
flange_b = Flange()
58+
tau = RealOutput()
59+
end
60+
@equations begin
61+
flange_a.phi ~ flange_b.phi
62+
tau.u ~ flange_a.tau
63+
end
5564
end
5665

5766
"""
58-
RelSpeedSensor(;name)
67+
RelSpeedSensor(; name)
5968
6069
Ideal sensor to measure the relative angular velocity
6170
@@ -65,15 +74,19 @@ Ideal sensor to measure the relative angular velocity
6574
- `flange_b`: [Flange](@ref) Flange of shaft from which sensor information shall be measured
6675
- `w`: [RealOutput](@ref) Absolute angular velocity of flange
6776
"""
68-
@component function RelSpeedSensor(; name)
69-
@named flange_a = Flange()
70-
@named flange_b = Flange()
71-
@named w_rel = RealOutput()
72-
@variables phi_rel(t) = 0.0
73-
eqs = [0 ~ flange_a.tau + flange_b.tau
77+
@mtkmodel RelSpeedSensor begin
78+
@components begin
79+
flange_a = Flange()
80+
flange_b = Flange()
81+
w_rel = RealOutput()
82+
end
83+
@variables begin
84+
phi_rel(t) = 0.0
85+
end
86+
@equations begin
87+
0 ~ flange_a.tau + flange_b.tau
7488
phi_rel ~ flange_b.phi - flange_a.phi
7589
D(phi_rel) ~ w_rel.u
76-
0 ~ flange_a.tau]
77-
return ODESystem(eqs, t, [phi_rel], []; name = name,
78-
systems = [flange_a, flange_b, w_rel])
90+
0 ~ flange_a.tau
91+
end
7992
end

0 commit comments

Comments
 (0)