我的PID这是怎么了?
[mw_shl_code=c,true]void ADC_PID(void)
{
int s[6]={0};
for(i=0;i<6;i++)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3;
printf("\nADC_ConvertedValueLocal[%d]:%f\n",i,ADC_ConvertedValueLocal);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)==1)
{
normal=125;
if(ADC_ConvertedValueLocal<1.5)
{
s=temp1;
sum+=ADC_ConvertedValueLocal;
}
}
else
{
normal=150;
if(ADC_ConvertedValueLocal<1.5)
{
s=temp2;
sum+=ADC_ConvertedValueLocal;
}
}
}
inpid= (ADC_ConvertedValueLocal[0]*s[0]+ ADC_ConvertedValueLocal[1]*s[1]+ ADC_ConvertedValueLocal[2]*s[2]+ ADC_ConvertedValueLocal[3]*s[3]+ ADC_ConvertedValueLocal[4]*s[4]+ ADC_ConvertedValueLocal[5]*s[5])/sum;
printf("\ninpid%f\n",inpid);
}
void pid(void)
{
position_error_p=Position_Kp*(inpid-normal);
position_error_d=Position_Kd*(inpid-pre_inpid);
if(0<=(rMotor_Speed+(position_error_p-position_error_d))<999)
rMotor_Speed+=(position_error_p-position_error_d); //right
else if((rMotor_Speed+(position_error_p-position_error_d))<0)
rMotor_Speed=0;
else
rMotor_Speed=999;
if(0<(lMotor_Speed-(position_error_p-position_error_d))<999)
lMotor_Speed-=(position_error_p-position_error_d);//left
else if((lMotor_Speed+(position_error_p-position_error_d))<0)
lMotor_Speed=0;
else
lMotor_Speed=999;
printf("\n×ó??%d\n",lMotor_Speed);
printf("\n????%d\n",rMotor_Speed);
pre_inpid=inpid;
TIM2->CCR1=rMotor_Speed;
TIM2->CCR2=0;
TIM2->CCR3=lMotor_Speed;
TIM2->CCR4=0;
}
int main(void)
{while(1)
{
ADC_PID();
pid();
}} [/mw_shl_code]
如图PID 输出PWM我设定只能从0~999取值 但是串口返回明显不对(PWM文件我用的例程 初始化就没粘上去) 用JTAG 显示程序在if判断时就没有进过else if 和else 这是哪里错了? |