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

📄 simuflatview.cpp

📁 我自己早期编写的闭环控制小程序。一个DLL实现的PID控制器
💻 CPP
字号:
// SimuFlatView.cpp : implementation of the CSimuFlatView class
//

#include "stdafx.h"
#include "SimuFlat.h"

#include "SimuFlatDoc.h"
#include "SimuFlatView.h"
#include "diaMV.h"
#include "DigPID.h"
#include "DigPIDParam.h"
#include "PIDControl.h"

#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView

IMPLEMENT_DYNCREATE(CSimuFlatView, CView)

BEGIN_MESSAGE_MAP(CSimuFlatView, CView)
	//{{AFX_MSG_MAP(CSimuFlatView)
	ON_WM_TIMER()
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_STOP, OnStop)
	ON_COMMAND(ID_MANUL, OnManul)
	ON_COMMAND(ID_AUTO, OnAuto)
	ON_COMMAND(ID_SP, OnSp)
	ON_COMMAND(ID_PID, OnPid)
	ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
	ON_UPDATE_COMMAND_UI(ID_AUTO, OnUpdateAuto)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView construction/destruction

CSimuFlatView::CSimuFlatView()
{
	// TODO: add construction code here
	
	nRows=10;
	Running=FALSE;
	m_CurrentTool=ID_STOP;
}

CSimuFlatView::~CSimuFlatView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView drawing

void CSimuFlatView::OnDraw(CDC* pDC)
{
	CSimuFlatDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	RECT rect,rect1,rect2;
	GetClientRect(&rect);
	pDC->FillSolidRect(&rect,RGB(0,0,0));
	CPen pen;
	pen.CreatePen(PS_SOLID,10,RGB(255,255,255));
	pDC->SelectObject(&pen);
	rect1.left=rect.left+20;
	rect1.top=rect.top+20;
	rect1.bottom=rect.bottom/2-10;
	rect1.right=rect.right-250;
	pDC->Rectangle(&rect1);
	pDC->FillSolidRect(&rect1,RGB(0, 250, 140));
	rect2=rect1;
	rect2.top=rect1.bottom+20;
	rect2.bottom=rect.bottom-20;
	pDC->Rectangle(&rect2);
	pDC->FillSolidRect(&rect2,RGB(0, 250, 140));
	ox1=rect1.left+20;
	oy1=rect1.bottom-20;
	cx1=rect1.right-10;
	cy1=rect1.top;
	DrawGrid(pDC,ox1,oy1,cx1,cy1,RGB(0,0,0));
	ox2=rect2.left+20;
	oy2=rect2.bottom-20;
	cx2=rect2.right-10;
	cy2=rect2.top;
	DrawGrid(pDC,ox2,oy2,cx2,cy2,RGB(0,0,0));
	if(Running)
	{
		CString str;
		str.Format("PV:%f",pDoc->pv[0]);
		DrawData(pDC,cx1+50,cy1,cx1+220,cy1+50,str);
		str.Format("CV:%f",pDoc->cv[0]);
		DrawData(pDC,cx1+50,cy1+80,cx1+220,cy1+130,str);
		str.Format("SP:%f",pDoc->newsp);
		DrawData(pDC,cx1+50,cy1+160,cx1+220,cy1+210,str);
		str.Format("MV:%f",pDoc->cv[0]);
		DrawData(pDC,cx1+50,cy1+240,cx1+220,cy1+290,str);
		RECT rect;
		rect.left=cx1+50;
		rect.top=cy1+240;
		rect.bottom=cx1+220;
		rect.right=cy1+290;
		DrawCurv(pDC,RGB(255,0,0));	
	}
}

void CSimuFlatView::DrawGrid(CDC *pDC,int ox,int oy,int cx,int cy,COLORREF bcolorCell)
{
	nCellHeight=(oy-cy)/nRows;
	nCellWidth=nCellHeight;
	//	CString str;
	CPen pen;
	pen.CreatePen(PS_SOLID,1,bcolorCell);
	pDC->SelectObject(&pen);
	int xt=ox;
	while(xt<cx)
	{	
		pDC->MoveTo(xt,oy);
		pDC->LineTo(xt,cy);
		xt+=nCellWidth;
	}
	int yt=oy;
	while(yt>cy)
	{	
		pDC->MoveTo(ox,yt);
		pDC->LineTo(cx,yt);
		yt-=nCellHeight;
	} 	   
}

