How KittyBot Works

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

The KittyBot project includes the following 4 libraries (see below code).

If you're using the KittyBot program for the ActivityBot 360­° Robot, its fourth library #include shown below will be abdrive360 not abdrive. Every other part of this program should look the same regardless of which ActivityBot you use.

#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 meow.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("meow.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, hiss.wav. The pause call just lets it finish playing before moving again.  It could hiss while it is moving away, but this is a kitty trying to make a point!

    if(ping_cm(8) < 21)                        // If obstacle is still there
    {  
      wav_play("hiss.wav");                  // Play wav file named in string
      pause(1000);                             // Time to hiss 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 meowing the if(ping_cm(8) < 21) condition evaluates to false.  That would indicate that the obstacle did move out of the way.  The ActivityBot responds by playing purrs.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("purrs.wav");                // Play wav file named in string
      pause(1000);                             // Time to speak before moving
    }
  }
}

 


Did You Know?

  • There is an Arduino version of this project! Comparing the two will help you understand the similarities and differences between programming an Arduino and a Propeller.
  • 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.
  • 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!