0% found this document useful (0 votes)
81 views13 pages

Pid 2

The document discusses implementing a PID controller on an STM32 microcontroller. It describes adding a header file with structure definitions and function prototypes for initialization and calculation. It then discusses adding a source file that includes the header, declares global variables, and defines the initialization and calculation functions to set variables from the structure and perform the PID algorithm.

Uploaded by

Zubaid Satti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views13 pages

Pid 2

The document discusses implementing a PID controller on an STM32 microcontroller. It describes adding a header file with structure definitions and function prototypes for initialization and calculation. It then discusses adding a source file that includes the header, declares global variables, and defines the initialization and calculation functions to set variables from the structure and perform the PID algorithm.

Uploaded by

Zubaid Satti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

EmbeddedExpertIO HOME

Working with STM32 Home  All posts  Data Structures 

and PID Controller Part Working with STM32 and PID Controller Part 2...

2: Code implementation

Posted May 25, 2022 | by Husamuldeen | in Data Structures, Embedded Systems, STM32 |

Categories

 Data Structures

 Deep Learning

 Embedded Systems

 LCD

 Peripheral Drivers

 State Machine

 State Machines

 STM32
In the previous guide (here), the PID controller has been introduced and discussed. This part, we shall use  TM4C123
the equation introduced in part 1 to implement the code.
 Uncategorized

In this guide, we shall cover the following:


This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 1/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

Header file implementation.

Source file implementation.


Main implementation. Join the EmbeddedExpertIO
School

1. Header File Implementation:


To start off, create a new header file with name of pid.h.

Inside this header file, we shall use header guard as following: https://be.embedde
dexpert.io/
C
#ifndef PID_H_
#define PID_H_
 NOVEMBER 2023

#endif /* PID_H_ */ M T W T F S S

1 2 3 4 5

Then we create a data structure with the following parameters:


6 7 8 9 10 11 12

Kp 13 14 15 16 17 18 19
Ki
20 21 22 23 24 25 26
Kd

Ts 27 28 29 30

Set_point

Anti_windup_error
Out_max Tags
Out_min
ARM_Cortex_M7
Anit_windup

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-outData Structures
if you wish. Learn more here
the structure shall be like this:

https://blog.embeddedexpert.io/?p=968 2/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

C Deep Learning LCD

typedef struct Peripheral Drivers


{
Registers only
float Kp;
float Ki; State Machines STM32
float Kd;
float Ts; STM32F767 TM4C123
float Set_point;
float Anti_windup_error;
float Outmin;
float Outmax;
int Anti_windup;

}PID_Param_t;

Also the header file shall contain the initialization function and compute function as following:

C
void PID_init(PID_Param_t *par);
float PID_Calculation(float input);

Thats all for the header file.

2. Source File Implementation:


To start off, create a new source file with name of pid.c and include the pid.h file as following:
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 3/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

C
#include "pid.h"

We need some global variable as following:

C
float Kp,Ki,Kd,Ts,Outmin,Outmax,set_point,antiwinduperror;
int windup;
float error,prev_input,error_sum;

Those variable are only accessed in the source file

For the initialization function, the declared variable are set according to the values stored in the structure
as following:

C
void PID_init(PID_Param_t *par)
{

;
Kp=par->Kp;
Ki=par->Ki;
Kd=par->Kd;
Ts=par->Ts;
set_point=par->Set_point;
antiwinduperror=par->Anti_windup_error;
Outmin=par->Outmin;
Outmax=par->Outmax;
windup=par->Anti_windup;

if(0==par->Anti_windup_error){antiwinduperror=10;}
This website
} uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 4/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

Now, for the calculation:

