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

📄 watertankdlg.cpp

📁 三容水箱的模糊控制设计
💻 CPP
字号:
// WatertankDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Watertank.h"
#include "WatertankDlg.h"
#include "Windows.h"
#include "stdio.h"
#include "math.h"
#include "Mserial15.h"
#include "PidControl.h"
#include "Reference.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define a   0.0625
#define b  -20.9866
////////////////////////////////////////////////////////////////////////////
IOPort m_PCI2006_0;//定义0号数据采集卡
IOPort m_PCI2006_1;//定义1号数据采集卡
Produce_WhiteNoise WhiteNoise;
CMserial Mserial;
DataSave Save_Data;
CMserial15 Mserial15;
CReference Reference;
double ADValue0[3];//存储0#采集卡采集出来的AD值
double DAValue0[2];
double DAValue1[2];
double ADValue00=0;
double buffer[2];
double reference;
BOOL m_bbuffer=TRUE;
UINT Time_Proc(LPVOID lParam);//采集定时器线程
VOID Samp_Proc();//采样子程序
VOID View_Proc();//文本框显示子程序
UINT Time_Proc1(LPVOID lParam);
UINT Time_Proc2(LPVOID lParam);


int in=0;//存储数据时的数据序列

CPidControl pid;


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWatertankDlg dialog

CWatertankDlg::CWatertankDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWatertankDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWatertankDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWatertankDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWatertankDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWatertankDlg, CDialog)
	//{{AFX_MSG_MAP(CWatertankDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_STARTD, OnStartd)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_SAVADATA, OnSavadata)
	ON_BN_CLICKED(IDC_AddWhiteNoise, OnAddWhiteNoise)
	ON_BN_CLICKED(IDC_STOPSAVEDATA, OnStopsavedata)
	ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWatertankDlg message handlers

BOOL CWatertankDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	// determine the rectangle for the control
  CRect rect;
  GetDlgItem(IDC_OSCOPE)->GetWindowRect(rect) ;
  ScreenToClient(rect) ;

  // create the control
  m_OScopeCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this) ; 

  // customize the control
  m_OScopeCtrl.SetRange(-1, 1, 2) ;
  m_OScopeCtrl.SetYUnits("液位值") ;
  m_OScopeCtrl.SetXUnits("Samples (Windows Timer: 50 msec)") ;
  m_OScopeCtrl.SetBackgroundColor(RGB(0, 0, 64)) ;
  m_OScopeCtrl.SetGridColor(RGB(192, 192, 255)) ;
  m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;

  //------
 
  StartWhiteNoise=FALSE;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWatertankDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWatertankDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWatertankDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
LRESULT CWatertankDlg:: OnMyMessage(WPARAM wParam, LPARAM lParam)
{   
	
	CString str,str1;
	
	GetDlgItemText(IDC_EDIT4,str);
	GetDlgItemText(IDC_EDIT5,str1);
//atof(str1);//Signal.Produce();//atof(str1);
	DAValue1[1]=0;
	if(StartWhiteNoise==FALSE)
		DAValue1[0]=atof(str);
	else
	{	
		reference=atof(str1)+Reference.produce();//atof(str1);
		
     	DAValue1[0]=Mserial.Produce_Mserial();//WhiteNoise.ProduceWhiteNoise();//pid.CalcU(ADValue00*a+b,reference);//DAValue1[0]=Signal.Produce();//Reference.produce();atof(str)+Mserial.Produce_Mserial()*0.16;//+WhiteNoise.ProduceWhiteNoise();
	}
	m_PCI2006_1.WriteData(DAValue1);//;
	
	
     DAValue0[0]=0;
 	 DAValue0[1]=atof(str1);//Mserial15.Produce_Mserial()*0.2+3.5;//atof(str);
	 m_PCI2006_0.WriteData(DAValue0);
	return 0;

}

