iri_publish_params.cpp
Go to the documentation of this file.
00001 #include <iri_publish_params.h>
00002 #include <iri_publish_params/classifier_update_service.h>
00003 
00004 template <class T>
00005 bool from_string(T& t, 
00006                  const std::string& s, 
00007                  std::ios_base& (*f)(std::ios_base&))
00008 {
00009   std::istringstream iss(s);
00010   return !(iss >> f >> t).fail();
00011 }
00012 
00013 void do_fail(char** argv)
00014 {
00015   //check params
00016   ROS_ERROR("%s: Insufficient params!\nUsage: %s <params_file> <filter_method> [selected]. If the parameter 'selected' is ommited, it is set=0", argv[0], argv[0]);
00017   exit(-1);
00018 }
00019 
00020 
00021 int main(int argc, char *argv[])
00022 {
00023   int selected=0;
00024   int filter_method=0;
00025   std::string row;
00026   std::string filename;
00027   ros::init(argc, argv, "classifier_updates_publisher");
00028   ros::NodeHandle nh;
00029   ros::NodeHandle pn("~");
00030   ros::ServiceClient client = nh.serviceClient<iri_publish_params::classifier_update_service>("classifier_update");
00031   // wait until service is ready
00032   client.waitForExistence();
00033 
00034   if(argc<3)
00035   {
00036     if (! pn.getParam("input_file", filename)) do_fail(argv);
00037     if (! pn.getParam("selected_centroid", selected)) do_fail(argv);
00038     if (! pn.getParam("filter_method", filter_method)) do_fail(argv);
00039   }
00040   else
00041   {
00042     if(argc==4)
00043     {
00044       selected=atoi(argv[3]);
00045     }  
00046     filter_method=atoi(argv[2]);
00047     filename=argv[1];
00048   }
00049 
00050   iri_publish_params::classifier_update_service srv_message;
00051   srv_message.request.params.selected_centroid = selected;
00052   srv_message.request.params.filter_method = filter_method;
00053   
00054   std::ifstream pf(filename.c_str(), std::ifstream::in);
00055   if (!pf.is_open())
00056   {
00057     ROS_ERROR("%s: File not found!", argv[0]);
00058     exit(-1);
00059   }
00060   while(!pf.eof())
00061   {
00062     std::getline(pf,row);
00063 
00064     if (row.empty())
00065         continue;
00066 
00067     //std::vector<float> float_row;
00068     iri_publish_params::classifier_params float_row;
00069 
00070     // ROS_INFO("%s", row.c_str());
00071   
00072     std::string::size_type lastPos=0;
00073     std::string::size_type pos = row.find_first_of(" ", lastPos);
00074 
00075     while(std::string::npos != pos || std::string::npos != lastPos)
00076     {
00077       std::string param = row.substr(lastPos, pos - lastPos);
00078       float fparam;
00079       from_string<float>(fparam,param,std::dec);
00080       float_row.params.push_back(fparam);
00081 
00082       lastPos = row.find_first_not_of(" ", pos);
00083       pos = row.find_first_of(" ", lastPos);
00084     }
00085     //for(int a=0;a<float_row.params.size();a++)
00086     //  std::cout<<float_row.params[a]<<" ";
00087     srv_message.request.params.update_params.push_back(float_row);
00088     // ROS_INFO("%s", row.c_str());
00089   }
00090   pf.close();
00091 
00092   ROS_INFO("Sending params update (%d vectors dim %d, selected=%d method=%d)\n", srv_message.request.params.update_params.size(), 
00093                                                                                  srv_message.request.params.update_params[0].params.size(),
00094                                                                                  srv_message.request.params.selected_centroid,
00095                                                                                  srv_message.request.params.filter_method);
00096 
00097   iri_publish_params::classifier_update_service srv;
00098   client.call(srv_message);
00099   ros::spinOnce();
00100 
00101   return 0;
00102 }
00103 


iri_publish_params
Author(s): Arnau Ramisa
autogenerated on Fri Dec 6 2013 21:13:35