修改标定方式为取10s的值,去掉最低值和最大值,然后取平均值

This commit is contained in:
小马_666 2025-06-16 16:51:33 +08:00
parent 349e217407
commit 820a1c1a3b
6 changed files with 69 additions and 78 deletions

View File

@ -5,7 +5,7 @@
"type": "mrs-debugger",
"request": "launch",
"name": "ble_bjq_ch303rct6_ml307",
"cwd": "c:\\Users\\1\\Desktop\\yhq\\ble_-tyq_-bjq_-ch32-v303",
"cwd": "d:\\SXDT\\Project\\CH32\\ble_bjq_ch303rct6_ml307",
"openOCDCfg": {
"useLocalOpenOCD": true,
"executable": "c:/MounRiver/MounRiver_Studio2/resources/app/resources/win32/components/WCH/OpenOCD/OpenOCD/bin/openocd.exe",
@ -39,8 +39,8 @@
"additionalCommands": []
},
"loadedFiles": {
"executableFile": "c:\\Users\\1\\Desktop\\yhq\\ble_-tyq_-bjq_-ch32-v303\\obj\\ble_bjq_ch303rct6_ml307.elf",
"symbolFile": "c:\\Users\\1\\Desktop\\yhq\\ble_-tyq_-bjq_-ch32-v303\\obj\\ble_bjq_ch303rct6_ml307.elf",
"executableFile": "d:\\SXDT\\Project\\CH32\\ble_bjq_ch303rct6_ml307\\obj\\ble_bjq_ch303rct6_ml307.elf",
"symbolFile": "d:\\SXDT\\Project\\CH32\\ble_bjq_ch303rct6_ml307\\obj\\ble_bjq_ch303rct6_ml307.elf",
"executableFileOffset": 0,
"symbolFileOffset": 0
},

View File

