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