GasFlowMeter/User/main.c

218 lines
7.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
*********************************************************************
* @file main.c
* @author liaodeyun
* @version V1.0
* @date 2025-7-6
* @brief RT-Thread 3.0 + STM32 天然气流量计算
*********************************************************************
* @attention
*
* 实验平台:野火 H750 PRO STM32 开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :https://fire-stm32.taobao.com
*
**********************************************************************
*/
/*
*************************************************************************
* 包含的头文件
*************************************************************************
*/
#include "board.h"
#include "rtthread.h"
#include <stdio.h>
#include "NGCal.h"
#include "Therm.h"
#include "Detail.h"
#include "FlowCal.h"
/*
*************************************************************************
* 变量
*************************************************************************
*/
/* 定义线程控制块 */
static rt_thread_t OFlowCal_thread = RT_NULL;
static rt_thread_t key_thread = RT_NULL;
/*
*************************************************************************
* 函数声明
*************************************************************************
*/
static void OFlowCal_thread_entry(void* parameter);
static void key_thread_entry(void* parameter);
/*
*************************************************************************
* main 函数
*************************************************************************
*/
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
/*
* 开发板硬件初始化RTT系统初始化已经在main函数之前完成
* 即在component.c文件中的rtthread_startup()函数中完成了。
* 所以在main函数中只需要创建线程和启动线程即可。
*/
rt_kprintf("这是一个[野火]-STM32H750天然气流量计算机实验程序\n\n");
rt_kprintf("按下K1挂起线程按下K2恢复线程\n");
OFlowCal_thread = /* 线程控制块指针 */
rt_thread_create( "OFlowCal", /* 线程名字 */
OFlowCal_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
3, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (OFlowCal_thread != RT_NULL)
rt_thread_startup(OFlowCal_thread);
else
return -1;
key_thread = /* 线程控制块指针 */
rt_thread_create( "key", /* 线程名字 */
key_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
2, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (key_thread != RT_NULL)
rt_thread_startup(key_thread);
else
return -1;
}
/*
*************************************************************************
* 线程定义
*************************************************************************
*/
static void OFlowCal_thread_entry(void* parameter)
{
while (1)
{
// 初始化结构体
FlowParSTRUCT flowParams = {0};
NGParSTRUCT ngParams = {0};
// 设置基本参数
flowParams.dPatm = 0.0981; // 标准大气压(bar)
flowParams.dPf = 1.48; // 压力(MPa)
flowParams.dPfType = 0; // 0=表压1=绝压
flowParams.dDp = 12.50; // 差压(kPa)
flowParams.dTf = 15.0; // 温度(°C)
flowParams.dCbtj = 0; // 参比条件类型(0=标准状态)
// 设置管道参数
flowParams.dPipeD = 259.38; // 管道内径(mm)
flowParams.dOrificeD = 150.25; // 孔板孔径(mm)
flowParams.dPipeType = 0; // 管道类型
flowParams.dPtmode = 0; // 取压方式(0=法兰取压1=角接取压)
// 设置材料参数
flowParams.dPipeMaterial = 2; // 20号钢
flowParams.dOrificeMaterial = 10; // 镍铬合金
// 设置天然气组分(示例: 95%甲烷5%其他)
// 初始化天然气组分数组(GB/T 21446-2008 典型示例组成)
for(int i=0; i<NUMBEROFCOMPONENTS; i++) {
flowParams.dNG_Compents[i] = 0.0; // 先全部初始化为0
}
// 按照GB/T 21446-2008标准中典型天然气组分赋值(体积百分比)
flowParams.dNG_Compents[0] = 88.36; // 甲烷(CH4)
flowParams.dNG_Compents[1] = 0.68; // 氮气(N2)
flowParams.dNG_Compents[2] = 1.57; // 二氧化碳(CO2)
flowParams.dNG_Compents[3] = 6.25; // 乙烷(C2H6)
flowParams.dNG_Compents[4] = 2.4; // 丙烷(C3H8)
flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
flowParams.dNG_Compents[7] = 0.04; // 氢气(H2)
flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
flowParams.dNG_Compents[10] = 0.15; // 异丁烷(i-C4H10)
flowParams.dNG_Compents[11] = 0.35; // 正丁烷(n-C4H10)
flowParams.dNG_Compents[12] = 0.05; // 异戊烷(i-C5H12)
flowParams.dNG_Compents[13] = 0.1; // 正戊烷(n-C5H12)
flowParams.dNG_Compents[14] = 0.01; // 己烷(C6H14)
flowParams.dNG_Compents[15] = 0.0; // 庚烷(C7H16)
flowParams.dNG_Compents[16] = 0.0; // 辛烷(C8H18)
flowParams.dNG_Compents[17] = 0.0; // 壬烷(C9H20)
flowParams.dNG_Compents[18] = 0.0; // 癸烷(C10H22)
flowParams.dNG_Compents[19] = 0.04; // 氦气(He)
flowParams.dNG_Compents[20] = 0.0; // 其他组分
// 调用流量计算函数
OFlowCal(&flowParams, &ngParams);
// 打印计算结果
printf("========== 流量计算结果 ==========\n");
printf("工况流量: %.4f m3/s\n", flowParams.dVFlowf);
printf("标况流量: %.4f m3/s\n", flowParams.dVFlowb);
printf("标况质量流量: %.4f kg/s\n", flowParams.dMFlowb);
printf("标况能量流量: %.4f MJ/s\n", flowParams.dEFlowb / 1e6);
printf("管道内流速: %.2f m/s\n", flowParams.dVelocityFlow);
printf("流出系数: %.4f\n", flowParams.dCd);
printf("雷诺数: %.2f\n", flowParams.dRnPipe);
}
}
static void key_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
while (1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_PIN) == KEY_ON )/* K1 被按下 */
{
printf("挂起LED1线程\n");
uwRet = rt_thread_suspend(OFlowCal_thread);/* 挂起LED1线程 */
if(RT_EOK == uwRet)
{
rt_kprintf("挂起LED1线程成功\n");
}
else
{
rt_kprintf("挂起LED1线程失败失败代码0x%lx\n",uwRet);
}
}
if( Key_Scan(KEY2_GPIO_PORT,KEY2_PIN) == KEY_ON )/* K1 被按下 */
{
printf("恢复LED1线程\n");
uwRet = rt_thread_resume(OFlowCal_thread);/* 恢复LED1线程 */
if(RT_EOK == uwRet)
{
rt_kprintf("恢复LED1线程成功\n");
}
else
{
rt_kprintf("恢复LED1线程失败失败代码0x%lx\n",uwRet);
}
}
rt_thread_delay(20);
}
}
/********************************END OF FILE****************************/