Zeng wei (曾威) 2 years ago
parent de697a4704
commit e26d42517e

@ -1,6 +1,7 @@
#include "PID.h"
#include "Relays.h"
#include "USART.h"
#include "rs485.h"
extern u16 tem;
float cold_tem = 0;
float red_tem = 0;
@ -15,7 +16,6 @@ PID pid;
u8 rs485speed[8] = {0x01, 0x06, 0x60, 0x00, 0x00, 0x09, 0xBB, 0xAA}; // speed control for compressor controller
int min_speed_count = 1500;
int max_speed_count = 6000;
float tem_control_threshold = 0.5;
void PID_Init()
{
@ -24,6 +24,7 @@ void PID_Init()
if (pid.Kp < 1e-7) { pid.Kp = 40; }
if (pid.Ki < 1e-7) { pid.Ki = 0.001; }
if (pid.Kd < 1e-7) { pid.Kd = 340; }
if (pid.tem_threshold < 0.0001) { pid.tem_threshold = 0.5; }
pid.t = 500; // PID calc period
// pid.Ti=5000000;// integral time
@ -109,7 +110,7 @@ void PID_Calc() // pid calc
speed_count = 6000;
}
if (pid.now_tem < pid.set_tem - tem_control_threshold)
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
@ -120,14 +121,14 @@ void PID_Calc() // pid calc
speed_count = 1000; // close compressor
hot_clod_flag = 2;
pid.Iout = 0;
} else if (pid.now_tem > pid.set_tem - tem_control_threshold && pid.now_tem < pid.set_tem + tem_control_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)
{
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 + tem_control_threshold)
} 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

@ -35,6 +35,10 @@ typedef struct Pid
int pwmcycle;// PWM cycle
int times;
float tem_offset;
float tem_threshold;
}PID;
extern PID pid;

@ -41,14 +41,16 @@ void RX_BUF_Transfer(u8 zero,u8 transfer_num)//
for (i = zero; i < 128 - transfer_num; i++)
{
// RS485_RX_BUF[i] = RS485_RX_BUF[i + transfer_num];
RS485_RX_BUF_COPY[i] = RS485_RX_BUF_COPY[i + transfer_num];
}
for (i = 128 - transfer_num; i < 128; i++)
{
// RS485_RX_BUF[i] = 0;
RS485_RX_BUF_COPY[i] = 0;
}
//RS485_RX_CNT -= transfer_num;
// RS485_RX_CNT -= transfer_num;
}

