修复HP203B求平均值导致的数据错误

This commit is contained in:
95384 2025-02-21 17:01:25 +08:00
parent 34c91c3099
commit 83a9a79dcd
1 changed files with 26 additions and 14 deletions

View File

@ -84,6 +84,18 @@ BOOL Hp203bReadTempture(float *press)
return TRUE; return TRUE;
} }
static float calculateAverage(float arr[], int avgLength) {
float sum = 0;
// 遍历数组最多10个元素收集非零值直到达到指定数量
for (int i = 0; i < 10; ++i) {
sum += arr[i];
}
// 计算平均值并限制最大值
float average = sum / avgLength;
return average;
}
/**************************** /****************************
*get_press_data *get_press_data
* *
@ -99,12 +111,12 @@ BOOL get_HP203_data(float* tempdata, float* press)
uint8_t temp_falt = 0; uint8_t temp_falt = 0;
uint8_t press_falt = 0; uint8_t press_falt = 0;
// 压强 // 压强
U_DataType collect_pressure[COLLECT_HB203_DATA_NUM]={0x00}; float collect_pressure[COLLECT_HB203_DATA_NUM]={0x00};
for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){ for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){
hp203_set_mode(); hp203_set_mode();
osDelay(5); osDelay(5);
ret = Hp203bReadPressure(&collect_pressure[i].fValue); ret = Hp203bReadPressure(&collect_pressure[i]);
if(ret == FALSE) if(ret == FALSE)
{ {
press_falt++; press_falt++;
@ -117,25 +129,25 @@ BOOL get_HP203_data(float* tempdata, float* press)
{ {
goto error_return; goto error_return;
} }
//求平均
U_DataType tmp_press = filter_middle(collect_pressure, COLLECT_HB203_DATA_NUM - press_falt, FILTER_DATA_TYPE_FLOAT); float tmp_press = calculateAverage(collect_pressure, COLLECT_HB203_DATA_NUM - press_falt);
if(tmp_press.fValue < 300) if(tmp_press < 300)
{ {
// return FALSE; // return FALSE;
goto error_return; goto error_return;
} }
if(tmp_press.fValue > 1200) if(tmp_press > 1200)
{ {
// return FALSE; // return FALSE;
goto error_return; goto error_return;
} }
// 温度 // 温度
U_DataType collect_tempture[COLLECT_HB203_DATA_NUM]={0x00}; float collect_tempture[COLLECT_HB203_DATA_NUM]={0x00};
for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){ for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){
hp203_set_mode(); hp203_set_mode();
osDelay(5); osDelay(5);
ret = Hp203bReadTempture(&collect_tempture[i].fValue); ret = Hp203bReadTempture(&collect_tempture[i]);
if(ret == FALSE) if(ret == FALSE)
{ {
temp_falt++; temp_falt++;
@ -148,21 +160,21 @@ BOOL get_HP203_data(float* tempdata, float* press)
{ {
goto error_return; goto error_return;
} }
//求平均
U_DataType tmp_tempture = filter_middle(collect_tempture, COLLECT_HB203_DATA_NUM - temp_falt, FILTER_DATA_TYPE_FLOAT); float tmp_tempture = calculateAverage(collect_tempture, COLLECT_HB203_DATA_NUM - temp_falt);
if(tmp_tempture.fValue < -50) if(tmp_tempture < -50)
{ {
// return FALSE; // return FALSE;
goto error_return; goto error_return;
} }
if(tmp_tempture.fValue > 95) if(tmp_tempture > 95)
{ {
// return FALSE; // return FALSE;
goto error_return; goto error_return;
} }
*tempdata = tmp_tempture.fValue; *tempdata = tmp_tempture;
*press = tmp_press.fValue; *press = tmp_press;
return TRUE; return TRUE;
error_return: error_return: