中级会员
- 积分
- 240
- 金钱
- 240
- 注册时间
- 2018-4-21
- 在线时间
- 105 小时
|
楼主 |
发表于 2018-5-17 19:12:56
|
显示全部楼层
串级PID
#include "Maths.h"
#include "Control.h"
#include "struct_all.h"
float thr=0;
float Pitch_i,Roll_i,Yaw_i;
float Pitch_old,Roll_old,Yaw_old;
float Pitch_d,Roll_d,Yaw_d;
float RC_Pitch=0,RC_Roll=0,RC_Yaw=0;
float Pitch_shell_out,Roll_shell_out,Yaw_shell_out;
float Pitch_shell_kp=140;
float Pitch_shell_ki=0.2;
float Pitch_shell_kd=0.8;
float Roll_shell_kp=150;
float Roll_shell_ki=0.2;
float Roll_shell_kd=0.8;
float Yaw_shell_kp=15;//0.87
float Yaw_shell_ki=0.002;
float Yaw_shell_kd=0.02;
float Gyro_radian_old_x,Gyro_radian_old_y,Gyro_radian_old_z;
float pitch_core_kp_out,pitch_core_ki_out,pitch_core_kd_out,Roll_core_kp_out,\
Roll_core_ki_out,Roll_core_kd_out,Roll_core_ki_out,Yaw_core_kp_out,\
Yaw_core_ki_out,Yaw_core_kd_out;
float Pitch_core_out,Roll_core_out,Yaw_core_out;
float Pitch_core_kp=0.02;
float Pitch_core_ki=0;
float Pitch_core_kd=0.001;//0.001
float Roll_core_kp=0.02;
float Roll_core_ki=0;
float Roll_core_kd=0.001;
float Yaw_core_kp=0.001;
float Yaw_core_ki=0;
float Yaw_core_kd=0.001;
int16_t moto1=1613,moto2=1613,moto3=1613,moto4=1613;
void CONTROL_PID(struct _out_angle *angle)
{
RC_Pitch=(300000 -1499.0f )/50;
RC_Pitch-=3.3f;
Pitch_i+=(angle->pitch-RC_Pitch);
if(Pitch_i>300) Pitch_i=300;
else if(Pitch_i<-300) Pitch_i=-300;
Pitch_d=angle->pitch-Pitch_old;
Pitch_shell_out = Pitch_shell_kp*(angle->pitch-RC_Pitch) + Pitch_shell_ki*Pitch_i + Pitch_shell_kd*Pitch_d;
Pitch_old=angle->pitch;
RC_Roll=(300000-1502)/50;
Roll_i+=(angle->roll-RC_Roll);
if(Roll_i>300) Roll_i=300;
else if(Roll_i<-300) Roll_i=-300;
Roll_d=angle->roll-Roll_old;
Roll_shell_out = Roll_shell_kp*(angle->roll-RC_Roll) + Roll_shell_ki*Roll_i + Roll_shell_kd*Roll_d;
Roll_old=angle->roll;
Yaw_d=SI_gyro.z-Yaw_old;
Yaw_shell_out = Yaw_shell_kp*(SI_gyro.z-RC_Yaw) + Yaw_shell_ki*Yaw_i + Yaw_shell_kd*Yaw_d;
Yaw_old=SI_gyro.z;
pitch_core_kp_out = Pitch_core_kp * (Pitch_shell_out + SI_gyro.y );
pitch_core_kd_out = Pitch_core_kd * (SI_gyro.y - Gyro_radian_old_y);
Roll_core_kp_out = Roll_core_kp * (Roll_shell_out + SI_gyro.x );
Roll_core_kd_out = Roll_core_kd * (SI_gyro.x - Gyro_radian_old_x);
Yaw_core_kp_out = Yaw_core_kp * (Yaw_shell_out + SI_gyro.z);
Yaw_core_kd_out = Yaw_core_kd * (SI_gyro.z - Gyro_radian_old_z);
Pitch_core_out = pitch_core_kp_out + pitch_core_kd_out;
Roll_core_out = Roll_core_kp_out + Roll_core_kd_out;
Yaw_core_out = Yaw_core_kp_out + Yaw_core_kd_out;
Gyro_radian_old_y = SI_gyro.y;
Gyro_radian_old_x = SI_gyro.x;
Gyro_radian_old_z = SI_gyro.z;
thr=1300;
moto1=(int16_t)(thr - Roll_core_out - Pitch_core_out- Yaw_core_out);
moto2=(int16_t)(thr + Roll_core_out - Pitch_core_out+ Yaw_core_out);
moto3=(int16_t)(thr + Roll_core_out + Pitch_core_out- Yaw_core_out);
moto4=(int16_t)(thr - Roll_core_out + Pitch_core_out+ Yaw_core_out);
Motor_Out(moto1,moto2,moto3,moto4);
}
|
|