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 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include <cstdlib>
00037 #include <sstream>
00038 #include <iostream>
00039 #include <time.h>
00040 #include <stdlib.h>
00041 #include "ros/ros.h"
00042 #include "micros_hopfield/plan.h"
00043 #include "plan_visualization.h"
00044 
00045 using namespace std;
00046 
00047 int main(int argc, char **argv)
00048 {
00049   
00050   float g_FlyingHeight=650.0/90;
00051   
00052   srand((int)time(0));
00053   
00054   
00055   if (argc != 2)
00056   {
00057     ROS_INFO("usage: Plan_client NodeID");
00058     return 1;
00059   }
00060 
00061   
00062   int NodeID=atoi(argv[1]);
00063   if (NodeID <=0)
00064   {
00065     ROS_INFO("usage: Plan_client NodeID (NodeID must greater than 0)");
00066     return 1;
00067   }  
00068 
00069   char Name[255];
00070   sprintf(Name,"X%d",NodeID);
00071   ros::init(argc, argv, Name); 
00072  
00073   ros::NodeHandle n;
00074 
00075   
00076   ros::ServiceClient client = n.serviceClient<micros_hopfield::plan>("Planner");
00077   micros_hopfield::plan srv;
00078         
00079   
00080   InitMarkerPublisher();
00081 
00082   
00083   float r=1.0,g=1.0,b=1.0;
00084   GetStoreColor(NodeID,r,g,b);
00085 
00086   
00087   InitTail(100+NodeID,r,g,b);
00088 
00089   
00090   srv.request.sx = 1;
00091   srv.request.sy = 1;
00092   bool Finded=false;
00093 
00094    while (1)
00095   {
00096          srv.request.ex = int(1200.0*rand()/RAND_MAX);
00097          srv.request.ey = int(1200.0*rand()/RAND_MAX);
00098         
00099          Draw_Sphere(200+NodeID,srv.request.ex,srv.request.ey,g_FlyingHeight,4,r,g,b,0.5);
00100 
00101          ROS_INFO("Plan from (%ld,%ld) to (%ld,%ld)",   (long int)srv.request.sx, (long int)srv.request.sy,
00102                                                         (long int)srv.request.ex, (long int)srv.request.ey);
00103          struct timeval t_start,t_end;
00104          gettimeofday(&t_start,NULL);
00105 
00106          if (client.call(srv))
00107          {
00108             ROS_INFO("Total Points: %ld", (long int)srv.response.Route_Points);
00109          }
00110          else
00111          {
00112             ROS_ERROR("Failed to call service HopField");
00113             continue;
00114          }
00115 
00116    if (srv.response.Route_Points <= 0 )
00117    {
00118      ROS_WARN("No Solution! Reset Target Point");
00119    }
00120 
00121          int m_nStep=srv.response.Route_Points;
00122          if ((m_nStep>0)&&(m_nStep<=10000))
00123          {
00124                 ros::Rate loop_rate(m_nStep);
00125 
00126                 gettimeofday(&t_end,NULL);
00127                 long cost_time=abs(t_end.tv_usec-t_start.tv_usec);
00128                 ROS_INFO("--Cost Time: %f s--", cost_time*1.0/1000000); 
00129                 FILE* fp = fopen("TimeConsume.txt","a");
00130                 fprintf(fp,"time : %f\n",cost_time*1.0/1000);
00131                 fprintf(fp,"step : %d\n",m_nStep);
00132                 fclose(fp);             
00133 
00134                 ResetTail();
00135                 for (int i=0;i<m_nStep;i++)
00136                 {
00137                         Draw_Sphere(300+NodeID,srv.response.Route_X[i],srv.response.Route_Y[i],g_FlyingHeight,4,r,g,b,1.0);
00138                         AddTailPoint(srv.response.Route_X[i],srv.response.Route_Y[i],g_FlyingHeight);
00139                         loop_rate.sleep();
00140                 }
00141                 srv.request.sx =  srv.request.ex;
00142                 srv.request.sy =  srv.request.ey;  
00143                 Finded=true;
00144          }
00145         
00146          if (!Finded)
00147          {
00148                 srv.request.sx = int(1200.0*rand()/RAND_MAX);
00149                 srv.request.sy = int(1200.0*rand()/RAND_MAX);
00150         }
00151   }
00152 
00153   return 0;
00154 }