forked from sch2307/all_new_rascar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AD_assignment_main.py
254 lines (202 loc) · 9.17 KB
/
AD_assignment_main.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#########################################################################
# Date: 2018/10/02
# file name: 2nd_assignment_main.py
# Purpose: this code has been generated for the 4 wheel drive body
# moving object to perform the project with line detector
# this code is used for the student only
#########################################################################
from car import Car
import time
from GPIO_LED import LED
from GPIO_Button import Button
from GPIO_PWM_Buzzer import Buzzer
class myCar(object):
def __init__(self, car_name):
self.car = Car(car_name)
self.ledl = LED(22)
self.button = Button(29)
self.ledr = LED(36)
self.buzzer = Buzzer(40)
def drive_parking(self):
self.car.drive_parking()
# =======================================================================
# 2ND_ASSIGNMENT_CODE
# Complete the code to perform Second Assignment
# =======================================================================
def line_tracing(self, lt_status):
if lt_status == [0,0,1,0,0] or lt_status == [0,1,1,1,0]:
return 90
elif lt_status == [0,1,1,0,0] or lt_status == [1,1,1,1,0]:
return 90-15
elif lt_status == [0,0,1,1,0] or lt_status == [0,1,1,1,1]:
return 90+15
elif lt_status == [0,1,0,0,0] or lt_status == [1,1,1,0,0]:
return 90-10
elif lt_status == [0,0,1,1,1] or lt_status == [0,0,0,1,0]:
return 90+10
elif lt_status == [1,1,0,0,0]:
return 90-30
elif lt_status == [0,0,0,1,1]:
return 90+30
elif lt_status == [1,0,0,0,0]:
return 90-35
elif lt_status == [0,0,0,0,1]:
return 90+35
else:
return 90
def car_startup(self):
current_speed = self.car.SLOW
current_direction = 90
current_distance = self.car.distance_detector.get_distance()
while current_distance > 25 or current_distance == -1:
lt_status = self.car.line_detector.read_digital()
current_direction = self.line_tracing(lt_status)
self.car.accelerator.go_forward(current_speed)
self.car.steering.turn(current_direction)
current_distance = self.car.distance_detector.get_distance()
print(lt_status, current_distance)
if current_direction > 90:
self.ledl.turn_on()
self.ledr.turn_off()
else:
self.ledl.turn_off()
self.ledr.turn_on()
if lt_status == [0,0,0,0,0]:
print("back")
if current_direction < 90:
self.ledl.turn_off()
self.ledr.turn_on()
self.car.steering.turn(90-35)
while not self.car.line_detector.read_digital()[3]:
self.car.accelerator.go_backward(current_speed)
else:
self.ledl.turn_on()
self.ledr.turn_off()
self.car.steering.turn(90+35)
while not self.car.line_detector.read_digital()[1]:
self.car.accelerator.go_backward(current_speed)
self.ledl.turn_off()
self.ledr.turn_off()
self.car.accelerator.stop()
self.button.wait_press()
current_speed = self.car.NORMAL
self.car.accelerator.go_forward(current_speed)
lt_status = self.car.line_detector.read_digital()
while True:
escape_time = 1.3
self.buzzer.time = time.time()
self.ledl.blink()
self.ledr.blink()
print("loop 1 in")
while 1 in lt_status:
self.car.steering.turn(90-35)
lt_status = self.car.line_detector.read_digital()
print(lt_status)
self.buzzer.beep()
self.ledl.blink()
self.ledr.blink()
time.sleep(escape_time)
self.car.steering.turn(90)
print("loop 2 in")
while lt_status == [0,0,0,0,0]:
lt_status = self.car.line_detector.read_digital()
self.buzzer.beep()
self.ledl.blink()
self.ledr.blink()
print("loop 3 in")
while 1 in lt_status:
self.car.steering.turn(90+35)
lt_status = self.car.line_detector.read_digital()
print(lt_status)
self.buzzer.beep()
self.ledl.blink()
self.ledr.blink()
time.sleep(escape_time+0.5)
self.car.steering.turn(90)
print("loop 4 in")
while lt_status == [0,0,0,0,0]:
lt_status = self.car.line_detector.read_digital()
self.buzzer.beep()
self.ledl.blink()
self.ledr.blink()
print("loop out")
break
current_distance = self.car.distance_detector.get_distance()
current_speed = self.car.NORMAL
while current_distance > 15 or current_distance == -1:
lt_status = self.car.line_detector.read_digital()
current_direction = self.line_tracing(lt_status)
self.car.accelerator.go_forward(current_speed)
self.car.steering.turn(current_direction)
current_distance = self.car.distance_detector.get_distance()
print(lt_status, current_distance)
self.buzzer.beep()
self.ledl.blink()
self.ledr.blink()
if current_direction > 90:
self.ledl.turn_on()
self.ledr.turn_off()
else:
self.ledl.turn_off()
self.ledr.turn_on()
if lt_status == [0,0,0,0,0]:
print("back")
if current_direction < 90:
self.ledl.turn_off()
self.ledr.turn_on()
self.car.steering.turn(90-35)
while not self.car.line_detector.read_digital()[3]:
self.car.accelerator.go_backward(current_speed)
self.buzzer.beep()
else:
self.ledl.turn_on()
self.ledr.turn_off()
self.car.steering.turn(90+35)
while not self.car.line_detector.read_digital()[1]:
self.car.accelerator.go_backward(current_speed)
self.buzzer.beep()
self.ledl.turn_off()
self.ledr.turn_off()
current_speed = self.car.NORMAL
self.buzzer.mute()
self.car.accelerator.go_backward(current_speed)
self.car.steering.turn(90+25)
time.sleep(1.7)
self.car.accelerator.go_forward(current_speed)
self.car.steering.turn(25)
time.sleep(0.5)
t = time.time()
while time.time() - t < 2:
lt_status = self.car.line_detector.read_digital()
current_direction = self.line_tracing(lt_status)
self.car.accelerator.go_forward(current_speed)
self.car.steering.turn(current_direction)
current_distance = self.car.distance_detector.get_distance()
print(lt_status, current_distance)
if lt_status == [0,0,0,0,0]:
print("back")
if current_direction < 90:
self.ledl.turn_off()
self.ledr.turn_on()
self.car.steering.turn(90-35)
while not self.car.line_detector.read_digital()[3]:
self.car.accelerator.go_backward(current_speed)
else:
self.ledl.turn_on()
self.ledr.turn_off()
self.car.steering.turn(90+35)
while not self.car.line_detector.read_digital()[1]:
self.car.accelerator.go_backward(current_speed)
self.ledl.turn_off()
self.ledr.turn_off()
self.car.accelerator.stop()
self.car.steering.center_alignment()
print("stop")
if __name__ == "__main__":
try:
myCar = myCar("CarName")
myCar.car_startup()
except KeyboardInterrupt:
# when the Ctrl+C key has been pressed,
# the moving object will be stopped
myCar.drive_parking()