金牌会员
- 积分
- 1700
- 威望
- 950
- 贡献
- 394
- 兑换币
- 60
- 注册时间
- 2012-2-5
- 在线时间
- 178 小时
- 毕业学校
- 重庆大学
|
用PE的看到你们这样写的程序就真心难受!/*------------------------
功能说明:电机舵机PID模块
-----------------------*/
#include"PID.h"
#include "Cpu.h"
#include "Events.h"
#include "KONGZHIMOKUAI.h"
#define KP 0.5 //0.6
#define KI 0.06 //0.08
#define KD 0.04 //0.02
word idea_speed=500; //单位为cm/s
word real_speed=0; //单位为cm/s
int uk=0; //u[k]
static int number=0;
int ek=0; //偏差e[k]
static int ek1=0; //偏差e[k-1]
static int ek2=0; //偏差e[k-2]
static int uk1=0; //对uk四舍五入
static int deltauk;
char flag1;
/*----------------------------------
测速模块初始化:cesuchushihua()
----------------------------------*/
void cesuchushihua(void)
{
setReg(TMRA2_CTRL,0);
setReg(TMRA2_SCR,0x00);
setReg(TMRA2_CNTR,0);
setReg(TMRA2_LOAD,0);
setRegBitGroup(TMRA2_CTRL,CM,0x01);
}
/*----------------------------------
测速模块:cesumokuai()
----------------------------------*/
void cesumokuai(void)
{
number=getReg(TMRA2_CNTR);
setReg(TMRA2_CNTR,0);
real_speed=(word)(0.94737*number);
}
/*----------------------------------
PID模块:dianjiPID()
----------------------------------*/
void dianjiPID(void)
{
deltauk=KP*(ek-ek1)+KI*(ek)+KD*(ek-2*ek1+ek2);
ek2=ek1;
ek1=ek;
uk=uk1+deltauk;
if(uk<-99)
uk=-99;
if(uk>99)
uk=99;
uk1=uk;
if(sc_flag==0)
{
if(uk<0)
{
uk=99+uk;
DIANJI_DIRECTION_IO1_SetVal();
DIANJI_PWM_SetDutyUS(uk);
}
else
{
DIANJI_DIRECTION_IO1_ClrVal();
DIANJI_PWM_SetDutyUS(uk);
}
}
else
{
sc_flag=sc_flag-1;
DIANJI_DIRECTION_IO1_SetVal();
DIANJI_PWM_SetDutyUS(60);
}
if(uk1<-95)
flag1++;
else
flag1=0;
if(flag1>4)
{
DIANJI_ENABLE_IO2_ClrVal();
}
} |
|