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

drawingsExampleWithRobot.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 #include <math.h>
00029 
00042 /* These are callbacks that respond to client requests for the drawings' 
00043  * geometry data. */
00044 void exampleHomeDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00045 void exampleDotsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00046 void exampleXDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00047 void exampleArrowsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00048 
00049 int main(int argc, char **argv)
00050 {
00051   Aria::init();
00052   ArRobot robot;
00053   ArServerBase server;
00054 
00055   ArArgumentParser parser(&argc, argv);
00056   ArSimpleConnector simpleConnector(&parser);
00057   ArServerSimpleOpener simpleOpener(&parser);
00058 
00059 
00060   // parse the command line... fail and print the help if the parsing fails
00061   // or if help was requested
00062   parser.loadDefaultArguments();
00063   if (!simpleConnector.parseArgs() || !simpleOpener.parseArgs() || 
00064       !parser.checkHelpAndWarnUnparsed())
00065   {    
00066     simpleConnector.logOptions();
00067     simpleOpener.logOptions();
00068     exit(1);
00069   }
00070 
00071   // Set up where we'll look for files such as config file, user/password file,
00072   // etc.
00073   char fileDir[1024];
00074   ArUtil::addDirectories(fileDir, sizeof(fileDir), Aria::getDirectory(), 
00075                          "ArNetworking/examples");
00076 
00077   // first open the server up
00078   if (!simpleOpener.open(&server, fileDir, 240))
00079   {
00080     if (simpleOpener.wasUserFileBad())
00081       printf("Error: Bad user/password/permissions file.\n");
00082     else
00083       printf("Error: Could not open server port. Use -help to see options.\n");
00084     exit(1);
00085   }
00086 
00087 
00088   // Devices
00089   ArAnalogGyro gyro(&robot);
00090 
00091   ArSonarDevice sonarDev;
00092   robot.addRangeDevice(&sonarDev);
00093 
00094   ArIRs irs;
00095   robot.addRangeDevice(&irs);
00096 
00097   ArBumpers bumpers;
00098   robot.addRangeDevice(&bumpers);
00099 
00100   ArSick sick(361, 180);
00101   robot.addRangeDevice(&sick);  
00102   
00103 
00104   ArServerInfoRobot serverInfoRobot(&server, &robot);
00105   ArServerInfoSensor serverInfoSensor(&server, &robot);
00106 
00107   // This is the service that provides drawing data to the client.
00108   ArServerInfoDrawings drawings(&server);
00109 
00110   // Convenience function that sets up drawings for all the robot's current
00111   // range devices (using default shape and color info)
00112   drawings.addRobotsRangeDevices(&robot);
00113 
00114   // Add our custom drawings
00115   drawings.addDrawing(
00116       //                shape:      color:               size:   layer:
00117       new ArDrawingData("polyLine", ArColor(255, 0, 0),  2,      49),
00118       "exampleDrawing_Home", 
00119       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleHomeDrawingNetCallback)
00120   );
00121   drawings.addDrawing(                                    
00122       new ArDrawingData("polyDots", ArColor(0, 255, 0), 250, 48),
00123       "exampleDrawing_Dots", 
00124       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleDotsDrawingNetCallback)
00125   );
00126   drawings.addDrawing(
00127       new ArDrawingData("polySegments", ArColor(0, 0, 0), 4, 52),
00128       "exampleDrawing_XMarksTheSpot", 
00129       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleXDrawingNetCallback)
00130   );
00131   drawings.addDrawing(
00132       new ArDrawingData("polyArrows", ArColor(255, 0, 255), 500, 100),
00133       "exampleDrawing_Arrows", 
00134       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleArrowsDrawingNetCallback)
00135   );
00136 
00137   // modes for moving the robot
00138   ArServerModeStop modeStop(&server, &robot);
00139   ArServerModeDrive modeDrive(&server, &robot);
00140   ArServerModeRatioDrive modeRatioDrive(&server, &robot);
00141   ArServerModeWander modeWander(&server, &robot);
00142   modeStop.addAsDefaultMode();
00143   modeStop.activate();
00144 
00145   
00146 
00147   // Connect to the robot.
00148   if (!simpleConnector.connectRobot(&robot))
00149   {
00150     printf("Error: Could not connect to robot... exiting\n");
00151     Aria::shutdown();
00152     return 1;
00153   }
00154 
00155   // set up the laser before handing it to the laser mode
00156   simpleConnector.setupLaser(&sick);
00157 
00158   robot.enableMotors();
00159 
00160   // start the robot cycle running in a background thread
00161   robot.runAsync(true);
00162 
00163   // start the laser processing cycle in a background thread
00164   sick.runAsync();
00165 
00166   // connect the laser if it was requested
00167   if (!simpleConnector.connectLaser(&sick))
00168   {
00169     printf("Error: Could not connect to laser... exiting\n");
00170     Aria::shutdown();
00171     return 1;
00172   }
00173 
00174   // log whatever we wanted to before the runAsync
00175   simpleOpener.checkAndLog();
00176 
00177   // run the server thread in the background
00178   server.runAsync();
00179 
00180   printf("Server is now running...\n");
00181 
00182 
00183   // Add a key handler mostly that windows can exit by pressing
00184   // escape, note that the key handler prevents you from running this program
00185   // in the background on Linux.
00186   ArKeyHandler *keyHandler;
00187   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00188   {
00189     keyHandler = new ArKeyHandler;
00190     Aria::setKeyHandler(keyHandler);
00191     robot.lock();
00192     robot.attachKeyHandler(keyHandler);
00193     robot.unlock();
00194     printf("To exit, press escape.\n");
00195   }
00196 
00197   robot.waitForRunExit();
00198  
00199 
00200   Aria::shutdown();
00201   exit(0);  
00202 }
00203 
00204 
00205 
00206 
00207 // Network callbacks for drawings' current geometry data:
00208 
00209 void exampleHomeDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00210   ArNetPacket reply;
00211 
00212   // 7 Vertices
00213   reply.byte4ToBuf(7);
00214 
00215   // Centered on 0,0.
00216   // X:                    Y:
00217   reply.byte4ToBuf(-500);  reply.byte4ToBuf(500);   // Vertex 1
00218   reply.byte4ToBuf(-500);  reply.byte4ToBuf(-500);  // Vertex 2
00219   reply.byte4ToBuf(500);   reply.byte4ToBuf(-500);  // Vertex 3
00220   reply.byte4ToBuf(500);   reply.byte4ToBuf(500);   // Vertex 4
00221   reply.byte4ToBuf(0);     reply.byte4ToBuf(1000);  // Vertex 5
00222   reply.byte4ToBuf(-500);  reply.byte4ToBuf(500);   // Vertex 6
00223   reply.byte4ToBuf(500);   reply.byte4ToBuf(500);   // Vertex 7
00224 
00225   client->sendPacketUdp(&reply);
00226 }
00227 
00228 void exampleDotsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00229   ArNetPacket reply;
00230 
00231   unsigned int tik = ArUtil::getTime() % 200;
00232   double t = tik / 5.0;
00233 
00234   // Three dots
00235   reply.byte4ToBuf(3);
00236 
00237   // Dot 1:
00238   reply.byte4ToBuf(3000);  // X coordinate (mm)
00239   reply.byte4ToBuf((int) (sin(t) * 1000));// Y
00240 
00241   // Dot 2:
00242   reply.byte4ToBuf(3500);  // X
00243   reply.byte4ToBuf((int) (sin(t+500) * 1000));// Y
00244 
00245   // Dot 3:
00246   reply.byte4ToBuf(4000);  // X
00247   reply.byte4ToBuf((int) (sin(t+1000) * 1000));// Y
00248 
00249   client->sendPacketUdp(&reply);
00250 }
00251 
00252 void exampleXDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00253   ArNetPacket reply;
00254 
00255   // X marks the spot. 2 line segments, so 4 vertices:
00256   reply.byte4ToBuf(4);
00257 
00258   // Segment 1:
00259   reply.byte4ToBuf(-4250); // X1
00260   reply.byte4ToBuf(250);   // Y1
00261   reply.byte4ToBuf(-3750); // X2
00262   reply.byte4ToBuf(-250);  // Y2
00263 
00264   // Segment 2:
00265   reply.byte4ToBuf(-4250); // X1
00266   reply.byte4ToBuf(-250);  // Y1
00267   reply.byte4ToBuf(-3750); // X2
00268   reply.byte4ToBuf(250);   // Y2
00269   
00270   client->sendPacketUdp(&reply);
00271 }
00272 
00273 void exampleArrowsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00274   // 1 Arrow that points at the robot
00275   ArNetPacket reply;
00276   reply.byte4ToBuf(1);
00277   reply.byte4ToBuf(0);      // Pos. X
00278   reply.byte4ToBuf(700);   // Pos. Y
00279   client->sendPacketUdp(&reply);
00280 }
00281 

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