Tuesday, July 24, 2012

Line Following Robot


This posts details the process of building a line following robot. The robot can follow both black and white lines. It uses a sensor array to sense the line and a PID control to follow the line. With the help of a PID control the robot does not follow the line by oscillating to the left and to the right like conventional line followers but follows the line smoothly without wavering too much the left and right, conserving battery power while at the same time following the line much more faster.


Here is a video of the robot
Specifications and features:
  • The robot can follow both black and white lines. 
  • It has uses an 6 sensor array to follow the line. 
  • It is controlled by an Arduno1280 running at 20 MHz. 
  • The program is written in Arduno. 
  • It is powered by 10 Ni-Mh batteries connected in series, giving around 12 V. 
  • It uses PID control to follow the line.
Structure:
For the perfect finishing we used rubber wheels for our robot. By using the lathe machine we made wheels and mounted. As a third wheel we used a caster ball. The structure of robot has been made by using aluminium.
Structure of the robot
Arduino mega (AT mega 1280):
Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. 
The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.
Arduino Mega Controller
7805 Voltage Regulator:
The best way to ensure accurate +5v is to use a 7805 voltage regulator. This three leg IC will take a range of voltages and output clean +5v. Eventually it could run from 12 volt.
The regulator also requires two capacitors on either side. The rating of these caps varies depending on the job. These caps are generally applied to smooth out power fluctuations. They act like a hammer arrestor in plumbing. When the current goes up, they suck it in and store it, when it goes down they spit it out again. This happens fast smoothing the power.
7805 voltage regulator
L298 Motor Controller IC:
L298 is working according to H-bridge method. These circuits are often used in robotics to allow DC motors to run forwards and backwards. There are 2 PWM connections can be used to control the speed of motors and there are 4 connections to the arduino and 4 connections to the motors. Following figure shows the connection type of L298 motor driver. The thing is externally 5 V is required to run the L298 chip, and that voltage can be supply by using 7805 voltage regulator.
Block Diagram of L298
L298N motor control chip incorporates two H-bridge motor-driving circuits into a single 15-pin package. Above figure shows a block diagram of this useful integrated circuit.
The schematic of the motor circuit shows how the L298N chips are controlled the Robot. Six bits are used to control two motors. Four of the bits determine the direction of the motors and two bits determine when the motors are on or off. The speed of a motor may be controlled by pulsing the enable bit of its associated controller chip on and off. This technique is called pulse width modulation.
Motor controller:
Motor controller is a very valuable circuit when considering a Line Following Robot which can use to control 2 motors according to the sensor input value which are connecting through the arduino board. Motor controller circuit has various components; those are L298 motor control IC, 7805 voltage regulator, capacitors, resistors and connectors with connecting wires.
PCB layout of motor controller circuit
Hardware configuration of motor controller circuit
Sensor array:
LDR sensor connection diagram 
The maze follower robot basically will follow the line that has different brightness. For example, the line is white and background is black. It is difficult to mechanical device to detect lines. The suitable way is to use the LDR sensor as a detector. It can detect the light that reflect on the bright surface and transfer the arduino board as the input.
Light dependent resistor (LDR) sensor is a resistor that sensitive lights. The value of resistance is proportional to the light received. If the resistor in shade condition, the resistance is too high but if in the room light, the resistance is too low. The resistance can drop dramatically when illuminated.
In our case we used touch LED for lightening purpose. The current which are going through LED has been limited by using resisters as shown in figure. We used 50KΩ variable resisters to adjust sensor read.
We made another program to adjust the sensors and it has been mentioned under algorithm title. When we are adjusting the resistors according to the program the serial monitor of arduino was very useful.  We were adjusted the resistors according to the serial monitor value, 1 for white surface and 0 for black surface.
PCB layout of sensor array
Gear motors:
Gear motor
Machine component consisting of a toothed wheel attached to a rotating shaft. Gears operate in pairs, the teeth of one engaging the teeth of a second, to transmit and modify rotary motion and torque. To transmit motion smoothly, the contacting surfaces of gear teeth must be carefully shaped to a specific profile. The smallest of a gear pair is often known as the pinion. If the pinion is on the driving shaft, the pair acts to reduce speed and to amplify torque; if the pinion is on the drive shaft, the pair acts to increase speed and reduce torque.
Algorithm:
The algorithm is the one thing that determines the performance of your robot more than anything else. The most basic algorithm I know is the one which uses only one sensor. The sensor is placed in a position that is a little off centered to one of the sides, say right. When the sensor is detects no line the robot moves to the left and when the sensor detects the line the robot moves to the right. A robot with this algorithm would follow the line like shown in the picture below
 
The drawback of this method is that the line following is not smooth. The robot keeps wavering left and right on the track, wasting battery power and time. A modification to this method is to add sensors on both sides of the robot and place them such that they just sense the line on either side. And the algorithm would be to move forward if both the sensors sense the line or to move left if only the left sensor senses the line and move right if only the right sensor senses the line. A robot with this algorithm would follow the line like shown in the picture below
 PID control
