Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

robotConnectionCallbacks.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 
00032 /*
00033   This class contains the methods called by the connection callback functors.
00034 */
00035 class ConnHandler
00036 {
00037 public:
00038   // Constructor
00039   ConnHandler(ArRobot *robot);
00040 
00041   // Destructor, its just empty
00042   ~ConnHandler(void) {}
00043   
00044   // called if the connection was sucessfully made
00045   void connected(void);
00046 
00047   // called if the connection failed. stop the robot processing thread.
00048   void connectionFailed(void);
00049 
00050   // called when the connection is closed
00051   void disconnected(void);
00052 
00053   // called if the connection is lost due to an error
00054   void connectionLost(void);
00055 
00056 protected:
00057   // keep a robot pointer
00058   ArRobot *myRobot;
00059 
00060   // the functor callbacks
00061   ArFunctorC<ConnHandler> myConnectedCB;
00062   ArFunctorC<ConnHandler> myConnFailCB;
00063   ArFunctorC<ConnHandler> myDisconnectedCB;
00064   ArFunctorC<ConnHandler> myConnLostCB;
00065 };
00066 
00067 /* ConnHandler constructor. Initialize functor objects, then
00068  * add them as connection handler callbacks with the robot object.
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   // keep a robot pointer for use by the handler callback methods
00078   myRobot = robot;
00079 
00080   // add the callbacks to the robot
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 // just exit if the connection failed
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   // turn off sonar, turn off amigobot sounds
00098   myRobot->comInt(ArCommands::SONAR, 0);
00099   myRobot->comInt(ArCommands::SOUNDTOG, 0);
00100 }
00101 
00102 // disconnected
00103 void ConnHandler::disconnected(void)
00104 {
00105   ArLog::log(ArLog::Normal, "ConnHandler: Connection closed. Exiting the program.");
00106   exit(0);
00107 }
00108 
00109 // lost connection due to errror, exit
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   // Create a connection handler object, defined above, then try to connect to the
00131   // robot.
00132   ConnHandler ch(&robot);
00133   con.connectRobot(&robot);
00134   robot.runAsync(true);
00135 
00136   // Sleep for 10 seconds, then request that ArRobot stop its thread.
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 

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0