serverDemo.cpp

Example ArNetworking server providing teleoperation, sonar data, control the camera, etc.

This is a basic ArNetworking server. It connects to a robot or simulator, including, if available, IRs, gyro, and bumpers. Give the option "-connectLaser" on the command line to enable the laser rangefinder, if available.

Run "./serverDemo -help" for a full list of command line options.

Once running, connect to this server with a a client such as MobileEyes.

This server provides the following services:

Note that this program requires a terminal to run -- i.e. you can't run it in the background in Linux. To modify it to allow that, remove the key handler code in main().

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

Generated on Fri Jul 31 12:37:28 2009 for ArNetworking by  doxygen 1.4.7