|
|
|
@ -20,8 +20,8 @@ void PID_Init()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// pid.set_tem=tem;// user set temperature
|
|
|
|
// 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 = 40; }
|
|
|
|
if (pid.Kp < 1e-7) { pid.Kp = 9.6; }
|
|
|
|
if (pid.Ki < 1e-7) { pid.Ki = 0.001; }
|
|
|
|
if (pid.Ki < 1e-7) { pid.Ki = 0.01; }
|
|
|
|
if (pid.Kd < 1e-7) { pid.Kd = 340; }
|
|
|
|
if (pid.Kd < 1e-7) { pid.Kd = 340; }
|
|
|
|
if (pid.tem_threshold < 0.0001) { pid.tem_threshold = 0.2; }
|
|
|
|
if (pid.tem_threshold < 0.0001) { pid.tem_threshold = 0.2; }
|
|
|
|
|
|
|
|
|
|
|
|
@ -61,7 +61,12 @@ void set_compressor_power(int speed) {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void set_heater_power(int percent) {
|
|
|
|
void set_heater_power(int percent) {
|
|
|
|
u8 data[8] = { 0x10, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
u8 data[8] = { 0x10, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
if (percent > 100) {
|
|
|
|
|
|
|
|
percent = 100;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (percent < 0) {
|
|
|
|
|
|
|
|
percent = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
data[4] = percent / 256;
|
|
|
|
data[4] = percent / 256;
|
|
|
|
data[5] = percent % 256;
|
|
|
|
data[5] = percent % 256;
|
|
|
|
|
|
|
|
|
|
|
|
@ -74,23 +79,35 @@ void set_heater_power(int percent) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* heater power calc
|
|
|
|
* heater power calc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
int calc_p(float t_t, float t_c) {
|
|
|
|
int calc_p(float t_t, float t_c, float error, int p_b, float pid_p, float pid_i) {
|
|
|
|
int p_b = 52;
|
|
|
|
int p = p_b + pid_p * (t_t - t_c) + pid_i * error;
|
|
|
|
if (t_t - t_c > 5) {
|
|
|
|
if (p > 100) {
|
|
|
|
return 100;
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t_t - t_c < 5 && t_t > t_c) {
|
|
|
|
if (p < 0) {
|
|
|
|
return p_b + (t_t - t_c) * (100 - p_b) / 5;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t_t <= t_c) {
|
|
|
|
|
|
|
|
return p_b;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
|
|
|
// int p_b = 52;
|
|
|
|
|
|
|
|
// if (t_t - t_c > 5) {
|
|
|
|
|
|
|
|
// return 100;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (t_t - t_c < 5 && t_t > t_c) {
|
|
|
|
|
|
|
|
// return p_b + (t_t - t_c) * (100 - p_b) / 5;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (t_t <= t_c) {
|
|
|
|
|
|
|
|
// return p_b;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return 0;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PID_Calc() // pid calc
|
|
|
|
void PID_Calc() // pid calc
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
pid.set_tem = 37;
|
|
|
|
|
|
|
|
pid.Kp = 9.6;
|
|
|
|
|
|
|
|
pid.Ki = 0.01;
|
|
|
|
|
|
|
|
int p_base = 52;
|
|
|
|
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;
|
|
|
|
@ -107,7 +124,8 @@ void PID_Calc() // pid calc
|
|
|
|
// {
|
|
|
|
// {
|
|
|
|
// pid.Ek = pid.now_tem - pid.set_tem;
|
|
|
|
// pid.Ek = pid.now_tem - pid.set_tem;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
pid.Ek = pid.now_tem - pid.set_tem;
|
|
|
|
// pid.Ek = pid.now_tem - pid.set_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
|
|
|
|
@ -146,7 +164,7 @@ void PID_Calc() // pid calc
|
|
|
|
pid.C1ms = 0;
|
|
|
|
pid.C1ms = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// heater percent
|
|
|
|
// heater percent
|
|
|
|
int heater_percent = calc_p(pid.set_tem, pid.now_tem);
|
|
|
|
int heater_percent = calc_p(pid.set_tem, pid.now_tem, pid.SEk, p_base, pid.Kp, pid.Ki);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO:: temply, set Kd to heater_percent, use for data upload
|
|
|
|
// TODO:: temply, set Kd to heater_percent, use for data upload
|
|
|
|
pid.Kd = heater_percent;
|
|
|
|
pid.Kd = heater_percent;
|
|
|
|
|