0% found this document useful (0 votes)
77 views

PID Arduino

This document introduces a series of posts about improving a beginner's PID controller code. It will explain the PID algorithm and controller code in detail. The goal is to explain what is happening in the PID library code and help others write their own PID code. The first post describes a basic PID controller code as most people first learn it. The series will then modify the code to address issues like sample time, derivative kick, tuning changes, windup, on/off control, initialization, and direction. This will result in a robust PID algorithm on par with industrial controllers.

Uploaded by

George
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

PID Arduino

This document introduces a series of posts about improving a beginner's PID controller code. It will explain the PID algorithm and controller code in detail. The goal is to explain what is happening in the PID library code and help others write their own PID code. The first post describes a basic PID controller code as most people first learn it. The series will then modify the code to address issues like sample time, derivative kick, tuning changes, windup, on/off control, initialization, and direction. This will result in a robust PID algorithm on par with industrial controllers.

Uploaded by

George
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

http://brettbeauregard.

com/blog/2011/04/improving-the-beginners-pid-introduction/
Improving the Beginners PID Introduction
In conjunction with the release of the new Arduino PID Library Ive decided to release this series of posts.
The last library, while solid, didnt really come with any code explanation. This time around the plan is to
explain in great detail why the code is the way it is. Im hoping this will be of use to two groups of people:
People directly interested in whats going on inside the Arduino PID library will get a detailed
explanation.
Anyone writing their own PID algorithm can take a look at how I did things and borrow whatever
they like.
Its going to be a tough slog, but I think I found a not-too-painful way to explain my code. Im going to
start with what I call The Beginners PID. Ill then improve it step-by-step until were left with an
efficient, robust pid algorithm.
The Beginners PID
Heres the PID equation as everyone first learns it:

This leads pretty much everyone to write the following PID controller:
1 /*working variables*/
2 unsigned long lastTime;
3 double Input, Output, Setpoint;
4 double errSum, lastErr;
5 double kp, ki, kd;
6 void Compute()
7 {
8 /*How long since we last calculated*/
9 unsigned long now = millis();
10 double timeChange = (double)(now - lastTime);
11
12 /*Compute all the working error variables*/
13 double error = Setpoint - Input;
14 errSum += (error * timeChange);
15 double dErr = (error - lastErr) / timeChange;
16
17 /*Compute PID Output*/
18 Output = kp * error + ki * errSum + kd * dErr;
19
20 /*Remember some variables for next time*/
21 lastErr = error;
22 lastTime = now;
23}
24
25void SetTunings(double Kp, double Ki, double Kd)
26{
27 kp = Kp;
28 ki = Ki;
29 kd = Kd;
30}
Compute() is called either regularly or irregularly, and it works pretty well. This series isnt about works
pretty well though. If were going to turn this code into something on par with industrial PID controllers,
well have to address a few things:
1. Sample Time The PID algorithm functions best if it is evaluated at a regular interval. If the
algorithm is aware of this interval, we can also simplify some of the internal math.
2. Derivative Kick Not the biggest deal, but easy to get rid of, so were going to do just that.
3. On-The-Fly Tuning Changes A good PID algorithm is one where tuning parameters can be
changed without jolting the internal workings.
4. Reset Windup Mitigation Well go into what Reset Windup is, and implement a solution with side
benefits
5. On/Off (Auto/Manual) In most applications, there is a desire to sometimes turn off the PID
controller and adjust the output by hand, without the controller interfering
6. Initialization When the controller first turns on, we want a bumpless transfer. That is, we dont
want the output to suddenly jerk to some new value
7. Controller Direction This last one isnt a change in the name of robustness per se. its designed to
ensure that the user enters tuning parameters with the correct sign.
8. NEW: Proportional on Measurement Adding this feature makes it easier to control certain types
of processes
Once weve addressed all these issues, well have a solid PID algorithm. Well also, not coincidentally, have
the code thats being used in the lastest version of the Arduino PID Library. So whether youre trying to
write your own algorithm, or trying to understand whats going on inside the PID library, I hope this helps
you out. Lets get started.
Next >>
UPDATE: In all the code examples Im using doubles. On the Arduino, a double is the same as a float
(single precision.) True double precision is WAY overkill for PID. If the language youre using does true
double precision, Id recommend changing all doubles to floats.

Control de motor DC Arduino MATLAB


https://www.youtube.com/watch?v=-KF7Ew_Nz-A
Controlling a DC motor with Matlab GUI & Arduino PID
https://www.youtube.com/watch?v=drVRofe4qEQ
PID controller for DC motor speed control modeled in matlab based on Arduino UNO
https://www.youtube.com/watch?v=5HI7KHRr7Bc

You might also like