This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_correction_test.py
71 lines (57 loc) · 1.91 KB
/
path_correction_test.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
from utime import sleep_ms
from drivers import motor, button, ping, buzzer
from config import AXIL_LENGTH, MIN_SENSOR_WIDTH, CRAWL_PWM
from config.pins import BUTTON_PIN, BUZZER_PIN, MOTOR_PINS, PING_PINS
from config.maze import UNIT_SIZE, MAZE_SIZE, SOLUTIONS
btn = button.Button(BUTTON_PIN)
bzz = buzzer.Buzzer(BUZZER_PIN)
motors = motor.TwoWheel(MOTOR_PINS, AXIL_LENGTH)
threshold = max(UNIT_SIZE) - MIN_SENSOR_WIDTH
ping_sensors = list(map(lambda pins: ping.PingProximitySensor(pins, threshold), PING_PINS))
ping_collection = ping.PingProxCollection(ping_sensors)
def update_ping():
ping = ping_collection.ping()
prox = ping_collection.getProx()
return (ping, prox)
CORRECTION_RADIUS = 120
def left():
motors.rot(-CRAWL_PWM, -CORRECTION_RADIUS)
sleep_ms(300)
motors.stop()
def right():
motors.rot(CRAWL_PWM, CORRECTION_RADIUS)
sleep_ms(300)
motors.stop()
def straight():
motors.straight(CRAWL_PWM)
sleep_ms(300)
motors.stop()
def main():
ping = []
prox = []
orientation = 0
direction = 0
while not btn.isPressed:
...
while True:
ping, prox = update_ping()
print(ping)
if orientation == direction:
if ping[(orientation+1)%4] < threshold:
print("left")
motors.rot(-CRAWL_PWM, -CORRECTION_RADIUS)
elif ping[(orientation-1)%4] < threshold:
print("right")
motors.rot(CRAWL_PWM, CORRECTION_RADIUS)
else:
print("Straight")
motors.straight(CRAWL_PWM)
else:
if ping[(orientation-1)%4] < threshold:
print("left")
motors.rot(-CRAWL_PWM, CORRECTION_RADIUS)
elif ping[(orientation+1)%4] < threshold:
print("right")
motors.rot(-CRAWL_PWM, -CORRECTION_RADIUS)
else:
motors.straight(-CRAWL_PWM)