⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modeldlg.cpp

📁 开发的锌流量计算程序
💻 CPP
字号:
// ModelDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ShaoYe.h"
#include "ModelDlg.h"
#include "time.h"
#include "stdio.h"
#include "global_fun.h"
#include "modelset.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CModelDlg dialog
extern CString model_date;//模型建立日期
extern float model_parameter_tare;//皮重
extern float model_parameter_a;//常数项
extern float model_parameter_b;//一次项系数
extern float model_parameter_c;//二次向系数

CModelDlg::CModelDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CModelDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CModelDlg)
	m_flux = 0;
	m_volt = 0;
	m_weight = 0;
	//}}AFX_DATA_INIT
}

void CModelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModelDlg)
	DDX_Control(pDX, IDC_BUTTON2, m_button2);
	DDX_Control(pDX, IDC_BUTTON1, m_button1);
	DDX_Text(pDX, IDC_FLUX, m_flux);
	DDX_Text(pDX, IDC_VOLT, m_volt);
	DDX_Text(pDX, IDC_WEIGHT, m_weight);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CModelDlg, CDialog)
	//{{AFX_MSG_MAP(CModelDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModelDlg message handlers

void CModelDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	m_button2.EnableWindow(TRUE);
	m_button1.EnableWindow(FALSE);

	m_count=0;
	m_volt=0;
	m_weight=0;
	m_flux=0;
	sample_time=0;

	SetTimer(2,100,NULL);

}

void CModelDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
    KillTimer(2);
	data_process();//数据处理
	flux_modeling();//流量建模(多项式拟合)
	m_button2.EnableWindow(FALSE);
	m_button1.EnableWindow(TRUE);	
}

void CModelDlg::OnTimer(UINT nIDEvent) 
{
	int jj,kk;
	float temp;
	// TODO: Add your message handler code here and/or call default
//	volt_array[m_count]=convert_volt();//采样电压值
	volt_array[m_count]=(float)(rand()%13+56)/100.0;
	m_count++;
	if (m_count==50 )
	{
		m_count=0;//计数器清零

		//对50个采样点升序排序
		for(jj=1;jj<50;jj++)
		{
			for(kk=jj;kk>0;kk--)
			{
				if(volt_array[kk]<volt_array[kk-1])
				{
					temp=volt_array[kk];
					volt_array[kk]=volt_array[kk-1];
					volt_array[kk-1]=temp;
				}
				else break;
			}
		}

		m_volt=0;
		for(kk=10;kk<40;kk++) 
			m_volt=m_volt+volt_array[kk];
		m_volt=m_volt/30; //数字滤波取平均值——电压
		if(sample_time!=0)
		{
			m_flux=m_weight-m_volt*60;//流量:前一个重量减去当前重量
			if(sample_time<1000)
				flux_array[sample_time-1]=m_flux;
		}
		m_weight=m_volt*60;//重量
		if(sample_time<1000) 
			weight_array[sample_time]=m_weight;
		sample_time++;//采样点个数累加
		UpdateData(FALSE);//刷新显示
	}
	CDialog::OnTimer(nIDEvent);
}

//数据处理函数
void CModelDlg::data_process()
{
	int min_time=0;
	for(int i=0,j=0;i<sample_time-1;i++)
	{
 		if(flux_array[i]>0)
		{
			if(flux_array[i]<0.15)
				min_time++;
			if(min_time==25)
				break;
			weight_array[j]=weight_array[i];
			flux_array[j]=flux_array[i];
			j++;
		}
	}
	sample_time=j;
	model_parameter_tare=weight_array[0];
	for(i=1;i<sample_time;i++)
		if(weight_array[i]<model_parameter_tare)
			model_parameter_tare=weight_array[i];
	for(i=0;i<sample_time;i++)
		weight_array[i]-=model_parameter_tare;
/*
	FILE *fp;
	if ((fp=fopen("weight.txt","w"))==NULL)
		exit(0);
   	for(int ii=0;ii<sample_time;ii++)
		fprintf(fp,"%f\n",weight_array[ii]);
	fclose(fp);

	if ((fp=fopen("flux.txt","w"))==NULL)
		exit(0);
   	for(ii=0;ii<sample_time;ii++)
		fprintf(fp,"%f\n",flux_array[ii]);
	fclose(fp);
*/
}

