Skip to content

Commit

Permalink
Clase dimmer implementada. Faltan optimizaciones de calculos repetido…
Browse files Browse the repository at this point in the history
…s en el plotter pero no vienen al caso.
  • Loading branch information
barreiroleo committed Aug 27, 2021
1 parent 3faa6a8 commit 189d521
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 302 deletions.
94 changes: 0 additions & 94 deletions dimmer.ipynb

This file was deleted.

72 changes: 44 additions & 28 deletions dimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@

class Dimmer:
def __init__(self, frec, amp, time_int=0) -> None:
self.frequency = frec
self.amplitud = amp
self.__frequency = frec
self.amplitud = amp
self.time_interrupt = time_int
self._periode = 1 / frec


self.__periode = 1 / frec

@property
def frequency(self): return self.__frequency
@property
def periode(self): return self.__periode

@frequency.setter
def frequency(self, frec):
self.__frequency, self.__periode = frec, 1 / frec

@periode.setter
def periode(self, periode):
self.__periode, self.__frequency = periode, 1 / periode

def _v_sine(self, t):
omega = 2 * np.pi * self.frequency
return self.amplitud * np.sin(omega * t)

def v_dimmer(self, t):
per = self._periode
per = self.periode
tint = self.time_interrupt

if (0 <= t and t < tint):
return 0
elif (tint <= t and t < 0.5 * per):
Expand All @@ -26,51 +38,55 @@ def v_dimmer(self, t):
return 0
elif (0.5*per + tint <= t and t < per):
return self._v_sine(t)

def vrms_num(self):
t_series = np.arange(0, self._periode, self._periode / 200)
t_series = np.arange(0, self.periode, self.periode / 200)
v_series = [self.v_dimmer(t) for t in t_series]
v_sum = 0
v_sum = 0
for i in v_series:
v_sum = v_sum + i**2
rms = np.sqrt(v_sum / len(v_series))
return rms

def vrms_simbolic(self):
amp, omega = self.amplitud, 2 * np.pi * self.frequency
per, tint = self._periode, self.time_interrupt
T1, T2 = 0, per
per, tint = self.periode, self.time_interrupt
T1, T2 = 0, per

def sine_integrate(t):
int_a = amp ** 2
int_b = t / 2
int_c = (np.sin(2*t*omega) / (4*omega))
int_a = amp ** 2
int_b = t / 2
int_c = (np.sin(2*t*omega) / (4*omega))
sine_int = int_a * (int_b - int_c)
return sine_int

rms_a = 1 / (T2 - T1)
rms_a = 1 / (T2 - T1)
rms_b1 = sine_integrate(T2 / 2)
rms_b2 = sine_integrate(tint)
rms_b = (rms_b1 - rms_b2) * 2
rms = np.sqrt(rms_a * rms_b)
rms_b = (rms_b1 - rms_b2) * 2
rms = np.sqrt(rms_a * rms_b)
return rms

def convert_rms_duty(self, vrms):
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms_duty = 100 * vrms / vrms_max
return vrms_duty

def convert_duty_rms(self, vrms_duty):
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms = vrms_duty * vrms_max
vrms = vrms_duty * vrms_max
return vrms

def solve_tint_for_duty(self, duty_perc):
# HACK: Se respalda el estado del time interrupt porque se utiliza calacular. Al final se restaura.
time_interrupt_backup = self.time_interrupt

def function(tint, bias=0):
self.time_interrupt = tint
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms_duty = self.vrms_simbolic() / vrms_max
return vrms_duty - bias
root = bisect(function, 0, self._periode * 0.5, args=(duty_perc / 100))
root = bisect(function, 0, self.periode * 0.5, args=(duty_perc / 100))

self.time_interrupt = time_interrupt_backup
return root

118 changes: 48 additions & 70 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,91 @@
from scipy.optimize import bisect

from functions import *
from dimmer import Dimmer

dimmer = Dimmer(frec=50, amp=1)


def main():
for i in range(100+1):
print("duty([per/2] * {:>14.10f}%)= {:>6.2f}%".format(solve_tInt(i), i))

tint = dimmer.solve_tint_for_duty(i)
print(f"duty({tint*1e3:>13.10f} ms)= {i:>6.2f}%")
init_plot()


def gen_data(tInt_perc=50, frec=1, amp=1):
samples = 100
periode = 1 / frec
t = np.linspace(0, periode, samples)
v_dimmer = [fun_Dimmer(ti, tInt_perc, frec, amp) for ti in t]
rms_dimmer = fun_rms_simbolic(tInt_perc, frec, amp) * np.ones(samples)
return [t, v_dimmer, rms_dimmer]


def init_plot():
global fig, ax1, l1, l2
t_series, vt_series, rms_series = gen_data()
t_serie, v_serie, vrms_serie = gen_data()
plt.ion()

fig, ax1 = plt.subplots(figsize=(6, 6))
plt.subplots_adjust(bottom=0.35, top=0.95, right=0.89, wspace=0, hspace=0)

l1, = plt.plot(t_series, vt_series)
l2, = plt.plot(t_series, rms_series)
l1, = plt.plot(t_serie, v_serie)
l2, = plt.plot(t_serie, vrms_serie)

plt.grid(True)
update_plot()
setting_sliders()
ax1.set_ylabel(r'Amplitud [v]')
secax_y = ax1.secondary_yaxis(
'right', functions=(rms_to_duty, duty_to_rms))
secax_y = ax1.secondary_yaxis('right', functions=(
dimmer.convert_rms_duty, dimmer.convert_rms_duty))
secax_y.set_ylabel(r'Duty [%]')
plt.show(block=True)


