Turn your ActivityBot into an adorable kitty! This project equips your ActivityBot to act like a cat would when it encounters people or objects while roaming around. The KittyBot uses the PING))) Ultrasonic Distance Sensor to check for obstacles within range. When it detects something, it stops and plays WAV files stored on a microSD card, selecting different files if the obstacle moves out of the way or not.
Three WAV files with feline responses are provided, but you can record your own to better reflect your KittyBot's unique personality! Of course, you could modify this robot to respond with music, sound effects, animal calls, or whatever you'd like. Find all the project WAV files in our sound library [1], or you could reformat existing audio files to WAVs [2], or make your own WAVs [3] using free Audacity software.
Requires parts not included in the base ActivityBot Robot or ActivityBot 360° Robot kit and additional accessories; see parts list below. It is highly recommended to finish the entire ActivityBot for C tutorial series before trying this project.
*Check your ActivityBot kit; a microSD card was included with some versions as a bonus and it is now sold as an optional accessory.
Did you know there is an Arduino version of this project that uses our Arduino Shield Robot? Click here to check it out [9]!
The Project archive includes updated libraries: wavplayer 0.9 and abdrive 0.5.5. If you run the project from its un-zipped archive using SimpleIDE 0-9-43 or later, it will use these libraries. Get the latest SimpleIDE software [10], the latest Learn folder & Simple Libraries [11], and ActivityBot Libraries [12].
You will need to transfer the three WAV files from the project folder to the microSD card that comes with the ActivityBot kit. They are named meow.wav, hiss.wav, and purrs.wav. If you have a microSD/USB card adapter, you can use that to load the files onto the SD card directly from your computer. If you don't, just follow the directions below.
Now, you are ready to run the program!
The ActivityBot should roam around the room. If you stand in front of it, it should come to a stop and meow. If you step out of the way, it should purr and continue forward. If you do not move, it should hiss, then turn away and go another direction.
Remember to turn the speaker on! If the blue light doesn't light up on your speaker, it needs charging before use.
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 [14] and Play WAV files [15] 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 } } }
Don't like cats? Or perhaps you just want to try out some different sounds?
The Polite ActivityBot is a variation of the same code base, with different WAV files for the three behaviors. Instead of meowing, purring, and hissing, Polite ActivityBot uses its words to nicely request that you move out of its way.
Download the Polite ActivityBot code [18]
The behavior code itself is exactly the same, so if you want an explanation of how Polite ActivityBot works just look back one page to "How KittyBot Works". The code download above already has the new WAV file names in it, you just need to upload the included WAV files to your sd Card. To check out Polite ActivityBot in action, watch the short video below.
/* Polite ActivityBot.c Version 1.0, 10/31/2013 http://learn.parallax.com/activitybot Requires abdrive version 0.5.5 and wavplayer 0.9 or above Drive forward until object detected with PING))) sensor on P8, within 20 cm. Stop and play 1st audio response from SD, then recheck for object. If it moved, play 2nd audio response and continue. If it did not move, play 3rd audio response, rotate left or right at random until clear path is detected, then drive forward. */ #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 int main() // Main function { freqout(4, 2000, 2000); // Reset indicator drive_setRampStep(10); // 10 ticks/sec / 20 ms 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 while(1) { drive_ramp(128, 128); // Forward 128 ticks/second while(ping_cm(8) >= 20) pause(5); // Drive until object in range drive_ramp(0, 0); // Then stop wav_play("excuseme.wav"); // Play wav file named in string pause(4000); // Time to speak & let obstacle move 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 int turn = rand() % 2; // Turn in a random direction 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 while(ping_cm(8) < 20); // Turn till object not in view } else { wav_play("thankyou.wav"); // Play wav file named in string pause(1000); // Time to speak before moving } } }
Links
[1] https://learn.parallax.com/reference/sound-library
[2] https://learn.parallax.com/reference/formatting-audio-files-wav-players
[3] https://learn.parallax.com/reference/create-audio-files-wav-players
[4] http://www.parallax.com/product/32500
[5] http://www.parallax.com/product/32600
[6] https://www.parallax.com/product/900-00018
[7] https://www.parallax.com/product/8-gb-microsdhc-card/
[8] https://www.parallax.com/product/725-32905
[9] https://learn.parallax.com/project/arduino-kittybot
[10] https://learn.parallax.com/propeller-c-set-simpleide
[11] https://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
[12] https://learn.parallax.com/activitybot/software-and-programming
[13] https://learn.parallax.com/sites/default/files/Files/Docs/Projects/KittyBot/KittyBot-Projects.zip
[14] http://learn.parallax.com/propeller-c-simple-devices/sd-card-data
[15] http://learn.parallax.com/propeller-c-simple-devices/play-wav-files
[16] http://learn.parallax.com/project/arduino-kittybot
[17] http://learn.parallax.com/reference/create-audio-files-wav-players
[18] https://learn.parallax.com/sites/default/files/Files/Docs/Projects/Polite-ActivityBot/Polite-ActivityBot.zip