Update PID.c

main
Zeng wei (曾威) 2 years ago
parent 1e9a4a15d6
commit d81878fc09

@ -18,24 +18,24 @@ int max_speed_count = 6000;
void PID_Init() void PID_Init()
{ {
// pid.set_tem=tem;//用户设定温度 // pid.set_tem=tem;// user set temperature
pid.Kp = 110; pid.Kp = 110;
pid.Ki = 0.001; pid.Ki = 0.001;
pid.Kd = 340; pid.Kd = 340;
pid.t = 500; // PID计算周期 pid.t = 500; // PID calc period
// pid.Ti=5000000;//积分时间 // pid.Ti=5000000;// integral time
// pid.Td=1000;//微分时间 // pid.Td=1000;// differential time
pid.pwmcycle = 200; // pwm周期200 pid.pwmcycle = 200; // pwm cycle 200
pid.OUT0 = 1; pid.OUT0 = 1;
pid.C1ms = 0; pid.C1ms = 0;
} }
void PID_Calc() // pid计算 void PID_Calc() // pid calc
{ {
float DelEk; // 最近两次偏差之差 float DelEk; // The difference between the last two deviations
// float td; // float td;
float out; float out;
if (pid.C1ms < (pid.t)) // 计算周期未到 if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived
{ {
return; return;
} }
@ -48,21 +48,21 @@ void PID_Calc() // pid
{ {
pid.Ek = pid.now_tem - pid.set_tem; pid.Ek = pid.now_tem - pid.set_tem;
} }
pid.Pout = pid.Kp * pid.Ek; // 比例输出 pid.Pout = pid.Kp * pid.Ek; // Proportional output
pid.SEk += pid.Ek; // 历史偏差总和 pid.SEk += pid.Ek; // Total historical deviation
DelEk = pid.Ek - pid.Ek_1; // 最近两次偏差之差 DelEk = pid.Ek - pid.Ek_1; // The difference between the last two deviations
// ti=pid.t/pid.Ti; // ti=pid.t/pid.Ti;
// ki=ti*pid.Kp; // ki=ti*pid.Kp;
pid.Iout = pid.Ki * pid.SEk; // 积分输出 pid.Iout = pid.Ki * pid.SEk; // integral output
// td=pid.Td/pid.t; // td=pid.Td/pid.t;
// kd=pid.Kp*td; // kd=pid.Kp*td;
pid.Dout = pid.Kd * DelEk; // 微分输出 pid.Dout = pid.Kd * DelEk; // difference output
if (pid.Dout < 0) if (pid.Dout < 0)
{ {
pid.Dout = 0 - pid.Dout; pid.Dout = 0 - pid.Dout;
@ -82,13 +82,15 @@ void PID_Calc() // pid
{ {
pid.OUT = out; pid.OUT = out;
} }
pid.Ek_1 = pid.Ek; // 更新偏差 pid.Ek_1 = pid.Ek; // udpate difference
pid.C1ms = 0; pid.C1ms = 0;
if (pid.set_tem > pid.now_tem + 1) if (pid.set_tem > pid.now_tem + 1)
{ {
// 得到当前的偏差值设置温度大于实际温度1℃加热close compressoropen heater // Obtain the current deviation value
/*GPIO1->报警铃 GPIO3->heater GPIO4—>新风风扇 GPIO5->加湿器 GPIO6->compressor */ // 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 HC595_Send_Byte(gpio_state &= 0xDF);//close compressor &=1101 1111 0xDF
num = 0; num = 0;
TIM_SetCompare3(TIM3, 0); TIM_SetCompare3(TIM3, 0);
@ -97,17 +99,19 @@ void PID_Calc() // pid
pid.Iout = 0; pid.Iout = 0;
} else if (pid.now_tem > pid.set_tem) } else if (pid.now_tem > pid.set_tem)
{ {
// 得到当前的偏差值设置温度小于实际温度制冷open compressorclose heater // 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 HC595_Send_Byte(gpio_state &= 0xFB); // close heater &=1111 1011 0xFB
// 0-200对应0-100%如果pid.out=50,占空比就是25%//num=50*400/200=100100/400=25% // 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); // pid.OUT与pwm占空比的值换算 num = (((pid.OUT * 400) / pid.pwmcycle) - 1); // Conversion of pid.OUT and PWM Duty Cycle Values
TIM_SetCompare3(TIM3, num / 4); TIM_SetCompare3(TIM3, num / 4);
// printf("%d\r\n",num); // 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; hot_clod_flag = 1;
// pid.Iout=0; // pid.Iout=0;
} }
if (hot_clod_flag == 1 && T <= tem - 3) // 制冷过程中温度下降低于设定温度0.3℃ 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 HC595_Send_Byte(gpio_state&=0xDB);// close compressor and heater &=1101 1011 0xDB
num = 0; num = 0;

Loading…
Cancel
Save