def update_plot(tInt_perc=50, frec=1, ampl=1):
def gen_data():
t_serie = np.linspace(0, dimmer.periode, 100)
v_serie = [dimmer.v_dimmer(t) for t in t_serie]
vrms_serie = dimmer.vrms_simbolic() * np.ones(len(t_serie))
return [t_serie, v_serie, vrms_serie]


def update_plot():
global fig, ax1, l1, l2
t_series, vt_series, rms_series = gen_data(tInt_perc, frec, ampl)
t_serie, v_serie, vrms_serie = gen_data()
# Plot data
l1.set_xdata(t_series), l2.set_xdata(t_series)
l1.set_ydata(vt_series), l2.set_ydata(rms_series)
l1.set_xdata(t_serie), l2.set_xdata(t_serie)
l1.set_ydata(v_serie), l2.set_ydata(vrms_serie)
# Update legend
sine_str = r'$v(t) = \mathcal{A} \mathrm{sin}(2 \omega t)$'
rms_str = "RMS: {:.5f}".format(rms_series[0])
rms_str = "RMS: {:.5f}".format(vrms_serie[0])
ax1.legend([sine_str, rms_str])
# Update limits
ax1.set_xlim(np.min(0), np.max(1 / frec))
ax1.set_ylim(-ampl, ampl)
ax1.set_xlim(0, dimmer.periode)
ax1.set_ylim(-dimmer.amplitud, dimmer.amplitud)


def setting_sliders():
def update_sliders(val):
update_plot(slide_tInt.val, slide_frec.val, slide_amp.val)
frec, ampl, tInt_perc = slide_frec.val, slide_amp.val, slide_tInt.val
rms_unit = (np.sqrt(2) / 2) * ampl
rms_value = fun_rms_simbolic(tInt_perc, frec=frec, amp=ampl) / rms_unit
slide_duty.set_val(rms_value * 100)

def update_slider_duty(val):
tint = solve_tInt(slide_duty.val, slide_frec.val, slide_amp.val)
slide_tInt.set_val(tint)

global ax_frec, ax_amp, ax_tInt, ax_duty, ax_solve
global slide_frec, slide_amp, slide_tInt, slide_duty, btn_solve
ax_frec = plt.axes([0.1, 0.10, 0.80, 0.03])
ax_amp = plt.axes([0.1, 0.15, 0.80, 0.03])
ax_tInt = plt.axes([0.1, 0.20, 0.80, 0.03])
ax_duty = plt.axes([0.1, 0.25, 0.6, 0.03])
ax_solve = plt.axes([0.8, 0.25, 0.1, 0.03])
slide_frec = Slider(ax_frec, 'Frec', 1.0, 50, valinit=1, valstep=1)
slide_amp = Slider(ax_amp, 'Amp', 1.0, 311, valinit=1, valstep=1)
slide_tInt = Slider(ax_tInt, '%Int', 0.0, 100, valinit=50, valstep=1)
slide_duty = Slider(ax_duty, '%Duty', 0.0, 100, valinit=50, valstep=1)
btn_solve = Button(ax_solve, 'Solve')

def update_sliders(val):
dimmer.frequency = slide_frec.val
dimmer.amplitud = slide_amp.val
dimmer.time_interrupt = dimmer.periode * 0.5 * slide_tInt.val / 100
vrms_max = np.sqrt(2) * 0.5 * dimmer.amplitud
slide_duty.set_val(100 * dimmer.vrms_simbolic() / vrms_max)
update_plot()

def do_tint_solve(val):
tint = dimmer.solve_tint_for_duty(slide_duty.val)
slide_tInt.set_val(100 * tint / (dimmer.periode * 0.5))

ax_frec = plt.axes([0.12, 0.10, 0.80, 0.03])
ax_amp = plt.axes([0.12, 0.15, 0.80, 0.03])
ax_tInt = plt.axes([0.12, 0.20, 0.80, 0.03])
ax_duty = plt.axes([0.12, 0.25, 0.60, 0.03])
ax_solve = plt.axes([0.8, 0.25, 0.10, 0.03])
slide_frec = Slider(ax_frec, 'Frec Hz', 1.0, 50, valinit=1, valstep=1)
slide_amp = Slider(ax_amp, 'Ampl V', 1.0, 311, valinit=1, valstep=1)
slide_tInt = Slider(ax_tInt, 'Int. %', 0.0, 100, valinit=50, valstep=1)
slide_duty = Slider(ax_duty, 'Duty %', 0.0, 100, valinit=50, valstep=1)
btn_solve = Button(ax_solve,'Solve')
slide_frec.on_changed(update_sliders)
slide_amp.on_changed(update_sliders)
slide_tInt.on_changed(update_sliders)
# slide_duty.on_changed(solve_tInt)
btn_solve.on_clicked(update_slider_duty)


def rms_to_duty(x):
vrms = x
# vrms = fun_rms_simbolic(slide_tInt.val, slide_frec.val, slide_amp.val)
vrms_max = np.sqrt(2) * 0.5 * slide_amp.val
vrms_duty = 100 * vrms / vrms_max
return vrms_duty


def duty_to_rms(x):
vrms_duty = x
vrms_max = np.sqrt(2) * 0.5 * slide_amp.val
vrms = vrms_duty * vrms_max / 100
return vrms


def solve_tInt(duty, frec=1, amp=1):
def function(tInt_perc, bias=0):
rms_unit = (np.sqrt(2) / 2) * amp
rms_value = fun_rms_simbolic(tInt_perc, frec=frec, amp=amp) / rms_unit
return rms_value - bias
root = bisect(function, 0, 100, args=(duty / 100))
return root
btn_solve.on_clicked(do_tint_solve)


if __name__ == "__main__":
Expand Down
156 changes: 46 additions & 110 deletions rms_vs_tinterrupt.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 189d521

Please sign in to comment.