📄 pidcontrol.cpp
字号:
// PidControl.cpp : implementation file
//
#include "stdafx.h"
#include "Watertank.h"
#include "PidControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPidControl
IMPLEMENT_DYNCREATE(CPidControl, CCmdTarget)
CPidControl::CPidControl()
{
prev_u = 0.0;
prev_e1 = prev_e2 = 0.0;
//m_Ki = m_Kd = m_Kp = 1.0;
kp = 0.1711;//0.000000001;
ki = 0.014; //0.0002;
kd = 0.0079;// 2; //0.00001;
}
CPidControl::~CPidControl()
{
}
BEGIN_MESSAGE_MAP(CPidControl, CCmdTarget)
//{{AFX_MSG_MAP(CPidControl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPidControl message handlers
double CPidControl::CalcU( double input, double reference)
{
double output;
double cur_err = reference- input;
double delta_u = 0.0;
double integ_bound = 10.0;
if(cur_err < integ_bound && cur_err > -integ_bound)
delta_u = kp * (cur_err - prev_e1)
+ ki * cur_err
+ kd * (cur_err - 2*prev_e1 + prev_e2);
else
delta_u = kp * (cur_err - prev_e1)
+ kd * (cur_err - 2*prev_e1 + prev_e2);
output = prev_u + delta_u;
//shift variables
prev_e2 = prev_e1;
prev_e1 = cur_err;
prev_u = output;
//limit the minimum value
if(output<0)
output=0;
if(output>3)
output=3;
return output;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -