Set Certain Speeds

Sometimes your ActivityBot might need to go a certain direction until it runs into an obstacle.  Setting the robot to go a certain speed, instead of a certain distance, is helpful for that kind of application.

We can do this with the abdrive library's drive_speed function.  It sets each drive servo to a certain speed, in encoder ticks per second.  Since there are 64 ticks in a wheel turn, if you set a wheel speed to 64 ticks per second, that’s one full turn per second.  Use positive values for forward, and negative values for reverse.

We’ll also try a drive_ramp function that lets the ActivityBot speed up and slow down gradually. This prevents its wheels from losing traction, and helps keep the robot from tipping forward from abrupt stops.

Speed Control Example

This example sets the robot to go straight ahead at 64 ticks/second until it has gone more than 200 ticks forward.  Then it executes a 45-degree right turn with the left wheel turning 32 ticks per second.  After that, it continues for another 200+ ticks at 128 ticks per second.

  • Click SimpleIDE’s Open Project button.
  • Open Speeds for Navigation.side from ...Documents\SimpleIDE\Learn\Examples\ActivityBot. 
  • Click the Load EEPROM & Run button.
  • Verify that it goes forward 200 (or maybe a few more) ticks, turns right by about 45-degrees, and goes 200 + ticks again.

 

How it Works

The program sets out by setting both servos to turn at 64 ticks per second with drive_speed(64, 64).  Since 64 ticks per second is one full wheel revolution per second, and since a pause(2000) follows it, we know the wheels should both turn at 64 ticks per second for two seconds.  In terms of distance, that should be 64 ticks/second x 2 seconds = 128 ticks.  Keep in mind that this may not be precise because the robot will have some momentum when it gets to drive_speed(0, 0).  Also keep in mind that if you do want a more precise distance traveled, just use the drive_goto function.

#include "simpletools.h"                      // simpletools library
#include "abdrive.h"                          // abdrive library

int main()                   
{
  drive_speed(64, 64);                       // Forward 64 tps for 2 s
  pause(2000);
  drive_speed(0, 0);

  drive_speed(26, 0);                        // Turn 26 tps for 1 s
  pause(1000);
  drive_speed(0, 0);

  drive_speed(128, 128);                     // Forward 128 tps for 1 s
  pause(1000);
  drive_speed(0, 0);
}

Next, drive_speed(26, 0) makes the left wheel turn at about 26 ticks per second, while the right wheel stays still.  Since it’s going for 1 second, the left wheel should turn about 26 ticks for a 45-degree turn.  drive_speed(0, 0) stops it after the 1 second.  The last maneuver should go about the same distance as the first maneuver, but it’s going twice as fast (128 ticks per second instead of 64) for half the time (1 second instead of 2 seconds).

 


Did You Know?

Velocity is speed, but it can be either positive (like forward) or negative (like backward).  Speed itself is just velocity without the sign.  So, if you are going backwards at a speed of 5 km/hr, you could also call that a velocity of -5 km/hr.

Here is an equation you can use to calculate distance (s) given velocity (v) and time (t).

  s = v × t

Example: A car goes 40 km/hr, how many km does  it travel in 0.25 hours?  Answer: s = v × t = 40 km/hr × 0.25 hr = 10 km.

Applied to the ActivityBot, if a wheel goes 64 ticks per second for 2 seconds, that’s v = 64 ticks per second and t = 2 seconds.  So:

  s = 64 ticks/second × 2 seconds = 128 ticks.

For a distance in centimeters, remember that a tick is 0.325 cm, so the wheel travels:

  s = 128 ticks × 0.325 cm/tick = 41.6 cm.

If you divide both sides of s = v × t by v, you get:

  t = s ÷ v

…and that’s really useful for picking a time to go a certain direction if you know the speed and distance you want the ActivityBot to go.

Example: The ActivityBot needs to go 192 ticks forward at a rate of 64 ticks per second. 

  • How long should that take?  Answer: t = 192 ticks ÷ 64 ticks/second = 3 seconds. 
  • How long of a pause is that?  Answer: The pause function is in milliseconds (thousandths of a second), so multiply 3 by 1000.  Your program would need pause(3000);

 

Try This

If you noticed some sudden starts and stops in the previous example programs, these can be cushioned with drive_ramp.  This function gradually speeds up or slows down the wheels. 

  • Try replacing drive_speed with drive_ramp, re-run and observe the difference.  Note that it'll go a little further because drive_ramp takes some time to reach cruising speed.

You can also use drive_speed to make the ActivityBot travel in curves.

  • Try setting one wheel to 80 ticks/second and the other one to 60 ticks/second.  What happens?
     
#include "simpletools.h"
#include "abdrive.h"

int main()
{
  drive_speed(80, 60);
  pause(2000);
  drive_speed(0, 0);
}

 

Your Turn

If your program ever needs to know how many ticks each wheel has traveled, you can use the drive_getTicks function.  Here is an example that records the number of ticks traveled before and after the Try This program that curved to the right.  Use Run with Terminal, and leave your robot plugged in.  (Try setting the robot on a stand or hold it above the desk so that it doesn't go to far while attached to the USB cable.)

Your challenge is to add two more maneuvers and display the distance traveled for each maneuver. 
Hint: You can calculate distance for each maneuver by using something like: distanceLeft = distLeft[1] - distLeft[0];

#include "simpletools.h"
#include "abdrive.h"

int distLeft[4], distRight[4];
 
int main()
{
  drive_getTicks(&distLeft[0], &distRight[0]);

  print("distLeft[0] = %d, distRight[0] = %d\n", distLeft[0], distRight[0]);

  drive_speed(80, 60);
  pause(2000);
  drive_speed(0, 0);

  drive_getTicks(&distLeft[1], &distRight[1]);

  print("distLeft[1] = %d, distRight[1] = %d\n", distLeft[1], distRight[1]);
}