{"id":968,"date":"2022-05-25T05:24:12","date_gmt":"2022-05-25T05:24:12","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=968"},"modified":"2022-06-19T07:38:49","modified_gmt":"2022-06-19T07:38:49","slug":"working-with-stm32-and-pid-controller-part-2-code-implementation","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=968","title":{"rendered":"Working with STM32 and PID Controller Part 2: Code implementation"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"364\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1024x364.png\" alt=\"\" class=\"wp-image-961\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1024x364.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-300x107.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-768x273.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1536x546.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-2048x727.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1150x408.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-750x266.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-400x142.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-250x89.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=960\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=960\" target=\"_blank\">here<\/a>), the PID controller has been introduced and discussed. This part, we shall use the equation introduced in part 1 to implement the code.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Header file implementation.<\/li><li>Source file implementation.<\/li><li>Main implementation.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Header File Implementation:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>To start off, create a new header file with name of pid.h.<\/p>\n\n\n\n<p>Inside this header file, we shall use header guard as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef PID_H_\n#define PID_H_\n\n\n#endif \/* PID_H_ *\/<\/pre><\/div>\n\n\n\n<p>Then we create a data structure with the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Kp<\/li><li>Ki<\/li><li>Kd<\/li><li>Ts<\/li><li>Set_point<\/li><li>Anti_windup_error<\/li><li>Out_max<\/li><li>Out_min<\/li><li>Anit_windup<\/li><\/ul>\n\n\n\n<p>the structure shall be like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef struct\n\t{\n\tfloat Kp;\n\tfloat Ki;\n\tfloat Kd;\n\tfloat Ts;\n\tfloat Set_point;\n\tfloat Anti_windup_error;\n\tfloat Outmin;\n\tfloat Outmax;\n\tint Anti_windup;\n\n\t}PID_Param_t;\n<\/pre><\/div>\n\n\n\n<p>Also the header file shall contain the initialization function and compute function as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void PID_init(PID_Param_t *par);\nfloat PID_Calculation(float input);\n<\/pre><\/div>\n\n\n\n<p>Thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Source File Implementation:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>To start off, create a new source file with name of pid.c and include the pid.h file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;pid.h&quot;<\/pre><\/div>\n\n\n\n<p>We need some global variable as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float Kp,Ki,Kd,Ts,Outmin,Outmax,set_point,antiwinduperror;\nint windup;\nfloat error,prev_input,error_sum;<\/pre><\/div>\n\n\n\n<p>Those variable are only accessed in the source file<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For the initialization function, the declared variable are set according to the values stored in the structure as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void PID_init(PID_Param_t *par)\n\t{\n\n\t;\n\tKp=par-&gt;Kp;\n\tKi=par-&gt;Ki;\n\tKd=par-&gt;Kd;\n\tTs=par-&gt;Ts;\n\tset_point=par-&gt;Set_point;\n\tantiwinduperror=par-&gt;Anti_windup_error;\n\tOutmin=par-&gt;Outmin;\n\tOutmax=par-&gt;Outmax;\n\twindup=par-&gt;Anti_windup;\n\n\tif(0==par-&gt;Anti_windup_error){antiwinduperror=10;}\n\t}\n<\/pre><\/div>\n\n\n\n<p>Now, for the calculation:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float PID_Calculation(float input)\n\t{\n\terror=(set_point-input);\n\n\tfloat out;\n\tif(Anti_windup_enabled==windup)\n\t\t{\n\n\t\tif(antiwinduperror&lt;abs(error))\n\t\t\t{\n\t\t\tout=Kp*(error)+Kd*(input-prev_input)\/Ts;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tout=(Kp*(error)) +( Ki*(Ki_sum)*Ts) -( Kd*(input-prev_input)\/Ts);\n\t\t\t}\n\n\t\t}\n\n\telse\n\t\t{\n\t\tout=Kp*(error) + Ki*(Ki_sum)*Ts - Kd*(input-prev_input)\/Ts;\n\t\t}\n\tKi_sum=Ki_sum+(Ki_sum);\n\tif(out&gt;Outmax){out=Outmax;}\n\tif(out&lt;Outmin){out=Outmin;}\n\tprev_input=input;\n\treturn out;\n\t}\n<\/pre><\/div>\n\n\n\n<p>First, the error calculation:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">error=input-set_point;<\/pre><\/div>\n\n\n\n<p>Accumulate the error for the integrator part:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">error_sum+=error;<\/pre><\/div>\n\n\n\n<p>Check if the error is bigger than the output max and less than output minimum as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tif(error_sum&lt;Outmin){error_sum=Outmin;}\n\tif(error_sum&gt;Outmax){error_sum=Outmax;}<\/pre><\/div>\n\n\n\n<p>For the calculation, we have two paths, first one if the anti windup is enabled:<\/p>\n\n\n\n<p>This will have two condition:<\/p>\n\n\n\n<p>If the error bigger than what the anti windup set point, the calculation shall not include the integrator part:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if(antiwinduperror&lt;abs(error))\n\t\t\t{\n\t\t\t*output=Kp*(error)+Kd*(input-prev_input)\/Ts;\n\t\t\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>If the error is less, calculate with integrator part :<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">else\n\t\t\t{\n\t\t\t*output=Kp*(error) + Ki*(error_sum)*Ts - Kd*(input-prev_input)\/Ts;\n\t\t\t}\n<\/pre><\/div>\n\n\n\n<p>If the ani windup is not active, calculate the PID out as usual:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">else\n\t\t{\n\t\t*output=Kp*(error) + Ki*(error_sum)*Ts - Kd*(input-prev_input)\/Ts;\n\t\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, store the previous input for the differential part:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tprev_input=input;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Main Implementation:<\/h2>\n\n\n\n<p>For the main source file, we shall first include the header file following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;pid.h&quot;<\/pre><\/div>\n\n\n\n<p>Then declare the structure of the PID parameters as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">PID_Param_t PIDParam;<\/pre><\/div>\n\n\n\n<p>Set the PID parameters as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tPIDParam.Kp=15.2;\n\tPIDParam.Kd=0.3;\n\tPIDParam.Ki=0.001;\n\tPIDParam.Outmin=0;\n\tPIDParam.Outmax=1000;\n\tPIDParam.Ts=200;\n\tPIDParam.Set_point=200;\n\tPIDParam.Anti_windup=Anti_windup_enabled;\n\tPIDParam.Anti_windup_error=10;<\/pre><\/div>\n\n\n\n<p>Note: those are not actual parameters, just for demonstration only .<\/p>\n\n\n\n<p>Then pass the address of the structure to the PID_init function as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">PID_init(&amp;PIDParam);<\/pre><\/div>\n\n\n\n<p>The compute_pid function shall be called according the sample time stated in the structure:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if(millis()-previous&gt;PIDParam-&gt;Ts)\n\t\t\t\t{\n\t\t\t\tPID_Calculation(input, &amp;output);\n\t\t\t\tprevious=millis();\n\t\t\t\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In part three, we shall see how to use PID in simple application<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), the PID controller has been introduced and discussed. This part, we shall use the equation introduced in part 1 to implement the code. In this guide, we shall cover the following: Header file implementation. Source file implementation. Main implementation. 1. Header File Implementation: To start off, create a new header [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,12],"tags":[],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/968"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=968"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":1033,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions\/1033"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}