資料介紹
繼卡爾曼封裝之后推出第二個封裝,PID算法,這個其實比上個簡單多了下面貼出代碼
/**
??******************************************************************************
??* @file ???PID_Control.h
??* @author ?willieon
??* @version V0.1
??* @date ???January-2015
??* @brief ??PID控制算法頭文件
??* ???????????????????????定義結構體類型以及聲明函數
??* ???????????????????????#define IF_THE_INTEGRAL_SEPARATION ?0/1 ?為積分分離標志
??******************************************************************************
??**/
?
#ifndef __PID_CONTROL_H__
#define __PID_CONTROL_H__
?
#define IF_THE_INTEGRAL_SEPARATION ?0 ???
//#define IF_THE_INTEGRAL_SEPARATION ?1 ??//是否積分分離 ?0-不分離,1 -分離
?
typedef struct
{
????????double SetPoint; // 設定目標 Desired Value ??
????????double Proportion; // 比例常數 Proportional Const
????????double Integral; // 積分常數 Integral Const
????????double Derivative; // 微分常數 Derivative Const ??
????????double LastError; // Error[-1]
????????double PrevError; // Error[-2]
????????double SumError; // Sums of Errors ?
}PID;
?
#if IF_THE_INTEGRAL_SEPARATION ???????????//是否積分分離預編譯開始
?
double PIDCalc(double NextPoint ,double SepLimit, PID *pp); ??//帶積分分離的PID運算
?
#else
?
double PIDCalc( double NextPoint, PID *pp); ????//不帶積分分離的PID運算
?
#endif ???????//是否積分分離預編譯結束
?
void PIDInit (double SetPoint, double Proportion, double Integral, double Derivative, PID *pp);
?
#endif
/**
??******************************************************************************
??* @file ???PID_Control.c
??* @author ?willieon
??* @version V0.1
??* @date ???January-2015
??* @brief ??PID控制算法函數代碼
??* ???????
??*
??******************************************************************************
??**/
?
#include "PID_Control.h"
#include "math.h"
?
/*************************************************************************************
* ???????名 ???稱: double PIDCalc( PID *pp, double NextPoint ,double SepLimit)
* ???????功 ???能: PID控制運算
* ???????入口參數: PID *pp ?- 定義的運算所需變量的結構體
* ??????????????????????????NextPoint - 負反饋輸入值
* ??????????????????????????SepLimit ?- 積分分離上限
* ???????出口參數: 返回PID控制量
* ???????說 ???明: 默認不進行積分分離,如果用戶需要使用積分分離,需在PID_Control.h中
* ???????????????????????????????將 #define IF_THE_INTEGRAL_SEPARATION ?0 ?改為
* ???????????????????????????#define IF_THE_INTEGRAL_SEPARATION ?1
* ???????調用方法: 進行積分分離時入口參數為3個,具體方法如下:
* ???????????????????????????????PID PIDControlStruct ; ??//定義PID運算結構體
* ???????????????????????????????PIDInit(50, 0.24, 0.04, 0.2, &PIDControlStruct);//結構體初始化,注意&符號不能省
* ???????????????????????????????ControlData = PIDCalc(ReadData, 200, &PIDControlStruct); ??//控制量 = PIDCalc(反饋值,積分分離上限,PID運算結構體)
*
***************************************************************************************
*/
?
#if IF_THE_INTEGRAL_SEPARATION
?
double PIDCalc(double NextPoint ,double SepLimit, PID *pp)
{
????????double dError, Error,Flag; ??
????????Error = pp->SetPoint - NextPoint; ????????// 偏差
????????if(abs(Error) > SepLimit) ???????//當偏差大于分離上限積分分離
????????{
????????????????Flag = 0;
????????}
????????else ??????//當偏差小于分離上限,積分項不分離
????????{
????????????????Flag = 1;
????????????????pp->SumError += Error; ????????// 積分 ?
????????}
????????dError = pp->LastError - pp->PrevError; ????????// 當前微分
????????pp->PrevError = pp->LastError;
????????pp->LastError = Error; ?
????????return (
????????????????pp->Proportion ???????????????* ???????????????Error ????????????????// 比例項
????????????????+ Flag * pp->Integral ???????* ???????????????pp->SumError ????????// 積分項
????????????????+ pp->Derivative ???????????????* ???????????????dError ????????????????// 微分項
????????????????);
}
?
#else
?
double PIDCalc( double NextPoint, PID *pp)
{ ?
????????double dError, Error; ??
????????Error = pp->SetPoint - NextPoint; ????????????????????????// 偏差
????????pp->SumError += Error; ???????????????????????????????????????// 積分 ?
????????dError = pp->LastError - pp->PrevError; ???????????????// 當前微分
????????pp->PrevError = pp->LastError;
????????pp->LastError = Error; ?
????????return (pp->Proportion ???????* ???????Error ???????????????// 比例項
????????????????+ pp->Integral ???????????????* ???????pp->SumError ????????// 積分項
????????????????+ pp->Derivative ???????* ???????dError ????????// 微分項
????????????????);
}
?
#endif
?
?
/*************************************************************************************
* ???????名 ???稱: double PIDCalc( PID *pp, double NextPoint ,double SepLimit)
* ???????功 ???能: PID初始化設定
* ???????入口參數: PID *pp ?- 定義的運算所需變量的結構體
* ??????????????????????????SetPoint - 設定的目標值
* ??????????????????????????Proportion,Integral ,Derivative - P,I,D系數
* ???????出口參數: 無
* ???????說 ???明: ???????
* ???????調用方法: ?PID PIDControlStruct ; ??//定義PID運算結構體
* ???????????????????????????????PIDInit(50, 0.24, 0.04, 0.2, &PIDControlStruct);//結構體初始化,注意&符號不能省
* ???????????????????????????????因為函數需要傳入一個指針,需要對結構體取首地址傳給指針
*
***************************************************************************************
*/
?
?
void PIDInit (double SetPoint, double Proportion, double Integral, double Derivative, PID *pp)
{ ?
????????pp -> SetPoint = SetPoint; // 設定目標 Desired Value ??
????????pp -> Proportion = Proportion; // 比例常數 Proportional Const
????????pp -> Integral = Integral; // 積分常數 Integral Const
????????pp -> Derivative = Derivative; // 微分常數 Derivative Const ??
????????pp -> LastError = 0; // Error[-1]
????????pp -> PrevError = 0; // Error[-2]
????????pp -> SumError = 0; // Sums of Errors
?
????????//memset ( pp,0,sizeof(struct PID)); ??//need include "string.h"
}
將前篇 卡爾曼 和本次的 PID放在一起,在VS2010平臺中調試 代碼如下
#include "kalman.h"
?
#include "stdio.h"
#include "stdlib.h"
#include "PID_Control.h"
?
void main(void)
?
{
????????KalmanCountData k;
????????PID PIDControlStruct;
????????Kalman_Filter_Init(&k);
????????PIDInit(50, 1, 0.04, 0.2, &PIDControlStruct);
????????int m,n;
?
????????double out;
?
????????for(int a = 0;a<80;a++)
????????{
????????????????m = 1+ rand() %100;
????????????????n = 1+ rand() %100;
????????????????Kalman_Filter((float)m,(float)n,&k);
????????????????out = PIDCalc(k.Angle_Final, &PIDControlStruct);
????????????????printf("%3d and %3d is %6f -pid- %6f\r\n",m,n,k.Angle_Final,out);
?
????????}
}
?
/**
??******************************************************************************
??* @file ???PID_Control.h
??* @author ?willieon
??* @version V0.1
??* @date ???January-2015
??* @brief ??PID控制算法頭文件
??* ???????????????????????定義結構體類型以及聲明函數
??* ???????????????????????#define IF_THE_INTEGRAL_SEPARATION ?0/1 ?為積分分離標志
??******************************************************************************
??**/
?
#ifndef __PID_CONTROL_H__
#define __PID_CONTROL_H__
?
#define IF_THE_INTEGRAL_SEPARATION ?0 ???
//#define IF_THE_INTEGRAL_SEPARATION ?1 ??//是否積分分離 ?0-不分離,1 -分離
?
typedef struct
{
????????double SetPoint; // 設定目標 Desired Value ??
????????double Proportion; // 比例常數 Proportional Const
????????double Integral; // 積分常數 Integral Const
????????double Derivative; // 微分常數 Derivative Const ??
????????double LastError; // Error[-1]
????????double PrevError; // Error[-2]
????????double SumError; // Sums of Errors ?
}PID;
?
#if IF_THE_INTEGRAL_SEPARATION ???????????//是否積分分離預編譯開始
?
double PIDCalc(double NextPoint ,double SepLimit, PID *pp); ??//帶積分分離的PID運算
?
#else
?
double PIDCalc( double NextPoint, PID *pp); ????//不帶積分分離的PID運算
?
#endif ???????//是否積分分離預編譯結束
?
void PIDInit (double SetPoint, double Proportion, double Integral, double Derivative, PID *pp);
?
#endif
/**
??******************************************************************************
??* @file ???PID_Control.c
??* @author ?willieon
??* @version V0.1
??* @date ???January-2015
??* @brief ??PID控制算法函數代碼
??* ???????
??*
??******************************************************************************
??**/
?
#include "PID_Control.h"
#include "math.h"
?
/*************************************************************************************
* ???????名 ???稱: double PIDCalc( PID *pp, double NextPoint ,double SepLimit)
* ???????功 ???能: PID控制運算
* ???????入口參數: PID *pp ?- 定義的運算所需變量的結構體
* ??????????????????????????NextPoint - 負反饋輸入值
* ??????????????????????????SepLimit ?- 積分分離上限
* ???????出口參數: 返回PID控制量
* ???????說 ???明: 默認不進行積分分離,如果用戶需要使用積分分離,需在PID_Control.h中
* ???????????????????????????????將 #define IF_THE_INTEGRAL_SEPARATION ?0 ?改為
* ???????????????????????????#define IF_THE_INTEGRAL_SEPARATION ?1
* ???????調用方法: 進行積分分離時入口參數為3個,具體方法如下:
* ???????????????????????????????PID PIDControlStruct ; ??//定義PID運算結構體
* ???????????????????????????????PIDInit(50, 0.24, 0.04, 0.2, &PIDControlStruct);//結構體初始化,注意&符號不能省
* ???????????????????????????????ControlData = PIDCalc(ReadData, 200, &PIDControlStruct); ??//控制量 = PIDCalc(反饋值,積分分離上限,PID運算結構體)
*
***************************************************************************************
*/
?
#if IF_THE_INTEGRAL_SEPARATION
?
double PIDCalc(double NextPoint ,double SepLimit, PID *pp)
{
????????double dError, Error,Flag; ??
????????Error = pp->SetPoint - NextPoint; ????????// 偏差
????????if(abs(Error) > SepLimit) ???????//當偏差大于分離上限積分分離
????????{
????????????????Flag = 0;
????????}
????????else ??????//當偏差小于分離上限,積分項不分離
????????{
????????????????Flag = 1;
????????????????pp->SumError += Error; ????????// 積分 ?
????????}
????????dError = pp->LastError - pp->PrevError; ????????// 當前微分
????????pp->PrevError = pp->LastError;
????????pp->LastError = Error; ?
????????return (
????????????????pp->Proportion ???????????????* ???????????????Error ????????????????// 比例項
????????????????+ Flag * pp->Integral ???????* ???????????????pp->SumError ????????// 積分項
????????????????+ pp->Derivative ???????????????* ???????????????dError ????????????????// 微分項
????????????????);
}
?
#else
?
double PIDCalc( double NextPoint, PID *pp)
{ ?
????????double dError, Error; ??
????????Error = pp->SetPoint - NextPoint; ????????????????????????// 偏差
????????pp->SumError += Error; ???????????????????????????????????????// 積分 ?
????????dError = pp->LastError - pp->PrevError; ???????????????// 當前微分
????????pp->PrevError = pp->LastError;
????????pp->LastError = Error; ?
????????return (pp->Proportion ???????* ???????Error ???????????????// 比例項
????????????????+ pp->Integral ???????????????* ???????pp->SumError ????????// 積分項
????????????????+ pp->Derivative ???????* ???????dError ????????// 微分項
????????????????);
}
?
#endif
?
?
/*************************************************************************************
* ???????名 ???稱: double PIDCalc( PID *pp, double NextPoint ,double SepLimit)
* ???????功 ???能: PID初始化設定
* ???????入口參數: PID *pp ?- 定義的運算所需變量的結構體
* ??????????????????????????SetPoint - 設定的目標值
* ??????????????????????????Proportion,Integral ,Derivative - P,I,D系數
* ???????出口參數: 無
* ???????說 ???明: ???????
* ???????調用方法: ?PID PIDControlStruct ; ??//定義PID運算結構體
* ???????????????????????????????PIDInit(50, 0.24, 0.04, 0.2, &PIDControlStruct);//結構體初始化,注意&符號不能省
* ???????????????????????????????因為函數需要傳入一個指針,需要對結構體取首地址傳給指針
*
***************************************************************************************
*/
?
?
void PIDInit (double SetPoint, double Proportion, double Integral, double Derivative, PID *pp)
{ ?
????????pp -> SetPoint = SetPoint; // 設定目標 Desired Value ??
????????pp -> Proportion = Proportion; // 比例常數 Proportional Const
????????pp -> Integral = Integral; // 積分常數 Integral Const
????????pp -> Derivative = Derivative; // 微分常數 Derivative Const ??
????????pp -> LastError = 0; // Error[-1]
????????pp -> PrevError = 0; // Error[-2]
????????pp -> SumError = 0; // Sums of Errors
?
????????//memset ( pp,0,sizeof(struct PID)); ??//need include "string.h"
}
將前篇 卡爾曼 和本次的 PID放在一起,在VS2010平臺中調試 代碼如下
#include "kalman.h"
?
#include "stdio.h"
#include "stdlib.h"
#include "PID_Control.h"
?
void main(void)
?
{
????????KalmanCountData k;
????????PID PIDControlStruct;
????????Kalman_Filter_Init(&k);
????????PIDInit(50, 1, 0.04, 0.2, &PIDControlStruct);
????????int m,n;
?
????????double out;
?
????????for(int a = 0;a<80;a++)
????????{
????????????????m = 1+ rand() %100;
????????????????n = 1+ rand() %100;
????????????????Kalman_Filter((float)m,(float)n,&k);
????????????????out = PIDCalc(k.Angle_Final, &PIDControlStruct);
????????????????printf("%3d and %3d is %6f -pid- %6f\r\n",m,n,k.Angle_Final,out);
?
????????}
}
?
下載該資料的人也在下載
下載該資料的人還在閱讀
更多 >
- STM32平衡小車原理圖 1次下載
- 平衡小車與電機PID系列教程 29次下載
- stm32平衡小車 37次下載
- 基于STM32的自平衡小車DIY
- 【STM32】兩輪自平衡小車學習筆記1
- 畢設分享:基于STM32兩輪自平衡小車 含源碼、原理圖及PCB文件
- STM32智能平衡小車詳細電路原理圖 171次下載
- 使用PID設計兩輪智能平衡小車的資料合集 35次下載
- 使用STM32單片機實現雙輪自平衡智能小車的設計與實現學習PPT課件 2次下載
- 兩輪自平衡智能小車的資料合集免費下載 52次下載
- 設計一個平衡智能小車的全套資料合集 24次下載
- STM32運用之自平衡小車的卡爾曼算法封裝 8次下載
- 基于滑膜干擾抑制的兩輪小車自平衡控制算法_陳運勝 3次下載
- 兩輪自平衡小車的設計與實現 20次下載
- 兩輪自平衡小車資料 29次下載
- 基于R128開發板的自平衡賽車機器人設計 809次閱讀
- 基于FPGA的平衡小車設計 659次閱讀
- 基于STM32的兩輪平衡小車設計 2577次閱讀
- PID控制算法代碼 PID控制算法的原理 2338次閱讀
- 烤箱自平衡門鉸鏈機構的設計 1760次閱讀
- PID控制算法基本原理 1w次閱讀
- PID控制算法詳解 1.2w次閱讀
- 位置式PID算法和增量式PID算法的差異 1.2w次閱讀
- PID控制算法原理、方法概述 9334次閱讀
- 自動化控制PID到底是啥趣味講解資料說明 6036次閱讀
- 詳細分析PID控制算法原理和調試口訣 2.1w次閱讀
- 基于STC12C5A60S2單片機及PID控制算法的氣味循跡車設計 3122次閱讀
- 白平衡幾種算法總結 2.1w次閱讀
- 基于STM32和MPU-6050的兩輪自平衡小車系統設計與實現 3w次閱讀
- 新手入門超聲自平衡小車最佳方案 1.3w次閱讀
下載排行
本周
- 1TC358743XBG評估板參考手冊
- 1.36 MB | 330次下載 | 免費
- 2開關電源基礎知識
- 5.73 MB | 6次下載 | 免費
- 3100W短波放大電路圖
- 0.05 MB | 4次下載 | 3 積分
- 4嵌入式linux-聊天程序設計
- 0.60 MB | 3次下載 | 免費
- 5基于FPGA的光纖通信系統的設計與實現
- 0.61 MB | 2次下載 | 免費
- 6基于FPGA的C8051F單片機開發板設計
- 0.70 MB | 2次下載 | 免費
- 751單片機窗簾控制器仿真程序
- 1.93 MB | 2次下載 | 免費
- 8基于51單片機的RGB調色燈程序仿真
- 0.86 MB | 2次下載 | 免費
本月
- 1OrCAD10.5下載OrCAD10.5中文版軟件
- 0.00 MB | 234315次下載 | 免費
- 2555集成電路應用800例(新編版)
- 0.00 MB | 33564次下載 | 免費
- 3接口電路圖大全
- 未知 | 30323次下載 | 免費
- 4開關電源設計實例指南
- 未知 | 21548次下載 | 免費
- 5電氣工程師手冊免費下載(新編第二版pdf電子書)
- 0.00 MB | 15349次下載 | 免費
- 6數字電路基礎pdf(下載)
- 未知 | 13750次下載 | 免費
- 7電子制作實例集錦 下載
- 未知 | 8113次下載 | 免費
- 8《LED驅動電路設計》 溫德爾著
- 0.00 MB | 6653次下載 | 免費
總榜
- 1matlab軟件下載入口
- 未知 | 935054次下載 | 免費
- 2protel99se軟件下載(可英文版轉中文版)
- 78.1 MB | 537796次下載 | 免費
- 3MATLAB 7.1 下載 (含軟件介紹)
- 未知 | 420026次下載 | 免費
- 4OrCAD10.5下載OrCAD10.5中文版軟件
- 0.00 MB | 234315次下載 | 免費
- 5Altium DXP2002下載入口
- 未知 | 233046次下載 | 免費
- 6電路仿真軟件multisim 10.0免費下載
- 340992 | 191185次下載 | 免費
- 7十天學會AVR單片機與C語言視頻教程 下載
- 158M | 183278次下載 | 免費
- 8proe5.0野火版下載(中文版免費下載)
- 未知 | 138040次下載 | 免費
評論
查看更多