Implementation of line following logic based on sensor data

In this task, we will implement a simple line following technique using the infrared sensor. We will make use of a pair of infrared sensors to track a black line on a white surface. The robot will move forward if both the sensors are on a white surface. The robot turns left if the left sensor is on the black line and vice versa.

Prepare for lift off

The sensor needs to be soldered and connected to the Raspberry Pi (something like the one shown in the preceding schematic). Alternatively, you may use a sensor of your choice.

Engage thrusters

  1. As always, we will get started by importing the required modules, especially Rpi.GPIO:
    import RPi.GPIO as GPIO
    from time import sleep
  2. We will set the pin configuration that we will use in this program:
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18,GPIO.IN)
    GPIO.setup(25,GPIO.IN)
  3. The control logic explained earlier is implemented as follows:
    state = 1
    prev_state = 0
    while True:
      #both sensors are on white surface
      if ((GPIO.input(18)==GPIO.HIGH) and (GPIO.input(25)==GPIO.HIGH)):
        state = 0
      #left sensor alone is on the black surface
      elif ((GPIO.input(18)==GPIO.LOW) and (GPIO.input(25)==GPIO.HIGH)):
        state = 1
      #right sensor alone is on the black surface
      elif ((GPIO.input(18)==GPIO.HIGH) and (GPIO.input(25)==GPIO.LOW)):
        state = 2
      #if sensor state has changed since last time, update motor control
      if state != prev_state:
        if state == 0:
          #move robot forward
        elif state == 1:
          #turn robot left
        elif state == 2:
          #turn robot right
        prev_state = state
    
      sleep(0.15)

    The sensor states are checked once every 15 milliseconds. If there is a change of sensor states, the motor control is updated (as per the logic explained in this task).

  4. Since we are using only two sensors, the robot runs crisscross across the line. In order to achieve smooth tracking of the line, a sensor array is used for driving the motors using a control algorithm such as a PID control algorithm.

Objective complete – mini debriefing

In this task, we implemented the line following logic for our robot. In the next task, we will discuss motor control to drive the robot This would be eventually integrated into the line following logic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.193.108