5.Infrared Line Tracking Car

Description

The car’s line following capability is implemented through three infrared sensors. Each sensor includes an LED indicator that illuminates upon detecting a black line. The car’s movement control system responds to the combined input from these three sensors.

Note

Before running the standalone program, make sure to shut down the main program ccording to the following sections; otherwise, the standalone program will not be able to start. Test Subroutine

Note

If you need to operate the tracking function normally, before running the program, you need to use the black tape included with the kit to paste a path with two tape widths on the ground or a flat surface. This way, the tracking sensor can recognize it properly.

Run program

1. You can jump to the project folder directly by running this command: cd /opt/Code/Pi4/Server or cd /opt/Code/Pi5/Server-pi5

_images/path1.png

2. Run Light.py: sudo python Line_Tracking.py

_images/running1.png

You can press “Ctrl + C” to end the program

Details of the program

 1 import time
 2 from Motor import *
 3 import RPi.GPIO as GPIO
 4
 5 class Line_Tracking:
 6 def __init__(self):
 7     # Define GPIO pins for three infrared sensors
 8     self.IR01 = 14    # Left sensor
 9     self.IR02 = 15    # Middle sensor
10     self.IR03 = 23    # Right sensor
11     # Set GPIO mode and configure pins as inputs
12     GPIO.setmode(GPIO.BCM)
13     GPIO.setup(self.IR01,GPIO.IN)
14     GPIO.setup(self.IR02,GPIO.IN)
15     GPIO.setup(self.IR03,GPIO.IN)
16
17 def run(self):
18     while True:
19         # Initialize sensor state variable
20         self.LMR=0x00
21         # Read sensors and update state using bitwise operations
22         if GPIO.input(self.IR01)==True:    # If left sensor detects line
23             self.LMR=(self.LMR | 4)        # Set bit 2 (binary 100)
24         if GPIO.input(self.IR02)==True:    # If middle sensor detects line
25             self.LMR=(self.LMR | 2)        # Set bit 1 (binary 010)
26         if GPIO.input(self.IR03)==True:    # If right sensor detects line
27             self.LMR=(self.LMR | 1)        # Set bit 0 (binary 001)
28
29         # Control car movement based on sensor states
30         if self.LMR==2:      # Only middle sensor detects line (010)
31             PWM.setMotorModel(1000,1000,1000,1000)    # Move forward
32         elif self.LMR==6:    # Left and middle sensors detect line (110)
33             PWM.setMotorModel(-1100,-1100,1100,1100)  # Turn left slightly
34         elif self.LMR==4:    # Only left sensor detects line (100)
35             PWM.setMotorModel(-1300,-1300,1300,1300)  # Turn left sharply
36         elif self.LMR==3:    # Middle and right sensors detect line (011)
37             PWM.setMotorModel(1100,1100,-1100,-1100)  # Turn right slightly
38         elif self.LMR==1:    # Only right sensor detects line (001)
39             PWM.setMotorModel(1300,1300,-1300,-1300)  # Turn right sharply
40         elif self.LMR==0:    # No sensor detects line (000)
41             PWM.setMotorModel(0,0,0,0)     # Stop
42         elif self.LMR==7:    # All sensors detect line (111)
43             PWM.setMotorModel(0,0,0,0)     # Stop
44
45 # Create instance of Line_Tracking class
46 infrared=Line_Tracking()
47
48 # Main program logic follows:
49 if __name__ == '__main__':
50 print ('Program is starting ... ')
51 try:
52     infrared.run()    # Start line tracking
53 except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, stop the car
54     PWM.setMotorModel(0,0,0,0)
55 except Exception as e:    # Handle other exceptions
56     print(f'An error occurred: {e}')
57 finally:    # Ensure motor stops in any case
58     PWM.setMotorModel(0,0,0,0)
59     print('Motor model has been set to stop state.')

Result Analysis:

The line tracking car uses three infrared sensors to detect black lines and performs different movements based on different sensor combinations:

  1. When middle sensor detects line (LMR = 2): - Indicates car is correctly centered on the line - The car moves forward straight (setMotorModel(1000, 1000, 1000, 1000))

  2. When left sensors detect line (LMR = 4 or 6): - LMR = 4: Only left sensor detects, indicating significant rightward drift - The car makes a sharp left turn (setMotorModel(-1300, -1300, 1300, 1300)) - LMR = 6: Left and middle sensors detect, indicating slight rightward drift - The car makes a gentle left turn (setMotorModel(-1100, -1100, 1100, 1100))

  3. When right sensors detect line (LMR = 1 or 3): - LMR = 1: Only right sensor detects, indicating significant leftward drift - The car makes a sharp right turn (setMotorModel(1300, 1300, -1300, -1300)) - LMR = 3: Right and middle sensors detect, indicating slight leftward drift - The car makes a gentle right turn (setMotorModel(1100, 1100, -1100, -1100))

  4. When no line is detected (LMR = 0) or all sensors detect line (LMR = 7): - Indicates either line is lost or car is at intersection - The car stops (setMotorModel(0, 0, 0, 0))

Through this control strategy, the car can: - Maintain accurate line following - Make appropriate adjustments based on drift direction - Vary turning intensity based on deviation amount - Handle different line following scenarios effectively - Stop safely when line is lost or at intersections

_images/line.gif

Need Help?

tech_edu_service@outlook.com