Skip to content

Commit 4f6bb5d

Browse files
committed
changes
1 parent da097a6 commit 4f6bb5d

4 files changed

Lines changed: 46 additions & 29 deletions

File tree

course/measuring_distance/locate_object.rst

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,19 @@ How can we find the change in distance readings over each iteration of the loop?
2222
reading in a variable, and compare it to the current distance reading. If this change is greater than some threshold,
2323
then we can assume that an object has been detected.
2424

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-
2925
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
3227
the threshold, the robot can stop spinning and head towards the object.
3328

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.
3530

3631
.. tab-set::
3732

3833
.. tab-item:: Python
3934

4035
.. code-block:: python
4136
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
4438
4539
# store initial value for current distance
4640
currentDistance = rangefinder.distance()
@@ -54,11 +48,9 @@ In the following example code, we use a change threshold of 15cm, and a maximum
5448
previousDistance = currentDistance
5549
currentDistance = rangefinder.distance()
5650
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
6254
6355
time.sleep(0.1)
6456
@@ -94,7 +86,7 @@ If :code:`increase` is :code:`True`, then we want to detect an increase in dista
9486

9587
If :code:`increase` is :code:`False`, then we want to detect a decrease in distance, which is when :code:`previousDistance - currentDistance > changeThreshold`.
9688

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.
9890

9991
Here's the function definition:
10092

@@ -104,7 +96,7 @@ Here's the function definition:
10496

10597
.. code-block:: python
10698
107-
def turnUntilEdge(isIncrease, maximumDistance, changeThreshold):
99+
def turnUntilEdge(isIncrease, changeThreshold):
108100
109101
# store initial value for current distance
110102
currentDistance = rangefinder.distance()
@@ -118,14 +110,12 @@ Here's the function definition:
118110
previousDistance = currentDistance
119111
currentDistance = rangefinder.distance()
120112
121-
if currentDistance < maximumDistance: # only consider an object detected if it's within the maximum distance
122-
123-
if isIncrease and currentDistance - previousDistance > changeThreshold:
124-
# if sudden increase in distance, then an object has been detected
125-
break
126-
elif not isIncrease and previousDistance - currentDistance > changeThreshold:
127-
# if sudden decrease in distance, then an object has been detected
128-
break
113+
if isIncrease and currentDistance - previousDistance > changeThreshold:
114+
# if sudden increase in distance, then an object has been detected
115+
break
116+
elif not isIncrease and previousDistance - currentDistance > changeThreshold:
117+
# if sudden decrease in distance, then an object has been detected
118+
break
129119
130120
time.sleep(0.1)
131121
@@ -151,17 +141,44 @@ Here's the equivalent function call to the turn and detection code in the previo
151141
.. tab-item:: Blockly
152142

153143
.. image:: media/detectioncall.png
154-
:width: 900
144+
:width: 200
155145

156-
Let's walk through the code step by step.
146+
Now, it's time to write the full program to detect both edges and turn to the center.
157147

158-
First, the robot should spin in place until it detects the first edge, then stop. The code should be similar as before.
148+
Implementing Dual Edge Detection
149+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159150

160-
[code]
151+
Let's walk through each step of the process in code.
152+
153+
First, the robot should spin in place until it detects the first edge, then stop. This is simply the function call we saw earlier.
154+
155+
.. tab-set::
156+
157+
.. tab-item:: Python
158+
159+
.. code-block:: python
160+
161+
turnUntilEdge(False, 40, 15)
162+
163+
.. tab-item:: Blockly
164+
165+
.. image:: media/detectioncall.png
166+
:width: 200
161167

162168
Next, we want to record the robot's heading for this first edge, and store it to :code:`firstAngle`.
163169

164-
[code]
170+
.. tab-set::
171+
172+
.. tab-item:: Python
173+
174+
.. code-block:: python
175+
176+
turnUntilEdge(False, 40, 15)
177+
178+
.. tab-item:: Blockly
179+
180+
.. image:: media/detectioncall.png
181+
:width: 200
165182

166183
Then, the robot should spin in place again until it detects the second edge, then stop. The whole time while the robot is spinning
167184
from the first to second edge, it should be detecting the object in close proximity, so the robot should know its hit the second
-44.4 KB
Loading
-48.3 KB
Loading
-439 KB
Loading

0 commit comments

Comments
 (0)