Skip to content

Commit da097a6

Browse files
committed
function and definitino
1 parent 467a974 commit da097a6

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

course/measuring_distance/locate_object.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,76 @@ Instead, the robot should remember the heading it faces when detecting *both* ed
8383
between those edges, and thus the center of the object. We can store each edge angle in variables, naming them :code:`firstAngle`
8484
and :code:`secondAngle`.
8585

86+
We're already quite familiar with turning until an edge is detected. Now, we'll need to detect *both* edges. However, it would be
87+
quite unwieldy and error-prone to just copy the edge detection code, so let's make a function to generalize this. Note that the existing
88+
code detects a sudden *decrease* in distance, but we want to handle sudden *increases* in distances too.
89+
90+
How can we support both behaviors in a single function? We can pass in a parameter to specify whether we want to detect an increase
91+
or decrease in distance! We can call this parameter :code:`isIncrease` and pass in a boolean (true or false) value.
92+
93+
If :code:`increase` is :code:`True`, then we want to detect an increase in distance, which is when :code:`currentDistance - previousDistance > changeThreshold`.
94+
95+
If :code:`increase` is :code:`False`, then we want to detect a decrease in distance, which is when :code:`previousDistance - currentDistance > changeThreshold`.
96+
97+
For more flexibility, let's also have a parameter for the maximum distance, and a parameter for the change threshold.
98+
99+
Here's the function definition:
100+
101+
.. tab-set::
102+
103+
.. tab-item:: Python
104+
105+
.. code-block:: python
106+
107+
def turnUntilEdge(isIncrease, maximumDistance, changeThreshold):
108+
109+
# store initial value for current distance
110+
currentDistance = rangefinder.distance()
111+
112+
# start spinning in place until an object is detected
113+
differentialDrive.set_speed(5, -5)
114+
115+
while True: # doesn't actually repeat forever. loop will be broken if an object is detected
116+
117+
# update previous and current distance
118+
previousDistance = currentDistance
119+
currentDistance = rangefinder.distance()
120+
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
129+
130+
time.sleep(0.1)
131+
132+
# stop spinning drive motors
133+
differentialDrive.stop()
134+
135+
136+
.. tab-item:: Blockly
137+
138+
.. image:: media/detectiondefinition.png
139+
:width: 900
140+
141+
Here's the equivalent function call to the turn and detection code in the previous section:
142+
143+
.. tab-set::
144+
145+
.. tab-item:: Python
146+
147+
.. code-block:: python
148+
149+
turnUntilEdge(False, 40, 15)
150+
151+
.. tab-item:: Blockly
152+
153+
.. image:: media/detectioncall.png
154+
:width: 900
155+
86156
Let's walk through the code step by step.
87157

88158
First, the robot should spin in place until it detects the first edge, then stop. The code should be similar as before.
-16.3 KB
Loading
74.4 KB
Loading
650 KB
Loading

0 commit comments

Comments
 (0)