-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpicartest1.py
129 lines (110 loc) · 3.34 KB
/
picartest1.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# ___ ___ ___ _ _ ___ ___ ____ ___ ____
# / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
#| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
# \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
# (____/
# Osoyoo Model-Pi L298N DC motor driver programming guide
# tutorial url: https://osoyoo.com/2020/03/01/python-programming-tutorial-model-pi-l298n-motor-driver-for-raspberry-pi/
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # GPIO number in BCM mode
GPIO.setwarnings(False)
#define L298N(Model-Pi motor drive board) GPIO pins
IN1 = 23 #Right motor direction pin
IN2 = 24 #Right motor direction pin
ENA = 18 #Right motor speed pin
IN3 = 27 #Left motor direction pin
IN4 = 22 #Left motor direction pin
ENB = 12 #Left motor speed pin
sensor1= 25 # No.1 sensor from right
sensor2= 9 # No.2 sensor from right
sensor3= 11 # middle sensor
sensor4= 8 # No.2 sensor from left
sensor5= 7 #No.1 sensor from left
sts1=0
sts2=0
sts3=0
sts4=0
sts5=0
# Define motor control pins as output
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)
GPIO.setup(ENB, GPIO.OUT)
GPIO.setup(sensor1, GPIO.IN)
GPIO.setup(sensor2, GPIO.IN)
GPIO.setup(sensor3, GPIO.IN)
GPIO.setup(sensor4, GPIO.IN)
GPIO.setup(sensor5, GPIO.IN)
pwm_Left = GPIO.PWM(ENB, 1000) #set Left motor PWM frequency @1000 hz
pwm_Left.start(25) # initial Left motor PWM power @25% of maximum value
pwm_Right = GPIO.PWM(ENA, 1000) #set Right motor PWM frequency @1000 hz
pwm_Right.start(25) # initial Right motor PWM power @25% of maximum value
def changespeed(speed):
pwm_Left.ChangeDutyCycle(speed)
pwm_Right.ChangeDutyCycle(speed)
def stopcar():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
stopcar()
def forward():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_Left.ChangeDutyCycle(40)
pwm_Right.ChangeDutyCycle(40)
#following two lines can be removed if you want car make continuous movement without pause
#time.sleep(0.25)
#stopcar()
def backward():
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
pwm_Left.ChangeDutyCycle(40)
pwm_Right.ChangeDutyCycle(40)
#following two lines can be removed if you want car make continuous movement without pause
#time.sleep(0.25)
#stopcar()
def turnRight():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_Left.ChangeDutyCycle(75)
pwm_Right.ChangeDutyCycle(0)
#following two lines can be removed if you want car make continuous movement without pause
#time.sleep(0.25)
#stopcar()
def turnLeft():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_Left.ChangeDutyCycle(0)
pwm_Right.ChangeDutyCycle(75)
#following two lines can be removed if you want car make continuous movement without pause
#time.sleep(0.25)
#stopcar()
while Ture
forward()
time.sleep(1)
stopcar()
time.sleep(0.25)
backward()
time.sleep(1)
stopcar()
time.sleep(0.25)
turnLeft()
time.sleep(1)
stopcar()
time.sleep(0.25)
turnRight()
time.sleep(1)
stopcar()
time.sleep(0.25)