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 {
43  startPick();
44 }
45 
47 {
48  try
49  {
50  stopPick();
51  }
52  catch (const std::exception& ex)
53  {
54  ROS_FATAL("Exception during destruction of PickClient: %s", ex.what());
55  }
56  catch (...)
57  {
58  ROS_FATAL("Exception during destruction of PickClient");
59  }
60 }
61 
63 {
65 }
66 
68 {
70 }
71 
72 void PickClient::paramsToCfg(const json& params, rc_pick_client::pickModuleConfig& cfg)
73 {
74  for (const auto& param : params)
75  {
76  const auto& name = param["name"];
77  if (name == "max_grasps")
78  {
79  cfg.max_grasps = param["value"];
80  }
81  if (name == "cluster_max_curvature")
82  {
83  cfg.cluster_max_curvature = param["value"];
84  }
85  else if (name == "cluster_max_dimension")
86  {
87  cfg.cluster_max_dimension = param["value"];
88  }
89  else if (name == "clustering_discontinuity_factor")
90  {
91  cfg.clustering_discontinuity_factor = param["value"];
92  }
93  else if (name == "clustering_max_surface_rmse")
94  {
95  cfg.clustering_max_surface_rmse = param["value"];
96  }
97  else if (name == "clustering_patch_size")
98  {
99  cfg.clustering_patch_size = param["value"];
100  }
101  }
102 }
103 
105 {
106  rc_pick_client::pickModuleConfig cfg;
107 
108  // first get the current values from sensor
109  const auto j_params = rest_helper_.getParameters();
110 
111  paramsToCfg(j_params, cfg);
112 
113  // second, try to get ROS parameters:
114  // if parameter is not set in parameter server, we default to current sensor configuration
115  nh_.param("cluster_max_curvature", cfg.cluster_max_curvature, cfg.cluster_max_curvature);
116  nh_.param("cluster_max_dimension", cfg.cluster_max_dimension, cfg.cluster_max_dimension);
117  nh_.param("clustering_discontinuity_factor", cfg.clustering_discontinuity_factor,
118  cfg.clustering_discontinuity_factor);
119  nh_.param("clustering_max_surface_rmse", cfg.clustering_max_surface_rmse, cfg.clustering_max_surface_rmse);
120  nh_.param("clustering_patch_size", cfg.clustering_patch_size, cfg.clustering_patch_size);
121  nh_.param("max_grasps", cfg.max_grasps, cfg.max_grasps);
122 
123  // set parameters on parameter server so that dynamic reconfigure picks them up
124  nh_.setParam("cluster_max_curvature", cfg.cluster_max_curvature);
125  nh_.setParam("cluster_max_dimension", cfg.cluster_max_dimension);
126  nh_.setParam("clustering_discontinuity_factor", cfg.clustering_discontinuity_factor);
127  nh_.setParam("clustering_max_surface_rmse", cfg.clustering_max_surface_rmse);
128  nh_.setParam("clustering_patch_size", cfg.clustering_patch_size);
129  nh_.setParam("max_grasps", cfg.max_grasps);
130 
131  // instantiate dynamic reconfigure server that will initially read those values
132  using ReconfServer = dynamic_reconfigure::Server<rc_pick_client::pickModuleConfig>;
133  server_ = std::unique_ptr<ReconfServer>(new ReconfServer(nh_));
134 }
135 
136 json PickClient::createSharedParameters(rc_pick_client::pickModuleConfig& config)
137 {
138  // fill json request from dynamic reconfigure request
139  json js_params, js_param;
140  js_param["name"] = "cluster_max_curvature";
141  js_param["value"] = config.cluster_max_curvature;
142  js_params.push_back(js_param);
143  js_param["name"] = "clustering_discontinuity_factor";
144  js_param["value"] = config.clustering_discontinuity_factor;
145  js_params.push_back(js_param);
146  js_param["name"] = "clustering_max_surface_rmse";
147  js_param["value"] = config.clustering_max_surface_rmse;
148  js_params.push_back(js_param);
149  js_param["name"] = "max_grasps";
150  js_param["value"] = config.max_grasps;
151  js_params.push_back(js_param);
152  return js_params;
153 }
154 
155 } // namespace ros_pick_client
ros::NodeHandle::setParam
void setParam(const std::string &key, bool b) const
ros_pick_client::PickClient::~PickClient
virtual ~PickClient()
Definition: pick_client.cpp:46
pick_client.h
ros_pick_client::PickClient::initConfiguration
void initConfiguration()
Definition: pick_client.cpp:104
ros_pick_client::PickClient::rest_helper_
rc_rest_api::RestHelper rest_helper_
Definition: pick_client.h:64
ros_pick_client::PickClient::nh_
ros::NodeHandle nh_
Definition: pick_client.h:61
ros_pick_client::PickClient::paramsToCfg
void paramsToCfg(const json &params, rc_pick_client::pickModuleConfig &cfg)
Definition: pick_client.cpp:72
ros_pick_client::PickClient::stopPick
void stopPick()
Definition: pick_client.cpp:67
json
nlohmann::json json
Definition: pick_client.h:49
rc_rest_api::RestHelper::servicePutRequest
json servicePutRequest(const std::string &service_name)
Definition: rest_helper.cpp:92
ROS_FATAL
#define ROS_FATAL(...)
ros_pick_client::PickClient::startPick
void startPick()
Definition: pick_client.cpp:62
ros_pick_client::PickClient::PickClient
PickClient(const std::string &host, const std::string &node_name, const ros::NodeHandle &nh)
Definition: pick_client.cpp:39
ros_pick_client::PickClient::server_
std::unique_ptr< dynamic_reconfigure::Server< rc_pick_client::pickModuleConfig > > server_
Definition: pick_client.h:62
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
param
T param(const std::string &param_name, const T &default_val)
ros_pick_client::PickClient::createSharedParameters
json createSharedParameters(rc_pick_client::pickModuleConfig &config)
Definition: pick_client.cpp:136
rc_rest_api::RestHelper::getParameters
json getParameters()
Definition: rest_helper.cpp:112
ros_pick_client
Definition: boxpick_client.cpp:35
ros::NodeHandle


rc_pick_client
Author(s): Monika Florek-Jasinska
autogenerated on Sun May 15 2022 02:24:50