289 lines
9.8 KiB
C
289 lines
9.8 KiB
C
#include "app_config.h"
|
|
#include "interface.h"
|
|
#include "app_frm_monitor.h"
|
|
#include "app_frm_signal.h"
|
|
#include "app_frm_timer.h"
|
|
#include "app_param_manage.h"
|
|
|
|
#include "app_temp.h"
|
|
|
|
|
|
// 声明 temp_data 变量
|
|
TempSystem temp_data;
|
|
|
|
|
|
static void handleTemperatureAlarm(int16_t current_temp, float alarm_temp,
|
|
float critical_temp, float threshold_temp,
|
|
TempState *state)
|
|
{
|
|
switch (*state)
|
|
{
|
|
case TEMP_NORMAL:
|
|
// 从正常状态进入警告状态的条件
|
|
if (current_temp > (alarm_temp + threshold_temp))
|
|
{
|
|
*state = TEMP_WARNING;
|
|
printf("Temperature Warning: Activated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
// 从正常状态直接进入严重状态的条件
|
|
else if (current_temp > (critical_temp + threshold_temp))
|
|
{
|
|
*state = TEMP_CRITICAL;
|
|
printf("Temperature Critical: Activated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
else
|
|
{
|
|
*state = TEMP_NORMAL;
|
|
}
|
|
break;
|
|
|
|
case TEMP_WARNING:
|
|
// 从警告状态返回正常状态的条件
|
|
if (current_temp < (alarm_temp - threshold_temp))
|
|
{
|
|
*state = TEMP_NORMAL;
|
|
printf("Temperature Warning: Deactivated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
// 从警告状态进入严重状态的条件
|
|
else if (current_temp > (critical_temp + threshold_temp))
|
|
{
|
|
*state = TEMP_CRITICAL;
|
|
printf("Temperature Critical: Activated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
else
|
|
{
|
|
*state = TEMP_WARNING;
|
|
}
|
|
break;
|
|
|
|
case TEMP_CRITICAL:
|
|
// 从严重状态返回警告状态的条件
|
|
if (current_temp < (critical_temp - threshold_temp))
|
|
{
|
|
*state = TEMP_WARNING;
|
|
printf("Temperature Critical: Deactivated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
// 从严重状态直接返回正常状态的条件
|
|
else if (current_temp < (alarm_temp - threshold_temp))
|
|
{
|
|
*state = TEMP_NORMAL;
|
|
printf("Temperature Warning: Deactivated! Current temp: %d°C\n", current_temp);
|
|
}
|
|
else
|
|
{
|
|
*state = TEMP_CRITICAL;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
*state = TEMP_NORMAL;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
// 温度输出处理函数
|
|
static void tempOutput(void *signal_id)
|
|
{
|
|
(void)signal_id;
|
|
|
|
// 电机1风扇 左前
|
|
switch (temp_data.state[0])
|
|
{
|
|
case TEMP_NORMAL:
|
|
un_inf_can_kgf_output1.bit_data.KGF01 = setFanOff();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_01 = 0;
|
|
break;
|
|
case TEMP_WARNING:
|
|
un_inf_can_kgf_output1.bit_data.KGF01 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_01 = 5;
|
|
break;
|
|
case TEMP_CRITICAL:
|
|
un_inf_can_kgf_output1.bit_data.KGF01 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_01 = 0;
|
|
break;
|
|
}
|
|
|
|
// 电机2风扇 右前
|
|
switch (temp_data.state[1])
|
|
{
|
|
case TEMP_NORMAL:
|
|
un_inf_can_kgf_output1.bit_data.KGF02 = setFanOff();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_02 = 0;
|
|
break;
|
|
case TEMP_WARNING:
|
|
un_inf_can_kgf_output1.bit_data.KGF02 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_02 = 5;
|
|
break;
|
|
case TEMP_CRITICAL:
|
|
un_inf_can_kgf_output1.bit_data.KGF02 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output1.bit_data.pwm_02 = 0;
|
|
break;
|
|
}
|
|
|
|
|
|
// 电机3风扇 左后
|
|
switch (temp_data.state[2])
|
|
{
|
|
case TEMP_NORMAL:
|
|
un_inf_can_kgf_output2.bit_data.KGF07 = setFanOff();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_07 = 0;
|
|
break;
|
|
case TEMP_WARNING:
|
|
un_inf_can_kgf_output2.bit_data.KGF07 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_07 = 5;
|
|
break;
|
|
case TEMP_CRITICAL:
|
|
un_inf_can_kgf_output2.bit_data.KGF07 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_07 = 0;
|
|
break;
|
|
}
|
|
|
|
// 电机4风扇 右后
|
|
switch (temp_data.state[3])
|
|
{
|
|
case TEMP_NORMAL:
|
|
un_inf_can_kgf_output2.bit_data.KGF08 = setFanOff();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_08 = 0;
|
|
break;
|
|
case TEMP_WARNING:
|
|
un_inf_can_kgf_output2.bit_data.KGF08 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_08 = 5;
|
|
break;
|
|
case TEMP_CRITICAL:
|
|
un_inf_can_kgf_output2.bit_data.KGF08 = setFanOn();//电机控制器风扇
|
|
un_inf_can_kgf_output2.bit_data.pwm_08 = 0;
|
|
break;
|
|
}
|
|
|
|
// // 电机3风扇
|
|
// switch (temp_data.state[2])
|
|
// {
|
|
// case TEMP_NORMAL:
|
|
// un_inf_can_kgf_output1.bit_data.KGF01 = setFanOff();//电机控制器风扇
|
|
// un_inf_can_kgf_output1.bit_data.pwm_01 = 0;
|
|
// break;
|
|
// case TEMP_WARNING:
|
|
// un_inf_can_kgf_output1.bit_data.KGF01 = setFanOn();//电机控制器风扇
|
|
// un_inf_can_kgf_output1.bit_data.pwm_01 = 5;
|
|
// break;
|
|
// case TEMP_CRITICAL:
|
|
// un_inf_can_kgf_output1.bit_data.KGF01 = setFanOn();//电机控制器风扇
|
|
// un_inf_can_kgf_output1.bit_data.pwm_01 = 0;
|
|
// break;
|
|
// }
|
|
|
|
publishMessage(&un_inf_can_kgf_output1, 1);
|
|
}
|
|
|
|
// 温度状态处理函数
|
|
static void tempProcess(void *signal_id)
|
|
{
|
|
(void)signal_id;
|
|
int16_t max_temp[4] = {0,0};
|
|
|
|
max_temp[0] = temp_data.current_temp[0];
|
|
max_temp[1] = temp_data.current_temp[1];
|
|
max_temp[2] = temp_data.current_temp[2];
|
|
max_temp[3] = temp_data.current_temp[3];
|
|
|
|
|
|
// printf("motor1 temp: %d, motor2 temp: %d\n", max_temp[0], max_temp[1]);
|
|
handleTemperatureAlarm(max_temp[0], MOTOR_WARNING_TEMP, MOTOR_CRITICAL_TEMP, MOTOR_THRESHOLD_TEMP, &temp_data.state[0]);
|
|
|
|
handleTemperatureAlarm(max_temp[1], MOTOR_WARNING_TEMP, MOTOR_CRITICAL_TEMP, MOTOR_THRESHOLD_TEMP, &temp_data.state[1]);
|
|
|
|
handleTemperatureAlarm(max_temp[2], MOTOR_WARNING_TEMP, MOTOR_CRITICAL_TEMP, MOTOR_THRESHOLD_TEMP, &temp_data.state[2]);
|
|
|
|
handleTemperatureAlarm(max_temp[3], MOTOR_WARNING_TEMP, MOTOR_CRITICAL_TEMP, MOTOR_THRESHOLD_TEMP, &temp_data.state[3]);
|
|
|
|
|
|
|
|
// if (max_temp[0] >= 60) // 假设60度为危险温度
|
|
// {
|
|
// temp_data.state[0] = TEMP_CRITICAL;
|
|
// }
|
|
// else if (max_temp[0] >= 40) // 假设40度为警告温度
|
|
// {
|
|
// temp_data.state[0] = TEMP_WARNING;
|
|
// }
|
|
// else
|
|
// {
|
|
// temp_data.state[0] = TEMP_NORMAL;
|
|
// }
|
|
//
|
|
//
|
|
// max_temp[1] = temp_data.current_temp[1];
|
|
// if (max_temp[1] >= 60) // 假设60度为危险温度
|
|
// {
|
|
// temp_data.state[1] = TEMP_CRITICAL;
|
|
// }
|
|
// else if (max_temp[1] >= 40) // 假设40度为警告温度
|
|
// {
|
|
// temp_data.state[1] = TEMP_WARNING;
|
|
// }
|
|
// else
|
|
// {
|
|
// temp_data.state[1] = TEMP_NORMAL;
|
|
// }
|
|
//
|
|
//// printf("motor1 temp: %d, motor2 temp: %d\n", max_temp[0], max_temp[1]);
|
|
//// printf("motor1 state: %d, motor2 state: %d\n", temp_data.state[0], temp_data.state[1]);
|
|
|
|
tempOutput(NULL);
|
|
|
|
timerStart(&temp_data.timer, 1000, 1); //1s
|
|
}
|
|
|
|
// 处理输入信号的函数
|
|
static void tempInput(void *signal_id)
|
|
{
|
|
(void)signal_id;
|
|
// 填充数据
|
|
if (signal_id == &un_motor_temp1)
|
|
{
|
|
temp_data.current_temp[0] = (int16_t)( (un_motor_temp1.bit_data.controller_temp) - 40 );//40偏移量
|
|
}
|
|
else if(signal_id == &un_motor_temp2)
|
|
{
|
|
temp_data.current_temp[1] = (int16_t)( (un_motor_temp2.bit_data.controller_temp) - 40 );
|
|
}
|
|
else if(signal_id == &un_motor_temp3)
|
|
{
|
|
temp_data.current_temp[2] = (int16_t)( (un_motor_temp3.bit_data.controller_temp) - 40 );
|
|
}
|
|
else if(signal_id == &un_motor_temp4)
|
|
{
|
|
temp_data.current_temp[3] = (int16_t)( (un_motor_temp4.bit_data.controller_temp) - 40 );
|
|
}
|
|
else{}
|
|
}
|
|
|
|
|
|
// APP模块的初始化
|
|
void tempAppInit(void)
|
|
{
|
|
// 初始化
|
|
timerInit(&temp_data.timer);
|
|
|
|
memset(&temp_data, 0, sizeof(TempSystem));
|
|
temp_data.state[0] = TEMP_NORMAL;
|
|
temp_data.state[1] = TEMP_NORMAL;
|
|
temp_data.mode = TEMP_MODE_AUTO;
|
|
temp_data.target_temp = 25; // 默认目标温度25度
|
|
|
|
// 订阅输入信号,处理温度逻辑
|
|
subscribe(&un_motor_temp1, tempInput);
|
|
subscribe(&un_motor_temp2, tempInput);
|
|
subscribe(&un_motor_temp3, tempInput);
|
|
subscribe(&un_motor_temp4, tempInput);
|
|
|
|
// 启动定时器,每秒检查一次温度
|
|
subscribe(&temp_data.timer, tempProcess);
|
|
timerStart(&temp_data.timer, 1000, 1); //1s
|
|
|
|
printf("app_temp: initial OK \n");
|
|
|
|
|
|
}
|