00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "Aria.h"
00027
00032
00033
00034
00035 class ConnHandler
00036 {
00037 public:
00038
00039 ConnHandler(ArRobot *robot);
00040
00041
00042 ~ConnHandler(void) {}
00043
00044
00045 void connected(void);
00046
00047
00048 void connectionFailed(void);
00049
00050
00051 void disconnected(void);
00052
00053
00054 void connectionLost(void);
00055
00056 protected:
00057
00058 ArRobot *myRobot;
00059
00060
00061 ArFunctorC<ConnHandler> myConnectedCB;
00062 ArFunctorC<ConnHandler> myConnFailCB;
00063 ArFunctorC<ConnHandler> myDisconnectedCB;
00064 ArFunctorC<ConnHandler> myConnLostCB;
00065 };
00066
00067
00068
00069
00070 ConnHandler::ConnHandler(ArRobot *robot) :
00071 myConnectedCB(this, &ConnHandler::connected),
00072 myConnFailCB(this, &ConnHandler::connectionFailed),
00073 myDisconnectedCB(this, &ConnHandler::disconnected),
00074 myConnLostCB(this, &ConnHandler::connectionLost)
00075
00076 {
00077
00078 myRobot = robot;
00079
00080
00081 myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
00082 myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
00083 myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
00084 myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
00085 }
00086
00087
00088 void ConnHandler::connectionFailed(void)
00089 {
00090 ArLog::log(ArLog::Normal, "ConnHandler: Connection failed. Exiting the program.");
00091 exit(1);
00092 }
00093
00094 void ConnHandler::connected(void)
00095 {
00096 ArLog::log(ArLog::Normal, "ConnHandler: Connected. Turning off sonar,");
00097
00098 myRobot->comInt(ArCommands::SONAR, 0);
00099 myRobot->comInt(ArCommands::SOUNDTOG, 0);
00100 }
00101
00102
00103 void ConnHandler::disconnected(void)
00104 {
00105 ArLog::log(ArLog::Normal, "ConnHandler: Connection closed. Exiting the program.");
00106 exit(0);
00107 }
00108
00109
00110 void ConnHandler::connectionLost(void)
00111 {
00112 ArLog::log(ArLog::Normal, "ConnHandler: Lost connection due to an error! Exiting the program!");
00113 exit(1);
00114 }
00115
00116
00117
00118 int main(int argc, char **argv)
00119 {
00120 Aria::init();
00121 ArRobot robot;
00122 ArArgumentParser argParser(&argc, argv);
00123 ArSimpleConnector con(&argParser);
00124 if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00125 {
00126 Aria::logOptions();
00127 return 1;
00128 }
00129
00130
00131
00132 ConnHandler ch(&robot);
00133 con.connectRobot(&robot);
00134 robot.runAsync(true);
00135
00136
00137 ArLog::log(ArLog::Normal, "Sleeping for 10 seconds...");
00138 ArUtil::sleep(10000);
00139 ArLog::log(ArLog::Normal, "...requesting that the robot thread exit, then shutting down ARIA and exiting.");
00140 robot.stopRunning();
00141 Aria::shutdown();
00142 return 0;
00143 }
00144