arnlJustLocalization.cpp

Example which does localization only, but provides servers for remote operation with MobileEyes.

00001 
00002 
00021 #include "Aria.h"
00022 #include "ArNetworking.h"
00023 #include "Arnl.h"
00024 
00025 #include "ArLocalizationTask.h"
00026 
00027 
00028 
00029 int main(int argc, char *argv[])
00030 {
00031   Aria::init();
00032   Arnl::init();
00033 
00034   ArRobot robot;
00035   ArServerBase server;
00036   ArArgumentParser parser(&argc, argv);
00037   parser.loadDefaultArguments();
00038   ArSimpleConnector simpleConnector(&parser);
00039   ArServerSimpleOpener simpleOpener (&parser);
00040   ArAnalogGyro gyro (&robot);
00041 
00042   parser.loadDefaultArguments();
00043   if (!Aria::parseArgs () || !parser.checkHelpAndWarnUnparsed())
00044   {
00045     Aria::logOptions ();
00046     Aria::exit (1);
00047   }
00048 
00049   ArSick sick;
00050   robot.addRangeDevice (&sick);
00051 
00052   ArSonarDevice sonarDev;
00053   robot.addRangeDevice (&sonarDev);
00054 
00055   ArBumpers bumpers;
00056   robot.addRangeDevice (&bumpers);
00057 
00058   // Use the "examples" directory as a place to keep map files
00059   char fileDir[1024];
00060   ArUtil::addDirectories (fileDir, sizeof (fileDir), Aria::getDirectory (),
00061             "examples");
00062 
00063   // Set up the map, this will look for files in the examples
00064   // directory (unless the file name starts with a / \ or .
00065   // You can take out the argument to look in the current directory
00066   ArMap arMap (fileDir);
00067   // set it up to ignore empty file names (otherwise the parseFile
00068   // on the config will fail)
00069   arMap.setIgnoreEmptyFileName (true);
00070 
00071 
00072   ArLocalizationManager locManager(&robot, &arMap);
00073   ArLocalizationTask locTask (&robot, &sick, &arMap);
00074   locManager.addLocalizationTask(&locTask);
00075 
00076 
00077 
00078   // Add log controls to configuration
00079   ArLog::addToConfig (Aria::getConfig ());
00080 
00081   // Open the server port
00082   if (!simpleOpener.open (&server, fileDir, 240))
00083   {
00084     ArLog::log (ArLog::Normal, "Could not open server port");
00085     exit (2);
00086   }
00087 
00088   // Connect to the robot
00089   if (!simpleConnector.connectRobot (&robot))
00090   {
00091     ArLog::log (ArLog::Normal, "Could not connect to robot... exiting");
00092     Aria::exit (3);
00093   }
00094 
00095   robot.enableMotors ();
00096   robot.clearDirectMotion ();
00097 
00098   // reset the simulator to its start position
00099   robot.com(ArCommands::SIM_RESET);
00100 
00101   simpleConnector.setupLaser (&sick);
00102 
00103   // run robot task cycle
00104   robot.runAsync (true);
00105 
00106   // Run SICK thread and try to connect
00107   sick.runAsync ();
00108   if (!sick.blockingConnect ())
00109   {
00110     ArLog::log (ArLog::Normal, "Couldn't connect to laser, exiting");
00111     Aria::exit (4);
00112   }
00113 
00114   ArUtil::sleep (300);
00115 
00116   // Forbidden regions from the map.
00117   ArForbiddenRangeDevice forbidden (&arMap);
00118   robot.addRangeDevice (&forbidden);
00119 
00120   // Objects that provide network services:
00121   ArServerInfoRobot serverInfoRobot (&server, &robot);
00122   ArServerInfoSensor serverInfoSensor (&server, &robot);
00123   ArServerInfoLocalization serverInfoLocalization (&server, &robot, &locManager);
00124   ArServerHandlerLocalization serverLocHandler (&server, &robot, &locManager);
00125   ArServerHandlerMap serverMap (&server, &arMap);
00126 
00127   // Drawing services in the map display:
00128   ArServerInfoDrawings drawings (&server);
00129   drawings.addRobotsRangeDevices (&robot);
00130 
00131   // Misc. "custom " commands:
00132   ArServerHandlerCommands commands (&server);
00133   ArServerSimpleComUC uCCommands (&commands, &robot);
00134   ArServerSimpleComMovementLogging loggingCommands (&commands, &robot);
00135   ArServerSimpleComGyro gyroCommands (&commands, &robot, &gyro);
00136   ArServerSimpleComLogRobotConfig configCommands (&commands, &robot);
00137 
00138   // Set up the possible modes for remote control from a client such as
00139   // MobileEyes:
00140 
00141   // To stop and remain stopped:
00142   ArServerModeStop modeStop (&server, &robot);
00143   modeStop.addAsDefaultMode ();
00144 
00145   // Disable the sonar automatically if stopped and enable when we
00146   // move
00147   ArSonarAutoDisabler sonarAutoDisabler (&robot);
00148 
00149   // Teleoperate by keyboard, joystick, etc:
00150   ArServerModeRatioDrive modeRatioDrive (&server, &robot);
00151 
00152   // Teloperation mode's configuration and special commands:
00153   modeRatioDrive.addControlCommands (&commands);
00154   modeRatioDrive.addToConfig (Aria::getConfig (), "Teleop settings");
00155 
00156   // Wander mode:
00157   ArServerModeWander
00158   modeWander (&server, &robot);
00159 
00160   // Prevent driving if localization is lost:
00161   ArActionLost actionLostRatioDrive (&locManager, NULL, &modeRatioDrive);
00162   modeRatioDrive.getActionGroup ()->addAction (&actionLostRatioDrive, 110);
00163 
00164   // Prevent wandering if lost:
00165   ArActionLost
00166   actionLostWander (&locManager, NULL, &modeWander);
00167   modeWander.getActionGroup ()->addAction (&actionLostWander, 110);
00168 
00169   // This provides a small table of interesting information for the client
00170   // to display to the operator:
00171      ArServerInfoStrings
00172      stringInfo (&server);
00173   Aria::getInfoGroup ()->addAddStringCallback (stringInfo.
00174                          getAddStringFunctor ());
00175 
00176   // Display localization score and more
00177   Aria::getInfoGroup ()->addStringDouble ("Localization Score", 8,
00178                     new ArRetFunctorC < double,
00179                     ArLocalizationTask > (&locTask,
00180                                   &ArLocalizationTask::
00181                                   getLocalizationScore),
00182                     "%.03f");
00183   Aria::getInfoGroup ()->addStringInt ("Laser Packet Count", 10,
00184                      new ArRetFunctorC < int, ArSick > (&sick,
00185                                     &ArSick::
00186                                     getSickPacCount));
00187 
00188 
00189 Aria::getInfoGroup ()->addStringInt ("Motor Packet Count", 10,
00190                      new ArConstRetFunctorC < int,
00191                      ArRobot > (&robot,
00192                         &ArRobot::getMotorPacCount));
00193 
00194 
00195   // Create service that allows client to change configuration parameters in ArConfig 
00196   ArServerHandlerConfig handlerConfig (&server, Aria::getConfig (),
00197                      Arnl::getTypicalDefaultParamFileName (),
00198                      Aria::getDirectory ());
00199 
00200 
00201 
00202   // Read in parameter files.
00203   Aria::getConfig ()->useArgumentParser (&parser);
00204   if (!Aria::getConfig ()->parseFile (Arnl::getTypicalParamFileName ()))
00205   {
00206     ArLog::log (ArLog::Normal, "Trouble loading configuration file, exiting");
00207     Aria::exit (5);
00208   }
00209 
00210   // Error if there is no map
00211   if (arMap.getFileName () == NULL || strlen (arMap.getFileName ()) <= 0)
00212   {
00213     ArLog::log(ArLog::Terse, "Warning, no map given. Use the -map command-line argument or modify the config using MobileEyes or by editing the parameter file.");
00214     ArLog::log(ArLog::Terse, "See the  documentation, including MAPPING.txt or SONAR_MAPPING.txt.");
00215   }
00216 
00217   ArLog::log (ArLog::Normal, "Directory for maps and file serving: %s", fileDir);
00218 
00219   ArLog::log (ArLog::Normal, "See the  README.txt for more information");
00220 
00221 
00222   robot.unlock();
00223 
00224 
00225   // Localize robot at home.
00226   locTask.localizeRobotAtHomeBlocking();
00227   
00228   server.runAsync();
00229 
00230   ArLog::log(ArLog::Normal, "Server now running on port %d. Press Control-C to exit.", server.getTcpPort());
00231 
00232 
00233   robot.waitForRunExit();
00234   Aria::exit(0);
00235 
00236 }
00237 

Generated on Thu Apr 23 10:19:34 2009 for ARNL by  doxygen 1.5.1