play_song.cpp
Go to the documentation of this file.
00001 
00031 #include "create/create.h"
00032 
00033 #include <iostream>
00034 
00035 int main(int argc, char** argv) {
00036   // Select robot. Assume Create 2 unless argument says otherwise
00037   create::RobotModel model = create::RobotModel::CREATE_2;
00038   std::string port = "/dev/ttyUSB0";
00039   int baud = 115200;
00040   if (argc > 1 && std::string(argv[1]) == "create1") {
00041     model = create::RobotModel::CREATE_1;
00042     baud = 57600;
00043     std::cout << "Running driver for Create 1" << std::endl;
00044   }
00045   else {
00046     std::cout << "Running driver for Create 2" << std::endl;
00047   }
00048 
00049   // Construct robot object
00050   create::Create* robot = new create::Create(model);
00051 
00052   // Connect to robot
00053   if (robot->connect(port, baud))
00054     std::cout << "Connected to robot" << std::endl;
00055   else {
00056     std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl;
00057     return 1;
00058   }
00059 
00060   // Switch to Full mode
00061   robot->setMode(create::MODE_FULL);
00062 
00063   // Useful note defintions
00064   const uint8_t G = 55;
00065   const uint8_t AS = 58;
00066   const uint8_t DS = 51;
00067   const float half = 1.0f;
00068   const float quarter = 0.5f;
00069   const float dotted_eigth = 0.375f; 
00070   const float sixteenth = 0.125f;
00071 
00072   // Define a song
00073   const uint8_t song_len = 9;
00074   const uint8_t notes[song_len] = { G, G, G, DS, AS, G, DS, AS, G };
00075   const float durations[song_len] = { quarter, quarter, quarter, dotted_eigth, sixteenth, quarter, dotted_eigth, sixteenth, half };
00076   robot->defineSong(0, song_len, notes, durations);
00077 
00078   // Sleep to provide time for song to register
00079   usleep(1000000);
00080 
00081   std::cout << "Playing a song!" << std::endl;
00082 
00083   // Request to play the song we just defined
00084   robot->playSong(0); 
00085 
00086   // Expect the song to take about four seconds
00087   usleep(4500000);
00088 
00089   // Call disconnect to avoid leaving robot in Full mode
00090   robot->disconnect();
00091 
00092   // Clean up
00093   delete robot;
00094 
00095   std::cout << "Bye!" << std::endl;
00096   return 0;
00097 }


libcreate
Author(s): Jacob Perron
autogenerated on Thu Jun 6 2019 21:02:06