PID (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
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:
- A repeatable process. Doing the same thing will result in the same system response, allowing it to be controlled.
- Allow the creation of system disturbance to test the control response.
- Be cheap, simple and easy to build.
- 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 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
The 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.
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.
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.