Skip to content

Commit c571a1b

Browse files
committed
small bugfixes, make variable instead of magic number
1 parent 85ba5ec commit c571a1b

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

course/robot_control/distance_tracker.rst

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ For this activity, let's use a target distance of 30 cm.
2222
2323
rangefinder = Rangefinder.get_default_rangefinder()
2424
25+
distance = 30
2526
while True:
26-
if rangefinder.distance() < 30:
27+
if rangefinder.distance() < distance:
2728
drivetrain.set_speed(-20, -20)
28-
elif rangefinder.distance() > 30:
29+
elif rangefinder.distance() > distance:
2930
drivetrain.set_speed(20, 20)
3031
3132
3233
.. tab-item:: Blockly
3334

3435
.. image:: media/SimpleStandoff.png
35-
:width: 300
36+
:width: 500
3637

3738
You'll notice that this code causes the robot to move back and forth, or oscillate, as the sonar distance continuously swaps between being greater than and less than 30 cm.
3839
So what if we add a third case that tells the robot's motors to stop when sonar distance equals 30 cm?
@@ -47,10 +48,11 @@ So what if we add a third case that tells the robot's motors to stop when sonar
4748
4849
rangefinder = Rangefinder.get_default_rangefinder()
4950
51+
distance = 30
5052
while True:
51-
if rangefinder.distance() < 30:
53+
if rangefinder.distance() < distance:
5254
drivetrain.set_speed(-20, -20)
53-
elif rangefinder.distance() > 30:
55+
elif rangefinder.distance() > distance:
5456
drivetrain.set_speed(20, 20)
5557
else:
5658
drivetrain.stop()
@@ -59,7 +61,7 @@ So what if we add a third case that tells the robot's motors to stop when sonar
5961
.. tab-item:: Blockly
6062

6163
.. image:: media/SimpleStandoffStop.png
62-
:width: 300
64+
:width: 500
6365

6466
Unfortunately, even with this code, our robot still doesn't stop! The issue is that the distance sensor is so precise that it
6567
never reads exactly 30 cm. We can combat this by making our robot stop when it's *close* to 30 cm instead of *exactly* 30 cm.
@@ -76,10 +78,12 @@ We can do this by creating a range in which our robot stops called a "deadband."
7678
7779
rangefinder = Rangefinder.get_default_rangefinder()
7880
81+
distance = 30
82+
tolerance = 2.5
7983
while True:
80-
if rangefinder.distance() < 27.5:
84+
if rangefinder.distance() < distance - tolerance:
8185
drivetrain.set_speed(-20, -20)
82-
elif rangefinder.distance() > 32.5:
86+
elif rangefinder.distance() > distance + tolerance:
8387
drivetrain.set_speed(20, 20)
8488
else:
8589
drivetrain.stop()
@@ -88,7 +92,14 @@ We can do this by creating a range in which our robot stops called a "deadband."
8892
.. tab-item:: Blockly
8993

9094
.. image:: media/deadband.png
91-
:width: 300
95+
:width: 550
96+
97+
.. note::
98+
Notice how, instead of hardcoding numbers such as 27.5 and 32.5, we used variables. This gives us two benefits:
99+
100+
1. We can easily change the desired distance and tolerance without having to change the code itself.
101+
102+
2. It's much easier to decipher what the code is doing, using "magic" numbers like 27.5 and 32.5 can be confusing to read because the user has to figure out what those numbers mean.
92103

93104
This code should allow the robot to stop when it senses a sonar distance of ~30 cm. Our issue now is that
94-
there is a potential error of +- 2.5 cm from our desired following distance. Luckily, there is a solution to this called "proportional control."
105+
there is a potential error of 2.5 cm from our desired following distance. Luckily, in the next section, we'll learn about something called "proportional control"...
38.5 KB
Loading
42.4 KB
Loading
320 KB
Loading

0 commit comments

Comments
 (0)