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.
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.
// 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 }
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 [1]that you have best speed control over the servos in the 1400 to 1600 µs range.
Top speed clockwise | Linear speed zone starts | Full stop | Linear speed zone ends | Top speed counterclockwise |
1300 | 1400 | 1500 | 1600 | 1700 |
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.
You might find that there’s an entirely different situation when you program your BOE Shield-Bot to roll backward.
The amount of time the BOE Shield-Bot spends rotating in place determines how far it turns. So, to tune a turn, all you need to do is adjust the delay function’s ms parameter to make it turn for a different amount of time.
Let’s say that the BOE Shield-Bot turns just a bit more than 90° (1/4 of a full circle). Try delay(580), or maybe even delay(560). If it doesn’t turn far enough, make it run longer by increasing the delay function’s ms parameter 20 ms at a time.
The smallest change that actually makes a difference is 20.
Servo control pulses are sent every 20 ms, so adjust your delay function call’s ms parameter in multiples of 20.
If you find yourself with one value slightly overshooting 90° and the other slightly undershooting, choose the value that makes it turn a little too far, then slow down the servos slightly. In the case of rotating left, both writeMicroseconds us parameters should be changed from 1300 to something closer to 1500. Start with 1400 and then gradually increase the values to slow both servos. For rotating right, start by changing the us parameters from 1700 to 1600, and then experiment with reducing in increments of 10 from there.
Carpeting can cause navigation errors.
If you are running your BOE Shield-Bot on carpeting, don’t expect perfect results! The way the carpet pile is laying can affect the way your BOE Shield-Bot travels, especially over long distances. For more precise maneuvers, use a smooth surface.
Links
[1] https://learn.parallax.com/207