⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uever悠威电子网-c语言实现pid算法.htm

📁 关于PID算法的一些说明和应用!适合做精确控制的工程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                  <DIV align=center><A 
                  href="http://www.uever.com/technic/electronic/elecdiy.asp"></A><A 
                  href="http://www.uever.com/software/softdown.asp"><FONT 
                  color=#000000>软件下载</FONT></A></DIV></TD>
                <TD width="1%">
                  <DIV align=center>|</DIV></TD>
                <TD width="10%">
                  <DIV align=center><FONT color=#0000ff>技术资料</FONT></DIV></TD>
                <TD width="1%">
                  <DIV align=center>|</DIV></TD>
                <TD width="10%">
                  <DIV align=center><A 
                  href="http://www.uever.com/mark/mark.asp"><FONT 
                  color=#000000>刻录服务</FONT></A></DIV></TD>
                <TD width="1%">
                  <DIV align=center>|</DIV></TD>
                <TD width="10%">
                  <DIV align=center><A 
                  href="http://www.uever.com/design/design.asp"><FONT 
                  color=#000000>技术方案</FONT></A></DIV></TD>
                <TD width="1%">
                  <DIV align=center>|</DIV></TD>
                <TD width="10%">
                  <DIV align=center><A 
                  href="http://www.uever.com/shop/OrderShow.asp"><FONT 
                  color=#000000>订单查询</FONT></A></DIV></TD>
                <TD width="1%">
                  <DIV align=center>|</DIV></TD>
                <TD width="10%">
                  <DIV align=center><A 
                  href="http://www.uever.com/forum/forum.asp"><FONT 
                  color=#000000>技术论坛</FONT></A></DIV></TD>
                <TD width="1%">
                  <DIV align=center></DIV></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD height=3>&nbsp;</TD>
          <TD width=389 height=3>&nbsp;</TD>
          <TD colSpan=2 height=3>&nbsp;</TD></TR>
        <TR>
          <TD height=15>
            <DIV align=left>今天是: 2007年8月4日 星期六 </DIV></TD>
          <TD colSpan=3 height=15>&nbsp; 您现在的位置:UEVER&gt;&gt;<A 
            href="http://www.uever.com/technic/technic.asp"><FONT 
            color=#000066>技术资料</FONT></A>&gt;&gt;单片机&gt;&gt; C语言实现PID算法 
            <DIV align=center></DIV></TD></TR>
        <TR>
          <TD height=20>
            <DIV align=center>当前在线44人 </DIV></TD>
          <TD> </TD>
          <TD width=12> </TD>
          <TD width=205> </TD></TR>
        <TR>
          <TD background=Uever悠威电子网-C语言实现PID算法.files/bg_dian.gif colSpan=4 
          height=1></TD></TR>
        <TR vAlign=top align=middle>
          <TD colSpan=4 height=999>
            <TABLE height=1007 cellSpacing=0 cellPadding=0 width="100%" 
border=0>
              <TBODY>
              <TR>
                <TD colSpan=2> </TD>
                <TD width="65%"> </TD>
                <TD colSpan=3> </TD></TR>
              <TR>
                <TD colSpan=6 height=27>
                  <DIV align=center><FONT color=#ff0000 
                  size=2><STRONG>C语言实现PID算法</STRONG></FONT></DIV></TD></TR>
              <TR vAlign=center align=middle>
                <TD colSpan=6> </TD></TR>
              <TR vAlign=center align=middle>
                <TD 
                  colSpan=6>作者:unknow&nbsp;&nbsp;更新时间:2006-5-7&nbsp;&nbsp;点击数:3499</TD></TR>
              <TR>
                <TD height=16>&nbsp;</TD>
                <TD colSpan=4 height=16>
                  <HR>
                </TD>
                <TD height=16>&nbsp;</TD></TR>
              <TR>
                <TD width="2%" height=25> </TD>
                <TD vAlign=top align=middle colSpan=4 height=25> </TD>
                <TD width="2%"> </TD></TR>
              <TR>
                <TD width="2%" height=26> </TD>
                <TD vAlign=top align=middle colSpan=4 height=26>
                  <TABLE cellSpacing=0 cellPadding=0 width="93%" border=0>
                    <TBODY>
                    <TR>
                      <TD>关键词&nbsp;PID&nbsp;pid算法&nbsp;pid控制算法&nbsp;pid程序&nbsp;pid调节<BR><BR>&nbsp;<BR><BR>BC31&nbsp;TC30&nbsp;编译过,可运行。<BR>  <BR>  <BR>  #include&nbsp;<STDIO.H><BR>  #include<MATH.H><BR>  <BR>  struct&nbsp;_pid&nbsp;{<BR>  &nbsp;int&nbsp;pv;&nbsp;/*integer&nbsp;that&nbsp;contains&nbsp;the&nbsp;process&nbsp;value*/<BR>  &nbsp;int&nbsp;sp;&nbsp;/*integer&nbsp;that&nbsp;contains&nbsp;the&nbsp;set&nbsp;point*/<BR>  &nbsp;float&nbsp;integral;<BR>  &nbsp;float&nbsp;pgain;<BR>  &nbsp;float&nbsp;igain;<BR>  &nbsp;float&nbsp;dgain;<BR>  &nbsp;int&nbsp;deadband;<BR>  &nbsp;int&nbsp;last_error;<BR>  };<BR>  <BR>  struct&nbsp;_pid&nbsp;warm,*pid;<BR>  int&nbsp;process_point,&nbsp;set_point,dead_band;&nbsp;<BR>  float&nbsp;p_gain,&nbsp;i_gain,&nbsp;d_gain,&nbsp;integral_val,new_integ;;&nbsp;<BR>  <BR>  <BR>  <BR>  /*------------------------------------------------------------------------&nbsp;<BR>  pid_init&nbsp;<BR>  <BR>  DESCRIPTION&nbsp;This&nbsp;function&nbsp;initializes&nbsp;the&nbsp;pointers&nbsp;in&nbsp;the&nbsp;_pid&nbsp;structure&nbsp;<BR>  to&nbsp;the&nbsp;process&nbsp;variable&nbsp;and&nbsp;the&nbsp;setpoint.&nbsp;*pv&nbsp;and&nbsp;*sp&nbsp;are&nbsp;<BR>  integer&nbsp;pointers.&nbsp;<BR>  ------------------------------------------------------------------------*/&nbsp;<BR>  void&nbsp;pid_init(struct&nbsp;_pid&nbsp;*warm,&nbsp;int&nbsp;process_point,&nbsp;int&nbsp;set_point)<BR>  {&nbsp;<BR>  &nbsp;struct&nbsp;_pid&nbsp;*pid;&nbsp;<BR>  &nbsp;<BR>  &nbsp;pid&nbsp;=&nbsp;warm;&nbsp;<BR>  &nbsp;pid-&gt;pv&nbsp;=&nbsp;process_point;&nbsp;<BR>  &nbsp;pid-&gt;sp&nbsp;=&nbsp;set_point;&nbsp;<BR>  }&nbsp;<BR>  <BR>  <BR>  /*------------------------------------------------------------------------&nbsp;<BR>  pid_tune&nbsp;<BR>  <BR>  DESCRIPTION&nbsp;Sets&nbsp;the&nbsp;proportional&nbsp;gain&nbsp;(p_gain),&nbsp;integral&nbsp;gain&nbsp;(i_gain),&nbsp;<BR>  derivitive&nbsp;gain&nbsp;(d_gain),&nbsp;and&nbsp;the&nbsp;dead&nbsp;band&nbsp;(dead_band)&nbsp;of&nbsp;<BR>  a&nbsp;pid&nbsp;control&nbsp;structure&nbsp;_pid.&nbsp;<BR>  ------------------------------------------------------------------------*/&nbsp;<BR>  <BR>  void&nbsp;pid_tune(struct&nbsp;_pid&nbsp;*pid,&nbsp;float&nbsp;p_gain,&nbsp;float&nbsp;i_gain,&nbsp;float&nbsp;d_gain,&nbsp;int&nbsp;dead_band)&nbsp;<BR>  {&nbsp;<BR>  &nbsp;pid-&gt;pgain&nbsp;=&nbsp;p_gain;&nbsp;<BR>  &nbsp;pid-&gt;igain&nbsp;=&nbsp;i_gain;&nbsp;<BR>  &nbsp;pid-&gt;dgain&nbsp;=&nbsp;d_gain;&nbsp;<BR>  &nbsp;pid-&gt;deadband&nbsp;=&nbsp;dead_band;&nbsp;<BR>  &nbsp;pid-&gt;integral=&nbsp;integral_val;&nbsp;<BR>  &nbsp;pid-&gt;last_error=0;&nbsp;<BR>  }&nbsp;<BR>  <BR>  /*------------------------------------------------------------------------&nbsp;<BR>  pid_setinteg&nbsp;<BR>  <BR>  DESCRIPTION&nbsp;Set&nbsp;a&nbsp;new&nbsp;value&nbsp;for&nbsp;the&nbsp;integral&nbsp;term&nbsp;of&nbsp;the&nbsp;pid&nbsp;equation.&nbsp;<BR>  This&nbsp;is&nbsp;useful&nbsp;for&nbsp;setting&nbsp;the&nbsp;initial&nbsp;output&nbsp;of&nbsp;the&nbsp;<BR>  pid&nbsp;controller&nbsp;at&nbsp;start&nbsp;up.&nbsp;<BR>  ------------------------------------------------------------------------*/&nbsp;<BR>  void&nbsp;pid_setinteg(struct&nbsp;_pid&nbsp;*pid,float&nbsp;new_integ)<BR>  {&nbsp;<BR>  &nbsp;pid-&gt;integral&nbsp;=&nbsp;new_integ;&nbsp;<BR>  &nbsp;pid-&gt;last_error&nbsp;=&nbsp;0;&nbsp;<BR>  }&nbsp;<BR>  <BR>  /*------------------------------------------------------------------------&nbsp;<BR>  pid_bumpless&nbsp;<BR>  <BR>  DESCRIPTION&nbsp;Bumpless&nbsp;transfer&nbsp;algorithim.&nbsp;When&nbsp;suddenly&nbsp;changing&nbsp;<BR>  setpoints,&nbsp;or&nbsp;when&nbsp;restarting&nbsp;the&nbsp;PID&nbsp;equation&nbsp;after&nbsp;an&nbsp;<BR>  extended&nbsp;pause,&nbsp;the&nbsp;derivative&nbsp;of&nbsp;the&nbsp;equation&nbsp;can&nbsp;cause&nbsp;<BR>  a&nbsp;bump&nbsp;in&nbsp;the&nbsp;controller&nbsp;output.&nbsp;This&nbsp;function&nbsp;will&nbsp;help&nbsp;<BR>  smooth&nbsp;out&nbsp;that&nbsp;bump.&nbsp;The&nbsp;process&nbsp;value&nbsp;in&nbsp;*pv&nbsp;should&nbsp;<BR>  be&nbsp;the&nbsp;updated&nbsp;just&nbsp;before&nbsp;this&nbsp;function&nbsp;is&nbsp;used.&nbsp;<BR>  ------------------------------------------------------------------------*/&nbsp;<BR>  void&nbsp;pid_bumpless(struct&nbsp;_pid&nbsp;*pid)&nbsp;<BR>  {&nbsp;<BR>  <BR>  &nbsp;pid-&gt;last_error&nbsp;=&nbsp;(pid-&gt;sp)-(pid-&gt;pv);&nbsp;<BR>  &nbsp;<BR>  }&nbsp;<BR>  <BR>  /*------------------------------------------------------------------------&nbsp;<BR>  pid_calc&nbsp;<BR>  <BR>  DESCRIPTION&nbsp;Performs&nbsp;PID&nbsp;calculations&nbsp;for&nbsp;the&nbsp;_pid&nbsp;<BR>structure&nbsp;*a.&nbsp;This&nbsp;function&nbsp;uses&nbsp;the&nbsp;positional&nbsp;form&nbsp;of&nbsp;the&nbsp;pid&nbsp;equation,&nbsp;and&nbsp;incorporates&nbsp;an&nbsp;integral&nbsp;windup&nbsp;<BR>prevention&nbsp;algorithim.&nbsp;Rectangular&nbsp;integration&nbsp;is&nbsp;used,&nbsp;so<BR>&nbsp;this&nbsp;function&nbsp;must&nbsp;be&nbsp;repeated&nbsp;on&nbsp;a&nbsp;consistent&nbsp;time&nbsp;basis&nbsp;<BR>for&nbsp;accurate&nbsp;control.&nbsp;<BR>  <BR>  RETURN&nbsp;VALUE&nbsp;The&nbsp;new&nbsp;output&nbsp;value&nbsp;for&nbsp;the&nbsp;pid&nbsp;loop.&nbsp;<BR>  <BR>  USAGE&nbsp;#include&nbsp;"control.h"*/&nbsp;<BR>  <BR>  <BR>  float&nbsp;pid_calc(struct&nbsp;_pid&nbsp;*pid)<BR>  {&nbsp;<BR>  &nbsp;int&nbsp;err;<BR>  &nbsp;float&nbsp;pterm,&nbsp;dterm,&nbsp;result,&nbsp;ferror;&nbsp;<BR>  &nbsp;<BR>  &nbsp;err&nbsp;=&nbsp;(pid-&gt;sp)&nbsp;-&nbsp;(pid-&gt;pv);&nbsp;<BR>  &nbsp;if&nbsp;(abs(err)&nbsp;&gt;&nbsp;pid-&gt;deadband)&nbsp;<BR>  &nbsp;{&nbsp;<BR>  &nbsp;ferror&nbsp;=&nbsp;(float)&nbsp;err;&nbsp;/*do&nbsp;integer&nbsp;to&nbsp;float&nbsp;conversion&nbsp;only&nbsp;once*/&nbsp;<BR>  &nbsp;pterm&nbsp;=&nbsp;pid-&gt;pgain&nbsp;*&nbsp;ferror;&nbsp;<BR>  &nbsp;if&nbsp;(pterm&nbsp;&gt;&nbsp;100&nbsp;||&nbsp;pterm&nbsp;&lt;&nbsp;-100)<BR>  &nbsp;{<BR>  &nbsp;pid-&gt;integral&nbsp;=&nbsp;0.0;&nbsp;<BR>  &nbsp;}<BR>  &nbsp;else&nbsp;<BR>  &nbsp;{&nbsp;<BR>  &nbsp;pid-&gt;integral&nbsp;+=&nbsp;pid-&gt;igain&nbsp;*&nbsp;ferror;&nbsp;<BR>  &nbsp;if&nbsp;(pid-&gt;integral&nbsp;&gt;&nbsp;100.0)&nbsp;<BR>  &nbsp;{<BR>  &nbsp;pid-&gt;integral&nbsp;=&nbsp;100.0;&nbsp;<BR>  &nbsp;}<BR>  &nbsp;else&nbsp;if&nbsp;(pid-&gt;integral&nbsp;&lt;&nbsp;0.0)&nbsp;pid-&gt;integral&nbsp;=&nbsp;0.0;&nbsp;<BR>  &nbsp;}&nbsp;<BR>  &nbsp;dterm&nbsp;=&nbsp;((float)(err&nbsp;-&nbsp;pid-&gt;last_error))&nbsp;*&nbsp;pid-&gt;dgain;&nbsp;<BR>  &nbsp;result&nbsp;=&nbsp;pterm&nbsp;+&nbsp;pid-&gt;integral&nbsp;+&nbsp;dterm;&nbsp;<BR>  &nbsp;}&nbsp;<BR>  &nbsp;else&nbsp;result&nbsp;=&nbsp;pid-&gt;integral;&nbsp;<BR>  &nbsp;pid-&gt;last_error&nbsp;=&nbsp;err;&nbsp;<BR>  &nbsp;return&nbsp;(result);&nbsp;<BR>  }<BR>  <BR>  <BR>  void&nbsp;main(void)<BR>  {<BR>  &nbsp;float&nbsp;display_value;<BR>  &nbsp;int&nbsp;count=0;<BR>  <BR>  &nbsp;pid&nbsp;=&nbsp;&amp;warm;<BR>  <BR>  //&nbsp;printf("Enter&nbsp;the&nbsp;values&nbsp;of&nbsp;Process&nbsp;point,&nbsp;Set&nbsp;point,&nbsp;P&nbsp;gain,&nbsp;I&nbsp;gain,&nbsp;D&nbsp;gain&nbsp;\n");<BR>  //&nbsp;scanf("%d%d%f%f%f",&nbsp;&amp;process_point,&nbsp;&amp;set_point,&nbsp;&amp;p_gain,&nbsp;&amp;i_gain,&nbsp;&amp;d_gain);<BR>  <BR>  <BR>  <BR>  &nbsp;process_point&nbsp;=&nbsp;30;<BR>  &nbsp;set_point&nbsp;=&nbsp;40;<BR>  &nbsp;p_gain&nbsp;=&nbsp;(float)(5.2);<BR>  &nbsp;i_gain&nbsp;=&nbsp;(float)(0.77);<BR>  &nbsp;d_gain&nbsp;=&nbsp;(float)(0.18);<BR>  <BR>  <BR>  <BR>  &nbsp;dead_band&nbsp;=&nbsp;2;<BR>  &nbsp;integral_val&nbsp;=(float)(0.01);<BR>  <BR>  <BR>  &nbsp;printf("The&nbsp;values&nbsp;of&nbsp;Process&nbsp;point,&nbsp;Set&nbsp;point,&nbsp;P&nbsp;gain,&nbsp;I&nbsp;gain,&nbsp;D&nbsp;gain&nbsp;\n");<BR>  &nbsp;printf("&nbsp;%6d&nbsp;%6d&nbsp;%4f&nbsp;%4f&nbsp;%4f\n",&nbsp;process_point,&nbsp;set_point,&nbsp;p_gain,&nbsp;i_gain,&nbsp;d_gain);<BR>  <BR>  &nbsp;printf("Enter&nbsp;the&nbsp;values&nbsp;of&nbsp;Process&nbsp;point\n");<BR>  <BR>  &nbsp;while(count&lt;=20)<BR>  &nbsp;{<BR>  <BR>  <BR>  <BR>  &nbsp;scanf("%d",&amp;process_point);<BR>  <BR>  &nbsp;pid_init(&amp;warm,&nbsp;process_point,&nbsp;set_point);<BR>  &nbsp;pid_tune(&amp;warm,&nbsp;p_gain,i_gain,d_gain,dead_band);<BR>  &nbsp;pid_setinteg(&amp;warm,0.0);&nbsp;//pid_setinteg(&amp;warm,30.0);<BR>  <BR>  &nbsp;//Get&nbsp;input&nbsp;value&nbsp;for&nbsp;process&nbsp;point<BR>  &nbsp;pid_bumpless(&amp;warm);<BR>  <BR>  &nbsp;//&nbsp;how&nbsp;to&nbsp;display&nbsp;output<BR>  &nbsp;display_value&nbsp;=&nbsp;pid_calc(&amp;warm);&nbsp;<BR>  &nbsp;printf("%f\n",&nbsp;display_value);&nbsp;<BR>  &nbsp;//printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain);&nbsp;<BR>  &nbsp;count++;&nbsp;<BR>  &nbsp;<BR>  &nbsp;}&nbsp;<BR>  <BR>  }<BR><BR></TD></TR></TBODY></TABLE></TD>
                <TD width="2%"> </TD></TR>
              <TR>
                <TD colSpan=2> </TD>
                <TD> </TD>
                <TD colSpan=3> </TD></TR>
              <TR>
                <TD height=16>   </TD>
                <TD colSpan=4 height=16>&nbsp;</TD>
                <TD> </TD></TR>
              <TR>
                <TD colSpan=2 height=18> <FONT color=#ff0000>上一篇:</FONT></TD>
                <TD><A 
                  href="http://www.uever.com/technic/technicshow.asp?artID=115"><FONT 
                  color=#000066>单片机温度控制应用</FONT></A></TD>
                <TD width="12%">
                  <DIV align=center></DIV></TD>
                <TD width="13%">
                  <DIV align=center>[<A href="javascript:window.close()"><FONT 
                  color=#000000>关闭窗口</FONT></A>]</DIV></TD>
                <TD></TD></TR>
              <TR>
                <TD colSpan=2 height=18> <FONT color=#ff0000>下一篇:</FONT></TD>
                <TD><A 
                  href="http://www.uever.com/technic/technicshow.asp?artID=117"><FONT 
                  color=#000066>ISD1400 系列单片永久性语音录放电路应用</FONT></A></TD>
                <TD>
                  <DIV align=center></DIV></TD>
                <TD>
                  <DIV align=center></DIV></TD>
                <TD>&nbsp;</TD></TR>
              <TR vAlign=top>
                <TD colSpan=6 height=80>
                  <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=top align=middle width="33%" height=234>
                        <TABLE height=250 cellSpacing=1 cellPadding=0 
                        width="98%" border=1>
                          <TBODY>
                          <TR>
                            <TD width="100%" bgColor=#efefef height=15>
                              <DIV align=center><FONT 
                              color=#ff0000>最近浏览</FONT></DIV></TD></TR>
                          <TR>
                            <TD vAlign=top>
                              <DIV align=center>
                              <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                              border=0>
                                <TBODY>
                                <TR>
                                <TD height=5></TD>
                                <TD height=5></TD></TR>
                                <TR>
                                <TD width="8%">
                                <DIV align=center><IMG height=6 
                                src="Uever悠威电子网-C语言实现PID算法.files/dimg_10.gif" 
                                width=6></DIV></TD>
                                <TD width="92%">
                                <DIV 
                                style="OVERFLOW: hidden; WIDTH: 220px; TEXT-OVERFLOW: ellipsis"><NOBR><A 
                                href="http://www.uever.com/technic/technicshow.asp?artID=180"><FONT 
                                color=#000066>基于烟雾检测火灾自动报警系统</FONT></A></NOBR></DIV></TD></TR></TBODY></TABLE>
                              <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                              border=0>
                                <TBODY>
                                <TR>
                                <TD height=5></TD>
                                <TD height=5></TD></TR>
                                <TR>
                                <TD width="8%">
                                <DIV align=center><IMG height=6 
                                src="Uever悠威电子网-C语言实现PID算法.files/dimg_10.gif" 
                                width=6></DIV></TD>
                                <TD width="92%">
                                <DIV 
                                style="OVERFLOW: hidden; WIDTH: 220px; TEXT-OVERFLOW: ellipsis"><NOBR><A 
                                href="http://www.uever.com/technic/technicshow.asp?artID=189"><FONT 
                                color=#000066>奉献:AT45DB041读写程序</FONT></A></NOBR></DIV></TD></TR></TBODY></TABLE>
                              <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                              border=0>
                                <TBODY>
                                <TR>
                                <TD height=5></TD>
                                <TD height=5></TD></TR>
                                <TR>
                                <TD width="8%">
                                <DIV align=center><IMG height=6 
                                src="Uever悠威电子网-C语言实现PID算法.files/dimg_10.gif" 
                                width=6></DIV></TD>
                                <TD width="92%">
                                <DIV 
                                style="OVERFLOW: hidden; WIDTH: 220px; TEXT-OVERFLOW: ellipsis"><NOBR><A 
                                href="http://www.uever.com/technic/technicshow.asp?artID=142"><FONT 
                                color=#000066>单片机系统软件抗干扰方法</FONT></A></NOBR></DIV></TD></TR></TBODY></TABLE>
                              <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                              border=0>
                                <TBODY>
                                <TR>
                                <TD height=5></TD>
                                <TD height=5></TD></TR>
                                <TR>
                                <TD width="8%">
                                <DIV align=center><IMG height=6 
                                src="Uever悠威电子网-C语言实现PID算法.files/dimg_10.gif" 
                                width=6></DIV></TD>
                                <TD width="92%">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -