Commit 6fc19f88 authored by Tristan Renon's avatar Tristan Renon
Browse files

Working trapeze ans sin3 + shifter

parent aa15c7dd
......@@ -26,6 +26,37 @@ def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin
def generateSin3(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftReg3=None, phaseAmplitude3=None, phaseShiftMec=None, nSamples=100):
# Generate things if user did not tell about them
if nSamples is None:
nSamples = 50
if phaseAmplitude is None or len(phaseAmplitude) != n:
phaseAmplitude = np.ones(n)
if phaseAmplitude3 is None or len(phaseAmplitude3) != n:
phaseAmplitude3 = 0.15*phaseAmplitude
if phaseShiftMec is None or len(phaseShiftMec) != n:
phaseShiftMec = np.linspace(0, 360, n, endpoint=False)
if phaseShiftReg is None or len(phaseShiftReg) != n:
phaseShiftReg = phaseShiftMec
if phaseShiftReg3 is None or len(phaseShiftReg3) != n:
phaseShiftReg3 = phaseShiftMec
# Generate x (0 to 2pi)
xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list()
# Generate power supply for coils
for i in range(n):
ret.append(phaseAmplitude[i]*np.sin(xSin+phaseShiftReg[i]/180*np.pi)
+ phaseAmplitude3[i]*np.sin(3*(xSin+phaseShiftReg3[i]/180*np.pi)))
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin
def generateSquare(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None, nSamples=50):
# Generate things if user did not tell about them
if nSamples is None:
......@@ -75,7 +106,9 @@ def generateSinPWM(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=N
ret.append(phaseAmplitude[i]*signal.square((xSin+phaseShiftReg[i]/180*np.pi)*29, duty=(sig+1)/2))
duty = (sig+1)/2
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin, duty
# Duty could be return for purposes
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin
def generateTriangle(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None, nSamples=100):
# Generate things if user did not tell about them
......@@ -120,68 +153,71 @@ def generateTrapeze(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=
ret = list()
# Generate power supply for coils
trap = trapeze(dataX=xSin, nSamples=nSamples)[0]
for i in range(n):
sig = np.sin(xSin+phaseShiftReg[i]/180*np.pi)
sig = signal.sawtooth()
ret.append(phaseAmplitude[i]*signal.square((xSin+phaseShiftReg[i]/180*np.pi)*30, duty=(sig+1)/2))
ret.append(phaseShifter(phaseAmplitude[i]*trap, phaseShiftReg[i]/180*np.pi))
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin
# def trapeze(dataX, thetaZ, thetaOmega, nSamples=50):
# if nSamples is None:
# nSamples = 50
# if dataX is None:
# dataX = np.linspace(0, 2 * np.pi, nSamples)
# if thetaZ is None:
# thetaZ = dataX[-1]/64
# if thetaOmega is None:
# thetaOmega=thetaZ
#
# trapeze = dataX
# for i in range(len(dataX)):
# if dataX[i] <= dataX[-1]/2:
# if dataX[i] < thetaZ:
# trapeze[i] = 0
# elif dataX[i] < thetaZ+thetaOmega:
# trapeze[i] = FAIRE UNE PENTE de 0 à 1
# elif dataX[i] < dataX[-1]/2-thetaZ-thetaOmega:
# trapeze[i] = 1
# elif dataX[i] < dataX[-1]/2-thetaZ:
# trapeze[i] = FAIRE UNE PENTE de 1 à 0
# else:
# trapeze[i] = 0
# else:
# if dataX[i] < dataX[-1]/2+thetaZ:
# trapeze[i] = 0
# elif dataX[i] < dataX[-1]/2+thetaZ+thetaOmega:
# trapeze[i] = FAIRE UNE PENTE de 0 à -1
# elif dataX[i] < dataX[-1]-thetaZ-thetaOmega:
# trapeze[i] = -1
# elif dataX[i] < dataX[-1]-thetaZ:
# trapeze[i] = Faire Une pente de -1 à 0
# else:
# trapeze[i] = 0
# return trapeze
def trapeze(dataX=None, thetaZ=None, thetaOmega=None, nSamples=50):
if nSamples is None:
nSamples = 50
if dataX is None:
dataX = np.linspace(0, 2 * np.pi, nSamples)
if thetaZ is None:
thetaZ = dataX[-1]/16
if thetaOmega is None:
thetaOmega=thetaZ
trapeze = list()
for i in range(len(dataX)):
if dataX[i] <= dataX[-1]/2:
if dataX[i] <= thetaZ:
trapeze.append(0)
elif dataX[i] <= thetaZ+thetaOmega:
trapeze.append((dataX[i]-thetaZ)*1/thetaOmega)
elif dataX[i] <= dataX[-1]/2-thetaZ-thetaOmega:
trapeze.append(1)
elif dataX[i] <= dataX[-1]/2-thetaZ:
trapeze.append(1-((dataX[i]-(dataX[-1]/2-thetaZ-thetaOmega))*1/thetaOmega))
else:
trapeze.append(0)
else:
if dataX[i] <= dataX[-1]/2+thetaZ:
trapeze.append(0)
elif dataX[i] <= dataX[-1]/2+thetaZ+thetaOmega:
trapeze.append((dataX[i]-dataX[-1]/2-thetaZ)*(-1/thetaOmega))
elif dataX[i] <= dataX[-1]-thetaZ-thetaOmega:
trapeze.append(-1)
elif dataX[i] <= dataX[-1]-thetaZ:
trapeze.append(-1-(dataX[i]-(dataX[-1]-thetaZ-thetaOmega))*(-1/thetaOmega))
else:
trapeze.append(0)
trapeze = np.array(trapeze)
return trapeze, dataX
def phaseShifter(dataToShift, shiftRad):
index = int(len(dataToShift)*shiftRad/(2*np.pi))
shifted = list()
shifted = list(dataToShift[index:-1])+list(dataToShift[0:index+1])
shifted = np.array(shifted)
return shifted
'''
n = 3
nSamples = 1000
res, _, _, _, _, xSin = generateSin(n, nSamples=nSamples)
# res, _, _, _, _, xSin = generateSin(n, nSamples=nSamples)
# res, _, _, _, _, xSin = generateSquare(n)
ros, _, _, _, _, xSin, duty = generateSinPWM(n, nSamples=nSamples)
# ros, _, _, _, _, xSin, duty = generateSinPWM(n, nSamples=nSamples)
# res, _, _, _, _, xSin = generateTriangle(n)
res, _, _, _, _, xSin = generateTrapeze(n, nSamples=nSamples)
# res, _, _, _, _, xSin = generateSin3(n, nSamples=nSamples)
# for i in range(n):
# plt.plot(xSin, res[i])
# plt.plot(xSin, ros[i])
plt.plot(xSin, res[0])
plt.plot(xSin, ros[1])
plt.plot(xSin, duty)
'''
plt.show()
......@@ -15,10 +15,11 @@ inputNPhases = 5
# generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, _ = gsl.generateSin(inputNPhases, inputPhaseShiftReg, inputPhaseAmplitude, inputPhaseShiftMec, nSamples=nSamples)
generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, _, _ = gsl.generateSinPWM(n=inputNPhases, nSamples=nSamples)
# generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, _ = gsl.generateSin3(n=inputNPhases, nSamples=nSamples)
generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, _ = gsl.generateSinPWM(n=inputNPhases, nSamples=nSamples)
fig, ax = plt.subplots(figsize=(8, 8))
limit = (nPhases+1)/2*2
limit = (nPhases+1)/2
def animate(i):
ax.cla()
......@@ -50,7 +51,7 @@ def animate(i):
ax.arrow(sliding[0][0], sliding[0][1],
sliding[1][0], sliding[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
head_width=.1, head_length=.1, fc=colors, ec=colors, linewidth=1)
# Needed to draw the ellipse
xTemp = actualCos*generatedSin[j][:i]
......@@ -60,7 +61,7 @@ def animate(i):
plotX += xTemp
plotY += yTemp
ax.plot(plotX, plotY, color='skyblue')
ax.plot(plotX, plotY, color='skyblue', linewidth=1)
ani = animation.FuncAnimation(fig, animate, interval=20, frames=nSamples, blit=False, save_count=nSamples)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment