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

📄 piddlg.cpp

📁 实现了PID控制的仿真显示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PIDDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PID.h"
#include "PIDDlg.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <float.h>
 

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

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

/////////////////////////////////////////////////////////////////////////////
// CPIDDlg dialog

CPIDDlg::CPIDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPIDDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPIDDlg)
	m_divTd = 0.0f;
	m_integralTi = 0.0f;
	m_integralV = 0.0f;
	m_paramK1 = 0.0f;
	m_paramT1 = 0.0f;
	m_preparamI = 0.0f;
	m_preparamK = 0.0f;
	m_proportionKp = 0.0f;
	m_sampleT = 0.0f;
	m_stepR0 = 0.0f;
	m_stepU0 = 0.0f;
	m_chuanxian = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPIDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPIDDlg)
	DDX_Text(pDX, IDC_DIVTd, m_divTd);
	DDX_Text(pDX, IDC_INTEGRALTi, m_integralTi);
	DDX_Text(pDX, IDC_INTEGRALVALUE, m_integralV);
	DDX_Text(pDX, IDC_PARAMK1, m_paramK1);
	DDX_Text(pDX, IDC_PARAMT1, m_paramT1);
	DDX_Text(pDX, IDC_PREPARAMI, m_preparamI);
	DDX_Text(pDX, IDC_PREPARAMK, m_preparamK);
	DDX_Text(pDX, IDC_PROPORTIONKp, m_proportionKp);
	DDX_Text(pDX, IDC_SAMPLET, m_sampleT);
	DDX_Text(pDX, IDC_STEPR0, m_stepR0);
	DDX_Text(pDX, IDC_STEPU0, m_stepU0);
	DDX_Text(pDX, IDC_EDIT_CHUXIAN, m_chuanxian);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPIDDlg, CDialog)
	//{{AFX_MSG_MAP(CPIDDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONFIG, OnConfig)
	ON_BN_CLICKED(IDC_CHUANDI, OnChuandi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPIDDlg message handlers

BOOL CPIDDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	UpdateData(true);

	// 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
	for(int i=0;i<100;i++)
	{
		YI[i]=0;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPIDDlg::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 CPIDDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	 	// TODO: Add your message handler code here
	show_data();
	
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPIDDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CPIDDlg::show_data()
{ 
	CWnd* pWnd=GetDlgItem(IDC_STATIC_SHOW);	
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	CDC *pDC=pWnd->GetDC();
//	pDC->SetBkColor(RGB(0,255,255));
	CPen pen_bian,*poldpen1;
	pen_bian.CreatePen(PS_SOLID,2,RGB(0,0,255));

	poldpen1=pDC->SelectObject(&pen_bian);

    CPen pen_bian2,*poldpen2;
   	pen_bian2.CreatePen(PS_SOLID,1,RGB(255,0,0));
	poldpen2=pDC->SelectObject(&pen_bian2);

	CPen pen_grid,*poldpen3;
	pen_grid.CreatePen(PS_DOT,1,RGB(0,0,255));
	const int num=100;
    double initX[num];//={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    double initY[num];//={2,5,1,8,8,17,25,35.2,40.1,50,33.3,28.4,22.2,24.1,26.2,18.3,14.1,24.7,18,6};
	//分别取得X和Y最大值,最小值
	initX[0]=0;
	initY[0]=0;
	 for(int i=0;i<num;i++)
	 {
		 initX[i]=i+1;
		 initY[i]=i+1;
	 }
	UpdateData();
 	double U[num];
	   U[0]=0;
   double Y[num];
	    Y[0]=0;
	    Y[1]=0;
      for( i=2;i<num;i++)
	   {
		   U[i-1]=m_stepU0;		  
		  double a=(-1)*(m_sampleT)/(m_paramT1);
		  double b=(-2)*(m_sampleT)/(m_paramT1);
		  double c= exp(a);
		  double d= exp(b);
		   Y[i]=2*c*(Y[i-1])-d*(Y[i-2])+(m_paramK1)*(1-c+a*c)*(U[i-1])-(m_paramK1)*(1-c+a)*c*(U[i-2]);
	  }
       double maxX=initX[0];
       double minX=initX[0];
       double maxY=initY[0];
       double minY=initY[0];
       double MAXY=Y[0];
       double MINY=Y[0];
       for(  i=0;i<num;i++)
       {
              if(initX[i]>maxX)
                     maxX=initX[i];
              if(initX[i]<minX)
                     minX=initX[i];
              if(initY[i]>maxY)
                     maxY=initY[i];
              if(initY[i]<minY)
                     minY=initY[i];
			  if(Y[i]>MAXY)
			         MAXY=Y[i];
			  if(Y[i]<MINY)
			         MINY=Y[i];
       } 
// m_chuanxian=MAXY;	  
      //如果原点必须在X轴上,加上下面2行,否则注释掉

⌨️ 快捷键说明

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