HawkExample.C

00001 // File: HawkExample.C
00002 // Author: Josh Villbrandt <josh.villbrandt@usc.edu>
00003 // Date: April 2010
00004 
00005 #include "Robots/BeoHawk/computer/HawkExample.H"
00006 
00007 // ######################################################################
00008 HawkExample::HawkExample(std::string myName, int argc, char* argv[])
00009 : HawkAgent(myName, argc, argv) {
00010         // Help section
00011         helpTitle = "HawkExample";
00012         helpDescription = "A basic implementation of HawkAgent.";
00013         helpOptions.push_back("\t--test\t\t(12.3) Try changing this number!");
00014         
00015         // Parameters
00016         double test = loadDoubleParameter("test", 12.3);
00017         std::cout << "test: " << test << std::endl;
00018         
00019         // Set state
00020         state = INIT;
00021 }
00022 
00023 // ######################################################################
00024 void HawkExample::registerTopics() {
00025         registerPublisher("ExampleMessage");
00026         registerSubscription("ExampleMessage");
00027 }
00028 
00029 // ######################################################################
00030 bool HawkExample::scheduler() {
00031         if(state == INIT) {
00032                 sendExampleMessageOne();
00033                 return true;
00034         }
00035         else if(state == RECEIVED_FIRST_MESSAGE) {
00036                 sendExampleMessageTwo();
00037                 return true;
00038         }
00039         
00040         return false;
00041 }
00042 
00043 // ######################################################################
00044 void HawkExample::catchMessage(const HawkMessages::MessagePtr& hawkMessage, const Ice::Current&) {
00045         if(hawkMessage->ice_isA("::HawkMessages::ExampleMessage"))
00046         {
00047                 HawkMessages::ExampleMessagePtr msg = HawkMessages::ExampleMessagePtr::dynamicCast(hawkMessage);
00048                 std::cout << "Caught a message! " << msg->name << " says: " << msg->chatter << std::endl;
00049                 
00050                 // Update State / Wake Up Agent
00051                 if(msg->name != itsName && msg->chatter == "Hello?") {
00052                         state = RECEIVED_FIRST_MESSAGE;
00053                         wakeUp();
00054                 }
00055         }
00056 }
00057 
00058 // ######################################################################
00059 void HawkExample::sendExampleMessageOne() {
00060         // Create Message
00061         HawkMessages::ExampleMessagePtr msg = new HawkMessages::ExampleMessage;
00062     msg->name = itsName;
00063     msg->chatter = "Hello?";
00064     
00065     // Send Message
00066         publish("ExampleMessage", msg);
00067         
00068         // Update State
00069         state = WAITING;
00070 }
00071 
00072 // ######################################################################
00073 void HawkExample::sendExampleMessageTwo() {
00074         // Create Message
00075         HawkMessages::ExampleMessagePtr msg = new HawkMessages::ExampleMessage;
00076     msg->name = itsName;
00077     msg->chatter = "O hai! ^_^";
00078     
00079     // Send Message
00080         publish("ExampleMessage", msg);
00081         
00082         // Update State
00083         state = WAITING;
00084 }
00085 
00086 // ######################################################################
00087 int main (int argc, char* argv[]) {
00088         std::cout << "HawkExample: starting..." << std::endl;
00089         
00090         HawkExample agent("HawkExample", argc, argv);
00091         agent.start();
00092         
00093         std::cout << "HawkExample: all done!" << std::endl;
00094 }
Generated on Sun May 8 08:41:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3