blocksim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blocksim/__init__.py +524 -0
- blocksim/_logging.py +125 -0
- blocksim/aerospace.py +186 -0
- blocksim/analysis.py +1143 -0
- blocksim/astro.py +660 -0
- blocksim/atmosphere.py +150 -0
- blocksim/auxiliar.py +218 -0
- blocksim/avionics.py +992 -0
- blocksim/comm.py +237 -0
- blocksim/continuous.py +586 -0
- blocksim/control.py +159 -0
- blocksim/core.py +4522 -0
- blocksim/discontinuities.py +334 -0
- blocksim/discrete.py +833 -0
- blocksim/distributed.py +559 -0
- blocksim/driveline.py +496 -0
- blocksim/electrical.py +1086 -0
- blocksim/f16.py +409 -0
- blocksim/f16_hifi.py +250 -0
- blocksim/fmu.py +505 -0
- blocksim/fusion.py +984 -0
- blocksim/gemini.py +422 -0
- blocksim/hydraulics.py +816 -0
- blocksim/jsbsim.py +1284 -0
- blocksim/manipulator.py +928 -0
- blocksim/math.py +254 -0
- blocksim/mechanics.py +231 -0
- blocksim/power.py +1063 -0
- blocksim/process.py +595 -0
- blocksim/propulsion.py +1219 -0
- blocksim/py.typed +0 -0
- blocksim/radar.py +111 -0
- blocksim/robotics.py +164 -0
- blocksim/rotation.py +154 -0
- blocksim/simulator_claude.py +432 -0
- blocksim/sources.py +1846 -0
- blocksim/structural.py +613 -0
- blocksim/symbolic.py +934 -0
- blocksim/table.py +355 -0
- blocksim/thermal.py +395 -0
- blocksim/thermo.py +657 -0
- blocksim/vehicle.py +1274 -0
- blocksim-0.1.0.dist-info/METADATA +256 -0
- blocksim-0.1.0.dist-info/RECORD +47 -0
- blocksim-0.1.0.dist-info/WHEEL +4 -0
- blocksim-0.1.0.dist-info/licenses/COPYING +674 -0
- blocksim-0.1.0.dist-info/licenses/COPYING.LESSER +165 -0
blocksim/astro.py
ADDED
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Astrodynamics and spacecraft: orbits, perturbations and attitude.
|
|
3
|
+
|
|
4
|
+
Two separate problems live here, and they are separate on purpose.
|
|
5
|
+
|
|
6
|
+
**Where the spacecraft is.** `TwoBody` integrates position and velocity
|
|
7
|
+
under a point-mass central body, and takes a perturbing acceleration on
|
|
8
|
+
an input port. Every perturbation -- `J2Gravity`, `AtmosphericDrag`, a
|
|
9
|
+
thruster -- is its own block computing an acceleration from the current
|
|
10
|
+
state, summed into that port. That keeps the propagator honest (it does
|
|
11
|
+
exactly one thing) and makes what is and is not being modelled visible
|
|
12
|
+
in the diagram rather than buried in a flag.
|
|
13
|
+
|
|
14
|
+
**Which way it is pointing.** `SpacecraftAttitude` integrates a
|
|
15
|
+
quaternion and a body rate under Euler's equations. Attitude and orbit
|
|
16
|
+
are coupled only through what a payload is pointing at, so they are not
|
|
17
|
+
forced into one 13-state block; wire them together when you need both.
|
|
18
|
+
|
|
19
|
+
Conversions between Cartesian state and classical orbital elements are
|
|
20
|
+
free functions, because that is what they are -- `state_to_elements`,
|
|
21
|
+
`elements_to_state`, and the `kepler_equation` solve that connects mean
|
|
22
|
+
to eccentric anomaly.
|
|
23
|
+
|
|
24
|
+
Everything is SI and in an inertial frame centred on the primary:
|
|
25
|
+
metres, m/s, radians, kilograms. Quaternions are scalar-first
|
|
26
|
+
`[q0, q1, q2, q3]` and rotate body to inertial, matching
|
|
27
|
+
`aerospace.RigidBody6DOF`.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
import numpy as np
|
|
31
|
+
|
|
32
|
+
from .core import Block
|
|
33
|
+
from .rotation import (
|
|
34
|
+
body_to_inertial,
|
|
35
|
+
inertia_and_inverse,
|
|
36
|
+
quaternion_rate,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"AtmosphericDrag",
|
|
41
|
+
"EARTH_J2",
|
|
42
|
+
"EARTH_MU",
|
|
43
|
+
"EARTH_RADIUS",
|
|
44
|
+
"ExponentialAtmosphere",
|
|
45
|
+
"J2Gravity",
|
|
46
|
+
"ReactionWheel",
|
|
47
|
+
"SpacecraftAttitude",
|
|
48
|
+
"TwoBody",
|
|
49
|
+
"circular_speed",
|
|
50
|
+
"elements_to_state",
|
|
51
|
+
"kepler_equation",
|
|
52
|
+
"orbital_period",
|
|
53
|
+
"state_to_elements",
|
|
54
|
+
"vis_viva",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
#: standard gravitational parameter of Earth, m^3/s^2
|
|
58
|
+
#: Earth gravitational parameter in m^3/s^2.
|
|
59
|
+
EARTH_MU = 3.986004418e14
|
|
60
|
+
#: equatorial radius, m
|
|
61
|
+
#: Earth reference equatorial radius in m.
|
|
62
|
+
EARTH_RADIUS = 6378137.0
|
|
63
|
+
#: second zonal harmonic -- the oblateness term, and by a wide margin the
|
|
64
|
+
#: largest perturbation on a low Earth orbit
|
|
65
|
+
#: Earth second zonal harmonic coefficient (dimensionless).
|
|
66
|
+
EARTH_J2 = 1.08262668e-3
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ==============================================================================
|
|
70
|
+
# two-body relations
|
|
71
|
+
# ==============================================================================
|
|
72
|
+
def orbital_period(a, mu=EARTH_MU):
|
|
73
|
+
"""2 pi sqrt(a^3 / mu), seconds. Independent of eccentricity, which
|
|
74
|
+
is Kepler's third law and the reason `a` alone sizes an orbit."""
|
|
75
|
+
if a <= 0.0:
|
|
76
|
+
raise ValueError("period is only defined for a bound orbit (a > 0)")
|
|
77
|
+
return 2.0 * np.pi * np.sqrt(a**3 / mu)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def circular_speed(r, mu=EARTH_MU):
|
|
81
|
+
"""sqrt(mu / r)."""
|
|
82
|
+
return np.sqrt(mu / r)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def vis_viva(r, a, mu=EARTH_MU):
|
|
86
|
+
"""Speed at radius `r` on an orbit of semi-major axis `a`:
|
|
87
|
+
`sqrt(mu (2/r - 1/a))`. The energy integral, in the form you
|
|
88
|
+
actually use it."""
|
|
89
|
+
return np.sqrt(mu * (2.0 / r - 1.0 / a))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def kepler_equation(mean_anomaly, eccentricity, tol=1e-13, max_iter=60):
|
|
93
|
+
"""Eccentric anomaly from mean anomaly: solve `M = E - e sin E`.
|
|
94
|
+
|
|
95
|
+
Newton from a starting guess that already accounts for the
|
|
96
|
+
eccentricity, so it converges in a handful of iterations even at
|
|
97
|
+
e = 0.9 where the naive `E = M` start struggles.
|
|
98
|
+
"""
|
|
99
|
+
if not 0.0 <= eccentricity < 1.0:
|
|
100
|
+
raise ValueError(
|
|
101
|
+
f"eccentricity must be in [0, 1) for an elliptical orbit, "
|
|
102
|
+
f"got {eccentricity}"
|
|
103
|
+
)
|
|
104
|
+
mean = np.mod(mean_anomaly + np.pi, 2.0 * np.pi) - np.pi
|
|
105
|
+
eccentric = mean + eccentricity * np.sin(mean) / (
|
|
106
|
+
1.0 - np.sin(mean + eccentricity) + np.sin(mean)
|
|
107
|
+
)
|
|
108
|
+
if not np.isfinite(eccentric):
|
|
109
|
+
eccentric = mean
|
|
110
|
+
|
|
111
|
+
for _ in range(max_iter):
|
|
112
|
+
residual = eccentric - eccentricity * np.sin(eccentric) - mean
|
|
113
|
+
slope = 1.0 - eccentricity * np.cos(eccentric)
|
|
114
|
+
step = residual / slope
|
|
115
|
+
eccentric -= step
|
|
116
|
+
if abs(step) < tol:
|
|
117
|
+
break
|
|
118
|
+
return eccentric
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def state_to_elements(r, v, mu=EARTH_MU):
|
|
122
|
+
"""Classical elements from a Cartesian state.
|
|
123
|
+
|
|
124
|
+
Returns `(a, e, inclination, raan, argument of perigee, true
|
|
125
|
+
anomaly)` in metres and radians. Circular and equatorial orbits leave
|
|
126
|
+
some of these undefined; the usual conventions are used (RAAN
|
|
127
|
+
measured from x for an equatorial orbit, argument of perigee taken
|
|
128
|
+
from the ascending node for a circular one).
|
|
129
|
+
"""
|
|
130
|
+
r = np.asarray(r, dtype=float)
|
|
131
|
+
v = np.asarray(v, dtype=float)
|
|
132
|
+
radius = np.linalg.norm(r)
|
|
133
|
+
speed = np.linalg.norm(v)
|
|
134
|
+
|
|
135
|
+
momentum = np.cross(r, v)
|
|
136
|
+
h = np.linalg.norm(momentum)
|
|
137
|
+
if h < 1e-12:
|
|
138
|
+
raise ValueError("degenerate (radial) trajectory has no orbital plane")
|
|
139
|
+
|
|
140
|
+
node = np.cross([0.0, 0.0, 1.0], momentum)
|
|
141
|
+
node_norm = np.linalg.norm(node)
|
|
142
|
+
|
|
143
|
+
eccentricity_vector = np.cross(v, momentum) / mu - r / radius
|
|
144
|
+
e = np.linalg.norm(eccentricity_vector)
|
|
145
|
+
|
|
146
|
+
energy = speed**2 / 2.0 - mu / radius
|
|
147
|
+
a = np.inf if abs(energy) < 1e-12 else -mu / (2.0 * energy)
|
|
148
|
+
|
|
149
|
+
inclination = np.arccos(np.clip(momentum[2] / h, -1.0, 1.0))
|
|
150
|
+
|
|
151
|
+
if node_norm > 1e-10:
|
|
152
|
+
raan = np.arctan2(node[1], node[0]) % (2.0 * np.pi)
|
|
153
|
+
if e > 1e-10:
|
|
154
|
+
cos_arg = np.dot(node, eccentricity_vector) / (node_norm * e)
|
|
155
|
+
argp = np.arccos(np.clip(cos_arg, -1.0, 1.0))
|
|
156
|
+
if eccentricity_vector[2] < 0.0:
|
|
157
|
+
argp = 2.0 * np.pi - argp
|
|
158
|
+
else: # circular: measure from the node
|
|
159
|
+
argp = 0.0
|
|
160
|
+
else: # equatorial: measure from the x axis
|
|
161
|
+
raan = 0.0
|
|
162
|
+
argp = (
|
|
163
|
+
np.arctan2(eccentricity_vector[1], eccentricity_vector[0]) % (2.0 * np.pi)
|
|
164
|
+
if e > 1e-10
|
|
165
|
+
else 0.0
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
if e > 1e-10:
|
|
169
|
+
cos_nu = np.dot(eccentricity_vector, r) / (e * radius)
|
|
170
|
+
nu = np.arccos(np.clip(cos_nu, -1.0, 1.0))
|
|
171
|
+
if np.dot(r, v) < 0.0:
|
|
172
|
+
nu = 2.0 * np.pi - nu
|
|
173
|
+
elif node_norm > 1e-10: # circular inclined: argument of latitude
|
|
174
|
+
cos_u = np.dot(node, r) / (node_norm * radius)
|
|
175
|
+
nu = np.arccos(np.clip(cos_u, -1.0, 1.0))
|
|
176
|
+
if r[2] < 0.0:
|
|
177
|
+
nu = 2.0 * np.pi - nu
|
|
178
|
+
else: # circular equatorial: true longitude
|
|
179
|
+
nu = np.arctan2(r[1], r[0]) % (2.0 * np.pi)
|
|
180
|
+
|
|
181
|
+
return a, e, inclination, raan, argp, nu % (2.0 * np.pi)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def elements_to_state(a, e, inclination, raan, argp, nu, mu=EARTH_MU):
|
|
185
|
+
"""Cartesian state from classical elements. The inverse of
|
|
186
|
+
`state_to_elements` on every orbit where the elements are defined."""
|
|
187
|
+
if not 0.0 <= e < 1.0:
|
|
188
|
+
raise ValueError(f"eccentricity must be in [0, 1), got {e}")
|
|
189
|
+
|
|
190
|
+
p = a * (1.0 - e**2)
|
|
191
|
+
radius = p / (1.0 + e * np.cos(nu))
|
|
192
|
+
|
|
193
|
+
# in the perifocal frame, then rotated out by the three angles
|
|
194
|
+
r_pf = radius * np.array([np.cos(nu), np.sin(nu), 0.0])
|
|
195
|
+
v_pf = np.sqrt(mu / p) * np.array([-np.sin(nu), e + np.cos(nu), 0.0])
|
|
196
|
+
|
|
197
|
+
cos_o, sin_o = np.cos(raan), np.sin(raan)
|
|
198
|
+
cos_w, sin_w = np.cos(argp), np.sin(argp)
|
|
199
|
+
cos_i, sin_i = np.cos(inclination), np.sin(inclination)
|
|
200
|
+
|
|
201
|
+
rotation = np.array(
|
|
202
|
+
[
|
|
203
|
+
[
|
|
204
|
+
cos_o * cos_w - sin_o * sin_w * cos_i,
|
|
205
|
+
-cos_o * sin_w - sin_o * cos_w * cos_i,
|
|
206
|
+
sin_o * sin_i,
|
|
207
|
+
],
|
|
208
|
+
[
|
|
209
|
+
sin_o * cos_w + cos_o * sin_w * cos_i,
|
|
210
|
+
-sin_o * sin_w + cos_o * cos_w * cos_i,
|
|
211
|
+
-cos_o * sin_i,
|
|
212
|
+
],
|
|
213
|
+
[sin_w * sin_i, cos_w * sin_i, cos_i],
|
|
214
|
+
]
|
|
215
|
+
)
|
|
216
|
+
return rotation @ r_pf, rotation @ v_pf
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# ==============================================================================
|
|
220
|
+
# propagation
|
|
221
|
+
# ==============================================================================
|
|
222
|
+
class TwoBody(Block):
|
|
223
|
+
"""Point-mass orbital motion, with a port for everything else.
|
|
224
|
+
|
|
225
|
+
States
|
|
226
|
+
[r (3, m), v (3, m/s)]
|
|
227
|
+
|
|
228
|
+
Inputs
|
|
229
|
+
[perturbing acceleration (3, m/s^2)]
|
|
230
|
+
|
|
231
|
+
Outputs
|
|
232
|
+
[r (3), v (3), radius, speed, specific energy, specific
|
|
233
|
+
angular momentum magnitude]
|
|
234
|
+
|
|
235
|
+
::
|
|
236
|
+
|
|
237
|
+
dv/dt = -mu r / ``|r|``^3 + a_perturbing
|
|
238
|
+
|
|
239
|
+
The perturbation port is the whole design: the propagator models the
|
|
240
|
+
central body and nothing else, and `J2Gravity`, `AtmosphericDrag`, a
|
|
241
|
+
thruster or a third body each contribute their own acceleration
|
|
242
|
+
through a `Sum`. What is being modelled is then visible in the
|
|
243
|
+
diagram instead of hidden behind constructor flags.
|
|
244
|
+
|
|
245
|
+
The reported specific energy and angular momentum are the two
|
|
246
|
+
integrals a two-body propagator must conserve, so they are the
|
|
247
|
+
cheapest possible check that a run is behaving.
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
def __init__(self, mu=EARTH_MU, r0=None, v0=None, name=None):
|
|
251
|
+
self.mu = float(mu)
|
|
252
|
+
super().__init__(
|
|
253
|
+
num_inputs=3,
|
|
254
|
+
num_outputs=10,
|
|
255
|
+
num_states=6,
|
|
256
|
+
name=name,
|
|
257
|
+
inputs=[("ax", "m/s^2"), ("ay", "m/s^2"), ("az", "m/s^2")],
|
|
258
|
+
outputs=[
|
|
259
|
+
("rx", "m"),
|
|
260
|
+
("ry", "m"),
|
|
261
|
+
("rz", "m"),
|
|
262
|
+
("vx", "m/s"),
|
|
263
|
+
("vy", "m/s"),
|
|
264
|
+
("vz", "m/s"),
|
|
265
|
+
("radius", "m"),
|
|
266
|
+
("speed", "m/s"),
|
|
267
|
+
("energy", "J/kg"),
|
|
268
|
+
("h", "m^2/s"),
|
|
269
|
+
],
|
|
270
|
+
states=[
|
|
271
|
+
("rx", "m"),
|
|
272
|
+
("ry", "m"),
|
|
273
|
+
("rz", "m"),
|
|
274
|
+
("vx", "m/s"),
|
|
275
|
+
("vy", "m/s"),
|
|
276
|
+
("vz", "m/s"),
|
|
277
|
+
],
|
|
278
|
+
)
|
|
279
|
+
if r0 is None:
|
|
280
|
+
r0 = [EARTH_RADIUS + 500e3, 0.0, 0.0]
|
|
281
|
+
if v0 is None:
|
|
282
|
+
v0 = [0.0, circular_speed(EARTH_RADIUS + 500e3, self.mu), 0.0]
|
|
283
|
+
self.x0 = np.concatenate([np.asarray(r0, float), np.asarray(v0, float)])
|
|
284
|
+
|
|
285
|
+
@classmethod
|
|
286
|
+
def from_elements(
|
|
287
|
+
cls, a, e=0.0, inclination=0.0, raan=0.0, argp=0.0, nu=0.0, mu=EARTH_MU, **kw
|
|
288
|
+
):
|
|
289
|
+
r0, v0 = elements_to_state(a, e, inclination, raan, argp, nu, mu)
|
|
290
|
+
return cls(mu=mu, r0=r0, v0=v0, **kw)
|
|
291
|
+
|
|
292
|
+
def derivative(self, t, x, u):
|
|
293
|
+
r = x[:3]
|
|
294
|
+
radius = np.linalg.norm(r)
|
|
295
|
+
if radius < 1.0:
|
|
296
|
+
raise ValueError("the trajectory has reached the centre of the primary")
|
|
297
|
+
return np.concatenate([x[3:], -self.mu * r / radius**3 + np.asarray(u, float)])
|
|
298
|
+
|
|
299
|
+
def output(self, t, x):
|
|
300
|
+
r, v = x[:3], x[3:]
|
|
301
|
+
radius = np.linalg.norm(r)
|
|
302
|
+
speed = np.linalg.norm(v)
|
|
303
|
+
return np.concatenate(
|
|
304
|
+
[
|
|
305
|
+
x,
|
|
306
|
+
[
|
|
307
|
+
radius,
|
|
308
|
+
speed,
|
|
309
|
+
speed**2 / 2.0 - self.mu / radius,
|
|
310
|
+
np.linalg.norm(np.cross(r, v)),
|
|
311
|
+
],
|
|
312
|
+
]
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
def elements(self, x):
|
|
316
|
+
"""Classical elements of a state vector from this block."""
|
|
317
|
+
return state_to_elements(x[:3], x[3:], self.mu)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class J2Gravity(Block):
|
|
321
|
+
"""The oblateness perturbation -- the largest one on a low orbit by
|
|
322
|
+
two orders of magnitude.
|
|
323
|
+
|
|
324
|
+
Inputs : [r (3, m)]
|
|
325
|
+
Outputs : [acceleration (3, m/s^2)]
|
|
326
|
+
|
|
327
|
+
Its signature effect is secular: the node regresses at
|
|
328
|
+
|
|
329
|
+
dRAAN/dt = -3/2 n J2 (Re/p)^2 cos i
|
|
330
|
+
|
|
331
|
+
and the perigee advances at a related rate, while `a` and `e` are
|
|
332
|
+
untouched on average. A sun-synchronous orbit is chosen precisely to
|
|
333
|
+
make that regression match the year.
|
|
334
|
+
"""
|
|
335
|
+
|
|
336
|
+
def __init__(self, mu=EARTH_MU, radius=EARTH_RADIUS, j2=EARTH_J2, name=None):
|
|
337
|
+
self.mu = float(mu)
|
|
338
|
+
self.radius = float(radius)
|
|
339
|
+
self.j2 = float(j2)
|
|
340
|
+
super().__init__(
|
|
341
|
+
num_inputs=3,
|
|
342
|
+
num_outputs=3,
|
|
343
|
+
name=name,
|
|
344
|
+
inputs=[("rx", "m"), ("ry", "m"), ("rz", "m")],
|
|
345
|
+
outputs=[("ax", "m/s^2"), ("ay", "m/s^2"), ("az", "m/s^2")],
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
def nodal_regression(self, a, e, inclination):
|
|
349
|
+
"""dRAAN/dt in rad/s, the secular rate this perturbation causes."""
|
|
350
|
+
n = np.sqrt(self.mu / a**3)
|
|
351
|
+
p = a * (1.0 - e**2)
|
|
352
|
+
return -1.5 * n * self.j2 * (self.radius / p) ** 2 * np.cos(inclination)
|
|
353
|
+
|
|
354
|
+
def output(self, t, u):
|
|
355
|
+
r = np.asarray(u, dtype=float)
|
|
356
|
+
norm = np.linalg.norm(r)
|
|
357
|
+
if norm < 1.0:
|
|
358
|
+
return np.zeros(3)
|
|
359
|
+
factor = 1.5 * self.j2 * self.mu * self.radius**2 / norm**5
|
|
360
|
+
z_ratio = 5.0 * (r[2] / norm) ** 2
|
|
361
|
+
return -factor * np.array(
|
|
362
|
+
[r[0] * (1.0 - z_ratio), r[1] * (1.0 - z_ratio), r[2] * (3.0 - z_ratio)]
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
class ExponentialAtmosphere(Block):
|
|
367
|
+
"""Density falling exponentially with altitude.
|
|
368
|
+
|
|
369
|
+
Inputs : [r (3, m)]
|
|
370
|
+
Outputs : [density (kg/m^3), altitude (m)]
|
|
371
|
+
|
|
372
|
+
rho = rho0 exp(-(h - h0) / H)
|
|
373
|
+
|
|
374
|
+
A single scale height is crude above about 200 km, where the real
|
|
375
|
+
profile bends as the composition changes -- but it is the right level
|
|
376
|
+
of model when the drag coefficient is a guess anyway, and it makes
|
|
377
|
+
the decay rate's sensitivity to `H` explicit instead of hidden in a
|
|
378
|
+
table.
|
|
379
|
+
"""
|
|
380
|
+
|
|
381
|
+
def __init__(
|
|
382
|
+
self,
|
|
383
|
+
rho0=1.585e-12,
|
|
384
|
+
h0=500e3,
|
|
385
|
+
scale_height=63.8e3,
|
|
386
|
+
radius=EARTH_RADIUS,
|
|
387
|
+
name=None,
|
|
388
|
+
):
|
|
389
|
+
if scale_height <= 0.0:
|
|
390
|
+
raise ValueError("scale height must be positive")
|
|
391
|
+
self.rho0 = float(rho0)
|
|
392
|
+
self.h0 = float(h0)
|
|
393
|
+
self.scale_height = float(scale_height)
|
|
394
|
+
self.radius = float(radius)
|
|
395
|
+
super().__init__(
|
|
396
|
+
num_inputs=3,
|
|
397
|
+
num_outputs=2,
|
|
398
|
+
name=name,
|
|
399
|
+
inputs=[("rx", "m"), ("ry", "m"), ("rz", "m")],
|
|
400
|
+
outputs=[("rho", "kg/m^3"), ("altitude", "m")],
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
def density(self, altitude):
|
|
404
|
+
return self.rho0 * np.exp(-(altitude - self.h0) / self.scale_height)
|
|
405
|
+
|
|
406
|
+
def output(self, t, u):
|
|
407
|
+
altitude = float(np.linalg.norm(np.asarray(u, float))) - self.radius
|
|
408
|
+
return np.array([self.density(altitude), altitude])
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
class AtmosphericDrag(Block):
|
|
412
|
+
"""Drag from the co-rotating atmosphere.
|
|
413
|
+
|
|
414
|
+
Inputs
|
|
415
|
+
[r (3), v (3), density (kg/m^3)]
|
|
416
|
+
|
|
417
|
+
Outputs
|
|
418
|
+
[acceleration (3, m/s^2)]
|
|
419
|
+
|
|
420
|
+
::
|
|
421
|
+
|
|
422
|
+
a = -1/2 rho ``|v_rel|`` v_rel / B, B = m / (Cd A)
|
|
423
|
+
|
|
424
|
+
`B` is the ballistic coefficient, which is the only thing that
|
|
425
|
+
matters -- mass, area and drag coefficient never appear separately.
|
|
426
|
+
The relative velocity subtracts the atmosphere's rotation with the
|
|
427
|
+
Earth, which is a few hundred m/s at low inclination and worth having.
|
|
428
|
+
"""
|
|
429
|
+
|
|
430
|
+
def __init__(self, ballistic_coefficient=100.0, omega_earth=7.292115e-5, name=None):
|
|
431
|
+
if ballistic_coefficient <= 0.0:
|
|
432
|
+
raise ValueError("ballistic coefficient must be positive")
|
|
433
|
+
self.ballistic_coefficient = float(ballistic_coefficient)
|
|
434
|
+
self.omega_earth = float(omega_earth)
|
|
435
|
+
super().__init__(
|
|
436
|
+
num_inputs=7,
|
|
437
|
+
num_outputs=3,
|
|
438
|
+
name=name,
|
|
439
|
+
inputs=[
|
|
440
|
+
("rx", "m"),
|
|
441
|
+
("ry", "m"),
|
|
442
|
+
("rz", "m"),
|
|
443
|
+
("vx", "m/s"),
|
|
444
|
+
("vy", "m/s"),
|
|
445
|
+
("vz", "m/s"),
|
|
446
|
+
("rho", "kg/m^3"),
|
|
447
|
+
],
|
|
448
|
+
outputs=[("ax", "m/s^2"), ("ay", "m/s^2"), ("az", "m/s^2")],
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
def output(self, t, u):
|
|
452
|
+
r, v, rho = np.asarray(u[:3], float), np.asarray(u[3:6], float), float(u[6])
|
|
453
|
+
v_atmosphere = np.cross([0.0, 0.0, self.omega_earth], r)
|
|
454
|
+
relative = v - v_atmosphere
|
|
455
|
+
speed = np.linalg.norm(relative)
|
|
456
|
+
if speed < 1e-9 or rho <= 0.0:
|
|
457
|
+
return np.zeros(3)
|
|
458
|
+
return -0.5 * rho * speed * relative / self.ballistic_coefficient
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
# ==============================================================================
|
|
462
|
+
# attitude
|
|
463
|
+
# ==============================================================================
|
|
464
|
+
class SpacecraftAttitude(Block):
|
|
465
|
+
"""Rigid-body attitude: Euler's equations plus a quaternion.
|
|
466
|
+
|
|
467
|
+
States
|
|
468
|
+
[q (4, scalar first), omega (3, rad/s body)]
|
|
469
|
+
|
|
470
|
+
Inputs
|
|
471
|
+
[external torque (3, N m body), reaction torque from
|
|
472
|
+
internal devices (3, N m on the body)]
|
|
473
|
+
|
|
474
|
+
Outputs
|
|
475
|
+
[q (4), omega (3), body angular momentum (3), kinetic
|
|
476
|
+
energy (J), quaternion norm]
|
|
477
|
+
|
|
478
|
+
::
|
|
479
|
+
|
|
480
|
+
I domega/dt = tau_ext + tau_reaction - omega x (I omega + h_w)
|
|
481
|
+
|
|
482
|
+
Both torque triples are *on the body* and both add, which is what
|
|
483
|
+
makes `ReactionWheel` wire straight in: a wheel reports the torque it
|
|
484
|
+
applies to its spacecraft, already negated, so there is no sign to
|
|
485
|
+
remember at the connection. `wheel_momentum` is a constant bias in
|
|
486
|
+
the gyroscopic term, for a spacecraft carrying stored momentum it is
|
|
487
|
+
not otherwise tracking.
|
|
488
|
+
|
|
489
|
+
The `omega x I omega` term is the gyroscopic coupling, and it is what
|
|
490
|
+
makes a torque-free asymmetric body tumble rather than spin steadily
|
|
491
|
+
about an arbitrary axis. The quaternion is renormalized in the
|
|
492
|
+
*output*, never in the state -- rescaling a state behind an
|
|
493
|
+
integrator's back invalidates its error estimate. The reported norm
|
|
494
|
+
is the drift, and it is the check that a run is behaving.
|
|
495
|
+
"""
|
|
496
|
+
|
|
497
|
+
def __init__(
|
|
498
|
+
self, inertia=None, wheel_momentum=None, q0=None, omega0=None, name=None
|
|
499
|
+
):
|
|
500
|
+
self.inertia, self.inertia_inv = inertia_and_inverse(
|
|
501
|
+
np.eye(3) if inertia is None else inertia
|
|
502
|
+
)
|
|
503
|
+
self.wheel_momentum = (
|
|
504
|
+
np.zeros(3)
|
|
505
|
+
if wheel_momentum is None
|
|
506
|
+
else np.asarray(wheel_momentum, dtype=float)
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
super().__init__(
|
|
510
|
+
num_inputs=6,
|
|
511
|
+
num_outputs=12,
|
|
512
|
+
num_states=7,
|
|
513
|
+
name=name,
|
|
514
|
+
inputs=[
|
|
515
|
+
("tx", "N*m"),
|
|
516
|
+
("ty", "N*m"),
|
|
517
|
+
("tz", "N*m"),
|
|
518
|
+
("rx", "N*m"),
|
|
519
|
+
("ry", "N*m"),
|
|
520
|
+
("rz", "N*m"),
|
|
521
|
+
],
|
|
522
|
+
outputs=[
|
|
523
|
+
("q0", None),
|
|
524
|
+
("q1", None),
|
|
525
|
+
("q2", None),
|
|
526
|
+
("q3", None),
|
|
527
|
+
("p", "rad/s"),
|
|
528
|
+
("q", "rad/s"),
|
|
529
|
+
("r", "rad/s"),
|
|
530
|
+
("Lx", "N*m*s"),
|
|
531
|
+
("Ly", "N*m*s"),
|
|
532
|
+
("Lz", "N*m*s"),
|
|
533
|
+
("energy", "J"),
|
|
534
|
+
("qnorm", None),
|
|
535
|
+
],
|
|
536
|
+
states=[
|
|
537
|
+
("q0", None),
|
|
538
|
+
("q1", None),
|
|
539
|
+
("q2", None),
|
|
540
|
+
("q3", None),
|
|
541
|
+
("p", "rad/s"),
|
|
542
|
+
("q", "rad/s"),
|
|
543
|
+
("r", "rad/s"),
|
|
544
|
+
],
|
|
545
|
+
)
|
|
546
|
+
quaternion = (
|
|
547
|
+
np.array([1.0, 0.0, 0.0, 0.0])
|
|
548
|
+
if q0 is None
|
|
549
|
+
else np.asarray(q0, dtype=float)
|
|
550
|
+
)
|
|
551
|
+
rate = np.zeros(3) if omega0 is None else np.asarray(omega0, dtype=float)
|
|
552
|
+
self.x0 = np.concatenate([quaternion / np.linalg.norm(quaternion), rate])
|
|
553
|
+
|
|
554
|
+
def derivative(self, t, x, u):
|
|
555
|
+
q, omega = x[:4], x[4:]
|
|
556
|
+
torque = np.asarray(u[:3], dtype=float)
|
|
557
|
+
reaction = np.asarray(u[3:], dtype=float)
|
|
558
|
+
momentum = self.inertia @ omega + self.wheel_momentum
|
|
559
|
+
rate = self.inertia_inv @ (torque + reaction - np.cross(omega, momentum))
|
|
560
|
+
return np.concatenate([quaternion_rate(q, omega), rate])
|
|
561
|
+
|
|
562
|
+
def output(self, t, x):
|
|
563
|
+
q, omega = x[:4], x[4:]
|
|
564
|
+
norm = np.linalg.norm(q)
|
|
565
|
+
momentum = self.inertia @ omega + self.wheel_momentum
|
|
566
|
+
return np.concatenate(
|
|
567
|
+
[
|
|
568
|
+
q / (norm if norm > 0.0 else 1.0),
|
|
569
|
+
omega,
|
|
570
|
+
momentum,
|
|
571
|
+
[0.5 * omega @ self.inertia @ omega, norm],
|
|
572
|
+
]
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
def rotation_matrix(self, q):
|
|
576
|
+
"""Body to inertial, from a (possibly unnormalized) quaternion.
|
|
577
|
+
|
|
578
|
+
Kept as a method because a spacecraft is the natural place to
|
|
579
|
+
ask; the maths lives in `blocksim.rotation`.
|
|
580
|
+
"""
|
|
581
|
+
return body_to_inertial(q)
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
class ReactionWheel(Block):
|
|
585
|
+
"""One momentum wheel on one axis.
|
|
586
|
+
|
|
587
|
+
States
|
|
588
|
+
[wheel speed (rad/s)]
|
|
589
|
+
|
|
590
|
+
Inputs
|
|
591
|
+
[commanded torque (N m)]
|
|
592
|
+
|
|
593
|
+
Outputs
|
|
594
|
+
[reaction torque on the body (N m), wheel speed, stored
|
|
595
|
+
momentum (N m s), saturation fraction]
|
|
596
|
+
|
|
597
|
+
The reaction torque is the negative of what the wheel accelerates
|
|
598
|
+
with, which is the whole point: a wheel produces control torque by
|
|
599
|
+
storing momentum, and it can only keep doing that until it runs out
|
|
600
|
+
of speed. Past `max_speed` the commanded torque is refused in the
|
|
601
|
+
direction that would spin it faster, so a saturated wheel goes quiet
|
|
602
|
+
instead of quietly lying -- which is when the momentum has to be
|
|
603
|
+
dumped by something else.
|
|
604
|
+
"""
|
|
605
|
+
|
|
606
|
+
def __init__(
|
|
607
|
+
self,
|
|
608
|
+
inertia=1e-4,
|
|
609
|
+
max_speed=600.0,
|
|
610
|
+
max_torque=0.02,
|
|
611
|
+
friction=1e-7,
|
|
612
|
+
omega0=0.0,
|
|
613
|
+
name=None,
|
|
614
|
+
):
|
|
615
|
+
if inertia <= 0.0 or max_speed <= 0.0 or max_torque <= 0.0:
|
|
616
|
+
raise ValueError("inertia, max speed and max torque must be positive")
|
|
617
|
+
self.inertia = float(inertia)
|
|
618
|
+
self.max_speed = float(max_speed)
|
|
619
|
+
self.max_torque = float(max_torque)
|
|
620
|
+
self.friction = float(friction)
|
|
621
|
+
super().__init__(
|
|
622
|
+
num_inputs=1,
|
|
623
|
+
num_outputs=4,
|
|
624
|
+
num_states=1,
|
|
625
|
+
name=name,
|
|
626
|
+
inputs=[("tau_cmd", "N*m")],
|
|
627
|
+
outputs=[
|
|
628
|
+
("tau_body", "N*m"),
|
|
629
|
+
("omega", "rad/s"),
|
|
630
|
+
("momentum", "N*m*s"),
|
|
631
|
+
("saturation", None),
|
|
632
|
+
],
|
|
633
|
+
states=[("omega", "rad/s")],
|
|
634
|
+
)
|
|
635
|
+
self.x0 = np.array([float(omega0)])
|
|
636
|
+
|
|
637
|
+
def applied_torque(self, speed, command):
|
|
638
|
+
"""What the wheel can actually do, given where it already is."""
|
|
639
|
+
torque = float(np.clip(command, -self.max_torque, self.max_torque))
|
|
640
|
+
if speed >= self.max_speed and torque > 0.0:
|
|
641
|
+
return 0.0
|
|
642
|
+
if speed <= -self.max_speed and torque < 0.0:
|
|
643
|
+
return 0.0
|
|
644
|
+
return torque
|
|
645
|
+
|
|
646
|
+
def derivative(self, t, x, u):
|
|
647
|
+
torque = self.applied_torque(float(x[0]), float(u[0]))
|
|
648
|
+
return np.array([(torque - self.friction * x[0]) / self.inertia])
|
|
649
|
+
|
|
650
|
+
def output(self, t, x, u):
|
|
651
|
+
speed = float(x[0])
|
|
652
|
+
torque = self.applied_torque(speed, float(u[0]))
|
|
653
|
+
return np.array(
|
|
654
|
+
[
|
|
655
|
+
-torque, # equal and opposite, on the body
|
|
656
|
+
speed,
|
|
657
|
+
self.inertia * speed,
|
|
658
|
+
abs(speed) / self.max_speed,
|
|
659
|
+
]
|
|
660
|
+
)
|