stm32f103中while(1)不按照设定的进行循环?

2019-08-19 19:34发布

我想不通为什么每循环30周期,arhs_contrl_PID()函数会那么不按照规律性的来个50ms突变然后归于正常。详细的看图
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
a133760
1楼-- · 2019-08-21 00:26
[mw_shl_code=applescript,true]void all_ch()
                {
                                 moto1=(int16_t) (moto1+thr1);
                                 moto2=(int16_t) (moto2+thr2);
                                 moto3=(int16_t) (moto3+thr3);
                                 moto4=(int16_t) (moto4+thr4);
                                        if(moto1>move_Vmin)
                                                moto1=move_Vmin;
                                        if(moto1<move_Vmax)
                                                moto1=move_Vmax;
                                       
                                        if(moto2>move_Vmin)
                                                moto2=move_Vmin;
                                        if(moto2<move_Vmax)
                                                moto2=move_Vmax;
                                               
                                        if(moto3>move_Vmin)
                                                moto3=move_Vmin;       
                                        if(moto3<move_Vmax)
                                                moto3=move_Vmax;
                                               
                                        if(moto4>move_Vmin)
                                                moto4=move_Vmin;
                                        if(moto4<move_Vmax)
                                                moto4=move_Vmax;
                       
                        if(NRF24L01_RxPacket(rxbuf)==1)        //è&#231;1&#251;&#195;&#187;óD&#189;óêüμ&#189;è&#206;o&#206;êy&#190;Y&#190;í&#213;a&#209;ù&#214;′DD
                                        {
                                         TIM_SetCompare1(TIM2,moto1);
                                         TIM_SetCompare2(TIM2,moto2);
                                         TIM_SetCompare3(TIM2,moto3);
                                         TIM_SetCompare4(TIM2,moto4);          //&#203;&#196;&#184;&#246;í¨μà&#182;&#212;ó|&#203;&#196;&#184;&#246;μ&#231;&#187;ú
                                        }
                         else
                                         while(1)
                                         {
                                                 TIM_SetCompare1(TIM2,val);
                                                 TIM_SetCompare2(TIM2,val);
                                                 TIM_SetCompare3(TIM2,val);
                                                 TIM_SetCompare4(TIM2,val);                                 //&#203;&#196;&#184;&#246;í¨μà&#182;&#212;ó|&#203;&#196;&#184;&#246;μ&#231;&#187;ú
                                         }       
                }
void contrl_pid(void)
{
//        float x1,x2,x3;
                                 moto1=0;
                                moto2=0;
                                moto3=0;
                                moto4=0;
                        /*shell pitch*/
        for(ave_n=0;ave_n<5;ave_n++)
        {
            pitch.shell_xn=Angle_SET.pitch;
                        pitch.shell_error=pitch.shell_Set - pitch.shell_xn;
                  pitch.shell_Sn +=pitch.shell_error;
                   if(pitch.shell_Sn>0.5)
                                 pitch.shell_Sn=0.4;
                         if(pitch.shell_Sn<-0.5)
                                 pitch.shell_Sn=-0.4;
                        pitch.shell_out= pitch.shell_kp*pitch.shell_error + pitch.shell_ki*pitch.shell_Sn + pitch.shell_kd*( pitch.shell_error - pitch.shell_error_last );
                        pitch.shell_error_last=pitch.shell_error;        //shell contrl renew data
                         /*core pitch*/
                  pitch.core_xn=MPU6050_GYRO_LAST.X/65.6;          //?????
                        pitch.error=pitch.shell_out - pitch.core_xn;
                  pitch.core_Sn += pitch.error;
                   if(pitch.core_Sn>10)
                                 pitch.core_Sn=10;//&#187;y·&#214;&#207;T·ù         
                         if(pitch.core_Sn<-10)
                                 pitch.core_Sn=-10;//&#187;y·&#214;&#207;T·ù       
                        pitch.core_out=pitch.core_kp*pitch.error + pitch.core_ki*pitch.core_Sn + pitch.core_kd*( pitch.error -  pitch.last_error );
                        pitch.last_error=pitch.error;
                  //Sum+=pitch.core_out;          
            //delay_us(100);
      //pitch.core_out=Sum/5.0;
               
            moto1=moto1 + pitch.core_out;
            moto2=moto2 + pitch.core_out;
                  moto3=moto3 - pitch.core_out;
            moto4=moto4 - pitch.core_out;
                       
                  /*shell roll*/
                        roll.shell_xn=Angle_SET.roll;
                        roll.shell_error=roll.shell_Set - roll.shell_xn;
                         roll.shell_Sn  +=roll.shell_error;
                   if(roll.shell_Sn>2)
                                 roll.shell_Sn=2;
                         if(roll.shell_Sn<-2)
                                 roll.shell_Sn=-2;
                        roll.shell_out=roll.shell_kp*roll.shell_error + roll.shell_ki*roll.shell_Sn + roll.shell_kd*( roll.shell_error - roll.shell_error_last );
                        roll.shell_error_last=roll.shell_error;//renew data
                        /*core roll*/   
                         
            roll.core_xn=MPU6050_GYRO_LAST.Y /65.6;          //????? ?00?s
                        roll.error = roll.shell_out - roll.core_xn;
                        roll.core_Sn+=roll.error;
                                        if(roll.core_Sn>10)
                                        roll.core_Sn=10;//&#187;y·&#214;&#207;T·ù         
                                        if(roll.core_Sn<-10)
                                        roll.core_Sn=-10;//&#187;y·&#214;&#207;T·ù       
                        roll.core_out=roll.core_kp*roll.error + roll.core_ki * roll.core_Sn + roll.core_kd*(roll.error - roll.last_error);
                        roll.last_error=roll.error;
       
      moto1=moto1 + roll.core_out;
            moto2=moto2 - roll.core_out;
                  moto3=moto3 - roll.core_out;
            moto4=moto4 + roll.core_out;
           
            /*shell yaw*/
                        yaw.shell_xn=Angle_SET.yaw;
                        yaw.shell_error=yaw.shell_Set - yaw.shell_xn;
                         yaw.shell_Sn  += yaw.shell_error;
                   if(yaw.shell_Sn>0.5)
                                 yaw.shell_Sn=0.4;
                         if(yaw.shell_Sn<-0.5)
                                yaw.shell_Sn=-0.4;
                        yaw.shell_out=yaw.shell_kp*yaw.shell_error + yaw.shell_ki*yaw.shell_Sn + yaw.shell_kd*( yaw.shell_error - yaw.shell_error_last );
                        yaw.shell_error_last=yaw.shell_error;
            /*core yaw*/
      yaw.core_xn=MPU6050_GYRO_LAST.Z /65.6;                        
                        yaw.error = yaw.shell_out - yaw.core_xn;
                        yaw.core_Sn+=yaw.error;
                        if(yaw.core_Sn>10)
                        yaw.core_Sn=10;
                        if(yaw.core_Sn<-10)
                        yaw.core_Sn=-10;//&#187;y·&#214;&#207;T·ù         //&#187;y·&#214;&#207;T·ù         
                        yaw.core_out=yaw.core_kp*yaw.error + yaw.core_ki * yaw.core_Sn + yaw.core_kd*(yaw.error - yaw.last_error);
                        yaw.last_error=yaw.error;
                       
                        moto1=moto1 + yaw.core_out;
            moto2=moto2 - yaw.core_out;
                  moto3=moto3 + yaw.core_out;
            moto4=moto4 - yaw.core_out;
                delay_us(200);       
                }       
           moto1=moto1/5.0;
                 moto2=moto2/5.0;
                 moto3=moto3/5.0;
                 moto4=moto4/5.0;
                all_ch();
}[/mw_shl_code]
a133760
2楼-- · 2019-08-21 05:39
代码也上去了,来个大神指导指导我

一周热门 更多>