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

📄 daoli2view.cpp

📁 用几种规则控制倒立摆的模糊运动的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DAOLI2View.cpp : implementation of the CDAOLI2View class
//

#include "stdafx.h"
#include "DAOLI2.h"

#include "DAOLI2Doc.h"
#include "DAOLI2View.h"

#include "SETUP.h"
#include "math.h"

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

extern  bool DeListRules[RulesCounter];
extern int poleangle;
extern int polespeed;
/////////////////////////////////////////////////////////////////////////////
// CDAOLI2View

IMPLEMENT_DYNCREATE(CDAOLI2View, CView)

BEGIN_MESSAGE_MAP(CDAOLI2View, CView)
	//{{AFX_MSG_MAP(CDAOLI2View)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_PAINT()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDAOLI2View construction/destruction

CDAOLI2View::CDAOLI2View():nCarx(0),dcarF(0.0),bsettimer(false)
{
   
	// TODO: add construction code here
	blackBrush.CreateSolidBrush(RGB(0,0,0));
	dkgrayBrush.CreateSolidBrush(RGB(128,128,128));
    ltgrayBrush.CreateSolidBrush(RGB(192,192,192));

	blackPen.CreatePen(PS_SOLID,1,RGB(0,0,0));
	grayPen.CreatePen(PS_SOLID,4,RGB(192,192,192));

	
}

CDAOLI2View::~CDAOLI2View()
{
}


/////////////////////////////////////////////////////////////////////////////
// CDAOLI2View drawing

void CDAOLI2View::OnDraw(CDC* pDC)
{
	//CDAOLI2Doc* pDoc = GetDocument();
	//ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here	
}

void CDAOLI2View::OnInitialUpdate()
{
	CView::OnInitialUpdate();
   

	// TODO: This code navigates to a popular spot on the web.
	//  change the code to go where you'd like.
	//Navigate2(_T("http://www.sdu.edu.cn"),NULL,NULL);
}

/////////////////////////////////////////////////////////////////////////////
// CDAOLI2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDAOLI2View message handlers


BOOL CDAOLI2View::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CView::PreCreateWindow(cs);
}


int CDAOLI2View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{

	for(int i=0;i<RulesCounter;i++)
	{
		bViewRules[i]=DeListRules[i];
	}
	dVpoleangle=(double)poleangle;
	dVpolespeed=(double)polespeed;
	dpaiAngle=dVpoleangle*3.1415926/180.00;

    CClientDC dc(this);

	//dc.SetBkColor(RGB(255,255,255));

	//dc.CreateCompatibleDC(NULL);

	//ReleaseDC(hdc);
	
	


	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

   
	return 0;
}

void CDAOLI2View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	if(bsettimer)
	{
		if((dVpoleangle>-90)&&(dVpoleangle<90))
		{
			// TODO: Add your message handler code here and/or call default
			GetcarF();
			CRect rect;
			GetClientRect(rect);

			nCarx=nCarx+(int)(dcarF*(double)rect.right/1000.0);
			
        
			
			//mypDC

			
			
			
		    bool t1=(abs((int)dVpoleangle)<1),t2=(abs((int)dVpolespeed)<1),t4=(dVpoleangle>=90),t5=(dVpoleangle<=-90);
			if(t4)
			{
				dVpoleangle=90.0;
			}
			if(t5)
			{
				dVpoleangle=-90.0;
			}
			poleangle=(int)dVpoleangle;
			polespeed=(int)dVpolespeed;
			bool t3;
			t3=t1&&t2;
			if(t3||t4||t5)
			{
				bsettimer=false;
				KillTimer(1);
			}
			else
			{
				Invalidate();
			}
		}
	}
	//CView::OnTimer(nIDEvent);
}

void CDAOLI2View::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	
	if((dVpoleangle>-90)&&(dVpoleangle<90))
	{
		// TODO: Add your message handler code here and/or call default
		GetcarF();
		CRect rect;
		GetClientRect(rect);

		nCarx=nCarx+(int)(dcarF*(double)rect.right/1000.0);

		if(dVpoleangle>90)
		{
			dVpoleangle=90;
		}
		else if(dVpoleangle<-90)
		{
			dVpoleangle=-90;
		}
	
		//mypDC

		poleangle=(int)dVpoleangle;
		polespeed=(int)dVpolespeed;
		
	
		Invalidate();
	}
		//CView::OnLButtonDblClk(nFlags, point);
}


