📄 pid_8c.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>AVR221 - PID controller: pid.c File Reference</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.7 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div><h1>pid.c File Reference</h1><hr><a name="_details"></a><h2>Detailed Description</h2>General PID implementation for AVR. <p>Discrete PID controller implementation. Set up by giving P/I/D terms to Init_PID(), and uses a struct <a class="el" href="structPID__DATA.html">PID_DATA</a> to store internal values.<p><ul><li>File: <a class="el" href="pid_8c.html">pid.c</a></li><li>Compiler: IAR EWAAVR 4.11A</li><li>Supported devices: All AVR devices can be used.</li><li>AppNote: AVR221 - Discrete PID controller</li></ul><p><dl compact><dt><b>Author:</b></dt><dd>Atmel Corporation: <a href="http://www.atmel.com">http://www.atmel.com</a> <br> Support email: <a href="mailto:avr@atmel.com">avr@atmel.com</a></dd></dl><dl compact><dt><b>Name</b></dt><dd>RELEASE_1_0 </dd></dl><dl compact><dt><b>Revision</b></dt><dd>1.1 </dd></dl><dl compact><dt><b>RCSfile</b></dt><dd><a class="el" href="pid_8c.html">pid.c</a>,v </dd></dl><dl compact><dt><b>Date</b></dt><dd>2006/02/16 11:46:13 </dd></dl><p>Definition in file <a class="el" href="pid_8c-source.html">pid.c</a>.<p><code>#include "<a class="el" href="pid_8h-source.html">pid.h</a>"</code><br><code>#include "<a class="el" href="stdint_8h-source.html">stdint.h</a>"</code><br><p>Include dependency graph for pid.c:<p><center><img src="pid_8c__incl.png" border="0" usemap="#pid.c_map" alt="Include dependency graph"></center><map name="pid.c_map"><area href="pid_8h.html" shape="rect" coords="58,156,108,183" alt=""><area href="stdint_8h.html" shape="rect" coords="19,81,83,108" alt=""></map><p><a href="pid_8c-source.html">Go to the source code of this file.</a><table border=0 cellpadding=0 cellspacing=0><tr><td></td></tr><tr><td colspan=2><br><h2>Functions</h2></td></tr><tr><td class="memItemLeft" nowrap align=right valign=top><a class="el" href="stdint_8h.html#a57">int16_t</a> </td><td class="memItemRight" valign=bottom><a class="el" href="pid_8c.html#a1">pid_Controller</a> (<a class="el" href="stdint_8h.html#a57">int16_t</a> setPoint, <a class="el" href="stdint_8h.html#a57">int16_t</a> processValue, struct <a class="el" href="structPID__DATA.html">PID_DATA</a> *pid_st)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">PID control algorithm. <a href="#a1"></a><br><br></td></tr><tr><td class="memItemLeft" nowrap align=right valign=top>void </td><td class="memItemRight" valign=bottom><a class="el" href="pid_8c.html#a0">pid_Init</a> (<a class="el" href="stdint_8h.html#a57">int16_t</a> p_factor, <a class="el" href="stdint_8h.html#a57">int16_t</a> i_factor, <a class="el" href="stdint_8h.html#a57">int16_t</a> d_factor, struct <a class="el" href="structPID__DATA.html">PID_DATA</a> *pid)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Initialisation of PID controller parameters. <a href="#a0"></a><br><br></td></tr><tr><td class="memItemLeft" nowrap align=right valign=top>void </td><td class="memItemRight" valign=bottom><a class="el" href="pid_8c.html#a2">pid_Reset_Integrator</a> (<a class="el" href="structPID__DATA.html">pidData_t</a> *pid_st)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Resets the integrator. <a href="#a2"></a><br><br></td></tr></table><hr><h2>Function Documentation</h2><a class="anchor" name="a1" doxytag="pid.c::pid_Controller" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> <a class="el" href="stdint_8h.html#a57">int16_t</a> pid_Controller </td> <td class="md" valign="top">( </td> <td class="md" nowrap valign="top"><a class="el" href="stdint_8h.html#a57">int16_t</a> </td> <td class="mdname" nowrap> <em>setPoint</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td></td> <td class="md" nowrap><a class="el" href="stdint_8h.html#a57">int16_t</a> </td> <td class="mdname" nowrap> <em>processValue</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td></td> <td class="md" nowrap>struct <a class="el" href="structPID__DATA.html">PID_DATA</a> * </td> <td class="mdname" nowrap> <em>pid_st</em></td> </tr> <tr> <td></td> <td class="md">) </td> <td class="md" colspan="2"></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>PID control algorithm. <p>Calculates output from setpoint, process value and PID status.<p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td></td><td valign=top><em>setPoint</em> </td><td>Desired value. </td></tr> <tr><td></td><td valign=top><em>processValue</em> </td><td>Measured value. </td></tr> <tr><td></td><td valign=top><em>pid_st</em> </td><td>PID status struct. </td></tr> </table></dl><p>Definition at line <a class="el" href="pid_8c-source.html#l00059">59</a> of file <a class="el" href="pid_8c-source.html">pid.c</a>.<p>References <a class="el" href="pid_8h-source.html#l00041">PID_DATA::D_Factor</a>, <a class="el" href="pid_8h-source.html#l00039">PID_DATA::I_Factor</a>, <a class="el" href="stdint_8h-source.html#l00028">int16_t</a>, <a class="el" href="stdint_8h-source.html#l00030">int32_t</a>, <a class="el" href="pid_8h-source.html#l00033">PID_DATA::lastProcessValue</a>, <a class="el" href="pid_8h-source.html#l00055">MAX_I_TERM</a>, <a class="el" href="pid_8h-source.html#l00053">MAX_INT</a>, <a class="el" href="pid_8h-source.html#l00043">PID_DATA::maxError</a>, <a class="el" href="pid_8h-source.html#l00045">PID_DATA::maxSumError</a>, <a class="el" href="pid_8h-source.html#l00037">PID_DATA::P_Factor</a>, <a class="el" href="pid_8h-source.html#l00025">SCALING_FACTOR</a>, and <a class="el" href="pid_8h-source.html#l00035">PID_DATA::sumError</a>.<p>Referenced by <a class="el" href="main_8c-source.html#l00117">main()</a>.<p><pre class="fragment"><div>00060 {00061 <a class="code" href="stdint_8h.html#a57">int16_t</a> error, p_term, d_term;00062 <a class="code" href="stdint_8h.html#a59">int32_t</a> i_term, ret, temp;00063 00064 error = setPoint - processValue;00065 00066 <span class="comment">// Calculate Pterm and limit error overflow</span>00067 <span class="keywordflow">if</span> (error > pid_st-><a class="code" href="structPID__DATA.html#o3">maxError</a>){00068 p_term = <a class="code" href="pid_8h.html#a1">MAX_INT</a>;00069 }00070 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (error < -pid_st-><a class="code" href="structPID__DATA.html#o3">maxError</a>){00071 p_term = -<a class="code" href="pid_8h.html#a1">MAX_INT</a>;00072 }00073 <span class="keywordflow">else</span>{00074 p_term = pid_st-><a class="code" href="structPID__DATA.html#o5">P_Factor</a> * error;00075 }00076 00077 <span class="comment">// Calculate Iterm and limit integral runaway</span>00078 temp = pid_st-><a class="code" href="structPID__DATA.html#o6">sumError</a> + error;00079 <span class="keywordflow">if</span>(temp > pid_st-><a class="code" href="structPID__DATA.html#o4">maxSumError</a>){00080 i_term = <a class="code" href="pid_8h.html#a3">MAX_I_TERM</a>;00081 pid_st-><a class="code" href="structPID__DATA.html#o6">sumError</a> = pid_st-><a class="code" href="structPID__DATA.html#o4">maxSumError</a>;00082 }00083 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(temp < -pid_st-><a class="code" href="structPID__DATA.html#o4">maxSumError</a>){00084 i_term = -<a class="code" href="pid_8h.html#a3">MAX_I_TERM</a>;00085 pid_st-><a class="code" href="structPID__DATA.html#o6">sumError</a> = -pid_st-><a class="code" href="structPID__DATA.html#o4">maxSumError</a>;00086 }00087 <span class="keywordflow">else</span>{00088 pid_st-><a class="code" href="structPID__DATA.html#o6">sumError</a> = temp;00089 i_term = pid_st-><a class="code" href="structPID__DATA.html#o1">I_Factor</a> * pid_st-><a class="code" href="structPID__DATA.html#o6">sumError</a>;00090 }00091 00092 <span class="comment">// Calculate Dterm</span>00093 d_term = pid_st-><a class="code" href="structPID__DATA.html#o0">D_Factor</a> * (pid_st-><a class="code" href="structPID__DATA.html#o2">lastProcessValue</a> - processValue);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -