pick_client.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  *
4  * Author: Monika Florek-Jasinska, Carlos Xavier Garcia Briones
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 "pick_client.h"
34 
35 using std::string;
36 
37 namespace ros_pick_client
38 {
39 PickClient::PickClient(const string& host, const string& node_name, const ros::NodeHandle& nh)
40  : nh_(nh), rest_helper_(host, node_name, 10000), visualizer_(nh)
41 {
44  startPick();
45 }
46 
48 {
49  try
50  {
51  stopPick();
52  }
53  catch (const std::exception& ex)
54  {
55  ROS_FATAL("Exception during destruction of PickClient: %s", ex.what());
56  }
57  catch (...)
58  {
59  ROS_FATAL("Exception during destruction of PickClient");
60  }
61 }
62 
64 {
66 }
67 
69 {
71 }
72 
73 bool PickClient::setLoadCarrier(rc_pick_client::SetLoadCarrierRequest& request,
74  rc_pick_client::SetLoadCarrierResponse& response)
75 {
76  callService("set_load_carrier", request, response);
77  return true;
78 }
79 
80 bool PickClient::getLoadCarriers(rc_pick_client::GetLoadCarriersRequest& request,
81  rc_pick_client::GetLoadCarriersResponse& response)
82 {
83  callService("get_load_carriers", request, response);
84  return true;
85 }
86 
87 bool PickClient::deleteLoadCarriersSrv(rc_pick_client::DeleteLoadCarriersRequest& request,
88  rc_pick_client::DeleteLoadCarriersResponse& response)
89 {
90  callService("delete_load_carriers", request, response);
91  return true;
92 }
93 
94 bool PickClient::detectLoadCarriersSrv(rc_pick_client::DetectLoadCarriersRequest& request,
95  rc_pick_client::DetectLoadCarriersResponse& response)
96 {
97  callService("detect_load_carriers", request, response);
98  visualizer_.visualizeLoadCarriers(response.load_carriers);
99  return true;
100 }
101 
102 bool PickClient::detectFillingLevelSrv(rc_pick_client::DetectFillingLevelRequest& request,
103  rc_pick_client::DetectFillingLevelResponse& response)
104 {
105  callService("detect_filling_level", request, response);
106  visualizer_.visualizeLoadCarriers(response.load_carriers);
107  return true;
108 }
109 
110 bool PickClient::setROI(rc_pick_client::SetRegionOfInterestRequest& request,
111  rc_pick_client::SetRegionOfInterestResponse& response)
112 {
113  callService("set_region_of_interest", request, response);
114  return true;
115 }
116 
117 bool PickClient::getROIs(rc_pick_client::GetRegionsOfInterestRequest& request,
118  rc_pick_client::GetRegionsOfInterestResponse& response)
119 {
120  callService("get_regions_of_interest", request, response);
121  return true;
122 }
123 
124 bool PickClient::deleteROISrv(rc_pick_client::DeleteRegionsOfInterestRequest& request,
125  rc_pick_client::DeleteRegionsOfInterestResponse& response)
126 {
127  callService("delete_regions_of_interest", request, response);
128  return true;
129 }
130 
132 {
133  srv_detect_lc_ = nh_.advertiseService("detect_load_carriers", &PickClient::detectLoadCarriersSrv, this);
134  srv_delete_lcs_ = nh_.advertiseService("delete_load_carriers", &PickClient::deleteLoadCarriersSrv, this);
135  srv_get_lcs_ = nh_.advertiseService("get_load_carriers", &PickClient::getLoadCarriers, this);
136  srv_set_lc_ = nh_.advertiseService("set_load_carrier", &PickClient::setLoadCarrier, this);
137  srv_delete_rois_ = nh_.advertiseService("delete_regions_of_interest", &PickClient::deleteROISrv, this);
138  srv_get_rois_ = nh_.advertiseService("get_regions_of_interest", &PickClient::getROIs, this);
139  srv_set_roi_ = nh_.advertiseService("set_region_of_interest", &PickClient::setROI, this);
141 }
142 
143 void PickClient::paramsToCfg(const json& params, rc_pick_client::pickModuleConfig& cfg)
144 {
145  for (const auto& param : params)
146  {
147  const auto& name = param["name"];
148  if (name == "cluster_max_curvature")
149  {
150  cfg.cluster_max_curvature = param["value"];
151  }
152  else if (name == "cluster_max_dimension")
153  {
154  cfg.cluster_max_dimension = param["value"];
155  }
156  else if (name == "clustering_discontinuity_factor")
157  {
158  cfg.clustering_discontinuity_factor = param["value"];
159  }
160  else if (name == "clustering_max_surface_rmse")
161  {
162  cfg.clustering_max_surface_rmse = param["value"];
163  }
164  else if (name == "clustering_patch_size")
165  {
166  cfg.clustering_patch_size = param["value"];
167  }
168  else if (name == "load_carrier_crop_distance")
169  {
170  cfg.load_carrier_crop_distance = param["value"];
171  }
172  else if (name == "load_carrier_model_tolerance")
173  {
174  cfg.load_carrier_model_tolerance = param["value"];
175  }
176  }
177 }
178 
180 {
181  rc_pick_client::pickModuleConfig cfg;
182 
183  // first get the current values from sensor
184  const auto j_params = rest_helper_.getParameters();
185 
186  paramsToCfg(j_params, cfg);
187 
188  // second, try to get ROS parameters:
189  // if parameter is not set in parameter server, we default to current sensor configuration
190  nh_.param("cluster_max_curvature", cfg.cluster_max_curvature, cfg.cluster_max_curvature);
191  nh_.param("cluster_max_dimension", cfg.cluster_max_dimension, cfg.cluster_max_dimension);
192  nh_.param("clustering_discontinuity_factor", cfg.clustering_discontinuity_factor,
193  cfg.clustering_discontinuity_factor);
194  nh_.param("clustering_max_surface_rmse", cfg.clustering_max_surface_rmse, cfg.clustering_max_surface_rmse);
195  nh_.param("clustering_patch_size", cfg.clustering_patch_size, cfg.clustering_patch_size);
196  nh_.param("load_carrier_crop_distance", cfg.load_carrier_crop_distance, cfg.load_carrier_crop_distance);
197  nh_.param("load_carrier_model_tolerance", cfg.load_carrier_model_tolerance, cfg.load_carrier_model_tolerance);
198 
199  // set parameters on parameter server so that dynamic reconfigure picks them up
200  nh_.setParam("cluster_max_curvature", cfg.cluster_max_curvature);
201  nh_.setParam("cluster_max_dimension", cfg.cluster_max_dimension);
202  nh_.setParam("clustering_discontinuity_factor", cfg.clustering_discontinuity_factor);
203  nh_.setParam("clustering_max_surface_rmse", cfg.clustering_max_surface_rmse);
204  nh_.setParam("clustering_patch_size", cfg.clustering_patch_size);
205  nh_.setParam("load_carrier_crop_distance", cfg.load_carrier_crop_distance);
206  nh_.setParam("load_carrier_model_tolerance", cfg.load_carrier_model_tolerance);
207 
208  // instantiate dynamic reconfigure server that will initially read those values
209  using ReconfServer = dynamic_reconfigure::Server<rc_pick_client::pickModuleConfig>;
210  server_ = std::unique_ptr<ReconfServer>(new ReconfServer(nh_));
211 }
212 
213 json PickClient::createSharedParameters(rc_pick_client::pickModuleConfig& config)
214 {
215  // fill json request from dynamic reconfigure request
216  json js_params, js_param;
217  js_param["name"] = "cluster_max_curvature";
218  js_param["value"] = config.cluster_max_curvature;
219  js_params.push_back(js_param);
220  js_param["name"] = "clustering_discontinuity_factor";
221  js_param["value"] = config.clustering_discontinuity_factor;
222  js_param["name"] = "clustering_max_surface_rmse";
223  js_param["value"] = config.clustering_max_surface_rmse;
224  js_params.push_back(js_param);
225  js_param["name"] = "load_carrier_crop_distance";
226  js_param["value"] = config.load_carrier_crop_distance;
227  js_params.push_back(js_param);
228  js_param["name"] = "load_carrier_model_tolerance";
229  js_param["value"] = config.load_carrier_model_tolerance;
230  js_params.push_back(js_param);
231  return js_params;
232 }
233 
234 } // namespace ros_pick_client
nlohmann::json json
Definition: pick_client.h:57
bool param(const std::string &param_name, T &param_val, const T &default_val)
#define ROS_FATAL(...)
PickClient(const std::string &host, const std::string &node_name, const ros::NodeHandle &nh)
Definition: pick_client.cpp:39
bool detectLoadCarriersSrv(rc_pick_client::DetectLoadCarriersRequest &request, rc_pick_client::DetectLoadCarriersResponse &response)
Definition: pick_client.cpp:94
ros::ServiceServer srv_detect_filling_level_
Definition: pick_client.h:77
ros::ServiceServer srv_set_lc_
Definition: pick_client.h:78
bool deleteROISrv(rc_pick_client::DeleteRegionsOfInterestRequest &request, rc_pick_client::DeleteRegionsOfInterestResponse &response)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool setROI(rc_pick_client::SetRegionOfInterestRequest &request, rc_pick_client::SetRegionOfInterestResponse &response)
ros::ServiceServer srv_detect_lc_
Definition: pick_client.h:76
ros::ServiceServer srv_get_lcs_
Definition: pick_client.h:79
ros::ServiceServer srv_get_rois_
Definition: pick_client.h:82
ros::ServiceServer srv_set_roi_
Definition: pick_client.h:81
std::unique_ptr< dynamic_reconfigure::Server< rc_pick_client::pickModuleConfig > > server_
Definition: pick_client.h:70
bool param(const std::string &param_name, T &param_val, const T &default_val) const
json createSharedParameters(rc_pick_client::pickModuleConfig &config)
ros::ServiceServer srv_delete_rois_
Definition: pick_client.h:83
ros::ServiceServer srv_delete_lcs_
Definition: pick_client.h:80
bool setLoadCarrier(rc_pick_client::SetLoadCarrierRequest &request, rc_pick_client::SetLoadCarrierResponse &response)
Definition: pick_client.cpp:73
bool detectFillingLevelSrv(rc_pick_client::DetectFillingLevelRequest &request, rc_pick_client::DetectFillingLevelResponse &response)
bool getLoadCarriers(rc_pick_client::GetLoadCarriersRequest &request, rc_pick_client::GetLoadCarriersResponse &response)
Definition: pick_client.cpp:80
bool getROIs(rc_pick_client::GetRegionsOfInterestRequest &request, rc_pick_client::GetRegionsOfInterestResponse &response)
bool callService(const std::string &name, const Request &req, Response &res)
Definition: pick_client.h:89
void visualizeLoadCarriers(const std::vector< rc_pick_client::LoadCarrier > &ros_lcs)
void paramsToCfg(const json &params, rc_pick_client::pickModuleConfig &cfg)
bool deleteLoadCarriersSrv(rc_pick_client::DeleteLoadCarriersRequest &request, rc_pick_client::DeleteLoadCarriersResponse &response)
Definition: pick_client.cpp:87
pick_visualization::Visualization visualizer_
Definition: pick_client.h:74
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
rc_rest_api::RestHelper rest_helper_
Definition: pick_client.h:72
json servicePutRequest(const std::string &service_name)
Definition: rest_helper.cpp:92


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