diff --git a/HARDWARE/PID.c b/HARDWARE/PID.c index 9aed91a..1d54584 100644 --- a/HARDWARE/PID.c +++ b/HARDWARE/PID.c @@ -4,6 +4,8 @@ #include "Relays.h" #include "USART.h" #include "rs485.h" +#define MODE_H 1 +#define MODE_L 0 float cold_tem = 0; float red_tem = 0; float ti; @@ -61,9 +63,9 @@ void PID_Init() pid.h_base_h = 0; pid.hp_l = 9; - pid.hi_l = 0.01; + pid.hi_l = 0.025; pid.hd_l = 0; - pid.h_base_l = 10; + pid.h_base_l = 30; pid.hp = 0; pid.hi = 0; @@ -165,11 +167,20 @@ int calc_compressor_speed(int percent, int v_min, int v_max) { if (v < v_min) { return v_min; } + return v; } void PID_Calc() // pid calc { + int mode = 0; + if (pid.set_tem > pid.max_compressor_tem) { + // h mode + mode = MODE_H; + } else { + // l mode + mode = MODE_L; + } // int min_speed_count = 1800; // int max_speed_count = 4800; float DelEk; // The difference between the last two deviations @@ -180,26 +191,26 @@ void PID_Calc() // pid calc // return; // } - - float delta_t = pid.set_tem - pid.now_tem; - pid.hp = pid.hp_h; - pid.hi = pid.hi_h; - pid.hd = pid.hd_h; - pid.h_base = pid.h_base_h; + float delta_t = pid.set_tem - pid.now_tem; - - // if now temp is close to set temp, the heater will be less power - if (pid.set_tem - pid.now_tem < 3) { - pid.hp = pid.hp_h * 0.6; - } - if (pid.set_tem - pid.now_tem < 1) { - pid.hp = pid.hp_h * 0.3; - } + if (mode == MODE_H) { + pid.hp = pid.hp_h; + pid.hi = pid.hi_h; + pid.hd = pid.hd_h; + pid.h_base = pid.h_base_h; + // if now temp is close to set temp, the heater will be less power + if (pid.set_tem - pid.now_tem < 3) { + pid.hp = pid.hp_h * 0.6; + } + if (pid.set_tem - pid.now_tem < 1) { + pid.hp = pid.hp_h * 0.3; + } + } // l mode - if (pid.set_tem <= pid.max_compressor_tem) { + if (mode == MODE_L) { pid.hp = pid.hp_l; pid.hi = pid.hi_l; pid.hd = pid.hd_l; @@ -222,7 +233,6 @@ void PID_Calc() // pid calc pid.hp = (-pid.hp_l / 6 + 0.06) * delta_tem + pid.hp_l / 6 + 0.3; } } - } pid.Ek = pid.set_tem - pid.now_tem; @@ -232,17 +242,31 @@ void PID_Calc() // pid calc 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 - 5) { - // pid.SEk = 0; - // } - - + // h mode + if (mode == MODE_H) { + // when set temp is larger then real temp, the heater will be less power + // in h mode, when we want to heat over 5 degrees, just use hp + if (pid.set_tem > pid.now_tem + 5) { + pid.hi = 0; + pid.hd = 0; + pid.SEk = 0; + } + } - // SEk limit, updated func, remain a little heater power when the compressor is running in full state - if (pid.SEk < - (pid.h_base / 2) / pid.hi) { - pid.SEk = - (pid.h_base / 2) / pid.hi; + // l mode + if (mode == MODE_L) { + // make integral smaller when the temp is too low and the SEk is too small + if (pid.now_tem < pid.set_tem - 2 && pid.SEk <= - (pid.h_base / 2) / pid.hi) { + pid.SEk /= 10; + } + // SEk limit, updated func, remain a little heater power when the compressor is running in full state + if (pid.SEk < - (pid.h_base / 2) / pid.hi) { + pid.SEk = - (pid.h_base / 2) / pid.hi; + } } + + + // when the compressor is in full status, then the integral will be 0 if (pid.c_speed == max_speed_count) { pid.SEk = 0; } @@ -291,7 +315,8 @@ void PID_Calc() // pid calc } else { // if outer temp is got, we calc pid by outer temp if (abs(pid.out_tem) > 1e-5) { - pid.cp = (pid.out_tem - pid.set_tem) * 0.28; + // pid.cp = (pid.out_tem - pid.set_tem) * 0.28; + pid.cp = (pid.out_tem - pid.set_tem) * 0.2; } // use nagetive error and error diff when calc compressor power int p_c = calc_cp(delta_t, - Error_calc, - DelEk, pid.c_base, pid.cp, pid.ci, pid.cd); @@ -300,6 +325,10 @@ void PID_Calc() // pid calc // heater percent pid.h_percent = calc_hp(delta_t, Error_calc, DelEk, pid.h_base, pid.hp, pid.hi, pid.hd); + // in h mode, if current temp is close to set temp, the heater will close + if (pid.now_tem > pid.set_tem - 0.2 && pid.set_tem > pid.max_compressor_tem) { + pid.h_percent = 0; + } // close heater when compressor is running in full state if (pid.c_speed == max_speed_count) { pid.h_percent = 0; diff --git a/HARDWARE/rs485.c b/HARDWARE/rs485.c index 19c2648..4d87f95 100644 --- a/HARDWARE/rs485.c +++ b/HARDWARE/rs485.c @@ -4,10 +4,10 @@ #include "Relays.h" #include "myfreertos.h" -u8 prev_sendbuf[49] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; -u8 sendbuf[49] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; +u8 prev_sendbuf[53] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; +u8 sendbuf[53] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; u8 sendbuf_crc[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; extern u8 global_buffer[64]; @@ -394,10 +394,10 @@ void RS485_1_Upload_Params(void) sendbuf[1] = 0xB5; sendbuf[2] = 0x05; - sendbuf[45] = 0xFF; - sendbuf[46] = 0xFC; - sendbuf[47] = 0xFF; - sendbuf[48] = 0xFF; + sendbuf[49] = 0xFF; + sendbuf[50] = 0xFC; + sendbuf[51] = 0xFF; + sendbuf[52] = 0xFF; if (T <= 1000 && T >= -1000) { @@ -491,30 +491,34 @@ void RS485_1_Upload_Params(void) sendbuf[36] = (int)(pid.cp * 1000) % 256; // Kp 110 sendbuf[37] = ((int)(pid.ci * 100000)) / 256; // Ti 0.001 sendbuf[38] = ((int)(pid.ci * 100000)) % 256; // Ti 0.001 - // sendbuf[39] = (int)pid.cd / 256; // Td 340 - // sendbuf[40] = (int)pid.cd % 256; // Td 340 - // TODO::temply print out_tem - sendbuf[39] = (int)(pid.out_tem * 10) / 256; // Td 340 - sendbuf[40] = (int)(pid.out_tem * 10) % 256; // Td 340 + sendbuf[39] = (int)pid.cd / 256; // Td 340 + sendbuf[40] = (int)pid.cd % 256; // Td 340 + // // TODO::temply print out_tem + // sendbuf[39] = (int)(pid.out_tem * 10) / 256; // Td 340 + // sendbuf[40] = (int)(pid.out_tem * 10) % 256; // Td 340 sendbuf[41] = pid.c_speed / 256; // h_percent sendbuf[42] = pid.c_speed % 256; // h_percent + sendbuf[43] = (int)(pid.out_tem * 10) / 256; + sendbuf[44] = (int)(pid.out_tem * 10) % 256; + sendbuf[45] = (int)(pid.out_humidity * 10) / 256; + sendbuf[46] = (int)(pid.out_humidity * 10) % 256; // bufcut_Init(sendbuf_crc, sendbuf, 3, 31); // GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2); - GetCRC16(sendbuf, 43, &crc_num1, &crc_num2); - sendbuf[43] = crc_num1; - sendbuf[44] = crc_num2; + GetCRC16(sendbuf, 47, &crc_num1, &crc_num2); + sendbuf[47] = crc_num1; + sendbuf[48] = crc_num2; // u8 tmpabc[] = {0xAA, 0xBB, 0xCC}; // RS485_1_Send_Data(tmpabc, 3); // only when modify happen, then send // if (cmp_str(prev_sendbuf, sendbuf, 49) != 0) { - RS485_1_Send_Data(sendbuf, 49); + RS485_1_Send_Data(sendbuf, 53); vTaskDelay(100); - cp_str_to_prev(prev_sendbuf, sendbuf, 49); + cp_str_to_prev(prev_sendbuf, sendbuf, 53); // } } diff --git a/USER/control.uvguix.Administrator b/USER/control.uvguix.Administrator index 59f8db1..8207195 100644 --- a/USER/control.uvguix.Administrator +++ b/USER/control.uvguix.Administrator @@ -75,10 +75,10 @@ 44 2 - 3 + 2 - -1 - -1 + -32000 + -32000 -1 @@ -95,7 +95,7 @@ 0 392 - 01000000040000000100000001000000010000000100000000000000020000000000000001000000010000000000000028000000280000000100000002000000010000000100000057453A5CD6C7C4DCC5A9D2B520D6C7C4DCBBB7BEB3C6F8BAF2CFE45CD6C7C4DCC6F8BAF2C5E0D1F8CFE45CD6F7BFD8B0E55CB4FAC2EB5C636F6E74726F6C5F6672656572746F735C48415244574152455C72733438352E63000000000772733438352E6300000000FFDC7800FFFFFFFF55453A5CD6C7C4DCC5A9D2B520D6C7C4DCBBB7BEB3C6F8BAF2CFE45CD6C7C4DCC6F8BAF2C5E0D1F8CFE45CD6F7BFD8B0E55CB4FAC2EB5C636F6E74726F6C5F6672656572746F735C48415244574152455C5049442E6300000000055049442E6300000000BECEA100FFFFFFFF0100000010000000C5D4F200FFDC7800BECEA100F0A0A100BCA8E1009CC1B600F7B88600D9ADC200A5C2D700B3A6BE00EAD6A300F6FA7D00B5E99D005FC3CF00C1838300CACAD500010000000000000002000000F4000000660000008007000063030000 + 01000000040000000100000001000000010000000100000000000000020000000000000001000000010000000000000028000000280000000100000002000000010000000100000057453A5CD6C7C4DCC5A9D2B520D6C7C4DCBBB7BEB3C6F8BAF2CFE45CD6C7C4DCC6F8BAF2C5E0D1F8CFE45CD6F7BFD8B0E55CB4FAC2EB5C636F6E74726F6C5F6672656572746F735C48415244574152455C72733438352E63000000000772733438352E6300000000FFDC7800FFFFFFFF55453A5CD6C7C4DCC5A9D2B520D6C7C4DCBBB7BEB3C6F8BAF2CFE45CD6C7C4DCC6F8BAF2C5E0D1F8CFE45CD6F7BFD8B0E55CB4FAC2EB5C636F6E74726F6C5F6672656572746F735C48415244574152455C5049442E6300000000055049442E6300000000BECEA100FFFFFFFF0100000010000000C5D4F200FFDC7800BECEA100F0A0A100BCA8E1009CC1B600F7B88600D9ADC200A5C2D700B3A6BE00EAD6A300F6FA7D00B5E99D005FC3CF00C1838300CACAD500010000000000000002000000F483FFFF4F83FFFF808AFFFF4C86FFFF @@ -1722,7 +1722,7 @@ Debug 2373 - 00200000000000001900FFFF01001100434D4643546F6F6C426172427574746F6ECC880000000000002500000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000018017800000000000002600000000000000000000000000000000010000000100000001801D800000000000002700000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000001801A800000000000002800000000000000000000000000000000010000000100000001801B80000000000000290000000000000000000000000000000001000000010000000180E57F0000000000002A00000000000000000000000000000000010000000100000001801C800000000000002B00000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000018000890000000000002C00000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF0000000000000000000000000001000000010000000180E48B0000000000002D0000000000000000000000000000000001000000010000000180F07F0000000000002E0000000000000000000000000000000001000000010000000180E8880000000000003700000000000000000000000000000000010000000100000001803B010000000000002F0000000000000000000000000000000001000000010000000180BB8A00000000000030000000000000000000000000000000000100000001000000FFFF01001500434D4643546F6F6C4261724D656E75427574746F6E0E01000000000000310000000D57617463682057696E646F7773000000000000000000000000010000000100000000000000000000000100000003001380D88B00000000000031000000085761746368202631000000000000000000000000010000000100000000000000000000000100000000001380D98B00000000000031000000085761746368202632000000000000000000000000010000000100000000000000000000000100000000001380CE01000000000000FFFFFFFF0C576174636820416E63686F720100000000000000010000000000000001000000000000000000000001000000000013800F01000000000000320000000E4D656D6F72792057696E646F7773000000000000000000000000010000000100000000000000000000000100000004001380D28B00000000000032000000094D656D6F7279202631000000000000000000000000010000000100000000000000000000000100000000001380D38B00000000000032000000094D656D6F7279202632000000000000000000000000010000000100000000000000000000000100000000001380D48B00000000000032000000094D656D6F7279202633000000000000000000000000010000000100000000000000000000000100000000001380D58B00000000000032000000094D656D6F72792026340000000000000000000000000100000001000000000000000000000001000000000013801001000000000000330000000E53657269616C2057696E646F77730000000000000000000000000100000001000000000000000000000001000000040013809307000000000000330000000855415254202326310000000000000000000000000100000001000000000000000000000001000000000013809407000000000000330000000855415254202326320000000000000000000000000100000001000000000000000000000001000000000013809507000000000000330000000855415254202326330000000000000000000000000100000001000000000000000000000001000000000013809607000000000000330000001626446562756720287072696E746629205669657765720000000000000000000000000100000001000000000000000000000001000000000013803C010000000000003400000010416E616C797369732057696E646F7773000000000000000000000000010000000100000000000000000000000100000004001380658A000000000000340000000F264C6F67696320416E616C797A6572000000000000000000000000010000000100000000000000000000000100000000001380DC7F0000000000003E0000001526506572666F726D616E636520416E616C797A6572000000000000000000000000010000000100000000000000000000000100000000001380E788000000000000380000000E26436F646520436F766572616765000000000000000000000000010000000100000000000000000000000100000000001380CD01000000000000FFFFFFFF0F416E616C7973697320416E63686F7201000000000000000100000000000000010000000000000000000000010000000000138053010000000000003F0000000D54726163652057696E646F77730000000000000000000000000100000001000000000000000000000001000000010013805401000000000000FFFFFFFF115472616365204D656E7520416E63686F720100000000000000010000000000000001000000000000000000000001000000000013802901000000000000350000001553797374656D205669657765722057696E646F77730000000000000000000000000100000001000000000000000000000001000000010013804B01000000000000FFFFFFFF1453797374656D2056696577657220416E63686F720100000000000000010000000000000001000000000000000000000001000000000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000013800189000000000000360000000F26546F6F6C626F782057696E646F7700000000000000000000000001000000010000000000000000000000010000000300138044C5000000000000FFFFFFFF0E5570646174652057696E646F77730100000000000000010000000000000001000000000000000000000001000000000013800000000000000400FFFFFFFF000000000000000000010000000000000001000000000000000000000001000000000013805B01000000000000FFFFFFFF12546F6F6C626F78204D656E75416E63686F72010000000000000001000000000000000100000000000000000000000100000000000000000005446562756764020000 + 00200000000000001900FFFF01001100434D4643546F6F6C426172427574746F6ECC880000000000002500000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000018017800000000000002600000000000000000000000000000000010000000100000001801D800000000000002700000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000001801A800000000000002800000000000000000000000000000000010000000100000001801B80000000000000290000000000000000000000000000000001000000010000000180E57F0000000000002A00000000000000000000000000000000010000000100000001801C800000000000002B00000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000018000890000000000002C00000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF0000000000000000000000000001000000010000000180E48B0000000000002D0000000000000000000000000000000001000000010000000180F07F0000000000002E0000000000000000000000000000000001000000010000000180E8880000000000003700000000000000000000000000000000010000000100000001803B010000000000002F0000000000000000000000000000000001000000010000000180BB8A00000000000030000000000000000000000000000000000100000001000000FFFF01001500434D4643546F6F6C4261724D656E75427574746F6E0E01000000000000310000000D57617463682057696E646F7773000000000000000000000000010000000100000000000000000000000100000003001380D88B00000000000031000000085761746368202631000000000000000000000000010000000100000000000000000000000100000000001380D98B00000000000031000000085761746368202632000000000000000000000000010000000100000000000000000000000100000000001380CE01000000000000FFFFFFFF0C576174636820416E63686F720000000000000000010000000000000001000000000000000000000001000000000013800F01000000000000320000000E4D656D6F72792057696E646F7773000000000000000000000000010000000100000000000000000000000100000004001380D28B00000000000032000000094D656D6F7279202631000000000000000000000000010000000100000000000000000000000100000000001380D38B00000000000032000000094D656D6F7279202632000000000000000000000000010000000100000000000000000000000100000000001380D48B00000000000032000000094D656D6F7279202633000000000000000000000000010000000100000000000000000000000100000000001380D58B00000000000032000000094D656D6F72792026340000000000000000000000000100000001000000000000000000000001000000000013801001000000000000330000000E53657269616C2057696E646F77730000000000000000000000000100000001000000000000000000000001000000040013809307000000000000330000000855415254202326310000000000000000000000000100000001000000000000000000000001000000000013809407000000000000330000000855415254202326320000000000000000000000000100000001000000000000000000000001000000000013809507000000000000330000000855415254202326330000000000000000000000000100000001000000000000000000000001000000000013809607000000000000330000001626446562756720287072696E746629205669657765720000000000000000000000000100000001000000000000000000000001000000000013803C010000000000003400000010416E616C797369732057696E646F7773000000000000000000000000010000000100000000000000000000000100000004001380658A000000000000340000000F264C6F67696320416E616C797A6572000000000000000000000000010000000100000000000000000000000100000000001380DC7F0000000000003E0000001526506572666F726D616E636520416E616C797A6572000000000000000000000000010000000100000000000000000000000100000000001380E788000000000000380000000E26436F646520436F766572616765000000000000000000000000010000000100000000000000000000000100000000001380CD01000000000000FFFFFFFF0F416E616C7973697320416E63686F7200000000000000000100000000000000010000000000000000000000010000000000138053010000000000003F0000000D54726163652057696E646F77730000000000000000000000000100000001000000000000000000000001000000010013805401000000000000FFFFFFFF115472616365204D656E7520416E63686F720000000000000000010000000000000001000000000000000000000001000000000013802901000000000000350000001553797374656D205669657765722057696E646F77730000000000000000000000000100000001000000000000000000000001000000010013804B01000000000000FFFFFFFF1453797374656D2056696577657220416E63686F720000000000000000010000000000000001000000000000000000000001000000000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000013800189000000000000360000000F26546F6F6C626F782057696E646F7700000000000000000000000001000000010000000000000000000000010000000300138044C5000000000000FFFFFFFF0E5570646174652057696E646F77730000000000000000010000000000000001000000000000000000000001000000000013800000000000000400FFFFFFFF000000000000000000010000000000000001000000000000000000000001000000000013805B01000000000000FFFFFFFF12546F6F6C626F78204D656E75416E63686F72000000000000000001000000000000000100000000000000000000000100000000000000000005446562756764020000 898 @@ -1758,9 +1758,9 @@ ../HARDWARE/PID.c - 15 - 119 - 149 + 13 + 116 + 140 1 0 diff --git a/myfreertos/myfreertos.c b/myfreertos/myfreertos.c index 4320881..da18732 100644 --- a/myfreertos/myfreertos.c +++ b/myfreertos/myfreertos.c @@ -544,7 +544,7 @@ void process_outer_sensor_data(u8 *data) { out_humidity = ((~out_humidity + 1) & 0xFFFF); } - pid.out_humidity = out_humidity; + pid.out_humidity = (float)out_humidity / 10; } /**