Activity 3: Calculating Distances

In many robotics contests, more precise robot navigation means better scores.  One popular entry-level robotics contest is called dead reckoning.  The entire goal of this contest is to make your robot go to one or more locations and then return to exactly where it started.

You might remember asking your parents this question, over and over again, while on your way to a vacation destination or relatives’ house:

“Are we there yet?”

Perhaps when you got a little older, and learned division in school, you started watching the road signs to see how far it was to the destination city.  Next, you checked the car’s speedometer.  By dividing the speed into the distance, you got a pretty good estimate of the time it would take to get there.  You may not have been thinking in these exact terms, but here is the equation you were using:

Equation: travel time equals distance over speed

U.S. customary units example: If you’re 140 miles away from your destination, and you’re traveling 70 miles per hour, it’s going to take 2 hours to get there.

Equation: time equals 140 miles over 70 miles per hour, which resolves to time equals two hours

Metric units example: If you’re 200 kilometers away from your destination, and you’re traveling 100 kilometers per hour, it’s going to take 2 hours to get there.

Equation: time equals 200 kilometers over 100 kilometers per hour, which resolves to time equals two hours

You can do the same exercise with the BOE Shield-Bot, except you have control over how far away the destination is.  Here’s the equation you will use:

Equation: servo run time equals BOE Shield-bot distance over BOE Shield-Bot speed

  • Enter, save, and run ForwardOneSecond.
  • Place your BOE Shield-Bot next to a ruler. 
  • Make sure to line up the point where the wheel touches the ground with the 0 in/cm position on the ruler.

  • Press the Reset button on your board to re-run the sketch.
  • Measure how far your BOE Shield-Bot traveled by recording the measurement where the wheel is now touching the ground here:_______________ (in or cm).
// Robotics with the BOE Shield - ForwardOneSecond
// Make the BOE Shield-Bot roll forward for one seconds, then stop.

#include <Servo.h>                           // Include servo library
Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
void setup()                                 // Built-in initialization block
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone

  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                     // Attach right signal to pin 12
                                             // Full speed forward
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(1000);                               // ...for 1 second
  servoLeft.detach();                        // Stop sending servo signals
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating

The distance you just recorded is your BOE Shield-Bot’s speed, in units per second.  Now, you can figure out how many seconds your BOE Shield-Bot has to travel to go a particular distance.

Inches and centimeters per second   
The abbreviation for inches is in, and the abbreviation for centimeters is cm.  Likewise, inches per second is abbreviated in/s, and centimeters per second is abbreviated cm/s.  Both are convenient speed measurements for the BOE Shield-Bot.  There are 2.54 cm in 1 in.  You can convert inches to centimeters by multiplying the number of inches by 2.54.  You can convert centimeters to inches by dividing the number of centimeters by 2.54

Keep in mind that your calculations will be in terms of seconds, but the delay function will need a parameter that’s in terms of milliseconds.  So, take your result, which is in terms of seconds, and multiply it by 1000.  Then, use that value in your delay function call.  For example, to make your BOE Shield-Bot run for 2.22 seconds, you’d use delay(2220) after your writeMicroseconds calls.

U.S. customary units example: At 9 in/s, your BOE Shield-Bot has to travel for 2.22 s to travel 20 in.

Equation: time equals 20 inches over nine inches per second, which resolves to 2.22 seconds

Metric units example: At 23 cm/s, your BOE Shield-Bot has to travel for 2.22 s to travel 51 cm.

Equation: time equals 51 centimeters over 23 centimeters per second, which resolves to 2.22 seconds

Both examples above resolve to the same answer:

Equation: time in milliseconds equals time in seconds multiplied by 1000 milliseconds over seconds.  Given a time in seconds of 2.22, this resolves to 2220 milliseconds

So, use delay(2220) after your writeMicroseconds calls:


Your Turn – Your BOE Shield-Bot’s Distance

Now it’s time to try this out with distances that you choose.

  • Decide how far you want your BOE Shield-Bot to travel.
  • Use the equation below to figure out how many milliseconds of forward travel you need for that distance:

Equation: servo run time in milliseconds equalst the quantity BOE Shield-Bot distance over BOE Shield-Bot speed times the quantity 1000 milliseconds over seconds

  • Modify ForwardOneSecond to make your BOE Shield-Bot travel forward the amount of time that you determined, and try it out.  How close does it come?

Increase the accuracy of your BOE Shield-Bot distances with devices called encoders which count the holes in the BOE Shield-Bot’s wheels as they pass.