File size: 13,875 Bytes
77ba96d d4a6e31 77ba96d | 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | """Álgebra de diagramas de bloques: las asociaciones de los apuntes
(serie, paralelo, realimentación y lazos anidados), resueltas por el
método infalible de las ecuaciones de señales, y su dibujo."""
import re
import sympy as sp
from matplotlib.figure import Figure
from matplotlib.patches import Circle, Rectangle
from graficas import NEGRO
from nucleo import FT, s
G1, G2, H1, H2, X = sp.symbols("G_1 G_2 H_1 H_2 X")
E, E1, E2, B, B1, V, Y, Y1, Y2, U = sp.symbols("E E_1 E_2 B B_1 V Y Y_1 Y_2 U")
TOPOLOGIAS = ["Lazo cerrado (realimentación)", "Lazos anidados", "Serie", "Paralelo"]
BLOQUES_USADOS = {
"Serie": ["G1", "G2"],
"Paralelo": ["G1", "G2"],
"Lazo cerrado (realimentación)": ["G1", "G2", "H1"],
"Lazos anidados": ["G1", "G2", "H1", "H2"],
}
SIMB = {"G1": G1, "G2": G2, "H1": H1, "H2": H2}
def ecuaciones(topo, negativa=True, resta_paralelo=False):
"""Ecuaciones de señales: una por cada bloque y cada punto de suma."""
sg = -1 if negativa else 1
if topo == "Serie":
return [sp.Eq(V, G1 * X), sp.Eq(Y, G2 * V)], [V, Y]
if topo == "Paralelo":
return [sp.Eq(Y1, G1 * X), sp.Eq(Y2, G2 * X),
sp.Eq(Y, Y1 + (-1 if resta_paralelo else 1) * Y2)], [Y1, Y2, Y]
if topo == "Lazo cerrado (realimentación)":
return [sp.Eq(E, X + sg * B), sp.Eq(U, G1 * E), sp.Eq(Y, G2 * U), sp.Eq(B, H1 * Y)], [E, U, Y, B]
return [sp.Eq(E1, X + sg * B1), sp.Eq(B1, H2 * Y), sp.Eq(E2, G1 * E1 + sg * H1 * Y),
sp.Eq(Y, G2 * E2)], [E1, B1, E2, Y]
def ecuaciones_tex(topo, negativa=True, resta_paralelo=False):
"""Las mismas ecuaciones, escritas como en la pizarra, y el resultado."""
m = "-" if negativa else "+"
p = "+" if negativa else "-"
if topo == "Serie":
return [r"V &= G_1X", r"Y &= G_2V"], r"G_1G_2"
if topo == "Paralelo":
sg = "-" if resta_paralelo else "+"
return [r"Y_1 &= G_1X", r"Y_2 &= G_2X", r"Y &= Y_1 " + sg + " Y_2"], r"G_1 " + sg + " G_2"
if topo == "Lazo cerrado (realimentación)":
return ([r"E &= X " + m + " B", r"U &= G_1E", r"Y &= G_2U", r"B &= H_1Y"],
r"\frac{G_1G_2}{1 " + p + r" G_1G_2H_1}")
return ([r"E_1 &= X " + m + " B_1", r"B_1 &= H_2Y", r"E_2 &= G_1E_1 " + m + " H_1Y", r"Y &= G_2E_2"],
r"\frac{G_1G_2}{1 " + p + " G_2H_1 " + p + r" G_1G_2H_2}")
def formula(topo, negativa=True, resta_paralelo=False):
ecs, inc = ecuaciones(topo, negativa, resta_paralelo)
sol = sp.solve(ecs, inc, dict=True)[0]
F = sp.factor(sp.simplify(sol[Y] / X))
return F, ecs
def ganancia_lazo(topo, negativa=True):
"""Ganancia de lazo abierto (la que aparece en 1 + L = 0)."""
if topo == "Lazo cerrado (realimentación)":
return G1 * G2 * H1
if topo == "Lazos anidados":
return G1 * G2 * H2 / (1 + (1 if negativa else -1) * G2 * H1)
return None
def evalua(F, subs):
"""Sustituye cada bloque por N/D SIN cancelar factores.
Se multiplica numerador y denominador de la fórmula por el producto de
los denominadores de los bloques, de modo que el denominador resultante
contiene todos los modos de la interconexión. Si después numerador y
denominador comparten un factor, eso es una cancelación polo-cero que
hay que vigilar (aviso de los apuntes).
"""
A, Bd = sp.fraction(sp.together(F))
syms = [x for x in subs if A.has(x) or Bd.has(x)]
deg = {x: max(sp.degree(A, x), sp.degree(Bd, x)) for x in syms}
def sustituye(P):
P = sp.Poly(sp.expand(P), *syms)
tot = sp.Integer(0)
for mon, c in P.terms():
term = c
for x, k in zip(syms, mon):
term *= subs[x].N.as_expr() ** k * subs[x].D.as_expr() ** (deg[x] - k)
tot += term
return sp.expand(tot)
return FT(sustituye(A), sustituye(Bd))
def ganancia_estatica_diagrama(F, subs):
e = F.subs({x: ft.sym() for x, ft in subs.items()})
v = sp.limit(sp.together(e), s, 0)
return complex(v) if v.is_finite else float("inf")
# =====================================================================
# Dibujo
# =====================================================================
def _bloque(ax, x, y, txt, w=1.3, h=0.72):
ax.add_patch(Rectangle((x - w / 2, y - h / 2), w, h, fc="white", ec=NEGRO, lw=1.0, zorder=3))
ax.text(x, y, txt, ha="center", va="center", fontsize=12.5, zorder=4)
return x - w / 2, x + w / 2
def _suma(ax, x, y, r=0.2):
ax.add_patch(Circle((x, y), r, fc="white", ec=NEGRO, lw=1.0, zorder=3))
d = r * 0.7071
ax.plot([x - d, x + d], [y - d, y + d], color=NEGRO, lw=0.8, zorder=4)
ax.plot([x - d, x + d], [y + d, y - d], color=NEGRO, lw=0.8, zorder=4)
def _flecha(ax, pts):
xs, ys = zip(*pts)
if len(pts) > 2:
ax.plot(xs[:-1], ys[:-1], color=NEGRO, lw=1.0, zorder=2)
ax.annotate("", xy=pts[-1], xytext=pts[-2],
arrowprops=dict(arrowstyle="-|>", color=NEGRO, lw=1.0, mutation_scale=11,
shrinkA=0, shrinkB=0), zorder=2)
def _linea(ax, pts):
xs, ys = zip(*pts)
ax.plot(xs, ys, color=NEGRO, lw=1.0, zorder=2)
def _nodo(ax, x, y):
ax.plot([x], [y], "o", ms=4.2, color=NEGRO, zorder=5)
def _t(ax, x, y, txt, **kw):
kw.setdefault("fontsize", 11)
kw.setdefault("ha", "center")
kw.setdefault("va", "bottom")
ax.text(x, y, txt, zorder=6, **kw)
def dibuja(topo, negativa=True, resta_paralelo=False, h2_unitario=False):
sgn = "$-$" if negativa else "$+$"
if topo == "Serie":
fig = Figure(figsize=(7.0, 1.3))
ax = fig.add_axes([0, 0, 1, 1])
a1, b1 = _bloque(ax, 2.4, 0, "$G_1(s)$")
a2, b2 = _bloque(ax, 4.9, 0, "$G_2(s)$")
_flecha(ax, [(0.2, 0), (a1, 0)])
_t(ax, 0.9, 0.08, "$X(s)$")
_flecha(ax, [(b1, 0), (a2, 0)])
_t(ax, (b1 + a2) / 2, 0.08, "$V(s)$")
_flecha(ax, [(b2, 0), (7.0, 0)])
_t(ax, 6.4, 0.08, "$Y(s)$")
ax.set_xlim(0, 7.4)
ax.set_ylim(-0.75, 0.75)
elif topo == "Paralelo":
fig = Figure(figsize=(7.0, 2.6))
ax = fig.add_axes([0, 0, 1, 1])
_flecha(ax, [(0.2, 0), (1.4, 0)])
_t(ax, 0.7, 0.08, "$X(s)$")
_nodo(ax, 1.4, 0)
a1, b1 = _bloque(ax, 3.6, 0.95, "$G_1(s)$")
a2, b2 = _bloque(ax, 3.6, -0.95, "$G_2(s)$")
_flecha(ax, [(1.4, 0), (1.4, 0.95), (a1, 0.95)])
_flecha(ax, [(1.4, 0), (1.4, -0.95), (a2, -0.95)])
_suma(ax, 5.8, 0)
_flecha(ax, [(b1, 0.95), (5.8, 0.95), (5.8, 0.2)])
_flecha(ax, [(b2, -0.95), (5.8, -0.95), (5.8, -0.2)])
_t(ax, 5.55, 0.28, "$+$", fontsize=10)
_t(ax, 5.55, -0.5, "$-$" if resta_paralelo else "$+$", fontsize=10)
_t(ax, 4.9, 1.0, "$Y_1$", fontsize=10)
_t(ax, 4.9, -0.9, "$Y_2$", fontsize=10)
_flecha(ax, [(6.0, 0), (7.1, 0)])
_t(ax, 6.6, 0.08, "$Y(s)$")
ax.set_xlim(0, 7.4)
ax.set_ylim(-1.45, 1.45)
elif topo == "Lazo cerrado (realimentación)":
fig = Figure(figsize=(7.0, 2.6))
ax = fig.add_axes([0, 0, 1, 1])
_flecha(ax, [(0.1, 0), (0.85, 0)])
_t(ax, 0.42, 0.08, "$X(s)$")
_suma(ax, 1.05, 0)
a1, b1 = _bloque(ax, 2.65, 0, "$G_1(s)$")
a2, b2 = _bloque(ax, 4.75, 0, "$G_2(s)$")
_flecha(ax, [(1.25, 0), (a1, 0)])
_t(ax, (1.25 + a1) / 2, 0.08, "$E$", fontsize=10)
_flecha(ax, [(b1, 0), (a2, 0)])
_t(ax, (b1 + a2) / 2, 0.08, "$U$", fontsize=10)
_nodo(ax, 6.15, 0)
_flecha(ax, [(b2, 0), (7.15, 0)])
_t(ax, 6.7, 0.08, "$Y(s)$")
ah, bh = _bloque(ax, 3.7, -1.25, "$H_1(s)$")
_flecha(ax, [(6.15, 0), (6.15, -1.25), (bh, -1.25)])
_flecha(ax, [(ah, -1.25), (1.05, -1.25), (1.05, -0.2)])
_t(ax, 1.9, -1.2, "$B$", fontsize=10)
_t(ax, 0.78, 0.18, "$+$", fontsize=10)
_t(ax, 1.3, -0.55, sgn, fontsize=10)
ax.set_xlim(0, 7.4)
ax.set_ylim(-1.75, 0.75)
else:
fig = Figure(figsize=(7.0, 3.1))
ax = fig.add_axes([0, 0, 1, 1])
_flecha(ax, [(0.05, 0), (0.7, 0)])
_t(ax, 0.35, 0.08, "$X$")
_suma(ax, 0.9, 0)
a1, b1 = _bloque(ax, 2.15, 0, "$G_1(s)$", w=1.2)
_flecha(ax, [(1.1, 0), (a1, 0)])
_t(ax, (1.1 + a1) / 2, 0.08, "$E_1$", fontsize=10)
_suma(ax, 3.4, 0)
_flecha(ax, [(b1, 0), (3.2, 0)])
a2, b2 = _bloque(ax, 4.65, 0, "$G_2(s)$", w=1.2)
_flecha(ax, [(3.6, 0), (a2, 0)])
_t(ax, (3.6 + a2) / 2, 0.08, "$E_2$", fontsize=10)
_nodo(ax, 5.85, 0)
_nodo(ax, 6.55, 0)
_flecha(ax, [(b2, 0), (7.3, 0)])
_t(ax, 7.0, 0.08, "$Y$")
ah, bh = _bloque(ax, 4.65, -1.05, "$H_1(s)$", w=1.2)
_flecha(ax, [(5.85, 0), (5.85, -1.05), (bh, -1.05)])
_flecha(ax, [(ah, -1.05), (3.4, -1.05), (3.4, -0.2)])
if h2_unitario:
_linea(ax, [(6.55, 0), (6.55, -2.0), (0.9, -2.0)])
_flecha(ax, [(0.9, -2.0), (0.9, -0.2)])
else:
ah2, bh2 = _bloque(ax, 3.7, -2.0, "$H_2(s)$", w=1.2)
_flecha(ax, [(6.55, 0), (6.55, -2.0), (bh2, -2.0)])
_flecha(ax, [(ah2, -2.0), (0.9, -2.0), (0.9, -0.2)])
for xs_ in (0.9, 3.4):
_t(ax, xs_ - 0.28, 0.18, "$+$", fontsize=10)
_t(ax, xs_ + 0.25, -0.55, sgn, fontsize=10)
_t(ax, 4.65, -1.72, "lazo interno", fontsize=8.5, style="italic", color="#555")
_t(ax, 2.0, -1.95, "lazo externo", fontsize=8.5, style="italic", color="#555")
ax.set_xlim(0, 7.4)
ax.set_ylim(-2.45, 0.7)
ax.set_aspect("equal")
ax.axis("off")
return fig
def _tex_mpl(txt):
"""LaTeX de KaTeX -> mathtext de matplotlib."""
return txt.replace(r"\dfrac", r"\frac").replace(r"\left", "").replace(r"\right", "")
def _largo(txt):
return len(re.sub(r"\[a-zA-Z]+|[{}^ ,]", "", txt))
def dibuja_pid(G, kp, ki, kd, usar, *, N=0.0, deriv_medida=False, saturar=False, umin=None, umax=None,
perturbacion=False):
"""Lazo de control con el PID en estructura paralela (figura 6.2 de los apuntes),
rotulado con los valores actuales de las ganancias y de la planta."""
from nucleo import poly_tex
fig = Figure(figsize=(7.6, 3.0))
ax = fig.add_axes([0, 0, 1, 1])
gris = "#9a9a9a"
activo = [usar[0] and kp != 0, usar[1] and ki != 0, usar[2] and kd != 0]
def bloque_accion(y, txt, on):
ax.add_patch(Rectangle((2.75, y - 0.32), 2.7, 0.64, fc="white", ec=NEGRO if on else gris, lw=1.0,
ls="-" if on else "--", zorder=3))
ax.text(4.1, y, txt if on else txt + " (apagada)", ha="center", va="center", fontsize=10.5,
color=NEGRO if on else gris, zorder=4)
_flecha(ax, [(0.05, 0), (0.75, 0)])
_t(ax, 0.38, 0.08, "$r(t)$", fontsize=10)
_suma(ax, 0.95, 0)
_t(ax, 0.68, 0.17, "$+$", fontsize=9)
_t(ax, 1.2, -0.52, "$-$", fontsize=9)
_linea(ax, [(1.15, 0), (1.95, 0)])
_t(ax, 1.55, 0.08, "$e(t)$", fontsize=10)
_nodo(ax, 1.95, 0)
bloque_accion(1.2, f"P: $K_p={kp:.3g}$", activo[0])
bloque_accion(0.0, f"I: $K_i/s$, $K_i={ki:.3g}$", activo[1])
dtxt = (f"D: $K_d s/(1+(T_d/N)s)$, $K_d={kd:.3g}$" if (N and N > 0) else f"D: $K_d\\,s$, $K_d={kd:.3g}$")
bloque_accion(-1.2, dtxt, activo[2])
_flecha(ax, [(1.95, 0), (1.95, 1.2), (2.75, 1.2)])
_flecha(ax, [(1.95, 0), (2.75, 0)])
if deriv_medida:
_flecha(ax, [(2.35, -2.35), (2.35, -1.2), (2.75, -1.2)])
_t(ax, 2.2, -1.85, "$-y$", fontsize=9.5, ha="right")
else:
_flecha(ax, [(1.95, 0), (1.95, -1.2), (2.75, -1.2)])
_suma(ax, 6.05, 0)
_flecha(ax, [(5.45, 1.2), (6.05, 1.2), (6.05, 0.2)])
_flecha(ax, [(5.45, 0), (5.85, 0)])
_flecha(ax, [(5.45, -1.2), (6.05, -1.2), (6.05, -0.2)])
for (x_, y_) in ((6.25, 0.32), (5.72, 0.1), (6.25, -0.55)):
_t(ax, x_, y_, "$+$", fontsize=9)
x = 6.25
if saturar:
ax.add_patch(Rectangle((6.75, -0.32), 0.8, 0.64, fc="white", ec=NEGRO, lw=1.0, zorder=3))
ax.plot([6.85, 7.0, 7.3, 7.45], [-0.2, -0.2, 0.2, 0.2], color=NEGRO, lw=1.0, zorder=4)
_flecha(ax, [(x, 0), (6.75, 0)])
lim = f"[{umin:.3g}, {umax:.3g}]" if (umin is not None and umax is not None) else ""
_t(ax, 7.15, 0.38, "saturación " + lim, fontsize=8, style="italic", color="#555")
x = 7.55
nume = poly_tex(G.num)
deno = poly_tex(G.den)
ancho = max(1.9, 0.17 * max(_largo(nume), _largo(deno)) + 1.0)
if perturbacion:
xd = x + 1.0
_suma(ax, xd, 0)
_flecha(ax, [(x, 0), (xd - 0.2, 0)])
_t(ax, (x + xd - 0.2) / 2, 0.08, "$u(t)$", fontsize=10)
_flecha(ax, [(xd, 0.95), (xd, 0.2)])
_t(ax, xd, 0.98, "perturbación $d(t)$", fontsize=9.5)
_t(ax, xd - 0.27, 0.17, "$+$", fontsize=9)
_t(ax, xd + 0.27, 0.2, "$+$", fontsize=9)
x = xd + 0.2
cx = x + 0.6 + ancho / 2
a, bx = _bloque(ax, cx, 0, "", w=ancho, h=0.95)
ax.text(cx, 0.0, "$G(s)=" + _tex_mpl(G.tex()) + "$", ha="center", va="center", fontsize=11, zorder=4)
_t(ax, cx, 0.55, "planta", fontsize=8.5, style="italic", color="#555")
_flecha(ax, [(x, 0), (a, 0)])
if not perturbacion:
_t(ax, (x + a) / 2, 0.08, "$u(t)$", fontsize=10)
xn = bx + 0.55
_nodo(ax, xn, 0)
_flecha(ax, [(bx, 0), (xn + 0.85, 0)])
_t(ax, xn + 0.45, 0.08, "$y(t)$", fontsize=10)
_linea(ax, [(xn, 0), (xn, -2.35), (0.95, -2.35)])
_flecha(ax, [(0.95, -2.35), (0.95, -0.2)])
_t(ax, (xn + 0.95) / 2, -2.3, "realimentación de la medida", fontsize=8.5, style="italic", color="#555")
ax.set_xlim(-0.05, xn + 1.0)
ax.set_ylim(-2.75, 1.75)
ax.set_aspect("equal")
ax.axis("off")
fig.set_size_inches(7.6 * (xn + 1.05) / 13.0, 7.6 * (xn + 1.05) / 13.0 * 4.5 / (xn + 1.05))
return fig
|