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

📄 pid_8c-source.html

📁 离散 PID 控制 设计方法 ATMEL参考设计
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>AVR221 - PID controller: pid.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs">  <ul>    <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>    <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>    <li class="current"><a href="files.html"><span>Files</span></a></li>    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>  </ul></div><div class="tabs">  <ul>    <li><a href="files.html"><span>File&nbsp;List</span></a></li>    <li><a href="globals.html"><span>Globals</span></a></li>  </ul></div><h1>pid.c</h1><a href="pid_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*This file has been prepared for Doxygen automatic documentation generation.*/</span><a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="pid_8h.html" title="Header file for pid.c.">pid.h</a>"</span><a name="l00024"></a>00024 <span class="preprocessor">#include "<a class="code" href="stdint_8h.html">stdint.h</a>"</span><a name="l00025"></a>00025 <a name="l00035"></a><a class="code" href="pid_8h.html#29fe06205528f56bcfdd242e3d1f9c19">00035</a> <span class="keywordtype">void</span> <a class="code" href="pid_8c.html#29fe06205528f56bcfdd242e3d1f9c19" title="Initialisation of PID controller parameters.">pid_Init</a>(<a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> p_factor, <a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> i_factor, <a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> d_factor, <span class="keyword">struct</span> <a class="code" href="structPID__DATA.html" title="PID Status.">PID_DATA</a> *pid)<a name="l00036"></a>00036 <span class="comment">// Set up PID controller parameters</span><a name="l00037"></a>00037 {<a name="l00038"></a>00038   <span class="comment">// Start values for PID controller</span><a name="l00039"></a>00039   pid-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> = 0;<a name="l00040"></a>00040   pid-&gt;<a class="code" href="structPID__DATA.html#824a2012e383ca3548dfc0969ce78af0" title="Last process value, used to find derivative of process value.">lastProcessValue</a> = 0;<a name="l00041"></a>00041   <span class="comment">// Tuning constants for PID loop</span><a name="l00042"></a>00042   pid-&gt;<a class="code" href="structPID__DATA.html#30e77a609c5cd17e9d74e1e8315bf883" title="The Proportional tuning constant, multiplied with SCALING_FACTOR.">P_Factor</a> = p_factor;<a name="l00043"></a>00043   pid-&gt;<a class="code" href="structPID__DATA.html#27fadf88e502b77612cf733ca336e1a9" title="The Integral tuning constant, multiplied with SCALING_FACTOR.">I_Factor</a> = i_factor;<a name="l00044"></a>00044   pid-&gt;<a class="code" href="structPID__DATA.html#77a0668d1381d333ca942dc250afe32d" title="The Derivative tuning constant, multiplied with SCALING_FACTOR.">D_Factor</a> = d_factor;<a name="l00045"></a>00045   <span class="comment">// Limits to avoid overflow</span><a name="l00046"></a>00046   pid-&gt;<a class="code" href="structPID__DATA.html#80263266448b31cb1b80b30a9a990fe3" title="Maximum allowed error, avoid overflow.">maxError</a> = <a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a> / (pid-&gt;<a class="code" href="structPID__DATA.html#30e77a609c5cd17e9d74e1e8315bf883" title="The Proportional tuning constant, multiplied with SCALING_FACTOR.">P_Factor</a> + 1);<a name="l00047"></a>00047   pid-&gt;<a class="code" href="structPID__DATA.html#e580ff6ff0cb31d86d094f1de1270bd7" title="Maximum allowed sumerror, avoid overflow.">maxSumError</a> = <a class="code" href="pid_8h.html#c375d861606f40c7616251bb33f7c9ab">MAX_I_TERM</a> / (pid-&gt;<a class="code" href="structPID__DATA.html#27fadf88e502b77612cf733ca336e1a9" title="The Integral tuning constant, multiplied with SCALING_FACTOR.">I_Factor</a> + 1);<a name="l00048"></a>00048 }<a name="l00049"></a>00049 <a name="l00050"></a>00050 <a name="l00059"></a><a class="code" href="pid_8h.html#c0129c983e062dd6ee925ea932bae93f">00059</a> <a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> <a class="code" href="pid_8c.html#c0129c983e062dd6ee925ea932bae93f" title="PID control algorithm.">pid_Controller</a>(<a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> setPoint, <a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> processValue, <span class="keyword">struct</span> <a class="code" href="structPID__DATA.html" title="PID Status.">PID_DATA</a> *pid_st)<a name="l00060"></a>00060 {<a name="l00061"></a>00061   <a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a> error, p_term, d_term;<a name="l00062"></a>00062   <a class="code" href="stdint_8h.html#2ba621978a511250f7250fb10e05ffbe">int32_t</a> i_term, ret, temp;<a name="l00063"></a>00063 <a name="l00064"></a>00064   error = setPoint - processValue;<a name="l00065"></a>00065 <a name="l00066"></a>00066   <span class="comment">// Calculate Pterm and limit error overflow</span><a name="l00067"></a>00067   <span class="keywordflow">if</span> (error &gt; pid_st-&gt;<a class="code" href="structPID__DATA.html#80263266448b31cb1b80b30a9a990fe3" title="Maximum allowed error, avoid overflow.">maxError</a>){<a name="l00068"></a>00068     p_term = <a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>;<a name="l00069"></a>00069   }<a name="l00070"></a>00070   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (error &lt; -pid_st-&gt;<a class="code" href="structPID__DATA.html#80263266448b31cb1b80b30a9a990fe3" title="Maximum allowed error, avoid overflow.">maxError</a>){<a name="l00071"></a>00071     p_term = -<a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>;<a name="l00072"></a>00072   }<a name="l00073"></a>00073   <span class="keywordflow">else</span>{<a name="l00074"></a>00074     p_term = pid_st-&gt;<a class="code" href="structPID__DATA.html#30e77a609c5cd17e9d74e1e8315bf883" title="The Proportional tuning constant, multiplied with SCALING_FACTOR.">P_Factor</a> * error;<a name="l00075"></a>00075   }<a name="l00076"></a>00076 <a name="l00077"></a>00077   <span class="comment">// Calculate Iterm and limit integral runaway</span><a name="l00078"></a>00078   temp = pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> + error;<a name="l00079"></a>00079   <span class="keywordflow">if</span>(temp &gt; pid_st-&gt;<a class="code" href="structPID__DATA.html#e580ff6ff0cb31d86d094f1de1270bd7" title="Maximum allowed sumerror, avoid overflow.">maxSumError</a>){<a name="l00080"></a>00080     i_term = <a class="code" href="pid_8h.html#c375d861606f40c7616251bb33f7c9ab">MAX_I_TERM</a>;<a name="l00081"></a>00081     pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> = pid_st-&gt;<a class="code" href="structPID__DATA.html#e580ff6ff0cb31d86d094f1de1270bd7" title="Maximum allowed sumerror, avoid overflow.">maxSumError</a>;<a name="l00082"></a>00082   }<a name="l00083"></a>00083   <span class="keywordflow">else</span> <span class="keywordflow">if</span>(temp &lt; -pid_st-&gt;<a class="code" href="structPID__DATA.html#e580ff6ff0cb31d86d094f1de1270bd7" title="Maximum allowed sumerror, avoid overflow.">maxSumError</a>){<a name="l00084"></a>00084     i_term = -<a class="code" href="pid_8h.html#c375d861606f40c7616251bb33f7c9ab">MAX_I_TERM</a>;<a name="l00085"></a>00085     pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> = -pid_st-&gt;<a class="code" href="structPID__DATA.html#e580ff6ff0cb31d86d094f1de1270bd7" title="Maximum allowed sumerror, avoid overflow.">maxSumError</a>;<a name="l00086"></a>00086   }<a name="l00087"></a>00087   <span class="keywordflow">else</span>{<a name="l00088"></a>00088     pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> = temp;<a name="l00089"></a>00089     i_term = pid_st-&gt;<a class="code" href="structPID__DATA.html#27fadf88e502b77612cf733ca336e1a9" title="The Integral tuning constant, multiplied with SCALING_FACTOR.">I_Factor</a> * pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a>;<a name="l00090"></a>00090   }<a name="l00091"></a>00091 <a name="l00092"></a>00092   <span class="comment">// Calculate Dterm</span><a name="l00093"></a>00093   d_term = pid_st-&gt;<a class="code" href="structPID__DATA.html#77a0668d1381d333ca942dc250afe32d" title="The Derivative tuning constant, multiplied with SCALING_FACTOR.">D_Factor</a> * (pid_st-&gt;<a class="code" href="structPID__DATA.html#824a2012e383ca3548dfc0969ce78af0" title="Last process value, used to find derivative of process value.">lastProcessValue</a> - processValue);<a name="l00094"></a>00094 <a name="l00095"></a>00095   pid_st-&gt;<a class="code" href="structPID__DATA.html#824a2012e383ca3548dfc0969ce78af0" title="Last process value, used to find derivative of process value.">lastProcessValue</a> = processValue;<a name="l00096"></a>00096 <a name="l00097"></a>00097   ret = (p_term + i_term + d_term) / <a class="code" href="pid_8h.html#1dfc2df4edf95f828461ec28a276d36c">SCALING_FACTOR</a>;<a name="l00098"></a>00098   <span class="keywordflow">if</span>(ret &gt; <a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>){<a name="l00099"></a>00099     ret = <a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>;<a name="l00100"></a>00100   }<a name="l00101"></a>00101   <span class="keywordflow">else</span> <span class="keywordflow">if</span>(ret &lt; -<a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>){<a name="l00102"></a>00102     ret = -<a class="code" href="pid_8h.html#aa1ac5caef84256eaeb39594e58e096f" title="Maximum values.">MAX_INT</a>;<a name="l00103"></a>00103   }<a name="l00104"></a>00104 <a name="l00105"></a>00105   <span class="keywordflow">return</span>((<a class="code" href="stdint_8h.html#269259c924dce846340ddbb810db2e3c">int16_t</a>)ret);<a name="l00106"></a>00106 }<a name="l00107"></a>00107 <a name="l00112"></a><a class="code" href="pid_8h.html#7b7ef4e688a58b21b6d36f620d8a645e">00112</a> <span class="keywordtype">void</span> <a class="code" href="pid_8c.html#7b7ef4e688a58b21b6d36f620d8a645e" title="Resets the integrator.">pid_Reset_Integrator</a>(<a class="code" href="structPID__DATA.html" title="PID Status.">pidData_t</a> *pid_st)<a name="l00113"></a>00113 {<a name="l00114"></a>00114   pid_st-&gt;<a class="code" href="structPID__DATA.html#bb4705ed19c8668cc3b2a290bcad221d" title="Summation of errors, used for integrate calculations.">sumError</a> = 0;<a name="l00115"></a>00115 }</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Mon Sep 17 09:08:11 2007 for AVR221 - PID controller by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>

⌨️ 快捷键说明

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