void CWatertankDlg::OnStartd() 
{   
	m_PCI2006_0.Open(0);
	m_PCI2006_1.Open(1);
	m_PCI2006_1.reset();
	m_PCI2006_0.reset();
	m_hwind=AfxBeginThread(Time_Proc,this);//开始数据采集线程
	m_hwind1=AfxBeginThread(Time_Proc1,this);//开始输入线程
    SetTimer(1,1000,NULL);//开始曲线显示,打开出水阀门,显示输出值
  


		
		
	

	// TODO: Add your control notification handler code here
	
}
UINT Time_Proc(LPVOID lParam)
{
	LARGE_INTEGER  litmp; 
	LONGLONG       QPart1,QPart2;
	double         dfMinus, dfFreq, dfTim; 
	QueryPerformanceFrequency(&litmp); 
	dfFreq = (double)litmp.QuadPart; // 获得计数器的时钟频率
	QueryPerformanceCounter(&litmp); 
	QPart1 = litmp.QuadPart; // 获得初始值
	QPart2=QPart1;
	
	while(TRUE)
	{
		QPart1 = QPart2;// 上一次的终止值变成新的起始值
		//-----------------------------------------------------------------------
         Samp_Proc();  
	   
	    
	
	
		//-----------------------------------------------------------------------
	
		do{
			QueryPerformanceCounter(&litmp); 
			QPart2 = litmp.QuadPart;// 获得中止值 
			dfMinus = (double)(QPart2-QPart1); 
			dfTim = dfMinus / dfFreq; // 获得对应的时间值,单位为秒
		}while(dfTim<0.001);
	}
	return 0;
}
UINT Time_Proc1(LPVOID lParam)
{   
	CWatertankDlg *pDlg=(CWatertankDlg *)lParam;

	LARGE_INTEGER  litmp; 
	LONGLONG       QPart1,QPart2;
	double         dfMinus, dfFreq, dfTim; 
	QueryPerformanceFrequency(&litmp); 
	dfFreq = (double)litmp.QuadPart; // 获得计数器的时钟频率
	QueryPerformanceCounter(&litmp); 
	QPart1 = litmp.QuadPart; // 获得初始值
	QPart2=QPart1;
	
	while(TRUE)
	{
		QPart1 = QPart2;// 上一次的终止值变成新的起始值
		//-----------------------------------------------------------------------
          
	   SendMessage(pDlg->m_hWnd,WM_MYMESSAGE,0,0);
	    
		

	
	
		//-----------------------------------------------------------------------
	
		do{
			QueryPerformanceCounter(&litmp); 
			QPart2 = litmp.QuadPart;// 获得中止值 
			dfMinus = (double)(QPart2-QPart1); 
			dfTim = dfMinus / dfFreq; // 获得对应的时间值,单位为秒
		}while(dfTim<1);
	}
	return 0;
}


VOID Samp_Proc()
{    
   double sum=0;

	if(m_PCI2006_1.IsPortOpen())//step1:检测设备是否启动
		{
			m_PCI2006_1.ReadData(ADValue0);//step2:开始采集数据

		}
	if(m_bbuffer==TRUE)
	{
		for(int i=0;i<2;i++)
			buffer[i]=ADValue0[0];
		m_bbuffer=FALSE;
	}
	for(int j=1;j<2;j++)
	{
	 sum=sum+buffer[j];
	 buffer[j-1]=buffer[j];
	}
	sum=sum+ADValue0[0];
	buffer[1]=ADValue0[0];
	ADValue00=sum/2.0;


}

VOID View_Proc()
{  
	int i;
	CString str[3];
	if(m_PCI2006_1.IsPortOpen())
	{
		for(i=0;i<3;i++)
		{
			str[i].Format("%0.1f",ADValue00*a+b);
			
		}
		SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT3,str[0]);
		//SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT2,str[1]);
	//	SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,str[2]);


	}

}
void CWatertankDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	m_hwind->ExitInstance();//停止数据采集
	m_hwind1->ExitInstance();//停止输入


	KillTimer(1);//停止显示曲线,出水阀门关闭,输出值显示停止;
	m_PCI2006_0.Close();//关闭设备0
	m_PCI2006_1.Close();//关闭设备1
	
    

	
}

void CWatertankDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	  m_OScopeCtrl.AppendPoint(DAValue1[0]);//曲线显示1#液位值ADValue00*a+b
	 //---------------
	 
	  
	  
	  
	  View_Proc();
	  

 
    
	  //---------------
	  
	CDialog::OnTimer(nIDEvent);
}



void CWatertankDlg::OnSavadata() 
{
	// TODO: Add your control notification handler code here
   Save_Data.Save_Initial();
   m_hwind2=AfxBeginThread(Time_Proc2,this);
	  
}
UINT Time_Proc2(LPVOID lParam)
{
	LARGE_INTEGER  litmp; 
	LONGLONG       QPart1,QPart2;
	double         dfMinus, dfFreq, dfTim; 
	QueryPerformanceFrequency(&litmp); 
	dfFreq = (double)litmp.QuadPart; // 获得计数器的时钟频率
	QueryPerformanceCounter(&litmp); 
	QPart1 = litmp.QuadPart; // 获得初始值
	QPart2=QPart1;
	
	while(TRUE)
	{
		QPart1 = QPart2;// 上一次的终止值变成新的起始值
		//-----------------------------------------------------------------------
     in++  ;
		
     Save_Data.Save(in,DAValue1[0],ADValue00*a+b,DAValue0[1],reference);
	   
		

	
	
		//-----------------------------------------------------------------------
	
		do{
			QueryPerformanceCounter(&litmp); 
			QPart2 = litmp.QuadPart;// 获得中止值 
			dfMinus = (double)(QPart2-QPart1); 
			dfTim = dfMinus / dfFreq; // 获得对应的时间值,单位为秒
		}while(dfTim<1);
	}
	return 0;

}

void CWatertankDlg::OnAddWhiteNoise() 
{
	// TODO: Add your control notification handler code here
   Signal.Initial();
   StartWhiteNoise=TRUE;
   WhiteNoise.Initial();


	
}

void CWatertankDlg::OnStopsavedata() 
{
	// TODO: Add your control notification handler code here
	m_hwind2->ExitInstance();//停止存储
//	Save_Data.Save_end();

}

⌨️ 快捷键说明

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