00001 #include "Aria.h"
00002
00007
00008
00009
00010 class ConnHandler
00011 {
00012 public:
00013
00014 ConnHandler(ArRobot *robot);
00015
00016
00017 ~ConnHandler(void) {}
00018
00019
00020 void connected(void);
00021
00022
00023 void connectionFailed(void);
00024
00025
00026 void disconnected(void);
00027
00028
00029 void connectionLost(void);
00030
00031 protected:
00032
00033 ArRobot *myRobot;
00034
00035
00036 ArFunctorC<ConnHandler> myConnectedCB;
00037 ArFunctorC<ConnHandler> myConnFailCB;
00038 ArFunctorC<ConnHandler> myDisconnectedCB;
00039 ArFunctorC<ConnHandler> myConnLostCB;
00040 };
00041
00042
00043
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
00053 myRobot = robot;
00054
00055
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
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
00073 myRobot->comInt(ArCommands::SONAR, 0);
00074 myRobot->comInt(ArCommands::SOUNDTOG, 0);
00075 }
00076
00077
00078 void ConnHandler::disconnected(void)
00079 {
00080 ArLog::log(ArLog::Normal, "ConnHandler: Connection closed. Exiting the program.");
00081 exit(0);
00082 }
00083
00084
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
00106
00107 ConnHandler ch(&robot);
00108 con.connectRobot(&robot);
00109 robot.runAsync(true);
00110
00111
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