In the first part of this blog 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 ( 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).
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:
- Set all gains to zero.
- Increase the Kp until the response to a disturbance is steady oscillation.
- Increase the Td until the oscillations stop (i.e. it’s critically damped).
- Repeat steps 2 and 3 until increasing the Td does not stop the oscillations.
- Set Kp and Td to the last stable values.
- Increase Ti gain until the convergence to the set point occurs with or without overshoot at an acceptable rate.
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.
In my case, iterating additional steps 2 & 3 yielded nothing, so I moved on to Step 4.
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.
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 ). 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:
- Set all gains to zero.
- Increase the Kp until the response to a disturbance is steady oscillation. This is called the ‘ultimate’ gain Ku.
- 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 ).
|Pessen Integral Rule||0.7Ku||Tu/2.5||3Tu/20|
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.
The 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 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 . 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.
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.
- Wikipedia https://en.wikipedia.org/wiki/PID_controller is a good overview of PID loops
- Zeigler-Nichols Method at https://en.wikipedia.org/wiki/Ziegler-Nichols_method
- “Cohen-Coon Tuning Rules” at http://blog.opticontrols.com/archives/383
- 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