Controlador PID - Tutorial
Controlador PID - Tutorial
Introduction: For many application, a more precise level of control is required than that of an open-loop
system. By adding a sensor that measures the condition of the load, the system can adjust itself. The
correction is made by sending an output signal from the measuring device to the controller. As shown
in Figure 1, the connection between the measurement device and controller is called the feedback loop.
The comparison between command signal and measured signal produces an error signal that applied to the
amplifier. If either the command signal is changed or the measurement feedback signal is changed due to
load condition, the control will produce a different error signal. The result will be that the actuator will
automatically be controlled to make the proper adjustment to reach the desired condition.
Figure 1: Block Diagram of a Closed-loop system, reproduced from Terry Bartelt, Industrial Control Elec-
tronics: Devices, Systems & Application
PID controller: stands for proportional-integral-derivative controller. It is one of the most popular con-
trollers which widely used in industrial control system. The controller calculates an error value as the
different between a measuring signal and the command signal or set point value. The controller consists of
three terms: the proportional, integral and derivative.
• Proportional term produces an output value that is proportional to the current error value. The
response can be adjusted multiplying the error with a constant Kp called the proportional gain.
Pout = Kp en (1)
• Integral term is the sum of the instantaneous error over time and gives the accumulated value that
should have been corrected previously. The accumulated error is then multiplied by the integral gain
Ki and added to the controller output.
n
X
Iout = Ki ei ∆T (2)
i=0
• Derivative term slows the rate of change of the controller output. Derivative control is used to
reduce the magnitude of the overshoot produced by the integral component and improve the combined
controller-process stability.
en − en−1
Dout = Kd (3)
∆T
1
The output of PID controller is the result of summing proportional(1), integral(2), and derivative(3)
terms, which can be expressed as
n
X en − en−1
un = Kp en + Ki ei ∆T + Kd (4)
i=0
∆T
where en is the error between setpoint value and measurement value, un is the controller output. n is discrete
time, ∆T is sampling time. For motor control, the controller output could be either input voltage or PWM
duty cycle.
Components required
1. Arduino UNO R3
2. Proto-board
3. Jumper (Connecting wires)
4. DC motor
5. L293D
6. Optical Encoder
7. Potentiometer
8. 5V DC power supply
2
(a) Diagram generated by Fritzing-0.7.11 (b) Schematics generated by Fritzing-0.7.11
Code:
//Define Variable
int MotorPWM = 11; // ENABLE to digital pin 11
int MotorIN_A = 8;
int MotorIN_B = 12;
int encoder0PinA = 2;
int encoder0PinB = 4;
int sensorPin = A0; // select the input pin for the potentiometer
/*working variables*/
double PV, Output, Setpoint;
double kp, ki, kd;
volatile unsigned long lastTime;
volatile double errSum, lastErr;
volatile int encoder0Pos = 0;
int sensorValue = 512;
void setup(){
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); //turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); //turn on pullup resistor
3
kd = 0.0;
Serial.begin (9600);
Serial.println("start");
}
void loop(){
sensorValue = analogRead(sensorPin); //read the value
Setpoint = (double)(sensorValue - 512)*0.35; //Full scale setpoint +/- 180deg
PV = (double)encoder0Pos*11.25; //360 deg = 32 pulses
compute(); // Find controller output
4
double dT = (double)(now - lastTime); //Our Sampling time
Description:
In this tutorial we use interrupts to read a rotary encoder. It is a perfect job for interrupt because the
interrupt service routine (doEncoder function) can be short and quick. When the Arduino sees a change
on the A channel (connect to pin 2), it immediately skips to the ”doEncoder” function which parses out
both the low-to-high and the high-to-low edges, consequently counting twice as many transitions. Thus the
resolution of encoder is increased twice. Since our encoder disc has the resolution 16 slot per revolution, the
program can count for 32 pulses when the disc is rotated for one round.
In loop function, the reference position (Setpoint) is obtained from the knob position of potentiometer.
Then, we get the position of the wheel by ”doEncoder” function. After that, ”compute” function finds
the error by substracting setpoint value with measured value. The controller output is calculated by using
expression 4. The result is used as the motor command which the sign +/- indicates the direction of rotation.
References
• http://playground.arduino.cc/Code/PIDLibrary
• http://playground.arduino.cc/Main/RotaryEncoders
• http://multimechatronics.com/images/uploads/design/2012/Optical%20Encoder%20and%20the%
20Arduino%202012.pdf
• ”Industrial Control Electronics: Devices, Systems & Application” by - Terry Bartelt.