Programming Line Follower Raspberry Pi Robot with Python – Part 2

coding

We are in part 3 of tutorial series of Building Raspberry Pi Line Follower Robot! If you want to jump back and forth or missed any of my previous tutorials – here is the consolidated list.

All Tutorials to build line follower Pi Robot:

  1. Build Line follower using Raspberry Pi
  2. Adding IR Sensor Module to Raspberry Pi Robot
  3. Programming Line Follower Raspberry Pi Robot with Python
  4. Final Line Follower Raspberry Pi robot in Action

If you want to see all my Raspberry Pi Robotics tutorials do visit this page – http://www.vishalavalani.com/robotics-tutorials/

Its programming time and giving brain to the Robot! Hope you have assembled all the components i.e. IR Sensor module to Raspberry Pi. Here is the consolidated connection diagram if you have missed the tutorial:

Declaring Needed Variables and Initialization

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

GPIO.setup(31, GPIO.IN)  # Left Line Sensor Reading
GPIO.setup(32, GPIO.IN)  # Right Line Sensor Reading

Creating Functions

Best practice is to convert repetitive code into function. Here, we have following below functions with their own onus:

  • GetSensorReadings– Read from the sensor
  • Forward – Go forward
  • Reverse – Go reverse
  • Right – Turn right
  • Left – Turn left
  • Brake – Stop the robot

def GetSensorReadings():
    print "Taking sensor readings"
    leftlinesensor = GPIO.input(31)
    rightlinesensor = GPIO.input(32)
    print leftlinesensor
    print rightlinesensor
    return leftlinesensor, rightlinesensor


def Forward():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 1)
    GPIO.output(16, 0)


def Reverse():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 0)
    GPIO.output(16, 1)


def Right():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 0)
    GPIO.output(16, 1)


def Left():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 0)


def Brake():
    GPIO.output(11, 1)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 1)

Master Logic

First we will create infinite loop and read the sensor readings:

rightlinesensor, leftlinesensor = GetSensorReadings()

Now we will detect if there is any line or not and rotate as needed. Here is the algorithm:

  • If left sensor detects a black line we have to move the robot right
  • If right sensor detects a black line we have to move the robot left
  • else move forward

Now, by doing so the robot is running on DC motor and moves very fast and there are changes that robot goes off track very quickly. Hence, I have applied a break if sensor is detected so that it slows down the robot a bit. Here is the code for that:

    rightlinesensor, leftlinesensor = GetSensorReadings()
        if leftlinesensor == 1 and rightlinesensor == 0:
            Brake()
            time.sleep(0.15)
            Right()
            time.sleep(0.15)
        elif rightlinesensor == 1 and leftlinesensor == 0:
            Brake()
            time.sleep(0.15)
            Left()
            time.sleep(0.15)
        else:
            Forward()

Putting it all together

 Here is entire code that you can use:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

GPIO.setup(31, GPIO.IN)  # Left Line Sensor Reading
GPIO.setup(32, GPIO.IN)  # Right Line Sensor Reading

print "Waiting For Sensor To Settle"
time.sleep(2)



def GetSensorReadings():
    print "Taking sensor readings"
    leftlinesensor = GPIO.input(31)
    rightlinesensor = GPIO.input(32)
    print leftlinesensor
    print rightlinesensor
    return leftlinesensor, rightlinesensor


def Forward():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 1)
    GPIO.output(16, 0)


def Reverse():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 0)
    GPIO.output(16, 1)


def Right():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 0)
    GPIO.output(16, 1)


def Left():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 0)


def Brake():
    GPIO.output(11, 1)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 1)


try:
    while (True):
        rightlinesensor, leftlinesensor = GetSensorReadings()
        if leftlinesensor == 1 and rightlinesensor == 0:
            Brake()
            time.sleep(0.15)
            Right()
            time.sleep(0.15)
        elif rightlinesensor == 1 and leftlinesensor == 0:
            Brake()
            time.sleep(0.15)
            Left()
            time.sleep(0.15)
        else:
            Forward()

finally:
    Brake()
    print("Cleaning Up!")
    GPIO.cleanup()

Testing and final outcome

Enjoy building Line follower 🙂

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *