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

serverDemo.cpp

Go to the documentation of this file.
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 #include "ArNetworking.h"
00028 
00058 int main(int argc, char **argv)
00059 {
00060   Aria::init();
00061   //ArLog::init(ArLog::StdErr, ArLog::Verbose);
00062   ArRobot robot;
00063 
00064   // our base server object
00065   ArServerBase server;
00066 
00067   ArArgumentParser parser(&argc, argv);
00068   ArSimpleConnector simpleConnector(&parser);
00069   ArServerSimpleOpener simpleOpener(&parser);
00070 
00071   // set up a gyro, if installed
00072   ArAnalogGyro gyro(&robot);
00073 
00074   // load the default arguments 
00075   parser.loadDefaultArguments();
00076 
00077   ArClientSwitchManager clientSwitchManager(&server, &parser);
00078 
00079   // parse the command line... fail and print the help if the parsing fails
00080   // or if the help was requested
00081   if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
00082   {    
00083     Aria::logOptions();
00084     Aria::exit(1);
00085   }
00086 
00087   // Set up where we'll look for files such as user/password 
00088   char fileDir[1024];
00089   ArUtil::addDirectories(fileDir, sizeof(fileDir), Aria::getDirectory(), 
00090                          "ArNetworking/examples");
00091 
00092   // first open the server up
00093   if (!simpleOpener.open(&server, fileDir, 240))
00094   {
00095     if (simpleOpener.wasUserFileBad())
00096       printf("Bad user/password/permissions file\n");
00097     else
00098       printf("Could not open server port\n");
00099     exit(1);
00100   }
00101 
00102   // Range devices:
00103   
00104   ArSonarDevice sonarDev;
00105   robot.addRangeDevice(&sonarDev);
00106 
00107   ArIRs irs;
00108   robot.addRangeDevice(&irs);
00109 
00110   ArBumpers bumpers;
00111   robot.addRangeDevice(&bumpers);
00112 
00113   ArSick sick(361, 180);
00114   robot.addRangeDevice(&sick);  
00115 
00116   
00117 
00118   // attach services to the server
00119   ArServerInfoRobot serverInfoRobot(&server, &robot);
00120   ArServerInfoSensor serverInfoSensor(&server, &robot);
00121   ArServerInfoDrawings drawings(&server);
00122   drawings.addRobotsRangeDevices(&robot);
00123 
00124   // this gets high-reflectance readings from the laser and draws them 
00125   // in a different color. (requires special reflector beacons.)
00126   ArLaserReflectorDevice reflector(&sick, &robot);
00127   drawings.addRangeDevice(&reflector);
00128 
00129 
00130   // modes for controlling robot movement
00131   ArServerModeStop modeStop(&server, &robot);
00132   ArServerModeDrive modeDrive(&server, &robot);
00133   ArServerModeRatioDrive modeRatioDrive(&server, &robot);
00134   ArServerModeWander modeWander(&server, &robot);
00135   modeStop.addAsDefaultMode();
00136   modeStop.activate();
00137 
00138   // set up the simple commands
00139   ArServerHandlerCommands commands(&server);
00140   ArServerSimpleComUC uCCommands(&commands, &robot);  // send commands directly to microcontroller
00141   ArServerSimpleComMovementLogging loggingCommands(&commands, &robot); // control debug logging
00142   ArServerSimpleComGyro gyroCommands(&commands, &robot, &gyro); // configure gyro
00143   ArServerSimpleComLogRobotConfig configCommands(&commands, &robot); // control more debug logging
00144   ArServerSimpleServerCommands serverCommands(&commands, &server); // control ArNetworking debug logging
00145   modeDrive.addControlCommands(&commands); // configure the drive modes (e.g. enable/disable safe drive)
00146 
00147   // Forward video if either ACTS or SAV server are running.
00148   // You can find out more about SAV and ACTS on our website
00149   // http://robots.activmedia.com. ACTS is for color tracking and is
00150   // a separate product. SAV just does software A/V transmitting and is
00151   // free to all our customers. Just run ACTS or SAV server before you
00152   // start this program and this class here will forward video from the
00153   // server to the client.
00154   ArHybridForwarderVideo videoForwarder(&server, "localhost", 7070);
00155   
00156   // Control a pan/tilt/zoom camera, if one is installed, and the video
00157   // forwarder was enabled above.
00158   ArPTZ *camera = NULL;
00159   ArServerHandlerCamera *handlerCamera = NULL;
00160   ArCameraCollection *cameraCollection = NULL;
00161   if (videoForwarder.isForwardingVideo())
00162   {
00163     bool invertedCamera = false;
00164     camera = new ArVCC4(&robot, invertedCamera, 
00165                         ArVCC4::COMM_UNKNOWN, true, true);
00166     camera->init();
00167 
00168     cameraCollection = new ArCameraCollection();
00169     cameraCollection->addCamera("Cam1", "VCC4", "Camera", "VCC4");
00170     handlerCamera = new ArServerHandlerCamera("Cam1", 
00171                                               &server, 
00172                                               &robot,
00173                                               camera, 
00174                                               cameraCollection);
00175   }
00176 
00177   // You can use this class to send a set of arbitrary strings 
00178   // for MobileEyes to display, this is just a small example
00179   ArServerInfoStrings stringInfo(&server);
00180   Aria::getInfoGroup()->addAddStringCallback(stringInfo.getAddStringFunctor());
00181   Aria::getInfoGroup()->addStringInt(
00182           "Motor Packet Count", 10, 
00183           new ArConstRetFunctorC<int, ArRobot>(&robot, 
00184                                                &ArRobot::getMotorPacCount));
00185   Aria::getInfoGroup()->addStringInt(
00186           "Laser Packet Count", 10, 
00187           new ArRetFunctorC<int, ArSick>(&sick, 
00188                                          &ArSick::getSickPacCount));
00189 
00190   
00191 
00192   // Connect to the robot.
00193   if (!simpleConnector.connectRobot(&robot))
00194   {
00195     printf("Could not connect to robot... exiting\n");
00196     Aria::shutdown();
00197     return 1;
00198     exit(1);
00199   }
00200 
00201   // set up the laser before handing it to the laser mode
00202   simpleConnector.setupLaser(&sick);
00203 
00204   // start the robot running, true means that if we lose connection the run thread stops
00205   robot.enableMotors();
00206   robot.runAsync(true);
00207 
00208   sick.runAsync();
00209 
00210   // connect the laser if it was requested
00211   if (!simpleConnector.connectLaser(&sick))
00212   {
00213     printf("Could not connect to laser... exiting\n");
00214     Aria::exit(2);
00215   }
00216 
00217   // log whatever we wanted to before the runAsync
00218   simpleOpener.checkAndLog();
00219   // now let it spin off in its own thread
00220   server.runAsync();
00221 
00222   printf("Server is now running...\n");
00223 
00224   // Add a key handler so that you can exit by pressing
00225   // escape. Note that a key handler prevents you from running
00226   // a program in the background on Linux, since it expects an 
00227   // active terminal to read keys from; remove this if you want
00228   // to run it in the background.
00229   ArKeyHandler *keyHandler;
00230   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00231   {
00232     keyHandler = new ArKeyHandler;
00233     Aria::setKeyHandler(keyHandler);
00234     robot.lock();
00235     robot.attachKeyHandler(keyHandler);
00236     robot.unlock();
00237     printf("To exit, press escape.\n");
00238   }
00239 
00240   clientSwitchManager.runAsync();
00241 
00242   robot.waitForRunExit();
00243   Aria::exit(0);
00244 }
00245 
00246 

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0