Activity 2: Tuning the Basic Maneuvers

Imagine writing a sketch that instructs your BOE Shield-Bot to travel full-speed forward for fifteen seconds.  What if your robot curves slightly to the left or right during its travel, when it’s supposed to be traveling straight ahead?  There’s no need to take the BOE Shield-Bot back apart and re-adjust the servos with a screwdriver to fix this.  You can simply adjust the sketch slightly to get both wheels traveling the same speed.  While the screwdriver approach could be considered a hardware adjustment, the programming approach would be a software adjustment.  

Straightening the Shield-Bot’s Path

So, would your BOE Shield-Bot travel in an arc instead of in a straight line?  Top speed varies from one servo to the next, so one wheel is likely to rotate a little faster than the other, causing the BOE Shield-Bot to make a gradual turn.   

To correct this, the first step is to examine your BOE Shield-Bot’s forward travel for a longer period of time to see if it is curving, and which way, and how much.    

Example Sketch: ForwardTenSeconds

  • Open ForwardThreeSeconds and re-save it as ForwardTenSeconds.
  • Change delay(3000) to delay(10000), and update the title and comments too. 
  • Set the power switch to 1, upload the sketch, then disconnect the USB cable.
  • Place the Shield-Bot on a long stretch of smooth, bare floor.
  • Hold the Reset button down while you move the Power switch to 2, then let go.  
  • Press the Reset button, then watch closely to see if your BOE Shield-Bot veers to the right or left as it travels forward for ten seconds.
// Robotics with the BOE Shield - ForwardTenSeconds
// Make the BOE Shield-Bot roll forward for ten 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(10000);                              // ...for 10 seconds
 
  servoLeft.detach();                        // Stop sending servo signals
  servoRight.detach();
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

Your Turn – Adjusting Servo Speed to Straighten the BOE Shield-Bot’s Path

If your BOE Shield-Bot turns slightly when you want it to go straight forward, the solution is fairly simple.  Just slow down the faster wheel.  Remember from the servo transfer curve graph that you have best speed control over the servos in the 1400 to 1600 µs range.

Table 4-1: us Parameters in writeMicroseconds(us)
Top speed clockwiseLinear speed zone startsFull stopLinear speed zone endsTop speed counterclockwise
13001400150016001700

Let’s say that your BOE Shield-Bot gradually turns left.  That means the right wheel is turning faster than the left.  Since the left wheel is already going as fast as it possibly can, the right wheel needs to be slowed down to straighten out the robot’s path.  To slow it down, change the us parameter in servoRight.writeMicroseconds(us) to a value closer to 1500. First, try 1400. Is it still going too fast?  Raise it 1410.  Keep raising the parameter by 10 until the BOE Shield-Bot no longer curves to the left. If any adjustment overshoots ‘straight’ and your BOE Shield-Bot starts curving to the right instead, start decreasing the us parameter by smaller amounts.  Keep refining that us parameter until your BOE Shield-Bot goes straight forward.  This is called an iterative process, meaning that it takes repeated tries and refinements to get to the right value.

If your BOE Shield-Bot curved to the right instead of the left, it means you need to slow down the left wheel.  You’re starting with servoLeft.writeMicroseconds(1700) so the us parameter needs to decrease. Try 1600, then reduce by increments of 10 until it either goes straight or starts turning the other direction, and increase by 2 if you overshoot. 

  • Modify ForwardTenSeconds so that it makes your BOE Shield-Bot go straight forward. 
  • Use masking tape or a sticker to label each servo with the best us parameters for your writeMicroseconds(us) function calls.
  • If your BOE Shield-Bot already travels straight forward, try the modifications just discussed anyway, to see the effect.  It should cause the BOE Shield-Bot to travel in a curve instead of a straight line.

You might find that there’s an entirely different situation when you program your BOE Shield-Bot to roll backward.

  • Modify ForwardTenSeconds so that it makes the BOE Shield-Bot roll backward for ten seconds.
  • Repeat the test for a straight line.
  • Repeat the steps for correcting the us parameter for the writeMicroseconds function call to straighten the BOE Shield-Bot’s backward travel.