node.cpp
Go to the documentation of this file.
1 #include "node.h"
2 #include "names.h"
3 
4 //command line parameters
5 #include "cmd_line/cmd_line.h"
6 
7 
8 //tracking
11 #include "libauto_tracker/events.h"
12 
13 #include "visp_tracker/MovingEdgeSites.h"
14 #include "visp_tracker/KltPoints.h"
15 
16 //visp includes
17 #include <visp/vpDisplayX.h>
18 #include <visp/vpMbEdgeKltTracker.h>
19 #include <visp/vpTime.h>
20 
21 //detectors
22 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
25 #else
26 # include <visp/vpDetectorDataMatrixCode.h>
27 # include <visp/vpDetectorQRCode.h>
28 #endif
29 
30 #include <visp_bridge/camera.h>
31 #include <visp_bridge/image.h>
32 #include <visp_bridge/3dpose.h>
33 
35 
37 
38 #include "std_msgs/Int8.h"
39 #include "std_msgs/String.h"
40 
41 namespace visp_auto_tracker{
43  n_("~"),
44  queue_size_(1),
45  tracker_config_path_(),
46  model_description_(),
47  model_path_(),
48  model_name_(),
49  code_message_(),
50  debug_display_(false),
51  I_(),
52  image_header_(),
53  got_image_(false),
54  cam_(),
55  t_(NULL) {
56  //get the tracker configuration file
57  //this file contains all of the tracker's parameters, they are not passed to ros directly.
58  n_.param<std::string>("tracker_config_path", tracker_config_path_, "");
59  n_.param<bool>("debug_display", debug_display_, false);
60  std::string model_full_path;
61  n_.param<std::string>("model_path", model_path_, "");
62  n_.param<std::string>("model_name", model_name_, "");
63  n_.param<std::string>("code_message", code_message_, "");
64  model_path_= model_path_[model_path_.length()-1]=='/'?model_path_:model_path_+std::string("/");
65  model_full_path = model_path_+model_name_;
66  tracker_config_path_ = model_full_path+".cfg";
67  ROS_INFO("model full path=%s",model_full_path.c_str());
68 
69  //Parse command line arguments from config file (as ros param)
70  cmd_.init(tracker_config_path_);
71  cmd_.set_data_directory(model_path_); //force data path
72  cmd_.set_pattern_name(model_name_); //force model name
73  cmd_.set_show_fps(false);
74  if (! code_message_.empty()) {
75  ROS_WARN_STREAM("Track only code with message: \"" << code_message_ << "\"");
76  cmd_.set_code_message(code_message_);
77  }
78 
81  try {
82  res = r.get(std::string("file://")+cmd_.get_mbt_cad_file());
83  }
84  catch(...) {
85  ROS_ERROR_STREAM("Unable to read wrl or cao model file as resource: " << std::string("file://")+cmd_.get_mbt_cad_file());
86  }
87 
88  model_description_.resize(res.size);
89  for (unsigned int i=0; i < res.size; ++i)
90  model_description_[i] = res.data.get()[i];
91 
92  ROS_INFO("Model content=%s",model_description_.c_str());
93 
94  n_.setParam ("/model_description", model_description_);
95  }
96 
98  while ( ros::ok ()){
99  if(got_image_) return;
100  ros::spinOnce();
101  }
102  }
103 
104  //records last recieved image
105  void Node::frameCallback(const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& cam_info){
106  boost::mutex::scoped_lock(lock_);
107  image_header_ = image->header;
108  I_ = visp_bridge::toVispImageRGBa(*image); //make sure the image isn't worked on by locking a mutex
110 
111  got_image_ = true;
112  }
113 
114  void Node::spin(){
115 
116  if(cmd_.should_exit()) return; //exit if needed
117 
118  vpMbTracker* tracker; //mb-tracker will be chosen according to config
119 
120  //create display
121  vpDisplayX* d = NULL;
122  if(debug_display_)
123  d = new vpDisplayX();
124 
125  //init detector based on user preference
126 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
127  detectors::DetectorBase* detector = NULL;
129  detector = new detectors::qrcode::Detector;
130  else if(cmd_.get_detector_type() == CmdLine::DMTX)
131  detector = new detectors::datamatrix::Detector;
132 #else // ViSP >= 2.10.0. In that case we use the detectors from ViSP
133  vpDetectorBase *detector = NULL;
135  detector = new vpDetectorQRCode;
136  else if(cmd_.get_detector_type() == CmdLine::DMTX)
137  detector = new vpDetectorDataMatrixCode;
138 #endif
139 
140 #if 0
141  //init tracker based on user preference
143  tracker = new vpMbKltTracker();
145  tracker = new vpMbEdgeKltTracker();
146  else if(cmd_.get_tracker_type() == CmdLine::MBT)
147  tracker = new vpMbEdgeTracker();
148 #else
149  // Use the best tracker
150  tracker = new vpMbEdgeKltTracker();
151 #endif
152  tracker->setCameraParameters(cam_);
153  tracker->setDisplayFeatures(true);
154 
155  //compile detectors and paramters into the automatic tracker.
156  t_ = new tracking::Tracker(cmd_, detector, tracker, debug_display_);
157  t_->start(); //start the state machine
158 
159  //subscribe to ros topics and prepare a publisher that will publish the pose
162  message_filters::TimeSynchronizer<sensor_msgs::Image, sensor_msgs::CameraInfo> image_info_sync(raw_image_subscriber, camera_info_subscriber, queue_size_);
163  image_info_sync.registerCallback(boost::bind(&Node::frameCallback,this, _1, _2));
164  ros::Publisher object_pose_publisher = n_.advertise<geometry_msgs::PoseStamped>(object_position_topic, queue_size_);
165  ros::Publisher object_pose_covariance_publisher = n_.advertise<geometry_msgs::PoseWithCovarianceStamped>(object_position_covariance_topic, queue_size_);
166  ros::Publisher moving_edge_sites_publisher = n_.advertise<visp_tracker::MovingEdgeSites>(moving_edge_sites_topic, queue_size_);
167  ros::Publisher klt_points_publisher = n_.advertise<visp_tracker::KltPoints>(klt_points_topic, queue_size_);
168  ros::Publisher status_publisher = n_.advertise<std_msgs::Int8>(status_topic, queue_size_);
169  ros::Publisher code_message_publisher = n_.advertise<std_msgs::String>(code_message_topic, queue_size_);
170 
171  //wait for an image to be ready
172  waitForImage();
173  {
174  //when an image is ready tell the tracker to start searching for patterns
175  boost::mutex::scoped_lock(lock_);
176  if(debug_display_) {
177  d->init(I_); //also init display
178  vpDisplay::setTitle(I_, "visp_auto_tracker debug display");
179  }
180 
181  t_->process_event(tracking::select_input(I_));
182  }
183 
184  unsigned int iter=0;
185  geometry_msgs::PoseStamped ps;
186  geometry_msgs::PoseWithCovarianceStamped ps_cov;
187 
188  ros::Rate rate(30); //init 25fps publishing frequency
189  while(ros::ok()){
190  double t = vpTime::measureTimeMs();
191  boost::mutex::scoped_lock(lock_);
192  //process the new frame with the tracker
193  t_->process_event(tracking::input_ready(I_,cam_,iter));
194  //When the tracker is tracking, it's in the tracking::TrackModel state
195  //Access this state and broadcast the pose
196  tracking::TrackModel& track_model = t_->get_state<tracking::TrackModel&>();
197 
198  ps.pose = visp_bridge::toGeometryMsgsPose(track_model.cMo); //convert
199 
200  // Publish resulting pose.
201  if (object_pose_publisher.getNumSubscribers () > 0)
202  {
203  ps.header = image_header_;
204  ps.header.frame_id = tracker_ref_frame;
205  object_pose_publisher.publish(ps);
206  }
207 
208  // Publish resulting pose covariance matrix.
209  if (object_pose_covariance_publisher.getNumSubscribers () > 0)
210  {
211  ps_cov.pose.pose = ps.pose;
212  ps_cov.header = image_header_;
213  ps_cov.header.frame_id = tracker_ref_frame;
214 
215  for (unsigned i = 0; i < track_model.covariance.getRows(); ++i)
216  {
217  for (unsigned j = 0; j < track_model.covariance.getCols(); ++j)
218  {
219  unsigned idx = i * track_model.covariance.getCols() + j;
220  if (idx >= 36)
221  continue;
222  ps_cov.pose.covariance[idx] = track_model.covariance[i][j];
223  }
224  }
225 
226  object_pose_covariance_publisher.publish(ps_cov);
227  }
228 
229  // Publish state machine status.
230  if (status_publisher.getNumSubscribers () > 0)
231  {
232  std_msgs::Int8 status;
233  status.data = (unsigned char)(*(t_->current_state()));
234  status_publisher.publish(status);
235  }
236 
237  // Publish moving edge sites.
238  if (moving_edge_sites_publisher.getNumSubscribers () > 0)
239  {
240  visp_tracker::MovingEdgeSitesPtr sites (new visp_tracker::MovingEdgeSites);
241  // Test if we are in the state tracking::TrackModel. In that case the pose is good;
242  // we can send the moving edges. Otherwise we send an empty list of features
243  if (*(t_->current_state()) == 3) {
244  t_->updateMovingEdgeSites(sites);
245  }
246 
247  sites->header = image_header_;
248  moving_edge_sites_publisher.publish(sites);
249  }
250 
251  // Publish KLT points.
252  if (klt_points_publisher.getNumSubscribers () > 0)
253  {
254  visp_tracker::KltPointsPtr klt (new visp_tracker::KltPoints);
255  // Test if we are in the state tracking::TrackModel. In that case the pose is good;
256  // we can send the klt points. Otherwise we send an empty list of features
257  if (*(t_->current_state()) == 3) {
258  t_->updateKltPoints(klt);
259  }
260  klt->header = image_header_;
261  klt_points_publisher.publish(klt);
262  }
263 
264  if (code_message_publisher.getNumSubscribers() > 0)
265  {
266  std_msgs::String message;
267  if (*(t_->current_state()) == 3) { // Tracking successful
268 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
269  message.data = detector->get_message();
270 #else
271  message.data = detector->getMessage( cmd_.get_code_message_index() );
272 #endif
273  }
274  else {
275  message.data = std::string();
276  }
277  code_message_publisher.publish(message);
278  ROS_INFO_STREAM("Code with message \"" << message.data << "\" under tracking");
279  }
280 
281  ros::spinOnce();
282  rate.sleep();
283  if (cmd_.show_fps())
284  std::cout << "Tracking done in " << vpTime::measureTimeMs() - t << " ms" << std::endl;
285  }
286  t_->process_event(tracking::finished());
287  }
288 }
d
std::string klt_points_topic
size_t get_code_message_index() const
Definition: cmd_line.cpp:343
void publish(const boost::shared_ptr< M > &message) const
vpCameraParameters toVispCameraParameters(const sensor_msgs::CameraInfo &cam_info)
std::string tracker_config_path_
Definition: node.h:24
tracking::Tracker * t_
Definition: node.h:36
std::string object_position_covariance_topic
std::string camera_info_topic
msm::back::state_machine< Tracker_ > Tracker
Definition: tracking.h:186
ros::NodeHandle n_
Definition: node.h:22
std::string status_topic
std::string model_name_
Definition: node.h:27
void init(std::string &config_file)
Definition: cmd_line.cpp:135
bool should_exit() const
Definition: cmd_line.cpp:226
geometry_msgs::Pose toGeometryMsgsPose(const vpHomogeneousMatrix &mat)
std::string code_message_topic
Connection registerCallback(C &callback)
boost::shared_array< uint8_t > data
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
vpCameraParameters cam_
Definition: node.h:34
std::string code_message_
Definition: node.h:28
ROSCPP_DECL bool ok()
void set_pattern_name(std::string &name)
Definition: cmd_line.cpp:363
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
std::string object_position_topic
bool sleep()
std::string model_path_
Definition: node.h:26
MemoryResource get(const std::string &url)
void set_show_fps(bool show_fps)
Definition: cmd_line.cpp:366
boost::mutex lock_
Definition: node.h:21
vpImage< vpRGBa > I_
Definition: node.h:31
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
void set_data_directory(std::string &dir)
Definition: cmd_line.cpp:359
std::string image_topic
std::string model_description_
Definition: node.h:25
bool show_fps() const
Definition: cmd_line.cpp:234
std::string tracker_ref_frame
TRACKER_TYPE get_tracker_type() const
Definition: cmd_line.cpp:314
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
r
void set_code_message(const std::string &msg)
Definition: cmd_line.cpp:370
std::string moving_edge_sites_topic
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
vpImage< vpRGBa > toVispImageRGBa(const sensor_msgs::Image &src)
unsigned long queue_size_
Definition: node.h:23
DETECTOR_TYPE get_detector_type() const
Definition: cmd_line.cpp:307
std_msgs::Header image_header_
Definition: node.h:32
void frameCallback(const sensor_msgs::ImageConstPtr &image, const sensor_msgs::CameraInfoConstPtr &cam_info)
Definition: node.cpp:105
std::string get_mbt_cad_file() const
Definition: cmd_line.cpp:270


visp_auto_tracker
Author(s): Filip Novotny
autogenerated on Wed Jul 3 2019 19:48:10