pose_prediction.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <map>
20 #include <string>
21 
22 //Pkg includes
23 #include "pose_prediction_ism/shortest_path.h"
24 #include "pose_prediction_ism/old_prediction_non_normalized.h"
25 #include "pose_prediction_ism/old_prediction_normalized.h"
26 #include "pose_prediction_ism/best_path.h"
27 #include "pose_prediction_ism/random_path.h"
28 
29 //Local includes
32 
33 using namespace std;
34 using namespace ros;
35 using namespace asr_msgs;
36 using namespace std_srvs;
37 using namespace ISM;
38 using namespace VIZ;
39 
41 {
42 
43 PosePrediction::PosePrediction(std::vector<ISM::RecognitionResultPtr>& results_already_shared, SharedRecognitionResultsManagerPtr shared_recognition_results_ptr):
44  results_already_shared_(results_already_shared),
45  node_handle_(ros::this_node::getName())
46 {
47 
48  ParamHelper ph("Pose Prediction", node_handle_);
49  /* ----------------- Names ------------------ */
50  string point_cloud_server_name;
51  string pose_prediction_markers_publisher_name;
52  string scene_markers_pose_prediction_publisher_name;
53  string found_object_client_name;
54  bool latched;
55 
56  ph.getParam<string>("foundObjectClientName", &found_object_client_name, "");
57  ph.getParam<string>("sceneMarkersPosePredictionPublisherName", &scene_markers_pose_prediction_publisher_name, "");
58  ph.getParam<string>("posePredictionMarkersPublisherName", &pose_prediction_markers_publisher_name, "");
59  ph.getParam<string>("PointCloudServerName", &point_cloud_server_name, "");
60  ph.getParam<bool>("latched", &latched, false);
61 
62  found_object_client_ = node_handle_.serviceClient<asr_world_model::GetFoundObjectList>(found_object_client_name);
63  ROS_ASSERT_MSG(found_object_client_.waitForExistence(), "Please start the world model");
64 
66 
67  pose_prediction_markers_publisher_ = node_handle_.advertise<visualization_msgs::MarkerArray>(pose_prediction_markers_publisher_name, 1000, latched);
68  pose_prediction_visualizer_ptr_ = PosePredictionVisualizerPtr(new PosePredictionVisualizer(pose_prediction_markers_publisher_, ros::NodeHandle(node_handle_.getNamespace() + "/pose_prediction_visualizer/")));
69 
70  scene_markers_publisher_ = node_handle_.advertise<visualization_msgs::MarkerArray>(scene_markers_pose_prediction_publisher_name, 1000, latched);
71  res_visualizer_ = ISMResultVisualizerRVIZPtr(new ISMResultVisualizerRVIZ(scene_markers_publisher_, ros::NodeHandle(node_handle_.getNamespace() + "/pose_prediction_result_visualizer/")));
72 
73  ROS_INFO("PosePrediction: Visualization initialized.");
74 
75 
76  /* ----------------- Pose prediction attributes------------------ */
77  ph.getParam<string>("dbfilename", &database_filename_, "");
78  ph.getParam<double>("predictionGenerationFactor", &prediction_generation_factor_, 1);
79 
80  /* ----------------- Resampler attributes------------------ */
81  bool equal_results_number;
82  unsigned int importance_resampled_size;
83 
84  ph.getParam<bool>("equal_results_number", &equal_results_number, true);
85  ph.getParam<unsigned int>("importance_resampled_size", &importance_resampled_size, 10);
86  resampler_ptr_ = ResamplerPtr(new Resampler(equal_results_number, importance_resampled_size, shared_recognition_results_ptr));
87 
88  ROS_INFO("PosePrediction: Resampler initialized.");
89 
90  /* ----------------- ROS & Viz initialization ------------------ */
91 
92  reconfigure_server_ = new dynamic_reconfigure::Server<asr_recognizer_prediction_ism::pose_predictionConfig>(ros::NodeHandle(node_handle_.getNamespace() + "/pose_prediction/"));
93  dynamic_reconfigure::Server<asr_recognizer_prediction_ism::pose_predictionConfig>::CallbackType reconf_callback = boost::bind(&PosePrediction::dynamicReconfCallback, this, _1, _2);
94  reconfigure_server_->setCallback(reconf_callback);
95 
96 }
97 
98 
99 void PosePrediction::dynamicReconfCallback(asr_recognizer_prediction_ism::pose_predictionConfig &config, uint32_t level){
100  ROS_DEBUG_NAMED("PosePrediction", "Callback PosePrediction::dynamicReconfCallback() is called.");
101 
102  switch(config.posePredictor){
103  case 0:
104  pose_predictor_ptr_ = pose_prediction_ism::BestPathPtr(new pose_prediction_ism::BestPath(database_filename_));
105  break;
106  case 1:
107  pose_predictor_ptr_ = pose_prediction_ism::PaperPredictionNonNormalizedPtr(new pose_prediction_ism::PaperPredictionNonNormalized(database_filename_));
108  break;
109  case 2:
110  pose_predictor_ptr_ = pose_prediction_ism::PaperPredictionNormalizedPtr(new pose_prediction_ism::PaperPredictionNormalized(database_filename_));
111  break;
112  case 3:
113  pose_predictor_ptr_ = pose_prediction_ism::RandomPathPtr(new pose_prediction_ism::RandomPath(database_filename_));
114  break;
115  case 4:
116  pose_predictor_ptr_ = pose_prediction_ism::ShortestPathPtr(new pose_prediction_ism::ShortestPath(database_filename_));
117  break;
118  default:
119  pose_predictor_ptr_ = pose_prediction_ism::ShortestPathPtr(new pose_prediction_ism::ShortestPath(database_filename_));
120  break;
121  }
122  pose_predictor_ptr_->setPredictionGenerationFactor(prediction_generation_factor_);
123  ROS_INFO_STREAM("Using " << pose_predictor_ptr_ << " for pose prediction.");
124 
125  is_visualization_active_ = config.enableVisualization;
126  ROS_INFO_COND(is_visualization_active_, "Visualization is active");
127  ROS_WARN_COND(!is_visualization_active_, "Visualization is not active");
128 
129 }
130 
131 bool PosePrediction::processGetPointCloudServiceCall(asr_recognizer_prediction_ism::GetPointCloud::Request &req,
132  asr_recognizer_prediction_ism::GetPointCloud::Response &res)
133 {
134  ROS_INFO("\n==================================================\n");
135 
136  ROS_INFO("Callback PosePrediction::processGetPointCloudServiceCall() is called.");
137 
138  vector<AsrObject> found_objects;
139  for (asr_msgs::AsrObject asr_object : req.objects)
140  {
141  ROS_ASSERT_MSG(!asr_object.sampledPoses.empty(),"Object estimate with no samples not allowed.");
142  found_objects.push_back(asr_object);
143  }
144 
145 
146  //Ask world model if we have found any objects during asr => Missing objects for prediction != objects not in scene recognition results.
147  if (!found_objects.empty())
148  {
149  stringstream output;
150  output << "Summary of results from PosePrediction::processGetPointCloudServiceCall() in rp_ism_node." << endl;
151 
152  pose_predictor_ptr_->setFoundObjects(found_objects);
153  output << "These results come from importance (re)sampling and are used for pose prediction:";
154  //Ask if there are any scene recognition results sampled from shared memory to process. Need reference of scene recognition result for pose prediction.
155  if (resampler_ptr_->recognitionResultsAvailable())
156  {
157 
158  pose_prediction_visualizer_ptr_->clearAllMarkerOfTopic();
159  res_visualizer_->clearAllMarkerOfTopic();
160 
161  ROS_DEBUG_NAMED("PosePrediction", "Flushed visualization for pose prediction.");
162 
163  //Here actual resampling of recognition results for pose prediction takes place.
164  std::vector<RecognitionResult> sampled_results = resampler_ptr_->drawSamples();
165  res_visualizer_->setSceneCount(sampled_results.size());
166 
167  AsrAttributedPointCloud union_attributed_point_cloud;
168  set<pair<string,string > > predicted_objects;
169 
170  char result_specifier = 'a';
171 
172  ROS_INFO_STREAM("List of recognition results used to predict poses with their relative summarized votes (percentages):");
173  for (unsigned int i = 0; i < sampled_results.size(); i ++) {
174  ISM::RecognitionResult result = sampled_results[i];
175  ROS_INFO_STREAM("i: " << i << " for patternName: " << result.patternName << " with %: " << 1.0 / sampled_results.size());
176  }
177 
178  int i = 0;
179  for (RecognitionResult recognition_result : sampled_results)
180  {
181 
182  //Save recognition result just extracted by importance sampling to temporary buffer until initial search reached again.
183  results_buffer_.push_back(RecognitionResultPtr(new RecognitionResult(recognition_result)));
184  output << endl << recognition_result << endl;
185 
186  //May add reference
187  visualize(recognition_result, &result_specifier, &i, sampled_results.size(), &union_attributed_point_cloud, &predicted_objects);
188  }
189 
190  output << "Poses have been predicted for the following objects: " << endl;
191 
192  for (pair<string, string> predicted_object : predicted_objects)
193  output << "[" << predicted_object.first << ", " << predicted_object.second << "], ";
194  output << endl;
195 
196  pose_prediction_visualizer_ptr_->publishCollectedMarkers();
197 
198  res_visualizer_->publishCollectedMarkers();
199 
200  checkPointCloud(union_attributed_point_cloud);
201  //Return merged pointcloud in service call response.y
202  res.point_cloud = union_attributed_point_cloud;
203  res.output = output.str();
204 
205  ROS_INFO("Successfully finished pose prediction for given recognition results.");
206 
207  ROS_INFO("\n==================================================\n");
208  return true;
209  }
210  else
211  {
212  ROS_ERROR("No recognition results available for pose predicition. Returning empty point cloud.");
213  AsrAttributedPointCloud cloud;
214  res.point_cloud = cloud;
215  res.output = "No recognition results available for pose predicition. Returning empty point cloud.";
216 
217  ROS_INFO_STREAM("Pushing already used results from this indirect-based run into buffer: " << results_buffer_.size());
218  //Save buffered recognition results from current indirect-based object search run to prevent using them again.
220  //Reset buffer for next indirect-based object search execution.
221  results_buffer_.clear();
222 
223  ROS_INFO("\n==================================================\n");
224  return true;
225  }
226  }
227  else
228  {
229  ROS_ERROR("Failed to call the service get_found_objects");
230 
231  ROS_INFO("\n==================================================\n");
232  return false;
233  }
234 }
235 
236 void PosePrediction::visualize(RecognitionResult recognition_result, char *result_specifier, int *i, unsigned int sampled_results_size, AsrAttributedPointCloud *union_attributed_point_cloud, set<pair<string, string>> *predicted_objects){
237  //Get reference pose of recognition result as starting point (or better pose) for pose prediction.
238  PosePtr reference_pose_ptr = recognition_result.referencePose;
239  //This is the actual pose prediction call to asr_lib_pose_prediction_ism.
240  AsrAttributedPointCloud current_point_cloud =
241  pose_predictor_ptr_->predictUnfoundPoses(reference_pose_ptr, recognition_result.patternName, 1.0 / sampled_results_size);
242  (*i)++;
243 
244  //Merge pose prediction results for current recognition result with results from other recognition results, used too.
245  for (AsrAttributedPoint attributed_point : current_point_cloud.elements)
246  {
247  union_attributed_point_cloud->elements.push_back(attributed_point);
248  predicted_objects->insert(make_pair(attributed_point.type, attributed_point.identifier));
249  }
250 
252  {
253  res_visualizer_->addVisualization(RecognitionResultPtr(new RecognitionResult(recognition_result)));
254  string pose_prediction_name_space = pose_predictor_ptr_->getMarkerNameSpace() + "_" + (*result_specifier);
255  pose_prediction_visualizer_ptr_->addPosePredictionVisualization(PosePtr(reference_pose_ptr), current_point_cloud, pose_prediction_name_space);
256  (*result_specifier)++;
257  }
258 }
259 
260 void PosePrediction::checkPointCloud(AsrAttributedPointCloud attributed_point_cloud)
261 {
262  map<string, map<string, unsigned int> > currentMap;
263  for (AsrAttributedPoint attributed_point : attributed_point_cloud.elements)
264  currentMap[attributed_point.type][attributed_point.identifier]++;
265 
266  ROS_INFO_STREAM("Attributed pointcloud from pose prediction contains:" << endl);
267  for (map<string, map<string,unsigned int> >::iterator typeIt = currentMap.begin(); typeIt != currentMap.end(); ++typeIt)
268  for (map<string, unsigned int>::iterator idIt = typeIt->second.begin(); idIt != typeIt->second.end(); ++idIt)
269  ROS_INFO_STREAM(idIt->second << " predicted poses for object "<< "(" << typeIt->first << "," << idIt->first << ").");
270 
271 }
272 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
VIZ::PosePredictionVisualizerPtr pose_prediction_visualizer_ptr_
dynamic_reconfigure::Server< asr_recognizer_prediction_ism::pose_predictionConfig > * reconfigure_server_
std::string getName(void *handle)
std::vector< ISM::RecognitionResultPtr > results_buffer_
std::vector< ISM::RecognitionResultPtr > & results_already_shared_
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
#define ROS_DEBUG_NAMED(name,...)
void checkPointCloud(asr_msgs::AsrAttributedPointCloud attributed_point_cloud)
#define ROS_INFO(...)
#define ROS_ASSERT_MSG(cond,...)
pose_prediction_ism::PosePredictorPtr pose_predictor_ptr_
const std::string & getNamespace() const
void dynamicReconfCallback(asr_recognizer_prediction_ism::pose_predictionConfig &config, uint32_t level)
void getParam(std::string param_name, T *variable, T default_value)
Definition: param_helper.h:28
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool processGetPointCloudServiceCall(asr_recognizer_prediction_ism::GetPointCloud::Request &req, asr_recognizer_prediction_ism::GetPointCloud::Response &res)
RecognizerPredictionISM::processGetPointCloudServiceCall Callback function for a GetPointCloud Reques...
VIZ::ISMResultVisualizerRVIZPtr res_visualizer_
void visualize(ISM::RecognitionResult recognition_result, char *result_specifier, int *i, unsigned int sampled_results_size, asr_msgs::AsrAttributedPointCloud *union_attributed_point_cloud, std::set< std::pair< std::string, std::string >> *predicted_objects)
#define ROS_INFO_STREAM(args)
#define ROS_WARN_COND(cond,...)
#define ROS_INFO_COND(cond,...)
#define ROS_ERROR(...)
boost::shared_ptr< Resampler > ResamplerPtr
Definition: resampler.h:53


asr_recognizer_prediction_ism
Author(s): Aumann Florian, Heller Florian, Hutmacher Robin, Meißner Pascal, Stöckle Patrick, Stroh Daniel
autogenerated on Wed Jan 8 2020 03:18:32