Skip to content

Commit

Permalink
Merge pull request #131 from Open-STEM/ansel-minor-changes
Browse files Browse the repository at this point in the history
small bugfixes, make variable instead of magic number
  • Loading branch information
bradamiller authored Aug 22, 2023
2 parents 1f3998e + c571a1b commit d514ee8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions course/robot_control/distance_tracker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ For this activity, let's use a target distance of 30 cm.
rangefinder = Rangefinder.get_default_rangefinder()
distance = 30
while True:
if rangefinder.distance() < 30:
if rangefinder.distance() < distance:
drivetrain.set_speed(-20, -20)
elif rangefinder.distance() > 30:
elif rangefinder.distance() > distance:
drivetrain.set_speed(20, 20)
.. tab-item:: Blockly

.. image:: media/SimpleStandoff.png
:width: 300
:width: 500

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.
So what if we add a third case that tells the robot's motors to stop when sonar distance equals 30 cm?
Expand All @@ -47,10 +48,11 @@ So what if we add a third case that tells the robot's motors to stop when sonar
rangefinder = Rangefinder.get_default_rangefinder()
distance = 30
while True:
if rangefinder.distance() < 30:
if rangefinder.distance() < distance:
drivetrain.set_speed(-20, -20)
elif rangefinder.distance() > 30:
elif rangefinder.distance() > distance:
drivetrain.set_speed(20, 20)
else:
drivetrain.stop()
Expand All @@ -59,7 +61,7 @@ So what if we add a third case that tells the robot's motors to stop when sonar
.. tab-item:: Blockly

.. image:: media/SimpleStandoffStop.png
:width: 300
:width: 500

Unfortunately, even with this code, our robot still doesn't stop! The issue is that the distance sensor is so precise that it
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.
Expand All @@ -76,10 +78,12 @@ We can do this by creating a range in which our robot stops called a "deadband."
rangefinder = Rangefinder.get_default_rangefinder()
distance = 30
tolerance = 2.5
while True:
if rangefinder.distance() < 27.5:
if rangefinder.distance() < distance - tolerance:
drivetrain.set_speed(-20, -20)
elif rangefinder.distance() > 32.5:
elif rangefinder.distance() > distance + tolerance:
drivetrain.set_speed(20, 20)
else:
drivetrain.stop()
Expand All @@ -88,7 +92,14 @@ We can do this by creating a range in which our robot stops called a "deadband."
.. tab-item:: Blockly

.. image:: media/deadband.png
:width: 300
:width: 550

.. note::
Notice how, instead of hardcoding numbers such as 27.5 and 32.5, we used variables. This gives us two benefits:

1. We can easily change the desired distance and tolerance without having to change the code itself.

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.

This code should allow the robot to stop when it senses a sonar distance of ~30 cm. Our issue now is that
there is a potential error of +- 2.5 cm from our desired following distance. Luckily, there is a solution to this called "proportional control."
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"...
Binary file modified course/robot_control/media/SimpleStandoff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified course/robot_control/media/SimpleStandoffStop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified course/robot_control/media/deadband.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d514ee8

Please sign in to comment.