psm_node_server.cpp
Go to the documentation of this file.
1 
18 #include <ros/ros.h>
19 #include <sensor_msgs/PointCloud2.h>
20 #include <asr_msgs/AsrAttributedPointCloud.h>
21 #include <asr_msgs/AsrObject.h>
22 #include <inference/SceneInferenceEngine.h>
23 #include <inference/model/SceneIdentifier.h>
24 #include <asr_recognizer_prediction_psm/recognizer_prediction_psm.h>
25 #include <asr_recognizer_prediction_psm/psm_node.h>
26 
27 
28 std::vector<ProbabilisticSceneRecognition::SceneIdentifier> doPSMInference(asr_recognizer_prediction_psm::psm_node::Request &req)
29 {
30  std::string base_frame_id = "/map";
31  ros::NodeHandle nh;
32  nh.getParam("/psm_node/base_frame_id", base_frame_id);
33  nh.setParam("/js_probabilistic_scene_inference_engine/base_frame_id", base_frame_id);
34 
35  std::vector<asr_msgs::AsrObject> objects = req.objects;
36  std::vector<boost::shared_ptr<asr_msgs::AsrObject>> object_pointers;
37 
38  // convert the object msgs
39  for(asr_msgs::AsrObject o : objects)
40  {
42  p->type = o.type;
43  p->identifier = o.identifier;
44  p->header.frame_id = o.header.frame_id;
45  if(!o.sampledPoses.size()){
46  std::cerr << "Got a AsrObject without poses." << std::endl;
47  std::exit(1);
48  }
49  p->sampledPoses.push_back(o.sampledPoses.front());
50  object_pointers.push_back(p);
51 
52  }
53 
54 
55 
56 
57 
58 
59 
63  ProbabilisticSceneRecognition::SceneInferenceEngine ie;
64 
65  // add the objects of the service call to the evidence buffer
66  for(boost::shared_ptr<asr_msgs::AsrObject> o : object_pointers)
67  {
68  ie.newObservationCallback(o);
69  }
70 
71  // update the model and calculate the probabilities
72  ie.update();
73 
77  std::vector<ProbabilisticSceneRecognition::SceneIdentifier> pSceneList;
78  ie.getModel().getSceneListWithProbabilities(pSceneList);
79 
80 
81 
82 
83 
84 
85 
86 
87  printf("\nThis are the scene probabilities:\n");
88  for(ProbabilisticSceneRecognition::SceneIdentifier i : pSceneList)
89  printf(" -> %s (%s): %f (%f)\n", i.mDescription.c_str(), i.mType.c_str(), i.mLikelihood, i.mPriori);
90  printf("\n");
91 
92  return pSceneList;
93 }
94 
95 void doPrediction(std::vector<ProbabilisticSceneRecognition::SceneIdentifier> &pSceneList,
96  asr_recognizer_prediction_psm::psm_node::Request &req,
97  asr_recognizer_prediction_psm::psm_node::Response &res)
98 {
100  bool vis = false;
101 
102  n.getParam("/psm_node/visualize_hypothesis", vis);
103  n.setParam("/recognizer_prediction_psm/visualize_hypothesis", vis);
104 
105  ros::ServiceClient client = n.serviceClient<asr_recognizer_prediction_psm::recognizer_prediction_psm>("recognizer_prediction_psm");
106  asr_recognizer_prediction_psm::recognizer_prediction_psm srv;
107 
108 
109 
113  std::string path;
114  int votes;
115  std::string base_frame_id;
116  n.getParam("/js_probabilistic_scene_inference_engine/scene_model_filename", path);
117  n.getParam("/psm_node/num_hypothesis", votes);
118  n.getParam("/psm_node/base_frame_id", base_frame_id);
119  srv.request.num_votes = votes;
120  srv.request.objects = req.objects;
121  srv.request.base_frame_id = base_frame_id;
122  srv.request.pathToXML = path;
123 
124  std::vector<std::string> scenes;
125  std::vector<float> scene_probabilities;
126 
127  for(ProbabilisticSceneRecognition::SceneIdentifier i : pSceneList)
128  {
129  scenes.push_back(i.mDescription.c_str());
130  scene_probabilities.push_back(i.mLikelihood);
131  }
132 
133  srv.request.scenes = scenes;
134  srv.request.scene_probabilities = scene_probabilities;
135 
136 
140  if (client.call(srv))
141  {
142  /*
143  for(auto e : srv.response.pose_hypothesis.elements)
144  {
145  ROS_INFO("Hypothesis type \"%s\" at position (%.2f, %.2f, %.2f), orientation (%.2f, %.2f, %.2f, %.2f), base_frame \"%s\"",
146  e.object_type.c_str(),
147  e.pose.position.x,
148  e.pose.position.y,
149  e.pose.position.z,
150  e.pose.orientation.w,
151  e.pose.orientation.x,
152  e.pose.orientation.y,
153  e.pose.orientation.z,
154  srv.request.base_frame_id.c_str());
155  }*/
156 
160  res.pose_hypothesis = srv.response.pose_hypothesis;
161  res.all_scene_objects_found = srv.response.all_scene_objects_found;
162  }
163  else
164  {
165  ROS_ERROR("Failed to call service recognizer_prediction_psm");
166  return;
167  }
168 
169  return;
170 
171 
172 }
173 
174 
175 bool run(asr_recognizer_prediction_psm::psm_node::Request &req,
176  asr_recognizer_prediction_psm::psm_node::Response &res)
177 {
178  // The recognition result
179  std::vector<ProbabilisticSceneRecognition::SceneIdentifier> pSceneList;
180 
181  // do the scene recognition
182  pSceneList = doPSMInference(req);
183 
184  // do the prediction
185  doPrediction(pSceneList, req, res);
186 
187 
188  return true;
189 }
190 
192 {
193 
194  return true;
195 }
196 
197 
198 
199 int main(int argc, char **argv)
200 {
201  ros::init(argc, argv, "psm_node_server");
202  ros::NodeHandle n;
203 
204  ros::ServiceServer service = n.advertiseService("psm_node", run);
205  ROS_INFO("Ready to do the inference\n");
206 
207 
208  ros::spin();
209 
210  return 0;
211 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool getNumberOfUnobservedObjects()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
bool run(asr_recognizer_prediction_psm::psm_node::Request &req, asr_recognizer_prediction_psm::psm_node::Response &res)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ROSCPP_DECL void spin(Spinner &spinner)
void doPrediction(std::vector< ProbabilisticSceneRecognition::SceneIdentifier > &pSceneList, asr_recognizer_prediction_psm::psm_node::Request &req, asr_recognizer_prediction_psm::psm_node::Response &res)
#define ROS_INFO(...)
std::vector< ProbabilisticSceneRecognition::SceneIdentifier > doPSMInference(asr_recognizer_prediction_psm::psm_node::Request &req)
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
int main(int argc, char **argv)


asr_recognizer_prediction_psm
Author(s): Braun Kai, Meißner Pascal
autogenerated on Wed Feb 19 2020 03:31:30