Saturday, January 30, 2016

Line Follower | PID Algorithm | Arduino Code

 I've been lately working on making a line follower that can run on any track be it black line on white background or vice versa and recently won a line follower event at my college. Some people asked me for the code and hence writing a post on the same. There are obviously minor adjustments to made and other tweaks but the code is perfectly running and the PID tuning done after long hours of trial and testing. Here is the list of hardware and software used.

NOTE that the PID constants that I have tuned for my bot may or may not work for your bot so if you're going to use the code then make sure you tune the constants properly.


1. Line Sensor Array

I have used a line sensor array consisting of 7 IR leds. The reason for using such an array is that sharper turns can be detected easily using the 7 sensors and overshooting of the bot from the path gets reduced. One can also use just two led's on either side of the line but it'll work only for acute turns and not obtuse or right angled turns. The thing with line follower is that even if you tune it with  perfect constants it'll still wobble a bit while running, so the 7 IR's sort help in reducing oscillations also.

One important thing I experienced is that DO NOT place the motors and the sensor array nearer to each other. There should be enough distance between the motors and the sensor placing. Hence, I placed it in the front and the motor at the back. The reason behind this is that this will give enough amount of time for motors to react as the sensors detect sharp turns.

2. L298 and 200 rpm motors

As I have explained in the previous post the l293d is an inefficient driver when it comes to handling large currents and moreover there's considerable amount of voltage drop at the PNP and NPN transistors inside. Check it here

There's no specific reason as to why I chose 200 rpm motors. They were just lying around in my room so I used them, although increasing the rpm may increase the difficulty in making it perfectly follow the line.

3. Generic wheels and Chassis

4. 11.1 v Li-po battery

5. Arduino Uno

Basic sensing:-

There's no specific reason as to why I chose PID as a control strategy. I found this through other similar bots that this is one of the best strategies to efficiently follow the line. Before I get into PID this is how I assigned values to the sensor readings.

i) The mean position sensor is assigned an integer value of error=20.
ii) Same way the other six sensor are calibrated for values from 35(rightmost sensor) to 5.

There final error generated is,


PID Algorithm:-

1.Proportional Term,Kp :- 
Proportional term generates output according to the final error generated in previously. But the problem is when the bot is at the Center IR the output is zero.


2.Derivative Term,Kd:-
Derivative term dampens the system meaning it'll lessen the oscillations or wobbling occurring while it is following the path. Again this term wont help when final_error will be zero.



3. Integral Term,Ki:-
Integral term does what the the other two terms can't do, it integrates the error over time when the error is zero.(Previous values aren't zero).It's nothing but summing of all values.


Final output is ,


This output is fed accordingly to the left or right motor respectively through PWM.

PID Tuning:-

This is the most cumbersome and time-consuming part of the whole process of making a Line-follower. There's no strict method to tune it. However, here are few steps i found on the net:

i) Make all the constants 0 and start with the Kp term randomly and see how he bot behaves. If the bot overshoots the path then increase Kp or if it's too oscillatory then decrease Kp.

ii) Next come over to Kd, and make it such that the wobbling is lowered and bot turns perfectly.

iii) Though I have not been able to master this term but this term is to make the go fast on a straight track as the error is zero on a straight track and the bot accelerates quickly.

See for the bot_code.ino file here:

Please do subscribe to posts by mail.


Tuesday, January 19, 2016

Arduino Cookbook By Jeremy Blum | Subscribe by Email to get the book!

Here's an awesome Arduino cookbook by one of the best in the business- Jeremy Blum! The book contains lots of projects and basic interfacing like LCD's, RTC's and many more! I have been using this book for a year and a half now and it contains basics as well as harder interfacing. If you're new to Arduino or want to learn about the Arduino's peripheral interfacings, then this is the Book!

To claim the book subscribe to the posts via email. To subscribe, type in your mail in the email box on the right of this page. After subscribing i'll send you the google drive link.

Friday, January 15, 2016

OpenCV setup on Raspberry Pi 2

