pid调节-加热棒pi+压缩机p+错误累计限制,压缩机额定功率调节为4500转

pid
Zeng wei (曾威) 2 years ago
parent e2638b70e7
commit 598778b7b6

9
.gitignore vendored

@ -1 +1,10 @@
OBJ/*
*.d
*.o
*.crf
*.sct
*.lnp
*.htm
*.axf
*.hex
*.dep

@ -25,7 +25,7 @@ void PID_Init()
if (pid.Kd < 1e-7) { pid.Kd = 340; }
if (pid.tem_threshold < 0.0001) { pid.tem_threshold = 0.2; }
pid.t = 5000; // PID calc period
pid.t = 1000; // PID calc period
// pid.Ti=5000000;// integral time
// pid.Td=1000;// differential time
pid.pwmcycle = 200; // pwm cycle 200
@ -50,6 +50,7 @@ void set_compressor_power(int speed) {
GetCRC16(data, 6, data + 6, data + 7);
RS485_3_Init(9600);
delay_xms(30);
RS485_3_Send_Data(data, 8);
delay_xms(30);
RS485_1_Init(9600);
@ -72,6 +73,8 @@ void set_heater_power(int percent) {
GetCRC16(data, 6, data + 6, data + 7);
RS485_1_Init(9600);
delay_xms(30);
RS485_1_Send_Data(data, 8);
delay_xms(30);
}
@ -79,7 +82,7 @@ void set_heater_power(int percent) {
/**
* heater power calc
*/
int calc_p(float t_t, float t_c, float error, int p_b, float pid_p, float pid_i) {
int calc_hp(float t_t, float t_c, float error, int p_b, float pid_p, float pid_i) {
int p = p_b + pid_p * (t_t - t_c) + pid_i * error;
if (p > 100) {
return 100;
@ -90,33 +93,58 @@ int calc_p(float t_t, float t_c, float error, int p_b, float pid_p, float pid_i)
return p;
}
/**
* compressor power percent calc
*/
int calc_cp(float t_t, float t_c, int p_cb, float pid_cp) {
int percent = p_cb + pid_cp * (t_c - t_t);
if (percent > 100) {
return 100;
}
if (percent < 0) {
return 0;
}
return percent;
}
/**
* compressor speed calc
*/
int calc_compressor_speed(int percent, int v_min, int v_max) {
int v = percent * v_max / 100.0;
if (v > v_max) {
return v_max;
}
if (v < v_min) {
return v_min;
}
return v;
}
void PID_Calc() // pid calc
{
pid.Kp = 19.2;
pid.Ki = 0.02;
float Kp = 19.2;
float Ki = 0.02;
int p_base = 52;
float DelEk; // The difference between the last two deviations
// float td;
float out;
if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived
{
return;
}
// if (pid.set_tem > pid.now_tem)
// if (pid.C1ms < (pid.t)) // The calculation cycle has not yet arrived
// {
// pid.Ek = pid.set_tem - pid.now_tem;
// return;
// }
// else
// {
// 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.SEk += pid.Ek; // Total historical deviation
// SEk limit
if (pid.SEk < - p_base / Ki) {
pid.SEk = - p_base / Ki;
}
DelEk = pid.Ek - pid.Ek_1; // The difference between the last two deviations
// ti=pid.t/pid.Ti;
@ -151,74 +179,16 @@ void PID_Calc() // pid calc
pid.C1ms = 0;
// heater percent
int heater_percent = calc_p(pid.set_tem, pid.now_tem, pid.SEk, p_base, pid.Kp, pid.Ki);
int heater_percent = calc_hp(pid.set_tem, pid.now_tem, pid.SEk, p_base, Kp, Ki);
// 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;
// if (speed_count > 6000) {
// speed_count = 6000;
// }
int speed_count = 1500;
// if (pid.now_tem < pid.set_tem + pid.tem_offset - pid.tem_threshold)
// {
// // 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
// HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04
// speed_count = 1500;
// hot_clod_flag = 2;
// 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)
// {
// HC595_Send_Byte(gpio_state &= 0xFB); // close heater &=1111 1011 0xFB
// speed_count = 1000; // close compressor
// hot_clod_flag = 0;
// // pid.Iout=0;
// } else if (pid.now_tem > pid.set_tem + pid.tem_offset + pid.tem_threshold)
// {
// // 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 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;
// }
int p_c = calc_cp(pid.set_tem, pid.now_tem, 32, Kp);
int speed_count = calc_compressor_speed(p_c, 1500, 4500);
// TODO:: temply, set Ki to speed count, Kd to heater_percent, use for data upload
pid.Ki = speed_count;
pid.Kd = heater_percent;
set_compressor_power(speed_count);
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
// {
// HC595_Send_Byte(gpio_state&=0xDB);// close compressor and heater &=1101 1011 0xDB
// // num = 0;
// // TIM_SetCompare3(TIM3, 0); // close compressor
// speed_count = 1000;
// hot_clod_flag = 0;
// }
// if (hot_clod_flag == 2 && T >= tem) // while heat, T above tem
// {
// HC595_Send_Byte(gpio_state&=0xDB);//close compressor and heater &=1101 1011 0xDB
// num = 0;
// TIM_SetCompare3(TIM3, 0); // close compressor
// hot_clod_flag = 0;
// }
// HC595_Send_Byte(gpio_state&=0xDB);// close compressor and heater &=1101 1011 0xDB
}

@ -463,10 +463,10 @@ void RS485_1_Send_Data_2(void)
// sendbuf[28] = (int)pid.Kd % 256; // Td 340
// TODO::temp
sendbuf[23] = (int)pid.Ek / 256; // Ek
sendbuf[24] = (int)pid.Ek % 256;
sendbuf[25] = (int)pid.SEk / 256; // SEk
sendbuf[26] = (int)pid.SEk % 256;
sendbuf[23] = (int)pid.SEk / 256; // SEk
sendbuf[24] = (int)pid.SEk % 256;
sendbuf[25] = (int)pid.Ki / 256; // compressor speed
sendbuf[26] = (int)pid.Ki % 256;
sendbuf[27] = (int)pid.Kd / 256; // heater percent
sendbuf[28] = (int)pid.Kd % 256;

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project_projx.xsd">
<SchemaVersion>2.1</SchemaVersion>
<Header>### uVision Project, (C) Keil Software</Header>
<Targets>
<Target>
<TargetName>control</TargetName>
@ -10,33 +13,33 @@
<pCCUsed>5060422::V5.06 update 4 (build 422)::ARMCC</pCCUsed>
<TargetOption>
<TargetCommonOption>
<Device/>
<Vendor/>
<Device>STM32F103C8</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32F1xx_DFP.2.3.0</PackID>
<PackURL>http://www.keil.com/pack/</PackURL>
<Cpu>IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00010000) CPUTYPE(&quot;Cortex-M3&quot;) CLOCK(12000000) ELITTLE</Cpu>
<FlashUtilSpec/>
<StartupFile/>
<Cpu>IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00010000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
<FlashDriverDll>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM))</FlashDriverDll>
<DeviceId>0</DeviceId>
<RegisterFile>$$Device:STM32F103C8$Device\Include\stm32f10x.h</RegisterFile>
<MemoryEnv/>
<Cmp/>
<Asm/>
<Linker/>
<OHString/>
<InfinionOptionDll/>
<SLE66CMisc/>
<SLE66AMisc/>
<SLE66LinkerMisc/>
<MemoryEnv></MemoryEnv>
<Cmp></Cmp>
<Asm></Asm>
<Linker></Linker>
<OHString></OHString>
<InfinionOptionDll></InfinionOptionDll>
<SLE66CMisc></SLE66CMisc>
<SLE66AMisc></SLE66AMisc>
<SLE66LinkerMisc></SLE66LinkerMisc>
<SFDFile>$$Device:STM32F103C8$SVD\STM32F103xx.svd</SFDFile>
<bCustSvd>0</bCustSvd>
<UseEnv>0</UseEnv>
<BinPath/>
<IncludePath/>
<LibPath/>
<RegisterFilePath/>
<DBRegisterFilePath/>
<BinPath></BinPath>
<IncludePath></IncludePath>
<LibPath></LibPath>
<RegisterFilePath></RegisterFilePath>
<DBRegisterFilePath></DBRegisterFilePath>
<TargetStatus>
<Error>0</Error>
<ExitCodeStop>0</ExitCodeStop>
@ -58,8 +61,8 @@
<BeforeCompile>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name/>
<UserProg2Name/>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
<nStopU1X>0</nStopU1X>
@ -68,8 +71,8 @@
<BeforeMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name/>
<UserProg2Name/>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
<nStopB1X>0</nStopB1X>
@ -86,7 +89,7 @@
<nStopA2X>0</nStopA2X>
</AfterMake>
<SelectedForBatchBuild>0</SelectedForBatchBuild>
<SVCSIdString/>
<SVCSIdString></SVCSIdString>
</TargetCommonOption>
<CommonProperty>
<UseCPPCompiler>0</UseCPPCompiler>
@ -100,17 +103,17 @@
<AssembleAssemblyFile>0</AssembleAssemblyFile>
<PublicsOnly>0</PublicsOnly>
<StopOnExitCode>3</StopOnExitCode>
<CustomArgument/>
<IncludeLibraryModules/>
<CustomArgument></CustomArgument>
<IncludeLibraryModules></IncludeLibraryModules>
<ComprImg>1</ComprImg>
</CommonProperty>
<DllOption>
<SimDllName>SARMCM3.DLL</SimDllName>
<SimDllArguments>-REMAP</SimDllArguments>
<SimDllArguments> -REMAP</SimDllArguments>
<SimDlgDll>DCM.DLL</SimDlgDll>
<SimDlgDllArguments>-pCM3</SimDlgDllArguments>
<TargetDllName>SARMCM3.DLL</TargetDllName>
<TargetDllArguments/>
<TargetDllArguments></TargetDllArguments>
<TargetDlgDll>TCM.DLL</TargetDlgDll>
<TargetDlgDllArguments>-pCM3</TargetDlgDllArguments>
</DllOption>
@ -134,11 +137,11 @@
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2CM3.DLL</Flash2>
<Flash3/>
<Flash4/>
<pFcarmOut/>
<pFcarmGrp/>
<pFcArmRoot/>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>
<pFcArmRoot></pFcArmRoot>
<FcArmLst>0</FcArmLst>
</Utilities>
<TargetArmAds>
@ -170,8 +173,8 @@
<AdsLsxf>1</AdsLsxf>
<RvctClst>0</RvctClst>
<GenPPlst>0</GenPPlst>
<AdsCpuType>&quot;Cortex-M3&quot;</AdsCpuType>
<RvctDeviceName/>
<AdsCpuType>"Cortex-M3"</AdsCpuType>
<RvctDeviceName></RvctDeviceName>
<mOS>0</mOS>
<uocRom>0</uocRom>
<uocRam>0</uocRam>
@ -179,7 +182,7 @@
<hadIRAM>1</hadIRAM>
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>1</RvdsVP>
<RvdsVP>0</RvdsVP>
<hadIRAM2>0</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -303,7 +306,7 @@
<Size>0x0</Size>
</OCR_RVCT10>
</OnChipMemories>
<RvctStartVector/>
<RvctStartVector></RvctStartVector>
</ArmAdsMisc>
<Cads>
<interw>1</interw>
@ -329,9 +332,9 @@
<v6WtE>0</v6WtE>
<v6Rtti>0</v6Rtti>
<VariousControls>
<MiscControls/>
<MiscControls></MiscControls>
<Define>STM32F10X_MD,USE_STDPERIPH_DRIVER</Define>
<Undefine/>
<Undefine></Undefine>
<IncludePath>.;..\CORE;..\STM32F10x_FWLib\inc;..\HARDWARE;..\SYSTEM;..\dhcp;..\dns;..\md5;..\MQTT;..\MQTT\lib;..\w5500;..\FreeRTOS\include;..\FreeRTOS\portable\RVDS\ARM_CM3;..\myfreertos;RTE\_LED</IncludePath>
</VariousControls>
</Cads>
@ -347,10 +350,10 @@
<useXO>0</useXO>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls/>
<MiscControls></MiscControls>
<Define>STM32F10X_MD,USE_STDPERIPH_DRIVER</Define>
<Undefine/>
<IncludePath/>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
</Aads>
<LDads>
@ -362,13 +365,13 @@
<useFile>0</useFile>
<TextAddressRange>0x08000000</TextAddressRange>
<DataAddressRange>0x20000000</DataAddressRange>
<pXoBase/>
<pXoBase></pXoBase>
<ScatterFile>../OBJ/LED.sct</ScatterFile>
<IncludeLibs/>
<IncludeLibsPath/>
<Misc/>
<LinkerInputFile/>
<DisabledWarnings/>
<IncludeLibs></IncludeLibs>
<IncludeLibsPath></IncludeLibsPath>
<Misc></Misc>
<LinkerInputFile></LinkerInputFile>
<DisabledWarnings></DisabledWarnings>
</LDads>
</TargetArmAds>
</TargetOption>
@ -1076,9 +1079,11 @@
</Groups>
</Target>
</Targets>
<RTE>
<apis/>
<components/>
<files/>
</RTE>
</Project>

