Complete PID Tutorial - DevBhoomi Robots

Home Top Ad

Responsive Ads Here

Post Top Ad

Your Ad Spot

Tuesday, September 18, 2018

Complete PID Tutorial

PID_FCPID (Proportional, Integral, Derivative) control is a classic control algorithm that I have used for a few projects, ending with ‘good enough’ control, without really spending time learning how to properly tune the PID constants.
Time for me to fill in the gap in my knowledge, so in this two part blog I want to capture my learning. Hopefully it is useful for someone else. In this first part I will document the learning and testing rig and software. The next part will be about tuning the control loop.

Principles of Control

Any feedback control loop basically works like the diagram below
PID_FC_Controller
The Set Point (SP) is the value that we want the process to be. The controller compares the SP with the actual value of the Process Variable (PV).  If SP = PV the controller doesn’t have to do anything and it will maintain the Controller Output (CO) at the current level (or set it to zero).
If SP ≠ PV, this is expressed as an error or deviation and needs corrective action. This action takes place in the Controller, which changes the CO until SP = CV again. If a disturbance (like a change in SP, or an external event) occurs the controller has to work it out all over again. In practice the control loop operates continuously to fine tune the CO.

Test Rig System Design

The description above implies the testing rig needs to have certain characteristics to provide a valid learning platform:
  1. A repeatable process. Doing the same thing will result in the same system response, allowing it to be controlled.
  2. Allow the creation of system disturbance to test the control response.
  3. Be cheap, simple and easy to build.
  4. As this is an Arduino project, be suitable for digital control.
Somewhere in my web browsing I had come across an idea for a project that seemed to suit this application. The project involved a fan blowing into a vertical clear plastic pipe from below to keep a table tennis ball at a set height in the pipe.
Providing the ball is a loose fit in the tube, this arrangement is inherently unstable and will cause it’s own disturbance. Also, the ball ‘floats’ because of the fan air pressure below and I would expect that the higher it would go up the tube the more unstable it would be (which was found to be true in practice).
The control feedback is a simple distance measurement and the control output is a signal for fan speed, both of which are eminently doable with an Arduino Uno processor.
PID_FC_Block_DiagramThe block diagram for the system is shown on the right. Except for the tube, all the other materials were already at hand. A table tennis ball had a 40mm diameter and standard tube is available with 45mm internal diameter – perfect!
The ultrasonic sensor is a very cheap and commonly available SR-04. The fan was a small 24V laptop cooling fan I salvaged some time ago.
The L293D motor controller was from my parts stock. It simplifies the design of the hardware as it has built-in motor output diodes. An alternative is a transistor or MOSFET based switching circuit.

Making the Test Rig

PID_FC_Hardware1The stand for the equipment was made from wood and plywood scraps, glued and screwed. construction is self-explanatory by looking at the pictures below – not pretty but it works.
No dimensions are critical except for the distance between the top of the tube and the sensor. The sensor has to be far enough not to block airflow out of the tube, which stops the ball from rising, and close enough that the ultrasonic ‘pings’ go into the tube and don’t hit the top rim. Some trial and error found a sweet spot that worked.
PID_FC_BOM
The L293D is wired as per the diagram on the left. In the photo this IC is on the mini-breadboard. The L293D direction controls I1 and I2 are connected to 5V and GND as the fan is not reversible. The Arduino PWM (Control Output) pin is connected to the enable pin to provide the required fan speed control. The 5V power supply for the control circuit comes from the USB interface on the Arduino. The 24V motor supply is from a bench top power supply.

PID_FC_L293DControl Software

Finally, the control sketch needs to do a few things other than just run the PID control.
  • To make this an effective learning rig, the PID constants and the set point should be editable without recompiling and downloading application.
  • The software should log data to the Serial Monitor so that it can be graphed offline or charted with the Arduino IDE Serial Plotter. This should be controllable (on/off) as there is likely to be a lot of data.
  • Once optimized, PID parameters can be saved to EEPROM and will be reloaded at the next system restart.
The LCD display and the rotary encoder implement a user interface for a menu options that allow the online changes required.
The full sketch is available from my code repository.
In the next instalment, the rig gets used for experiments.


  I described building a test apparatus that allows me to experiment with tuning a PID loop controlling a levitating pin pong ball in a tube.
This second installment is about trying different hands-on methods of tuning the loop, understanding how they are derived, and how well they perform compared to each other.

What does “Tuning a PID Controller” mean?

I won’t cover the theory of PID controllers – there is a lot of material already available ([1] is a good overview), but in general a PID controller is configured using three parameters acting in sum to close the loop error – proportional gain (Kp), integral time (Ti), and derivative time (Td).
Keeping it simple:
  • The proportional action Kp will reduce the steady state error. Increasing Kp reduces rise time  – the controller responds more aggressively to the error.
  • The derivative action Td interprets the change of slope of error changes. Td is the look-ahead time to try to estimate future system behavior. Large values of Td create overshoots as it ‘guesses’ wrongly.
  • Ti eliminates steady state error and reduces rise time. Ti may improve the response of the system but it is a little bit tricky and PD control is often sufficient.
These parameters can be derived from models of the process being controlled. However, as this does not apply in many to the kinds of systems I build, I will be looking at  heuristic (trial and error) methods for estimating the values.
Tuning the loop is about finding a combination of these three parameters that gives an appropriate response to a disturbance, as illustrated in the figure below. The ‘right’ response depends on what the loop is controlling (objective) and the expectations of the person doing the tuning (subjective).
PID_Tuning_Aim
For the purpose of this experiment, I will be aiming for the ‘Acceptable’ curve in the figure – I am not concerned with some overshoot and I want to have a relatively fast response.

Running in Manual

The first thing to do was run the test rig in manual mode (ie, with the PID controls turned off) to get a feel for how the system behaves. Conclusions:
  • The system is inherently unstable – you cannot ‘set and forget’. The ball rarely stays in one place very long with the CO set constant.
  • At the bottom end of the tube, near the fan, the ball vibrates severely and the small volume of air under the ball struggles to keep it levitating.
  • At the open (top) end of the tube the fan struggles to keep enough airflow into the tube to keep the ball there.
  • There is a section in the center of the tube where airflow from the fan is laminar and vibration is reduced. This zone extends from about 10cm up to 20cm up the tube. This is the sweet spot for testing system step response.
  • The resolution of the ultrasonic sensor is 1cm and the vibrations of the ball can be the same magnitude, causing a spiky and a noisy signal. This could be compensated for in the software, but for the purposes of the experiment the raw signal is used.
So, for these tests described, the system disturbance is a step change in SP from 10 to 20. All other testing attributes are held constant, except for the PID parameters being tested.
All the graphs and plots are captured in real time using the Arduino IDE Serial Plotter. The x axis is the number of the data point, with each datapoint generated once per PID cycle (50ms in this experiment, and set by the SAMPLING_PERIOD constant in the code). The lines on the charts are CO (green), SP (blue) and CV (red).

Heuristic A Method (unidentified)

This method was found here in response to a forum question. The method is supposed to provide a simple way to get a good baseline tune for small, low torque motors (ie, like the fan) with little or no gearing.
The method consists of the following steps:
  1. Set all gains to zero.
  2. Increase the Kp until the response to a disturbance is steady oscillation.
  3. Increase the Td until the oscillations stop (i.e. it’s critically damped).
  4. Repeat steps 2 and 3 until increasing the Td does not stop the oscillations.
  5. Set Kp and Td to the last stable values.
  6. Increase Ti gain until the convergence to the set point occurs with or without overshoot at an acceptable rate.
PID_Tuning_Heuristic_1Step 2 was straightforward and I eventually obtained an oscillating system response, shown in the figure at left, with Kp=6.6.
This response curve is also the basis for the constants calculated in the Zeigler-Nichols method below. Between readings 2249 and 2239 (100 data points at 50ms each) on the x-axis are 2.5 oscillations, giving 5s for 2.5 oscillations, or 2s/osc.



