You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: course/measuring_distance/locate_object.rst
+46-29Lines changed: 46 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,25 +22,19 @@ How can we find the change in distance readings over each iteration of the loop?
22
22
reading in a variable, and compare it to the current distance reading. If this change is greater than some threshold,
23
23
then we can assume that an object has been detected.
24
24
25
-
But, it's possible that there are some objects super far away that still result in a large change in distance readings.
26
-
To avoid this, we can set a maximum distance threshold, and only consider an object to be detected if it's within some
27
-
maximum distance.
28
-
29
25
To code this, we can start by setting the drive motor speeds to spin in opposite directions to start spinning
30
-
the robot in place. Then, we can utilize a while loop to keep spinning while the distance reading is greater than
31
-
some threshold, meaning that the robot has not yet found an object. Once the change distance reading is greater than
26
+
the robot in place. Then, once the change in distance reading is greater than
32
27
the threshold, the robot can stop spinning and head towards the object.
33
28
34
-
In the following example code, we use a change threshold of 15cm, and a maximum distance threshold of 40cm.
29
+
In the following example code, we use a change threshold of 30 cm.
35
30
36
31
.. tab-set::
37
32
38
33
.. tab-item:: Python
39
34
40
35
.. code-block:: python
41
36
42
-
changeThreshold =15# distance change in cm needed to trigger detection
43
-
maximumDistance =40# maximum distance in cm to consider an object detected
37
+
changeThreshold =30# distance change in cm needed to trigger detection
44
38
45
39
# store initial value for current distance
46
40
currentDistance = rangefinder.distance()
@@ -54,11 +48,9 @@ In the following example code, we use a change threshold of 15cm, and a maximum
54
48
previousDistance = currentDistance
55
49
currentDistance = rangefinder.distance()
56
50
57
-
if currentDistance < maximumDistance: # only consider an object detected if it's within the maximum distance
58
-
59
-
# if sudden decrease in distance, then an object has been detected
60
-
if previousDistance - currentDistance > changeThreshold:
61
-
break# break out of the while loop
51
+
# if sudden decrease in distance, then an object has been detected
52
+
if previousDistance - currentDistance > changeThreshold:
53
+
break# break out of the while loop
62
54
63
55
time.sleep(0.1)
64
56
@@ -94,7 +86,7 @@ If :code:`increase` is :code:`True`, then we want to detect an increase in dista
94
86
95
87
If :code:`increase` is :code:`False`, then we want to detect a decrease in distance, which is when :code:`previousDistance - currentDistance > changeThreshold`.
96
88
97
-
For more flexibility, let's also have a parameter for the maximum distance, and a parameter for the change threshold.
89
+
For more flexibility, let's add a parameter for the change threshold.
98
90
99
91
Here's the function definition:
100
92
@@ -104,7 +96,7 @@ Here's the function definition:
0 commit comments