void CSimuFlatView::DrawData(CDC *pDC,int ox,int oy,int cx,int cy,CString str)
{
	RECT rect={ox,oy,cx,cy};
	CPen pen;
	pen.CreatePen(PS_SOLID,10,RGB(255,255,0));
	pDC->SelectObject(&pen);
	pDC->Rectangle(&rect);
	pDC->FillSolidRect(&rect,RGB(255, 0, 0));
	pDC->TextOut(ox+10,oy+15,str,strlen(str));
}

void CSimuFlatView::DrawCurv(CDC *pDC,COLORREF bcolorCell)
{
	CSimuFlatDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	int i,ay;
	float maxpv1,minpv1,maxcv1,mincv1,lx;
	float *pv1,*cv1;

	CPen pen;
	pen.CreatePen(PS_SOLID,1,bcolorCell);
	pDC->SelectObject(&pen);
	pv1=pDoc->pv;
	cv1=pDoc->cv;
	maxpv1=pDoc->maxpv;
	minpv1=pDoc->minpv;
	maxcv1=pDoc->maxcv;
	mincv1=pDoc->mincv;
	lx=(maxpv1-minpv1)/(nRows-1);
	if(lx!=0)
	{
		float t=(float)exp(log(10)*((int)log10(lx)-1));		
		lx=((int)(lx/t)+1)*t;
		minpv1=((int)(minpv1/lx)-1)*lx;
	}
	else
	{
		lx=1;
		minpv1=minpv1-1;
	}
	ay=oy1-(int)((pv1[0]-minpv1)/lx/10*(oy1-cy1));
	pDC->MoveTo(cx1,ay);
	for(i=1;(i<=999)&&(cx1-i>=ox1);i++)
	{
		ay=oy1-(int)((pv1[i]-minpv1)/lx/10*(oy1-cy1));
		pDC->LineTo(cx1-i,ay);		
	}
	pDC->SetBkMode(TRANSPARENT);
	int t=oy1;
	for(i=0;i<=nRows;i++)
	{
		CString str;
		str.Format("%.1f",minpv1+i*lx);
		pDC->TextOut(ox1-20,t-10,str);
		t-=nCellHeight;
	}

	lx=(maxcv1-mincv1)/(nRows-1);
	if(lx!=0)
	{
		float t=(float)exp(log(10)*((int)log10(lx)-1));
		lx=((int)(lx/t)+1)*t;
		mincv1=((int)(mincv1/lx)-1)*lx;
	}
	else
	{
		lx=1;
		mincv1=mincv1-1;
	}
	ay=oy2-(int)((cv1[0]-mincv1)/lx/10*(oy2-cy2));
	pDC->MoveTo(cx2,ay);
	for(i=1;(i<=999)&&(cx2-i>=ox2);i++)
	{
		ay=oy2-(int)((cv1[i]-mincv1)/lx/10*(oy2-cy2));
		pDC->LineTo(cx2-i,ay);		
	}
	pDC->SetBkMode(TRANSPARENT);
	t=oy2;
	for(i=0;i<=nRows;i++)
	{
		CString str;
		str.Format("%.1f",mincv1+i*lx);
		pDC->TextOut(ox2-20,t-10,str);
		t-=nCellHeight;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSimuFlatView message handlers

void CSimuFlatView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CSimuFlatDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	pDoc->CaculData();
	CView::OnTimer(nIDEvent);
}

void CSimuFlatView::OnStart() 
{
	// TODO: Add your command handler code here
	SetTimer(1,1000,NULL);	
	Running=TRUE;
	m_CurrentTool=ID_START;
}

void CSimuFlatView::OnStop() 
{
	// TODO: Add your command handler code here
	KillTimer(1);
	m_CurrentTool=ID_STOP;
}

void CSimuFlatView::OnManul() 
{
	// TODO: Add your command handler code here
	diaMV diaMV1;
	diaMV1.DoModal();
}

void CSimuFlatView::OnAuto() 
{
	// TODO: Add your command handler code here
	CSimuFlatDoc* pDoc=GetDocument();
	if(pDoc->Auto_Man)
	{
		pDoc->Auto_Man=FALSE;
	}
	else
	{
		pDoc->Auto_Man=TRUE;
		StartControl();
	}
	m_CurrentTool=ID_AUTO;

}

void CSimuFlatView::OnSp() 
{
	// TODO: Add your command handler code here
	CDigPID digSP;
	digSP.DoModal();	
}

void CSimuFlatView::OnPid() 
{
	// TODO: Add your command handler code here
	CDigPIDParam digPID;
	digPID.DoModal();	
}

void CSimuFlatView::OnUpdateStart(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_CurrentTool==ID_START?1:0);	
}

void CSimuFlatView::OnUpdateAuto(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_CurrentTool==ID_AUTO?1:0);
}

⌨️ 快捷键说明

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