rcnet.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. # Xytronic LF-1600
  3. # Open Source firmware
  4. # R/C network calculation
  5. #
  6. # Copyright (c) 2019 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. """
  22. import math
  23. __all__ = [
  24. "RCNet",
  25. ]
  26. class RCNet(object):
  27. """Discrete R/C network calculation.
  28. """
  29. __slots__ = (
  30. "r", # Resistor value, in Ohms.
  31. "c", # Capacitor value, in Farad.
  32. "v", # Capacitor voltage.
  33. "i", # Network current, in Ampere.
  34. )
  35. def __init__(self, r, c, v=None, q=None):
  36. self.r = r
  37. self.c = c
  38. self.v = v or 0.0
  39. if q is not None:
  40. assert(v is None)
  41. self.q = q
  42. self.i = 0.0
  43. @property
  44. def q(self):
  45. """Get charge, in Coulomb.
  46. """
  47. return self.c * self.v
  48. @q.setter
  49. def q(self, newQ):
  50. """Set charge, in Coulomb.
  51. """
  52. self.v = newQ / self.c
  53. def calc(self, vIn, dt):
  54. """Calculate the next discrete step.
  55. Returns the new C voltage.
  56. """
  57. c, r = self.c, self.r
  58. dv = vIn - self.v
  59. fact = math.exp((-1.0 / (r * c)) * dt)
  60. self.i = (dv / r) * fact
  61. self.v += dv * (1.0 - fact)
  62. return self.v
  63. def __str__(self):
  64. return "v=%.3f V, i=%.3f mA, q=%.3f uC" % (
  65. self.v,
  66. self.i * 1e3,
  67. self.q * 1e6)
  68. if __name__ == "__main__":
  69. import matplotlib.pyplot as plt
  70. rc = RCNet(r=(10e3),
  71. c=(220e-6),
  72. q=0.0)
  73. dt = 0.01
  74. times = [t * dt for t in range(500)]
  75. values = []
  76. for t in times:
  77. rc.calc(vIn=(5.0 if t <= 3.0 else -2.0),
  78. dt=dt)
  79. print("t=%.3f s, %s" % (t, str(rc)))
  80. values.append((rc.q * 1e6, rc.i * 1e3, rc.v))
  81. fig, ax = plt.subplots(3, sharex=True)
  82. ax[0].plot(times, [val[0] for val in values], label="Q (uC)")
  83. ax[0].legend()
  84. ax[1].plot(times, [val[1] for val in values], label="I (mA)")
  85. ax[1].legend()
  86. ax[2].plot(times, [val[2] for val in values], label="U (V)")
  87. ax[2].legend()
  88. ax[2].set_xlabel("t (s)")
  89. plt.show()