基于AVR ATMega16 的PID 控制算法程序
詳細(xì)的代碼和仿真情況如下:(我的硬件系統(tǒng)是本站的min Mega16/32 + JTAG ICE)
- #include "config.h"
- struct _PID
- {
- float PVn; //反饋信號(hào)變量
- float SPn; //設(shè)定值
- float Mn; //PID運(yùn)算結(jié)果
- float Kc; //比例系數(shù)
- float Ts; //采樣時(shí)間(ms)
- float Ti; //積分時(shí)間(ms)
- float Td; //微分時(shí)間(ms)
- float Mx; //積分項(xiàng)調(diào)整參數(shù)
- float PVn_1;//前一次反饋?zhàn)兞?/li>
- float MPn; //比例項(xiàng)的結(jié)果值
- float MIn; //積分項(xiàng)的結(jié)果值
- float MDn; //微分項(xiàng)的結(jié)果值
- };
- struct _PID *myPID;
- void init_myPID(void);
- void init_ports(void);
- void init_device(void);
- float MPn_value(struct _PID *PID);
- float MIn_value(struct _PID *PID);
- float MDn_value(struct _PID *PID);
- float Mx_value(struct _PID *PID);
- float Mn_value(struct _PID *PID);
- void main (void)
- {
- init_device();
- init_myPID();
- myPID->SPn = 155.5;
- myPID->Kc = 13.2;
- myPID->Ts = 0.2;
- myPID->Ti = 600.0;
- myPID->Td = 0.0;
- myPID->PVn = 108.2;
- while(1)
- {
- myPID->MPn = MPn_value(myPID);
- myPID->MDn = MDn_value(myPID);
- myPID->Mx = Mx_value(myPID);
- myPID->MIn = MIn_value(myPID);
- myPID->Mn = Mn_value(myPID);
- myPID->PVn_1 = myPID->PVn;
- }
- }
- /******************************************************************************/
- void init_myPID(void)
- {
- myPID->PVn = 0.0;
- myPID->SPn = 0.0;
- myPID->Mn = 0.0;
- myPID->Kc = 0.0;
- myPID->Ts = 0.0;
- myPID->Ti = 0.0;
- myPID->Td = 0.0;
- myPID->Mx = 0.0;
- myPID->PVn_1 = 0.0;
- myPID->MPn = 0.0;
- myPID->MIn = 0.0;
- myPID->MDn = 0.0;
- }
- //------------------------------------------------------------------------------
- void init_ports(void)
- {
- PORTA = 0x00; //If ADC Function was be used,the PORTA could`t set bit 1
- DDRA = 0x00; //the port set input mode.
- PORTB = 0x00;
- DDRB = 0x00;
- PORTC = 0x00; //m103 output on
ly - DDRC = 0x00;
- PORTD = 0x00;
- DDRD = 0x00;
- }
- //------------------------------------------------------------------------------
- void init_device(void)
- {
- CLI();
- init_ports();
- MCUCR = 0x00; //Set Power control(State:Close)
- GICR = 0x00; //Set boot guide(State:Close).
- SEI(); //re-enable interrupts
- //all peripherals are now initialized
- }
- // 計(jì)算 比例項(xiàng)的值
- //------------------------------------------------------------------------------
- float MPn_value(struct _PID *PID)
- {
- float myMPn = 0.0;
- myMPn = PID->Kc *( PID->SPn - PID->PVn);
- return myMPn;
- }
- //計(jì)算積分項(xiàng)的值
- //------------------------------------------------------------------------------
- float MIn_value(struct _PID *PID)
- {
- float myMIn = 0.0;
- myMIn = PID->Kc*(PID->Ts/PID->Ti)*(PID->SPn - PID->PVn) + PID->Mx;
- return myMIn;
- }
- //計(jì)算微分項(xiàng)的值
- //------------------------------------------------------------------------------
- float MDn_value(struct _PID *PID)
- {
- float myMDn = 0.0;
- myMDn = PID->Kc * (PID->Td/PID->Ts) * (PID->PVn_1 - PID->PVn);
- return myMDn;
- }
- //計(jì)算PID的結(jié)果
- //------------------------------------------------------------------------------
- float Mn_value(struct _PID *PID)
- {
- float myMn = 0.0;
- myMn = PID->MPn + PID->MIn + PID->MDn;
- return myMn;
- }
- //計(jì)算積分項(xiàng)的調(diào)整值
- //------------------------------------------------------------------------------
- float Mx_value(struct _PID *PID)
- {
- float myMx = 0.0;
- if(PID->Mn > 1.0)
- {
- myMx = 1.0 - (PID->MPn + PID->MDn);
- }
- else if(PID->Mn < 0.0)
- {
- myMx = -(PID->MPn + PID->MDn);
- }
- return myMx;
- }
運(yùn)行到PID初始化函數(shù):void init_myPID(void)時(shí)的仿真結(jié)果如下:無法全部初始化為0
運(yùn)行到:
myPID->SPn = 155.5;
myPID->Kc = 13.2;
myPID->Ts = 0.2;
myPID->Ti = 600.0;
myPID->Td = 0.0;
myPID->PVn = 108.2;
重新賦值后,部分參數(shù)Ts并不為0.2
評(píng)論