From d81878fc09351b7a5dab14c156c7644350ee13cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zeng=20wei=20=28=E6=9B=BE=E5=A8=81=29?= Date: Mon, 29 Jan 2024 21:27:42 +0800 Subject: [PATCH] Update PID.c --- HARDWARE/PID.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/HARDWARE/PID.c b/HARDWARE/PID.c index da9535e..c942dc9 100644 --- a/HARDWARE/PID.c +++ b/HARDWARE/PID.c @@ -18,24 +18,24 @@ int max_speed_count = 6000; void PID_Init() { - // pid.set_tem=tem;//用户设定温度 + // pid.set_tem=tem;// user set temperature pid.Kp = 110; pid.Ki = 0.001; pid.Kd = 340; - pid.t = 500; // PID计算周期 - // pid.Ti=5000000;//积分时间 - // pid.Td=1000;//微分时间 - pid.pwmcycle = 200; // pwm周期200 + pid.t = 500; // PID calc period + // pid.Ti=5000000;// integral time + // pid.Td=1000;// differential time + pid.pwmcycle = 200; // pwm cycle 200 pid.OUT0 = 1; 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 out; - if (pid.C1ms < (pid.t)) // 计算周期未到 + if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived { return; } @@ -48,21 +48,21 @@ void PID_Calc() // pid { 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; // ki=ti*pid.Kp; - pid.Iout = pid.Ki * pid.SEk; // 积分输出 + pid.Iout = pid.Ki * pid.SEk; // integral output // td=pid.Td/pid.t; // kd=pid.Kp*td; - pid.Dout = pid.Kd * DelEk; // 微分输出 + pid.Dout = pid.Kd * DelEk; // difference output if (pid.Dout < 0) { pid.Dout = 0 - pid.Dout; @@ -82,13 +82,15 @@ void PID_Calc() // pid { pid.OUT = out; } - pid.Ek_1 = pid.Ek; // 更新偏差 + pid.Ek_1 = pid.Ek; // udpate difference pid.C1ms = 0; if (pid.set_tem > pid.now_tem + 1) { - // 得到当前的偏差值,设置温度大于实际温度1℃,加热,close compressor,open heater - /*GPIO1->报警铃 GPIO3->heater GPIO4—>新风风扇 GPIO5->加湿器 GPIO6->compressor */ + // 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); @@ -97,17 +99,19 @@ void PID_Calc() // pid pid.Iout = 0; } else if (pid.now_tem > pid.set_tem) { - // 得到当前的偏差值,设置温度小于实际温度,制冷,open compressor,close 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 - // 0-200对应0-100%,如果pid.out=50,占空比就是25%,//num=50*400/200=100,100/400=25% - num = (((pid.OUT * 400) / pid.pwmcycle) - 1); // pid.OUT与pwm占空比的值换算 + // 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 hot_clod_flag = 1; // 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 num = 0;