//建模函数
void CModelDlg::flux_modeling()
{
	float input_array[1000][3];
	float temp[3][3];
	float temp_determinant;
	float temp_inv[3][3];
	float temp1[3][1000];
	int i,j,k;

	for(i=0;i<sample_time;i++)
	{
		input_array[i][0]=1;
		input_array[i][1]=weight_array[i];
		input_array[i][2]=weight_array[i]*weight_array[i];
	}
	
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			temp[i][j]=0;
			for(int k=0;k<sample_time;k++)
				temp[i][j]+=input_array[k][i]*input_array[k][j];
		}
	}

	temp_determinant=temp[0][0]*(temp[1][1]*temp[2][2]-temp[2][1]*temp[1][2]);
	temp_determinant-=temp[1][0]*(temp[0][1]*temp[2][2]-temp[2][1]*temp[0][2]);
	temp_determinant+=temp[2][0]*(temp[0][1]*temp[1][2]-temp[1][1]*temp[0][2]);

	temp_inv[0][0]=(temp[1][1]*temp[2][2]-temp[2][1]*temp[1][2])/temp_determinant;
	temp_inv[0][1]=-(temp[1][0]*temp[2][2]-temp[2][0]*temp[1][2])/temp_determinant;
	temp_inv[0][2]=(temp[1][0]*temp[2][1]-temp[2][0]*temp[1][1])/temp_determinant;
	temp_inv[1][0]=-(temp[0][1]*temp[2][2]-temp[2][1]*temp[0][2])/temp_determinant;
	temp_inv[1][1]=(temp[0][0]*temp[2][2]-temp[2][0]*temp[0][2])/temp_determinant;
	temp_inv[1][2]=-(temp[0][0]*temp[2][1]-temp[2][0]*temp[0][1])/temp_determinant;
	temp_inv[2][0]=(temp[0][1]*temp[1][2]-temp[1][1]*temp[0][2])/temp_determinant;
	temp_inv[2][1]=-(temp[0][0]*temp[1][2]-temp[1][0]*temp[0][2])/temp_determinant;
	temp_inv[2][2]=(temp[0][0]*temp[1][1]-temp[1][0]*temp[0][1])/temp_determinant;

	for(i=0;i<3;i++)
	{
		for(j=0;j<sample_time;j++)
		{
			temp1[i][j]=0;
			for(k=0;k<3;k++)
				temp1[i][j]+=temp_inv[i][k]*input_array[j][k];
		}
	}
	
	model_parameter_a=model_parameter_b=model_parameter_c=0;
	for(i=0;i<sample_time;i++)
	{
		model_parameter_a+=temp1[0][i]*flux_array[i];
		model_parameter_b+=temp1[1][i]*flux_array[i];
		model_parameter_c+=temp1[2][i]*flux_array[i];
	}
	
	//保存试验结果
	CModelSet modelset;
	CString date_str;
	CTime time = CTime::GetCurrentTime();
	date_str.Format("%4d-%02d-%02d",time.GetYear(),time.GetMonth(),time.GetDay());

	modelset.Open();
	modelset.MoveFirst();
	while(!modelset.IsEOF())
	{
		modelset.Edit();
		modelset.m_selected=false;
		modelset.Update();
		modelset.MoveNext();
	}
	if(!(modelset.Find(AFX_DAO_FIRST,"[日期]='"+date_str+"'")))
		modelset.AddNew();
	else
		modelset.Edit();
	model_date=date_str;
	modelset.m_selected=true;
	modelset.m_date=date_str;
	modelset.m_const=model_parameter_a;
	modelset.m_first=model_parameter_b;
	modelset.m_second=model_parameter_c;
	modelset.m_tare=model_parameter_tare;
	modelset.Update();
	modelset.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -