第三个部分用来记录FOC控制中的clark、park、反clark、反park变换;
本来这个板块只写SVPWM部分的,但是写了坐标系变化的代码,就一起记录下来;
具体的理论分析不做过多的介绍;
python的代码如下:
'''
author : sunyan
'''
from matplotlib import pyplot as plt
import numpy as np
import math
class SVPWM_sim:
def __init__(self):
plt.figure(figsize = (10, 10))
self.fre_to_time = 5
def three_phase_to_two(self):
for thate in range(0, 361, self.fre_to_time):
plt.cla() # 清除之前画的图
ua = math.cos(math.radians(thate))
ub = math.cos(math.radians(thate - 120))
uc = math.cos(math.radians(thate + 120))
xa0 = 0
ya0 = 0
xa1 = ua
ya1 = 0
xb0 = xa1
yb0 = ya1
xb1 = xb0 + ub * math.cos(math.radians(120))
yb1 = yb0 + ub * math.sin(math.radians(120))
xc0 = xb1
yc0 = yb1
xc1 = xc0 + uc * math.cos(math.radians(240))
yc1 = yc0 + uc * math.sin(math.radians(240))
xu0 = 0
yu0 = 0
xu1 = (3 / 2) * math.cos(math.radians(thate))
yu1 = (3 / 2) * math.sin(math.radians(thate))
plt.annotate('', xy = (xa1, ya1), xytext = (xa0, ya0), \
arrowprops = dict(color = 'blue', width = 1))
plt.annotate('', xy = (xb1, yb1), xytext = (xb0, yb0), \
arrowprops = dict(color = 'red', width = 1))
plt.annotate('', xy = (xc1, yc1), xytext = (xc0, yc0), \
arrowprops = dict(color = 'yellow', width = 1))
plt.annotate('', xy = (xu1, yu1), xytext = (xu0, yu0), \
arrowprops = dict(color = 'black', width = 1))
plt.xlim(-2, 2) # 设置x轴范围
plt.ylim(-2, 2) # 设置y轴范围
plt.grid(True)
plt.pause(0.01 * self.fre_to_time) # 暂停1/100秒
plt.show()
def three_phase_to_two(self, udc = 100, km = 1, thate = 0):
print('thate is : ' + str(thate))
Ts = 2 * math.pi * 1 / 10000
K = math.sqrt(3) * Ts / udc
uarfa = km * (2 / 3) * udc * math.cos(math.radians(thate))
ubata = km * (2 / 3) * udc * math.sin(math.radians(thate))
u1 = ubata
u2 = (math.sqrt(3) / 2 * uarfa) - ubata / 2
u3 = (-math.sqrt(3) / 2 * uarfa) - ubata / 2
if u1 > 0:
A = 1
else:
A = 0
if u2 > 0:
B = 1
else:
B = 0
if u3 > 0:
C = 1
else:
C = 0
N = 4 * C + 2 * B + A
if N == 3:
time4 = K * u2
time6 = K * u1
time7 = Ts - time4 - time6
if (time4 + time6) > Ts:
time4 = time4 / (time4 + time6) * Ts
time6 = time6 / (time4 + time6) * Ts
time7 = 0
T4 = time4 / Ts * 100
T6 = time6 / Ts * 100
T7 = time7 / 2 / Ts * 100
print('扇区1')
print('T4 的占空比是:' + str(T4) + '%')
print('T6 的占空比是:' + str(T6) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
elif N == 1:
time6 = -K * u3
time2 = -K * u2
time7 = Ts - time6 - time2
if (time6 + time2) > Ts:
time6 = time6 / (time6 + time2) * Ts
time2 = time2 / (time6 + time2) * Ts
time7 = 0
T6 = time6 / Ts * 100
T2 = time2 / Ts * 100
T7 = time7 / 2 / Ts * 100
print('扇区2')
print('T6 的占空比是:' + str(T6) + '%')
print('T2 的占空比是:' + str(T2) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
elif N == 5:
time2 = K * u1
time3 = K * u3
time7 = Ts - time2 - time3
if (time2 + time3) > Ts:
time2 = time2 / (time2 + time3) * Ts
time3 = time3 / (time2 + time3) * Ts
time7 = 0
T2 = time2 / Ts * 100
T3 = time3 / Ts * 100
T7 = time7 / 2 / Ts * 100
print('扇区3')
print('T2 的占空比是:' + str(T2) + '%')
print('T3 的占空比是:' + str(T3) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
elif N == 4:
time3 = -K * u2
time1 = -K * u1
time7 = Ts - time3 - time1
if (time3 + time1) > Ts:
time3 = time3 / (time3 + time1) * Ts
time1 = time1 / (time3 + time1) * Ts
time7 = 0
T3 = time3 / Ts * 100
T1 = time1 / Ts * 100
T7 = time7 / 2 / Ts * 100
print('扇区4')
print('T3 的占空比是:' + str(T3) + '%')
print('T1 的占空比是:' + str(T1) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
elif N == 6:
time1 = K * u3
time5 = K * u2
time7 = Ts - time1 - time5
if (time1 + time5) > Ts:
time1 = time1 / (time1 + time5) * Ts
time5 = time5 / (time1 + time5) * Ts
time7 = 0
T1 = time1 / Ts * 100
T5 = time5 / Ts * 100
T7 = time7 / 2 / Ts *100
print('扇区5')
print('T2 的占空比是:' + str(T1) + '%')
print('T3 的占空比是:' + str(T5) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
elif N == 2:
time5 = -K * u1
time4 = -K * u3
time7 = Ts - time5 - time4
if (time5 + time4) > Ts:
time5 = time5 / (time5 + time4) * Ts
time4 = time4 / (time5 + time4) * Ts
time7 = 0
T5 = time5 / Ts * 100
T4 = time4 / Ts * 100
T7 = time7 / 2 / Ts * 100
print('扇区6')
print('T5 的占空比是:' + str(T5) + '%')
print('T4 的占空比是:' + str(T4) + '%')
print('T7 的占空比是:' + str(T7) + '%')
print('T0 的占空比是:' + str(T7) + '%')
def clark_3s_to_2s(self, ia, ib, ic):
iarfa = ia
ibate = (1 / math.sqrt(3)) * ia + (2 / math.sqrt(3)) * ib
return iarfa, ibate
def park_2s_to_2r(self, iarfa, ibate, thate):
i_2r_d = iarfa * math.cos(thate) + ibate * math.sin(thate)
i_2r_q = -iarfa * math.sin(thate) + ibate * math.cos(thate)
return i_2r_d, i_2r_q
def park_2r_to_2s(self, i_2r_d, i_2r_q, thate):
iarfa = i_2r_d * math.cos(thate) - i_2r_q * math.sin(thate)
ibate = i_2r_d * math.sin(thate) + i_2r_q * math.cos(thate)
return iarfa, ibate
def clark_2s_to_3s(self, iarfa, ibate):
ia = iarfa
ib = -(1 / 2) * iarfa + (math.sqrt(3) / 2) * ibate
ic = -(1 / 2) * iarfa - (math.sqrt(3) / 2) * ibate
return ia, ib, ic
由于这部分调试代码比较多, 也就一并记录一下调试代码;
调试代码如下:
demo = SVPWM_sim()
te1 = 0
te2 = 0
te_d = 0
te_q = 0
te_arf = 0
te_bat = 0
te_ia = 0
te_ib = 0
te_ic = 0
time = []
re_ia = []
re_ib = []
re_ic = []
re_arf = []
re_bat = []
re_d = []
re_q = []
an_re_arfa = []
an_re_bate = []
an_re_ua = []
an_re_ub = []
an_re_uc = []
k = 2 * math.pi * 0.01 #此处应该是一条斜率为(2*pi*f)的斜坡信号,f是与正变换的3s信号的频率相同;
for index in range(0, 201, 1):
time.append(index)
ia = math.cos(2 * math.pi * 0.01 * index)
ib = math.cos(2 * math.pi * 0.01 * index - 120 / 180 * math.pi)
ic = math.cos(2 * math.pi * 0.01 * index + 120 / 180 * math.pi)
re_ia.append(ia)
re_ib.append(ib)
re_ic.append(ic)
te1 = demo.clark_3s_to_2s(ia, ib, ic)[0]
te2 = demo.clark_3s_to_2s(ia, ib, ic)[1]
re_arf.append(te1)
re_bat.append(te2)
te_d = demo.park_2s_to_2r(te1, te2, (k * index))[0]
te_q = demo.park_2s_to_2r(te1, te2, (k * index))[1]
re_d.append(te_d)
re_q.append(te_q)
te_arf = demo.park_2r_to_2s(te_d, te_q, (k * index))[0]
te_bat = demo.park_2r_to_2s(te_d, te_q, (k * index))[1]
an_re_arfa.append(te_arf)
an_re_bate.append(te_bat)
an_re_ua.append(demo.clark_2s_to_3s(te_arf, te_bat)[0])
an_re_ub.append(demo.clark_2s_to_3s(te_arf, te_bat)[1])
an_re_uc.append(demo.clark_2s_to_3s(te_arf, te_bat)[2])
plt.xlim(0, 200)
plt.ylim(-1.1, 1.1)
plt.grid(True)
plt.plot(time, re_ia)
plt.plot(time, re_ib)
plt.plot(time, re_ic)
plt.plot(time, re_arf)
plt.plot(time, re_bat)
plt.plot(time, re_d)
plt.plot(time, re_q)
plt.plot(time, an_re_arfa)
plt.plot(time, an_re_bate)
plt.plot(time, an_re_ua)
plt.plot(time, an_re_ub)
plt.plot(time, an_re_uc)
plt.show()
最后附一张绘制的图像: