计算斜率,判断斜率
#include
#include
#include
#include
// 定义常量
#define LOW_COOK_WINDOW_SIZE 20 // 滑动窗口大小,10个样本点(10秒)
#define LOW_COOK_SLOPE_THRESHOLD 0.12f // 斜率平缓的阈值
#define LOW_COOK_SAMPLE_INTERVAL 10 // 采样间隔,单位为100ms
#define LOW_COOK_DEFAULT_TIME_DIFFERENCE 10.0f // 默认时间间隔为10秒
#define MIN_TIME_DIFFERENCE 1.0f // 最小时间间隔为1秒
#define MAX_TIME_DIFFERENCE (LOW_COOK_WINDOW_SIZE - 1) // 最大时间间隔为窗口大小减1
// 定义加热系统结构
typedef struct
{
float slope;
float data[LOW_COOK_WINDOW_SIZE]; // 滑动窗口数据
unsigned char count; // 当前样本数量
bool slope_event; // 斜率事件
unsigned char tim_count; // 时间片计数器
unsigned char state; // 状态机
unsigned char slope_status; // 状态
} low_cook_heating_system_t;
// 全局变量
low_cook_heating_system_t low_cooksystem;
// 初始化加热系统
void low_cook_init_heating_system(low_cook_heating_system_t *system)
{
system->slope = 0.0f;
system->tim_count = 0;
system->state = 0;
system->count = 0;
system->slope_event = false;
system->slope_status = 1; // 样本不足
for (unsigned char i = 0; i data[i] = 0.0f;
}
}
// 添加样本点到滑动窗口,100ms调用一次,1S存数据一次
void low_cook_add_sample(low_cook_heating_system_t *system, float temperature)
{
if (++system->tim_count tim_count = 0;
if (system->count data[system->count++] = temperature;
}
else
{
// 如果窗口已满,丢弃最旧的数据,将最新的数据添加到最后的位置
for (unsigned char i = 1; i data[i - 1] = system->data[i];
}
system->data[LOW_COOK_WINDOW_SIZE - 1] = temperature;
}
}
// 计算温度斜率
float low_cook_calculate_slope(low_cook_heating_system_t *system, float time_difference)
{
// 限制 time_difference 在合理范围内
if (time_difference MAX_TIME_DIFFERENCE)
{
system->slope_status = 2; // 时间间隔超出范围
return 0.0f;
}
unsigned char interval_samples = (unsigned char)(time_difference); // 计算需要的样本间隔
if (system->count = system->count)
{
system->slope_status = 1; // 样本不足
return 0.0f;
}
// 获取最新样本点和 interval_samples 前的样本点
float current = system->data[system->count - 1];
float previous = system->data[system->count - 1 - interval_samples];
system->slope_status = 0; // 成功计算斜率
// 计算并返回斜率
return (current - previous) / time_difference;
}
void low_cook_update_slope(float time_difference)
{
// 添加新的温度数据到系统中
low_cook_add_sample(&low_cooksystem, stheat.temperature);
// 计算斜率
low_cooksystem.slope = low_cook_calculate_slope(&low_cooksystem, time_difference);
if ((low_cooksystem.slope_status == 0) && (stheat.temperature > 35))
{
// 判断斜率是否平缓
low_cooksystem.slope_event = (fabsf(low_cooksystem.slope)
(图片来源网络,侵删)
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
