PID應(yīng)用于電機(jī)閉環(huán)調(diào)速系統(tǒng)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#include
#include
#include
#include"lcdbn.h"
uchardata1[4]={0,2,5,0};
uchardata2[4]={0,0,0,0};
ucharj,key=0;
ucharchr[]="pre:*2%";
uchar*chr1[3]={"opencV:",
"shutcV:",
"acceler:"};
ucharcounter1=0x84;
ucharcounter2=0xc8;
ucharflag;//flag為開閉使能
intE1r;
intE2r=0,E3r=0;
uintspeedr=15,c_r=0;//maxc=700;
voidinit_intr(void)
{
SREG=0x80;
TIMSK=0x40;
TCCR2=0x06;
TCCR1A=0xe3;//A比較高電平,B比較低電平
TCCR1B=0x0a;
MCUCR=0x0a;///外部INT1/0中斷上升有效
GICR=0xc0;
}
////////////////////////////////////
/////左右電機(jī)pwm設(shè)置
///////////////////////////////////
voidsetpwmr(uchar*p)
{uinttemp1;
temp1=(uint)p[1]*10+p[2];
if(temp1<25)
{
speedr=(uint)28-temp1*28/25;
}
else
{
speedr=(uint)temp1*28/25-28;
}
temp1=1023*temp1/50;
OCR1A=temp1;
OCR1B=temp1;
}
/////轉(zhuǎn)速加/減調(diào)速//////
voidadd(uchar*p)
{
if(p[1]!=5)
{
p[2]++;
if(p[2]==10)
{
p[2]=0;p[1]++;
}
}
}
voidsub(uchar*p)
{
uchartemp;
temp=p[1]*20+p[2]*2;
if(temp!=0)
temp=temp-2;
p[1]=temp/20;//設(shè)定值還原//
p[2]=(temp-p[1]*20)/2;
}
voiddisplay()
{write_string(0x80,chr);
write_string(0xc0,chr1[key]);
write_order(counter1+0);
write_Data(data1[0]+0x30);
for(j=1;j<3;j++)
{write_order(counter1+j);
write_Data(data1[j]+0x30);
}
for(j=0;j<4;j++)
{
write_order(counter2+j);
write_Data(data2[j]+0x30);
}
}
//////速度調(diào)節(jié)////
voidspeed_change(void)
{
if(PINA.1==0)
{
delay_ms(100);
if(PINA.1==0)
{add(data1);}
}
if(PINA.2==0)
{delay_ms(100);
if(PINA.2==0)
{sub(data1);}
}
}
/////鍵盤讀取////
voidtestkey(void)
{
ucharflag2=0;
if(PINA!=0x0f)
{delay_ms(10);
if(PINA!=0x0f)
{flag2=1;}
}
if(flag2)
{
while(PINA.3!=0)
{
while(PINA.0==0)
{delay_ms(100);
if(PINA.0==0)
{
if(key<2)
{key++;}
else
key=0;}
}
switch(key)
{case0://模式1PID調(diào)節(jié)
flag=0;
speed_change();
setpwmr(data1);
display();
break;
case1://模式2開環(huán)
flag=1;
speed_change();
setpwmr(data1);
init_intr();
display();
break;
case2:
display();
break;
default:break;}
}
flag2=0;
}
}
/*voidspeed_convert(uintc)//測速轉(zhuǎn)化成pwm值
{
}*/
voidnew_pid(uintcl,uchark)//積分分離法
{inttemp3,z,p;//p為積分系數(shù)誤差越大P越小
if(flag)
{E1r=speedr-cl;
if(speedr>0&abs(E1r)<11)
{
switch(abs(E1r)/2)
{
case5:p=1;
break;
case4:p=3;
break;
case3:p=5;
break;
case2:p=8;
break;
case1:p=10;
break;
default:
break;
}
z=23*(E1r-E2r)+p*(E1r-2*E2r+E3r)-10*E3r;
z=(int)z*102/k;
temp3=OCR1A;
temp3=temp3+z;
OCR1A=temp3;
OCR1B=temp3;
E3r=E2r;
E2r=E1r;
}
}
}
/*voidpid_motr(uintcr,uchark)
{
inttemp,u;
if(flag)
{
if(speedr>0)
{
E1r=speedr-cr;
if(E1r==speedr)//E誤差很大時(shí)最大額定電流加速
{OCR1A=maxc;
OCR1B=maxc;}
elseif(abs(E1r)>0&&abs(E1r)<7)
{
u=(int)(24*E1r-35*E2r+14*E3r)*102/k;
temp=OCR1A;
temp=temp+u;
OCR1A=temp;
OCR1B=temp;
E3r=E2r;
E2r=E1r;
}
}
}
}*/
////////速度數(shù)據(jù)分解///////
voidspeed_count(uintr)
{
data2[3]=r%10;
r=r/10;
data2[2]=r%10;
r=r/10;
data2[1]=r%10;
r=r/10;
data2[0]=r%10;
}
/////采用外部中斷測速//////////
interrupt[EXT_INT0]voidint_0(void)
{c_r++;
}
/////T2定時(shí)80ms對速度采樣////////
interrupt[TIM2_OVF]voidtime_2(void)
{
MCUCR=0x00;
GICR=0x00;
new_pid(c_r,500);
//pid_motr(c_r,100);
speed_count(c_r);
MCUCR=0x0f;///外部INT1/0中斷上升有效
GICR=0xc0;
c_r=0;
}
voidinitall_IO(void)
{
DDRB=0xf0;
PORTB=0x00;
DDRA=0xF0;
PORTA=0x0F;
DDRC=0XFF;
PORTC=0X00;
DDRD=0xF0;
PORTD=0x0F;
}
voidmain(void)
{
initall_IO();
init_lcd();
while(1)
{
DDRA=0xF0;
PORTA=0x0F;
testkey();
init_intr();
display();
}
}
PID算法公式有點(diǎn)錯(cuò):應(yīng)改為
z=100*(E1r-E2r)+p*E1r+125*(E1r-2*E2r+E3r);
經(jīng)過老師的指導(dǎo):速度采樣用M法不適應(yīng)低,所以對于調(diào)速范圍大的就用M/T法,適合于高低測速!對測速還要加以數(shù)字濾波!可用一階慣性/中值法,平均法等!