Hello! In this post, i'll be explaining on how to setup the python library for OpenCV. OpenCV is a openware library for image processing on various platforms like python. I want to mention that there's an excellent book on Raspi image processing by Ashwin Pajankar and that i highly recommend that you go through it. Here's the link to buy the book:
Book Link

1. Before we start let me first tell you that this installation can take a considerable amount of time, in       some cases 4-5 hours depending on the internet speed you have. First make sure that the Raspi is       connected to the internet. If you're using a LAN cable then make sure that its connected to the             internet via the router or modem.
2.Run the following command on the terminal
   sudo service networking restart
3.Run the following commands in sequence to update and list the raspi and the rasbian                           software. apt stands for Advanced Utility Package for updating pacakges on the raspi
       sudo apt-get update

       sudo apt-get upgrade

   sudo rpi-update

4. Now you need to install the packages in the following by typing, 
   sudo apt-get install <package> where <package> is one of the following:

Now, that you've completed the setup your Raspi is ready for the Image processing. Maybe next week i'll come up with the final Ball tracker setup.


Simple Ball Tracker Bot | Part 1|Hardware and Software requirements

Hello readers. This is going to be my first blog post in three years and finally I've got enough material on projects I've worked on, will be working on, and what not but have got a considerable lot in my bag to turn them into blog posts. Hopefully I'll post updates as soon as possible.

Coming straight to the topic, The ball tracking bot is a simple image processing based bot that tracks a ball of a certain color(can be changed by changing the HSV values embedded with a tilt camera(with a servo) and follows the ball. It is going to be based on OpenCV. I chose this as my first topic as i wanted to start the blog with relatively easier topics than that would soon follow up. Image processing is a wide area and I've just started to learn image processing and only recently came up with an idea of such a bot (Obviously from getting inspired from similar bots on YouTube). So lets gets straight into it and look into the hardware and software requirements:

I. Hardware :

1) Raspberry Pi 2 B+

The reason for this being in the system is obvious, that is, that it'll be handling the image processing part of the proposed system. But i chose Raspi over other PC or computer based processing was that it'll make the bot mobile, with the capability of online editing of codes while running the bot. We're going to use the OpenCV library for python and we'll need python 2.7 or later versions. I'm not well versed with python, im still learning it. The OS i've installed is the Rasbian,

2) Slave Microcontroller  ( Arduino or any microcontroller that you are well versed with)

We'll be making decisions on the raspi for the tracking the ball but we'll send these decisions to execute to slave a microcontroller to move the servo and drive the motors for the chassis.

3) 9 gm Servo

Preferably a 9gm servo, i'm going to use as i think this is going to be enough to move a small USB camera. One thing important to note is that Servos require some amount of peak current and generally that amount for a 9 gm servo would be 200-300 mA so a compatible battery needs to be employed. Additionally i have put a pan tilt mechanism and mounted the camera on top and servo on the side.
4)USB Webcam/Camera.

A USB Webcam is i guess enough for the simple purpose of just tracking the ball, though if complex or higher end image processing is to be required then you need to use the Raspi Cam. The reason being that USB communication eats enough of the RAM/Processing Power but on the other hand the Raspi cam is directly connected to the dedicated GPU which makes the system available to compute the other tasks. Have a look at this:

5)DC Motors and l298D Driver

Normal 200 rpm dc motors i am going to use and the driver for it is going to be the l298d.

Note:- l293d will not be sufficient enough for this type of bot as explained here

6)Power supply 

For the raspi i'll be putting a seperate 1000 mAh battery pack and for the the uC, Servo and motor, i'll be using 1000 mAh Li-Po battery.

IMPORTANT: If you're going to use a uC other than Arduino, its better to use a USB to serial converter module due to different logic levels in raspi and uC pins. Like this one:


1) Python 2.7 on Raspi 

2)OpenCV library for python

optional :Arduino IDE on Raspi

Bot Chassis (Haven't made any connections yet)

Just get to the terminal window and type the following:

 sudo apt-get install arduino

I'll be coming up with another blog post on the how to setup the Raspi for OpenCV.


Subscribe my blog to keep a track on future posts!