@ -2,7 +2,7 @@
* @Author: mbw
* @Date: 2024-10-23 17:14:16
* @LastEditors: mbw && 1600520629@qq.com
* @LastEditTime: 2025-06-06 14:00:40
* @LastEditTime: 2025-06-16 16:46:25
* @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c
* @Descrt_thread_
*
@ -29,7 +29,6 @@
#include "bsp_bt.h"
#include "math.h"
#define LOG_TAG "main"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
@ -177,7 +176,7 @@ uint16_t Get_Stable_Reading(uint8_t cnt, uint32_t timeout_ms)
{
current = Get_Gas_VoltageAdcInt1000x();
uint16_t diff = fabs(current - last);// 差值
uint16_t diff = fabs(current - last); // 差值
if (diff < STABLE_THRESHOLD)
{
stable_count++;
@ -199,7 +198,7 @@ uint16_t Get_Stable_Reading(uint8_t cnt, uint32_t timeout_ms)
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);
@ -224,55 +223,69 @@ int APP_Calibration_Handle(void)
Send_Laser_Alarm_Event(kNotCalibratedEvent);
return 0;
}
Calibration_flag = 1;
uint8_t calibration_buf[2] = {0};
/* 获取10个数据 */
#if 1
uint8_t i = 0;
uint16_t gas_buf[10] = {0};
for (i = 0; i < 10; i++)
uint8_t i = 0;
uint16_t gas_buf[20] = {0};
// 采集10s的ADC值
for (i = 0; i < 20; i++)
{
gas_buf[i] = Get_Gas_VoltageAdcInt1000x();
if (gas_buf[i] == 0)
if ((gas_buf[i] == 0) || (gas_buf[i] >= 0xfff))
{
return 0;
}
gas_calibration_voltage += gas_buf[i];
rt_thread_mdelay(500);
}
if (i == 10)
{
gas_calibration_voltage = (uint16_t)(gas_calibration_voltage / 10);
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
// 冒泡排序(升序)
for (i = 0; i < 10 - 1; i++)
{
LOG_E("标定错误");
return -1;
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);
// 写入标定值
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(10, 10000);//10s内连续10次稳定则认为其标定浓度稳定进行标定
if(gas_calibration_voltage == 0)
gas_calibration_voltage = Get_Stable_Reading(10, 10000); // 10s内连续10次稳定则认为其标定浓度稳定进行标定
if (gas_calibration_voltage == 0)
{
LOG_D("标定超时,标定错误");
Send_Laser_Alarm_Event(kNotCalibratedEvent);
return -1;
return -1;
}
else
{
calibration_buf[0] = gas_calibration_voltage & 0xFF; // 低字节
calibration_buf[1] = (gas_calibration_voltage >> 8) & 0xFF; // 高字节
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("标定完成");
@ -284,15 +297,13 @@ int APP_Calibration_Handle(void)
}
#endif
}
int main(void)
{
// 定义超时时间,单位为毫秒
#define TIMEOUT_MS (3 * 60 * 1000)
#define WORK_TIMER_CNT (1000 * 60 * 60)//按照小时算
#define WORK_TIMER_CNT (1000 * 60 * 60) // 按照小时算
rt_thread_mdelay(200);
if (Get_VIN_VoltageInt1000x() > 10000)
{
@ -552,6 +563,7 @@ int main(void)
GAS_POWER_CLOSE;
// 写入掉电记录
Flash_Write_Record(kRecordPowerDown);
rt_thread_delay(100);
Flash_Set_WorkDuration(work_duration); // 写入工作时长
if (Flash_Get_Valve_Num())
{

View File

@ -4,7 +4,7 @@
* @LastEditors : stark1898y 1658608470@qq.com
* @LastEditTime : 2025-01-23 16:17:44
* @FilePath : \JT-DT-YD4N02A_4G_RTT_MRS\bsp\inc\bsp_vin_detection.h
* @Description :
* @Description
*
* Copyright (c) 2024 by yzy, All Rights Reserved.
*/
@ -21,11 +21,16 @@
#define VIN_VOLTAGE_RATIO (4.19148936F)
#ifdef VIN_THREAD_ENABLE
// 不含输入二极管的压降
// (4.7/19.7) * 13V = 3.1V, 3.1/3.3*4096 = 3723
#define VIN_ADC_HIGH_THRESHOLD (uint16_t)3847 // 13V
// (4.7/19.7) * 10V = 2.39V, 2.39/3.3*4096 = 2966
#define VIN_ADC_LOW_THRESHOLD (uint16_t)2369 // 8V
#else
#define VIN_ADC_LOW_THRESHOLD (8000) //8V
#endif
float Get_VIN_Voltage(void);

View File

@ -590,11 +590,11 @@ void Flash_Write_RecordIndex(TuFlashHrRecordFrame *pHrRecord, TeRecord record,
void Flash_Write_Record(TeRecord record)
{
LOG_D("/*********Flash_Write_Record***************/");
// LOG_D("/*********Flash_Write_Record***************/");
rt_uint16_t index_max = Flash_GetMaxIndex_Records(record);
rt_uint16_t index_new = index_max + 1;
LOG_D("index_new = %d", index_new);
// LOG_D("index_new = %d", index_new);
rt_uint16_t index_start;
rt_uint8_t index_start_page_offset;
@ -612,15 +612,15 @@ void Flash_Write_Record(TeRecord record)
else
{
index_start = index_max - (hr_record_max_num[record] - 1);
LOG_D("index_start = %d", index_start);
// LOG_D("index_start = %d", index_start);
index_start_page_offset = ((index_start - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record];
LOG_D("index_start_page_offset = %d", index_start_page_offset);
// LOG_D("index_start_page_offset = %d", index_start_page_offset);
index_max_page_offset = ((index_max - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record];
}
index_max_in_page_offset = (index_max - 1) % FLASH_PAGE_HR_RECORD_NUM;
LOG_D("index_max_in_page_offset = %d", index_max_in_page_offset);
// LOG_D("index_max_in_page_offset = %d", index_max_in_page_offset);
if ((index_max_in_page_offset + 1) == (FLASH_PAGE_HR_RECORD_NUM - 1) && index_max_page_offset == (hr_record_pages[record] - 1))
{

View File

@ -2020,7 +2020,7 @@ static void Ml307_Life_Thread_Entry (void *parameter)
result = -RT_ERROR;
goto __exit;
}
/* check SIM card */
/* check SIM card */
for (i = 0; i < 60; i++)
{
if (at_obj_exec_cmd (device->client, resp, "AT+CPIN?") == RT_EOK)
@ -2093,32 +2093,6 @@ static void Ml307_Life_Thread_Entry (void *parameter)
result = -RT_ERROR;
goto __exit;
}
// /* Define PDP Context */
// for (i = 0; i < 60; i++)
// {
// if (at_obj_exec_cmd (device->client, resp, "AT+CGACT=1") == RT_EOK)
// {
// LOG_D ("pdp激活成功");
// break;
// }
// else
// {
// // AT+CGACT=0,1 //去激活 PDP
// if (at_obj_exec_cmd (device->client, resp, "AT+CGACT=0") == RT_EOK)
// {
// LOG_D ("pdp去激活成功");
// }
// }
// rt_thread_mdelay (1000);
// }
// if (i == 60)
// {
// result = -RT_ERROR;
// goto __exit;
// }
if (at_obj_exec_cmd (device->client, resp, "AT+MIPCALL=1,1") != RT_EOK)
{
result = -RT_ERROR;

View File

@ -2,7 +2,7 @@
* @Author : stark1898y 1658608470@qq.com
* @Date : 2024-06-14 15:28:09
* @LastEditors: mbw && 1600520629@qq.com
* @LastEditTime: 2025-02-21 13:17:48
* @LastEditTime: 2025-06-16 16:20:51
* @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_vin_detection.c
* @Description :
*
@ -62,15 +62,15 @@ static void vin_thread_entry(void *param)
{
// rt_sem_take(sem_vin, RT_WAITING_FOREVER);
// 写入掉电记录
if (Get_VIN_VoltageInt1000x() <= 10000)
if (Get_VIN_VoltageInt1000x() <= VIN_ADC_LOW_THRESHOLD)
{
if (SysControl.status != kPowerDownEvent)
{
LOG_D("电压低于10V");
LOG_D("电压低于8V");
Send_Laser_Alarm_Event(kPowerDownEvent);
}
}
rt_thread_mdelay(50);
rt_thread_mdelay(500);
#if 0
Flash_Write_Record(kRecordPowerDown);