Saturday, June 15, 2013

How to make a simple line following robot





I'll explain how to build a 'Line Following Robot step by step.

                                         
It is better if you have any basic knowledge about electronics,such as reading resister color code,soldiering, familiar with circuit symbols,polarity of capacitors,diodes etc..

If you are not familiar,then use following links to learn about them before read this page

As the first step of line following robot,we have to make the chassis of it.
Following conditions should be considered before making the chassis of the bot.
  1. Weight.
  2. Moment of  inertia.
  3. Center of gravity.
  4. Wheel size.

Since we are planing to make a normal line following robot,we do not need to consider above conditions heavily.
  1. Weight   :-It is better if the robot is light weight.Because it is easy to control when its' weight is low.Also it helps to save energy of battery.
  2. Moment of inertia  :-It is hard to control robot movements when moment of inertia is high.so keep heavy items such as batteries,motors etc as close to robot center as possible.
  3. Center of gravity  :-To accelerate/decelerate robot weight should on its tires.If there is no weight on tires -> no grip.So center of gravity should close to the axle of wheels.C.G. should be low as possible.You can use Lighter or slimmer motors to lower c.g.
  4. Wheel size  :-Bigger wheels higher the c.g.smaller wheels lower the c.g.Keep wheels & shaft as light as possible.Wider wheels are hard to turn.
Lets select motors and wheels,
  • Motor   :-This is a 500rpm geared DC motor.You can use this kind of motor or any kind of 12v DC geared motor.




Now two motors should fix to the robot body.Next question is "What material should use as robot body".This material should light weight.I used perspex sheets(3mm width) as robot body.



For mount two motors to the robot body you can simply use L shaped aluminium bars.You can see them in next photo.




Above mentioned steps are easy steps for you if you have worked with them before.

Next two motors should attach to the robot body using screws. You may have heard "A picture is worth a thousand words" .So you will understand how I have done it after seeing following images.





 Ok,now you have attach two motors to the robot body.There is still one wheel left to attach.It is the caster wheel.Caster wheels are simple wheeled devices that are used with many different types of household and industrial devices. The wheels may be constructed in designs known as compound, undriven, single, and double. Available in a number of sizes, caster wheels are sometimes constructed with heavy-duty plastic or metal components.In here I have used a symple caster wheel which bought from pololu robotics and electronics. 

Sensors should be attach to the body far and away from the motors.If sensors located close to the motors,they will tend to give wrong signals(noise) and very hard to control the robot because motors have no time to respond as microcontroller says.






Up to here I explained how to make the robot body.Now I'm going to give life to it.


How to insert BRAIN to it   

I'm going to use arduino to tell him the path.According to the signal from the sensor panel,arduino manage the direction of motors and speeds of them.I will explain the simplest algorithm to move robot on the line. 

1.There are 8 sensors in pololu sensor panel.Let's divide those sensors into two groups as L and R.   
2.Lets take sensors are going to ON position when it is on the black line or otherwise in OFF position.
3.If the number of ON sensors of left side and right side are equal,well that means the robot is on the middle of           
   the line.So both the motors should go forward.


4.If the number of ON sensors of right side is more than that of left side,well that means the robot must turn right   
  to follow the line furthermore.So right motor should stop and left motor should go forward.


5.I need not to explain what conditions must satisfy to robot do turn left.

How to connect sensor panel and arduino

I used arduino mega 2560.it has 54 digital I/O pins.but we need only 8 pins to connect it with sensor panel.Connect out put pins of sensor panel to input pins of arduino 31,33,35,37,39,41,43,45.


Motor Controller

Motors cannot directly connect to the arduino out put pins.Motor controller driver circuit should add between them.There are so many types of motor controllers in the market.I'm not going to explain how they work in this article.You can gain some knowledge by visiting my previous post on motor controller.Here I'm going to use a mosfet motor controller which I bought from pololu.


How to connect motor controller to Arduino

Power supply circuit

Most of the electronic circuits I used work in 5V regulated power supply.To get reglated voltage from a battery or a tranformer voltage regulators can be use.7805 voltage regulator is use to get regulated 5V. 
We need to build a regulator power supply circuit using 7805, capacitors, connectors,diodes .
Arduino board can be powered in two ways.You can directly power it using 12v battery or 5v regulated power supply.

Following image show my power unit system and motor control unit prepared in a same circuit board.


Battery

You can use any battery 9v to 12v.Battery should be light weight and small.I used 12v lipo battery for this project.The most important thing you must know is never connect the circuits above mentioned into 12v power supply.make sure that all the wirings are correct before power up the circuit.You can buy lipo batteries from ebay.


Now all the hardware part is over,next we have to move on to the programming part.Since we use arduino board, arduino IDE should install into your computer

How to install arduino into your Windows OS has described in this link very well.after installing Arduino IDE  open new sketch to write our line following program.

Code


//Defining sensor inputs as S1,S2.......S8
#define S1 45//rightmost sensor
#define S2 43
#define S3 41
#define S4 39
#define S5 37
#define S6 35
#define S7 33
#define S8 31//leftmost sensor

//motor control pins
int  LMD2=22;    //left motor direction 1(BIN1)
int  LMD1=24;
int  RMD1=26;    //right motor direction 1(AIN1) 
int  RMD2=28;
int L,R=0;

void setup(){
pinMode(LMD1, OUTPUT); //set pin no 22 as a output pin
pinMode(LMD2, OUTPUT);
pinMode(RMD1, OUTPUT);
pinMode(RMD2, OUTPUT); 
pinMode(S1,INPUT);
pinMode(S2,INPUT);
pinMode(S3,INPUT);
pinMode(S4,INPUT);
pinMode(S5,INPUT);
pinMode(S6,INPUT);
pinMode(S7,INPUT);
pinMode(S8,INPUT);


}

void loop(){
  
  if(digitalRead(S1)==HIGH)//find where is the line on
  R++;
  if(digitalRead(S2)==HIGH)
  R++;
  if(digitalRead(S3)==HIGH)
  R++;
  if(digitalRead(S4)==HIGH)
  R++;
  if(digitalRead(S5)==HIGH)
  L++;
  if(digitalRead(S6)==HIGH)
  L++;  
  if(digitalRead(S7)==HIGH)
  L++;  
  if(digitalRead(S8)==HIGH)
  L++;  
  
  if(L<R)//line is on the right side,so must turn right
   {
   digitalWrite(LMD1, LOW);
   digitalWrite(LMD2, HIGH);
   analogWrite(3,200);
   digitalWrite(RMD1, LOW);
   digitalWrite(RMD2, HIGH);
   analogWrite(2,0);
   }
  if(L>R)//line is on the left side,so must turn left
   {
   digitalWrite(LMD1, LOW);
   digitalWrite(LMD2, HIGH);
   analogWrite(3,0);
   digitalWrite(RMD1, LOW);
   digitalWrite(RMD2, HIGH);
   analogWrite(2,200);
   }
   if(L==R)//line is on the middle,so both motors go forward
   {
   digitalWrite(LMD1, LOW);
   digitalWrite(LMD2, HIGH);
   analogWrite(3,200);
   digitalWrite(RMD1, LOW);
   digitalWrite(RMD2, HIGH);
   analogWrite(2,200);
   }
  L=0;
  R=0;//go back new calculation
}
  
compile this code and upload it.then your robot will follow the black line.This is the simplest algorithm to run a robot on a black line.there are so many algorithms such as PID ,FUZZY, etc.. to line following robots.they are very very fast and accurate.But tuning a robot using those algorithms are so hard. 

2 comments: