fake_data_publisher.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <iostream>
20 #include <string>
21 #include <set>
22 #include <vector>
23 #include <cmath>
24 
25 //Pkg includes
26 #include <ros/ros.h>
27 #include <ros/package.h>
28 
29 #include <visualization_msgs/Marker.h>
30 #include <tf/transform_datatypes.h>
31 #include <tf/transform_listener.h>
32 
33 #include <asr_msgs/AsrObject.h>
34 
35 #include <asr_ism_visualizations/ObjectModelVisualizerRVIZ.hpp>
36 
37 //ISM includes
38 #include <ISM/utility/TableHelper.hpp>
39 #include <ISM/common_type/ObjectSet.hpp>
40 #include <ISM/common_type/RecordedPattern.hpp>
41 #include <ISM/utility/Util.hpp>
42 
43 //Local includes
44 #include "ism_helper.hpp"
45 
53 public:
54 
59  {
60 
61  std::string objectTopic;
62  std::string dbfilename;
63  std::string visualizationTopic;
64  patternIter = 0;
65  setIter = 0;
66  usedPattern = -1;
67 
68  //Extract cli parameters of ros node for being used in this program.
69  getNodeParameters(objectTopic, dbfilename, visualizationTopic);
70  mTableHelper = ISM::TableHelperPtr (new ISM::TableHelper (dbfilename));
71  if(!mTableHelper->recordDataExists())
72  {
73  ISM::printRed("The database \"" + dbfilename + "\" doesn't contain any recordings!\n");
74  exit(0);
75  }
76 
77  sleep(1);
79 
80  int numberOfPatterns = mObjectSets.size();
81 
82  if (mNh.hasParam("usedPattern")) {
83  mNh.param<int>("usedPattern", usedPattern, 1);
84  if (usedPattern < 1)
85  {
86  usedPattern = 1;
87  ROS_INFO_STREAM("usedPattern is smaller than 1, setting it to 1 now.");
88  }
89  else if (usedPattern > numberOfPatterns)
90  {
91  usedPattern = numberOfPatterns;
92  ROS_INFO_STREAM("usedPattern is greater than " << (numberOfPatterns) << ", setting it to " << numberOfPatterns << " now.");
93  }
94  int numberOfSets = mObjectSets[usedPattern-1].size();
95  if (numberOfSets <= 0)
96  {
97  ROS_INFO_STREAM("Pattern doesn't contain any sets!");
98  exit(0);
99  }
100 
101  mNh.param<int>("firstUsedSet", firstUsedSet, 1);
102  if (firstUsedSet < 1)
103  {
104  firstUsedSet = 1;
105  ROS_INFO_STREAM("firstUsedSet is smaller than 1, setting it to 1 now.");
106  }
107  else if (firstUsedSet > numberOfSets)
108  {
109  firstUsedSet = numberOfSets ;
110  ROS_INFO_STREAM("firstUsedSet is greater than numberOfSets(" << (numberOfSets) << "), setting it to " << firstUsedSet << " now.");
111  }
112 
113  mNh.param<int>("lastUsedSet", lastUsedSet, numberOfSets);
114  if (lastUsedSet < 1)
115  {
116  lastUsedSet = 1;
117  ROS_INFO_STREAM("lastUsedSet is smaller than 1, setting it to 1 now.");
118  }
119  else if (lastUsedSet > numberOfSets)
120  {
121  lastUsedSet = numberOfSets;
122  ROS_INFO_STREAM("lastUsedSet is greater than numberOfSets(" << numberOfSets << "), setting it to " << lastUsedSet << " now.");
123  }
124 
125  if (firstUsedSet > lastUsedSet)
126  {
127  ROS_INFO_STREAM("firstUsedSet is greater than lastUsedSet!");
128  exit(0);
129  }
130 
131  std::cout << std::endl;
132  ROS_INFO_STREAM("using pattern " << (usedPattern));
133  ROS_INFO_STREAM("and sets from " << (firstUsedSet) << " to " << lastUsedSet << " (including last)");
134 
135  // sql tables start with 1 while std::vector starts with 0, so we reduce it by 1 and say it is a std::vector index/pos
137  setIter = --firstUsedSet;
138  lastUsedSet--;
139  }
140 
141  mDataPub = mNh.advertise<asr_msgs::AsrObject> (objectTopic, 5, this);
142  mVisualizationPub = mNh.advertise<visualization_msgs::MarkerArray>("fake_data_publisher_visualization", 100);
144  mObjectModelVisualizer = new VIZ::ObjectModelVisualizerRVIZ(mVisualizationPub, mBaseFrame, "", 0.0);
145 
146  std::cout << std::endl;
147  }
148  virtual ~Fake_data_publisher(){};
149 
150  void getNodeParameters(std::string& pObjectTopic, std::string& pDbfilename,
151  std::string& pVisualizationTopic)
152  {
153  if (!mNh.getParam("objectTopic", pObjectTopic))
154  {
155  pObjectTopic = "/objects";
156  }
157  ROS_INFO_STREAM("objectTopic: " << pObjectTopic);
158 
159  if (!mNh.getParam("capture_interval", mPublishInterval))
160  {
161  mPublishInterval = 1;
162  }
163  ROS_INFO_STREAM("PublishInterval (capture_interval): " << mPublishInterval);
164 
165  if (!mNh.getParam("dbfilename", pDbfilename))
166  {
167  throw std::runtime_error("dbfilename not set");
168  }
169  ROS_INFO_STREAM("dbfilename: " << pDbfilename);
170 
171  if (!mNh.getParam("baseFrame", mBaseFrame))
172  {
173  mBaseFrame = "/map";
174  }
175  ROS_INFO_STREAM("baseFrame: " << mBaseFrame);
176 
177  if (!mNh.getParam("step", mStep))
178  {
179  mStep = 0.1;
180  }
181  ROS_INFO_STREAM("step: " << mStep);
182 
183  if (!mNh.getParam("visualization_topic", pVisualizationTopic))
184  {
185  pVisualizationTopic = "visualization_marker";
186  }
187  ROS_INFO_STREAM("visualization_topic: " << pVisualizationTopic);
188  }
189 
190 private:
191 
192  // type and id in an incoming object estimation.
193  typedef std::pair<std::string, std::string> RecordKey;
194  typedef std::map<RecordKey, const asr_msgs::AsrObject> RecordMapType;
195  //Object estimations received within current time interval.
196  RecordMapType mObjectRecord;
197 
199 
200  //See launch file documentation
202  double mStep;
203  std::string mBaseFrame;
204 
205  //Core functionality
206  std::vector<std::vector<ISM::ObjectSetPtr>> mObjectSets;
207  ISM::TableHelperPtr mTableHelper;
208  ISM::Recognizer* mRecognizer;
209  unsigned int patternIter;
210  unsigned int setIter;
211  // first and last set used
215 
216  //Ros Node stuff
222 
223  //Visualization stuff
224  VIZ::ObjectModelVisualizerRVIZ* mObjectModelVisualizer;
225 
226 
227  /*A method to get all "dbfilename" recorded_sets
228  *
229  *return value is a 3D-Vector with entries database[pattern][set]->objects[object]
230  * (database [pattern][set] returns an object set, which can be accessed like a vector)
231  */
232  std::vector<std::vector<ISM::ObjectSetPtr>> getDbEntries ()
233  {
234  std::cout << std::endl;
235  ROS_INFO_STREAM("Extracting Patterns");
236  std::vector<std::string> patternNames = mTableHelper->getRecordedPatternNames();
237  ROS_INFO_STREAM("There are/is " << patternNames.size() << " pattern(s)");
238  std::vector<ISM::RecordedPatternPtr> recordedPatterns (patternNames.size());
239  std::vector<std::vector<ISM::ObjectSetPtr>> recordedSets (recordedPatterns.size());
240  for (unsigned int i = 0; i < patternNames.size(); ++i)
241  {
242  ROS_INFO_STREAM("Extracting pattern " << i + 1);
243  recordedPatterns[i] = mTableHelper->getRecordedPattern(patternNames[i]);
244  }
245  for (unsigned int pattern = 0; pattern < recordedPatterns.size(); ++pattern)
246  {
247  recordedSets[pattern] = std::vector<ISM::ObjectSetPtr> (recordedPatterns[pattern]->objectSets.size());
248  ROS_INFO_STREAM("Pattern " << pattern + 1 << " consists of " << recordedPatterns[pattern]->objectSets.size()
249  << " sets ");
250  for (unsigned int set = 0; set < recordedPatterns[pattern]->objectSets.size(); ++set)
251  {
252  recordedSets[pattern][set] = recordedPatterns[pattern]->objectSets[set];
253  ROS_INFO_STREAM("Set " << set + 1 << " of pattern " << pattern + 1 << " extracted");
254  }
255  }
256  return recordedSets;
257  }
258 
259  asr_msgs::AsrObjectPtr objectToMessage(ISM::ObjectPtr object)
260  {
261  asr_msgs::AsrObjectPtr msg;
262  msg.reset(new asr_msgs::AsrObject());
263 
264  //msg->header.stamp.secs = ros::Time::now().toSec();
265  //msg->header.stamp.nsecs = ros::Time::now().toNSec();
266  msg->header.frame_id = mBaseFrame;
267 
268  msg->providedBy = "fake_data_publisher";
269  geometry_msgs::Pose fake_pose;
270  fake_pose.position.x = object->pose->point->eigen.x();
271  fake_pose.position.y = object->pose->point->eigen.y();
272  fake_pose.position.z = object->pose->point->eigen.z();
273 
274  fake_pose.orientation.w = object->pose->quat->eigen.w();
275  fake_pose.orientation.x = object->pose->quat->eigen.x();
276  fake_pose.orientation.y = object->pose->quat->eigen.y();
277  fake_pose.orientation.z = object->pose->quat->eigen.z();
278 
279  geometry_msgs::PoseWithCovariance fake_pose_with_c;
280  fake_pose_with_c.pose = fake_pose;
281  msg->sampledPoses.push_back(fake_pose_with_c);
282  msg->type = object->type;
283  msg->typeConfidence = object->confidence;
284  msg->sizeConfidence = object->confidence;
285  msg->identifier = object->observedId;
286  msg->meshResourcePath = object->ressourcePath.string();
287 
288  /*for(unsigned int i = 0; i < msg->poseEstimation.covariance.size(); i++)
289  {
290  msg->poseEstimation.covariance.at(i) = 0.0f;
291  }
292  */
293 
294  //object->boundingBox = get_bounding_box(entry.sOivFilePath);
295 
296  //object->type = entry.sName;
297  //object->meshResourcePath = entry.sOivFilePath;
298  /*
299  // Segmentable Recognition:
300  // quality: Verhältnis zwischen gemessener und simulierter Größe (unter Verwendung der berechneten Objektlage)
301  // quality2: Korrelation der binären Objektsilhouetten (real, simuliert)
302  // Beides sind Werte zwischen 0 und 1, wobei 1 maximale Übereinstimmung bedeutet.
303  // Textured Recognition:
304  // entry.quality: Mean Error after Homography estimation
305  // entry.quality2: Number of Features after Homography estimation
306  object->typeConfidence = entry.quality2;
307  object->sizeConfidence = entry.quality;*/
308  return msg;
309  }
310 
311 void timerCallback(const ros::TimerEvent& timerEvent)
312 {
313  asr_msgs::AsrObjectPtr object;
314  unsigned int setSize = mObjectSets[patternIter][setIter]->objects.size();
315  unsigned int patterns = mObjectSets.size();
316  unsigned int patternSize = mObjectSets[patternIter].size();
317  ROS_INFO_STREAM("Publishing set " << setIter + 1 << " / " << patternSize << " of pattern "
318  << patternIter + 1 << " / " << patterns);
319  ROS_INFO_STREAM("There are " << setSize << " objects in this set");
320  for (unsigned int objectIter = 0; objectIter < setSize; ++objectIter)
321  {
322  ROS_INFO_STREAM("Publishing object " << objectIter + 1 << " / " << setSize << " of set "
323  << setIter + 1);
324  std::cout<<"info\n"<<std::endl;
325  object = objectToMessage(mObjectSets[patternIter][setIter]->objects[objectIter]);
326  mDataPub.publish(object);
327  }
328 
329  mObjectModelVisualizer->drawObjectModels(mObjectSets[patternIter][setIter]->objects);
330 
331  setIter++;
332  if (usedPattern < 0)
333  {
334  if (setIter >= patternSize)
335  {
336  patternIter++;
337  setIter = 0;
338  ROS_INFO_STREAM("Pattern " << patternIter << " completed\n");
339  if (patternIter >= patterns)
340  {
341  patternIter = 0;
342  setIter = 0;
343  ROS_INFO_STREAM("Done. Restarting.\n\n");
344  }
345  }
346  }
347  else
348  {
349  // we only use a part of all set
350  if (setIter > (unsigned int) lastUsedSet)
351  {
352  setIter = firstUsedSet;
353  ROS_INFO_STREAM("Pattern " << patternIter + 1 << " completed\n");
354  ROS_INFO_STREAM("Done. Restarting.\n\n");
355  }
356  }
357 }
358 
359 };
360 
361 
362 int main(int argc, char **argv)
363 {
364 
365  //Usual ros node stuff
366  ros::init(argc, argv, "fake_data_publisher");
367 
368  Fake_data_publisher* publisher = new Fake_data_publisher();
369 
370  //Let this node run as long as we want objects to be published.
371  ros::spin();
372 
373  delete publisher;
374 };
375 
376 
377 
378 
379 
std::vector< std::vector< ISM::ObjectSetPtr > > mObjectSets
VIZ::ObjectModelVisualizerRVIZ * mObjectModelVisualizer
void timerCallback(const ros::TimerEvent &timerEvent)
void publish(const boost::shared_ptr< M > &message) const
std::map< RecordKey, const asr_msgs::AsrObject > RecordMapType
std::vector< std::vector< ISM::ObjectSetPtr > > getDbEntries()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Publisher mVisualizationPub
ROSCPP_DECL void spin(Spinner &spinner)
void getNodeParameters(std::string &pObjectTopic, std::string &pDbfilename, std::string &pVisualizationTopic)
asr_msgs::AsrObjectPtr objectToMessage(ISM::ObjectPtr object)
IH::ObjectConverter * mConverter
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ISM::TableHelperPtr mTableHelper
std::pair< std::string, std::string > RecordKey
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
bool hasParam(const std::string &key) const
ISM::Recognizer * mRecognizer
int main(int argc, char **argv)


asr_ism
Author(s): Borella Jocelyn, Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Thu Jan 9 2020 07:20:58