from machine import Pin
import time
import math
pines = [2, 3, 4, 5, 6, 7, 8, 9, 10]
def GPIO_SALP(valor, listagpio):
pines_gpio = [Pin(pin, Pin.OUT) for pin in listagpio]
ii = 1
for i in range(9):
if valor & ii:
pines_gpio[i].value(1)
else:
pines_gpio[i].value(0)
ii = ii << 1
print("Serie de Fourier - Personalizada")
def graficador(nmax):
xmin = -2
xmax = 2
delta = 0.1
T = 6
x = xmin
while x < xmax:
suma = 0
w0 = 2 * math.pi / T
a0 = (1/3)*(
(0.01*(1))
+ (-2*(((-0.3+0.6)**2)/2 - ((-0.6+0.6)**2)/2))
+ ((46/3)*((0**2)/2 - ((-0.3)**2)/2) + 4*(0 - (-0.3)))
+ ((-50/3)*((0.3**2)/2 - (0**2)/2) + 4*(0.3 - 0))
+ ((10/3)*((0.6**2)/2 - (0.3**2)/2) -2*(0.6 - 0.3))
)
n = 1
while n <= nmax:
k = n * math.pi / 3
an = (1/3)*(
(0.01*(math.sin(k*(-1)) - math.sin(k*(-2)))/k)
- (1/4)*(
( -math.cos((3.14+k)*(-1))/(3.14+k) + math.cos((3.14+k)*(-2))/(3.14+k) )
+ ( -math.cos((3.14-k)*(-1))/(3.14-k) + math.cos((3.14-k)*(-2))/(3.14-k) )
)
+ (
-2*(
((-0.3)*math.sin(k*(-0.3))/k + math.cos(k*(-0.3))/k**2)
- ((-0.6)*math.sin(k*(-0.6))/k + math.cos(k*(-0.6))/k**2)
)
-1.2*(math.sin(k*(-0.3)) - math.sin(k*(-0.6)))/k
)
+ (
(46/3)*(
(0*math.sin(k*0)/k + math.cos(k*0)/k**2)
- ((-0.3)*math.sin(k*(-0.3))/k + math.cos(k*(-0.3))/k**2)
)
+ 4*(math.sin(k*0) - math.sin(k*(-0.3)))/k
)
+ (
(-50/3)*(
(0.3*math.sin(k*0.3)/k + math.cos(k*0.3)/k**2)
- (0*math.sin(k*0)/k + math.cos(k*0)/k**2)
)
+ 4*(math.sin(k*0.3) - math.sin(k*0))/k
)
+ (
(10/3)*(
(0.6*math.sin(k*0.6)/k + math.cos(k*0.6)/k**2)
- (0.3*math.sin(k*0.3)/k + math.cos(k*0.3)/k**2)
)
-2*(math.sin(k*0.6) - math.sin(k*0.3))/k
)
)
bn = (1/3)*(
(-0.01*(math.cos(k*(-1)) - math.cos(k*(-2)))/k)
- (1/4)*(
( math.sin((3.14-k)*(-1))/(3.14-k) - math.sin((3.14-k)*(-2))/(3.14-k) )
- ( math.sin((3.14+k)*(-1))/(3.14+k) - math.sin((3.14+k)*(-2))/(3.14+k) )
)
+ (
-2*(
( -(-0.3)*math.cos(k*(-0.3))/k + math.sin(k*(-0.3))/k**2 )
- ( -(-0.6)*math.cos(k*(-0.6))/k + math.sin(k*(-0.6))/k**2 )
)
-1.2*(-math.cos(k*(-0.3)) + math.cos(k*(-0.6)))/k
)
+ (
(46/3)*(
( -(0)*math.cos(k*0)/k + math.sin(k*0)/k**2 )
- ( -(-0.3)*math.cos(k*(-0.3))/k + math.sin(k*(-0.3))/k**2 )
)
+ 4*(-math.cos(k*0) + math.cos(k*(-0.3)))/k
)
+ (
(-50/3)*(
( -(0.3)*math.cos(k*0.3)/k + math.sin(k*0.3)/k**2 )
- ( -(0)*math.cos(k*0)/k + math.sin(k*0)/k**2 )
)
+ 4*(-math.cos(k*0.3) + math.cos(k*0))/k
)
+ (
(10/3)*(
( -(0.6)*math.cos(k*0.6)/k + math.sin(k*0.6)/k**2 )
- ( -(0.3)*math.cos(k*0.3)/k + math.sin(k*0.3)/k**2 )
)
-2*(-math.cos(k*0.6) + math.cos(k*0.3))/k
)
)
suma += an * math.cos(n*w0*x) + bn * math.sin(n*w0*x)
n += 1
fx = a0/2 + suma
print(fx)
valor = int((fx + 2)*50)
if valor < 0:
valor = 0
if valor > 511:
valor = 511
GPIO_SALP(valor, pines)
x += delta
time.sleep(0.001)
while True:
for narmonicos in range(1, 1000, 2):
graficador(narmonicos)
time.sleep(0.1)