Pid 2
Pid 2
EmbeddedExpertIO HOME
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
https://blog.embeddedexpert.io/?p=968 1/13
02/12/2023, 12:06 Working with STM32 and PID Controller Part 2: Code implementation – EmbeddedExpertIO
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
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
}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);
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"
C
float Kp,Ki,Kd,Ts,Outmin,Outmax,set_point,antiwinduperror;
int windup;
float error,prev_input,error_sum;
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
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;
}
C
error=input-set_point;
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:
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;
}
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;
}
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"
C
PID_Param_t PIDParam;
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();
}
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
2 Comments
Test | Posted May 26, 2022 | 11:01 am
Where is part 3?
Reply
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
August 2021
July 2021
September 2020
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