plan_client.cpp
Go to the documentation of this file.
00001 /*
00002 * Software License Agreement (BSD License)
00003 *Copyright (c) 2015, micROS Team
00004  http://micros.nudt.edu.cn/
00005 *All rights reserved.
00006 * Copyright (c) 2009, Willow Garage, Inc.
00007 * All rights reserved.
00008 *
00009 * Redistribution and use in source and binary forms, with or without
00010 * modification, are permitted provided that the following conditions
00011 * are met:
00012 *
00013 * * Redistributions of source code must retain the above copyright
00014 * notice, this list of conditions and the following disclaimer.
00015 * * Redistributions in binary form must reproduce the above
00016 * copyright notice, this list of conditions and the following
00017 * disclaimer in the documentation and/or other materials provided
00018 * with the distribution.
00019 * * Neither the name of Willow Garage, Inc. nor the names of its
00020 * contributors may be used to endorse or promote products derived
00021 * from this software without specific prior written permission.
00022 *
00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034 * POSSIBILITY OF SUCH DAMAGE.
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   //flight height(the data resolution ration is 90)
00050   float g_FlyingHeight=650.0/90;
00051   
00052   srand((int)time(0));
00053   
00054   //if lose parameter
00055   if (argc != 2)
00056   {
00057     ROS_INFO("usage: Plan_client NodeID");
00058     return 1;
00059   }
00060 
00061   //get the client ID
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   //service of plan 
00076   ros::ServiceClient client = n.serviceClient<micros_hopfield::plan>("Planner");
00077   micros_hopfield::plan srv;
00078         
00079   //publish the rviz
00080   InitMarkerPublisher();
00081 
00082   //the color of client in rviz
00083   float r=1.0,g=1.0,b=1.0;
00084   GetStoreColor(NodeID,r,g,b);
00085 
00086   //initialize
00087   InitTail(100+NodeID,r,g,b);
00088 
00089   //initial starting point(1,1)
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);//red ball stands for the client
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 }


micros_hopfield
Author(s): Xiaojia Xiang
autogenerated on Thu Jun 6 2019 21:05:58