@ -15,12 +15,13 @@ PID pid;
u8 rs485speed [ 8 ] = { 0x01 , 0x06 , 0x60 , 0x00 , 0x00 , 0x09 , 0xBB , 0xAA } ; // speed control for compressor controller
int min_speed_count = 1500 ;
int max_speed_count = 6000 ;
float tem_control_threshold = 0.5 ;
void PID_Init ( )
{
// pid.set_tem=tem;// user set temperature
// if flash have not a vaild value, just set a default value
if ( pid . Kp < 1e-7 ) { pid . Kp = 11 0; }
if ( pid . Kp < 1e-7 ) { pid . Kp = 4 0; }
if ( pid . Ki < 1e-7 ) { pid . Ki = 0.001 ; }
if ( pid . Kd < 1e-7 ) { pid . Kd = 340 ; }
@ -32,6 +33,20 @@ void PID_Init()
pid . C1ms = 0 ;
}
/**
* send speed count to compressor controller
*/
void send_speed_signal ( int speed ) {
rs485speed [ 4 ] = speed / 256 ;
rs485speed [ 5 ] = speed % 256 ;
RS485_3_Init ( 9600 ) ;
delay_xms ( 100 ) ;
RS485_3_Send_Data ( rs485speed , 8 ) ;
delay_xms ( 100 ) ;
RS485_1_Init ( 9600 ) ;
}
void PID_Calc ( ) // pid calc
{
float DelEk ; // The difference between the last two deviations
@ -42,14 +57,15 @@ void PID_Calc() // pid calc
return ;
}
if ( pid . set_tem > pid . now_tem )
{
pid . Ek = pid . set_tem - pid . now_tem ;
}
else
{
pid . Ek = pid . now_tem - pid . set_tem ;
}
// if (pid.set_tem > pid.now_tem)
// {
// pid.Ek = pid.set_tem - pid.now_tem;
// }
// else
// {
// pid.Ek = pid.now_tem - pid.set_tem;
// }
pid . Ek = pid . now_tem - pid . set_tem ;
pid . Pout = pid . Kp * pid . Ek ; // Proportional output
pid . SEk + = pid . Ek ; // Total historical deviation
@ -87,61 +103,66 @@ void PID_Calc() // pid calc
pid . Ek_1 = pid . Ek ; // udpate difference
pid . C1ms = 0 ;
if ( pid . set_tem > pid . now_tem + 1 )
// speed count
int speed_count = pid . OUT / 200.0 * ( max_speed_count - min_speed_count ) + min_speed_count ;
if ( speed_count > 6000 ) {
speed_count = 6000 ;
}
if ( pid . now_tem < pid . set_tem - tem_control_threshold )
{
// Obtain the current deviation value
// when the target temperature is 1 degree Celsius higher than the actual temperature, heat up
// close compressor open heater
/*GPIO1->Alarm bell GPIO3->heater GPIO4->Fresh air fan GPIO5->humidifier GPIO6->compressor */
HC595_Send_Byte ( gpio_state & = 0xDF ) ; //close compressor &=1101 1111 0xDF
num = 0 ;
TIM_SetCompare3 ( TIM3 , 0 ) ;
// HC595_Send_Byte(gpio_state &= 0xDF);//close compressor &=1101 1111 0xDF
HC595_Send_Byte ( gpio_state | = 0x04 ) ; // open heater |=0000 0100 0x04
speed_count = 1000 ; // close compressor
hot_clod_flag = 2 ;
pid . Iout = 0 ;
} else if ( pid . now_tem > pid . set_tem )
} else if ( pid . now_tem > pid . set_tem - tem_control_threshold & & pid . now_tem < pid . set_tem + tem_control_threshold )
{
HC595_Send_Byte ( gpio_state & = 0xFB ) ; // close heater &=1111 1011 0xFB
speed_count = 1000 ; // close compressor
hot_clod_flag = 0 ;
// pid.Iout=0;
} else if ( pid . now_tem > pid . set_tem + tem_control_threshold )
{
// Obtain the current deviation value
// when the target temperature is lower than the actual temperature, refrigerate
// open compressor close heater
HC595_Send_Byte ( gpio_state & = 0xFB ) ; // close heater &=1111 1011 0xFB
// 0-200 correspond 0-100%, if pid.out=50, percentage means 25% //num=50*400/200=100 100/400=25%
num = ( ( ( pid . OUT * 400 ) / pid . pwmcycle ) - 1 ) ; // Conversion of pid.OUT and PWM Duty Cycle Values
TIM_SetCompare3 ( TIM3 , num / 4 ) ;
// // 0-200 correspond 0-100%, if pid.out=50, percentage means 25% //num=50*400/200=100 100/400=25%
// num = (((pid.OUT * 400) / pid.pwmcycle) - 1); // Conversion of pid.OUT and PWM Duty Cycle Values
// TIM_SetCompare3(TIM3, num / 4);
// printf("%d\r\n",num);
HC595_Send_Byte ( gpio_state | = 0x20 ) ; //open compressor |=0010 0000
// HC595_Send_Byte(gpio_state|=0x20); //open compressor |=0010 0000
hot_clod_flag = 1 ;
// pid.Iout=0;
}
if ( hot_clod_flag = = 1 & & T < = tem - 3 ) // During the refrigeration process, the actual temperature drops by 0.3 degrees Celsius below the set temperature
{
HC595_Send_Byte ( gpio_state & = 0xDB ) ; // close compressor and heater &=1101 1011 0xDB
num = 0 ;
TIM_SetCompare3 ( TIM3 , 0 ) ; // close compressor
hot_clod_flag = 0 ;
}
if ( hot_clod_flag = = 2 & & T > = tem ) // while heat, T above tem
{
HC595_Send_Byte ( gpio_state & = 0xDB ) ; //close compressor and heater &=1101 1011 0xDB
num = 0 ;
TIM_SetCompare3 ( TIM3 , 0 ) ; // close compressor
hot_clod_flag = 0 ;
}
int out1 = pid . OUT ;
// speed count
int speed_count = out1 / 200.0 * ( max_speed_count - min_speed_count ) + min_speed_count ;
if ( speed_count > 6000 ) {
speed_count = 6000 ;
}
rs485speed [ 4 ] = speed_count / 256 ;
rs485speed [ 5 ] = speed_count % 256 ;
send_speed_signal ( speed_count ) ;
// if (hot_clod_flag == 1 && T <= tem - 3) // During the refrigeration process, the actual temperature drops by 0.3 degrees Celsius below the set temperature
// {
// HC595_Send_Byte(gpio_state&=0xDB);// close compressor and heater &=1101 1011 0xDB
// // num = 0;
// // TIM_SetCompare3(TIM3, 0); // close compressor
// speed_count = 1000;
// hot_clod_flag = 0;
// }
// if (hot_clod_flag == 2 && T >= tem) // while heat, T above tem
// {
// HC595_Send_Byte(gpio_state&=0xDB);//close compressor and heater &=1101 1011 0xDB
// num = 0;
// TIM_SetCompare3(TIM3, 0); // close compressor
// hot_clod_flag = 0;
// }
// RS485_3_Init(9600);
delay_xms ( 100 ) ;
// RS485_3_Send_Data(rs485speed, 8);
RS485_1_Send_Data_1 ( rs485speed , 8 ) ;
delay_xms ( 200 ) ;
// RS485_1_Init(9600);
// HC595_Send_Byte(gpio_state&=0xDB);// close compressor and heater &=1101 1011 0xDB
}