Skip to content

Commit 467a974

Browse files
committed
code revised
1 parent c5a75b6 commit 467a974

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

course/measuring_distance/locate_object.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,27 @@ In the following example code, we use a change threshold of 15cm, and a maximum
4242
changeThreshold = 15 # distance change in cm needed to trigger detection
4343
maximumDistance = 40 # maximum distance in cm to consider an object detected
4444
45-
# store initial values for current and previous distance
45+
# store initial value for current distance
4646
currentDistance = rangefinder.distance()
47-
previousDistance = currentDistance
4847
4948
# start spinning in place until an object is detected
5049
differentialDrive.set_speed(5, -5)
5150
52-
# repeat until change in distance is greater than threshold, and current distance is less than maximum
53-
while not (previousDistance - currentDistance > changeThreshold and currentDistance < maximumDistance):
51+
while True: # doesn't actually repeat forever. loop will be broken if an object is detected
5452
55-
time.sleep(0.1)
56-
5753
# update previous and current distance
5854
previousDistance = currentDistance
5955
currentDistance = rangefinder.distance()
6056
61-
# stop spinning
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
62+
63+
time.sleep(0.1)
64+
65+
# stop spinning drive motors
6266
differentialDrive.stop()
6367
6468
64.3 KB
Loading

0 commit comments

Comments
 (0)