rc_pick_client_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  *
4  * Author: Monika Florek-Jasinska
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 
33 #include "boxpick_client.h"
34 #include "itempick_client.h"
35 
36 #include <ros/ros.h>
37 #include <signal.h>
38 #include <rcdiscover/discover.h>
39 #include <rcdiscover/utils.h>
40 
41 std::unique_ptr<ros_pick_client::PickClient> pick_client;
42 
43 void sigintHandler(int)
44 {
45  pick_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 pick_type = PICK_TYPE;
119  ros::init(argc, argv, pick_type, ros::init_options::NoSigintHandler);
120 
121  if (pick_type != "rc_boxpick" && pick_type != "rc_itempick")
122  {
123  ROS_FATAL("node type not defined");
124  }
125 
126  signal(SIGINT, sigintHandler);
127 
128  ros::NodeHandle pnh("~");
129 
130  std::string host;
131  std::string device;
132  pnh.getParam("host", host);
133  pnh.getParam("device", device);
134  if (!host.empty() && !device.empty())
135  {
136  ROS_WARN("Both parameters: 'device' and 'host' are set. Using 'device' to start the client.");
137  }
138 
139  if (!device.empty() || host.empty())
140  {
141  std::size_t delim_pos = 0;
142  delim_pos = device.find(':');
143  std::string interface;
144  if (delim_pos != std::string::npos)
145  {
146  interface = device.substr(0, delim_pos);
147  device = device.substr(delim_pos + 1);
148  }
149 
150  host = getHost(device, interface);
151  }
152 
153  if (host.empty())
154  {
155  return 1;
156  }
157 
158  try
159  {
160  // instantiate wrapper and advertise services
161  if (pick_type == "rc_boxpick")
162  {
163  pick_client.reset(new ros_pick_client::BoxpickClient(host, pnh));
164  }
165  else
166  {
167  pick_client.reset(new ros_pick_client::ItempickClient(host, pnh));
168  }
169  ROS_INFO_STREAM(pick_type << " node started for host: " << host);
170  }
171  catch (const std::exception& ex)
172  {
173  ROS_FATAL("Client could not be created due to an error: %s", ex.what());
174  return 1;
175  }
176 
177  ros::Rate loop_rate(10);
178 
179  ros::spin();
180 }
#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(...)
void sigintHandler(int)
ROSCPP_DECL void spin(Spinner &spinner)
#define ROS_FATAL_STREAM(args)
bool getResponse(std::vector< DeviceInfo > &info, int timeout_per_socket=1000)
#define ROS_INFO_STREAM(args)
std::string getHost(const std::string &device_name, const std::string &interface)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
std::unique_ptr< ros_pick_client::PickClient > pick_client
int main(int argc, char **argv)


rc_pick_client
Author(s): Monika Florek-Jasinska
autogenerated on Sat Feb 13 2021 03:41:57