PID Based Line Follower
This is a PID implemented line follower using an Arduino ATmega168 clone, Pololu QTR-8RC sensor array, a Pololu TB6612FNG Motor Driver, and super fast and zippy pololu micrometal gear motors. ( 30:1 Gear Ratio, MP version)
PID? Is that the name of a super hero or a mega villain? It's kinda both. If you manage to tune the parameters perfecly, you'll be fascinated by the results that follow, however, at the same time, you're gonna have some sleepless nights if you get it completely wrong. :D
I am no expert here, but what information i have understood about PID, might be helpful for some beginners like me to take your next project up a notch. Basically, when implementing PID, your goal is to achieve a setpoint value. You do that by calculating your error, which is the difference of the setpoint value and your current value. For eg: in this line following robot, our setpoint value is the position of our bot when the middle sensors are exactly above the line it's following. Our current value is obviously the position of our robot at any instant. The error, as mentioned previously is the difference of these two values. Then, by multiplying the error with specific coinstants ( Kp, Ki, Kd), the motor speeds can be "predicted" as to smoothly follow a line.
I wrote a small tutorial on implementing PID in a line follower.
In other words, this robot follows a line not only by detecting the turns ( yep, that's the basic principle), but also by calculating the error, which is how far it has moved off the track, and then adjusts the motor speeds using the Proportional, Integral and Derivative technique, in short, PID.
There are many articles and infos across the web about PID, methods of fine-tuning the PID parameters, take a look, and see the results for yourself!!
With no 3D printer or CNC machine lurking around my place, and also with an intent of taking part in a national competition, i had to resort to a pre-manufactured simple stable chasis, that could to the job. I used a Pololu circular acrylic plate for the robot body.
Next up, i needed some grippy wheels and some super fast motors to get the most out of my PID routine. Again, i ordered a pair of pololu's popular micrometal gear motors, along with a pair of extended brackets, and a grippy tire set. The motors are rated at 6.0 V and reach speeds of 730 RPM.
For the line tracking part, i used the Pololu QTR-8RC array. It not only detects whether the line is bright or dark, but it intelligently is capable to determine the line position. I am pretty much impressed by this sensor, and with the arduino libraries, it is a nice choice for building a speedy line follower.
Then, i needed a powerful microcontroller to make the the error calculations fast as possible. Although not that powerful, i used a 16MHz ATmega168 Arduino clone, and it did not disappoint.
To handle turns and change directions in a snap, i needed a motor driver IC that was capable of effectively braking the motors when the PWM signal went low. For this i used the Pololu TB6612FNG Motor Driver. I wrote a little tutorial on controlling the direction and speed of two bi-directional DC motors using this motor driver here.
For powering up my system, i used a Duracell 9V to supply a stable 9V to my Arduino, and as a temporary choice, i used a 6.0 V Ni-MH battery pack, which i soon plan on replacing with a lightweight Li-Po battery.
And then, i used some jumper wires for connecting respective pins across the robot.
And finally some black electrical tape to make sure everything is in its place. Loose wires spell trouble!! Have been knocked out a couple of times from line following competitions, you don't want to repeat that mistake!! :D
Assembling the chasis is as easy as it gets. The sensors rest at a perfect height from the floor and the motors and wheels are a tight fit. I ordered the parts individually, but more or less, it is pretty much a line following kit. Of course, there are several other mounting options and expansion slots on the acrylic plate, you can hack it to make it something more than just a line follower.
This is what it looks like with the sensors, motors, and wheels mounted on to the plate.
Then i simply mounted the motor driver and the Arduino. ( big deal!!!)
And finally added the battery pack at the back, made the connections accordingly. This is what it looked like at last.
And here is a diagramatic representation of the connections to be made, if you're having some trouble connecting. Click on it for a better view.
And then, the reason why it's working, "DA CODE" !!! :D
PID Based Line Follower Arduino Code
Thanks to Ladvien, one of the few LMR geniuses around here, I came to know about "Source Code Beautifier". I have removed the boring download link to the code and instead, have pasted the code right here, with some colours and added beauty!!
Feel free to roll over the code, and make appropriate changes as per your robot's specifications. An instant transmission to your microcontroller using the magic "copy paste" might not work ;-) . I hope you'll get a fair bit of an idea how line following works using PID. :-)
Although i have mentioned PID, i have just used the Kp and Kd constants in the code. ( So, it's pretty much a PD based line follower). When it comes to line following, it is not mandatory to include Ki in the equation, but you can try and see how it goes
You may notice, the Kp, and Kd values are set to 0 in the code. Now to find these constants it's up to you, what's the fun in not finding it? experiment, and gud luck. Note that the Kd value should be much more bigger than the Kp value.
I intend to upgrade this bot, and soon grow it on my own custom chasis, maybe further fine-tune the PID constants, and of course, increase the speed. Probably also make use of encoders, just to let the robot "foresee" the turns and let it know what the speeds at various points should be. I'll also study some more about PID, and hopefully post some more helpful info on it. Till then, that's all guys!! Thanks for reading :-)