drawingsExample.cpp

Example showing how to draw custom graphics in clients such as MobileEyes

This is an example server that shows how to draw arbitrary figures in a client (e.g. MobileEyes) via ArServerInfoDrawings. It draws some lines, "arrows", and moving dots with various sizes and colors. You can use these drawings for debugging or visualization, for example, to represent sensor readings. In fact, specific support for ArRobot, ArSick, ArSonarDevice, ArIRs and ArBumpers are built in to ArServerInfoDrawings: see drawingsExampleWithRobot.cpp or serverDemo.cpp.

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

Generated on Fri Jul 31 12:37:28 2009 for ArNetworking by  doxygen 1.4.7