|
|
|
@ -2,7 +2,6 @@
|
|
|
|
#include "Relays.h"
|
|
|
|
#include "Relays.h"
|
|
|
|
#include "USART.h"
|
|
|
|
#include "USART.h"
|
|
|
|
#include "rs485.h"
|
|
|
|
#include "rs485.h"
|
|
|
|
extern u16 tem;
|
|
|
|
|
|
|
|
float cold_tem = 0;
|
|
|
|
float cold_tem = 0;
|
|
|
|
float red_tem = 0;
|
|
|
|
float red_tem = 0;
|
|
|
|
float ti;
|
|
|
|
float ti;
|
|
|
|
@ -18,7 +17,6 @@ int max_speed_count = 6000;
|
|
|
|
|
|
|
|
|
|
|
|
void PID_Init()
|
|
|
|
void PID_Init()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// pid.set_tem=tem;// user set temperature
|
|
|
|
|
|
|
|
// if flash have not a vaild value, just set a default value
|
|
|
|
// if flash have not a vaild value, just set a default value
|
|
|
|
if (pid.Kp < 1e-7) { pid.Kp = 9.6; }
|
|
|
|
if (pid.Kp < 1e-7) { pid.Kp = 9.6; }
|
|
|
|
if (pid.Ki < 1e-7) { pid.Ki = 0.01; }
|
|
|
|
if (pid.Ki < 1e-7) { pid.Ki = 0.01; }
|
|
|
|
@ -32,13 +30,22 @@ void PID_Init()
|
|
|
|
pid.OUT0 = 1;
|
|
|
|
pid.OUT0 = 1;
|
|
|
|
pid.C1ms = 0;
|
|
|
|
pid.C1ms = 0;
|
|
|
|
|
|
|
|
|
|
|
|
pid.hp = 2.4;
|
|
|
|
pid.max_compressor_tem = 30;
|
|
|
|
pid.hi = 0.02;
|
|
|
|
|
|
|
|
pid.hd = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pid.cp = 9.6;
|
|
|
|
pid.hp_h = 5;
|
|
|
|
|
|
|
|
pid.hi_h = 0.02;
|
|
|
|
|
|
|
|
pid.hd_h = 0.5;
|
|
|
|
|
|
|
|
pid.h_base_h = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pid.hp_l = 19.2;
|
|
|
|
|
|
|
|
pid.hi_l = 0.08;
|
|
|
|
|
|
|
|
pid.hd_l = 0;
|
|
|
|
|
|
|
|
pid.h_base_l = 30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pid.cp = 4.8;
|
|
|
|
pid.ci = 0;
|
|
|
|
pid.ci = 0;
|
|
|
|
pid.cd = 0;
|
|
|
|
pid.cd = 0;
|
|
|
|
|
|
|
|
pid.c_base = 37;
|
|
|
|
|
|
|
|
|
|
|
|
pid.h_percent = 0;
|
|
|
|
pid.h_percent = 0;
|
|
|
|
pid.c_speed = 0;
|
|
|
|
pid.c_speed = 0;
|
|
|
|
@ -93,8 +100,8 @@ void set_heater_power(int percent) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* heater power calc
|
|
|
|
* heater power calc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
int calc_hp(float delta_t, float Error_calc, int p_hb, float pid_hp, float pid_hi) {
|
|
|
|
int calc_hp(float delta_t, float Error_calc, float DelEk, int p_hb, float pid_hp, float pid_hi, float pid_hd) {
|
|
|
|
int p_h = p_hb + pid_hp * delta_t + pid_hi * Error_calc;
|
|
|
|
int p_h = p_hb + pid_hp * delta_t + pid_hi * Error_calc + pid_hd * DelEk;
|
|
|
|
if (p_h > 100) {
|
|
|
|
if (p_h > 100) {
|
|
|
|
return 100;
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -136,8 +143,6 @@ void PID_Calc() // pid calc
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int min_speed_count = 1800;
|
|
|
|
int min_speed_count = 1800;
|
|
|
|
int max_speed_count = 4800;
|
|
|
|
int max_speed_count = 4800;
|
|
|
|
int p_hb = 52;
|
|
|
|
|
|
|
|
int p_cb = 33;
|
|
|
|
|
|
|
|
float DelEk; // The difference between the last two deviations
|
|
|
|
float DelEk; // The difference between the last two deviations
|
|
|
|
// float td;
|
|
|
|
// float td;
|
|
|
|
float out;
|
|
|
|
float out;
|
|
|
|
@ -148,32 +153,55 @@ void PID_Calc() // pid calc
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
float delta_t = pid.set_tem - pid.now_tem;
|
|
|
|
float delta_t = pid.set_tem - pid.now_tem;
|
|
|
|
|
|
|
|
// When the target tem is greater then max compressor tem, the compressor will stop
|
|
|
|
|
|
|
|
if (pid.set_tem > pid.max_compressor_tem) {
|
|
|
|
|
|
|
|
pid.c_speed = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
int p_c = calc_cp(delta_t, pid.c_base, pid.cp);
|
|
|
|
|
|
|
|
pid.c_speed = calc_compressor_speed(p_c, min_speed_count, max_speed_count);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p_c = calc_cp(delta_t, p_cb, pid.cp);
|
|
|
|
float hp = pid.hp_h;
|
|
|
|
pid.c_speed = calc_compressor_speed(p_c, min_speed_count, max_speed_count);
|
|
|
|
float hi = pid.hi_h;
|
|
|
|
|
|
|
|
float hd = pid.hd_h;
|
|
|
|
|
|
|
|
int h_base = pid.h_base_h;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// l mode
|
|
|
|
|
|
|
|
if (pid.set_tem <= pid.max_compressor_tem) {
|
|
|
|
|
|
|
|
hp = pid.hp_l;
|
|
|
|
|
|
|
|
hi = pid.hi_l;
|
|
|
|
|
|
|
|
hd = pid.hd_l;
|
|
|
|
|
|
|
|
h_base = pid.h_base_l;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pid.Ek = pid.set_tem - pid.now_tem;
|
|
|
|
pid.Ek = pid.set_tem - pid.now_tem;
|
|
|
|
pid.Pout = pid.Kp * pid.Ek; // Proportional output
|
|
|
|
pid.Pout = pid.Kp * pid.Ek; // Proportional output
|
|
|
|
|
|
|
|
|
|
|
|
pid.SEk += pid.Ek; // Total historical deviation
|
|
|
|
pid.SEk += pid.Ek; // Total historical deviation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DelEk = pid.Ek - pid.Ek_prev; // The difference between the last two deviations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// no integral when the deviation is too large
|
|
|
|
|
|
|
|
if (pid.now_tem < pid.set_tem - 3) {
|
|
|
|
|
|
|
|
pid.SEk = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SEk limit, updated func
|
|
|
|
// SEk limit, updated func
|
|
|
|
if (pid.SEk < - p_hb / pid.hi) {
|
|
|
|
if (pid.SEk < - h_base / hi) {
|
|
|
|
pid.SEk = - p_hb / pid.hi;
|
|
|
|
pid.SEk = - h_base / hi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
pid.SEk = 0;
|
|
|
|
pid.SEk = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
float Error_calc = pid.SEk;
|
|
|
|
float Error_calc = pid.SEk;
|
|
|
|
if (Error_calc < - (p_hb + pid.hp * delta_t) / pid.hi) {
|
|
|
|
if (Error_calc < - (h_base + hp * delta_t) / hi) {
|
|
|
|
Error_calc = - (p_hb + pid.hp * delta_t) / pid.hi;
|
|
|
|
Error_calc = - (h_base + hp * delta_t) / hi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
Error_calc = 0;
|
|
|
|
Error_calc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
@ -203,11 +231,9 @@ void PID_Calc() // pid calc
|
|
|
|
{
|
|
|
|
{
|
|
|
|
pid.OUT = out;
|
|
|
|
pid.OUT = out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pid.Ek_1 = pid.Ek; // udpate difference
|
|
|
|
|
|
|
|
pid.C1ms = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// heater percent
|
|
|
|
// heater percent
|
|
|
|
pid.h_percent = calc_hp(delta_t, Error_calc, p_hb, pid.hp, pid.hi);
|
|
|
|
pid.h_percent = calc_hp(delta_t, Error_calc, DelEk, h_base, hp, hi, hd);
|
|
|
|
// close heater when compressor is running in full state
|
|
|
|
// close heater when compressor is running in full state
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
if (pid.c_speed == max_speed_count) {
|
|
|
|
pid.h_percent = 0;
|
|
|
|
pid.h_percent = 0;
|
|
|
|
@ -216,4 +242,8 @@ void PID_Calc() // pid calc
|
|
|
|
set_compressor_power(pid.c_speed);
|
|
|
|
set_compressor_power(pid.c_speed);
|
|
|
|
set_heater_power(pid.h_percent);
|
|
|
|
set_heater_power(pid.h_percent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pid.Ek_prev = pid.Ek; // udpate difference
|
|
|
|
|
|
|
|
pid.C1ms = 0;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|