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

📄 temperdlg.cpp

📁 基于模糊原理的温度控制演示程序
💻 CPP
字号:
// temperDlg.cpp : implementation file
//

#include "stdafx.h"
#include "temper.h"
#include "temperDlg.h"
#include "mymath.h"
#include "temperature.h"

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

#define TIMER_1 1
//#define K 0.1
//#define PIX_PER_MS 2			单位时间表示的像素
//#define PIX_PER_T	20			单位温度表示的像素
//#define TEMPNUM		155


/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTemperDlg dialog

CTemperDlg::CTemperDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTemperDlg::IDD, pParent)
{
	int i=0;
	for(i=0;i<5;i++)
		b[i]=0.0f;
	start=0;
	//{{AFX_DATA_INIT(CTemperDlg)
	m_curDE = 0.0f;
	m_a = 2.0f;
	m_curTemper = 5.0f;
	m_desTemper = 2.0f;
	m_nb = -4.0f;
	m_ns = -2.0f;
	m_pb = 4.0f;
	m_ps = 2.0f;
	m_zo = 0.0f;
	m_k = 0.2f;
	//}}AFX_DATA_INIT
	b[0]=m_nb;
	b[1]=m_ns;
	b[2]=m_zo;
	b[3]=m_ps;
	b[4]=m_pb;
	time=0;
	oldTemper=0;
	PIX_PER_MS=2;			//单位时间表示的像素
	PIX_PER_T=20;			//单位温度表示的像素
	TEMPNUM=155;
	temper=new float[500];
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTemperDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTemperDlg)
	DDX_Text(pDX, IDC_CURDE, m_curDE);
	DDX_Text(pDX, IDC_A, m_a);
	DDX_Text(pDX, IDC_CURTEMP, m_curTemper);
	DDX_Text(pDX, IDC_DESTEMP, m_desTemper);
	DDX_Text(pDX, IDC_NB, m_nb);
	DDX_Text(pDX, IDC_NS, m_ns);
	DDX_Text(pDX, IDC_PB, m_pb);
	DDX_Text(pDX, IDC_PS, m_ps);
	DDX_Text(pDX, IDC_ZO, m_zo);
	DDX_Text(pDX, IDC_K, m_k);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTemperDlg, CDialog)
	//{{AFX_MSG_MAP(CTemperDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ACK, OnAck)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_UP, OnUp)
	ON_BN_CLICKED(IDC_DOWN, OnDown)
	ON_BN_CLICKED(IDC_LEFT, OnLeft)
	ON_BN_CLICKED(IDC_RIGHT, OnRight)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTemperDlg message handlers