void CDAOLI2View::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	OnPrepareDC(&dc);


	
	// TODO: Add your message handler code here

	for(int i=0;i<RulesCounter;i++)
	{
		bViewRules[i]=DeListRules[i];
	}
	dVpoleangle=(double)poleangle;
	dVpolespeed=(double)polespeed;
	dpaiAngle=dVpoleangle*3.1415926/180.00;
	
	
	
	CRect rect;
	GetClientRect(rect);
	CBrush *poldBrush=dc.SelectObject(&blackBrush);
	//dc.Rectangle(rect);


	double height=(double)rect.bottom;
	double width=(double)rect.right;

	CRect rectBackground(rect.left,(int)(height/8.0),rect.right,(int)(height*7.0/8.0));
    dc.SelectObject(&dkgrayBrush);
    dc.Rectangle(rectBackground);
	

	CRect rectCar((int)(width*7.0/16.0)+nCarx,(int)(height*3.0/4.0),(int)(width*9.0/16.0)+nCarx,(int)(height*7.0/8.0));
    dc.SelectObject(&ltgrayBrush);
    dc.Rectangle(rectCar);

	dc.SelectObject(*poldBrush);

	CPen  *poldPen=dc.SelectObject(&grayPen);
    CPoint polestart,poleend;
	polestart.x=rectCar.CenterPoint().x;
	polestart.y=(int)(rectCar.CenterPoint().y-(double)rectCar.Height()/2.0);

	poleend.x=(int)(polestart.x+height/2.0*sin(dpaiAngle));
	poleend.y=(int)(polestart.y-height/2.0*cos(dpaiAngle));

	dc.MoveTo(polestart);
	dc.LineTo(poleend);

	dc.SelectObject(*poldPen);




	
	// Do not call CView::OnPaint() for painting messages
}

void CDAOLI2View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

     if(!bsettimer)
	 {
		 			 
		 CClientDC mydc(this);
		 CWnd *cwnd=mydc.GetWindow();
		 HWND hwnd=cwnd->GetSafeHwnd();
		 KillTimer(1);
		 cwnd->SetTimer(1,20,NULL);
		 bsettimer=true;
	 }

	 
	//CView::OnRButtonDown(nFlags, point);
}


void CDAOLI2View::GetcarF()
{
	    double newangle=dVpoleangle;           //用到的中间变量
		double newspeed=dVpolespeed;
			
		double ax=dVpoleangle/15.0,bx=dVpolespeed/60.0;
		double ya1=0.0,ya2=0.0;
		double yb1=0.0,yb2=0.0;
		double yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;
		double cx0=0.0;

		dcarF=0.0;

		//state (1)
		if((newangle<-15)&&(newspeed>=-30)&&(newspeed<30))
		{
			if(bx<0)               //输入隶属度函数的确定
			{
				yb1=1+2.0*bx;
			}
			else
			{
				yb1=1-2.0*bx;
			}

			//R[0]
			if(bViewRules[0])       //推理
			{	
				yc1=yb1;		
			}
			else
			{
				yc1=0.0;
			}
			
			if(bViewRules[0])       //解模糊
			{
				cx0=-0.25*yc1-0.75;
			}
			else
			{
				cx0=0.0;
			}

			ya1=0.0,ya2=0.0;        //还原输入中间变量
			yb1=0.0,yb2=0.0;
			yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;
		}
		//state (2)
		else if((newangle>=-15)&&(newangle<-7.5)&&(newspeed>=0.0)&&(newspeed<30))
		{
			ya1=-2.0*ax-1,ya2=2*ax+2;   //输入隶属度函数的确定
			yb1=1-2.0*bx,yb2=2*bx;

			//R[0],R[1],R[2]并行推理

			if(bViewRules[0])   //R[0]推理
			{
				if(ya1<yb1)    
				{
					yc1=ya1;
				}
				else
				{
					yc1=yb1;
				}
			}
			else
			{
				yc1=0.0;
			}

			if(bViewRules[1])   //R[1]推理
			{
				if(ya2<yb1)
				{
					yc2=ya2;
				}
				else
				{
					yc2=yb1;
				}
			}
			else
			{
				yc2=0.0;
			}

			if(bViewRules[2])   //R[2]推理
			{
				if(ya2<yb2)
				{
					yc3=ya2;
				}
				else
				{
					yc3=yb2;
				}
			}
			else
			{
				yc3=0.0;
			}
			
			//解模糊
			if(bViewRules[0]||bViewRules[1]||bViewRules[2])
			{
				if((yc1>=yc2)&&(yc1>=yc3))
				{
					cx0=-0.25*yc1-0.75;
				}
				else if((yc2>=yc1)&&(yc2>=yc3))
				{
					cx0=0.25*yc2-0.75;
				}
				else
				{
					cx0=0.25*yc3-0.25;
				}
			}
			else
			{
				cx0=0.0;
			}

			ya1=0.0,ya2=0.0;        //还原输入中间变量
			yb1=0.0,yb2=0.0;
			yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;
		}
		//state (3)
		else if((newangle>=-15)&&(newangle<-7.5)&&(newspeed>=-30.0)&&(newspeed<0))
		{
			ya1=-2.0*ax-1,ya2=2*ax+2;   //输入隶属度函数的确定
			yb1=2.0*bx+1.0,yb2=-2*bx;

			//R[0],R[1]并行推理

			if(bViewRules[0])   //R[0]推理
			{
				if(ya1<yb1)    
				{
					yc1=ya1;
				}
				else
				{
					yc1=yb1;
				}
			}
			else
			{
				yc1=0.0;
			}

			if(bViewRules[1])   //R[1]推理
			{
				if(ya2<yb1)
				{
					yc2=ya2;
				}
				else
				{
					yc2=yb1;
				}
			}
			else
			{
				yc2=0.0;
			}

			//解模糊
			if(bViewRules[0]||bViewRules[1])
			{
				if(yc1>yc2)
				{
					cx0=-0.25*yc1-0.75;
				}
				else
				{
					cx0=0.25*yc2-0.75;
				}
			}
			else
			{
				cx0=0.0;
			}

			ya1=0.0,ya2=0.0;        //还原输入中间变量
			yb1=0.0,yb2=0.0;
			yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;

		}
		//state (4)
		else if((newangle>=-7.5)&&(newangle<0)&&(newspeed>=-30.0)&&(newspeed<0))
		{
			ya1=2.0*ax+1,ya2=-2*ax;   //输入隶属度函数的确定
			yb1=2.0*bx+1,yb2=-2*bx;

			//R[5],R[4],R[1]并行推理

			if(bViewRules[5])   //R[5]推理
			{
				if(ya1<yb1)    
				{
					yc1=ya1;
				}
				else
				{
					yc1=yb1;
				}
			}
			else
			{
				yc1=0.0;
			}

			if(bViewRules[4])   //R[4]推理
			{
				if(ya1<yb2)
				{
					yc2=ya1;
				}
				else
				{
					yc2=yb2;
				}
			}
			else
			{
				yc2=0.0;
			}

			if(bViewRules[1])   //R[1]推理
			{
				if(ya2<yb1)
				{
					yc3=ya2;
				}
				else
				{
					yc3=yb1;
				}
			}
			else
			{
				yc3=0.0;
			}
			
			//解模糊
			if(bViewRules[5]||bViewRules[4]||bViewRules[1])
			{
				if((yc1>=yc2)&&(yc1>=yc3))
				{
					cx0=0.25*yc1-0.25;
				}
				else if((yc2>=yc1)&&(yc2>=yc3))
				{
					cx0=-0.25*yc2-0.25;
				}
				else
				{
					cx0=-0.25*yc3-0.25;
				}
			}
			else
			{
				cx0=0.0;
			}

			ya1=0.0,ya2=0.0;        //还原输入中间变量
			yb1=0.0,yb2=0.0;
			yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;
		}
		//state (5)
		else if((newangle>=-7.5)&&(newangle<0)&&(newspeed>=0.0)&&(newspeed<30.0))
		{
			ya1=2.0*ax+1,ya2=-2*ax;   //输入隶属度函数的确定
			yb1=2.0*bx,yb2=1-2*bx;

			//R[6],R[5],R[2],R[1]并行推理

			if(bViewRules[6])   //R[6]推理
			{
				if(ya1<yb1)    
				{
					yc1=ya1;
				}
				else
				{
					yc1=yb1;
				}
			}
			else
			{
				yc1=0.0;
			}

			if(bViewRules[5])   //R[5]推理
			{
				if(ya1<yb2)
				{
					yc2=ya1;
				}
				else
				{
					yc2=yb2;
				}
			}
			else
			{
				yc2=0.0;
			}

			if(bViewRules[2])   //R[2]推理
			{
				if(ya2<yb1)
				{
					yc3=ya2;
				}
				else
				{
					yc3=yb1;
				}
			}
			else
			{
				yc3=0.0;
			}

			if(bViewRules[1])   //R[1]推理
			{
				if(ya2<yb2)
				{
					yc4=ya2;
				}
				else
				{
					yc4=yb2;
				}
			}
			else
			{
				yc4=0.0;
			}
			
			//解模糊
			if(bViewRules[6]||bViewRules[5]||bViewRules[2]||bViewRules[1])
			{
				if((yc1>=yc2)&&(yc1>=yc3)&&(yc1>=yc4))
				{
					cx0=0.25*yc1+0.25;
				}
				else if((yc2>=yc1)&&(yc2>=yc3)&&(yc2>=yc4))
				{
					if(ya1<yb2)
					{
						cx0=0.25*yc2-0.25;
					}
					else
					{
						cx0=-0.25*yc2+0.25;
					}
				}
				else if((yc3>=yc1)&&(yc3>=yc2)&&(yc3>=yc4))
				{
					if(ya2<yb1)
					{
						cx0=0.25*yc3-0.25;
					}
					else
					{
						cx0=-0.25*yc3+0.25;
					}
				}
				else
				{
					cx0=-0.25*yc4-0.25;
				}
			}
			else
			{
				cx0=0.0;
			}

			ya1=0.0,ya2=0.0;        //还原输入中间变量
			yb1=0.0,yb2=0.0;
			yc1=0.0,yc2=0.0,yc3=0.0,yc4=0.0;
		}
		//state (6)
		else if((newangle>=-7.5)&&(newangle<0)&&(newspeed>=30.0)&&(newspeed<60.0))
		{
			ya1=2.0*ax+1,ya2=-2*ax;   //输入隶属度函数的确定
			yb1=-2.0*bx+2,yb2=2*bx-1;

			//R[6],R[7],R[2]并行推理

			if(bViewRules[6])   //R[6]推理
			{
				if(ya1<yb1)    
				{
					yc1=ya1;
				}
				else
				{
					yc1=yb1;
				}
			}
			else
			{
				yc1=0.0;
			}

			if(bViewRules[7])   //R[7]推理
			{
				if(ya1<yb2)
				{
					yc2=ya1;
				}
				else
				{
					yc2=yb2;
				}
			}
			else
			{
				yc2=0.0;
			}

			if(bViewRules[2])   //R[2]推理
			{
				if(ya2<yb1)
				{
					yc3=ya2;
				}
				else
				{
					yc3=yb1;
				}
			}
			else
			{
				yc3=0.0;
			}
			
			//解模糊
			if(bViewRules[6]||bViewRules[7]||bViewRules[2])
			{
				if((yc1>=yc2)&&(yc1>=yc3))
				{
					cx0=-0.25*yc1+0.75;
				}
				else if((yc2>=yc1)&&(yc2>=yc3))
				{
					cx0=0.25*yc2+0.75;
				}
				else
				{
					cx0=-0.25*yc3+0.25;
				}
			}
			else
			{

⌨️ 快捷键说明

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