数字pid笔记(2)

void pid_Cal(void)
{    
    //float index = 0;
	
	IncPid * p = NULL;
    p = &g_PID_Inc;

	p->SetVal = sys_para.given_temp; 

	   
    p->ActualVal = g_pt100.f_real_value;  
    p->err = p->SetVal - p->ActualVal;  

	if(work_temp_phase == work_phase_43C_level)
	{
		if(FAN_Speed_Status == FAN_Speed_LOW)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.8;	//0.4   0.475 
			
				p->Ki = 0.4; 	//0.05  0.07

				p->Kd = 0; 	//0.2
			}
		}
		else if(FAN_Speed_Status == FAN_Speed_HIGH)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.75;	//0.4   0.475 
			
				p->Ki = 0.38; 	//0.05  0.07

				p->Kd = 0; 		//0.2
			}
		}
	}
	else if(work_temp_phase == work_phase_38C_level)
	{
		if(FAN_Speed_Status == FAN_Speed_LOW)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.8;	//0.4   0.475 
			
				p->Ki = 0.4; 	//0.05  0.07

				p->Kd = 0; 	//0.2
			}
		}
		else if(FAN_Speed_Status == FAN_Speed_HIGH)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.75;	//0.4   0.475 
			
				p->Ki = 0.38; 	//0.05  0.07

				p->Kd = 0; 		//0.2
			}
		}
	}
	else if(work_temp_phase == work_phase_32C_level)
	{
		if(FAN_Speed_Status == FAN_Speed_LOW)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.8;	//0.4   0.475 
			
				p->Ki = 0.4; 	//0.05  0.07

				p->Kd = 0; 		//0.2
			}
		}
		else if(FAN_Speed_Status == FAN_Speed_HIGH)
		{
			if(fabs(p->err) >= 6.0f)
			{
				p->Kp = 0;	//0.45

				p->Ki = 0.4;	//0.05

				p->Kd = 0;
			}
			else
			{
				p->Kp = 1.75;	//0.4   0.475 
			
				p->Ki = 0.38; 	//0.05  0.07

				p->Kd = 0; 		//0.2
			}
		}
		
	}

	p->IncrementVal = (p->Kp * (p->err - p->err_next))  
					  + (p->Ki * p->err)          
                      + (p->Kd * (p->err - 2 * p->err_next + p->err_last));   					  
    
    p->err_last = p->err_next;
    p->err_next = p->err;
}

 可以看出,首先调整的是P控制,代码中为Ki,因为增量式pid的ki参数相当于位置式的p控制,所以首先调整ki,是的温度出现震荡,然后添加kp(位置式的d控制),这样就可以使系统稳定。

同时,也是用了微分分离的思想,就是温差比较大的时候,不要微控制,即kp=0;,到了一定稳定范围了,才使用pd控制。

上一篇:C语言实现CRC16和CRC32校验


下一篇:Linux 问题