BOOL CTemperDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTemperDlg::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 CTemperDlg::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
	{
		PAINTSTRUCT ps;
		CDC* pDC = BeginPaint(&ps);
		//PaintFuzzyField(pDC);						
		PaintTemperChg(pDC);
		EndPaint(&ps);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTemperDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTemperDlg::OnAck() 
{
	int i=0;
	UpdateData(TRUE);
	PIX_PER_MS=2;			//单位时间表示的像素
	PIX_PER_T=20;			//单位温度表示的像素
	TEMPNUM=155;
	b[0]=m_nb;
	b[1]=m_ns;
	b[2]=m_zo;
	b[3]=m_ps;
	b[4]=m_pb;
	for(i=0;i<4;i++)
	{
		if(b[i]+m_a<=b[i+1]-m_a)
		{
			MessageBox("ERROR!Fuzzy Field must cover all elements!","ERROR",MB_ICONERROR|MB_OK);
			return;
		}
	}
	start=1;
	curDE=m_curDE;
	curTemper=m_curTemper;
	oldTemper=m_curTemper;
	desTemper=m_desTemper;
	OnPaint();
	RedrawWindow();
	SetTimer(TIMER_1,10,NULL);
	time=0;
}

void CTemperDlg::PaintFuzzyField(CDC *pDC)
{
	float off=0.0;
	int i=0;
	char s[10];
	off=Fabs(m_nb)>Fabs(m_pb)?Fabs(m_nb):Fabs(m_pb);
	RECT rect={7,7,325,150};
	off=off/((rect.right-rect.left)/2-10);
	pDC->Rectangle(&rect);
	//pDC->MoveTo(rect.left,(rect.top+rect.bottom)/2);
	//pDC->LineTo(rect.right,(rect.top+rect.bottom)/2);
	pDC->MoveTo((rect.left+rect.right)/2,rect.top);
	pDC->LineTo((rect.left+rect.right)/2,rect.bottom);
	if(start==1)
	{
		for(i=0;i<5;i++)
		{
			pDC->MoveTo(b[i]/off+(rect.left+rect.right)/2,rect.bottom);
			if(i==0||i==4)
			{
				pDC->LineTo(b[i]/off+(rect.left+rect.right)/2,rect.top+20);
			}
			else
			{
				pDC->LineTo(b[i]/off+(rect.left+rect.right)/2,rect.bottom-10);
			}
			pDC->MoveTo(b[i]/off+(rect.left+rect.right)/2,rect.top+20);
			if(b[i]-m_a-b[0]>-0.001f)
			{
				pDC->LineTo((b[i]-m_a)/off+(rect.left+rect.right)/2,rect.bottom);
			}
			else
			{
				pDC->LineTo((b[0])/off+(rect.left+rect.right)/2,rect.bottom-(m_a-(b[i]-b[0]))/m_a*(rect.bottom-rect.top-20));
			}
			pDC->MoveTo(b[i]/off+(rect.left+rect.right)/2,rect.top+20);
			if(b[i]+m_a-b[4]<0.001f)
			{
				pDC->LineTo((b[i]+m_a)/off+(rect.left+rect.right)/2,rect.bottom);
			}
			else
			{
				pDC->LineTo((b[4])/off+(rect.left+rect.right)/2,rect.bottom-(m_a-(b[4]-b[i]))/m_a*(rect.bottom-rect.top-20));
			}
			pDC->SetBkMode(TRANSPARENT);
			sprintf(s,"%.1f",b[i]);
			pDC->TextOut(b[i]/off+(rect.left+rect.right)/2-3,rect.bottom-18,s);
			pDC->SetBkMode(OPAQUE);
		}
		
	}

}

void CTemperDlg::PaintTemperChg(CDC *pDC)
{
	int i=0;
	char s[50];
	RECT rect={1,1,325,200};
	sprintf(s,"curTemper:%.2f-Time:%d",curTemper,time);
	pDC->Rectangle(&rect);
	pDC->MoveTo(rect.left,(rect.top+rect.bottom)/2);
	pDC->LineTo(rect.right,(rect.top+rect.bottom)/2);
	pDC->MoveTo((rect.left+rect.right)/2,rect.top);
	pDC->LineTo((rect.left+rect.right)/2,rect.bottom);
	for(i=1;i<time%TEMPNUM;i++)
	{
		
		if((rect.left+(i)*PIX_PER_MS)%(rect.right-rect.left)==0)//折返时不画
		{
			pDC->MoveTo((rect.left+(i)*PIX_PER_MS)%(rect.right-rect.left),(temper[i]-desTemper)*PIX_PER_T*(-1)+(rect.top+rect.bottom)/2);
			pDC->LineTo((rect.left+i*PIX_PER_MS)%(rect.right-rect.left),(temper[i]-desTemper)*PIX_PER_T*(-1)+(rect.top+rect.bottom)/2);	
		}
		else
		{
			pDC->MoveTo((rect.left+(i-1)*PIX_PER_MS)%(rect.right-rect.left),(temper[i-1]-desTemper)*PIX_PER_T*(-1)+(rect.top+rect.bottom)/2);
			pDC->LineTo((rect.left+i*PIX_PER_MS)%(rect.right-rect.left),(temper[i]-desTemper)*PIX_PER_T*(-1)+(rect.top+rect.bottom)/2);
		}
		
		pDC->TextOut(rect.left+10,rect.top+20,s);
	}
}

void CTemperDlg::OnTimer(UINT nIDEvent) 
{
	float w;
	if(nIDEvent==TIMER_1&&start==1)
	{
		if(time==5)
		{
			time=5;
		}
		w=GetW(curTemper,desTemper,curDE,b,m_a);
		//if(time>40) m_a=3;
		curDE+=m_k*w;
		curTemper+=curDE;
		temper[time%TEMPNUM]=curTemper;
		time++;
		RedrawWindow();
		
	}
	CDialog::OnTimer(nIDEvent);
}

void CTemperDlg::OnPause() 
{
	start=!start;	
}

void CTemperDlg::OnOK() 
{
	// TODO: Add extra validation here
	delete []temper;
	CDialog::OnOK();
}

void CTemperDlg::OnUp() 
{
	PIX_PER_T+=20;
	RedrawWindow();
}

void CTemperDlg::OnDown() 
{
	if(PIX_PER_T-20>=0)PIX_PER_T-=20;
	RedrawWindow();
	
}

void CTemperDlg::OnLeft() 
{
	PIX_PER_MS++;
	if(PIX_PER_MS!=0)TEMPNUM=310/PIX_PER_MS;
	RedrawWindow();
}

void CTemperDlg::OnRight() 
{
	if(PIX_PER_MS-1>=0)PIX_PER_MS--;
	if(PIX_PER_MS!=0)TEMPNUM=310/PIX_PER_MS;
	RedrawWindow();
}

void CTemperDlg::WinHelp(DWORD dwData, UINT nCmd) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDialog::WinHelp(dwData, nCmd);
}

⌨️ 快捷键说明

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