getID.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include <urg_node/urg_c_wrapper.h>
00036 #include <vector>
00037 #include <string>
00038 
00039 #include <fcntl.h>
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 /* gcc defined unix */
00043 #ifdef unix
00044 #include <unistd.h>
00045 #endif
00046 #ifdef WIN32
00047 #include <io.h>
00048 #define pipe(X) _pipe(X,4096,O_BINARY)     
00049 #define fileno _fileno
00050 #define dup2 _dup2
00051 #define read _read
00052 #endif
00053 
00054 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
00055     std::stringstream ss(s);
00056     std::string item;
00057     while (std::getline(ss, item, delim)) {
00058         elems.push_back(item);
00059     }
00060     return elems;
00061 }
00062 
00063 
00064 std::vector<std::string> split(const std::string &s, char delim) {
00065     std::vector<std::string> elems;
00066     split(s, delim, elems);
00067     return elems;
00068 }
00069 
00070 int
00071 main(int argc, char** argv)
00072 {
00073   ros::Time::init();
00074 
00075   if (argc < 2 || argc > 3)
00076   {
00077     fprintf(stderr, "usage: getID /dev/ttyACM? [quiet]\nOutputs the device ID of a hokuyo at /dev/ttyACM? or IP address (specified as 192.168.1.6:10940). Add a second argument for script friendly output.\n");
00078     return 1;
00079   }
00080   
00081   bool verbose = (argc == 2);
00082 
00083   int save_stdout = dup(STDOUT_FILENO);
00084 
00085   if(!verbose){ // Block urg's prints
00086         int fds[2]; 
00087     int res; 
00088     char buf[256];
00089     int so; 
00090 
00091     res=pipe(fds);
00092     assert(res==0); 
00093 
00094     so=fileno(stdout);
00095     // close stdout handle and make the writable part of fds the new stdout.
00096     res=dup2(fds[1],so);
00097     assert(res!=-1); 
00098   }
00099 
00100   
00101   bool publish_intensity = false;
00102   bool publish_multiecho = false;
00103   int serial_baud = 115200;
00104   int ip_port = 10940;
00105   std::string ip_address = "";
00106   std::string serial_port = "";
00107 
00108   std::vector<std::string> ip_split = split(argv[1], ':');
00109   if(ip_split.size() < 2){ // Not an IP address
00110         serial_port = argv[1];
00111   } else if(ip_split.size() == 2){ // IP address formatted as IP:PORT
00112         ip_address = ip_split[0];
00113         ip_port = atoi(ip_split[1].c_str());
00114   } else { // Invalid
00115         if (verbose){
00116       printf("getID failed due to invalid specifier.\n");
00117       return 1;
00118     }
00119   }
00120   
00121   boost::shared_ptr<urg_node::URGCWrapper> urg_;
00122   
00123   for (int retries = 10; retries; retries--)
00124   {
00125         // Set up the urgwidget
00126         try{
00127                 if(ip_address != ""){
00128                         urg_.reset(new urg_node::URGCWrapper(ip_address, ip_port, publish_intensity, publish_multiecho));
00129                 } else {
00130                         urg_.reset(new urg_node::URGCWrapper(serial_baud, serial_port, publish_intensity, publish_multiecho));
00131                 }
00132                 std::string device_id = urg_->getDeviceID();
00133                 if (verbose){
00134                         if(ip_address != ""){
00135                                 printf("Device at %s:%i has ID ", ip_address.c_str(), ip_port);
00136                         } else {
00137                                 printf("Device at %s has ID ", serial_port.c_str());
00138                         }
00139                 }
00140                 // Print this in either mode
00141                 fflush(NULL); // Clear whatever we aren't printing
00142                 dup2(save_stdout, STDOUT_FILENO); // Restore std::out
00143                 printf("%s\n", device_id.c_str());
00144                 return 0;
00145         } catch(std::runtime_error& e){
00146                 printf("getID failed: %s\n", e.what());
00147         }
00148         ros::Duration(1.0).sleep();
00149   }
00150 
00151   if (verbose){
00152     printf("getID failed for 10 seconds. Giving up.\n");
00153         if(ip_address != ""){
00154                 printf("Device at %s:%i\n", ip_address.c_str(), ip_port);
00155         } else {
00156                 printf("Device at %s\n", serial_port.c_str());
00157         }
00158   }
00159   return 1;
00160 }


urg_node
Author(s): Chad Rockey
autogenerated on Fri Aug 28 2015 13:35:14