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
As always, we will get started by importing the required modules, especially
Rpi.GPIO
:import RPi.GPIO as GPIO from time import sleep
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)
The control logic explained earlier is implemented as follows:
state = 1 prev_state = 0 while True: #both sensors...