ShapeTrackerNode.cpp
Go to the documentation of this file.
00001 #include "shape_tracker/ShapeTrackerNode.h"
00002 
00003 #include <sensor_msgs/Image.h>
00004 
00005 #include <pcl_conversions/pcl_conversions.h>
00006 
00007 #include <ros/package.h>
00008 
00009 #include <cmath>
00010 
00011 #include <ros/console.h>
00012 
00013 #include <pcl/point_cloud.h>
00014 #include <pcl/point_types.h>
00015 #include <pcl/filters/passthrough.h>
00016 
00017 #include <boost/filesystem.hpp>
00018 #include <ctime>
00019 
00020 #include <omip_msgs/ShapeTrackerStates.h>
00021 
00022 using namespace omip;
00023 
00024 ShapeTrackerNode::ShapeTrackerNode() :
00025     _active(true)
00026 {
00027 
00028     this->_node_quit_subscriber = this->_node_handle.subscribe("/omip/shutdown", 1, &ShapeTrackerNode::TrackerNodeQuitCallback, this);
00029 
00030     this->_rgbd_pc_subscriber.subscribe(this->_node_handle, "/camera/depth_registered/points",1);
00031     this->_poses_and_vels_subscriber.subscribe(this->_node_handle, "/rb_tracker/state_after_feat_correction",1);
00032 
00033     this->_synchronizer = new message_filters::Synchronizer<STSyncPolicy>(STSyncPolicy(10), this->_rgbd_pc_subscriber, this->_poses_and_vels_subscriber);
00034     this->_synchronizer->registerCallback(boost::bind(&ShapeTrackerNode::RigibBodyMotionsAndPCCallback, this, _1, _2));
00035 
00036     int model_to_listen;
00037     this->_node_handle.getParam("/shape_tracker/model_type_to_listen", model_to_listen);
00038     this->_model_type_to_listen = (shape_model_selector_t)model_to_listen;
00039     ROS_INFO_STREAM_NAMED("ShapeTrackerNode.ShapeTrackerNode","model_type_to_listen = " << this->_model_type_to_listen);
00040 
00041     this->_shape_models_subscriber = this->_node_handle.subscribe("/shape_recons/state", 10, &ShapeTrackerNode::ShapeModelsCallback, this);
00042 
00043     this->_st_state_pub = this->_node_handle.advertise<omip_msgs::ShapeTrackerStates>("/shape_tracker/state", 1);
00044 
00045     std::string ci_topic;
00046     this->getROSParameter<std::string>(std::string("/feature_tracker/camera_info_topic"),ci_topic);
00047     this->_ci_sub = this->_node_handle.subscribe(ci_topic, 1,
00048                                                  &ShapeTrackerNode::CameraInfoCallback, this);
00049 }
00050 
00051 ShapeTrackerNode::~ShapeTrackerNode()
00052 {
00053 
00054 }
00055 
00056 void ShapeTrackerNode::TrackerNodeQuitCallback(const std_msgs::EmptyConstPtr &msg)
00057 {
00058     ROS_INFO_STREAM("Shape Tracker node quit stopping!");
00059     _active = false;
00060 }
00061 
00062 void ShapeTrackerNode::CameraInfoCallback(const sensor_msgs::CameraInfoConstPtr &ci_msg)
00063 {
00064     // We have to do this because sometimes the first ci message that is sent is zero (why?)
00065     if(ci_msg->height != 0)
00066     {
00067         this->_ci = sensor_msgs::CameraInfo(*ci_msg);
00068         this->_ci_sub.shutdown();
00069         for(shape_trackers_map_t::iterator it = this->_rb_trackers.begin(); it!=this->_rb_trackers.end(); it++)
00070         {
00071             it->second->setCameraInfo(this->_ci);
00072         }
00073     }
00074 }
00075 
00076 void ShapeTrackerNode::ShapeModelsCallback(const omip_msgs::ShapeModelsConstPtr &models_msg)
00077 {
00078     ROS_INFO_STREAM_NAMED("ShapeTrackerNode.ShapeModelsCallback", "Received new shape models. " << models_msg->rb_shape_models.size() << " models.");
00079     omip::RB_id_t rb_id_temp;
00080     for(int rb_models_idx = 0; rb_models_idx < models_msg->rb_shape_models.size(); rb_models_idx++)
00081     {
00082         rb_id_temp = models_msg->rb_shape_models.at(rb_models_idx).rb_id;
00083         int num_points = models_msg->rb_shape_models.at(rb_models_idx).rb_shape_model.height*models_msg->rb_shape_models.at(rb_models_idx).rb_shape_model.width;
00084         ROS_INFO_STREAM_NAMED("ShapeTrackerNode.ShapeModelsCallback",
00085                               "Received new shape model of RB " << rb_id_temp << " containing " << num_points  << " points");
00086 
00087         // If this RB (identified by its id) was previously tracked, we already have a ShapeTracker object for it
00088         if(this->_rb_trackers.find(rb_id_temp) == this->_rb_trackers.end())
00089         {
00090             omip::ShapeTrackerPtr tracker_ptr = omip::ShapeTrackerPtr(new omip::ShapeTracker(rb_id_temp));
00091             tracker_ptr->setCameraInfo(this->_ci);
00092             this->_rb_trackers[rb_id_temp] = tracker_ptr;
00093 
00094         }
00095         this->_rb_trackers[rb_id_temp]->setShapeModel(models_msg->rb_shape_models.at(rb_models_idx).rb_shape_model);
00096     }
00097 }
00098 
00099 void ShapeTrackerNode::RigibBodyMotionsAndPCCallback(const sensor_msgs::PointCloud2ConstPtr &pc_msg, const boost::shared_ptr<omip::rbt_state_t const> &poses_and_vels)
00100 {
00101     //ROS_WARN_STREAM( "Received. Poses time: " << poses_and_vels->header.stamp << " PC time: " << pc_msg->header.stamp );
00102     ros::WallTime t_init = ros::WallTime::now();
00103     omip::RB_id_t rb_id_temp;
00104     omip_msgs::ShapeTrackerStates shape_tracker_states;
00105     for(int rb_poses_idx = 1; rb_poses_idx < poses_and_vels->rb_poses_and_vels.size(); rb_poses_idx++)
00106     {
00107         rb_id_temp = poses_and_vels->rb_poses_and_vels.at(rb_poses_idx).rb_id;
00108 
00109         omip_msgs::ShapeTrackerState shape_tracker_state;
00110         // If this RB (identified by its id) was previously tracked, we already have a ShapeTracker object for it
00111         if(this->_rb_trackers.find(rb_id_temp) != this->_rb_trackers.end())
00112         {
00113             this->_rb_trackers[rb_id_temp]->step(pc_msg,
00114                                                  poses_and_vels->rb_poses_and_vels.at(rb_poses_idx),
00115                                                  shape_tracker_state);
00116             shape_tracker_states.shape_tracker_states.push_back(shape_tracker_state);
00117         }
00118     }
00119     //ROS_WARN_STREAM( "poses time minus pc time: " << (poses_and_vels->header.stamp - pc_msg->header.stamp).toSec() );
00120     shape_tracker_states.header.stamp = poses_and_vels->header.stamp;
00121     this->_st_state_pub.publish(shape_tracker_states);
00122     ros::WallTime t_end = ros::WallTime::now();
00123     ROS_ERROR_STREAM("Total time ST: " << t_end - t_init);
00124 }
00125 
00126 // Main program
00127 int main(int argc, char** argv)
00128 {
00129     ros::init(argc, argv, "ShapeTrackerNode");
00130     ShapeTrackerNode st_node;
00131 
00132     ros::Rate r(100); // 10 hz
00133     while (ros::ok() && st_node.getActive())
00134     {
00135         ros::spinOnce();
00136         r.sleep();
00137     }
00138 
00139     ROS_ERROR_STREAM("Shutting down ShapeTrackerNode");
00140 
00141     return (0);
00142 }


shape_tracker
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:54:11