Skip to content

Instantly share code, notes, and snippets.

@Klafyvel
Last active May 14, 2016 14:38
Show Gist options
  • Save Klafyvel/e851ed3c72b1a29bac44225310bb7a72 to your computer and use it in GitHub Desktop.
Save Klafyvel/e851ed3c72b1a29bac44225310bb7a72 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as pl
from matplotlib import animation
#CONFIG
VCC = 5 # V
R = int(input("R=")) # Ohms
P = 700 # Ohms
C = 1e-6 # F
Ud = 0.5 # V
t_max = 1e-3 # s
#CALC STUFF :D
pl.close('all')
fig, ax = pl.subplots()
X = np.linspace(0,t_max,500)
Y_C = [t for t in X]
Y_NE = [t for t in X]
alpha_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
freq_text = ax.text(0.02, 0.90, '', transform=ax.transAxes)
pl.ylim(-0.1, 5.1)
pl.title("Sortie du NE555 et bornes du condensateur avec R={} $\\Omega$".format(R))
pl.xlabel("Temps (ms)")
pl.ylabel("Tension (V)")
f_NE, = pl.plot(X,Y_NE,label="Sortie du NE555")
f_C, = pl.plot(X,Y_C,label="Bornes du condensateur")
pl.hlines(10/3, 0, t_max, label=r'$\frac{2}{3}$ VCC', color='r')
pl.hlines(5/3, 0, t_max, label=r'$\frac{1}{3}$ VCC', color='y')
pl.legend()
def update(v):
curseur = v #scur.val
tau_c = (curseur/100 * P + R) * C
tau_d = (1 - curseur/100) * P * C
t_c = tau_c * np.log((2/3*VCC - Ud)/(1/3*VCC - Ud))
t_d = tau_d * np.log((2/3*VCC + Ud)/(1/3*VCC + Ud))
A_c = - (2/3 * VCC - Ud)
C_c = VCC - Ud
A_d = 2/3 * VCC + Ud
C_d = - Ud
f_c = lambda t: A_c * np.exp(-t/tau_c) + C_c
f_d = lambda t: A_d * np.exp(-t/tau_d) + C_d
Y_C = []
Y_NE = []
charge = True
last_change = 0
for t in X:
if (charge and (t-last_change > t_c)) or ((not charge) and (t-last_change > t_d)):
charge = not charge
last_change = t
if charge :
Y_C.append(f_c(t-last_change))
Y_NE.append(5)
else:
Y_NE.append(0)
Y_C.append(f_d(t-last_change))
f_NE.set_ydata(Y_NE)
f_C.set_ydata(Y_C)
freq_text.set_text("$f = {}Hz$".format(round(1 / (t_c + t_d)),2))
alpha_text.set_text("$\\alpha = {} \\%$".format(round(t_c / (t_c+t_d) * 100, 2)))
return f_NE, f_C, freq_text, alpha_text
def animate(i):
print(i, "%", end="\r")
return update(i)
#scur.on_changed(update)
anim = animation.FuncAnimation(fig, animate, frames=100, interval=20, blit=True)
anim.save('{}_Ohms.mp4'.format(R), fps=30, extra_args=['-vcodec', 'libx264'])
#pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment