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

📄 pidcontrol.cpp

📁 我自己早期编写的闭环控制小程序。一个DLL实现的PID控制器
💻 CPP
字号:
// PIDControl.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "PIDControl.h"

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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CPIDControlApp

BEGIN_MESSAGE_MAP(CPIDControlApp, CWinApp)
	//{{AFX_MSG_MAP(CPIDControlApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPIDControlApp construction

float m_kp,m_ti,m_td;
float m_sp,m_pv,m_cv[2],m_err[3];
int m_ts;
BOOL Running;

CPIDControlApp::CPIDControlApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	
	m_kp=0.1;
	m_ti=1.0;
	m_td=0.0;
	m_ts=1000;
	Running=FALSE;
}

BOOL SetPIDPara(float Kp,float Ti,float Td,int Ts)
{
	m_kp=Kp;
	m_ti=Ti;
	m_td=Td;
	m_ts=Ts;
	return TRUE;
}

BOOL GetPIDPara(float &Kp,float &Ti,float &Td,int &Ts)
{
	Kp=m_kp;
	Ti=m_ti;
	Td=m_td;
	Ts=m_ts;	
	return TRUE;
}

BOOL SetSP(float SP)
{
	m_sp=SP;
	return TRUE;
}

BOOL SetPV(float PV)
{
	m_pv=PV;
	return TRUE;
}

BOOL SetCV(float CV)
{
	m_cv[0]=CV;
	return TRUE;
}

BOOL GetCV(float &CV)
{
	CV=m_cv[0];
	return TRUE;
}

UINT PIDThread(LPVOID pParam)
{	
	while(Running)
	{
		m_err[2]=m_err[1];
		m_err[1]=m_err[0];
		m_err[0]=m_sp-m_pv;
		m_cv[1]=m_cv[0];
		float d=0;
		d=(1+1/m_ti+m_td)*m_err[0];
		d=d-(1+2*m_td)*m_err[1];
		d=d+m_td*m_err[2];
		d=m_kp*d;
		d=m_cv[1]+d;
		//d=m_cv[1]+m_kp*((1+1/m_ti+m_td)*m_err[0]-(1+2*m_td)*m_err[1]+m_td*m_err[2]);
		if (d>100)
			m_cv[0]=100;
		else if (d<0)
			m_cv[0]=0;
		else
			m_cv[0]=d;
		Sleep(m_ts);
	}

	return 0;
}

BOOL StartControl(void)
{
	int Code=1;
	CWinThread* pPIDThread;

	Running=TRUE;
	pPIDThread=AfxBeginThread(PIDThread,&Code);

	return TRUE;
}

BOOL StopControl(void)
{
	Running=FALSE;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CPIDControlApp object

CPIDControlApp theApp;

⌨️ 快捷键说明

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