@ -202,10 +202,12 @@ void SyncEnvironmentDataRequestTask(void *pvParameters) {
int hot = 0;
/**
* 20%1min,10
* pid5
*/
void HotTestRequestTask(void *pvParameters) {
while (1) {
PID_Calc();
// if (T >= 350) {
// hot = 0;
// hot_clod_flag = 0;
@ -225,11 +227,11 @@ void HotTestRequestTask(void *pvParameters) {
// GetCRC16(temp_data, 6, temp_data + 6, temp_data + 7);
// // RS485_3_Init(9600);
// RS485_1_Send_Data(temp_data, 8);
// RS485_3_Init(9600);
// RS485_3_Send_Data(temp_data, 8);
// // RS485_1_Send_Data(temp_data, 8);
// delay_xms(30);
// // RS485_1_Init(9600);
// RS485_1_Init(9600);
// HC595_Send_Byte(gpio_state |= 0x04); // open heater |=0000 0100 0x04
@ -252,7 +254,7 @@ void HotTestRequestTask(void *pvParameters) {
// // HC595_Send_Byte(gpio_state &= 0xFB); // close heater &=1111 1011 0xFB
// hot_clod_flag = 0;
vTaskDelay(1000 * 10);
vTaskDelay(5000);
}
}
@ -264,6 +266,17 @@ void Sensor_Communication_task(void *pvParameters)
while (1)
{
// RS485_3_Init(9600);
// // temp[2] = (int)pid.Ki / 256;
// // temp[3] = (int)pid.Ki % 256;
// temp[2] = 4800 / 256;
// temp[3] = 4800 % 256;
// RS485_3_Send_Data(temp, 4);
// // RS485_1_Send_Data(temp_data, 8);
// delay_xms(30);
// RS485_1_Init(9600);
// RS485_1_Send_Data(temp, 4);
// HC595_Send_Byte(0x00);
// cnt_flag=do_mqtt(MQTT_SOCK,NET_CONFIG.rip,1883,5500);
// Humidity=H*0.1;
@ -479,21 +492,28 @@ void process_sensor_data(u8 *data) {
gpio_state &= 0xFE;
ALARM = 0;
}
if (T < 600 && T > -400)
{
diff = current_T - T;
if (diff < 0)
diff = -diff;
if (diff < 100 || current_T == 0) // 最近两次温差相差10℃,消除传感器感应出错的影响
{
// if (T < 800 && T > -400)
// {
// diff = current_T - T;
// if (diff < 0)
// diff = -diff;
// // if (diff < 100 || current_T == 0) // 最近两次温差相差10℃,消除传感器感应出错的影响
// // {
// pid.set_tem = tem / 10.0;
// pid.now_tem = T / 10.0;
// PID_Calc();
// // send sign to 485
// // out: 0-200
// current_T = T;
// // }
// }
pid.set_tem = tem / 10.0;
pid.now_tem = T / 10.0;
PID_Calc();
// PID_Calc();
// send sign to 485
// out: 0-200
current_T = T;
}
}
if (H > (hum + 50)) // 湿度高于设定值5打开新风风扇 GPIO4->PD1
{

Loading…
Cancel
Save