中级会员
- 积分
- 240
- 金钱
- 240
- 注册时间
- 2018-4-21
- 在线时间
- 105 小时
|
楼主 |
发表于 2018-8-3 14:27:44
|
显示全部楼层
float PID_Postion_Cal(float target,float measure,float p,float i,float d)
{
float out;
error=measure-target;
Integ+=error;
if(Integ>400)
Integ=400;
if(Integ<=-400)
Integ=-400;
Deriv=error-last_error;
out=p*error+i*Integ+d*Deriv;
last_error=error;
return out;
}
void PID(struct _out_angle *angle,struct _gyro *gyro_filter)
{
Pitch_shell_out=PID_Postion_Cal(0,angle->pitch,Pitch_shell_kp,Pitch_shell_ki,Pitch_shell_kd);
Roll_shell_out=PID_Postion_Cal(0,angle->roll,Roll_shell_kp,Roll_shell_ki,Pitch_shell_kd);
Yaw_shell_out=PID_Postion_Cal(0,angle->yaw,Yaw_shell_kp,Roll_shell_ki,Roll_shell_kd);
Pitch=PID_Postion_Cal(Pitch_shell_out,gyro_filter->y,Pitch_core_kp,Pitch_core_ki,Pitch_core_kd);
Roll=PID_Postion_Cal(Roll_shell_out,gyro_filter->x,Roll_core_kp,Roll_core_ki,Roll_core_kd);
Yaw=PID_Postion_Cal(Yaw_shell_out,gyro_filter->z,Yaw_core_kp,Yaw_core_ki,Yaw_core_kd);
thro1=(u16)(temp+Pitch+Roll-Yaw);
thro2=(u16)(temp-Pitch+Roll+Yaw);
thro3=(u16)(temp-Pitch-Roll-Yaw);
thro4=(u16)(temp+Pitch-Roll+Yaw);
if(thro1>1500)thro1=0;
if(thro2>1500)thro2=0;
if(thro3>1500)thro3=0;
if(thro4>1500)thro4=0;
Motor_Out(thro1,thro2,thro3,thro4);
Send_MotoPWM(thro1-1000,thro2-1000,thro3-1000,thro4-1000,Pitch_shell_out+200,Pitch+500,Yaw,0);
}
贴下我的PID程序 |
|