Tuesday 11 March 2014

3 sensor Line follower Arduino code(without PID) but just like PID

Line follower bot is basically a robot that follows a black line. If you have already made a simple line follower robot and looking forward to improve its performance than you are at the right place. To make an improvement to the performance of anything feedback is the best choice. In this post, a new method of using feedback is introduced which you can use to improve your line follower bot.
 
You will find most people use PID feedback to improve the performance of a system. PID controller has its own challenges. It is very hard to code and tune.  But here we are looking for something more simple and easy. The method discussed in this post is best suited with at least three sensors but you can try it two sensors but just little adjustments in code.
Let us start discussing the method. The basic idea is to memorize the current readings of the line follower bot at the end of the loop. In the next loop, If the bot finds itself out of line it trusts on reading taken in the previous loop. So our line follower bot becomes more intelligent by feedback and can deal with such a situation by taking suitable actions.

Example of three sensor bot: Let's say there are three sensors giving binary readings. Here 0 means the sensor is on the line and 1 means sensor is not on the line.

The following actions are taken according to sensors reading:
* Go forward if current readings of sensors are 101.
* Go right if current readings of sensors are 110.
* Go left if current readings of sensors are 011.
* Go right if current readings of sensors are 111 and previous are 110.
* Go left if current readings of sensors are 111 and previous are 011.

Since we are using readings taken only one step before we can call it first-order feedback. You can also use the readings from the last 2 readings. In that case, the complexity of the algorithm will go high.


//The code is

int rightmotor1=12;
int leftmotor1=10;
int rightmotor2=13;
int leftmotor2=11;


int sensorleft=14;
int sensorcenter=15;
int sensorright=16;


int l1=1;
int l2=1;
int l3=1;

int f1=0;
int f2=0;
int f3=0;

//////////////////////////////////////

void setup()
{

 pinMode(rightmotor1,OUTPUT);
 pinMode(leftmotor1,OUTPUT);
 pinMode(rightmotor2,OUTPUT);
 pinMode(leftmotor2,OUTPUT);

 pinMode(sensorleft,INPUT);
 pinMode(sensorcenter,INPUT);
 pinMode(sensorright,INPUT);

}
///////////////////////////////////////////////////

void loop()
{
l1=digitalRead(sensorleft);
l2=digitalRead(sensorcenter);
l3=digitalRead(sensorright);

/////////////////////////////////////////////////////////////////////
if(l1==1&&l2==1&&l3==1)   //feedback when comes all sensor on white
{
l1=f1;
l2=f2;
l3=f3;
}

////////////////////////////////////////////////////////////////////////////
if(l1==0&&l2==1&&l3==1||l1==0&&l2==0&&l3==1)   //left turn
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==1&&l3==0||l1==1&&l2==0&&l3==0)  //right turn
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==0&&l3==1)  //go forward straight
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==0&&l2==0&&l3==0) //stop
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}

///////////////////////////////////////////////////////////////////
f1=l1;
f2=l2;
f3=l3;        //memory variables
////////////////////////////////////////////////////////////////////
}




Here f1, f2, and f3 are our feedback variable or we can say memory variables.
This is a simple code you can change the code according to our needs.

This idea is also possible with two sensors but you will see the bot oscillating on the line. This idea best works with three sensors.
Check out this post for two sensor version. https://crackeconcept.blogspot.com/2020/01/two-sensor-code-for-line-follower-using.html
You can see the video is given below in which line follower works on the same algorithm but with four sensors. That is how a three IR sensor line follower will also work.
This line follower robot is also available in the Virtual line follower app given link below. Try yourself and see its performance there.
Hope this post will help you.
 /////////////////////////////////////////////////////////////////////////////////////
My free android apps.
https://play.google.com/store/apps/developer?id=Gaur+Tavm
Thank you.




17 comments:

  1. how we use this as 5 sensors
    any help

    ReplyDelete
    Replies
    1. 5 sensors will be useful for specific track according to which the conditions will be filled but it will get very complex.

      Delete
  2. why are there are only 2 motors then why is there 4 motor outputs?

    ReplyDelete
    Replies
    1. because program is written for both clockwise and anti-clockwise rotation of motor without PWM control.

      Delete
  3. is that work if i just connect the battery or it need application to work?

    ReplyDelete
  4. when ir sensor gives high and low? on black and white?

    ReplyDelete
    Replies
    1. for white its low
      for black its high

      Delete
    2. sorry according to this program -
      low for black
      high for white

      Delete
  5. what the value we have to give for anti clockwise rotation of motor

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. can you explain more the feedback system please

    ReplyDelete
  8. Connection diagram for this 3 sensor line follower project ?

    ReplyDelete
  9. can you help me in waiter line follower code?

    ReplyDelete
  10. What motors did you use? They seem to be going slow. I need such motors.

    ReplyDelete
    Replies
    1. Bo motor with gear. Gear makes the movement slow.

      Delete
  11. How to take feedback from last 2 readings

    ReplyDelete