大家好,又见面了,我是你们的朋友全栈君。
代码语言:javascript复制//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TChart *Chart1;
TLineSeries *Series1;
TChart *Chart2;
TLineSeries *Series2;
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TEdit *Edit4;
TEdit *Edit5;
TEdit *Edit6;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
TLabel *Label6;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
2.Unit1.cpp
代码语言:javascript复制//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "File1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit1->Text = 1.5; //控制参数
Edit2->Text = 0.5;
Edit3->Text = 0.1;
Edit4->Text = 3;
Edit5->Text = 0.3;
Edit6->Text = 20;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Kp = Edit1->Text.ToDouble();
double Ki = Edit2->Text.ToDouble();
double Kd = Edit3->Text.ToDouble();
double T = Edit4->Text.ToDouble();
double Ts = Edit5->Text.ToDouble();
double Signal = Edit6->Text.ToDouble();
CIncrementalPID IPID(Kp,Ki,Kd);
CPositionalPID PPID(Kp,Ki,Kd);
Series1->Clear() ;
Series2->Clear() ;
for(int i = 0; i < 200 ;i )
{
IPID.SetStepSignal(Signal);
IPID.SetInertiaTime(T,Ts);
PPID.SetStepSignal(Signal);
PPID.SetInertiaTime(T,Ts);
Series1->AddXY(i,IPID.SystemOutput,"",clRed );
Series2->AddXY(i,PPID.SystemOutput,"",clBlue );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
3.File1.h
代码语言:javascript复制#ifndef CPID_H
#define CPID_H
/*****************************************************************
# 增量式PID系统 #
#*****************************************************************/
class CIncrementalPID
{
public:
CIncrementalPID(double P, double I, double D);
~CIncrementalPID(){}
private:
double Kp;
double Ki;
double Kd;
double PIDOutput; //PID控制器输出
double LastSystemOutput; //上次系统输出值
double Error; //输出值与输入值的偏差
double LastError;
double LastLastError;
public:
double SystemOutput; //系统输出值
void SetStepSignal(double StepSignal);
void SetInertiaTime(double InertiaTime,double SampleTime);
};
/******************************************************************
# 位置式PID系统 #
# *****************************************************************/
class CPositionalPID
{
public:
CPositionalPID(double P, double I, double D);
~CPositionalPID(){}
private:
double Kp;
double Ki;
double Kd;
double ResultValueBack;
double PidOutput;
double PIDErrADD;
double ErrBack;
public:
double SystemOutput;
void SetStepSignal(double StepSignal);
void SetInertiaTime(double InertiaTime,double SampleTime);
};
#endif
4.File1.cpp
代码语言:javascript复制#include "File1.h"
CIncrementalPID::CIncrementalPID(double P, double I, double D)
{
Kp = P;
Ki = I;
Kd = D;
PIDOutput = 0.0; //PID控制器输出
SystemOutput = 0.0; //系统输出值
LastSystemOutput = 0.0; //上次系统输出值
Error = 0.0; //输出值与输入值的偏差
LastError = 0.0;
LastLastError = 0.0;
}
//设置PID控制器参数
void CIncrementalPID::SetStepSignal(double StepSignal)
{
Error = StepSignal - SystemOutput;
double IncrementValue = Kp * (Error - LastError) Ki * Error Kd * (Error - 2 * LastError LastLastError);
PIDOutput = IncrementValue;
LastLastError = LastError;
LastError = Error;
}
//设置一阶惯性环节系统 其中InertiaTime为惯性时间常数
void CIncrementalPID::SetInertiaTime(double InertiaTime,double SampleTime)
{
SystemOutput = (InertiaTime * LastSystemOutput SampleTime * PIDOutput) / (SampleTime InertiaTime);
LastSystemOutput = SystemOutput;
}
/********************************************************************
# 位置式PID系统 #
# ******************************************************************/
CPositionalPID::CPositionalPID(double P, double I, double D)
{
Kp = P;
Ki = I;
Kd = D;
SystemOutput = 0.0;
ResultValueBack = 0.0;
PidOutput = 0.0;
PIDErrADD = 0.0;
ErrBack = 0.0;
}
//设置PID控制器参数
void CPositionalPID::SetStepSignal(double StepSignal)
{
double Err = StepSignal - SystemOutput;
double KpWork = Kp * Err;
double KiWork = Ki * PIDErrADD;
double KdWork = Kd * (Err - ErrBack);
PidOutput = KpWork KiWork KdWork;
PIDErrADD = Err;
ErrBack = Err;
}
//设置一阶惯性环节系统 其中InertiaTime为惯性时间常数
void CPositionalPID::SetInertiaTime(double InertiaTime,double SampleTime)
{
SystemOutput = (InertiaTime * ResultValueBack SampleTime * PidOutput) / (SampleTime InertiaTime);
ResultValueBack = SystemOutput;
}
5.实现效果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185959.html原文链接:https://javaforall.cn