-
Notifications
You must be signed in to change notification settings - Fork 19
/
speed-measurement.jl
166 lines (138 loc) · 5.47 KB
/
speed-measurement.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using FunctionalModels, FunctionalModels.Lib
const D = FunctionalModels.D
const t = FunctionalModels.t
export SecondOrderSystem, SecondOrderSystemUsingFunctionalModelsLib, SampleAndHold, IntervalMeasure, PulseCounting
"""
# Examples of speed measurement
These examples show several ways of measuring speed on a rotational
system. They are based on Michael's section on [Speed
Measurement](http://mbe.modelica.university/behavior/discrete/measuring/). These
examples include use of Variable variables and Events.
The system is based on the following plant:
![diagram](http://mbe.modelica.university/static/_images/PlantWithPulseCounter.svg)
"""
@comment
"""
Rotational example
http://mbe.modelica.university/behavior/equations/mechanical/
"""
function SecondOrderSystem(; phi1 = Unknown(name = :phi1),
phi2 = Unknown(1.0, name = :phi2),
omega1 = Unknown(name = :omega1),
omega2 = Unknown(name = :omega2),
J1 = 0.4, J2 = 1.0, c1 = 11.0, c2 = 5.0, d1 = 0.2, d2 = 1.0)
[
## Equations for inertia 1
D(phi1) ~ omega1
D(omega1) ~ (c1*(phi2 - phi1) + d1*(omega2 - omega1)) / J1
## Equations for inertia 2
D(phi2) ~ omega2
D(omega2) ~ (c1*(phi1 - phi2) + d1*(omega1 - omega2) - c2*phi2 - d2*omega2) / J2
]
end
# function SecondOrderSystem(; phi1 = Unknown(name = :phi1),
# phi2 = Unknown(1.0, name = :phi2),
# omega1 = Unknown(name = :omega1),
# omega2 = Unknown(name = :omega2),
# J1 = 0.4, J2 = 1.0, k1 = 11.0, k2 = 5.0, d1 = 0.2, d2 = 1.0)
# # @named phidiff = Unknown(default_value(phi2) - default_value(phi1)) # Used because der(phi2 - phi1) isn't supported
# @named phidiff = Unknown(default_value(phi2) - default_value(phi1)) # Used because der(phi2 - phi1) isn't supported
# [
# ## Equations for inertia 1
# phidiff ~ phi2 - phi1
# der(phi1) ~ omega1
# J1*der(omega1) - d1*der(phidiff) ~ k1*(phi2-phi1)
# ## Equations for inertia 2
# der(phi2) ~ omega2
# J2*der(omega2) + d1*der(phidiff) ~ -d2*omega2 + k1*(phi1-phi2) - k2*phi2
# ]
# end
"""
Rotational example based on components in FunctionalModels.Lib
http://mbe.modelica.university/behavior/equations/mechanical/
![diagram](http://mbe.modelica.university/static/_images/PlantWithPulseCounter.svg)
"""
function SecondOrderSystemUsingFunctionalModelsLib(; phi1 = Angle(0.0, name = :phi1),
phi2 = Angle(1.0, name = :phi2),
omega1 = Unknown(0.0, name = :omega1),
omega2 = Unknown(0.0, name = :omega2),
J1 = 0.4, J2 = 1.0, c1 = 11.0, c2 = 5.0, d1 = 0.2, d2 = 1.0)
[
der(phi1) ~ omega1
der(phi2) ~ omega2
:in1 => Inertia(phi1, J = J1)
:in2 => Inertia(phi2, J = J2)
:sd1 => SpringDamper(phi1, phi2, c = c1, d = d1)
:sd2 => SpringDamper(phi2, 0.0, c = c2, d = d2)
]
end
"""
Rotational example with sample-and-hold measurement
http://mbe.modelica.university/behavior/discrete/measuring/#sample-and-hold
"""
function SampleAndHold()
sample_time = 0.125
@named omega1 = Unknown()
@named omega1_measured = Unknown()
[
SecondOrderSystem(omega1 = omega1)
Event(sin(FunctionalModels.t / sample_time * 2pi) ~ 0.0,
omega1_measured ~ omega1)
der(omega1_measured) ~ 0.0
]
end
"""
Rotational example with interval measurements
http://mbe.modelica.university/behavior/discrete/measuring/#interval-measurement
"""
function IntervalMeasure()
teeth = 200
tooth_angle = 2pi / teeth
@named phi1 = Unknown()
@named omega1_measured_u = Unknown()
@named omega1_measured = Variable(0.0)
@named next_phi = Variable(default_value(phi1) + tooth_angle)
@named prev_phi = Variable(default_value(phi1) - tooth_angle)
@named last_time = Variable(0.0)
[
Event(IfElse.ifelse(phi1 > next_phi, phi1 - next_phi, prev_phi - phi1),
[
omega1_measured ~ tooth_angle / (t - last_time)
next_phi ~ phi1 + tooth_angle
prev_phi ~ phi1 - tooth_angle
last_time ~ FunctionalModels.t
])
:sos => SecondOrderSystem(phi1 = phi1)
omega1_measured_u ~ omega1_measured
]
end
"""
Rotational example with pulse counting
http://mbe.modelica.university/behavior/discrete/measuring/#pulse-counting
"""
function PulseCounting()
sample_time = 0.125
teeth = 200
tooth_angle = 2pi / teeth
@named phi1 = Unknown()
@named omega1_measured_u = Unknown()
@named omega1_measured = Variable(0.0)
@named next_phi = Variable(default_value(phi1) + tooth_angle)
@named prev_phi = Variable(default_value(phi1) - tooth_angle)
@named count = Variable(0)
[
Event(IfElse.ifelse(phi1 > next_phi, phi1 - next_phi, prev_phi - phi1) ~ 0.0,
[
next_phi ~ phi1 + tooth_angle
prev_phi ~ phi1 - tooth_angle
count ~ count + 1
])
Event(sin(t / sample_time * 2pi) ~ 0.0,
[
omega1_measured ~ count * tooth_angle / sample_time
count ~ 0
])
:sos => SecondOrderSystem(phi1 = phi1)
omega1_measured_u ~ omega1_measured
]
end