00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00068 ArArgumentParser parser(&argc, argv);
00069
00070
00071 ArSimpleConnector simpleConnector(&parser);
00072 ArServerSimpleOpener simpleOpener(&parser);
00073
00074
00075 parser.loadDefaultArguments();
00076 if(!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
00077 {
00078 Aria::logOptions();
00079 return 1;
00080 }
00081
00082
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
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
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
00112 ArServerInfoRobot robotInfoServer(&server, &robot);
00113
00114
00115 ArServerInfoDrawings drawingsServer(&server);
00116 drawingsServer.addRobotsRangeDevices(&robot);
00117
00118
00119 ArServerHandlerPopup popupServer(&server);
00120
00121
00122
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
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",
00156 "Object Detected",
00157 "A range sensor detected a reading within 0.5 meters of the robot.",
00158 ArServerHandlerPopup::INFORMATION,
00159 0,
00160 0,
00161 5,
00162 NULL,
00163 "OK", "Acknowleged.",
00164 "Turn Around", "Requested rotate...",
00165 "Shut Down", "Shutting down server..."
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
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