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/control.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Estimation and limiting, for the parts a diagram cannot express directly.
|
|
3
|
+
|
|
4
|
+
Two blocks that did not fit anywhere else. `KalmanFilter` is a
|
|
5
|
+
discrete-time linear estimator; `SaturationDynamic` is a clamp whose
|
|
6
|
+
limits are themselves signals.
|
|
7
|
+
|
|
8
|
+
Most control lives elsewhere
|
|
9
|
+
----------------------------
|
|
10
|
+
This is not where controllers are. `blocksim.continuous.PIDController`
|
|
11
|
+
and `discrete.DiscretePIDController` are the regulators;
|
|
12
|
+
`blocksim.analysis` has `trim`, `linearize`, `lqr`, `lqe` and the margins,
|
|
13
|
+
which is where a control design starts; and the interconnection algebra
|
|
14
|
+
there builds a closed loop out of pieces. `examples/control` shows the
|
|
15
|
+
whole route, including handing the linearized model to an external
|
|
16
|
+
optimiser and testing the result on the nonlinear plant.
|
|
17
|
+
|
|
18
|
+
`SaturationDynamic` exists because `discontinuities.Saturation` fixes its
|
|
19
|
+
limits at construction. When the limit is itself computed -- a rate
|
|
20
|
+
limit that depends on speed, an authority that shrinks with dynamic
|
|
21
|
+
pressure -- it has to arrive as a signal, which is what this block takes.
|
|
22
|
+
The same warning applies to both: clamping an output does nothing about
|
|
23
|
+
an upstream integrator winding up behind it.
|
|
24
|
+
|
|
25
|
+
`KalmanFilter` is the standard predict-correct recursion on a fixed
|
|
26
|
+
linear model, updated on a clock tick. It is the textbook filter and not
|
|
27
|
+
a robust one: the model is assumed exact, the noise covariances constant,
|
|
28
|
+
and there is no joseph-form update or square-root factorisation, so a
|
|
29
|
+
long run with an ill-conditioned covariance can lose symmetry. For a
|
|
30
|
+
nonlinear plant, linearize about the current estimate yourself, or reach
|
|
31
|
+
for a library built for it.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
import numpy as np
|
|
35
|
+
from .core import Block
|
|
36
|
+
|
|
37
|
+
__all__ = ["KalmanFilter", "SaturationDynamic"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class SaturationDynamic(Block):
|
|
41
|
+
"""Limits the input signal between dynamically changing minimum and maximum limits.
|
|
42
|
+
Inputs:
|
|
43
|
+
|
|
44
|
+
[0]: u (input signal)
|
|
45
|
+
[1]: limit_min
|
|
46
|
+
[2]: limit_max
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, name=None):
|
|
50
|
+
super().__init__(
|
|
51
|
+
num_inputs=3,
|
|
52
|
+
num_outputs=1,
|
|
53
|
+
name=name,
|
|
54
|
+
inputs=[("u", None), ("limit_min", None), ("limit_max", None)],
|
|
55
|
+
outputs=[("y", None)],
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
def output(self, u):
|
|
59
|
+
return np.clip(u[0], u[1], u[2])
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class KalmanFilter(Block):
|
|
63
|
+
"""
|
|
64
|
+
The discrete-time Kalman filter: predict, then correct.
|
|
65
|
+
|
|
66
|
+
x[k+1] = A x[k] + B u[k] + w[k] w ~ N(0, Q)
|
|
67
|
+
y[k] = C x[k] + v[k] v ~ N(0, R)
|
|
68
|
+
|
|
69
|
+
Inputs : u0..u{m-1} then y_meas0..y_meas{p-1}
|
|
70
|
+
Outputs : the state estimate, held between ticks
|
|
71
|
+
Discrete: the estimate; the covariance `P` lives on `self`
|
|
72
|
+
|
|
73
|
+
`Q` and `R` are the design, and they are a RATIO more than two
|
|
74
|
+
numbers: `Q/R` says how much you trust the model against the
|
|
75
|
+
measurement. Too small a `Q` and the filter believes its model, and
|
|
76
|
+
lags or diverges when the model is wrong; too large and it chases
|
|
77
|
+
the noise and you have an expensive low-pass filter.
|
|
78
|
+
|
|
79
|
+
This is the textbook filter, not a robust one. The model is assumed
|
|
80
|
+
exact, the covariances constant, and there is no Joseph-form update
|
|
81
|
+
or square-root factorisation, so a long run with an
|
|
82
|
+
ill-conditioned `P` can lose symmetry. For a nonlinear plant,
|
|
83
|
+
linearize about the current estimate yourself.
|
|
84
|
+
|
|
85
|
+
This block owns its estimate on `self` and `output()` takes no `u`,
|
|
86
|
+
so it has no direct feedthrough and can close a loop. It used to be
|
|
87
|
+
written against an API the engine does not offer -- an `xd`
|
|
88
|
+
argument that arrives as `None`, an `update()` whose return value is
|
|
89
|
+
discarded, and no `clock.bind()` -- and so produced NaN for a whole
|
|
90
|
+
run. See `Block`'s docstring for the contract.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def __init__(self, A, B, C, Q, R, clock, x0=None, P0=None, name=None):
|
|
94
|
+
self.A = np.asarray(A, dtype=float)
|
|
95
|
+
self.B = np.asarray(B, dtype=float)
|
|
96
|
+
self.C = np.asarray(C, dtype=float)
|
|
97
|
+
self.Q = np.asarray(Q, dtype=float)
|
|
98
|
+
self.R = np.asarray(R, dtype=float)
|
|
99
|
+
self.clock = clock
|
|
100
|
+
|
|
101
|
+
self.n_states = self.A.shape[0]
|
|
102
|
+
self.n_inputs = self.B.shape[1]
|
|
103
|
+
self.n_measurements = self.C.shape[0]
|
|
104
|
+
|
|
105
|
+
self.initial_x = (
|
|
106
|
+
np.zeros(self.n_states, dtype=float)
|
|
107
|
+
if x0 is None
|
|
108
|
+
else np.asarray(x0, dtype=float).copy()
|
|
109
|
+
)
|
|
110
|
+
self.initial_P = (
|
|
111
|
+
np.eye(self.n_states, dtype=float)
|
|
112
|
+
if P0 is None
|
|
113
|
+
else np.asarray(P0, dtype=float).copy()
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
super().__init__(
|
|
117
|
+
num_inputs=self.n_inputs + self.n_measurements,
|
|
118
|
+
num_outputs=self.n_states,
|
|
119
|
+
num_discrete=self.n_states,
|
|
120
|
+
clock=clock,
|
|
121
|
+
name=name,
|
|
122
|
+
inputs=[(f"u{i}", None) for i in range(self.n_inputs)]
|
|
123
|
+
+ [(f"y_meas{i}", None) for i in range(self.n_measurements)],
|
|
124
|
+
outputs=[(f"x_est{i}", None) for i in range(self.n_states)],
|
|
125
|
+
discrete=[(f"xd_est{i}", None) for i in range(self.n_states)],
|
|
126
|
+
)
|
|
127
|
+
# declaring a clock records it; binding is what makes the tick
|
|
128
|
+
# actually reach update()
|
|
129
|
+
self.clock.bind(self)
|
|
130
|
+
self.xd0 = self.initial_x.copy()
|
|
131
|
+
self.xd = self.xd0.copy()
|
|
132
|
+
self.P = self.initial_P.copy()
|
|
133
|
+
|
|
134
|
+
def initialize(self):
|
|
135
|
+
super().initialize()
|
|
136
|
+
self.xd = self.xd0.copy()
|
|
137
|
+
self.P = self.initial_P.copy()
|
|
138
|
+
|
|
139
|
+
def update(self, t, u):
|
|
140
|
+
control = u[: self.n_inputs]
|
|
141
|
+
measurement = u[self.n_inputs :]
|
|
142
|
+
|
|
143
|
+
# 1. predict: where the model says we should be, and how much
|
|
144
|
+
# less we know for having propagated
|
|
145
|
+
predicted = self.A @ self.xd + self.B @ control
|
|
146
|
+
covariance = self.A @ self.P @ self.A.T + self.Q
|
|
147
|
+
|
|
148
|
+
# 2. correct: weigh the innovation by how much the measurement
|
|
149
|
+
# is worth against what we already believed
|
|
150
|
+
innovation_cov = self.C @ covariance @ self.C.T + self.R
|
|
151
|
+
gain = covariance @ self.C.T @ np.linalg.inv(innovation_cov)
|
|
152
|
+
|
|
153
|
+
# the engine DISCARDS what update() returns, so the estimate has
|
|
154
|
+
# to be assigned on self
|
|
155
|
+
self.xd = predicted + gain @ (measurement - self.C @ predicted)
|
|
156
|
+
self.P = (np.eye(self.n_states) - gain @ self.C) @ covariance
|
|
157
|
+
|
|
158
|
+
def output(self, t):
|
|
159
|
+
return np.copy(self.xd)
|