-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow_line.py
95 lines (70 loc) · 2.43 KB
/
follow_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import curses
import time
import RPi.GPIO as GPIO
MOTOR_RIGHT_IN1 = 31
MOTOR_RIGHT_IN2 = 29
MOTOR_RIGHT_ENABLE = 33
SENSOR_RIGHT = 37
MOTOR_LEFT_IN1 = 38
MOTOR_LEFT_IN2 = 40
MOTOR_LEFT_ENABLE = 32
SENSOR_LEFT = 36
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MOTOR_RIGHT_IN1, GPIO.OUT)
GPIO.setup(MOTOR_RIGHT_IN2, GPIO.OUT)
GPIO.setup(MOTOR_RIGHT_ENABLE, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_IN1, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_IN2, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_ENABLE, GPIO.OUT)
GPIO.setup(SENSOR_RIGHT, GPIO.IN)
GPIO.setup(SENSOR_LEFT, GPIO.IN)
PWM_RIGHT_ENABLE = GPIO.PWM(MOTOR_RIGHT_ENABLE, 1000) # 1Khz
PWM_RIGHT_ENABLE.start(0)
PWM_LEFT_ENABLE = GPIO.PWM(MOTOR_LEFT_ENABLE, 1000) # 1Khz
PWM_LEFT_ENABLE.start(0)
GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
PWM_RIGHT_ENABLE.ChangeDutyCycle(0)
GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
PWM_LEFT_ENABLE.ChangeDutyCycle(0)
PWM_DUTY_CYCLE_MINIMUM_PERCENT = 50
PWM_DUTY_CYCLE_MAXIMUM_PERCENT = 100
PWM_DUTY_CYCLE_PERCENT = 30
def left_motor_forward():
GPIO.output(MOTOR_LEFT_IN1, GPIO.HIGH)
GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
PWM_LEFT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)
def left_motor_stop():
GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
PWM_LEFT_ENABLE.ChangeDutyCycle(0)
def left_motor_backward():
GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
GPIO.output(MOTOR_LEFT_IN2, GPIO.HIGH)
PWM_LEFT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)
def right_motor_forward():
GPIO.output(MOTOR_RIGHT_IN1, GPIO.HIGH)
GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
PWM_RIGHT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)
def right_motor_stop():
GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
PWM_RIGHT_ENABLE.ChangeDutyCycle(0)
def right_motor_backward():
GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
GPIO.output(MOTOR_RIGHT_IN2, GPIO.HIGH)
PWM_RIGHT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)
try:
while True:
is_right_on_line = GPIO.input(SENSOR_RIGHT)
is_left_on_line = GPIO.input(SENSOR_LEFT)
if (is_right_on_line and is_left_on_line) or (not is_right_on_line and not is_left_on_line):
right_motor_forward()
left_motor_forward()
elif is_right_on_line:
right_motor_stop()
elif is_left_on_line:
left_motor_stop()
except:
GPIO.cleanup()