Your shopping cart is empty!
PID for Embedded Design
By Kong Wai Weng
RH2T Mag, Vol.4, Mar 2010
PID control system has been one of the industry's most mature and commonly used control strategies for decades thanks to its simple but effective algorithm. In this article, we will discuss the basic concept of PID controller and how to implement it in the embedded system.
Introduction
A closed-loop control system is an essential topic for embedded systems, bringing together actuators and sensors with the control algorithm in software. For example, the motor speed can be controlled by varying the PWM duty cycle used to drive the motor. This is the open-loop control. However, due to the lack of feedback, the actual motor speed could not be verified. This is important because driving the motor with the same PWM duty cycle does not guarantee the motor to run at the same speed under all circumstances. A motor will run faster in load-free conditions than under load with the same PWM signal. In order to control the motor speed, we need to close the loop by adding feedback on the actual motor speed to the controller.
Besides speed control, the PID control system can also be used to control other parameters such as position, temperature, water level, stability, etc. In this article, we will discuss how to implement a PID controller for position control based on PR24.
The Problem – DC Motor Position Control.
Before we begin to design a PID controller, we need to understand the problem. In this example, we want to move the shaft of the motor from its current position to the target position.
We want to move the output shaft of the motor from the current position to the target position
There are a few terms commonly used to describe the PID control loops, such as:
- Control Variable (CV) – This is the output of the control loop. In this case, the CV is the duty cycle of the PWM signal that drives the motor.
- Process Variable (PV) – This is the feedback value returned by the system to the controller. In this example, the PV is the current angle of the motor shaft.
- Set Point (SP) – The setpoint is the value that we desire for the system. In our case, the SP is the target position of the motor shaft at an angle.
- Error (E) – Error refers to the difference between the set point and the process variable. In other words, it means how far the current position of the motor shaft is from the target position.
The Hardware – PR24
Note: PR24 is already discontinued. The sample code can be downloaded here.
Cytron’s DIY Project Set PR24 (PID Motor Controller) is the best platform for beginners to learn the PID algorithm. It has the following features:
- PIC16F876A is the main controller.
- Geared DC motor as the output.
- A multi-turn variable resistor is connected to the motor shaft for position feedback.
- 2x16 Character LCD for tuning and troubleshooting.
- Presets for PID tuning.
DIY Project Set PR24 – PID Motor Controller
The sample source code for the PR24 (PID Motor Controller) can be downloaded from Cytron’s website under the PR24 product page (Github CytronTechnologies).
The Implementation of the PID Controller
The PID controller, just like its name, comprises a proportional (P), an integral (I), and a derivative (D) part. The controller parts are introduced in the following sections individually and in combined operation.
Proportional Controller
When the current position of the motor shaft is still far away from the target position, we want to apply more power to drive the motor toward the target position so that we can reach there faster. When the shaft is getting nearer to the target position, we will reduce the power to slow it down. At the time the shaft reaches the target position, the motor needs to be stopped. If the shaft position has overshot, we need to apply negative power to the motor (reverse the motor) to bring it back to the target position.
In short, this is called a proportional controller because the power we apply to the motor is proportional to the error of the system.
The block diagram of the proportional controller
From the block diagram of the proportional controller, we can see that the PWM duty cycle (output) is the result of multiplying the error with a constant, Kp.
Error = Set Point – Process Variable
Control Variable = Kp * Error
The figure below shows an example of a proportional loop implemented in C language.
Implementation of the proportional loop in C language
The value of Kp needs to be chosen carefully in order to get the optimum system response. Lower values for Kp will tend to give smoother but slower responses.
System response for the proportional controller with low Kp
Higher values of Kp will yield a much quicker response but may cause an overshoot, where the output oscillates before settling.
System response for a proportional controller with high Kp
Excessively high values of Kp may even throw the loop into an unstable state where the output oscillates without ever settling at the set point.
System response for a proportional controller with excessively high Kp
Integral Controller
As can be seen from the graph of the P controller, the actual position of the motor shaft then settles down will not reach the target position. This is because when the current position is near the target position, the error becomes very small and the computed PWM duty cycle is too small for the motor to overcome the friction and gravity. The small error that exists when the system has settled down is called the steady-state error.
Steady-state error due to the friction and gravity
To overcome the problem of steady-state error for the P controller, the I controller is being introduced. As its name suggests, the integral is merely an accumulated error signal encountered since startup.
Integral = ∑(Error)
This total is multiplied by a constant, Ki, and is added to the loop output. Unlike the P controller, the I controller is rarely used alone, but mostly in combination with the P or PD controller. When the system has already settled down with a small steady-state error, the integral still continues to accumulate until the CV is large enough to bring the PV in line with SP. The equations for the PI controller are as follows:
Error = Set Point – Process Variable
Integral = Integral + Error
Control Variable = (Kp * Error) + (Ki * Integral)
The figure below shows an example of a PI controller implemented in C language.
Implementation of PI loop in C language
Just like the P controller, the value of Ki needs to be chosen carefully. Too low a value, the steady-state error is corrected very slowly; too high a value, the system becomes unstable and oscillates.
System response for PI controller with no steady-state error
Because the integral can grow quite large when the set point cannot be reached, some applications stop accumulating the error when the CV is saturated.
Derivative Controller
Let’s say you are driving a car, and you need to stop your car exactly 100m from your current position as soon as possible. If you are traveling at 10km/h, you would want to accelerate your car so that you can reach the target sooner. In contrast, if you are cruising at 100km/h, you need to start braking so that you can stop at 100m and will not overshoot. This is where the derivative controller comes into play.
The derivative of any variable describes how that variable changes over time. In a PID controller, the derivative is the rate of change of the error. In digital form, it can be described as:
Derivative = Error – Last Error
where Error is the current error value and Last Error is the error value for the previous iteration.
Negative values of the derivative indicate an improvement (reduction) in the error signal. For example, if the last error was 20 and the current error is 10, the derivative will be -10. When these negative values are multiplied with a constant, Kd, and are added to the output of the loop, it can slow down the system when approaching the target.
Just like the I controller, the D controller is rarely used alone, but mostly in combination with the P or PI controller. The equations for the PD controller are as follows:
Last Error = Error
Error = Set Point – Process Variable
Derivative = Error – Last Error
Control Variable = (Kp * Error) + (Kd * Derivative)
The figure below shows an example of a PD controller implemented in C language.
Implementation of PD loop in C language
The damping effect of the D controller allows the system to have a higher value of Kp and/or Ki without overshooting. Consequently, this will give the system a better response time to set point changes. However, too high a value of Kd will also have a negative effect. The D controller tense amplifies the noise that exists in the feedback loop. If the Kd is too high, the system will become jerky if the feedback loop is noisy.
System response for a PD controller
Joining Them Together – PID Controller
By joining the P, I, and D controller, we can take advantage of the combined benefits of each controller. We have the P controller for fast system response, the I controller to correct the steady-state error, and the D controller to dampen the system and reduce overshoot.
The block diagram of the PID controller
From the block diagram of the PID controller, we can see that the output of the loop is merely the sum of the output from the P, I, and D controller. The equations for the PID loop are illustrated below:
Last Error = Error
Error = Set Point – Process Variable
Integral = Integral + Error
Derivative = Error – Last Error
Control Variable = (Kp * Error) + (Ki * Integral) + (Kd * Derivative)
The figure below shows an example of a PID controller implemented in C language.
Implementation of PID loop in C language
System response for the correctly tuned PID controller
Summary
PID controller is a simple yet effective control system widely used in industry. However, implementing the PID controller is simple, but not the tuning. The process of tuning the PID parameters (Kp, Ki, and Kd) is a continuous trial and error-process. There is no exact way to calculate the value for the parameters unless the whole system is mathematically modeled and simulated. Experience is an important factor to get the optimum PID parameters based on the observation of the system behavior during the tuning process. If you have an inquiry, feel free to discuss it in our technical forum.
References:
Dennis Clark and Michael Owings: Building Robot Drive Trains.
Thomas Braunl: Embedded Robotics – Mobile Robot Design and Applications with Embedded Systems.
http://en.wikipedia.org/wiki/PID_controller
http://www.expertune.com/tutor.html