pose_selector.cpp
Go to the documentation of this file.
1 #include <mutex>
2 #include <string>
3 #include <strings.h>
4 #include <algorithm>
5 
6 #include <ros/ros.h>
7 
8 #include <unique_id/unique_id.h>
9 #include <uuid_msgs/UniqueID.h>
10 
11 #include <ram_msgs/AdditiveManufacturingTrajectory.h>
12 #include <ram_msgs/AdditiveManufacturingPose.h>
13 
14 #include <ram_modify_trajectory/AddToSelection.h>
15 #include <ram_modify_trajectory/EraseSelection.h>
16 #include <ram_modify_trajectory/GetPosesFromLayer.h>
17 #include <ram_modify_trajectory/GetPosesFromLayersList.h>
18 #include <ram_modify_trajectory/GetPosesFromTrajectory.h>
19 #include <ram_modify_trajectory/GetSelection.h>
20 #include <ram_modify_trajectory/InvertSelection.h>
21 #include <ram_modify_trajectory/RemoveFromSelection.h>
22 
23 std::mutex trajectory_mutex;
24 ram_msgs::AdditiveManufacturingTrajectory layers;
25 
27 std::vector<ram_msgs::AdditiveManufacturingPose> selection;
28 
29 void saveTrajectoryCallback(const ram_msgs::AdditiveManufacturingTrajectoryConstPtr& msg)
30 {
31  std::lock_guard<std::mutex> lock_1(trajectory_mutex);
32  std::lock_guard<std::mutex> lock_2(selection_params_mutex);
33 
34  layers = *msg;
35  selection.clear();
36 }
37 
38 bool getPosesFromTrajectoryCallback(ram_modify_trajectory::GetPosesFromTrajectory::Request &req,
39  ram_modify_trajectory::GetPosesFromTrajectory::Response &res)
40 {
41  std::lock_guard<std::mutex> lock(trajectory_mutex);
42 
43  if (layers.poses.empty())
44  return false;
45 
46  if (req.pose_index_list.empty())
47  return false;
48 
49  std::vector<unsigned> pose_index_list = req.pose_index_list;
50  std::sort(pose_index_list.begin(), pose_index_list.end()); // Sort the list of index
51 
52  if (pose_index_list.back() >= layers.poses.size())
53  return false;
54 
55  for (auto pose_index : pose_index_list)
56  res.poses.push_back(layers.poses[pose_index]);
57 
58  return true;
59 }
60 
61 bool getPosesFromLayersListCallback(ram_modify_trajectory::GetPosesFromLayersList::Request &req,
62  ram_modify_trajectory::GetPosesFromLayersList::Response &res)
63 {
64  std::lock_guard<std::mutex> lock(trajectory_mutex);
65 
66  if (layers.poses.empty())
67  return false;
68 
69  if (req.layer_level_list.empty())
70  return false;
71 
72  if (req.layer_level_list.back() > layers.poses.back().layer_level)
73  return false;
74 
75  // Find poses
76  for (auto pose : layers.poses)
77  {
78  for (auto current_layer_level : req.layer_level_list)
79  {
80  if (pose.layer_level != current_layer_level)
81  continue;
82 
83  res.poses.push_back(pose);
84  break;
85  }
86  }
87  return true;
88 }
89 
90 bool getPosesFromLayerCallback(ram_modify_trajectory::GetPosesFromLayer::Request &req,
91  ram_modify_trajectory::GetPosesFromLayer::Response &res)
92 {
93  std::lock_guard<std::mutex> lock(trajectory_mutex);
94 
95  if (layers.poses.empty())
96  return false;
97 
98  if (req.index_list_relative.empty())
99  return false;
100 
101  if (req.layer_level > layers.poses.back().layer_level)
102  return false;
103 
104  std::vector<unsigned> index_list_relative = req.index_list_relative;
105  std::sort(index_list_relative.begin(), index_list_relative.end()); // Sort the list of index
106 
107  // Copy the poses of the layer in a new vector
108  std::vector<ram_msgs::AdditiveManufacturingPose> layer;
109  for (auto &pose : layers.poses)
110  if (pose.layer_level == req.layer_level)
111  layer.push_back(pose);
112 
113  // Layer is empty
114  if (layer.empty())
115  return false;
116 
117  // There are invalid indices in the request
118  if (index_list_relative.back() >= layer.size())
119  return false;
120 
121  // Save the poses
122  for (unsigned local_index(0); local_index < layer.size(); ++local_index)
123  {
124  if (index_list_relative.empty())
125  break;
126 
127  if (local_index == index_list_relative.front())
128  {
129  res.poses.push_back(layer[local_index]); // Previous checks makes sure this pose exists
130  index_list_relative.erase(index_list_relative.begin());
131  }
132 
133  }
134  return true;
135 }
136 
137 bool addToSelectionCallback(ram_modify_trajectory::AddToSelection::Request &req,
138  ram_modify_trajectory::AddToSelection::Response &res)
139 {
140  std::lock_guard<std::mutex> lock(selection_params_mutex);
141 
142  unsigned selection_size = selection.size(); // size before insert new poses
143 
144  for (auto request_pose : req.poses)
145  {
146  bool is_duplicate = false;
147  // verify duplicate
148  std::string request_uuid = unique_id::toHexString(request_pose.unique_id);
149  for (unsigned i(0); i < selection_size; ++i)
150  {
151  std::string selection_uuid = unique_id::toHexString(selection[i].unique_id);
152  if (request_uuid.compare(selection_uuid) != 0) // uuid are not equals
153  continue;
154 
155  is_duplicate = true;
156  break;
157  }
158 
159  if (!is_duplicate)
160  selection.push_back(request_pose);
161  }
162  res.selection = selection;
163  return true;
164 }
165 
166 bool removeFromSelectionCallback(ram_modify_trajectory::RemoveFromSelection::Request &req,
167  ram_modify_trajectory::RemoveFromSelection::Response &res)
168 {
169  std::lock_guard<std::mutex> lock(selection_params_mutex);
170 
171  for (auto request_pose : req.poses)
172  {
173  // verify duplicate
174  std::string request_uuid = unique_id::toHexString(request_pose.unique_id);
175  for (unsigned i(0); i < selection.size(); ++i)
176  {
177  std::string selection_uuid = unique_id::toHexString(selection[i].unique_id);
178  if (request_uuid.compare(selection_uuid) != 0) // uuid are not equals
179  continue;
180 
181  selection.erase(selection.begin() + i);
182  break;
183  }
184  }
185  res.selection = selection;
186  return true;
187 }
188 
189 bool getSelectionCallback(ram_modify_trajectory::GetSelection::Request &,
190  ram_modify_trajectory::GetSelection::Response &res)
191 {
192  std::lock_guard<std::mutex> lock(selection_params_mutex);
193  res.selection = selection;
194  return true;
195 }
196 
197 bool eraseSelectionCallback(ram_modify_trajectory::EraseSelection::Request &,
198  ram_modify_trajectory::EraseSelection::Response &res)
199 {
200  std::lock_guard<std::mutex> lock(selection_params_mutex);
201  selection.clear();
202 
203  res.selection = selection;
204  return true;
205 }
206 
207 bool invertSelectionCallback(ram_modify_trajectory::InvertSelection::Request &req,
208  ram_modify_trajectory::InvertSelection::Response &res)
209 {
210  std::lock_guard<std::mutex> lock(selection_params_mutex);
211 
212  for (auto request_pose : req.poses)
213  {
214  bool is_duplicate = false;
215  // verify duplicate
216  std::string request_uuid = unique_id::toHexString(request_pose.unique_id);
217  for (unsigned i(0); i < selection.size(); ++i)
218  {
219  std::string selection_uuid = unique_id::toHexString(selection[i].unique_id);
220  if (request_uuid.compare(selection_uuid) != 0) // uuid are not equals
221  continue;
222 
223  is_duplicate = true;
224  selection.erase(selection.begin() + i); // Remove
225  break;
226  }
227 
228  if (!is_duplicate)
229  selection.push_back(request_pose); // Add
230  }
231  res.selection = selection;
232  return true;
233 }
234 
235 int main(int argc,
236  char **argv)
237 {
238  ros::init(argc, argv, "pose_selector");
240 
241  // Subscribe on "ram/trajectory"
242  ros::Subscriber sub = nh.subscribe("ram/trajectory", 10, saveTrajectoryCallback);
243 
244  // Select poses
245  ros::ServiceServer service_1 = nh.advertiseService("ram/pose_selector/get_poses_from_trajectory",
247  ros::ServiceServer service_2 = nh.advertiseService("ram/pose_selector/get_poses_from_layers_list",
249  ros::ServiceServer service_3 = nh.advertiseService("ram/pose_selector/get_poses_from_layer",
251  // Modify Selection
252  ros::ServiceServer service_4 = nh.advertiseService("ram/pose_selector/add_to_selection", addToSelectionCallback);
253  ros::ServiceServer service_5 = nh.advertiseService("ram/pose_selector/remove_from_selection",
255  ros::ServiceServer service_6 = nh.advertiseService("ram/pose_selector/get_selection", getSelectionCallback);
256  ros::ServiceServer service_7 = nh.advertiseService("ram/pose_selector/erase_selection", eraseSelectionCallback);
257  ros::ServiceServer service_8 = nh.advertiseService("ram/pose_selector/invert_selection", invertSelectionCallback);
258 
260  spinner.start();
262  return 0;
263 }
void saveTrajectoryCallback(const ram_msgs::AdditiveManufacturingTrajectoryConstPtr &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::vector< ram_msgs::AdditiveManufacturingPose > selection
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
std::unique_ptr< ros::NodeHandle > nh
void spinner()
std::mutex trajectory_mutex
ram_msgs::AdditiveManufacturingTrajectory layers
bool getPosesFromLayersListCallback(ram_modify_trajectory::GetPosesFromLayersList::Request &req, ram_modify_trajectory::GetPosesFromLayersList::Response &res)
bool removeFromSelectionCallback(ram_modify_trajectory::RemoveFromSelection::Request &req, ram_modify_trajectory::RemoveFromSelection::Response &res)
bool invertSelectionCallback(ram_modify_trajectory::InvertSelection::Request &req, ram_modify_trajectory::InvertSelection::Response &res)
static std::string toHexString(boost::uuids::uuid const &uu)
std::mutex selection_params_mutex
bool getSelectionCallback(ram_modify_trajectory::GetSelection::Request &, ram_modify_trajectory::GetSelection::Response &res)
int main(int argc, char **argv)
bool getPosesFromTrajectoryCallback(ram_modify_trajectory::GetPosesFromTrajectory::Request &req, ram_modify_trajectory::GetPosesFromTrajectory::Response &res)
bool addToSelectionCallback(ram_modify_trajectory::AddToSelection::Request &req, ram_modify_trajectory::AddToSelection::Response &res)
bool eraseSelectionCallback(ram_modify_trajectory::EraseSelection::Request &, ram_modify_trajectory::EraseSelection::Response &res)
ROSCPP_DECL void waitForShutdown()
bool getPosesFromLayerCallback(ram_modify_trajectory::GetPosesFromLayer::Request &req, ram_modify_trajectory::GetPosesFromLayer::Response &res)


ram_modify_trajectory
Author(s): Andres Campos - Institut Maupertuis
autogenerated on Mon Jun 10 2019 14:50:00