get_world_state.cpp
Go to the documentation of this file.
1 /*
2  * get_world_state.cpp
3  *
4  * Created on: Nov 13, 2012
5  * Author: jelfring
6  */
7 
8 #include "ros/ros.h"
9 #include "wire_msgs/ObjectState.h"
10 #include "wire_msgs/WorldState.h"
11 
12 #include "problib/conversions.h"
13 
14 #include <vector>
15 
16 using namespace std;
17 
18 
19 // Function that extract the Gaussian with the highest weight from a mixture of Gaussians and/or a uniform distribution
20 const pbl::Gaussian* getBestGaussianFromMixture(const pbl::Mixture& mix, double min_weight = 0) {
21  const pbl::Gaussian* G_best = 0;
22  double w_best = min_weight;
23 
24  // Iterate over all components
25  for(int i = 0; i < mix.components(); ++i) {
26  const pbl::PDF& pdf = mix.getComponent(i);
27  const pbl::Gaussian* G = pbl::PDFtoGaussian(pdf);
28  double w = mix.getWeight(i);
29 
30  // Get Gaussian with the highest associated weight
31  if (G && w > w_best) {
32  G_best = G;
33  w_best = w;
34  }
35  }
36  return G_best;
37 }
38 
39 
40 
41 void worldStateCallback(const wire_msgs::WorldState::ConstPtr& msg) {
42 
43  ROS_INFO("Received world state with %d objects", msg->objects.size());
44 
45 
46  // Iterate over world model objects
47  for (vector<wire_msgs::ObjectState>::const_iterator it_obj = msg->objects.begin(); it_obj != msg->objects.end(); ++it_obj) {
48 
49  // Get object from the object array
50  wire_msgs::ObjectState obj = *it_obj;
51  ROS_INFO(" Object:");
52 
53  // Iterate over all properties
54  for (vector<wire_msgs::Property>::const_iterator it_prop = obj.properties.begin(); it_prop != obj.properties.end(); ++it_prop) {
55 
56  // Get the pdf of the current property
57  pbl::PDF* pdf = pbl::msgToPDF(it_prop->pdf);
58 
59  if (pdf) {
60 
62 
63  // Object class
64  if (it_prop->attribute == "class_label") {
65  string label = "";
66  pdf->getExpectedValue(label);
67 
68  // Get probability
69  const pbl::PMF* pmf = pbl::PDFtoPMF(*pdf);
70  double p = pmf->getProbability(label);
71  ROS_INFO(" - class %s with probability %f", label.c_str(), p);
72  }
73  // Position
74  else if (it_prop->attribute == "position") {
75 
76  // Get (Gaussian) position
77  const pbl::Gaussian* pos_gauss;
78  if (pdf->type() == pbl::PDF::MIXTURE) {
79  const pbl::Mixture* pos_pdf = pbl::PDFtoMixture(*pdf);
80  if (pos_pdf)
81  pos_gauss = getBestGaussianFromMixture(*pos_pdf);
82  else
83  ROS_INFO(" - position: object position unknown (uniform distribution)");
84  }
85  else if (pdf->type() == pbl::PDF::GAUSSIAN) {
86  pos_gauss = pbl::PDFtoGaussian(*pdf);
87  }
88 
89  // Print Gaussian position
90  if (pos_gauss) {
91  const pbl::Vector& pos = pos_gauss->getMean();
92  ROS_INFO(" - position: (%f,%f,%f)", pos(0), pos(1), pos(2));
93  ROS_INFO(" - diagonal position cov: (%f,%f,%f)",
94  pos_gauss->getCovariance()(0, 0), pos_gauss->getCovariance()(1, 1), pos_gauss->getCovariance()(2, 2));
95 
96  }
97  }
98  // Orientation
99  else if (it_prop->attribute == "orientation") {
100  const pbl::Mixture* orientation_pdf = pbl::PDFtoMixture(*pdf);
101  if (orientation_pdf) {
102  const pbl::Gaussian* orientation_gauss = getBestGaussianFromMixture(*orientation_pdf);
103  if (orientation_gauss) {
104  const pbl::Vector& ori = orientation_gauss->getMean();
105  ROS_INFO(" - orientation: (%f,%f,%f,%f)", ori(0), ori(1), ori(2), ori(3));
106  }
107  } else {
108  ROS_INFO(" - orientation: object orientation unknown (uniform distribution)");
109  }
110  }
111  // Color
112  else if (it_prop->attribute == "color") {
113  string color = "";
114  pdf->getExpectedValue(color);
115  ROS_INFO(" - color: %s", color.c_str());
116  }
117 
118  delete pdf;
119  }
120 
121  }
122 
123  }
124 
125 }
126 
127 int main(int argc, char **argv) {
128  ros::init(argc, argv, "get_world_state");
129  ros::NodeHandle nh;
130 
131  // Subscribe to the world model
132  ros::Subscriber sub_obj = nh.subscribe("/world_state", 1000, &worldStateCallback);
133 
134  ros::spin();
135  return 0;
136 }
double getProbability(const std::string &value) const
PDF * msgToPDF(const problib::PDF &msg)
PDFType type() const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
virtual bool getExpectedValue(std::string &v) const
arma::vec Vector
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int components() const
void worldStateCallback(const wire_msgs::WorldState::ConstPtr &msg)
const PMF * PDFtoPMF(const PDF &pdf)
const arma::mat & getCovariance() const
const PDF & getComponent(int i) const
const_iterator(const field< oT > &in_M, const bool at_end=false)
#define ROS_INFO(...)
const arma::vec & getMean() const
ROSCPP_DECL void spin()
const pbl::Gaussian * getBestGaussianFromMixture(const pbl::Mixture &mix, double min_weight=0)
double getWeight(int i) const
const Mixture * PDFtoMixture(const PDF &pdf)
const Gaussian * PDFtoGaussian(const PDF &pdf)
int main(int argc, char **argv)


wire_tutorials
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:37