![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Space Vector PWM Ver2
Diff: main.cpp
- Revision:
- 0:fd2cb1721c58
diff -r 000000000000 -r fd2cb1721c58 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Aug 21 01:39:52 2020 +0000 @@ -0,0 +1,347 @@ +#include "mbed.h" +#include "rtos.h" +#include <math.h> +#define TS1 0.2 + +float PI=3.141592; +int t=0,q=0,i=0; +int START=10; //7 +PwmOut mypwmA(PA_8); +PwmOut mypwmB(PA_9); +PwmOut mypwmC(PA_10); + +PwmOut Current_Ref(PB_4); + +DigitalOut Vector(PC_4); +DigitalOut EN1(PC_10); +DigitalOut EN2(PC_11); +DigitalOut EN3(PC_12); + + + +//AnalogIn V_adc(PB_1); //volume +AnalogIn V_adc(PC_2); // Gaibu Potention + +AnalogIn Vshuntu(PA_1); +AnalogIn Vshuntv(PA_0); +AnalogIn Vshuntw(PB_0); + +AnalogIn BEMF1(PC_3);//C7_37 +AnalogIn BEMF2(PB_0);//C7_34 +AnalogIn BEMF3(PA_7);//C10_26 + +DigitalIn GPIO_BEMF(PC_9); +DigitalIn CPOUT(PA_12); +AnalogIn Curr_ui(PA_0); +AnalogIn Curr_vi(PC_1); +AnalogIn Curr_wi(PA_1); + +InterruptIn HA(PA_15); +InterruptIn HB(PB_3); +InterruptIn HC(PB_10); +Timer uT; +Timer vT; +Timer wT; + +AnalogOut SWAVE(PA_4); + +Serial pc(USBTX,USBRX); + +DigitalOut myled(LED1); +float sq32=sqrt(3.0f/2.0f); +float sq23=2.0f/sqrt(3.0f); +float sq3=1.0f/sqrt(3.0f); +float VDC=1.8; +float Vdlink=1/VDC; +float aVa; +float a3Vb; +float Vr_adc; + +Timer Timer1; +float freq=0; +float z=1.0; //0.5 +float Wt,Va,Vb,Vq,Vd; +float d1,d2,d3,d4,d5,d6,d07; +float du,dv,dw; +bool cu,cv,cw; +float ave=0,aveo=0,avsum=0; + +float ut=0,ut1=0,ut2=0,usi=0; +float vt=0,vt1=0,vt2=0,vsi=0; +float wt=0,wt1=0,wt2=0,wsi=0; +float Speed; + +void HAH(){ + + ut1=uT.read_us(); + ut=0; + + } + void HAL(){ + + ut2=uT.read_us(); + uT.reset(); + + } + void HBH(){ + + vt1=vT.read_us(); + + } + void HBL(){ + + vt2=vT.read_us(); + + vT.reset(); + } + void HCH(){ + wt1=wT.read_us(); + + } + + void HCL(){ + wt2=wT.read_us(); + + wT.reset(); + } + + + +void CPLT(){ + //pc.printf("%.3f , %.3f \r" ,Speed ,Vr_adc); + //pc.printf("%.3f,%.3f,%.3f \r" ,du,dv,dw); + } + + void timerTS1(void const*argument){ + CPLT(); + } +int main() { + Timer1.start(); + EN1=1; + EN2=1; + EN3=1; + uT.start(); + vT.start(); + wT.start(); + + mypwmA.period_us(20); + + mypwmB.period_us(20); + + mypwmC.period_us(20); + + pc.baud(128000); + + /* RtosTimer RtosTimerTS1(timerTS1); + RtosTimerTS1.start((unsigned int)(TS1*5000)); //3000 + Thread::wait(100); */ + + + while(1) { + + HA.rise(&HAH); + HC.fall(&HCL); + HB.rise(&HBH); + HA.fall(&HAL); + HC.rise(&HCH); + HB.fall(&HBL); + Speed=60*(1/(7.0*2.0*usi*1E-6)); + usi=ut2-ut1; + vsi=vt2-vt1; + wsi=wt2-wt1; + Vd=0; + + float s = Timer1.read(); + Vr_adc=V_adc.read(); + myled = ((int)s) % 2 == 0; /* LED output(0.5Hz) */ + + +if((Vr_adc>0.08f)&&(q==0)){ +while(q<30){ + + mypwmA.write(0.5f); + mypwmB.write(0); + mypwmC.write(0); + wait_ms(START); + + mypwmA.write(0); + mypwmB.write(0.5f); + mypwmC.write(0.0); + wait_ms(START); + + mypwmA.write(0); + mypwmB.write(0.0); + mypwmC.write(0.5f); + wait_ms(START); + q++; + + } + } + if(Vr_adc < 0.05f){ + q=0; + Vr_adc=0; + + } + + aveo=Vr_adc; + + if((aveo<=0.15)&&(aveo>0.1)){ + Wt=fmodf(s * 45, 1) * PI* 2; + Vq=0.4; + } + if((aveo<=0.2)&&(aveo>0.15)){ + Wt=fmodf(s * 50, 1) * PI* 2; + Vq=0.42; + } + if((aveo<=0.25)&&(aveo>0.2)){ + Wt=fmodf(s * 55, 1) * PI* 2; + Vq=0.45; + } + if((aveo<=0.3)&&(aveo>0.25)){ + Wt=fmodf(s * 60, 1) * PI* 2; + Vq=0.48; + } + if((aveo<=0.35)&&(aveo>0.3)){ + Wt=fmodf(s * 65, 1) * PI* 2; + Vq=0.5; + } + if((aveo<=0.4)&&(aveo>0.35)){ + Wt=fmodf(s * 70, 1) * PI* 2; + Vq=0.52; + } + if((aveo<=0.45)&&(aveo>0.4)){ + Wt=fmodf(s * 75, 1) * PI* 2; + Vq=0.55; + } + if((aveo<=0.5)&&(aveo>0.45)){ + Wt=fmodf(s * 80, 1) * PI* 2; + Vq=0.58; + } + if((aveo<=0.55)&&(aveo>0.5)){ + Wt=fmodf(s * 85, 1) * PI* 2; + Vq=0.6; + } + if((aveo<=0.6)&&(aveo>0.55)){ + Wt=fmodf(s * 90, 1) * PI* 2; + Vq=0.62; + } + if((aveo<=0.65)&&(aveo>0.6)){ + Wt=fmodf(s * 95, 1) * PI* 2; + Vq=0.65; + } + if((aveo<=0.7)&&(aveo>0.65)){ + Wt=fmodf(s * 100, 1) * PI* 2; + Vq=0.67; + } + if((aveo<=0.75)&&(aveo>0.7)){ + Wt=fmodf(s * 105, 1) * PI* 2; + Vq=0.7; + } + if((aveo<=0.8)&&(aveo>0.75)){ + Wt=fmodf(s * 110, 1) * PI* 2; + Vq=0.72; + } + if((aveo<=0.85)&&(aveo>0.8)){ + Wt=fmodf(s * 120, 1) * PI* 2; + Vq=0.75; + } + if((aveo<=0.9)&&(aveo>0.85)){ + Wt=fmodf(s * 130, 1) * PI* 2; + Vq=0.78; + } + if((aveo<=0.95)&&(aveo>0.9)){ + Wt=fmodf(s * 140, 1) * PI* 2; + Vq=0.8; + } + if(aveo>0.95){ + Wt=fmodf(s * 150, 1) * PI* 2; + Vq=0.82; + } + Vq=0.9; +if(aveo<0.1){ + mypwmA.write(0); + mypwmB.write(0); + mypwmC.write(0); + +} +else{ + Va=cos(Wt)*Vd-sin(Wt)*Vq; + Vb=sin(Wt)*Vd+cos(Wt)*Vq; + aVa=abs(Va); + a3Vb=abs(sq3*Vb); + + if((Va>=0)&&(Vb>=0)&&(aVa>=a3Vb)){ //sect 0 + + d1=sq32*(Va-sq3*Vb)*Vdlink; + d2=sq32*(sq23*Vb)*Vdlink; + d07=(z-(d1+d2))*0.5; + // d07=0; + du=d1+d2+d07; + dv=d2+d07; + dw=d07; + + } + if((aVa<=sq3*Vb)){ //sect 1 + + d3=sq32*(-Va+sq3*Vb)*Vdlink; + d2=sq32*(Va+sq3*Vb)*Vdlink; + d07=(z-(d2+d3))*0.5; + //d07=0; + du=d2+d07; + dv=d2+d3+d07; + dw=d07; + } + + if((Va<=0)&&(Vb>=0)&&(aVa>=a3Vb)){ //sect 2 + + d3=sq32*sq23*Vb*Vdlink; + d4=sq32*(-Va-sq3*Vb)*Vdlink; + d07=(z-(d3+d4))*0.5; + // d07=0; + du=d07; + dv=d3+d4+d07; + dw=d4+d07; + } + + if((Va<=0)&&(Vb<=0)&&(aVa>=a3Vb)){ //sect 3 + + d5=-sq32*sq23*Vb*Vdlink; + d4=sq32*(-Va+sq3*Vb)*Vdlink; + d07=(z-(d4+d5))*0.5; + //d07=0; + du=d07; + dv=d4+d07; + dw=d4+d5+d07; + } + + if((aVa<=-sq3*Vb)){ //sect 4 + + d5=sq32*(-Va-sq3*Vb)*Vdlink;; + d6=sq32*(Va-sq3*Vb)*Vdlink; + d07=(z-(d5+d6))*0.5; + //d07=0; + du=d6+d07; + dv=d07; + dw=d5+d6+d07; + } + + if((Va>=0)&&(Vb<=0)&&(aVa>=a3Vb)){ //sect 5 + + d1=sq32*(Va+sq3*Vb)*Vdlink;; + d6=-sq32*sq23*Vb*Vdlink;; + d07=(z-(d1+d6))*0.5; + //d07=0; + du=d1+d6+d07; + dv=d07; + dw=d6+d07; + } + + mypwmA.write(du); + mypwmB.write(dv); + mypwmC.write(dw); + } + + SWAVE=dw; + //pc.printf("%.3f,%.3f,%.3f \r" ,du,dv,dw); + } + }