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

popupExample.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 
00027 #include "Aria.h"
00028 #include "ArNetworking.h"
00029 
00041 class SensorDetectPopup
00042 {
00043 public:
00044   SensorDetectPopup(ArRobot *robot, ArServerHandlerPopup *popupServer);
00045 protected:
00046   ArRobot *myRobot;
00047   ArServerHandlerPopup *myPopupServer;
00048   bool myPopupDisplayed;
00049   double myPrevObstacleAngle;
00050   bool myPrevObstacleAngleValid;
00051   ArFunctor2C<SensorDetectPopup, ArTypes::Byte4, int> *myPopupClosedCB;
00052 
00053   void popupClosed(ArTypes::Byte4 popupID, int button);
00054   void sensorTask(void)  ;
00055 };
00056 
00057 
00058 int main(int argc, char **argv)
00059 {
00060   Aria::init();
00061   ArRobot robot;
00062   ArSonarDevice sonar;
00063   ArSick sick;
00064   robot.addRangeDevice(&sonar);
00065   ArServerBase server;
00066 
00067   // Argument parser:
00068   ArArgumentParser parser(&argc, argv);
00069 
00070   // Connector and server opener:
00071   ArSimpleConnector simpleConnector(&parser);
00072   ArServerSimpleOpener simpleOpener(&parser);
00073 
00074   // Get command-line and other parameters
00075   parser.loadDefaultArguments();
00076   if(!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
00077   {
00078     Aria::logOptions();
00079     return 1;
00080   }
00081 
00082   // connect to the robot
00083   if(!simpleConnector.connectRobot(&robot))
00084   {
00085     ArLog::log(ArLog::Terse, "popupExample: Error, could not connect to robot.");
00086     return 1;
00087   }
00088   robot.runAsync(true);
00089 
00090   // connect to the laser
00091   sick.runAsync();
00092   if(!simpleConnector.connectLaser(&sick))
00093   {
00094     ArLog::log(ArLog::Normal, "popupExample: Note, could not connect to a SICK laser.");
00095   }
00096   else
00097   {
00098     robot.addRangeDevice(&sick);
00099   }
00100 
00101   // Open the server
00102   if(!simpleOpener.open(&server))
00103   {
00104     ArLog::log(ArLog::Terse, "popupExample: Error, could not open server.");
00105     return 1;
00106   }
00107   server.runAsync();
00108   ArLog::log(ArLog::Normal, "popupExample: Server running. Press control-C to exit.");
00109   ArLog::log(ArLog::Normal, "popupExample: Each time an obstacle is detected near the robot, a new popup message will be created. Connect with MobileEyes to see them.");
00110 
00111   // Sends robot position etc.
00112   ArServerInfoRobot robotInfoServer(&server, &robot);
00113 
00114   // This service sends drawings e.g. showing range device positions
00115   ArServerInfoDrawings drawingsServer(&server);
00116   drawingsServer.addRobotsRangeDevices(&robot);
00117 
00118   // This service can send messages to clients to display as popup dialogs:
00119   ArServerHandlerPopup popupServer(&server);
00120 
00121   // This object contains the robot sensor interpretation task and creates
00122   // popups:
00123   SensorDetectPopup(&robot, &popupServer);
00124 
00125   robot.enableMotors();
00126   robot.waitForRunExit();
00127 
00128 }
00129 
00130 SensorDetectPopup::SensorDetectPopup(ArRobot *robot, ArServerHandlerPopup *popupServer) :
00131   myRobot(robot),
00132   myPopupServer(popupServer),
00133   myPopupDisplayed(false),
00134   myPrevObstacleAngleValid(false)
00135 {
00136  myRobot->lock();
00137  myRobot->addSensorInterpTask("sensorDetectPopup", 50, new ArFunctorC<SensorDetectPopup>(this, &SensorDetectPopup::sensorTask));
00138  myPopupClosedCB = new ArFunctor2C<SensorDetectPopup, ArTypes::Byte4, int>(this, &SensorDetectPopup::popupClosed);
00139  myRobot->unlock();
00140 }
00141 
00142 void SensorDetectPopup::sensorTask(void)
00143 {
00144   // Basic obstacle detection
00145   
00146   if (myPopupDisplayed) return;
00147   double detectAngle, detectRange;
00148   detectRange = myRobot->checkRangeDevicesCurrentPolar(-90, 90, &detectAngle);
00149   if (detectRange > 0 && detectRange <= 500)
00150   {
00151     if(myPrevObstacleAngleValid && fabs(detectAngle - myPrevObstacleAngle) < 0.0001)
00152       return;
00153     ArLog::log(ArLog::Normal, "popupExample: New obstacle detected at range %f, angle %f. Displaying popup dialog on client...", detectRange, detectAngle);
00154 
00155     ArServerHandlerPopupInfo info("popupExample", // ID
00156               "Object Detected",                  // Title
00157               "A range sensor detected a reading within 0.5 meters of the robot.", // Message
00158               ArServerHandlerPopup::INFORMATION,  // Type
00159               0,                                  // Default button
00160               0,                                  // Cancel/escape button
00161               5,                                 // Timeout (sec.)
00162               NULL,                               // Timeout String
00163               "OK", "Acknowleged.",               // Button 0 Label/Acknowlegement
00164               "Turn Around", "Requested rotate...",   // Button 1 Label/Acknowlegement
00165               "Shut Down", "Shutting down server..."  // Button 2 Label/Acknowlegement
00166              );
00167     int id = myPopupServer->createPopup(&info, myPopupClosedCB);
00168     ArLog::log(ArLog::Normal, "\t...Created a popup with ID=%d", id);
00169     myPopupDisplayed = true;
00170     myPrevObstacleAngle = detectAngle;
00171     myPrevObstacleAngleValid = true;
00172   }
00173 }
00174 
00175 void SensorDetectPopup::popupClosed(ArTypes::Byte4 popupID, int button)
00176 {
00177   // A client closed the popup
00178   ArLog::log(ArLog::Normal, "popupExample: a client closed popup dialog window with id=%d. Button=%d...", popupID, button);
00179   myPopupDisplayed = false;
00180 
00181   if(button < 0)
00182   {
00183     ArLog::log(ArLog::Normal, "\t...popup timed out or closed due to an error.");
00184     return;
00185   }
00186 
00187   if (button == 0)
00188   {
00189     ArLog::log(ArLog::Normal, "\t...OK pressed.");
00190     return;
00191   }
00192 
00193   if(button == 1)
00194   {
00195     ArLog::log(ArLog::Normal, "\t...180 degree rotate requested.");
00196     myRobot->lock();
00197     myRobot->setDeltaHeading(180);
00198     myRobot->unlock();
00199     return;
00200   }
00201 
00202   if(button == 2)
00203   {
00204     ArLog::log(ArLog::Normal, "\t...exit requested.");
00205     myRobot->stopRunning();
00206     Aria::shutdown();
00207     Aria::exit(0);
00208   }
00209 }
00210 
00211 

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