-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_library.py
121 lines (93 loc) · 2 KB
/
mm_library.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
#-*- coding: utf-8 -*-
import time as time
import RPi.GPIO as GPIO
import random
#Set GPIO pin numbering
GPIO.setmode(GPIO.BCM)
#Intialize right side pins
GPIO.setup(27, GPIO.OUT) #1EN
GPIO.setup(2, GPIO.OUT) #1A
GPIO.setup(3, GPIO.OUT) #1B
#Initialize left side pins
GPIO.setup(22, GPIO.OUT) #2EN
GPIO.setup(4, GPIO.OUT) #2A
GPIO.setup(17, GPIO.OUT) #2B
#Set GPIO pin numbering
GPIO.setmode(GPIO.BCM)
#Right 10cm sensor
GPIO.setup(6, GPIO.IN)
#Left 10cm Sensor
GPIO.setup(5, GPIO.IN)
#Right 5cm Sensor
GPIO.setup(15, GPIO.IN)
#Left 5cm Sensor
GPIO.setup(14, GPIO.IN)
#Front 10cm Sensor
GPIO.setup(18, GPIO.IN)
pwm_left = GPIO.PWM(22,200)
pwm_right = GPIO.PWM(27,200)
pwm_left.start(85) ##82.25
pwm_right.start(100)
###################################################### L I B R A R Y
##SENSORS
def sensorFwd():
return GPIO.input(18)
def sensorR5():
return GPIO.input(15)
def sensorR10():
return GPIO.input(6)
def sensorL5():
return GPIO.input(14)
def sensorL10():
return GPIO.input(5)
##MOVEMENT
def stop():
GPIO.output(27, 0)
GPIO.output(2, 0)
GPIO.output(3, 0)
GPIO.output(22, 0)
GPIO.output(4, 0)
GPIO.output(17, 0)
def moveFwd():
GPIO.output(27, 1)
GPIO.output(2, 1)
GPIO.output(3, 0)
GPIO.output(22, 1)
GPIO.output(4, 0)
GPIO.output(17, 1)
def moveBwd():
GPIO.output(27, 1)
GPIO.output(2, 0)
GPIO.output(3, 1)
GPIO.output(22, 1)
GPIO.output(4, 1)
GPIO.output(17, 0)
def moveLeft():
GPIO.output(27, 1)
GPIO.output(2, 0)
GPIO.output(3, 1)
GPIO.output(22, 1)
GPIO.output(4, 0)
GPIO.output(17, 1)
def moveRight():
GPIO.output(27, 1)
GPIO.output(2, 1)
GPIO.output(3, 0)
GPIO.output(22, 1)
GPIO.output(4, 1)
GPIO.output(17, 0)
############
def maintain(): ## A function used to maintain a straight course
if ((sensorL5()==0 and sensorR5()==0) or (sensorL5()==1 and sensorR5()==1)):
moveFwd()
time.sleep(0.15)
if (sensorL5()==0):
moveRight()
time.sleep(0.02)
moveFwd()
time.sleep(0.15)
if (sensorR5()==0):
moveLeft()
time.sleep(0.02)
moveFwd()
time.sleep(0.15)