Skip to content

Commit 5652c2f

Browse files
committed
full locate object, fixes #58
1 parent 4f6bb5d commit 5652c2f

5 files changed

Lines changed: 65 additions & 15 deletions

File tree

course/measuring_distance/locate_object.rst

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In the following example code, we use a change threshold of 30 cm.
4040
currentDistance = rangefinder.distance()
4141
4242
# start spinning in place until an object is detected
43-
differentialDrive.set_speed(5, -5)
43+
drivetrain.set_speed(5, -5)
4444
4545
while True: # doesn't actually repeat forever. loop will be broken if an object is detected
4646
@@ -55,7 +55,7 @@ In the following example code, we use a change threshold of 30 cm.
5555
time.sleep(0.1)
5656
5757
# stop spinning drive motors
58-
differentialDrive.stop()
58+
drivetrain.stop()
5959
6060
6161
.. tab-item:: Blockly
@@ -102,7 +102,7 @@ Here's the function definition:
102102
currentDistance = rangefinder.distance()
103103
104104
# start spinning in place until an object is detected
105-
differentialDrive.set_speed(5, -5)
105+
drivetrain.set_speed(5, -5)
106106
107107
while True: # doesn't actually repeat forever. loop will be broken if an object is detected
108108
@@ -120,7 +120,7 @@ Here's the function definition:
120120
time.sleep(0.1)
121121
122122
# stop spinning drive motors
123-
differentialDrive.stop()
123+
drivetrain.stop()
124124
125125
126126
.. tab-item:: Blockly
@@ -136,7 +136,7 @@ Here's the equivalent function call to the turn and detection code in the previo
136136

137137
.. code-block:: python
138138
139-
turnUntilEdge(False, 40, 15)
139+
turnUntilEdge(False, 30)
140140
141141
.. tab-item:: Blockly
142142

@@ -158,7 +158,7 @@ First, the robot should spin in place until it detects the first edge, then stop
158158

159159
.. code-block:: python
160160
161-
turnUntilEdge(False, 40, 15)
161+
turnUntilEdge(False, 30)
162162
163163
.. tab-item:: Blockly
164164

@@ -173,28 +173,78 @@ Next, we want to record the robot's heading for this first edge, and store it to
173173

174174
.. code-block:: python
175175
176-
turnUntilEdge(False, 40, 15)
176+
firstAngle = imu.get_yaw()
177177
178178
.. tab-item:: Blockly
179179

180-
.. image:: media/detectioncall.png
180+
.. image:: media/firstangle.png
181181
:width: 200
182182

183-
Then, the robot should spin in place again until it detects the second edge, then stop. The whole time while the robot is spinning
184-
from the first to second edge, it should be detecting the object in close proximity, so the robot should know its hit the second
185-
edge when the distance reading is greater than the threshold again, plus a few cm, in case the object is slightly concave.
183+
Then, the robot should spin in place again until it detects the second edge, which is when there is a sudden increase in distance.
184+
185+
.. tab-set::
186+
187+
.. tab-item:: Python
188+
189+
.. code-block:: python
190+
191+
turnUntilEdge(True, 30)
192+
193+
.. tab-item:: Blockly
186194

187-
[code]
195+
.. image:: media/turntoedgetrue.png
196+
:width: 200
188197

189198
Once the robot has detected the second edge, it should record its heading and store it to :code:`secondAngle`. Now, need to figure
190199
out how much the robot needs to backtrack to aim for the center of the object. We can do this by finding the difference between
191200
the two angles, and dividing by two. This is half the angle between the two edges, and if the robot backtracks by this amount,
192201
it will be facing the center of the object. Let's store this in a variable called :code:`angleToTurn`.
193202

194-
[code]
203+
.. tab-set::
204+
205+
.. tab-item:: Python
206+
207+
.. code-block:: python
208+
209+
secondAngle = imu.get_yaw()
210+
angleToTurn = (firstAngle - secondAngle) / 2
211+
212+
.. tab-item:: Blockly
213+
214+
.. image:: media/angletoturn.png
215+
:width: 400
195216

196217
Finally, the robot can turn this much to face the center of the object, and head towards it.
197218

198-
Here's the full code:
219+
Here's the full code. Note that half-second pauses are added to make the robot's actions more visible:
220+
221+
.. tab-set::
222+
223+
.. tab-item:: Python
224+
225+
.. code-block:: python
226+
227+
# turn to first edge
228+
turnUntilEdge(False, 30)
229+
230+
# store angle at first edge
231+
firstAngle = imu.get_yaw()
232+
233+
time.sleep(0.5)
234+
235+
# turn to second edge
236+
turnUntilEdge(True, 30)
237+
238+
# store angle at second edge and calculate angle to turn
239+
secondAngle = imu.get_yaw()
240+
angleToTurn = (firstAngle - secondAngle) / 2
241+
242+
time.sleep(0.5)
243+
244+
# turn to center of object
245+
drivetrain.turn(angleToTurn)
246+
247+
.. tab-item:: Blockly
199248

200-
[code]
249+
.. image:: media/fulldualedge.png
250+
:width: 400
39.3 KB
Loading
14.4 KB
Loading
122 KB
Loading
25.2 KB
Loading

0 commit comments

Comments
 (0)