一直想搞一下四軸飛行器,,不過總是么有時(shí)間,,前段時(shí)間在家休息,好好研究一下四軸飛行器的PID算法?,F(xiàn)在湊個(gè)熱鬧,,分享傳說中的串級(jí)PID,內(nèi)環(huán)為角速度環(huán),,外環(huán)為角速度,。整定PID參數(shù)的時(shí)候先調(diào)外環(huán),再調(diào)內(nèi)環(huán),。不多說,,先上文件:chuanPID.zip(1.75 KB) (下載次數(shù): 1533, 2015-6-5 10:50 上傳)下載積分: 積分 -1 分 想直接看的可以看看這個(gè)代碼,都是一樣的- #include 'CONTROL.h'
- #include 'IMU1.h'
- #include 'moto.h'
- #include 'RFdate.h'
- #include
- extern T_RC_Data Rc_D; //遙控通道數(shù)據(jù);
- extern u8 txbuf[4]; //發(fā)送緩沖
- extern u8 rxbuf[4]; //接收緩沖
- extern u16 test1[3]; //接收到NRf24L01數(shù)據(jù)
- extern S_INT16_XYZ ACC_F,GYRO_F;
- PID PID_ROL,PID_PIT,PID_YAW;
- extern S_INT16_XYZ MPU6050_ACC_LAST,MPU6050_GYRO_LAST;
- int Motor_Ele=0; //俯仰期望
- int Motor_Ail=0; //橫滾期望
- //u8 ARMED = 0;
- //float rol_i=0,pit_i=0,yaw_p=0;
- float thr=0;
- S_FLOAT_XYZ EXP_ANGLE ,DIF_ANGLE;
- PID1 PID_Motor;
- /*********************************/
- float Pitch_i,Roll_i,Yaw_i; //積分項(xiàng)
- float Pitch_old,Roll_old,Yaw_old; //角度保存
- float Pitch_d,Roll_d,Yaw_d; //微分項(xiàng)
- float RC_Pitch,RC_Roll,RC_Yaw; //姿態(tài)角
- float Pitch_shell_out,Roll_shell_out,Yaw_shell_out;//外環(huán)總輸出
- //外環(huán)PID參數(shù)
- float Pitch_shell_kp=280;//30 140
- float Pitch_shell_kd=0;//
- float Pitch_shell_ki=0;//
- /*********************************/
- float Roll_shell_kp=250;//30
- float Roll_shell_kd=0;//10
- float Roll_shell_ki=0;//0.08
- /*********************************/
- float Yaw_shell_kp=1.5;//10;//30
- float Yaw_shell_kd=0;//10
- float Yaw_shell_ki=0;//0.08;//0.08
- float Gyro_radian_old_x,Gyro_radian_old_y,Gyro_radian_old_z;//陀螺儀保存
- float pitch_core_kp_out,pitch_core_kd_out,Roll_core_kp_out,Roll_core_kd_out,Yaw_core_kp_out,Yaw_core_kd_out;//內(nèi)環(huán)單項(xiàng)輸出
- float Pitch_core_out,Roll_core_out,Yaw_core_out;//內(nèi)環(huán)總輸出
-
- //內(nèi)環(huán)PID參數(shù)
- //float Pitch_core_kp=0.040;
- //float Pitch_core_kd=0.008;////0.007;//0.07;
- float Pitch_core_kp=0.040;
- float Pitch_core_kd=0.002;////0.007;//0.07;
- float Roll_core_kp=0.040;//;
- float Roll_core_kd=0.002;////0.007;//06;//0.07;
- float Yaw_core_kp=0.046;//;
- float Yaw_core_kd=0.012;////0.007;//06;//0.07;
- int16_t moto1=0,moto2=0,moto3=0,moto4=0;
- float tempjd=0;
- void CONTROL(float rol, float pit, float yaw)
- {
-
- RC_Pitch=(Rc_D.PITCH-1500)/20;
-
- ////////////////////////外環(huán)角度環(huán)(PID)///////////////////////////////
- Pitch_i+=(Q_ANGLE.Pitch-RC_Pitch);
- //-------------Pitch積分限幅----------------//
- if(Pitch_i>300) Pitch_i=300;
- else if(Pitch_i<-300) pitch_i="">
- //-------------Pitch微分--------------------//
- Pitch_d=Q_ANGLE.Pitch-Pitch_old;
- //-------------Pitch PID-------------------//
- Pitch_shell_out = Pitch_shell_kp*(Q_ANGLE.Pitch-RC_Pitch) + Pitch_shell_ki*Pitch_i + Pitch_shell_kd*Pitch_d;
- //角度保存
- Pitch_old=Q_ANGLE.Pitch;
- /*********************************************************/
-
- RC_Roll=(Rc_D.ROLL-1500)/20;
- Roll_i+=(Q_ANGLE.Rool-RC_Roll);
- //-------------Roll積分限幅----------------//
- if(Roll_i>300) Roll_i=300;
- else if(Roll_i<-300) roll_i="">
- //-------------Roll微分--------------------//
- Roll_d=Q_ANGLE.Rool-Roll_old;
- //-------------Roll PID-------------------//
- Roll_shell_out = Roll_shell_kp*(Q_ANGLE.Rool-RC_Roll) + Roll_shell_ki*Roll_i + Roll_shell_kd*Roll_d;
- //------------Roll角度保存------------------//
- Roll_old=Q_ANGLE.Rool;
-
-
- RC_Yaw=(Rc_D.YAW-1500)*10;
- //-------------Yaw微分--------------------//
- Yaw_d=MPU6050_GYRO_LAST.Z-Yaw_old;
- //-------------Roll PID-------------------//
- Yaw_shell_out = Yaw_shell_kp*(MPU6050_GYRO_LAST.Z-RC_Yaw) + Yaw_shell_ki*Yaw_i + Yaw_shell_kd*Yaw_d;
- //------------Roll角度保存------------------//
- Yaw_old=MPU6050_GYRO_LAST.Z;
-
-
- ////////////////////////內(nèi)環(huán)角速度環(huán)(PD)///////////////////////////////
- pitch_core_kp_out = Pitch_core_kp * (Pitch_shell_out + MPU6050_GYRO_LAST.Y * 3.5);
- pitch_core_kd_out = Pitch_core_kd * (MPU6050_GYRO_LAST.Y - Gyro_radian_old_y);
- Roll_core_kp_out = Roll_core_kp * (Roll_shell_out + MPU6050_GYRO_LAST.X *3.5);
- Roll_core_kd_out = Roll_core_kd * (MPU6050_GYRO_LAST.X - Gyro_radian_old_x);
- Yaw_core_kp_out = Yaw_core_kp * (Yaw_shell_out + MPU6050_GYRO_LAST.Z * 1);
- Yaw_core_kd_out = Yaw_core_kd * (MPU6050_GYRO_LAST.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 = MPU6050_GYRO_LAST.X;
- Gyro_radian_old_x = MPU6050_GYRO_LAST.Y;
- Gyro_radian_old_z = MPU6050_GYRO_LAST.Z; //儲(chǔ)存歷史值
-
- //--------------------將輸出值融合到四個(gè)電機(jī)--------------------------------//
-
- if(Rc_D.THROTTLE>1020)
- {
- thr=Rc_D.THROTTLE- 1000;
- // if(Rc_D.THROTTLE<>
- // {
- // moto1=(int16_t)(thr - Pitch_core_out);//- yaw);
- // moto2=(int16_t)(thr - Pitch_core_out);//+ yaw);
- // moto3=(int16_t)(thr + Pitch_core_out);// - yaw);
- // moto4=(int16_t)(thr + Pitch_core_out);//+ yaw);
-
- // moto1=(int16_t)(thr - Roll_core_out);//- yaw);
- // moto2=(int16_t)(thr + Roll_core_out);//+ yaw);
- // moto3=(int16_t)(thr + Roll_core_out);// - yaw);
- // moto4=(int16_t)(thr - Roll_core_out);//+ yaw);
- // moto1=(int16_t)(thr - Yaw_core_out);//- yaw);
- // moto2=(int16_t)(thr + Yaw_core_out);//+ yaw);
- // moto3=(int16_t)(thr - Yaw_core_out);// - yaw);
- // moto4=(int16_t)(thr + Yaw_core_out);//+ yaw);
-
- //moto1=(int16_t)(thr - Roll_core_out - Pitch_core_out);
- //moto2=(int16_t)(thr + Roll_core_out - Pitch_core_out);
- //moto3=(int16_t)(thr + Roll_core_out + Pitch_core_out);
- //moto4=(int16_t)(thr - Roll_core_out + Pitch_core_out);
- //
- 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);
-
- // }
- }
- else
- {
- moto1 = 0;
- moto2 = 0;
- moto3 = 0;
- moto4 = 0;
- }
- MOTO_PWMRFLASH(moto1,moto2,moto3,moto4);// Moto_PwmRflash(moto1,moto2,moto3,moto4);
- }
|