-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge3.py
212 lines (180 loc) · 5.23 KB
/
challenge3.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#-*- coding:UTF-8 -*-
import RPi.GPIO as GPIO
import time
#Definition of pin
IN1 = 20
IN2 = 21
IN3 = 19
IN4 = 26
ENA = 16
ENB = 13
#Definition of button
key = 8
#Definition of ultrasonic module pin
EchoPin = 0
TrigPin = 1
#Definition of RGB module pin
LED_R = 22
LED_G = 27
LED_B = 24
#Set the GPIO port to BCM encoding mode.
GPIO.setmode(GPIO.BCM)
#Ignore warning information
GPIO.setwarnings(False)
#RGB pins are initialized into output mode
GPIO.setup(LED_R, GPIO.OUT)
GPIO.setup(LED_G, GPIO.OUT)
GPIO.setup(LED_B, GPIO.OUT)
pwm_rled = GPIO.PWM(LED_R, 1000)
pwm_gled = GPIO.PWM(LED_G, 1000)
pwm_bled = GPIO.PWM(LED_B, 1000)
pwm_rled.start(0)
pwm_gled.start(0)
pwm_bled.start(0)
#Motor pins are initialized into output mode
#Key pin is initialized into input mode
#Ultrasonic pin initialization
def init():
global pwm_ENA
global pwm_ENB
global delaytime
GPIO.setup(ENA,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN1,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN2,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(ENB,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN3,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN4,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(key,GPIO.IN)
GPIO.setup(EchoPin,GPIO.IN)
GPIO.setup(TrigPin,GPIO.OUT)
#Set the PWM pin and frequency is 2000hz
pwm_ENA = GPIO.PWM(ENA, 2000)
pwm_ENB = GPIO.PWM(ENB, 2000)
pwm_ENA.start(0)
pwm_ENB.start(0)
#advance
def run(delaytime):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
time.sleep(delaytime)
#back
def back(delaytime):
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
time.sleep(delaytime)
#brake
def brake(delaytime):
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
time.sleep(delaytime)
def color_led_pwm(iRed,iGreen, iBlue):
v_red = (100*iRed)/255
v_green = (100*iGreen)/255
v_blue = (100*iBlue)/255
pwm_rled.ChangeDutyCycle(v_red)
pwm_gled.ChangeDutyCycle(v_green)
pwm_bled.ChangeDutyCycle(v_blue)
time.sleep(0.02)
#Button detection
def key_scan():
while GPIO.input(key):
pass
while not GPIO.input(key):
time.sleep(0.01)
if not GPIO.input(key):
time.sleep(0.01)
while not GPIO.input(key):
pass
def Distance():
GPIO.output(TrigPin,GPIO.LOW)
time.sleep(0.000002)
GPIO.output(TrigPin,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(TrigPin,GPIO.LOW)
t3 = time.time()
while not GPIO.input(EchoPin):
t4 = time.time()
if (t4 - t3) > 0.03 :
return -1
t1 = time.time()
while GPIO.input(EchoPin):
t5 = time.time()
if(t5 - t1) > 0.03 :
return -1
t2 = time.time()
time.sleep(0.01)
# print "distance is %d " % (((t2 - t1)* 340 / 2) * 100)
return ((t2 - t1)* 340 / 2) * 100
def Distance_test():
num = 0
ultrasonic = []
while num < 5:
distance = Distance()
while int(distance) == -1 :
distance = Distance()
# print("Tdistance is %f"%(distance) )
while (int(distance) >= 500 or int(distance) == 0) :
distance = Distance()
# print("Edistance is %f"%(distance) )
ultrasonic.append(distance)
num = num + 1
time.sleep(0.01)
# print ultrasonic
distance = (ultrasonic[1] + ultrasonic[2] + ultrasonic[3])/3
# print("distance is %f"%(distance) )
return distance
def GET_gesture():
distance1 = Distance_test()
time.sleep(0.1)
distance2 = Distance_test()
diff = distance1 - distance2
#10cm to 8 cm
if (diff < -1) :
return 1
elif (diff > 1) :
return -1
else :
return 0
# delay 2s
time.sleep(2)
#The try/except statement is used to detect errors in the try block.
#the except statement catches the exception information and processes it.
try:
init()
# key_scan()
while True:
print("start")
pwm_ENA.start(0)
pwm_ENB.start(0)
currentDistance = Distance_test()
movement=0
color_led_pwm(255,0,0)
for i in range(9):
direction = GET_gesture()
print("current measure is %f movement is %f"%(i, direction) )
movement = movement + direction
color_led_pwm(0,0,0)
print("current distance is %f movement is %f"%(currentDistance, movement) )
# to close, don't do anything
if movement > 2: # wave come here
run(0.5)
elif movement < -2: # wave go back
back(0.5)
time.sleep(0.5)
except KeyboardInterrupt:
pass
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()