-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_track_and_avoid.py
175 lines (143 loc) · 4.3 KB
/
find_track_and_avoid.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
import car
import time
import scheduler
track_flag = 1
loaded = False
last_rotate = 0
# 1 left
# 2 right
# 0 is nothing
avoid_flag = 0
# 1 nomorl find track
# 2 return track
# 3 nothing
# plan A
def track_detector_callback(status):
global avoid_flag
# plan A
left, middle, right = status
global last_rotate
if track_flag == 1:
if left == 1:
last_rotate = 1
# car.set_global_speed(1)
car.rotate_left()
elif right == 1:
last_rotate = 2
# car.set_global_speed(1)
car.rotate_right()
elif middle == 1:
if last_rotate == 1:
scheduler.schedule('buchang', (0, car.rotate_right), (5, car.go))
if last_rotate == 2:
scheduler.schedule('buchang', (0, car.rotate_left), (5, car.go))
last_rotate = 0
car.set_global_speed(10)
car.go()
elif track_flag == 3:
pass
else:
if middle == 1:
scheduler.cancel('avoid_ob')
avoid_flag = 0
change_flag_normal()
recover_wheel_to_normal()
car.rotate_left()
print('refind road')
# # plan B
# def track_detector_callback(status):
# global avoid_flag
# _, left, right = status
# global last_rotate
# if track_flag == 1:
# if left == 1:
# last_rotate = 1
# car.set_global_speed(1)
# car.rotate_left()
# time.sleep(0.05)
# car.go()
# elif right == 1:
# last_rotate = 2
# car.set_global_speed(1)
# car.rotate_right()
# time.sleep(0.05)
# car.go()
# elif track_flag == 3:
# pass
# else:
# if left == 1:
# scheduler.cancel('avoid_ob')
# avoid_flag = 0
# change_flag_normal()
# recover_wheel_to_normal()
# car.rotate_left()
# print('refind road')
def change_flag_normal():
global track_flag
track_flag = 1
def change_flag_nothing():
global track_flag
track_flag = 3
def change_flag_return():
global track_flag
track_flag = 2
def simple_avoid_ob(status):
left, middle, right = status
print(middle)
if middle == 0:
change_flag_nothing()
car.back()
scheduler.schedule('avoid_ob', (80, car.rotate_left),
(100, change_flag_return),
(1, car.go),
(250, car.rotate_right),
(160, car.go))
def set_wheel_to_rotate():
car.set_left_wheels_speed(10)
car.set_right_wheels_speed(4)
def recover_wheel_to_normal():
car.set_left_wheels_speed(10)
car.set_right_wheels_speed(10)
def simple_avoid_ob_from_ultrasonic(status):
print(status)
global avoid_flag
if status <= 25 and avoid_flag == 0:
scheduler.cancel('avoid_ob')
avoid_flag = 1
change_flag_nothing()
car.back()
print('start avoid')
# scheduler.schedule('avoid_ob', (10, car.rotate_left),
# (45, car.go),
# (1, change_flag_return),
# (120, car.rotate_right),
# (55, car.go))
scheduler.schedule('avoid_ob', (10, car.rotate_left_90),
(0, set_wheel_to_rotate),
(0, car.go),
(10, change_flag_return))
def load():
global loaded, avoid_flag, track_flag, last_rotate
# car.set_global_speed(5)
avoid_flag = 0
track_flag = 1
last_rotate = 0
if not loaded:
car.on_track_detector_change(track_detector_callback)
# car.on_infrared_sensor_change(simple_avoid_ob)
car.on_ultrasonic_in_range(simple_avoid_ob_from_ultrasonic, 0, 25)
scheduler.start()
loaded = True
def unload():
global loaded
if loaded:
car.remove_track_detector_callback(track_detector_callback)
# car.remove_infrared_sensor_change(simple_avoid_ob)
car.remove_ultrasonic_callback(simple_avoid_ob_from_ultrasonic)
scheduler.stop()
loaded = False
if __name__ == '__main__':
load()
car.go()
scheduler.start()
# scheduler.schedule('stop',(2000,car.brake))