|
|||||||||||
| 技術交流 | 電路欣賞 | 工控天地 | 數字廣電 | 通信技術 | 電源技術 | 測控之家 | EMC技術 | ARM技術 | EDA技術 | PCB技術 | 嵌入式系統 驅動編程 | 集成電路 | 器件替換 | 模擬技術 | 新手園地 | 單 片 機 | DSP技術 | MCU技術 | IC 設計 | IC 產業(yè) | CAN-bus/DeviceNe |
跪求大蝦們給這段PID程序加上注釋或說明一下! |
| 作者:哈佛大學 欄目:單片機 |
#include <stdio.h> #include<math.h> struct _pid { int pv; /*integer that contains the PROCESS VALUE.html">VALUE*/ int sp; /*integer that contains the set point*/ float integral; float pgain; float igain; float dgain; int deadband; int last_error; }; struct _pid warm,*pid; int PROCESS_point, set_point,dead_band; float p_gain, i_gain, d_gain, integral_val,new_integ;; /*------------------------------------------------------------------------ pid_init DESCRIPTION This function initializes the pointers in the _pid structure to the PROCESS variable and the setpoint. *pv and *sp are integer pointers. ------------------------------------------------------------------------*/ void pid_init(struct _pid *warm, int PROCESS_point, int set_point) { struct _pid *pid; pid = warm; pid->pv = PROCESS_point; pid->sp = set_point; } /*------------------------------------------------------------------------ pid_tune DESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain), derivitive gain (d_gain), and the dead band (dead_band) of a pid CONTROL structure _pid. ------------------------------------------------------------------------*/ void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band) { pid->pgain = p_gain; pid->igain = i_gain; pid->dgain = d_gain; pid->deadband = dead_band; pid->integral= integral_val; pid->last_error=0; } /*------------------------------------------------------------------------ pid_setinteg DESCRIPTION Set a new VALUE.html">VALUE for the integral term of the pid equation. This is useful for setting the initial OUTPUT of the pid CONTROLler at start up. ------------------------------------------------------------------------*/ void pid_setinteg(struct _pid *pid,float new_integ) { pid->integral = new_integ; pid->last_error = 0; } /*------------------------------------------------------------------------ pid_bumpless DESCRIPTION Bumpless transfer algorithim. When suddenly changing setpoints, or when restarting the PID equation after an extended pause, the derivative of the equation can cause a bump in the CONTROLler OUTPUT. This function will help smooth out that bump. The PROCESS VALUE.html">VALUE in *pv should be the updated just before this function is used. ------------------------------------------------------------------------*/ void pid_bumpless(struct _pid *pid) { pid->last_error = (pid->sp)-(pid->pv); } /*------------------------------------------------------------------------ pid_calc DESCRIPTION Performs PID calculations for the _pid structure *a. This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim. Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate CONTROL. RETURN VALUE The new OUTPUT VALUE.html">VALUE for the pid loop. USAGE #include "CONTROL.h"*/ float pid_calc(struct _pid *pid) { int err; float pterm, dterm, result, ferror; err = (pid->sp) - (pid->pv); if (abs(err) > pid->deadband) { ferror = (float) err; /*do integer to float conversion ONLY once*/ pterm = pid->pgain * ferror; if (pterm > 100 || pterm < -100) { pid->integral = 0.0; } else { pid->integral += pid->igain * ferror; if (pid->integral > 100.0) { pid->integral = 100.0; } else if (pid->integral < 0.0) pid->integral = 0.0; } dterm = ((float)(err - pid->last_error)) * pid->dgain; result = pterm + pid->integral + dterm; } else result = pid->integral; pid->last_error = err; return (result); } void main(void) { float DISPLAY_VALUE.html">VALUE; int count=0; pid = &warm; // printf("Enter the VALUE.html">VALUEs of PROCESS point, Set point, P gain, I gain, D gain \n"); // scanf("%d%d%f%f%f", &PROCESS_point, &set_point, &p_gain, &i_gain, &d_gain); PROCESS_point = 30; set_point = 40; p_gain = (float)(5.2); i_gain = (float)(0.77); d_gain = (float)(0.18); dead_band = 2; integral_val =(float)(0.01); printf("The VALUE.html">VALUEs of PROCESS point, Set point, P gain, I gain, D gain \n"); printf(" %6d %6d %4f %4f %4f\n", PROCESS_point, set_point, p_gain, i_gain, d_gain); printf("Enter the VALUE.html">VALUEs of PROCESS point\n"); while(count<=20) { scanf("%d",&PROCESS_point); pid_init(&warm, PROCESS_point, set_point); pid_tune(&warm, p_gain,i_gain,d_gain,dead_band); pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0); //Get input VALUE.html">VALUE for PROCESS point pid_bumpless(&warm); // how to DISPLAY OUTPUT DISPLAY_VALUE.html">VALUE = pid_calc(&warm); printf("%f\n", DISPLAY_VALUE.html">VALUE); //printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain); count++; } } |
| 2樓: | >>參與討論 |
| 作者: 哈佛大學 于 2006/9/28 21:53:00 發(fā)布:
00啊,匠人啊 |
|
| 3樓: | >>參與討論 |
| 作者: 哈佛大學 于 2006/9/28 21:57:00 發(fā)布:
平常人啊 |
|
| 4樓: | >>參與討論 |
| 作者: computer00 于 2006/9/29 7:59:00 發(fā)布:
我倒…………這么長……里面不是有注釋了么? |
|
| 5樓: | >>參與討論 |
| 作者: hqgboy 于 2006/9/29 8:07:00 發(fā)布:
哈佛大學??? |
|
|
|
| 免費注冊為維庫電子開發(fā)網會員,參與電子工程師社區(qū)討論,點此進入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號 |