Auto Tuning Pid Arduino
You don’t want to manually tune the PID gains of your motor controller anymore? Well, use genetic algorithms! To fully understand “how does genetic algorithms work?”, I decided to build a small demonstrator/simulator of an auto-tuned PID controller using a genetic algorithm.
Let’s assume the following system:
This project has been created to support tuning a PID controller for a home brewing setup using CraftBeerPI.It consists of a brewing kettle simulation, a PID controller (based on Arduino PID Library) and a PID autotune algorithm (based on Arduino PID Autotune Library). How to setup an PID library on an arduinolink to code:https://create.arduino.cc/editor/LogMaker3c-8dff-48cd-93be-e8ed52c5a898/preview. You might have heard of 'Auto Tuning' as a way to filter a singing voice to hit perfect pitches. Auto-tuning a PID controller is not quite the same thing - instead of improving your mediocre singing voice, it can help you to set the initial tuning parameters for your controller. Every 'system' has different parameters inherent in the physical.
The output of this system is an angular velocity for a given reference value as input. The angular velocity of the shaft of the motor should be as precise as possible. To ensure this, the feedback sensor measure the actual rotational speed of the shaft. The measured speed is substracted from the reference value and give the error value. The error value feed the PID controller which compute the correction factor. The PID controller consists of three gains:
- Proportional : the actual error is multiplied by the Kp gain.
- Derivative : the derivative error is multiplied by the Kd gain.
- Integral : the integral error is multiplied by the Ki gain.
The main problem is to find the value of each gain. Those values should optimise 3 characteristics : stability, accuracy and response time. First and foremost, let’s try to simulate a motor.
The motor can be modeled as a 2nd order low-pass filter. In the Laplace space, its transfer function is:
For simulation purpose, this continuous transfer function must be sampled. Therefore, the bilinear transform (aka Tustin’s method) should be used in order to find the respective digital filter. The bilinear transform aproximate . Applying this transform on the continuous transfer function gives:
Well, the hard work is done! The inverse Z transform leads to the following output equation:
The coefficients C1, C2, C3, C4 and C5 are computed according to the transfer function found on this page. To make it simple, I also used a fixed sampling period (1ms).
Now we are ready to implement the filter! For further details, please have a look to https://github.com/Kev-J/PID-autotune/blob/master/src/DummyMotor.cpp.
The digital PID formula used in this project is as follow:
With y(n) the output function and x(n) the input function. Let’s try to find the best Kp, Kd and Ki gains thanks to a genetic algorithm.
Adobe premier 2017 crack. Basically, a genetic algorithm is inspired by natural selection.
Diagram of the genetic algorithm used in this project.
On this project, each “genome” have three “genes” : Kp, Ki, Kd. The genetic algorithm try to find the best genome, thus, the best PID controller by following these steps:
- First and foremost, the population of genome is generated randomly. The genes are bounded to some user defined minimum and maximum limits.
- Each genome are evaluated by the fitness function. This function simulate a closed loop containing the PID controller and the motor. The fitness ratio is computed by the squarred error.
- The population is sorted according to the fitness function output : the fitness ratio.
- If the EliteNumber variable is greater than zero, then the EliteNumber best genomes are copied to the next population.
- As in natural selection, 2 parents are selected to give birth to offsprings. The parents are selected according to there fitness ratios.
- The arithmetic crossover operator generates offspring from pairs of parents by mixing their genes. All offspring’s genes are generated by choosing a random point between the first parent’s genes and second parent’s genes.
- In order to add more “random” to the genetic algorithm’s exploration, a gaussian mutation operator is applied on the new generated offspring.
- Finally, the offspring is added to the next population.
- The algorithm iterate until the user decide to stop it.
Please note that the crossover and mutation operator are applied with a crossover/mutation probability. If crossover does not occurs, the parents are simply copied in the next population.
Here is a short video of the program:
Wanna give it a try? Please have a look at https://github.com/Kev-J/PID-autotune
Adobe premiere elements 14 mac download. Do not hesitate to contact me for mistakes/bugs report.
Arduino Pid Motor
- Dr.Abdul-Kareem Z. Mansoor, Dr.Thair A. Salih, Mohamed Y. Hazim, “Self-tuning PID Controller using Genetic Algorithm”, Iraqi Journal of Statistical Science (20) 2011
- Jin-Sung Kim, Jin-Hwan Kim, Ji-Mo Park, Sung-Man Park, Won-Yong Choe and Hoon Heo, “Auto Tuning PID Controller based on Improved Genetic Algorithm for Reverse Osmosis Plant”,World Academy of Science, Engineering and Technology Vol:2 2008-11-28