测试三 pid调节-加热棒的p,粗犷的时代

pid
Zeng wei (曾威) 2 years ago
parent 6f62a4d54d
commit 784b535bb7

@ -13,7 +13,6 @@ extern u8 gpio_state;
extern int T; extern int T;
unsigned int num = 0; unsigned int num = 0;
PID pid; PID pid;
u8 rs485speed[8] = {0x01, 0x06, 0x60, 0x00, 0x00, 0x09, 0xBB, 0xAA}; // speed control for compressor controller
int min_speed_count = 1500; int min_speed_count = 1500;
int max_speed_count = 6000; int max_speed_count = 6000;
@ -26,7 +25,7 @@ void PID_Init()
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; }
pid.t = 500; // PID calc period pid.t = 5000; // PID calc period
// pid.Ti=5000000;// integral time // pid.Ti=5000000;// integral time
// pid.Td=1000;// differential time // pid.Td=1000;// differential time
pid.pwmcycle = 200; // pwm cycle 200 pid.pwmcycle = 200; // pwm cycle 200
@ -35,79 +34,129 @@ void PID_Init()
} }
/** /**
* send speed count to compressor controller * set compressor speed count
* range of speed count: 0-6000, if speed count lower than 1500, the compressor will stop
*/ */
void send_speed_signal(int speed) { void set_compressor_power(int speed) {
rs485speed[4] = speed / 256; u8 data[8] = {0x01, 0x06, 0x60, 0x00, 0x00, 0x09, 0xBB, 0xAA}; // speed control for compressor controller
rs485speed[5] = speed % 256; if (speed > 6000) {
speed = 6000;
}
if (speed < 0) {
speed = 0;
}
data[4] = speed / 256;
data[5] = speed % 256;
GetCRC16(data, 6, data + 6, data + 7);
RS485_3_Init(9600); RS485_3_Init(9600);
RS485_3_Send_Data(rs485speed, 8); RS485_3_Send_Data(data, 8);
delay_xms(30); delay_xms(30);
RS485_1_Init(9600); RS485_1_Init(9600);
} }
void PID_Calc() // pid calc /**
{ * set heater percent
// float DelEk; // The difference between the last two deviations * range of heater percent: 0-100
// // float td; */
// float out; void set_heater_power(int percent) {
// if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived u8 data[8] = { 0x10, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
// {
// 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;
// // }
// pid.Ek = pid.now_tem - pid.set_tem;
// pid.Pout = pid.Kp * pid.Ek; // Proportional output
// pid.SEk += pid.Ek; // Total historical deviation
// DelEk = pid.Ek - pid.Ek_1; // The difference between the last two deviations data[4] = percent / 256;
data[5] = percent % 256;
// // ti=pid.t/pid.Ti; GetCRC16(data, 6, data + 6, data + 7);
// // ki=ti*pid.Kp;
// pid.Iout = pid.Ki * pid.SEk; // integral output RS485_1_Send_Data(data, 8);
delay_xms(30);
}
// // td=pid.Td/pid.t; /**
// // kd=pid.Kp*td; * heater power calc
*/
int calc_p(float t_t, float t_c) {
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;
// pid.Dout = pid.Kd * DelEk; // difference output }
// if (pid.Dout < 0)
// {
// pid.Dout = 0 - pid.Dout;
// }
// // out= pid.Pout+pid.Iout+ pid.Dout; void PID_Calc() // pid calc
// out = pid.Pout; {
// if (out > pid.pwmcycle) float DelEk; // The difference between the last two deviations
// { // float td;
// pid.OUT = pid.pwmcycle; float out;
// } if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived
// else if (out <= 0) {
return;
}
// if (pid.set_tem > pid.now_tem)
// { // {
// pid.OUT = pid.OUT0; // pid.Ek = pid.set_tem - pid.now_tem;
// } // }
// else // else
// { // {
// pid.OUT = out; // pid.Ek = pid.now_tem - pid.set_tem;
// } // }
// pid.Ek_1 = pid.Ek; // udpate difference pid.Ek = pid.now_tem - pid.set_tem;
// pid.C1ms = 0; pid.Pout = pid.Kp * pid.Ek; // Proportional output
pid.SEk += pid.Ek; // Total historical deviation
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; // integral output
// td=pid.Td/pid.t;
// kd=pid.Kp*td;
pid.Dout = pid.Kd * DelEk; // difference output
if (pid.Dout < 0)
{
pid.Dout = 0 - pid.Dout;
}
// out= pid.Pout+pid.Iout+ pid.Dout;
out = pid.Pout;
if (out > pid.pwmcycle)
{
pid.OUT = pid.pwmcycle;
}
else if (out <= 0)
{
pid.OUT = pid.OUT0;
}
else
{
pid.OUT = out;
}
pid.Ek_1 = pid.Ek; // udpate difference
pid.C1ms = 0;
// heater percent
int heater_percent = calc_p(pid.set_tem, pid.now_tem);
// // speed count // TODO:: temply, set Kd to heater_percent, use for data upload
pid.Kd = heater_percent;
// speed count
// int speed_count = pid.OUT / 200.0 * (max_speed_count - min_speed_count) + min_speed_count; // int speed_count = pid.OUT / 200.0 * (max_speed_count - min_speed_count) + min_speed_count;
// if (speed_count > 6000) { // if (speed_count > 6000) {
// speed_count = 6000; // speed_count = 6000;
// } // }
int speed_count = 1500;
// if (pid.now_tem < pid.set_tem + pid.tem_offset - pid.tem_threshold) // if (pid.now_tem < pid.set_tem + pid.tem_offset - pid.tem_threshold)
// { // {
@ -117,7 +166,8 @@ void PID_Calc() // pid calc
// /*GPIO1->Alarm bell GPIO3->heater GPIO4->Fresh air fan GPIO5->humidifier GPIO6->compressor */ // /*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
// HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04 // HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04
// speed_count = 1000; // close compressor
// speed_count = 1500;
// hot_clod_flag = 2; // hot_clod_flag = 2;
// pid.Iout = 0; // pid.Iout = 0;
// } else if (pid.now_tem > pid.set_tem + pid.tem_offset - pid.tem_threshold && pid.now_tem < pid.set_tem + pid.tem_offset + pid.tem_threshold) // } else if (pid.now_tem > pid.set_tem + pid.tem_offset - pid.tem_threshold && pid.now_tem < pid.set_tem + pid.tem_offset + pid.tem_threshold)
@ -144,8 +194,8 @@ void PID_Calc() // pid calc
// // pid.Iout=0; // // pid.Iout=0;
// } // }
// send_speed_signal(speed_count); set_compressor_power(speed_count);
send_speed_signal(1500); set_heater_power(heater_percent);
// 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 // 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
// { // {

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -206,30 +206,30 @@ int hot = 0;
*/ */
void HotTestRequestTask(void *pvParameters) { void HotTestRequestTask(void *pvParameters) {
while (1) { while (1) {
if (T >= 350) { // if (T >= 350) {
hot = 0; // hot = 0;
hot_clod_flag = 0; // hot_clod_flag = 0;
} else { // } else {
hot = 52; // hot = 52;
hot_clod_flag = 2; // hot_clod_flag = 2;
// hot += 10; // // hot += 10;
// if (hot > 100) { // // if (hot > 100) {
// hot = 10; // // hot = 10;
// } // // }
} // }
u8 temp_data[8] = { 0x10, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 }; // u8 temp_data[8] = { 0x10, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
temp_data[4] = hot / 256; // temp_data[4] = hot / 256;
temp_data[5] = hot % 256; // temp_data[5] = hot % 256;
GetCRC16(temp_data, 6, temp_data + 6, temp_data + 7); // GetCRC16(temp_data, 6, temp_data + 6, temp_data + 7);
// RS485_3_Init(9600); // // RS485_3_Init(9600);
RS485_1_Send_Data(temp_data, 8);
// RS485_1_Send_Data(temp_data, 8); // RS485_1_Send_Data(temp_data, 8);
delay_xms(30); // // RS485_1_Send_Data(temp_data, 8);
// RS485_1_Init(9600); // delay_xms(30);
// // RS485_1_Init(9600);
// HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04 // HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04

Loading…
Cancel
Save