SceneInferenceEngine.cpp
Go to the documentation of this file.
1 
19 
21 
23  : mNodeHandle("~")
24  {
25  // The ROS topic to listen to.
26  std::string pPbdObjectTopic;
27 
28  // The ROS topic for scene graph messages to listen to.
29  std::string pPbdSceneGraphTopic;
30 
31  // Name of the XML file containing the scnene model.
32  std::string sceneModelFileName;
33 
34  // The name of the algorithm that should be used for the inference.
35  std::string inferenceAlgorithm;
36 
37  // The frame to transform the object poses to.
38  // Also the coordinate frame in which the visualization should take place.
39  std::string baseFrameId;
40 
41  // The visualization is pretty small, this scale factor enlarges it.
42  double scaleFactor;
43 
44  // This factor determines the radii of the covariance ellipse.
45  double sigmaMultiplicator;
46 
47  // Try to get the clearance for plotting the scene probabilties.
48  if(!mNodeHandle.getParam("plot", showPlot))
49  throw std::runtime_error("Please specify parameter " + std::string("plot") + " when starting this node.");
50 
51  // Try to get the name of the topic to listen to for new evidences.
52  if(!mNodeHandle.getParam("object_topic", pPbdObjectTopic))
53  throw std::runtime_error("Please specify parameter " + std::string("object_topic") + " when starting this node.");
54 
55  // Try to get the name of the topic to listen to for scene graph messages.
56  if(!mNodeHandle.getParam("scene_graph_topic", pPbdSceneGraphTopic))
57  throw std::runtime_error("Please specify parameter " + std::string("scene_graph_topic") + " when starting this node.");
58 
59  // Try to get the file name of the XML file from which the scene model should be read.
60  if(!mNodeHandle.getParam("scene_model_filename", sceneModelFileName))
61  throw std::runtime_error("Please specify parameter " + std::string("scene_model_filename") + " when starting this node.");
62 
63  // Try to get the name of the scene to be published.
64  if(!mNodeHandle.getParam("base_frame_id", baseFrameId))
65  throw std::runtime_error("Please specify parameter " + std::string("base_frame_id") + " when starting this node.");
66 
67  // Try to get the visualization scale factor.
68  if(!mNodeHandle.getParam("scale_factor", scaleFactor))
69  throw std::runtime_error("Please specify parameter " + std::string("scale_factor") + " when starting this node.");
70 
71  // Try to get the sigma multiplicator.
72  if(!mNodeHandle.getParam("sigma_multiplicator", sigmaMultiplicator))
73  throw std::runtime_error("Please specify parameter " + std::string("sigma_multiplicator") + " when starting this node.");
74 
75  // Try to get the targeting help flag.
76  if(!mNodeHandle.getParam("targeting_help", mTargetingHelp))
77  throw std::runtime_error("Please specify parameter " + std::string("targeting_help") + " when starting this node.");
78 
79  // Try to get the name of the inference algorithm.
80  if(!mNodeHandle.getParam("inference_algorithm", inferenceAlgorithm))
81  throw std::runtime_error("Please specify parameter " + std::string("inference_algorithm") + " when starting this node.");
82 
83  // Initialize the transformations of objects into the given frame.
84  mObjectTransform.setBaseFrame(baseFrameId);
85 
86  // Initialize the scene model with the parameters given on startup of this node.
87  loadSceneModel(sceneModelFileName, inferenceAlgorithm);
88 
89  // Initialize the visualization chain.
90  initializeVisualizationChain(scaleFactor, sigmaMultiplicator, baseFrameId);
91 
92  // Tell node how to react on messages from objects that could belong to scenes being looked for.
94  }
95 
97  {
98  }
99 
101  {
102  // Status information for the user.
103  ROS_DEBUG_STREAM("Updating inference engine.");
104 
105  /********************************************************************
106  * Integrate the collected evidence into the model.
107  ********************************************************************/
108 
109  // Process evidences!
110  while(!mEvidenceBuffer.empty())
111  {
112  // Get the first entry.
113 
114  boost::shared_ptr<asr_msgs::AsrObject> evidence = mEvidenceBuffer.front();//HERE: mEvidenceBuffer.front();
115 
116 
117  // Remove the entry processed from the queue.
118  mEvidenceBuffer.pop();
119 
120  // Status information for the user.
121  ROS_INFO_STREAM("Object of type '" << evidence->type << "' found.");
122 
123  try{
124  // Try to transform evidence into target coordinate system.
125  mObjectTransform.transform(evidence);
126  }
127  catch(std::exception& exception){
128  // No transformation found, dropping object!
129  ROS_ERROR("Unable to resolve transformation in target coordinate frame. Dropping object.");
130  continue;
131  }
132 
133  mModel.integrateEvidence(AsrObjectToISMObject(evidence)); //HERE: evidence
134  }
135 
136  // Update the model with the evidence collected until now.
138 
139  /********************************************************************
140  * Do the inference and show the results.
141  ********************************************************************/
142 
143  // Get the results and show them.
144  std::vector<SceneIdentifier> pSceneList;
146 
147  printf("===========================================");
148  printf("This are the scene probabilities:\n");
149  for(SceneIdentifier i : pSceneList)
150  printf(" -> %s (%s): %f (%f)\n", i.mDescription.c_str(), i.mType.c_str(), i.mLikelihood, i.mPriori);
151 
152  // Show plot of scene probabilities?
153  if(showPlot)
154  {
155  // List all scenes to get the labels required for the bar diagram and the associated probabilities.
156  std::vector<std::string> barLabels;
157  std::map<std::string, float> currentData;
158  for(SceneIdentifier i : pSceneList)
159  {
160  barLabels.push_back(i.mDescription);
161  currentData.insert(std::pair<std::string, float>(i.mDescription, i.mLikelihood));
162  }
163 
164  // Update visualizer with new data and visualize!
165  mVisGnuplot.updateBarChartValues(currentData);
166  mVisGnuplot.sendBarChartToGnuplot(true);
167  }
168 
169  /********************************************************************
170  * Visualize the scene.
171  ********************************************************************/
172 
173  // Do the visualization.
174  if(mTargetingHelp)
175  mVisualizer->drawInTargetingMode();
176  else
177  mVisualizer->drawInInferenceMode();
178  }
179 
181  {
182  // Try to get the bag path. We read it here so it doesn't throw any errors in online mode.
183  std::string bagPath;
184  if(!mNodeHandle.getParam("bag_path", bagPath))
185  throw std::runtime_error("Please specify parameter " + std::string("bag_path") + " when starting this node.");
186 
187  ROS_INFO_STREAM("Extracting AsrObject messages from rosbag file: " << bagPath);
188 
189  // Check whether topic name for scene graph has been set before trying to parse rosbag files.
190  if(!mObjectListener)
191  throw std::logic_error("Cannot parse bag file with AsrObjects without knowing on which topic they were sent.");
192 
193  // Set topic representatio. When parsing rosbag files this is required for extracting the messages which are representing scene graphs.
194  rosbag::TopicQuery pbdSceneGraphIdentifier(mObjectListener.getTopic());
195 
196  // Create file handler for rosbag file to be read.
197  rosbag::Bag pbdObjectsBag;
198 
199  // Get read-only access to messages in given rosbag file, create access infrastructure.
200  try {
201  pbdObjectsBag.open(bagPath, rosbag::bagmode::Read);
202  } catch(rosbag::BagIOException& exception) {
203  // ROS_ERROR does not work here.
204  std::cerr << "Trying to extract AsrObject messages aborted because of: " << exception.what() << std::endl;
205  // Quit this function as no data is to be processed.
206  return;
207  }
208 
209  // Create interface to extract only scene graph messages in given bag file from a previously defined topic.
210  rosbag::View pbdSceneGraphView(pbdObjectsBag, pbdSceneGraphIdentifier);
211 
212  // Check whether there is any raw data from a scene on the topic where we expect them.
213  if(!pbdSceneGraphView.size())
214  ROS_WARN_STREAM("No AsrObject messages exist in " << bagPath << " on topic " << mObjectListener.getTopic() << ".");
215 
216  // Get access to all scene graphs in bag file to transfer them to parameter learner for scene model.
217  for(rosbag::View::iterator sceneGraphIterator = pbdSceneGraphView.begin(); sceneGraphIterator != pbdSceneGraphView.end(); sceneGraphIterator++) {
218 
219  // Get interface compliant to AsrObject message on rosbag item currently taken into account.
220  boost::shared_ptr<asr_msgs::AsrObject> currentObject = sceneGraphIterator->instantiate<asr_msgs::AsrObject>();
221 
222 
223  // Update the model, if an object was found.
224  if(currentObject != NULL)
225  {
226  try{
227  // Try to transform evidence into target coordinate system.
228  mObjectTransform.transform(currentObject);
229  }
230  catch(std::exception& exception){
231  // No transformation found, dropping object!
232  ROS_INFO("Unable to resolve transformation in target coordinate frame. Dropping object.");
233  continue;
234  }
235 
236  // Forward the new evidence to the model.
237  mModel.integrateEvidence(AsrObjectToISMObject(currentObject)); //HERE: currentObject
238 
239  // Update the model with the evidence collected until now.
241  }
242  }
243 
244  // Clean up.
245  pbdObjectsBag.close();
246  }
247 
248  void SceneInferenceEngine::loadSceneModel(const std::string pSceneModelFileName, const std::string pInferenceAlgorithm)
249  {
250  // Message for the user.
251  ROS_INFO_STREAM("Initializing inference engine.");
252 
253  // Load the model from file. That's it. Now it's ready for operation!
254  mModel.loadModelFromFile(pSceneModelFileName, pInferenceAlgorithm);
255  }
256 
257  void SceneInferenceEngine::initializeVisualizationChain(const double pScale, const float pSigmaMultiplicator, const std::string pFrameId)
258  {
259  // Status information for the user.
260  ROS_INFO("Initializing visualization mechanism.");
261 
262  // Create a new coordinator for scene visualization.
263  mVisualizer.reset(new Visualization::ProbabilisticSceneModelVisualization());
264 
265  // Order the model to initialize the visualizers.
267 
268  // Set drawing parameters.
269  mVisualizer->setDrawingParameters(pScale, pSigmaMultiplicator, pFrameId);
270 
271  // Get the results and show them.
272  std::vector<SceneIdentifier> pSceneList;
274 
275  // Show plot of scene probabilities? Initialize here!
276  if(showPlot)
277  {
278  // List all scenes to get the labels required for the bar diagram and the associated probabilities.
279  std::vector<std::string> barLabels;
280  std::map<std::string, float> currentData;
281  for(SceneIdentifier i : pSceneList)
282  {
283  barLabels.push_back(i.mDescription);
284  currentData.insert(std::pair<std::string, float>(i.mDescription, i.mLikelihood));
285  }
286 
287  // Initialize the bar diagram, insert the values and visualize!
288  mVisGnuplot.initAnimatedBarChart(barLabels, "Scene Probability", "Probability", std::pair<float, float>(0.0, 1.0));
289  }
290  }
291 
292 
294 
295  {
296  // Buffers the evidence to keep callback time as short as possible.
297  mEvidenceBuffer.push(pObject); // HERE: pObject
298  }
299 
301  //Create Output Object
302  boost::shared_ptr<asr_msgs::AsrObject> outputObject = boost::shared_ptr<asr_msgs::AsrObject>(new asr_msgs::AsrObject());
303  //create and fill asr pose
304  geometry_msgs::Pose *asrPose = new geometry_msgs::Pose();
305  asrPose->position.x = pObject->pose->point->getEigen().x();
306  asrPose->position.y = pObject->pose->point->getEigen().y();
307  asrPose->position.z = pObject->pose->point->getEigen().z();
308  asrPose->orientation.w = pObject->pose->quat->getEigen().w();
309  asrPose->orientation.x = pObject->pose->quat->getEigen().x();
310  asrPose->orientation.y = pObject->pose->quat->getEigen().y();
311  asrPose->orientation.z = pObject->pose->quat->getEigen().z();
312  //create and fill pose with covariance
313  geometry_msgs::PoseWithCovariance *poseWithCovariance = new geometry_msgs::PoseWithCovariance();
314  poseWithCovariance->pose = *asrPose;
315  //add converted pose to output object
316  outputObject->sampledPoses.push_back(*poseWithCovariance);
317  //cpnvert all attributes
318  outputObject->identifier = pObject->observedId;
319  outputObject->type = pObject->type;
320  outputObject->providedBy = pObject->providedBy;
321  outputObject->meshResourcePath = pObject->ressourcePath.string();
322  outputObject->typeConfidence = pObject->weight;
323  outputObject->sizeConfidence = pObject->confidence;
324  //return converted Object
325  return outputObject;
326  }
327 
329  //Create Output Object
330  boost::shared_ptr<ISM::Object> outputObject = boost::shared_ptr<ISM::Object>(new ISM::Object());
331  //Create pose from given Object
332  geometry_msgs::Pose asrPose = pObject->sampledPoses.front().pose;
333  //Create point out of pose
334  ISM::Point *point = new ISM::Point(asrPose.position.x, asrPose.position.y, asrPose.position.z);
335  //create quaternion out of pose
336  ISM::Quaternion *quat = new ISM::Quaternion(asrPose.orientation.w, asrPose.orientation.x, asrPose.orientation.y, asrPose.orientation.z);
337  //add pose to output object
338  outputObject->pose = boost::shared_ptr<ISM::Pose>(new ISM::Pose(point, quat));
339 
340  //convert all attributes
341  outputObject->observedId = pObject->identifier;
342  outputObject->type = pObject->type;
343  outputObject->providedBy = pObject->providedBy;
344  outputObject->ressourcePath = pObject->meshResourcePath;
345  outputObject->weight = pObject->typeConfidence;
346  outputObject->confidence = pObject->sizeConfidence;
347  //return converted object
348  return outputObject;
349  }
350 
351 }
void loadSceneModel(const std::string pSceneModelFileName, const std::string pInferenceAlgorithm)
void open(std::string const &filename, uint32_t mode=bagmode::Read)
void initializeVisualizer(boost::shared_ptr< Visualization::ProbabilisticSceneModelVisualization > mSuperior)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void integrateEvidence(const boost::shared_ptr< const ISM::Object > &pObject)
boost::shared_ptr< Visualization::ProbabilisticSceneModelVisualization > mVisualizer
void initializeVisualizationChain(const double pScale, const float pSigmaMultiplicator, const std::string pFrameId)
uint32_t size()
void newObservationCallback(const boost::shared_ptr< asr_msgs::AsrObject > &pObject)
void getSceneListWithProbabilities(std::vector< SceneIdentifier > &pSceneList)
#define ROS_INFO(...)
std::string getTopic() const
boost::shared_ptr< asr_msgs::AsrObject > ISMObjectToAsrObject(boost::shared_ptr< ISM::Object > pObject)
void transform(const boost::shared_ptr< asr_msgs::AsrObject > &pObject)
std::string inferenceAlgorithm
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
#define ROS_INFO_STREAM(args)
iterator end()
void loadModelFromFile(std::string pPathToFile, std::string pAlgorithm)
bool getParam(const std::string &key, std::string &s) const
iterator begin()
std::queue< boost::shared_ptr< asr_msgs::AsrObject > > mEvidenceBuffer
boost::shared_ptr< ISM::Object > AsrObjectToISMObject(boost::shared_ptr< asr_msgs::AsrObject > pObject)
#define ROS_ERROR(...)


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 03:57:54