RT-GasFlowMeter/applications/main.c

355 lines
14 KiB
C
Raw Normal View History

/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-07-14 RT-Thread first version
*/
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtdevice.h>
#include <board.h>
#include "./ngflowcal/detail.h"
#include "./ngflowcal/FlowCal.h"
#include "./ngflowcal/NGCal.h"
#include "./ngflowcal/Therm.h"
#define LED_PIN GET_PIN(B, 0) // RT-Thread 的 PIN 宏
#define LED_PIN1 GET_PIN(B, 1) // RT-Thread 的 PIN 宏
#define LED_PIN2 GET_PIN(B, 2) // RT-Thread 的 PIN 宏
/* 线程控制块 */
static rt_thread_t led_thread = RT_NULL;
static rt_thread_t flowCal_thread = RT_NULL;
void NGFlowCal(void);
/* LED 线程入口函数 */
static void led_thread_entry(void *parameter)
{
rt_uint8_t led_state = 0;
/* 设置引脚为输出模式 */
rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED_PIN1, PIN_MODE_OUTPUT);
rt_pin_mode(LED_PIN2, PIN_MODE_OUTPUT);
while (1)
{
/* 切换 LED 状态 */
led_state = !led_state;
2025-07-15 15:14:00 +00:00
rt_pin_write(LED_PIN, !led_state);
rt_pin_write(LED_PIN1, led_state);
rt_pin_write(LED_PIN2, led_state);
rt_thread_mdelay(200);
rt_pin_write(LED_PIN, led_state);
rt_pin_write(LED_PIN1, led_state);
rt_pin_write(LED_PIN2, !led_state);
rt_thread_mdelay(200);
rt_pin_write(LED_PIN, led_state);
rt_pin_write(LED_PIN1, !led_state);
rt_pin_write(LED_PIN2, !led_state);
rt_thread_mdelay(200);
2025-07-15 15:14:00 +00:00
rt_kprintf("Numb \n",led_state);
}
}
/* flowCal 线程入口函数 */
static void flowCal_thread_entry(void *parameter)
{
while (1)
{
NGFlowCal();
/* 延时 500ms */
rt_thread_mdelay(500);
}
}
int main(void)
{
int count = 1;
/* 创建线程 */
led_thread = rt_thread_create(
"led",
led_thread_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX / 2,
20);
/* 启动线程 */
if (led_thread != RT_NULL)
{
rt_thread_startup(led_thread);
return RT_EOK;
}
else
{
return -RT_ERROR;
}
/* 创建线程 */
flowCal_thread = rt_thread_create(
"flowCal",
flowCal_thread_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX / 2,
20);
/* 启动线程 */
if (flowCal_thread != RT_NULL)
{
rt_thread_startup(flowCal_thread);
return RT_EOK;
}
else
{
return -RT_ERROR;
}
while (count++)
{
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
}
return RT_EOK;
}
void NGFlowCal(void){
// 定义并初始化 FlowParSTRUCT 结构体变量
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; // 温度(°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 = 9; // 镍铬合金
// 设置天然气组分(示例: 95%甲烷5%其他)
// 初始化天然气组分数组(GB/T 21446-2008 典型示例组成)
for (int i = 0; i < NUMBEROFCOMPONENTS; i++) {
flowParams.dNG_Compents[i] = 0.0; // 先全部初始化为0
}
// flowParams.dNG_Compents[0] = 92.47; // 甲烷(CH4)
// flowParams.dNG_Compents[1] = 0.68; // 氮气(N2)
// flowParams.dNG_Compents[2] = 1.75; // 二氧化碳(CO2)
// flowParams.dNG_Compents[3] =3.5; // 乙烷(C2H6)
// flowParams.dNG_Compents[4] = 0.98; // 丙烷(C3H8)
// flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
// flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
// flowParams.dNG_Compents[7] = 0.0; // 氢气(H2)
// flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
// flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
// flowParams.dNG_Compents[10] = 0.34; // 异丁烷(i-C4H10)
// flowParams.dNG_Compents[11] = 0.22; // 正丁烷(n-C4H10)
// flowParams.dNG_Compents[12] = 0.0; // 异戊烷(i-C5H12)
// flowParams.dNG_Compents[13] = 0.06; // 正戊烷(n-C5H12)
// flowParams.dNG_Compents[14] = 0.0; // 己烷(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.0; // 氦气(He)
// flowParams.dNG_Compents[20] = 0.0; // 其他组分
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; // 其他组分
// 按照GB/T 21446-2008标准中典型天然气组分赋值(体积百分比)
// flowParams.dNG_Compents[0] = 90.6724; // 甲烷(CH4)
// flowParams.dNG_Compents[1] = 3.1284; // 氮气(N2)
// flowParams.dNG_Compents[2] = 0.4676; // 二氧化碳(CO2)
// flowParams.dNG_Compents[3] =4.5279; // 乙烷(C2H6)
// flowParams.dNG_Compents[4] = 0.8280; // 丙烷(C3H8)
// flowParams.dNG_Compents[5] = 0.00; // 水(H2O)
// flowParams.dNG_Compents[6] = 0.00; // 硫化氢(H2S)
// flowParams.dNG_Compents[7] = 0.0; // 氢气(H2)
// flowParams.dNG_Compents[8] = 0.00; // 一氧化碳(CO)
// flowParams.dNG_Compents[9] = 0.00; // 氧气(O2)
// flowParams.dNG_Compents[10] = 0.1037; // 异丁烷(i-C4H10)
// flowParams.dNG_Compents[11] = 0.1563; // 正丁烷(n-C4H10)
// flowParams.dNG_Compents[12] = 0.0321; // 异戊烷(i-C5H12)
// flowParams.dNG_Compents[13] = 0.0443; // 正戊烷(n-C5H12)
// flowParams.dNG_Compents[14] = 0.0393; // 己烷(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.0; // 氦气(He)
// flowParams.dNG_Compents[20] = 0.0; // 其他组分
// flowParams.dNG_Compents[0] =96.5; // 甲烷(CH4)
// flowParams.dNG_Compents[1] =0.30; // 氮气(N2)
// flowParams.dNG_Compents[2] =0.6; // 二氧化碳(CO2)
// flowParams.dNG_Compents[3] =1.80; // 乙烷(C2H6)
// flowParams.dNG_Compents[4] =0.45; // 丙烷(C3H8)
// flowParams.dNG_Compents[5] =0; // 水(H2O)
// flowParams.dNG_Compents[6] =0; // 硫化氢(H2S)
// flowParams.dNG_Compents[7] =0; // 氢气(H2)
// flowParams.dNG_Compents[8] =0; // 一氧化碳(CO)
// flowParams.dNG_Compents[9] =0; // 氧气(O2)
// flowParams.dNG_Compents[10]= 0.1; // 异丁烷(i-C4H10)
// flowParams.dNG_Compents[11]= 0.1; // 正丁烷(n-C4H10)
// flowParams.dNG_Compents[12]= 0.05; // 异戊烷(i-C5H12)
// flowParams.dNG_Compents[13]= 0.03; // 正戊烷(n-C5H12)
// flowParams.dNG_Compents[14]= 0.07; // 己烷(C6H14)
// flowParams.dNG_Compents[15]= 0; // 庚烷(C7H16)
// flowParams.dNG_Compents[16]= 0; // 辛烷(C8H18)
// flowParams.dNG_Compents[17]= 0; // 壬烷(C9H20)
// flowParams.dNG_Compents[18]= 0; // 癸烷(C10H22)
// flowParams.dNG_Compents[19]= 0; // 氦气(He)
// flowParams.dNG_Compents[20]= 0; // 其他组分
// // 显式调用 NGCal_Init 初始化模块
// if (NGCal_NGCal != NGCal_Init()) {
// printf("错误NGCal 初始化失败!\n");
// return -1; // 退出程序
// }
// 调用流量计算函数
OFlowCal(&flowParams, &ngParams);
// 打印计算结果
printf("工况条件信息:\n");
printf("标准参比条件: %d\n", flowParams.dCbtj);
printf("计量参比压力: %.2f\n", flowParams.dPb_M);
printf("计量参比温度: %.2f\n", flowParams.dTb_M);
printf("能量参比压力: %.2f\n", flowParams.dPb_E);
printf("能量参比温度: %.2f\n", flowParams.dTb_E);
printf("大气压力: %.2f Pa\n", flowParams.dPatm);
printf("天然气组分:\n");
for (int i = 0; i < 21; i++) {
printf(" 组分 %d: %.6f\n", i, flowParams.dNG_Compents[i]);
}
printf("\n仪表参数:\n");
printf("仪表类型: %d\n", flowParams.dMeterType);
printf("核心类型: %d\n", flowParams.dCoreType);
printf("取压方式: %d\n", flowParams.dPtmode);
printf("管道类型: %d\n", flowParams.dPipeType);
printf("管道内径: %.2f mm\n", flowParams.dPipeD);
printf("管道材质: %d\n", flowParams.dPipeMaterial);
printf("孔板直径: %.2f mm\n", flowParams.dOrificeD);
printf("孔板材质: %d\n", flowParams.dOrificeMaterial);
printf("\n测量值:\n");
printf("压力: %.2f Pa\n", flowParams.dPf);
printf("压力类型: %d\n", flowParams.dPfType);
printf("温度: %.2f K\n", flowParams.dTf);
printf("差压: %.2f Pa\n", flowParams.dDp);
printf("仪表系数: %.6f\n", flowParams.dMeterFactor);
printf("脉冲数: %.2f\n", flowParams.dPulseNum);
printf("\n计算结果:\n");
printf("膨胀系数: %.6f\n", flowParams.dE);
printf("相对密度系数: %.6f\n", flowParams.dFG);
printf("超压缩系数: %.6f\n", flowParams.dFT);
printf("动力粘度: %.6f\n", flowParams.dDViscosity);
printf("热膨胀系数: %.6f\n", flowParams.dDExpCoefficient);
printf("管道雷诺数: %.2f\n", flowParams.dRnPipe);
printf("孔板弯曲系数: %.6f\n", flowParams.dBk);
printf("管道粗糙度: %.6f\n", flowParams.dRoughNessPipe);
printf("流出系数: %.6f\n", flowParams.dCd);
printf("流出系数修正: %.6f\n", flowParams.dCdCorrect);
printf("喷嘴流出系数: %.6f\n", flowParams.dCdNozell);
printf("标况体积流量: %.6f Nm3/s\n", flowParams.dVFlowb);
printf("工况体积流量: %.6f m3/s\n", flowParams.dVFlowf);
printf("质量流量: %.6f t/s\n", flowParams.dMFlowb);
printf("能量流量: %.6f MJ/s\n", flowParams.dEFlowb);
printf("流速: %.6f m/s\n", flowParams.dVelocityFlow);
printf("压力损失: %.6f\n", flowParams.dPressLost);
printf("直径比: %.6f\n", flowParams.dBeta);
printf("等熵指数: %.6f\n", flowParams.dKappa);
printf("压缩因子: %.6f\n", flowParams.dFpv);
printf("状态: %ld\n", ngParams.lStatus);
printf("强制更新标志: %d\n", ngParams.bForceUpdate);
printf("混合比:\n");
for (int i = 0; i < 21; i++) {
printf(" 组分 %d: %.6f\n", i, ngParams.adMixture[i]);
}
printf("参比条件: %d\n", ngParams.dCbtj);
printf("标准压力: %.2f Pa\n", ngParams.dPb);
printf("标准温度: %.2f K\n", ngParams.dTb);
printf("工作压力: %.2f Pa\n", ngParams.dPf);
printf("工作温度: %.2f K\n", ngParams.dTf);
printf("\nAGA 8 详细计算结果:\n");
printf("平均分子量: %.6f\n", ngParams.dMrx);
printf("标准条件下压缩因子: %.6f\n", ngParams.dZb);
printf("工作条件下压缩因子: %.6f\n", ngParams.dZf);
printf("超压缩因子: %.6f\n", ngParams.dFpv);
printf("标准条件下摩尔密度: %.6f moles/dm3\n", ngParams.dDb);
printf("工作条件下摩尔密度: %.6f moles/dm3\n", ngParams.dDf);
printf("标准条件下密度: %.6f kg/m3\n", ngParams.dRhob);
printf("工作条件下密度: %.6f kg/m3\n", ngParams.dRhof);
printf("理想相对密度: %.6f\n", ngParams.dRD_Ideal);
printf("实际相对密度: %.6f\n", ngParams.dRD_Real);
printf("\n热力学性质:\n");
printf("理想焓: %.6f\n", ngParams.dHo);
printf("实际焓: %.6f J/kg\n", ngParams.dH);
printf("实际熵: %.6f J/kg-mol.K\n", ngParams.dS);
printf("理想定压比热: %.6f J/kg-mol.K\n", ngParams.dCpi);
printf("实际定压比热: %.6f J/kg-mol.K\n", ngParams.dCp);
printf("实际定容比热: %.6f J/kg-mol.K\n", ngParams.dCv);
printf("比热比: %.6f\n", ngParams.dk);
printf("等熵指数: %.6f\n", ngParams.dKappa);
printf("声速: %.6f m/s\n", ngParams.dSOS);
printf("临界流函数: %.6f\n", ngParams.dCstar);
rt_kprintf("\n单位摩尔高热值: %.6f\n", ngParams.dHhvMol);
printf("单位摩尔低热值: %.6f\n", ngParams.dLhvMol);
}