C
float PID_Calculation(float input)
{
error=(set_point-input);

float out;
if(Anti_windup_enabled==windup)
{

if(antiwinduperror<abs(error))
{
out=Kp*(error)+Kd*(input-prev_input)/Ts;
}
else
{
out=(Kp*(error)) +( Ki*(Ki_sum)*Ts) -( Kd*(input-prev_input)/Ts);
}

else
{
out=Kp*(error) + Ki*(Ki_sum)*Ts - Kd*(input-prev_input)/Ts;
}
Ki_sum=Ki_sum+(Ki_sum);
if(out>Outmax){out=Outmax;}
if(out<Outmin){out=Outmin;}
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.
prev_input=input; Learn more here

https://blog.embeddedexpert.io/?p=968 5/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

return out;
}

First, the error calculation:

C
error=input-set_point;

Accumulate the error for the integrator part:

C
error_sum+=error;

Check if the error is bigger than the output max and less than output minimum as following:

C
if(error_sum<Outmin){error_sum=Outmin;}
if(error_sum>Outmax){error_sum=Outmax;}

For the calculation, we have two paths, first one if the anti windup is enabled:

This will have two condition:

If the error bigger than what the anti windup set point, the calculation shall not include the integrator part:
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 6/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

C
if(antiwinduperror<abs(error))
{
*output=Kp*(error)+Kd*(input-prev_input)/Ts;
}

If the error is less, calculate with integrator part :

C
else
{
*output=Kp*(error) + Ki*(error_sum)*Ts - Kd*(input-prev_input)/Ts;
}

If the ani windup is not active, calculate the PID out as usual:

C
else
{
*output=Kp*(error) + Ki*(error_sum)*Ts - Kd*(input-prev_input)/Ts;
}

Also, store the previous input for the differential part:

C
prev_input=input;

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 7/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

3. Main Implementation:
For the main source file, we shall first include the header file following:

C
#include "pid.h"

Then declare the structure of the PID parameters as following:

C
PID_Param_t PIDParam;

Set the PID parameters as following:

C
PIDParam.Kp=15.2;
PIDParam.Kd=0.3;
PIDParam.Ki=0.001;
PIDParam.Outmin=0;
PIDParam.Outmax=1000;
PIDParam.Ts=200;
PIDParam.Set_point=200;
PIDParam.Anti_windup=Anti_windup_enabled;
PIDParam.Anti_windup_error=10;

Note: those are not actual parameters, just for demonstration only .

Then pass the address of the structure to the PID_init function as following:

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here
C

https://blog.embeddedexpert.io/?p=968 8/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

PID_init(&PIDParam);

The compute_pid function shall be called according the sample time stated in the structure:

C
if(millis()-previous>PIDParam->Ts)
{
PID_Calculation(input, &output);
previous=millis();
}

In part three, we shall see how to use PID in simple application

Happy coding

About Husamuldeen

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 9/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

Working with STM32 and Working with STM32 and


External Flash W25QXX External Flash W25QXX
Part2: Reading data Part1: Reading ID

2 Comments
Test | Posted May 26, 2022 | 11:01 am

Where is part 3?

 Reply

Husamuldeen | Posted May 27, 2022 | 1:20 pm

We shall post part three soon.

 Reply

Add Comment
Your email address will not be published. Required fields are marked *

Comment

Name * Email *

This website
Websiteuses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 10/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 11/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO

Recent Comments Archives

 Husamuldeen on STM32 Advanced Timers  November 2023


Part 1: Generating PWM and
 October 2023
Complementary Output
 September 2023
 Husamuldeen on STM32 Advanced Timers
Part 1: Generating PWM and  August 2023
Complementary Output  July 2023
 Mehmet ali aydın on STM32 Advanced  June 2023
Timers Part 1: Generating PWM and
 May 2023
Complementary Output
 April 2023
 Mehmet ali aydın on STM32 Advanced
Timers Part 1: Generating PWM and  March 2023
Complementary Output  February 2023
 Mehmet ali aydın on STM32 Advanced  January 2023
Timers Part 1: Generating PWM and
 December 2022
Complementary Output
 November 2022
 October 2022
 September 2022
 August 2022
 July 2022
 June 2022
 May 2022
 April 2022
 March 2022
 February 2022
 January 2022
 December 2021
 November 2021
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out
2021if you wish. Learn more here
 October
S t b 2021
https://blog.embeddedexpert.io/?p=968 12/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO
 September 2021

 August 2021

 July 2021

 September 2020

All Rights Reserved

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Learn more here

https://blog.embeddedexpert.io/?p=968 13/13

You might also like