-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge4.py
165 lines (138 loc) · 3.97 KB
/
challenge4.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
#-*- 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
#Definition of servo pin
ServoPin = 23
#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
global pwm_servo
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)
GPIO.setup(ServoPin, GPIO.OUT)
pwm_servo = GPIO.PWM(ServoPin, 50)
pwm_servo.start(0)
#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)
def run(leftEngine, rightEngine):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(leftEngine)
pwm_ENB.ChangeDutyCycle(rightEngine)
time.sleep(0.1)
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)
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()
while (int(distance) >= 500 or int(distance) == 0) :
distance = Distance()
ultrasonic.append(distance)
num = num + 1
time.sleep(0.01)
distance = (ultrasonic[1] + ultrasonic[2] + ultrasonic[3])/3
return distance
time.sleep(1)
try:
init()
pwm_servo.ChangeDutyCycle(2)
time.sleep(1)
# drive car parralel to wall on the right
for i in range(40):
currentDistance = Distance_test()
print("current distance is %f cycle is %f"%(currentDistance, i) )
if currentDistance < 48 :
# to close
color_led_pwm(255,0,0)
run(12, 20)
elif currentDistance > 52 :
# to far
color_led_pwm(0,0,255)
run(20, 12)
else :
# perfect
color_led_pwm(0,255,0)
run(20, 20)
color_led_pwm(0,0,0)
run(0, 0)
except KeyboardInterrupt:
pass
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()