PID_Tuning_Heuristic_2Step 3, increasing Td, yielded the graph at right when Td=0.4. Interesting to note at this stage that the PID converges to a stable response but is offset from the actual SP.
In my case, iterating additional steps 2 & 3 yielded nothing, so I moved on to Step 4.


PID_Tuning_Heuristic_3Step 4 concluded when I achieved an ‘acceptable’ response with Ti=1.2.
It is interesting to note that the CO line (green) is working very hard to maintain control. In a mechanical system (eg a physical valve actuating) this would cause excessive wear and would probably not be acceptable. In my case of an all-electric system running a fan motor, this is not a major issue.



Finally, the fully controlled step response up (10 to 20) and down (20 to 10) are show in the figure below.
PID_Tuning_Heuristic_4
Heuristic A Kp=6.6 Ti=0.4 Td=1.2

Zeigler-Nichols Method

The Zeigler-Nicols method has been around since the 1940’s and is a popular heuristic PID tuning method, well described in the literature (see [2]). The ZN rules work well on processes where the dead time is less than half the length of the time constant.
The basic tuning steps are:
  1. Set all gains to zero.
  2. Increase the Kp until the response to a disturbance is steady oscillation. This is called the ‘ultimate’ gain Ku.
  3. Measure the ‘ultimate’ oscillation period Tu at this steady state.
Ku and Tu can then be used to calculate values for Kp, Ti and Td, depending on the type of control algorithm implemented, according to the table below (taken from [2]).
Control Type Kp Ti Td
P 0.5Ku
PI 0.45Ku Tu/1.2
PD 0.8Ku Tu/8
Classic PID 0.6Ku Tu/2 Tu/8
Pessen Integral Rule 0.7Ku Tu/2.5 3Tu/20
Some overshoot 0.33Ku Tu/2 Tu/3
No overshoot 0.2Ku Tu/2 Tu/3
Ku and Tu were determined in Step 2 in the previous method (Ku=6.6, Tu=2). Calculating it all out in a spreadsheet provides all the coefficients for the different test settings.
PID_ZN_CalcsThe charts obtained are shown below. The Classic and Pessen Integral Rule PID parameters performed the best in this situation. P was unacceptable due to the constant error from the SP (as expected). Neither of the ‘overshoot’ parameters worked to prevent overshoot but they damped the oscillations better than the pure PID parameters. I expect the overshoot is due to the characteristics of the equipment – it is really hard not to overshoot as the air pressure builds up under the ball. In all cases the control response could potentially be manually tuned further.

Cohen-Coon Method

Cohen-Coon tuning rules are second in popularity to the Ziegler-Nichols rules. Cohen and Coon published their method around a decade after Zeigler-Nichols, in the 1950’s. The CC rules work well on processes where the dead time is less than two times the length of the time constant.
The rules for this tuning method are given in [2]. It is far more involved that the ZN method as it requires measuring a process time constant.
However, as the ball levitation exercise has a short dead time constant (shorter than the process time constant) there seems to be little to gain in trying this alternative method so I will leave this for another time.

Conclusion

I set out to find out how to tune a PID loop and achieved that aim.
So which would I choose out of all the combinations tried? Heuristic A seems to give the most responsive settings for this equipment, followed by the ZN Classic and PIR parameters. In comparison to Heuristic A, all ZN parameters create less less extreme CO variation, which may be important in some applications.

References

  1. Wikipedia https://en.wikipedia.org/wiki/PID_controller is a good overview of PID loops
  2. Zeigler-Nichols Method at https://en.wikipedia.org/wiki/Ziegler-Nichols_method
  3. “Cohen-Coon Tuning Rules” at http://blog.opticontrols.com/archives/383
  4. Tim Westcott, “PID without the PhD”, Embedded Systems Programming, October 2000 found at http://www.embedded.com/design/prototyping-and-development/4211211/PID-without-a-PhD

Post Top Ad

Your Ad Spot