tracker-client.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <fstream>
3 #include <sstream>
4 #include <stdexcept>
5 
6 #include <boost/filesystem.hpp>
7 #include <boost/filesystem/fstream.hpp>
8 #include <boost/format.hpp>
9 #include <boost/optional.hpp>
10 #include <boost/version.hpp>
11 
12 #include <ros/ros.h>
13 #include <ros/param.h>
14 #include <ros/package.h>
15 #include <dynamic_reconfigure/server.h>
16 #include <geometry_msgs/PoseWithCovarianceStamped.h>
19 #include <visp_tracker/Init.h>
20 #include <visp_tracker/ModelBasedSettingsConfig.h>
21 
22 #include <visp/vpMe.h>
23 #include <visp/vpPixelMeterConversion.h>
24 #include <visp/vpPose.h>
25 
26 #define protected public
27 #include <visp/vpMbEdgeTracker.h>
28 #include <visp/vpMbKltTracker.h>
29 #include <visp/vpMbEdgeKltTracker.h>
30 #undef protected
31 
32 #include <visp/vpDisplayX.h>
33 #include <visp/vpImageIo.h>
34 #include <visp/vpIoTools.h>
35 
36 #include "conversion.hh"
37 #include "callbacks.hh"
38 #include "file.hh"
39 #include "names.hh"
40 
41 #include "tracker-client.hh"
42 
43 
44 namespace visp_tracker
45 {
47  ros::NodeHandle& privateNh,
48  volatile bool& exiting,
49  unsigned queueSize)
50  : exiting_ (exiting),
51  queueSize_(queueSize),
52  nodeHandle_(nh),
53  nodeHandlePrivate_(privateNh),
54  imageTransport_(nodeHandle_),
55  image_(),
56  modelPath_(),
57  modelPathAndExt_(),
58  modelName_(),
59  cameraPrefix_(),
60  rectifiedImageTopic_(),
61  cameraInfoTopic_(),
62  trackerType_("mbt"),
63  frameSize_(0.1),
64  bModelPath_(),
65  bInitPath_(),
66  cameraSubscriber_(),
67  mutex_(),
68  reconfigureSrv_(NULL),
69  reconfigureKltSrv_(NULL),
70  reconfigureEdgeSrv_(NULL),
71  movingEdge_(),
72  kltTracker_(),
73  cameraParameters_(),
74  tracker_(),
75  startFromSavedPose_(),
76  checkInputs_(),
77  resourceRetriever_()
78  {
79  // Parameters.
80  nodeHandlePrivate_.param<std::string>("model_path", modelPath_,
82  nodeHandlePrivate_.param<std::string>("model_name", modelName_, "");
83 
85  ("start_from_saved_pose", startFromSavedPose_, false);
86 
88  ("confirm_init", confirmInit_, true);
89 
90  nodeHandlePrivate_.param<std::string>("tracker_type", trackerType_, "mbt");
91  if(trackerType_=="mbt")
92  tracker_ = new vpMbEdgeTracker();
93  else if(trackerType_=="klt")
94  tracker_ = new vpMbKltTracker();
95  else
96  tracker_ = new vpMbEdgeKltTracker();
97 
98  nodeHandlePrivate_.param<double>("frame_size", frameSize_, 0.1);
99 
100  //tracker_->resetTracker(); // TO CHECK
101 
102  if (modelName_.empty ())
103  throw std::runtime_error
104  ("empty model\n"
105  "Relaunch the client while setting the model_name parameter, i.e.\n"
106  "$ rosrun visp_tracker client _model_name:=my-model"
107  );
108 
109  // Compute topic and services names.
110 
111  ros::Rate rate (1);
112  while (cameraPrefix_.empty ())
113  {
114  if (!nodeHandle_.getParam ("camera_prefix", cameraPrefix_) && !ros::param::get ("~camera_prefix", cameraPrefix_))
115  {
116  ROS_WARN
117  ("the camera_prefix parameter does not exist.\n"
118  "This may mean that:\n"
119  "- the tracker is not launched,\n"
120  "- the tracker and viewer are not running in the same namespace."
121  );
122  }
123  else if (cameraPrefix_.empty ())
124  {
125  ROS_INFO
126  ("tracker is not yet initialized, waiting...\n"
127  "You may want to launch the client to initialize the tracker.");
128  }
129  if (this->exiting())
130  return;
131  rate.sleep ();
132  }
133 
135  ros::names::resolve(cameraPrefix_ + "/image_rect");
137  ros::names::resolve(cameraPrefix_ + "/camera_info");
138 
139  // Check for subscribed topics.
140  checkInputs();
141 
142  // Camera subscriber.
146 
147  // Model loading.
148  bModelPath_ = getModelFileFromModelName(modelName_, modelPath_);
149  bInitPath_ = getInitFileFromModelName(modelName_, modelPath_);
150 
151  ROS_INFO_STREAM("Model file: " << bModelPath_);
152  ROS_INFO_STREAM("Init file: " << bInitPath_);
153 
154  // Load the 3d model.
155  loadModel();
156 
157  // Set callback for dynamic reconfigure.
158  // No more necessary as it is done via the reconfigure server
159 // if(trackerType_!="klt"){
160 // vpMbEdgeTracker* t = dynamic_cast<vpMbEdgeTracker*>(tracker_);
161 // t->setMovingEdge(movingEdge_);
162 // }
163 
164 // if(trackerType_!="mbt"){
165 // vpMbKltTracker* t = dynamic_cast<vpMbKltTracker*>(tracker_);
166 // t->setKltOpencv(kltTracker_);
167 // }
168 
169  // Dynamic reconfigure.
170  if(trackerType_=="mbt+klt"){ // Hybrid Tracker reconfigure
173  boost::bind(&reconfigureCallback, boost::ref(tracker_),
174  boost::ref(image_), boost::ref(movingEdge_), boost::ref(kltTracker_),
175  boost::ref(mutex_), _1, _2);
176  reconfigureSrv_->setCallback(f);
177  }
178  else if(trackerType_=="mbt"){ // Edge Tracker reconfigure
181  boost::bind(&reconfigureEdgeCallback, boost::ref(tracker_),
182  boost::ref(image_), boost::ref(movingEdge_),
183  boost::ref(mutex_), _1, _2);
184  reconfigureEdgeSrv_->setCallback(f);
185  }
186  else{ // KLT Tracker reconfigure
189  boost::bind(&reconfigureKltCallback, boost::ref(tracker_),
190  boost::ref(image_), boost::ref(kltTracker_),
191  boost::ref(mutex_), _1, _2);
192  reconfigureKltSrv_->setCallback(f);
193  }
194 
195  // Wait for the image to be initialized.
196  waitForImage();
197  if (this->exiting())
198  return;
199  if (!image_.getWidth() || !image_.getHeight())
200  throw std::runtime_error("failed to retrieve image");
201 
202  // Tracker initialization.
203  // - Camera
205  tracker_->setCameraParameters(cameraParameters_);
206  tracker_->setDisplayFeatures(true);
207 
209  // - Moving edges.
210  if(trackerType_!="klt"){
211  // No more necessary as it has been done via the reconfigure server
212 // movingEdge_.initMask();
213 // vpMbEdgeTracker* t = dynamic_cast<vpMbEdgeTracker*>(tracker_);
214 // t->setMovingEdge(movingEdge_);
216  //movingEdge_.print();
217  }
218 
219  if(trackerType_!="mbt"){
220  // No more necessary as it has been done via the reconfigure server
221 // vpMbKltTracker* t = dynamic_cast<vpMbKltTracker*>(tracker_);
222 // t->setKltOpencv(kltTracker_);
224  }
225 
226  // Display camera parameters and moving edges settings.
228  }
229 
230  void
232  {
233  ros::V_string topics;
234  topics.push_back(rectifiedImageTopic_);
235  topics.push_back(cameraInfoTopic_);
236  checkInputs_.start(topics, 60.0);
237  }
238 
239  void
241  {
242  boost::format fmtWindowTitle ("ViSP MBT tracker initialization - [ns: %s]");
243  fmtWindowTitle % ros::this_node::getNamespace ();
244 
245  vpDisplayX d(image_, image_.getWidth(), image_.getHeight(),
246  fmtWindowTitle.str().c_str());
247 
248  ros::Rate loop_rate_tracking(200);
249  bool ok = false;
250  vpHomogeneousMatrix cMo;
251  vpImagePoint point (10, 10);
252  while (!ok && !exiting())
253  {
254  try
255  {
256  // Initialize.
257  vpDisplay::display(image_);
258  vpDisplay::flush(image_);
259  if (!startFromSavedPose_)
260  init();
261  else
262  {
263  cMo = loadInitialPose();
264  startFromSavedPose_ = false;
265  tracker_->initFromPose(image_, cMo);
266  }
267  tracker_->getPose(cMo);
268 
269  ROS_INFO_STREAM("initial pose [tx,ty,tz,tux,tuy,tuz]:\n" << vpPoseVector(cMo).t());
270 
271  // Track once to make sure initialization is correct.
272  if (confirmInit_)
273  {
274  vpImagePoint ip;
275  vpMouseButton::vpMouseButtonType button =
276  vpMouseButton::button1;
277  do
278  {
279  vpDisplay::display(image_);
280  mutex_.lock ();
281  tracker_->track(image_);
282  tracker_->getPose(cMo);
283  tracker_->display(image_, cMo, cameraParameters_,
284  vpColor::red, 2);
285  vpDisplay::displayFrame(image_, cMo, cameraParameters_,frameSize_,vpColor::none,2);
286  mutex_.unlock();
287  vpDisplay::displayCharString
288  (image_, point, "tracking, click to initialize tracker",
289  vpColor::red);
290  vpDisplay::flush(image_);
291 
292  ros::spinOnce();
293  loop_rate_tracking.sleep();
294  if (exiting())
295  return;
296  }
297  while(!vpDisplay::getClick(image_, ip, button, false));
298  ok = true;
299  }
300  else
301  ok = true;
302  }
303  catch(const std::runtime_error& e)
304  {
305  mutex_.unlock();
306  ROS_ERROR_STREAM("failed to initialize: "
307  << e.what() << ", retrying...");
308  }
309  catch(const std::string& str)
310  {
311  mutex_.unlock();
312  ROS_ERROR_STREAM("failed to initialize: "
313  << str << ", retrying...");
314  }
315  catch(...)
316  {
317  mutex_.unlock();
318  ROS_ERROR("failed to initialize, retrying...");
319  }
320  }
321 
322  ROS_INFO_STREAM("Initialization done, sending initial cMo:\n" << cMo);
323  try
324  {
325  sendcMo(cMo);
326  }
327  catch(std::exception& e)
328  {
329  ROS_ERROR_STREAM("failed to send cMo: " << e.what ());
330  }
331  catch(...)
332  {
333  ROS_ERROR("unknown error happened while sending the cMo, retrying...");
334  }
335  }
336 
338  {
339  delete tracker_;
340 
341  if(reconfigureSrv_ != NULL)
342  delete reconfigureSrv_;
343 
344  if(reconfigureKltSrv_ != NULL)
345  delete reconfigureKltSrv_;
346 
347  if(reconfigureEdgeSrv_ != NULL)
348  delete reconfigureEdgeSrv_;
349  }
350 
351  void
352  TrackerClient::sendcMo(const vpHomogeneousMatrix& cMo)
353  {
354  ros::ServiceClient client =
356 
357 
358  ros::ServiceClient clientViewer =
360  visp_tracker::Init srv;
361 
362  // Load the model and send it to the parameter server.
363  std::string modelDescription = fetchResource(modelPathAndExt_);
364  nodeHandle_.setParam (model_description_param, modelDescription);
365 
366  vpHomogeneousMatrixToTransform(srv.request.initial_cMo, cMo);
367 
369 
370  if(trackerType_!="klt"){
372  }
373 
374  if(trackerType_!="mbt"){
376  }
377 
378  ros::Rate rate (1);
379  while (!client.waitForExistence ())
380  {
381  ROS_INFO
382  ("Waiting for the initialization service to become available.");
383  rate.sleep ();
384  }
385 
386  if (client.call(srv))
387  {
388  if (srv.response.initialization_succeed)
389  ROS_INFO("Tracker initialized with success.");
390  else
391  throw std::runtime_error("failed to initialize tracker.");
392  }
393  else
394  throw std::runtime_error("failed to call service init");
395 
396  if (clientViewer.call(srv))
397  {
398  if (srv.response.initialization_succeed)
399  ROS_INFO("Tracker Viewer initialized with success.");
400  else
401  throw std::runtime_error("failed to initialize tracker viewer.");
402  }
403  else
404  ROS_INFO("No Tracker Viewer node to initialize from service");
405  }
406 
407  void
409  {
410  try
411  {
412  ROS_DEBUG_STREAM("Trying to load the model "
413  << bModelPath_.native());
414 
415  std::string modelPath;
416  boost::filesystem::ofstream modelStream;
417  if (!makeModelFile(modelStream,
418  bModelPath_.native(),
419  modelPath))
420  throw std::runtime_error ("failed to retrieve model");
421 
422  tracker_->loadModel(modelPath);
423  ROS_INFO("Model has been successfully loaded.");
424 
425  if(trackerType_=="mbt"){
426  vpMbEdgeTracker* t = dynamic_cast<vpMbEdgeTracker*>(tracker_);
427  ROS_DEBUG_STREAM("Nb faces: "
428  << t->getFaces().getPolygon().size());
429  ROS_DEBUG_STREAM("Nb visible faces: " << t->getFaces().getNbVisiblePolygon());
430 
431  std::list<vpMbtDistanceLine *> linesList;
432  t->getLline(linesList);
433  ROS_DEBUG_STREAM("Nb line: " << linesList.size());
434  ROS_DEBUG_STREAM("nline: " << t->nline);
435  }
436  else if(trackerType_=="klt"){
437  vpMbKltTracker* t = dynamic_cast<vpMbKltTracker*>(tracker_);
438  ROS_DEBUG_STREAM("Nb faces: "
439  << t->getFaces().getPolygon().size());
440  ROS_DEBUG_STREAM("Nb visible faces: " << t->getFaces().getNbVisiblePolygon());
441  ROS_DEBUG_STREAM("Nb KLT points: " << t->getNbKltPoints());
442  }
443  else {
444  vpMbEdgeKltTracker* t = dynamic_cast<vpMbEdgeKltTracker*>(tracker_);
445  ROS_DEBUG_STREAM("Nb hidden faces: "
446  << t->getFaces().getPolygon().size());
447  ROS_DEBUG_STREAM("Nb visible faces: " << t->getFaces().getNbVisiblePolygon());
448  ROS_DEBUG_STREAM("Nb KLT points: " << t->getNbKltPoints());
449 
450  std::list<vpMbtDistanceLine *> linesList;
451  t->getLline(linesList);
452  ROS_DEBUG_STREAM("Nb line: " << linesList.size());
453  ROS_DEBUG_STREAM("nline: " << t->nline);
454  }
455  }
456  catch(...)
457  {
458  boost::format fmt
459  ("Failed to load the model %1%\n"
460  "Do you use resource_retriever syntax?\n"
461  "I.e. replace /my/model/path by file:///my/model/path");
462  fmt % bModelPath_;
463  throw std::runtime_error(fmt.str());
464  }
465  }
466 
467  vpHomogeneousMatrix
469  {
470  vpHomogeneousMatrix cMo;
471  cMo.eye();
472 
473  std::string initialPose = getInitialPoseFileFromModelName (modelName_, modelPath_);
474  std::string resource;
475  try
476  {
477  resource = fetchResource (initialPose);
478  std::stringstream file;
479  file << resource;
480 
481  if (!file.good())
482  {
483  ROS_WARN_STREAM("failed to load initial pose: " << initialPose << "\n"
484  << "using identity as initial pose");
485  return cMo;
486  }
487 
488  vpPoseVector pose;
489  for (unsigned i = 0; i < 6; ++i) {
490  if (file.good())
491  file >> pose[i];
492  else
493  {
494  ROS_WARN("failed to parse initial pose file");
495  return cMo;
496  }
497  }
498  cMo.buildFrom(pose);
499  return cMo;
500  }
501  catch (...)
502  {
503  // Failed to retrieve initial pose since model path starts with http://, package://, file:///
504  // We try to read from temporary file /tmp/$USER/
505  std::string username;
506  vpIoTools::getUserName(username);
507 
508  std::string filename;
509  #if defined(_WIN32)
510  filename ="C:/temp/" + username;
511  #else
512  filename ="/tmp/" + username;
513  #endif
514  filename += "/" + modelName_ + ".0.pos";
515  ROS_INFO_STREAM("Try to read init pose from: " << filename);
516  if (vpIoTools::checkFilename(filename)) {
517  ROS_INFO_STREAM("Retrieve initial pose from: " << filename);
518  std::ifstream in( filename.c_str() );
519  vpPoseVector pose;
520  pose.load(in);
521  cMo.buildFrom(pose);
522  in.close();
523  }
524 
525  return cMo;
526  }
527  }
528 
529  void
530  TrackerClient::saveInitialPose(const vpHomogeneousMatrix& cMo)
531  {
532  boost::filesystem::path initialPose = getInitialPoseFileFromModelName(modelName_, modelPath_);
533  boost::filesystem::ofstream file(initialPose);
534  if (!file.good())
535  {
536  // Failed to save initial pose since model path starts with http://, package://, file:///
537  // We create a temporary file in /tmp/$USER/
538  std::string username;
539  vpIoTools::getUserName(username);
540 
541  // Create a log filename to save velocities...
542  std::string logdirname;
543  #if defined(_WIN32)
544  logdirname ="C:/temp/" + username;
545  #else
546  logdirname ="/tmp/" + username;
547  #endif
548  // Test if the output path exist. If no try to create it
549  if (vpIoTools::checkDirectory(logdirname) == false) {
550  try {
551  vpIoTools::makeDirectory(logdirname);
552  }
553  catch (...) {
554  ROS_WARN_STREAM("Unable to create the folder " << logdirname << " to save the initial pose");
555  return;
556  }
557  }
558  std::string filename = logdirname + "/" + modelName_ + ".0.pos";
559  ROS_INFO_STREAM("Save initial pose in: " << filename);
560  std::fstream finitpos ;
561  finitpos.open(filename.c_str(), std::ios::out) ;
562  vpPoseVector pose;
563  pose.buildFrom(cMo);
564 
565  finitpos << pose;
566  finitpos.close();
567  }
568  else {
569  ROS_INFO_STREAM("Save initial pose in: " << initialPose);
570  vpPoseVector pose;
571  pose.buildFrom(cMo);
572  file << pose;
573  }
574  }
575 
578  {
579  points_t points;
580 
581  std::string init =
583  std::string resource = fetchResource(init);
584  std::stringstream file;
585  file << resource;
586 
587  if (!file.good())
588  {
589  boost::format fmt("failed to load initialization points: %1");
590  fmt % init;
591  throw std::runtime_error(fmt.str());
592  }
593 
594  char c;
595  // skip lines starting with # as comment
596  file.get(c);
597  while (!file.fail() && (c == '#')) {
598  file.ignore(256, '\n');
599  file.get(c);
600  }
601  file.unget();
602 
603  unsigned int npoints;
604  file >> npoints;
605  file.ignore(256, '\n'); // skip the rest of the line
606  ROS_INFO_STREAM("Number of 3D points " << npoints << "\n");
607 
608  if (npoints > 100000) {
609  throw vpException(vpException::badValue,
610  "Exceed the max number of points.");
611  }
612 
613  vpPoint point;
614  double X = 0., Y = 0., Z = 0.;
615  for (unsigned int i=0 ; i < npoints ; i++){
616  // skip lines starting with # as comment
617  file.get(c);
618  while (!file.fail() && (c == '#')) {
619  file.ignore(256, '\n');
620  file.get(c);
621  }
622  file.unget();
623 
624  file >> X >> Y >> Z ;
625  file.ignore(256, '\n'); // skip the rest of the line
626 
627  point.setWorldCoordinates(X,Y,Z) ; // (X,Y,Z)
628  points.push_back(point);
629  }
630 
631  return points;
632  }
633 
634  bool
635  TrackerClient::validatePose(const vpHomogeneousMatrix &cMo){
636  ros::Rate loop_rate(200);
637  vpImagePoint ip;
638  vpMouseButton::vpMouseButtonType button = vpMouseButton::button1;
639  vpDisplay::display(image_);
640  tracker_->setDisplayFeatures(false);
641  tracker_->display(image_, cMo, cameraParameters_, vpColor::green);
642  vpDisplay::displayFrame(image_, cMo, cameraParameters_,frameSize_,vpColor::none,2);
643  vpDisplay::displayCharString(image_, 15, 10,
644  "Left click to validate, right click to modify initial pose",
645  vpColor::red);
646  vpDisplay::flush(image_);
647  tracker_->setDisplayFeatures(true);
648 
649  do
650  {
651  ros::spinOnce();
652  loop_rate.sleep();
653  if (!ros::ok())
654  return false;
655  }
656  while(ros::ok() && !vpDisplay::getClick(image_, ip, button, false));
657 
658  if(button == vpMouseButton::button1)
659  return true;
660 
661  return false;
662  }
663 
664  void
666  {
667  ros::Rate loop_rate(200);
668  vpHomogeneousMatrix cMo;
669  vpImagePoint point (10, 10);
670 
671  cMo = loadInitialPose();
672  tracker_->initFromPose(image_, cMo);
673 
674  // Show last cMo.
675  vpImagePoint ip;
676  vpMouseButton::vpMouseButtonType button = vpMouseButton::button1;
677 
678  if(validatePose(cMo))
679  {
680  return;
681  }
682  vpDisplayX *initHelpDisplay = NULL;
683 
684  std::string helpImagePath;
685  nodeHandlePrivate_.param<std::string>("help_image_path", helpImagePath, "");
686  if (helpImagePath.empty()){
687 
689 
690  try{
692  char* tmpname = strdup("/tmp/tmpXXXXXX");
693  if (mkdtemp(tmpname) == NULL) {
694  ROS_ERROR_STREAM("Failed to create the temporary directory: " << strerror(errno));
695  }
696  else {
697  boost::filesystem::path path(tmpname);
698  path /= ("help.ppm");
699  free(tmpname);
700 
701  helpImagePath = path.native();
702  ROS_INFO("Copy help image from %s to %s", getHelpImageFileFromModelName(modelName_, modelPath_).c_str(),
703  helpImagePath.c_str());
704 
705 
706  FILE* f = fopen(helpImagePath.c_str(), "w");
707  fwrite(resource.data.get(), resource.size, 1, f);
708  fclose(f);
709  }
710  }
711  catch(...){
712  }
713 
714  ROS_WARN_STREAM("Auto detection of help file: " << helpImagePath);
715  }
716 
717  if (!helpImagePath.empty()){
718  try {
719  // check if the file exists
720  if (! vpIoTools::checkFilename(helpImagePath)) {
721  ROS_WARN("Error tracker initialization help image file \"%s\" doesn't exist", helpImagePath.c_str());
722  }
723  else {
724  ROS_INFO_STREAM("Load help image: " << helpImagePath);
725  int winx = 0;
726  int winy = 0;
727 #if VISP_VERSION_INT >= VP_VERSION_INT(2,10,0)
728  winx = image_.display->getWindowXPosition();
729  winy = image_.display->getWindowYPosition();
730 #endif
731  initHelpDisplay = new vpDisplayX (winx+image_.getWidth()+20, winy, "Init help image");
732 
733  vpImage<vpRGBa> initHelpImage;
734  vpImageIo::read(initHelpImage, helpImagePath);
735  initHelpDisplay->init(initHelpImage);
736  vpDisplay::display(initHelpImage);
737  vpDisplay::flush(initHelpImage);
738  }
739  } catch(vpException &e) {
740  ROS_WARN("Error diplaying tracker initialization help image file \"%s\":\n%s", helpImagePath.c_str(), e.what());
741  }
742  }
743 
745  imagePoints_t imagePoints;
746 
747  bool done = false;
748  while(!done){
749  vpDisplay::display(image_);
750  vpDisplay::flush(image_);
751 
752  imagePoints.clear();
753  for(unsigned i = 0; i < points.size(); ++i)
754  {
755  do
756  {
757  ros::spinOnce();
758  loop_rate.sleep();
759  if (!ros::ok())
760  return;
761  }
762  while(ros::ok() && !vpDisplay::getClick(image_, ip, button, false));
763 
764  imagePoints.push_back(ip);
765  vpDisplay::displayCross(image_, imagePoints.back(), 5, vpColor::green);
766  vpDisplay::flush(image_);
767  }
768 
769  tracker_->initFromPoints(image_,imagePoints,points);
770  tracker_->getPose(cMo);
771  if(validatePose(cMo))
772  done = true;
773  }
774  tracker_->initFromPose(image_, cMo);
775  saveInitialPose(cMo);
776  if (initHelpDisplay != NULL)
777  delete initHelpDisplay;
778  }
779 
780  void
782  points_t& points,
783  imagePoints_t& imagePoints,
784  ros::Rate& rate,
785  vpPose& pose)
786  {
787  vpImagePoint ip;
788  double x = 0., y = 0.;
789  boost::format fmt("click on point %u/%u");
790  fmt % (i + 1) % points.size(); // list points from 1 to n.
791 
792  // Click on the point.
793  vpMouseButton::vpMouseButtonType button = vpMouseButton::button1;
794  do
795  {
796  vpDisplay::display(image_);
797  vpDisplay::displayCharString
798  (image_, 15, 10,
799  fmt.str().c_str(),
800  vpColor::red);
801 
802  for (unsigned j = 0; j < imagePoints.size(); ++j)
803  vpDisplay::displayCross(image_, imagePoints[j], 5, vpColor::green);
804 
805  vpDisplay::flush(image_);
806  ros::spinOnce();
807  rate.sleep();
808  if (exiting())
809  return;
810  }
811  while(!vpDisplay::getClick(image_, ip, button, false));
812 
813  imagePoints.push_back(ip);
814  vpPixelMeterConversion::convertPoint(cameraParameters_, ip, x, y);
815  points[i].set_x(x);
816  points[i].set_y(y);
817  pose.addPoint(points[i]);
818  }
819 
820  void
822  {
823  ros::Rate loop_rate(10);
824  while (!exiting()
825  && (!image_.getWidth() || !image_.getHeight()))
826  {
827  ROS_INFO_THROTTLE(1, "waiting for a rectified image...");
828  ros::spinOnce();
829  loop_rate.sleep();
830  }
831  }
832 
833  std::string
834  TrackerClient::fetchResource(const std::string& s)
835  {
838  std::string result;
839  result.resize(resource.size);
840  unsigned i = 0;
841  for (; i < resource.size; ++i)
842  result[i] = resource.data.get()[i];
843  return result;
844  }
845 
846  bool
847  TrackerClient::makeModelFile(boost::filesystem::ofstream& modelStream,
848  const std::string& resourcePath,
849  std::string& fullModelPath)
850  {
851  std::string modelExt_ = ".wrl";
852  bool vrmlWorked = true;
854 
855  try{
856  resource = resourceRetriever_.get(resourcePath + modelExt_);
857  }
858  catch(...){
859  vrmlWorked = false;
860  }
861 
862  if(!vrmlWorked){
863  modelExt_ = ".cao";
864 
865  try{
866  resource = resourceRetriever_.get(resourcePath + modelExt_);
867  }
868  catch(...){
869  ROS_ERROR_STREAM("No .cao nor .wrl file found in: " << resourcePath);
870  }
871  }
872 
873  modelPathAndExt_ = resourcePath + modelExt_;
874 
875  //ROS_WARN_STREAM("Model file Make Client: " << resourcePath << modelExt_);
876 
877  // Crash after when model not found
878  std::string result;
879  result.resize(resource.size);
880  unsigned i = 0;
881  for (; i < resource.size; ++i)
882  result[i] = resource.data.get()[i];
883  result[resource.size];
884 
885  char* tmpname = strdup("/tmp/tmpXXXXXX");
886  if (mkdtemp(tmpname) == NULL)
887  {
889  ("Failed to create the temporary directory: " << strerror(errno));
890  return false;
891  }
892  boost::filesystem::path path(tmpname);
893  path /= ("model" + modelExt_);
894  free(tmpname);
895 
896  fullModelPath = path.native();
897 
898 
899  //ROS_WARN_STREAM("Model file Make Client Full path tmp: " << fullModelPath );
900 
901  modelStream.open(path);
902  if (!modelStream.good())
903  {
905  ("Failed to create the temporary file: " << path);
906  return false;
907  }
908  modelStream << result;
909  modelStream.flush();
910  return true;
911  }
912 } // end of namespace visp_tracker.
d
boost::filesystem::path bInitPath_
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
filename
std::string convertVpMeToRosMessage(const vpMbTracker *tracker, const vpMe &moving_edge)
Definition: conversion.cpp:113
TrackerClient(ros::NodeHandle &nh, ros::NodeHandle &privateNh, volatile bool &exiting, unsigned queueSize=5u)
void reconfigureCallback(vpMbTracker *tracker, vpImage< unsigned char > &I, vpMe &moving_edge, vpKltOpencv &kltTracker, boost::recursive_mutex &mutex, visp_tracker::ModelBasedSettingsConfig &config, uint32_t level)
Definition: callbacks.cpp:57
f
image_transport::CameraSubscriber::Callback bindImageCallback(vpImage< unsigned char > &image)
Definition: callbacks.cpp:42
void convertVpMeToInitRequest(const vpMe &moving_edge, const vpMbTracker *tracker, visp_tracker::Init &srv)
Definition: conversion.cpp:254
std::string getHelpImageFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:26
CameraSubscriber subscribeCamera(const std::string &base_topic, uint32_t queue_size, const CameraSubscriber::Callback &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
void start(const ros::V_string &topics, double duration)
void checkInputs()
Make sure the topics we subscribe already exist.
void saveInitialPose(const vpHomogeneousMatrix &cMo)
ros::NodeHandle & nodeHandle_
std::string convertVpKltOpencvToRosMessage(const vpMbTracker *tracker, const vpKltOpencv &klt)
Definition: conversion.cpp:134
bool call(MReq &req, MRes &res)
image_transport::ImageTransport imageTransport_
void convertVpMbTrackerToInitRequest(const vpMbTracker *tracker, visp_tracker::Init &srv)
Definition: conversion.cpp:208
reconfigureSrvStruct< visp_tracker::ModelBasedSettingsEdgeConfig >::reconfigureSrv_t * reconfigureEdgeSrv_
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
ros::NodeHandle & nodeHandlePrivate_
std::string init_service
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< vpPoint > points_t
#define ROS_WARN(...)
resource_retriever::Retriever resourceRetriever_
bool validatePose(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix loadInitialPose()
std::string getModelFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:35
image_proc::AdvertisementChecker checkInputs_
Helper used to check that subscribed topics exist.
std::string getInitFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:17
void initPoint(unsigned &i, points_t &points, imagePoints_t &imagePoints, ros::Rate &rate, vpPose &pose)
std::vector< std::string > V_string
#define ROS_INFO_THROTTLE(period,...)
ROSCPP_DECL const std::string & getNamespace()
boost::shared_array< uint8_t > data
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
ROSCPP_DECL bool get(const std::string &key, std::string &s)
std::string getInitialPoseFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:53
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
std::string model_description_param
dynamic_reconfigure::Server< ConfigType > reconfigureSrv_t
ROSCPP_DECL bool ok()
image_transport::CameraSubscriber cameraSubscriber_
TFSIMD_FORCE_INLINE const tfScalar & x() const
void sendcMo(const vpHomogeneousMatrix &cMo)
void reconfigureEdgeCallback(vpMbTracker *tracker, vpImage< unsigned char > &I, vpMe &moving_edge, boost::recursive_mutex &mutex, visp_tracker::ModelBasedSettingsEdgeConfig &config, uint32_t level)
Definition: callbacks.cpp:107
void reconfigureKltCallback(vpMbTracker *tracker, vpImage< unsigned char > &I, vpKltOpencv &kltTracker, boost::recursive_mutex &mutex, visp_tracker::ModelBasedSettingsKltConfig &config, uint32_t level)
Definition: callbacks.cpp:142
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
bool sleep()
MemoryResource get(const std::string &url)
sensor_msgs::CameraInfoConstPtr info_
reconfigureSrvStruct< visp_tracker::ModelBasedSettingsConfig >::reconfigureSrv_t * reconfigureSrv_
#define ROS_INFO_STREAM(args)
void convertVpKltOpencvToInitRequest(const vpKltOpencv &klt, const vpMbTracker *tracker, visp_tracker::Init &srv)
Definition: conversion.cpp:314
reconfigureSrvStruct< visp_tracker::ModelBasedSettingsKltConfig >::reconfigureSrv_t * reconfigureKltSrv_
bool getParam(const std::string &key, std::string &s) const
boost::filesystem::path bModelPath_
void initializeVpCameraFromCameraInfo(vpCameraParameters &cam, sensor_msgs::CameraInfoConstPtr info)
Definition: conversion.cpp:348
std::string default_model_path
std::string init_service_viewer
#define ROS_ERROR_STREAM(args)
vpCameraParameters cameraParameters_
void vpHomogeneousMatrixToTransform(geometry_msgs::Transform &dst, const vpHomogeneousMatrix &src)
Definition: conversion.cpp:151
std::string convertVpMbTrackerToRosMessage(const vpMbTracker *tracker)
Definition: conversion.cpp:87
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
std::vector< vpImagePoint > imagePoints_t
bool makeModelFile(boost::filesystem::ofstream &modelStream, const std::string &resourcePath, std::string &fullModelPath)
boost::recursive_mutex mutex_
std::string fetchResource(const std::string &)


visp_tracker
Author(s): Thomas Moulard
autogenerated on Wed Jul 3 2019 19:48:07