rc_tagdetect_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 "ros_tagdetect_client.h"
34 #include <ros/ros.h>
35 #include <signal.h>
36 #include <rcdiscover/discover.h>
37 #include <rcdiscover/utils.h>
38 
39 std::unique_ptr<rc_tagdetect_client::RosTagdetectClient> tagdetect_wrapper;
40 
41 void sigintHandler(int)
42 {
43  tagdetect_wrapper.reset();
44  ros::shutdown();
45 }
46 
47 std::string getHost(const std::string& device_name, const std::string& interface)
48 {
49  // broadcast discover request
50  rcdiscover::Discover discover;
51  discover.broadcastRequest();
52 
53  std::vector<rcdiscover::DeviceInfo> infos;
54 
55  // get responses
56  while (discover.getResponse(infos, 100))
57  {
58  }
59 
60  std::vector<std::vector<std::string>> devices;
61  std::vector<std::string> filtered_info = { "", "" };
62  for (const auto& info : infos)
63  {
64  if (!interface.empty() && interface != info.getIfaceName())
65  {
66  continue;
67  }
68 
69  // if used defined name is not set, fall back to model name
70  std::string user_defined_name = info.getUserName();
71  if (user_defined_name.empty())
72  {
73  user_defined_name = info.getModelName();
74  }
75 
76  // if no device is given, return any rc_visard
77  if (device_name.empty())
78  {
79  if (info.getModelName().find("rc_visard") != std::string::npos)
80  {
81  filtered_info[0] = user_defined_name;
82  filtered_info[1] = ip2string(info.getIP());
83  devices.push_back(filtered_info);
84  }
85  }
86  else if ((device_name == info.getSerialNumber()) || (device_name == user_defined_name))
87  {
88  filtered_info[0] = user_defined_name;
89  filtered_info[1] = ip2string(info.getIP());
90  devices.push_back(filtered_info);
91  }
92  }
93  std::sort(devices.begin(), devices.end());
94  auto unique_devices_it = std::unique(devices.begin(), devices.end());
95  devices.erase(unique_devices_it, devices.end());
96 
97  if (devices.empty())
98  {
99  ROS_FATAL_STREAM("No device found with the name '" << device_name << "'");
100  return "";
101  }
102  else if (devices.size() > 1)
103  {
104  ROS_FATAL_STREAM("Found " << devices.size() << " devices with the name '" << device_name
105  << "'. Please specify a unique device name.");
106  return "";
107  }
108 
109  ROS_INFO_STREAM("Using device '" << device_name << "' with name '" << devices[0][0] << "' and IP address "
110  << devices[0][1]);
111  return devices[0][1];
112 }
113 
114 int main(int argc, char** argv)
115 {
116  std::string name = DETECTION_TAG_TYPE;
117 
119  signal(SIGINT, sigintHandler);
120 
121  ros::NodeHandle pnh("~");
122 
123  std::string host;
124  std::string device;
125  pnh.getParam("host", host);
126  pnh.getParam("device", device);
127  if (!host.empty() && !device.empty())
128  {
129  ROS_WARN("Both parameters: 'device' and 'host' are set. Using 'device' to start the client.");
130  }
131 
132  if (!device.empty() || host.empty())
133  {
134  std::size_t delim_pos = 0;
135  delim_pos = device.find(':');
136  std::string interface;
137  if (delim_pos != std::string::npos)
138  {
139  interface = device.substr(0, delim_pos);
140  device = device.substr(delim_pos + 1);
141  }
142 
143  host = getHost(device, interface);
144  }
145 
146  if (host.empty())
147  {
148  return 1;
149  }
150 
151  try
152  {
153  // instantiate wrapper and advertise services
155  }
156  catch (const std::exception& ex)
157  {
158  ROS_FATAL("Client could not be created due to an error: %s", ex.what());
159  return 1;
160  }
161 
162  ROS_INFO_STREAM("TagDetect node started for host: " << host);
163 
164  ros::spin();
165 }
#define ROS_FATAL(...)
::std::string string
Definition: gtest-port.h:1129
std::string ip2string(const uint32_t ip)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string getHost(const std::string &device_name, const std::string &interface)
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
int main(int argc, char **argv)
curl_easy_setopt expects a curl_off_t argument for this option curl_easy_setopt expects a curl_write_callback argument for this option curl_easy_setopt expects a curl_ioctl_callback argument for this option curl_easy_setopt expects a curl_opensocket_callback argument for this option curl_easy_setopt expects a curl_debug_callback argument for this option curl_easy_setopt expects a curl_conv_callback argument for this option curl_easy_setopt expects a private data pointer as argument for this option curl_easy_setopt expects a FILE *argument for this option curl_easy_setopt expects a struct curl_httppost *argument for this option curl_easy_setopt expects a struct curl_slist *argument for this option curl_easy_getinfo expects a pointer to char *for this info curl_easy_getinfo expects a pointer to double for this info curl_easy_getinfo expects a pointer to struct curl_tlssessioninfo *for this info curl_easy_getinfo expects a pointer to curl_socket_t for this info size_t
#define ROS_FATAL_STREAM(args)
std::unique_ptr< rc_tagdetect_client::RosTagdetectClient > tagdetect_wrapper
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()
void sigintHandler(int)
const char * name
Definition: curl_sasl.c:54


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16