Repository navigation
Expand file tree
/
Copy pathrange.py
More file actions
109 lines (83 loc) · 2.61 KB
/
Copy pathrange.py
File metadata and controls
109 lines (83 loc) · 2.61 KB
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
from movement import (
move_and_adjust,
stop_motor,
pivot_180_left,
pivot_180_right,
left_pivot_90,
right_pivot_90,
move_until_junction,
move_back,
pick_up,
drop_off,
move_forward
)
from machine import Pin, I2C
from libs.VL53L0X.VL53L0X import VL53L0X
from utime import sleep
from magnet import get_magnet
# Initialise the range sensor once
i2c_bus = I2C(id=0, sda=Pin(8), scl=Pin(9), freq=100000)
vl53l0 = VL53L0X(i2c_bus)
vl53l0.set_Vcsel_pulse_period(vl53l0.vcsel_period_type[0], 18)
vl53l0.set_Vcsel_pulse_period(vl53l0.vcsel_period_type[1], 14)
vl53l0.start()
sleep(0.1)
def get_distance():
"""Return the measured distance in mm."""
distance = vl53l0.read()
if distance is None:
get_distance()
return None
return int(distance)
def move_till_box(distance_limit=50):
distance = get_distance()
while distance is not None and distance >= distance_limit: #mm, trial and error to find optimal distance
move_and_adjust(0.1)
distance = get_distance()
stop_motor()
# will go through the bays until true is returned, meaning a box isn't is present,
# unless dropping = false (so its picking up), then its the other way round
def detect_box(time, direction, dropping=True):
junction_count = 0
while True:
junction_count += 1
# Check the current bay
move_and_adjust(time)
distance = get_distance()
if dropping == True:
if distance is not None and distance >= 200: # mm
stop_motor()
sleep(1)
return junction_count
else:
if distance is not None and distance <= 200:
stop_motor()
sleep(1)
return junction_count
# box found: turn around
pivot_180_left(sleeping=True, forward=False, sleeping_time=3)
move_until_junction()
if direction == 'left':
# Turn left into the next bay
left_pivot_90(continue_time=1.2)
move_back(left_speed=60, right_speed=60)
sleep(0.5)
move_until_junction()
left_pivot_90(sleeping=True, continue_time=1.2)
if direction == 'right':
right_pivot_90(continue_time=1.2)
move_back(left_speed=60, right_speed=60)
sleep(0.5)
move_until_junction()
right_pivot_90(sleeping=True, continue_time=1.2)
def test():
sleep(3)
drop_off()
# detect_box(2.2, "left")
detect_box(2.2, 'left', dropping=False)
move_till_box()
for i in range(10):
pole = get_magnet()
sleep(0.5)
print(pole)
pick_up()