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

📄 mytctrllorview.cpp

📁 程序实现了模糊控制理论中恒温箱的模拟
💻 CPP
字号:
// MyTCtrllorView.cpp : implementation of the CMyTCtrllorView class
//

#include "stdafx.h"
#include "MyTCtrllor.h"

#include "MyTCtrllorDoc.h"
#include "MyTCtrllorView.h"

#include "TempDlg.h"
#include "EnhanceDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView

IMPLEMENT_DYNCREATE(CMyTCtrllorView, CView)

BEGIN_MESSAGE_MAP(CMyTCtrllorView, CView)
	//{{AFX_MSG_MAP(CMyTCtrllorView)
	ON_COMMAND(ID_TO_TEMP, OnToTemp)
	ON_COMMAND(ID_ENHANCE, OnEnhance)
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_PAUSE, OnPause)
	ON_COMMAND(ID_CLEAR, OnClear)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView construction/destruction

CMyTCtrllorView::CMyTCtrllorView()
{
	// TODO: add construction code here
	for(int i=0;i<200;i++)
		lineY[i]=-10000;
	cnt=0;

	target = 65;//目标温度:65度

	enhance=0.26f; //加速因子

	controllor.T=25; //当前温度
	controllor.enhance=enhance;
	controllor.detaE.value=0;
	controllor.E.value=controllor.T-target;

	sTarT = _T("目标温度:65度");
	sCurT = _T("当前温度:25度");
}

CMyTCtrllorView::~CMyTCtrllorView()
{
}

BOOL CMyTCtrllorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView drawing

void CMyTCtrllorView::OnDraw(CDC* pDC)
{
	CMyTCtrllorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	CPen *oldpen=NULL,pen;
	CBrush *oldbrush=NULL,brush;
	
	this->GetClientRect(&clientRect);

	//计算绘图区
	drawRect.left = (int)(clientRect.left + clientRect.Width()/10.0f);
	drawRect.right = (int)(drawRect.left + clientRect.Width()*4.0f/5);
	drawRect.top = (int)(clientRect.top + clientRect.Height()*3.0f/16);
	drawRect.bottom = (int)(drawRect.top + clientRect.Height()*3.0f/4);
	//将背景涂白
	brush.CreateSolidBrush(RGB(128,128,128));
	oldbrush=(CBrush *)pDC->SelectObject(&brush);
	pDC->Rectangle(&clientRect); 
	//将绘图区涂黑
	brush.DeleteObject();
	brush.CreateSolidBrush(RGB(0,0,0));
	pDC->SelectObject(&brush);
	pDC->Rectangle(&drawRect);

	//显示温度
	pen.CreatePen(PS_SOLID,1,RGB(255,255,0));
	oldpen=(CPen *)pDC->SelectObject(&pen);
	pDC->SelectObject(&pen);
	pDC->TextOut(clientRect.Width()/10.f,clientRect.Height()/16.0f,sTarT);
	pDC->TextOut(clientRect.Width()*3/5.0f,clientRect.Height()/16.0f,sCurT);
	pen.DeleteObject();

	pen.CreatePen(PS_SOLID,2,RGB(0,255,0));
	//oldpen=(CPen *)pDC->SelectObject(&pen);
	pDC->SelectObject(&pen);
	pDC->MoveTo(drawRect.left + 15,drawRect.top+ drawRect.Height()/2);   //画横轴
	pDC->LineTo(drawRect.left +drawRect.Width()-15,drawRect.top+drawRect.Height()/2);
	pDC->MoveTo(drawRect.left+15,drawRect.top+ 15); //画纵轴
	pDC->LineTo(drawRect.left+ 15,drawRect.bottom-15);

	pen.DeleteObject();
	pen.CreatePen(PS_SOLID,1,RGB(255,0,0));
	pDC->SelectObject(&pen);
	//画曲线
	pDC->MoveTo(drawRect.left +15,drawRect.bottom-drawRect.Height()/2-lineY[0]);
	for(int i=1;i<200;i++)
	{
		if(lineY[i]==-10000)
				continue;
		pDC->LineTo(drawRect.left +15+i*2.8f,drawRect.bottom-drawRect.Height()/2-lineY[i]);
	}
	pDC->SelectObject(oldpen);
	pDC->SelectObject(oldbrush);
}

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView printing

BOOL CMyTCtrllorView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyTCtrllorView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyTCtrllorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView diagnostics

#ifdef _DEBUG
void CMyTCtrllorView::AssertValid() const
{
	CView::AssertValid();
}

void CMyTCtrllorView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyTCtrllorDoc* CMyTCtrllorView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyTCtrllorDoc)));
	return (CMyTCtrllorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyTCtrllorView message handlers

void CMyTCtrllorView::OnToTemp() 
{
	// TODO: Add your command handler code here
	CTempDlg dialog;
	dialog.m_target = this->target;
	if(dialog.DoModal() == IDOK)
	{
		this->target = dialog.m_target;

		controllor.T=25;
		controllor.detaE.value=0;
		controllor.E.value=controllor.T-target;

		sTarT.Format("目标温度:%d度",target);
		Invalidate(FALSE);
	}
}

void CMyTCtrllorView::OnEnhance() 
{
	// TODO: Add your command handler code here
	CEnhanceDlg dialog;
	dialog.m_enhance = this->enhance;
	if(dialog.DoModal() == IDOK)
	{
		this->enhance = dialog.m_enhance;

		controllor.enhance=enhance;
	}
}

void CMyTCtrllorView::OnStart() 
{
	// TODO: Add your command handler code here
	SetTimer(1,100,NULL);
}

void CMyTCtrllorView::OnPause() 
{
	// TODO: Add your command handler code here
	KillTimer(1);
}

void CMyTCtrllorView::OnClear() 
{
	// TODO: Add your command handler code here
	int i;
	for(i=0;i<200;i++)
		lineY[i]=-10000;
	cnt=0;
	//m_now.SetWindowText("当前温度:25度");
	controllor.T=25;
	controllor.detaE.value=0;
	controllor.E.value=controllor.T-target;
	//InvalidateRect(&drawRect,FALSE);
	sTarT.Format("目标温度:%d度",target);
	sCurT.Format("当前温度:%d度",25);
	Invalidate(FALSE);
}

void CMyTCtrllorView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
		int i;
	if(cnt==200)
	{
		for(i=0;i<=198;i++)
			lineY[i]=lineY[i+1];
		cnt--;
	}

	controllor.MainContrllor();

	lineY[cnt++]=(int)(controllor.E.value/6*((drawRect.Height()-10)/2));

	//InvalidateRect(&drawRect,FALSE);
	Invalidate(FALSE);

	sCurT.Format("当前温度:%.6f度",target+controllor.E.value);

	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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