Go to the documentation of this file.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 #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
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 {
00056 std::stringstream ss(s);
00057 std::string item;
00058 while (std::getline(ss, item, delim))
00059 {
00060 elems.push_back(item);
00061 }
00062 return elems;
00063 }
00064
00065
00066 std::vector<std::string> split(const std::string &s, char delim)
00067 {
00068 std::vector<std::string> elems;
00069 split(s, delim, elems);
00070 return elems;
00071 }
00072
00073 int
00074 main(int argc, char** argv)
00075 {
00076 ros::Time::init();
00077
00078 if (argc < 2 || argc > 3)
00079 {
00080 fprintf(stderr,
00081 "usage: getID /dev/ttyACM? [quiet]\nOutputs the device ID of a hokuyo at /dev/ttyACM? or IP address"
00082 " (specified as 192.168.1.6:10940). Add a second argument for script friendly output.\n");
00083 return 1;
00084 }
00085
00086 bool verbose = (argc == 2);
00087
00088 int save_stdout = dup(STDOUT_FILENO);
00089
00090 if (!verbose)
00091 {
00092 int fds[2];
00093 int res;
00094 char buf[256];
00095 int so;
00096
00097 res = pipe(fds);
00098 assert(res == 0);
00099
00100 so = fileno(stdout);
00101
00102 res = dup2(fds[1], so);
00103 assert(res != -1);
00104 }
00105
00106
00107 bool publish_intensity = false;
00108 bool publish_multiecho = false;
00109 bool synchronize_time = false;
00110 int serial_baud = 115200;
00111 int ip_port = 10940;
00112 std::string ip_address = "";
00113 std::string serial_port = "";
00114
00115 std::vector<std::string> ip_split = split(argv[1], ':');
00116 if (ip_split.size() < 2)
00117 {
00118 serial_port = argv[1];
00119 }
00120 else if (ip_split.size() == 2)
00121 {
00122 ip_address = ip_split[0];
00123 ip_port = atoi(ip_split[1].c_str());
00124 }
00125 else
00126 {
00127 if (verbose)
00128 {
00129 printf("getID failed due to invalid specifier.\n");
00130 return 1;
00131 }
00132 }
00133
00134 boost::shared_ptr<urg_node::URGCWrapper> urg_;
00135
00136 for (int retries = 10; retries; retries--)
00137 {
00138
00139 try
00140 {
00141 if (ip_address != "")
00142 {
00143 urg_.reset(new urg_node::URGCWrapper(ip_address, ip_port,
00144 publish_intensity, publish_multiecho, synchronize_time));
00145 }
00146 else
00147 {
00148 urg_.reset(new urg_node::URGCWrapper(serial_baud, serial_port,
00149 publish_intensity, publish_multiecho, synchronize_time));
00150 }
00151 std::string device_id = urg_->getDeviceID();
00152 if (verbose)
00153 {
00154 if (ip_address != "")
00155 {
00156 printf("Device at %s:%i has ID ", ip_address.c_str(), ip_port);
00157 }
00158 else
00159 {
00160 printf("Device at %s has ID ", serial_port.c_str());
00161 }
00162 }
00163
00164 fflush(NULL);
00165 dup2(save_stdout, STDOUT_FILENO);
00166 printf("%s\n", device_id.c_str());
00167 return 0;
00168 }
00169 catch (std::runtime_error& e)
00170 {
00171 printf("getID failed: %s\n", e.what());
00172 }
00173 ros::Duration(1.0).sleep();
00174 }
00175
00176 if (verbose)
00177 {
00178 printf("getID failed for 10 seconds. Giving up.\n");
00179 if (ip_address != "")
00180 {
00181 printf("Device at %s:%i\n", ip_address.c_str(), ip_port);
00182 }
00183 else
00184 {
00185 printf("Device at %s\n", serial_port.c_str());
00186 }
00187 }
00188 return 1;
00189 }