diff --git a/dimmer.ipynb b/dimmer.ipynb new file mode 100644 index 0000000..f2354fc --- /dev/null +++ b/dimmer.ipynb @@ -0,0 +1,94 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "source": [ + "\r\n" + ], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": 1, + "source": [ + "from dimmer import Dimmer\r\n", + "import matplotlib.pyplot as plt\r\n", + "import numpy as np\r\n", + "\r\n", + "dimmer = Dimmer(frec=1, amp=1)" + ], + "outputs": [], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": 3, + "source": [ + "print(f'Duty 100% = {dimmer.convert_duty_rms(100):.4f} v')\r\n", + "print(f'Rms 220v = {dimmer.convert_rms_duty(220):.4f} %')\r\n", + "print(f'amplitud = {dimmer.amplitud}')\r\n", + "print(f'convert_duty_rms = {dimmer.convert_duty_rms(100)}')\r\n", + "print(f'convert_rms_duty = {dimmer.convert_rms_duty(0.7)}')\r\n", + "print(f'frequency = {dimmer.frequency}')\r\n", + "print(f'solve_tint_for_duty = {dimmer.solve_tint_for_duty(100)}')\r\n", + "print(f'time_interrupt = {dimmer.time_interrupt}')\r\n", + "print(f'v_dimmer = {dimmer.v_dimmer(0.1)}')\r\n", + "print(f'_v_sine = {dimmer._v_sine(0.1)}')\r\n", + "print(f'vrms_num = {dimmer.vrms_num()}')\r\n", + "print(f'vrms_simbolic = {dimmer.vrms_simbolic()}')" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Duty 100% = 70.7107 v\n", + "Rms 220v = 31112.6984 %\n", + "amplitud = 1\n", + "convert_duty_rms = 70.71067811865476\n", + "convert_rms_duty = 98.99494936611664\n", + "frequency = 1\n", + "solve_tint_for_duty = 0.0\n", + "time_interrupt = 0.5\n", + "v_dimmer = 0\n", + "_v_sine = 0.5877852522924731\n", + "vrms_num = 0.0\n", + "vrms_simbolic = 0.0\n" + ] + } + ], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": null, + "source": [], + "outputs": [], + "metadata": {} + } + ], + "metadata": { + "orig_nbformat": 4, + "language_info": { + "name": "python", + "version": "3.9.1", + "mimetype": "text/x-python", + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "pygments_lexer": "ipython3", + "nbconvert_exporter": "python", + "file_extension": ".py" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3.9.1 64-bit ('env': venv)" + }, + "interpreter": { + "hash": "a4046a09f199e48e61396275ea0e14da7d6ee49ffd95343aa82a9cc54eb636e7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/dimmer.py b/dimmer.py new file mode 100644 index 0000000..2d72f6a --- /dev/null +++ b/dimmer.py @@ -0,0 +1,76 @@ +import numpy as np +from scipy.optimize import bisect + + +class Dimmer: + def __init__(self, frec, amp, time_int=0) -> None: + self.frequency = frec + self.amplitud = amp + self.time_interrupt = time_int + self._periode = 1 / frec + + + 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 + tint = self.time_interrupt + + if (0 <= t and t < tint): + return 0 + elif (tint <= t and t < 0.5 * per): + return self._v_sine(t) + elif (0.5*per <= t and t < 0.5*per + tint): + 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) + v_series = [self.v_dimmer(t) for t in t_series] + 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 + + def sine_integrate(t): + 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_b1 = sine_integrate(T2 / 2) + rms_b2 = sine_integrate(tint) + 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_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 + return vrms + + def solve_tint_for_duty(self, duty_perc): + def function(tint, bias=0): + self.time_interrupt = tint + 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)) + return root + \ No newline at end of file