本帖最后由 zoomone 于 2011-10-14 21:18 编辑
//-------------------------------------------------------
//计算PWM输出值
//-------------------------------------------------------
static float K_voltage,K_angle,K_angle_dot,K_position,K_position_dot; //放大比例参数;这里是在main()函数中对它们初始化
static float K_angle_AD,K_angle_dot_AD,K_position_AD,K_position_dot_AD;//辅助放大比例参数
static float position,position_dot;
static float position_dot_filter;
static float PWM;
//-------------------------------------------------------
void PWM_calculate(void)
{
K_angle_AD=ADC(2)*0.009;//辅助参数由变阻器ad转化得来,便于调节
K_angle_dot_AD=ADC(3)*0.009;
K_position_AD=ADC(4)*0.009;
K_position_dot_AD=ADC(5)*0.009;
position_dot=(speed_real_LH+speed_real_RH)*0.5;//position_dot是电机转速
position_dot_filter*=0.85; //车轮速度滤波
position_dot_filter+=position_dot*0.15;
position+=position_dot_filter;//position电机转过的角度,由position_dot累加而来
position+=Speed_Need;
if (position<-768) //防止位置误差过大导致的不稳定
{
position=-768;
}
else if (position>768)
{
position=768;
}
PWM = K_angle*angle *K_angle_AD + K_angle_dot*angle_dot *K_angle_dot_AD +K_position*position *K_position_AD + _position_dot*position_dot_filter *K_position_dot_AD;//PWM是由四个参数累加而来,如果只要求平衡,可以不要position
PWM_output (PWM);
}
pwm输出函数//-------------------------------------------------------
//PWM输出
//-------------------------------------------------------
/*#define MA P4_0
#define MB P4_1
#define MC P4_2
#define MD P3_7*/
unsigned int PWM_10bit; // 10位 PWM 输出缓冲区
void PWM_output (int PWM_LH)
{
if (PWM_LH<0)
{
FW; //#define FW MA=1;MB=0;MC=1;MD=0
PWM_LH*=-1;
}
else
{
BW; //#define BW MA=0;MB=1;MC=0;MD=1
}
if (PWM_LH>252)
{
PWM_LH=252;
}
PWM_10bit=4*PWM_LH; //因为PWM原为8为pwm计算得来,这里重新定义10位pwm输出,所以,乘以4
//10位pwm获得方法参考网友Cortex-M0的https://bbs.21ic.com/frame.php?frameon=yes&referer=http%3A//bbs.21ic.com/iclist-11.html
}
|