修改标定方式为区间稳定加平均值法

This commit is contained in:
小马_666 2025-06-24 08:58:06 +08:00
parent faf02abe81
commit e29e8355da
2 changed files with 28 additions and 71 deletions

View File

@ -83,6 +83,7 @@
"ctime": "c",
"bsp_wdg.h": "c",
"rtdbg.h": "c",
"bsp_rng.h": "c"
"bsp_rng.h": "c",
"cstdlib": "c"
}
}

View File

@ -2,7 +2,7 @@
* @Author: mbw
* @Date: 2024-10-23 17:14:16
* @LastEditors: mbw && 1600520629@qq.com
* @LastEditTime: 2025-06-23 14:17:21
* @LastEditTime: 2025-06-24 08:57:50
* @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c
* @Descrt_thread_
*
@ -47,6 +47,7 @@ struct rt_timer preheat_timer; // 传统模式需要进行预热,
uint8_t preheat_flag = 0; // 预热时间是否结束标志
uint8_t Calibration_flag = 0; // 查看是否在标定检测状态
void Send_Laser_Alarm_Event(AlarmEvent event_type);
TeCalibrationStatus g_Calibration_status = kNotCalibrated;
@ -164,14 +165,18 @@ void Preheat_Timer_Callback(void *parameter)
}
}
#define STABLE_THRESHOLD 50 // 50波动视为稳定
#define STABLE_THRESHOLD 20 // 20波动视为稳定
uint16_t Get_Stable_Reading(uint8_t cnt, uint32_t timeout_ms)
{
uint32_t start = rt_tick_get();
rt_tick_t start = rt_tick_get();
uint16_t last = Get_Gas_VoltageAdcInt1000x();
uint16_t current;
uint8_t stable_count = 0;
uint16_t gas_buf[20] = {0};//传入的cnt不能大于这个数组大小
uint16_t gas_value = 0;
uint32_t sum = 0;
while ((rt_tick_get() - start) < timeout_ms)
{
@ -181,16 +186,25 @@ uint16_t Get_Stable_Reading(uint8_t cnt, uint32_t timeout_ms)
if (diff < STABLE_THRESHOLD)
{
gas_buf[stable_count] = current;
stable_count++;
if (stable_count >= cnt) return current; // 连续n次稳定
if (stable_count >= cnt)
{
//求取平均值
for (size_t i = 0; i < stable_count; i++)
{
sum += gas_buf[i];
}
return (uint16_t)(sum / stable_count); // 连续n次稳定
}
}
else
{
stable_count = 0;
}
last = current;
rt_thread_mdelay(500);
rt_thread_mdelay(100);
}
return 0; // 超时
@ -201,6 +215,7 @@ int APP_Calibration_Handle(void)
{
uint16_t gas_calibration_voltage = 0;
uint8_t calibration_buf[2] = {0};
// if (Get_Gas_VoltageAdcInt1000x() > 400 && Get_Gas_VoltageAdcInt1000x() < 900)
// {
// LED_OFF(r);
@ -223,72 +238,18 @@ int APP_Calibration_Handle(void)
g_Calibration_status = kNotCalibrated;
LOG_D("没有气体,请重新上电标定");
Send_Laser_Alarm_Event(kNotCalibratedEvent);
return 0;
return RT_EOK;
}
#if 0
// uint8_t i = 0;
// uint16_t gas_buf[10] = {0};
// rt_memset(gas_buf, 0, sizeof(gas_buf));
// 采集10s的ADC值
// for (i = 0; i < 10; i++)
// {
// gas_buf[i] = Get_Gas_VoltageAdcInt1000x();
// if ((gas_buf[i] == 0) || (gas_buf[i] >= 5000)) // (3.3*1.784*1000)
// {
// Send_Laser_Alarm_Event(kNotCalibratedEvent);
// return 0;
// }
// rt_thread_mdelay(500);
// }
// // 冒泡排序(升序)
// for (i = 0; i < 10 - 1; i++)
// {
// for (int j = 0; j < 10 - i - 1; j++)
// {
// if (gas_buf[j] > gas_buf[j + 1])
// {
// uint16_t temp = gas_buf[j];
// gas_buf[j] = gas_buf[j + 1];
// gas_buf[j + 1] = temp;
// }
// }
// }
// // 使用索引 1~8 的8个值求平均即去掉一个最小和一个最大
// uint32_t sum = 0;
// for (i = 1; i <= 8; i++)
// {
// sum += gas_buf[i];
// }
// gas_calibration_voltage = (uint16_t)((sum / 8.0f) - 85);
gas_calibration_voltage = (Get_Gas_VoltageAdcInt1000x() - 50); // 写入标定值
calibration_buf[0] = gas_calibration_voltage & 0xFF; // 低字节
calibration_buf[1] = (gas_calibration_voltage >> 8) & 0xFF; // 高字节
LOG_D("calibration_buf[0] = %X calibration_buf[1] = %X", calibration_buf[0], calibration_buf[1]);
Flash_Sys_Cfg(kAlarmLValueId, calibration_buf, 2);
LOG_D("标定完成");
Calibration_flag = 0;
Flash_Set_Calibration_State(kSysGasCalibStatus);
g_Calibration_status = kSysGasCalibStatus;
Send_Laser_Alarm_Event(kNormalDetectionEvents);
return 0;
#else
gas_calibration_voltage = Get_Stable_Reading(4, 10000); // 10s内连续3次稳定则认为其标定浓度稳定进行标定
if (gas_calibration_voltage == 0)
gas_calibration_voltage = Get_Stable_Reading(10, 10000); // 10s内连续10稳定则认为其标定浓度稳定进行标定
if ((gas_calibration_voltage == 0)||(gas_calibration_voltage >= 0xfff))
{
LOG_D("标定超时,标定错误");
Send_Laser_Alarm_Event(kNotCalibratedEvent);
return -1;
return -RT_ERROR;
}
else
{
LOG_D("标定成功, 标定值为:%d", gas_calibration_voltage);
calibration_buf[0] = (gas_calibration_voltage & 0xFF); // 低字节
calibration_buf[1] = ((gas_calibration_voltage >> 8) & 0xFF); // 高字节
LOG_D("calibration_buf[0] = %X calibration_buf[1] = %X", calibration_buf[0], calibration_buf[1]);
@ -297,12 +258,9 @@ int APP_Calibration_Handle(void)
Calibration_flag = 0;
Flash_Set_Calibration_State(kSysGasCalibStatus);
g_Calibration_status = kSysGasCalibStatus;
Send_Laser_Alarm_Event(kNormalDetectionEvents);
return RT_EOK;
}
#endif
}
int main(void)
@ -542,8 +500,6 @@ int main(void)
#if (IOT_MODULE_SWITCH == 1)
Ml307_Send_Event(kMl307AlarmRcyEvent);
#endif
}
else if (received_event & Get_Sys_Event_Flag(kFaultEvent)) // 故障
{