@ -4,10 +4,10 @@
#include "Relays.h"
#include "myfreertos.h"
u8 prev_sendbuf[37] = {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, 0xFF, 0xFC, 0xFF, 0xFF};
u8 sendbuf[37] = {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, 0xFF, 0xFC, 0xFF, 0xFF};
u8 prev_sendbuf[41] = {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, 0xFF, 0xFC, 0xFF, 0xFF};
u8 sendbuf[41] = {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, 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];
@ -382,11 +382,11 @@ void RS485_1_Send_Data_2(void)
sendbuf[0] = 0xEE;
sendbuf[1] = 0xB5;
sendbuf[2] = 0x05;
sendbuf[33] = 0xFF;
sendbuf[34] = 0xFC;
sendbuf[35] = 0xFF;
sendbuf[36] = 0xFF;
u8 a;
sendbuf[37] = 0xFF;
sendbuf[38] = 0xFC;
sendbuf[39] = 0xFF;
sendbuf[40] = 0xFF;
// u8 a;
if (T <= 1000 && T >= -1000)
{
if (T < 0) {
@ -459,6 +459,7 @@ void RS485_1_Send_Data_2(void)
sendbuf[26] = ((int)(pid.Ki * 100000)) % 256; // Ti 0.001
sendbuf[27] = (int)pid.Kd / 256; // Td 340
sendbuf[28] = (int)pid.Kd % 256; // Td 340
// num = (((pid.OUT * 350) / pid.pwmcycle) - 1);
int out1 = pid.OUT;
@ -475,16 +476,29 @@ void RS485_1_Send_Data_2(void)
// bufcut_Init(sendbuf_crc, sendbuf, 3, 31);
// GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2);
GetCRC16(sendbuf, 31, &crc_num1, &crc_num2);
sendbuf[31] = crc_num1;
sendbuf[32] = crc_num2;
int tem_offset_10times = (int)(pid.tem_offset * 10);
if (tem_offset_10times < 0) {
tem_offset_10times = (~(tem_offset_10times - 1)) & 0xFFFF;
}
sendbuf[31] = tem_offset_10times / 256; // tem offset
sendbuf[32] = tem_offset_10times % 256;
sendbuf[33] = ((int)(pid.tem_threshold * 10)) / 256; // tem threshold
sendbuf[34] = ((int)(pid.tem_threshold * 10)) % 256;
GetCRC16(sendbuf, 35, &crc_num1, &crc_num2);
sendbuf[35] = crc_num1;
sendbuf[36] = crc_num2;
// only when modify happen, then send
if (cmp_str(prev_sendbuf, sendbuf, 37) != 0) {
if (cmp_str(prev_sendbuf, sendbuf, 41) != 0) {
RS485_1_Send_Data(sendbuf, 37);
RS485_1_Send_Data(sendbuf, 41);
cp_str_to_prev(prev_sendbuf, sendbuf, 37);
cp_str_to_prev(prev_sendbuf, sendbuf, 41);
}
}

@ -11,7 +11,7 @@
extern u8 RS485_RX_BUF[128]; //
extern u8 store_stage;
u8 read_stage = 1; // 从flash读取出来的阶段号
u8 read_stage = 1; // stage from flash
extern u8 now_stage;
extern u8 chour;
extern u8 cminute;
@ -46,8 +46,8 @@ void Array(u8 *n, u8 *hour, u8 *min, u16 *tem, u16 *hum, u16 *red, u16 *blue, u1
uint8_t ArrayRead[108];
uint8_t ArrayWrite[108];
uint8_t pidsRead[6];
uint8_t pidsWrite[6];
uint8_t pidsRead[10];
uint8_t pidsWrite[10];
void Write_Init(void)
{
@ -68,34 +68,59 @@ void Write_Init(void)
pidsWrite[3] = ((int)(pid.Ki * 100000)) % 256; // Ti 0.001
pidsWrite[4] = (int)pid.Kd / 256; // Td 340
pidsWrite[5] = (int)pid.Kd % 256; // Td 340
int tem_offset_10times = (int)(pid.tem_offset * 10);
if (tem_offset_10times < 0) {
tem_offset_10times = (~(tem_offset_10times - 1)) & 0xFFFF;
}
pidsWrite[6] = tem_offset_10times / 256;
pidsWrite[7] = tem_offset_10times % 256;
pidsWrite[8] = ((int)(pid.tem_threshold * 10)) / 256;
pidsWrite[9] = ((int)(pid.tem_threshold * 10)) % 256;
// // wtite p i d
W25QXX_Write(pidsWrite, 0x002000, 6); // p i d, 2bytes for each
W25QXX_Write(pidsWrite, 0x002000, 10); // p i d tem_offset tem_threshold, 2bytes for each
W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来
W25QXX_Read(ArrayRead, 0x000000, 108);
}
void Read_Init(void)
{
W25QXX_Init();
W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来
bufcut_Init(total, ArrayRead, 0, 108); // 把读出来的数据赋值给total数组
W25QXX_Read(ArrayRead, 0x000000, 108);
bufcut_Init(total, ArrayRead, 0, 108);
W25QXX_Read(&read_stage, 0x001000, 1);
now_stage = read_stage; // 读出来的阶段赋值给当前阶段
now_stage = read_stage;
// if (now_stage <= 0 || now_stage > 6) {
// now_stage = 3;
// }
// now_stage = 3;
// read p i d
W25QXX_Read(pidsRead, 0x002000, 6); // p i d, 2bytes for each
W25QXX_Read(pidsRead, 0x002000, 8); // p i d tem_offset tem_threshold, 2bytes for each
float Kp = pidsRead[0] * 256.0 + pidsRead[1];
float Ki = (pidsRead[2] * 256.0 + pidsRead[3]) / 100000.0;
float Kd = pidsRead[4] * 256.0 + pidsRead[5];
int tem_offset_10times = pidsRead[6] * 256.0 + pidsRead[7];
// Negative tem offset treatment
if (tem_offset_10times & 0x8000)
{
tem_offset_10times = ((~tem_offset_10times + 1) & 0xFFFF);
}
float tem_offset = tem_offset_10times / 10.0;
float tem_threshold = (pidsRead[8] * 256.0 + pidsRead[9]) / 10.0;
// pid.Kp = 120;
if (Kp > 1e-3) { pid.Kp = Kp; }
if (Ki > 1e-3) { pid.Ki = Ki; }
if (Kd > 1e-3) { pid.Kd = Kd; }
pid.tem_offset = tem_offset;
if (tem_threshold > 1e-3) { pid.tem_threshold = tem_threshold; }
// pid.Ki = 0.002;
// pid.Kd = 300;
// pid.Kp = pidsRead[0] * 256.0 + pidsRead[1];
@ -112,7 +137,7 @@ void Read_Init(void)
// }
}
void Analysis(u8 *n, u16 *i, u8 *rs485_rx_buf) //(阶段号,数组值序号,)解析函数
void Analysis(u8 *n, u16 *i, u8 *rs485_rx_buf)
{
*n = rs485_rx_buf[3];
*i = rs485_rx_buf[4];
@ -132,7 +157,7 @@ void Analysis(u8 *n, u16 *i, u8 *rs485_rx_buf) //(
}
}
void Batch_synchronization(u8 *n, u8 *rs485_rx_buf) // 批量同步
void Batch_synchronization(u8 *n, u8 *rs485_rx_buf) // batch sync
{
*n = rs485_rx_buf[3];

File diff suppressed because it is too large Load Diff

@ -27,7 +27,7 @@ void Sensor_Communication_task(void *pvParameters);
void Host_Computer_Communication(void);
// 10seconds delay
// 5seconds delay
#define SENSOR_DATA_REQUEST_PERIOD pdMS_TO_TICKS(5000)
// 60seconds delay
@ -176,6 +176,8 @@ void SensorDataRequestTask(void *pvParameters) {
while (1) {
// 发送传感器数据请求的操作
RS485_1_Send_Data_1(rs485buf, 8);
// delay_xms(100);
// RS485_1_Send_Data_1(RS485_RX_BUF, 48);
vTaskDelay(SENSOR_DATA_REQUEST_PERIOD);
}
@ -192,6 +194,7 @@ void SyncEnvironmentDataRequestTask(void *pvParameters) {
u8 temp[4] = {0xaa,0xbb,0xcc,0xdd};
u8 times = 0;
void Sensor_Communication_task(void *pvParameters)
{
@ -215,7 +218,7 @@ void Sensor_Communication_task(void *pvParameters)
now_stage = timelong_Compare(); //
// printf("%d %d\r\n",chour,cminute);
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // 更新对应阶段号的参数
printf("current stage: %d\r\n",now_stage);
// printf("current stage: %d\r\n",now_stage);
// RS485_1_Send_Data_1(rs485buf, 8); //(RS485_RX_CNT=0已被注释)
// delay_xms(50);
@ -233,12 +236,19 @@ void Sensor_Communication_task(void *pvParameters)
// }
delay_xms(50);
// RX_BUF_Printf(128);
// if (RS485_RX_CNT == 0) {
// continue;
// }
bufcut_Init(RS485_RX_BUF_COPY, RS485_RX_BUF, 0, 128);
u8 c = 0;
RS485_RX_CNT = 0;
while (isAllZeros(RS485_RX_BUF_COPY, 128) == 0)
// while (RS485_RX_CNT > 0)
{
// bufcut_Init(RS485_RX_BUF_COPY, RS485_RX_BUF, 0, 128);
if (RS485_RX_BUF_COPY[0] == 0x02 && RS485_RX_BUF_COPY[1] == 0x03 && CRC16_check(RS485_RX_BUF_COPY, 21) == 1)
{
c++;
// receive message from sensor
u8 temp_data[23] = {0};
for (int i=0;i<23;i++) {
@ -246,6 +256,8 @@ void Sensor_Communication_task(void *pvParameters)
}
process_sensor_data(temp_data);
RX_BUF_Transfer(0, 23);
// u8 tmpaaa[] = {c};
// RS485_1_Send_Data_1(tmpaaa, 1);
}
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x01 && CRC16_check(RS485_RX_BUF_COPY, 16) == 1)
{
@ -287,16 +299,15 @@ void Sensor_Communication_task(void *pvParameters)
bufcut_Init(RS485_DATA_TMP, RS485_RX_BUF_COPY, 0, 6);
bufcut_Init(RS485_DATA_TMP + 8, RS485_SUFFIX, 6, 10);
RS485_1_Send_Data_1(RS485_DATA_TMP, 10);
delay_ms(10);
// delay_ms(10);
RS485_1_Send_Data_2(); // 上传参数
// }
RX_BUF_Transfer(0, 6);
}
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x05 && CRC16_check(RS485_RX_BUF_COPY, 4) == 1)
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x05)
{
// change pid params
// total length: 8
// if (CRC16_check(RS485_RX_BUF_COPY, 4) == 1) {
int param_index = RS485_RX_BUF_COPY[3];
switch(param_index) {
case 0x01:
@ -314,12 +325,27 @@ void Sensor_Communication_task(void *pvParameters)
pid.Kd = RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5];
break;
}
case 0x04:
{
int tem_offset_10times = RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5];
// Negative tem offset treatment
if (tem_offset_10times & 0x8000)
{
tem_offset_10times = ((~tem_offset_10times + 1) & 0xFFFF);
}
pid.tem_offset = tem_offset_10times / 10.0;
break;
}
case 0x05:
{
pid.tem_threshold = (RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5]) / 10.0;
break;
}
}
Write_Init();
bufcut_Init(RS485_DATA_TMP, RS485_RX_BUF_COPY, 0, 8);
bufcut_Init(RS485_DATA_TMP + 8, RS485_SUFFIX, 8, 12);
RS485_1_Send_Data_1(RS485_DATA_TMP, 12);
// }
RX_BUF_Transfer(0, 8);
}
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x11 && RS485_RX_BUF_COPY[15] == 0xFF && RS485_RX_BUF[16] == 0xFF)
@ -451,7 +477,7 @@ void process_sensor_data(u8 *data) {
HC595_Send_Byte(gpio_state);
RS485_1_Send_Data_2(); // 上传参数
delay_xms(200);
// delay_xms(200);
}
/**
@ -468,7 +494,7 @@ void process_hmi_btn_event(u8 page, u8 btn_index, u8 value) {
now_stage = target_stage;
store_stage = target_stage;
Write_Init();
delay_ms(10);
// delay_ms(10);
RS485_1_Send_Data_2(); // 上传参数
}
break;

Loading…
Cancel
Save