PID stands for Proportional Integral and Derivative. It is a popular control loop feedback control extensively used in industrial controls systems. But why would one need a PID controller for a line following robot, when there are many simpler algorithms already available for line following. Lets us see...
A conventional robot would follow a line as shown below(red line shows the path taken by the robot)
As you can see clearly in the picture the robot oscillates a lot about the line, wasting valuable time and battery power. By now you must have realized that there is a maximum speed beyond which you cannot use this algorithm, other wise the robot will overshoot the line.
A robot with PID control will follow the line as shown below
As you can see clearly the robot will follow the line smoothly keeping its centre always above the line. In straight lines, the robot will gradually stabiles go straight unlike a robot with the left-right algorithm. This enables the robot to follow the line faster and more efficiently.
Before I tell you how to implement a PID algorithm for a line following robot, let me tell you the terms associated with it –
Target – It is the position you want the line follower to always be(or try to be),that is, the center of the robot.
Current Position – It is the current position of the robot with respect to the line.
Error - It is the difference between the current position and the target. It can be negative, positive or zero.
Proportional – It tells us how far the robot is from the line like – to the right , to the extreme right, to the left or a little to the left. Proportional is the fundamental term used to calculate the other two.
Integral – It gives the accumulated error over time. It tells us if the robot has been on the line in the last few moments or not. 
Derivative – It is the rate at which the robot oscillates to the left and right about the line.
Kp, Ki and Kd are the constants used to vary the effect of Proportional, Integral and Derivative terms respectively.
What the controller does is first calculate the current position. Then calculate the error based on the current position. It will then command the motors to take a hard turn, if the error is high or a small turn if the error is low. Basically, the magnitude of the turn taken will be proportional to the error. This is a result of the Proportional control. Even after this, if the error does not decrease or decreases slowly, the controller will then increase the magnitude of the turn further and further over time till the robot centers over the line. This is a result of the Integral control. In the process of centering over the line the robot may overshoot the target position and move to the other side of the line where the above process is followed again. Thus the robot may keep oscillating about the line in order to center over the line. To reduce the oscillating effect over time the Derivative control is used.
Implementing PID control for a line following robot
The first requirement in implementing PID for a line follower is calculating the error of the robot. To calculate the error we will need to know the current position of the robot with respect to the line. There are a number of ways of knowing this.
A simple approach would be to place two IR sensors on either side of the line. The IR sensors should be tuned to give an output voltage that is promotional to the distance between the line and the sensor. The output can then be connected to the ADC pin of a microcontroller and the error can be calculated.
Though this method may seem simple and easy to implement it has a few drawbacks. Firstly, a robot using this method will have problems following a line whose width is varying. Secondly, the output of these sensors will be highly susceptible to interference from external light. And lastly, for this method to work, you have to ensure that the track is completely flat. Any change in the distance between the sensor and the surface will affect the sensor readings.
A better approach would be to use the traditional sensor array. Using an array of sensors, you can easily calculate the error by knowing which sensor is on the line. Consider you have an array of 10 sensors each placed 1cm apart. When the 7th sensor from the left detects the line you can be sure that the centre of the robot is 2 cm to the right of the line. Using a sensor array the processor can calculate the error faster as there is no ADC required to be done and thus you can increase the sampling rate. You can also use the robot as a grid solver.
When building a sensor array, there are a few things to keep in mind. Firstly, the more sensors you have in an array, the better will be the performance of the robot, because more sensors translates to more range of error. For line following anything between 6 to 10 sensors (depending on your robot) will be sufficient. The distance between each adjacent sensor determines the resolution of the readings. It is always best to place the sensors as close to each other as possible because your resolution will increase as you place the sensors closer (a resolution of +/- 1cm is always better than a resolution of +/- 3 cm).
The next important thing in implementing PID after the sensors is the writing the code itself. The algorithm for a PID control for line followers would be something like this –
Error = target_pos – current_pos //calculate error
P = Error * Kp //error times proportional constant gives P
I = I + Error //integral stores the accumulated error
I = I * Ki //calculates the integral value
D = Error – Previos_error //stores change in error to derivate
Correction = P + I + D  Tuning the PID control
This is the most interesting part in building a PID control. In this step one should tune the Kp, Ki and Kd values to get the best results. I cannot give you the values because what works for me will not work for you. The optimum Kp, Ki and Kd values vary a lot from robot to robot. And the best way to determine the optimum values is by trial and error.
First, set all values to 0 and start with tuning the Kp value. First time I just gave an approximate value. Seeing the robot perform will determine what you should do next. If the robot wobbles a lot reduce the Kp value, if the doesn't follow the line (goes straight in curves) increase the Kp value. Tune the Kp value till the robot smoothly follows the line. By now you will have observed that the robot goes with no acceleration on straight lines. Now, tune the Kd term. After tuning the Kd term move to the Ki term. After which, you will see the robot first center over a straight line and then accelerate also.
Note: The optimum Kp, Ki and Kd values vary a lot even from track to track. You can only know these optimum values by testing.

1 comment:

  1. Ma blog aka nimnaya blog reader akata athu karala oyga blog balanna tava katiya ganna gana puluvn http://nimnaya.info/signup/

    blog kiyvanna anna nimnayata http://nimnaya.info/

    ReplyDelete