robotConnectionCallbacks.cpp

Demonstrates how callbacks can be registered with ArRobot to be called when a connection succeeds or fails.

00001 #include "Aria.h"
00002 
00007 /*
00008   This class contains the methods called by the connection callback functors.
00009 */
00010 class ConnHandler
00011 {
00012 public:
00013   // Constructor
00014   ConnHandler(ArRobot *robot);
00015 
00016   // Destructor, its just empty
00017   ~ConnHandler(void) {}
00018   
00019   // called if the connection was sucessfully made
00020   void connected(void);
00021 
00022   // called if the connection failed. stop the robot processing thread.
00023   void connectionFailed(void);
00024 
00025   // called when the connection is closed
00026   void disconnected(void);
00027 
00028   // called if the connection is lost due to an error
00029   void connectionLost(void);
00030 
00031 protected:
00032   // keep a robot pointer
00033   ArRobot *myRobot;
00034 
00035   // the functor callbacks
00036   ArFunctorC<ConnHandler> myConnectedCB;
00037   ArFunctorC<ConnHandler> myConnFailCB;
00038   ArFunctorC<ConnHandler> myDisconnectedCB;
00039   ArFunctorC<ConnHandler> myConnLostCB;
00040 };
00041 
00042 /* ConnHandler constructor. Initialize functor objects, then
00043  * add them as connection handler callbacks with the robot object.
00044 */
00045 ConnHandler::ConnHandler(ArRobot *robot) :
00046   myConnectedCB(this, &ConnHandler::connected),  
00047   myConnFailCB(this, &ConnHandler::connectionFailed),
00048   myDisconnectedCB(this, &ConnHandler::disconnected),
00049   myConnLostCB(this, &ConnHandler::connectionLost)
00050 
00051 {
00052   // keep a robot pointer for use by the handler callback methods
00053   myRobot = robot;
00054 
00055   // add the callbacks to the robot
00056   myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
00057   myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
00058   myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
00059   myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
00060 }
00061 
00062 // just exit if the connection failed
00063 void ConnHandler::connectionFailed(void)
00064 {
00065   ArLog::log(ArLog::Normal, "ConnHandler: Connection failed. Exiting the program.");
00066   exit(1);
00067 }
00068 
00069 void ConnHandler::connected(void)
00070 {
00071   ArLog::log(ArLog::Normal, "ConnHandler: Connected. Turning off sonar,");
00072   // turn off sonar, turn off amigobot sounds
00073   myRobot->comInt(ArCommands::SONAR, 0);
00074   myRobot->comInt(ArCommands::SOUNDTOG, 0);
00075 }
00076 
00077 // disconnected
00078 void ConnHandler::disconnected(void)
00079 {
00080   ArLog::log(ArLog::Normal, "ConnHandler: Connection closed. Exiting the program.");
00081   exit(0);
00082 }
00083 
00084 // lost connection due to errror, exit
00085 void ConnHandler::connectionLost(void)
00086 {
00087   ArLog::log(ArLog::Normal, "ConnHandler: Lost connection due to an error! Exiting the program!");
00088   exit(1);
00089 }
00090 
00091 
00092 
00093 int main(int argc, char **argv) 
00094 {
00095   Aria::init();
00096   ArRobot robot;
00097   ArArgumentParser argParser(&argc, argv);
00098   ArSimpleConnector con(&argParser);
00099   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00100   {
00101     Aria::logOptions();
00102     return 1;
00103   }
00104 
00105   // Create a connection handler object, defined above, then try to connect to the
00106   // robot.
00107   ConnHandler ch(&robot);
00108   con.connectRobot(&robot);
00109   robot.runAsync(true);
00110 
00111   // Sleep for 10 seconds, then request that ArRobot stop its thread.
00112   ArLog::log(ArLog::Normal, "Sleeping for 10 seconds...");
00113   ArUtil::sleep(10000);
00114   ArLog::log(ArLog::Normal, "...requesting that the robot thread exit, then shutting down ARIA and exiting.");
00115   robot.stopRunning();
00116   Aria::shutdown();
00117   return 0;
00118 }
00119 

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7