rc_hand_eye_calibration_client_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 Roboception GmbH
3  *
4  * Author: Felix Endres
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
34 #include <ros/ros.h>
35 #include <signal.h>
36 #include <rcdiscover/discover.h>
37 #include <rcdiscover/utils.h>
38 
40 std::unique_ptr<HandEyeCalibClient> client;
41 
42 void sigintHandler(int)
43 {
44  ROS_INFO("Shutting down...");
45  client.reset();
46  ros::shutdown();
47 }
48 
49 std::string getHost(const std::string& device_name, const std::string& interface)
50 {
51  // broadcast discover request
52  rcdiscover::Discover discover;
53  discover.broadcastRequest();
54 
55  std::vector<rcdiscover::DeviceInfo> infos;
56 
57  // get responses
58  while (discover.getResponse(infos, 100))
59  {
60  }
61 
62  std::vector<std::vector<std::string>> devices;
63  std::vector<std::string> filtered_info = { "", "" };
64  for (const auto& info : infos)
65  {
66  if (!interface.empty() && interface != info.getIfaceName())
67  {
68  continue;
69  }
70 
71  // if used defined name is not set, fall back to model name
72  std::string user_defined_name = info.getUserName();
73  if (user_defined_name.empty())
74  {
75  user_defined_name = info.getModelName();
76  }
77 
78  // if no device is given, return any rc_visard
79  if (device_name.empty())
80  {
81  if (info.getModelName().find("rc_visard") != std::string::npos)
82  {
83  filtered_info[0] = user_defined_name;
84  filtered_info[1] = ip2string(info.getIP());
85  devices.push_back(filtered_info);
86  }
87  }
88  else if ((device_name == info.getSerialNumber()) || (device_name == user_defined_name))
89  {
90  filtered_info[0] = user_defined_name;
91  filtered_info[1] = ip2string(info.getIP());
92  devices.push_back(filtered_info);
93  }
94  }
95  std::sort(devices.begin(), devices.end());
96  auto unique_devices_it = std::unique(devices.begin(), devices.end());
97  devices.erase(unique_devices_it, devices.end());
98 
99  if (devices.empty())
100  {
101  ROS_FATAL_STREAM("No device found with the name '" << device_name << "'");
102  return "";
103  }
104  else if (devices.size() > 1)
105  {
106  ROS_FATAL_STREAM("Found " << devices.size() << " devices with the name '" << device_name
107  << "'. Please specify a unique device name.");
108  return "";
109  }
110 
111  ROS_INFO_STREAM("Using device '" << device_name << "' with name '" << devices[0][0] << "' and IP address "
112  << devices[0][1]);
113  return devices[0][1];
114 }
115 
116 int main(int argc, char** argv)
117 {
118  std::string name = "rc_hand_eye_calibration_client";
119  ros::init(argc, argv, name);
120  signal(SIGINT, sigintHandler);
121 
122  ros::NodeHandle pnh("~");
123 
124  std::string host;
125  std::string device;
126  pnh.getParam("host", host);
127  pnh.getParam("device", device);
128  if (!host.empty() && !device.empty())
129  {
130  ROS_WARN("Both parameters: 'device' and 'host' are set. Using 'device' to start the client.");
131  }
132 
133  if (!device.empty() || host.empty())
134  {
135  std::size_t delim_pos = 0;
136  delim_pos = device.find(':');
137  std::string interface;
138  if (delim_pos != std::string::npos)
139  {
140  interface = device.substr(0, delim_pos);
141  device = device.substr(delim_pos + 1);
142  }
143 
144  host = getHost(device, interface);
145  }
146 
147  if (host.empty())
148  {
149  return 1;
150  }
151 
152  try
153  {
154  // instantiate wrapper and advertise services
155  client.reset(new HandEyeCalibClient(host, pnh));
156  }
157  catch (const std::exception& ex)
158  {
159  ROS_FATAL("Client could not be created due to an error: %s", ex.what());
160  return 1;
161  }
162 
163  ROS_INFO_STREAM("Hand eye calibration node started for host: " << host);
164 
165  // start ros node
166  ros::spin();
167 }
int main(int argc, char **argv)
#define ROS_FATAL(...)
std::string ip2string(const uint32_t ip)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
std::string getHost(const std::string &device_name, const std::string &interface)
std::unique_ptr< HandEyeCalibClient > client
#define ROS_FATAL_STREAM(args)
#define ROS_INFO(...)
bool getResponse(std::vector< DeviceInfo > &info, int timeout_per_socket=1000)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()


rc_hand_eye_calibration_client
Author(s): Christian Emmerich
autogenerated on Sat Feb 13 2021 03:41:55