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

drawingsExample.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 
00030 
00046 /* These are callbacks that respond to client requests for the drawings' 
00047  * geometry data. */
00048 void exampleHomeDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00049 void exampleDotsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00050 void exampleXDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00051 void exampleArrowsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt);
00052 
00053 int main(int argc, char **argv)
00054 {
00055   Aria::init();
00056   ArServerBase server;
00057 
00058   ArArgumentParser parser(&argc, argv);
00059   ArServerSimpleOpener simpleOpener(&parser);
00060 
00061   // parse the command line... fail and print the help if the parsing fails
00062   // or if help was requested
00063   parser.loadDefaultArguments();
00064   if (!simpleOpener.parseArgs() || !parser.checkHelpAndWarnUnparsed())
00065   {    
00066     simpleOpener.logOptions();
00067     exit(1);
00068   }
00069 
00070 
00071   // first open the server up
00072   if (!simpleOpener.open(&server))
00073   {
00074     if (simpleOpener.wasUserFileBad())
00075       printf("Error: Bad user/password/permissions file.\n");
00076     else
00077       printf("Error: Could not open server port. Use -help to see options.\n");
00078     exit(1);
00079   }
00080 
00081 
00082   // This is the service that provides drawing data to the client.
00083   ArServerInfoDrawings drawings(&server);
00084 
00085   // Add our custom drawings
00086   drawings.addDrawing(
00087       //                shape:      color:               size:   layer:
00088       new ArDrawingData("polyLine", ArColor(255, 0, 0),  2,      49),
00089       "exampleDrawing_Home", 
00090       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleHomeDrawingNetCallback)
00091   );
00092   drawings.addDrawing(                                    
00093       new ArDrawingData("polyDots", ArColor(0, 255, 0), 250, 48),
00094       "exampleDrawing_Dots", 
00095       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleDotsDrawingNetCallback)
00096   );
00097   drawings.addDrawing(
00098       new ArDrawingData("polySegments", ArColor(0, 0, 0), 4, 52),
00099       "exampleDrawing_XMarksTheSpot", 
00100       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleXDrawingNetCallback)
00101   );
00102   drawings.addDrawing(
00103       new ArDrawingData("polyArrows", ArColor(255, 0, 255), 500, 100),
00104       "exampleDrawing_Arrows", 
00105       new ArGlobalFunctor2<ArServerClient*, ArNetPacket*>(&exampleArrowsDrawingNetCallback)
00106   );
00107 
00108 
00109   // log whatever we wanted to before the runAsync
00110   simpleOpener.checkAndLog();
00111 
00112   // run the server thread in the background
00113   server.runAsync();
00114 
00115   printf("Server is now running...\n");
00116 
00117 
00118   // Add a key handler mostly that windows can exit by pressing
00119   // escape, note that the key handler prevents you from running this program
00120   // in the background on Linux.
00121   ArKeyHandler *keyHandler;
00122   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00123   {
00124     keyHandler = new ArKeyHandler;
00125     Aria::setKeyHandler(keyHandler);
00126     printf("To exit, press escape.\n");
00127   }
00128 
00129  
00130   // run forever
00131   while(true) ArUtil::sleep(10000);
00132 
00133   Aria::shutdown();
00134   exit(0);  
00135 }
00136 
00137 
00138 
00139 
00140 // Network callbacks for drawings' current geometry data:
00141 
00142 void exampleHomeDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00143   ArNetPacket reply;
00144 
00145   // 7 Vertices
00146   reply.byte4ToBuf(7);
00147 
00148   // Centered on 0,0.
00149   // X:                    Y:
00150   reply.byte4ToBuf(-500);  reply.byte4ToBuf(500);   // Vertex 1
00151   reply.byte4ToBuf(-500);  reply.byte4ToBuf(-500);  // Vertex 2
00152   reply.byte4ToBuf(500);   reply.byte4ToBuf(-500);  // Vertex 3
00153   reply.byte4ToBuf(500);   reply.byte4ToBuf(500);   // Vertex 4
00154   reply.byte4ToBuf(0);     reply.byte4ToBuf(1000);  // Vertex 5
00155   reply.byte4ToBuf(-500);  reply.byte4ToBuf(500);   // Vertex 6
00156   reply.byte4ToBuf(500);   reply.byte4ToBuf(500);   // Vertex 7
00157 
00158   client->sendPacketUdp(&reply);
00159 }
00160 
00161 void exampleDotsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00162   ArNetPacket reply;
00163 
00164   unsigned int tik = ArUtil::getTime() % 200;
00165   double t = tik / 5.0;
00166 
00167   // Three dots
00168   reply.byte4ToBuf(3);
00169 
00170   // Dot 1:
00171   reply.byte4ToBuf(3000);  // X coordinate (mm)
00172   reply.byte4ToBuf((int) (sin(t) * 1000));// Y
00173 
00174   // Dot 2:
00175   reply.byte4ToBuf(3500);  // X
00176   reply.byte4ToBuf((int) (sin(t+500) * 1000));// Y
00177 
00178   // Dot 3:
00179   reply.byte4ToBuf(4000);  // X
00180   reply.byte4ToBuf((int) (sin(t+1000) * 1000));// Y
00181 
00182   client->sendPacketUdp(&reply);
00183 }
00184 
00185 void exampleXDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00186   ArNetPacket reply;
00187 
00188   // X marks the spot. 2 line segments, so 4 vertices:
00189   reply.byte4ToBuf(4);
00190 
00191   // Segment 1:
00192   reply.byte4ToBuf(-4250); // X1
00193   reply.byte4ToBuf(250);   // Y1
00194   reply.byte4ToBuf(-3750); // X2
00195   reply.byte4ToBuf(-250);  // Y2
00196 
00197   // Segment 2:
00198   reply.byte4ToBuf(-4250); // X1
00199   reply.byte4ToBuf(-250);  // Y1
00200   reply.byte4ToBuf(-3750); // X2
00201   reply.byte4ToBuf(250);   // Y2
00202   
00203   client->sendPacketUdp(&reply);
00204 }
00205 
00206 void exampleArrowsDrawingNetCallback(ArServerClient* client, ArNetPacket* requestPkt) {
00207   // 1 Arrow that points at the robot
00208   ArNetPacket reply;
00209   reply.byte4ToBuf(1);
00210   reply.byte4ToBuf(0);      // Pos. X
00211   reply.byte4ToBuf(700);   // Pos. Y
00212   client->sendPacketUdp(&reply);
00213 }
00214 

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