Describe Me, Please

Let's take a look at the code.  You can adapt it to work with different sensors, and use your own WAV files to speak different phrases.

The Polite ActivityBot project includes the following libraries. The first three are Simple Libraries included with the Learn folder.  The fourth, abdrive, is part of the ActivityBot library.

#include "simpletools.h"                      // Include header files
#include "ping.h"
#include "wavplayer.h"                        // Must be 0.9 or later, 10-31-2013
#include "abdrive.h"                          // Must be 0.5.5 or later, 10-31-2013

 

The main function starts with two lines that were introduced in the ActivityBot tutorial.  The call to freqout plays a tone on the piezo speaker, so you can hear if the robot resets while driving due to low batteries.  The drive_setRampStep function prevents abrupt starts and stops.

int main()                                    // Main function
{
  freqout(4, 2000, 2000);                     // Reset indicator
  drive_setRampStep(10);                      // 10 ticks/sec / 20 ms

 

The next three lines were introduced in the SD Card Data and Play WAV files tutorial.  The first two prepare the SD card for use by the program. Then, wav_volume(10) sets the audio playback to maximum, but you can change it here to any number from 1 to 10.

  int DO = 22, CLK = 23, DI = 24, CS = 25;    // SD I/O pins
  sd_mount(DO, CLK, DI, CS);                  // Mount SD card
  wav_volume(10);                             // Set volume here, 1-10

 

Now the program enters an infinite loop, where the action happens.  To begin, the drive_ramp(128, 128)  sets the robot rolling straight forward at maximum encoder-controlled speed.  The next line lets the robot keep up that speed while the PING))) sensor connected to P8 only senses objects farther away than 20 cm, rechecking every 5 ms.

  while(1)
  {
    drive_ramp(128, 128);                      // Forward 128 ticks/second
    while(ping_cm(8) >= 20) pause(5);          // Drive until object in range

 

If the while(ping...) condition senses an object closer than 20 cm, the code progresses to drive_ramp(0, 0), which makes the robot stop.  Next, the wav_play call starts playing the file named in the text string, in this case excuseme.wav.  The 4000 ms pause that follows gives time for the obstacle to move out of the way.

    drive_ramp(0, 0);                          // Then stop
    wav_play("excuseme.wav");                  // Play wav file named in string
    pause(4000);                               // Time to speak & let obstacle move

 

After the pause, the PING))) sensor checks again to see if there is still an obstacle detected within 21 cm.  If true, the code block below it is executed. First it plays the named audio file, byebye.wav. The pause call just lets it finish playing before moving again.  It could speak while it is moving away, but that isn't so polite.

    if(ping_cm(8) < 21)                        // If obstacle is still there
    {  
      wav_play("byebye.wav");                  // Play wav file named in string
      pause(1000);                             // Time to speak before moving

 

Since the obstacle did not get out of the way (how rude!) the robot needs to turn away and find a clear path before moving again.  The statement int turn = rand() %2 is the code equivalent of flipping a coin—it randomly assigns the value 1 or 0 to the local variable turn. 

      int turn = rand() % 2;                   // Turn in a random direction

 

If turn equals 1, drive_speed(48, -48) makes the robot turn right.  If turn equals 0, it turns left instead with drive_speed(-48, 48).

      if(turn == 1)                            // If turn is odd (1)
        drive_speed(48, -48);                  // rotate right
      else                                     // else if turn is even (0)
        drive_speed(-48, 48);                  // rotate left

 

This inner while(ping_cm...) statement keeps the robot turning until the sensor no longer detects an object within 20 cm.  And since that's the end of the outer if(ping_cm....) condition, the code returns to the first statement beneath while(1) and the loop repeats.

      while(ping_cm(8) < 20);                  // Turn till object not in view
    }

 

Let's not forget this last else code block.  This one gets executed if, after stopping and saying "Excuse me, please!" the if(ping_cm(8) < 21) condition evaluates to false.  That would indicate that the obstacle did move out of the way (how polite!).  The ActivityBot responds by playing thankyou.wav and pausing for 2 seconds.  This is the end of the while(1) loop, so execution starts over with the robot once again driving forward and checking for new obstacles.

    else
    {
      wav_play("thankyou.wav");                // Play wav file named in string
      pause(1000);                             // Time to speak before moving
    }
  }
}

 


Did You Know?

You can record your own WAV files with Audacity, a free download for Mac and Windows.  See Creating Audio Files for WAV Players here.

To let your ActivityBot's own personality shine through, just use your own WAV files or use different WAV files than provided in the example code by replacing the file names in the play_wav function calls.  You might also want to lengthen or shorten the pause that follows each one.

Below is an example of an easy customization you can make to this project.

Meet "KittyBot!"  KittyBot uses 3 wav files from our Sound Library: meow.wav, purrs.wav, and hiss.wav.

See a short clip of KittyBot on Instagram (offsite)

Get creative with this project; don't be afraid to try animal noises, car noises, or any other sound effects you can come up with. The possibilities are endless!