cam3d_throttle.cpp
Go to the documentation of this file.
00001 
00060 #include "ros/ros.h"
00061 #include "pluginlib/class_list_macros.h"
00062 #include "nodelet/nodelet.h"
00063 
00064 #include "sensor_msgs/PointCloud2.h"
00065 #include <sensor_msgs/Image.h>
00066 #include <sensor_msgs/CameraInfo.h>
00067 
00068 #include <message_filters/subscriber.h>
00069 #include <message_filters/synchronizer.h>
00070 #include <message_filters/sync_policies/approximate_time.h>
00071 
00072 
00073 namespace cob_cam3d_throttle
00074 {
00075 typedef sensor_msgs::PointCloud2 tPointCloud;
00076 typedef sensor_msgs::Image tImage;
00077 typedef sensor_msgs::CameraInfo tCameraInfo;
00078 //typedef message_filters::sync_policies::ApproximateTime<tPointCloud, tImage, tCameraInfo, tImage, tPointCloud> tSyncPolicy;
00079 typedef message_filters::sync_policies::ApproximateTime<tPointCloud, tImage, tCameraInfo> tSyncPolicy;
00080 
00081 class Cam3DThrottle : public nodelet::Nodelet
00082 {
00083 public:
00084   //Constructor
00085   Cam3DThrottle() :
00086     max_update_rate_(0),
00087     sub_counter_(0),
00088     sync_(tSyncPolicy(3))
00089   {
00090   };
00091 
00092 private:
00093   virtual void onInit()
00094   {
00095     nh_ = getNodeHandle();
00096     ros::NodeHandle& private_nh = getPrivateNodeHandle();
00097 
00098     private_nh.getParam("max_rate", max_update_rate_);
00099 
00100     rgb_cloud_pub_ = nh_.advertise<tPointCloud>("rgb_cloud_out", 1, boost::bind(&Cam3DThrottle::connectCB, this, _1), boost::bind(&Cam3DThrottle::disconnectCB, this, _1));
00101     rgb_image_pub_ = nh_.advertise<tImage>("rgb_image_out", 1, boost::bind(&Cam3DThrottle::connectCB, this, _1), boost::bind(&Cam3DThrottle::disconnectCB, this, _1));
00102     rgb_caminfo_pub_ = nh_.advertise<tCameraInfo>("rgb_caminfo_out", 1, boost::bind(&Cam3DThrottle::connectCB, this, _1), boost::bind(&Cam3DThrottle::disconnectCB, this, _1));
00103     depth_image_pub_ = nh_.advertise<tImage>("depth_image_out", 1, boost::bind(&Cam3DThrottle::connectCB, this, _1), boost::bind(&Cam3DThrottle::disconnectCB, this, _1));
00104     cloud_pub_ = nh_.advertise<tPointCloud>("cloud_out", 1, boost::bind(&Cam3DThrottle::connectCB, this, _1), boost::bind(&Cam3DThrottle::disconnectCB, this, _1));
00105 
00106     last_update_ = ros::Time::now();
00107     //sync_.connectInput(rgb_cloud_sub_, rgb_image_sub_, rgb_caminfo_sub_, depth_image_sub_, cloud_sub_);
00108     //sync_.registerCallback(boost::bind(&Cam3DThrottle::callback, this, _1, _2, _3, _4, _5));
00109     sync_.connectInput(rgb_cloud_sub_, rgb_image_sub_, rgb_caminfo_sub_);
00110     sync_.registerCallback(boost::bind(&Cam3DThrottle::callback, this, _1, _2, _3));
00111   };
00112 
00113 /*  void callback(const tPointCloud::ConstPtr& rgb_cloud,
00114                 const tImage::ConstPtr& rgb_image,
00115                 const tCameraInfo::ConstPtr& rgb_caminfo,
00116                 const tImage::ConstPtr& depth_image,
00117                 const tPointCloud::ConstPtr& cloud
00118                 )
00119   {
00120     if (max_update_rate_ > 0.0)
00121     {
00122       NODELET_DEBUG("update set to %f", max_update_rate_);
00123       if ( last_update_ + ros::Duration(1.0/max_update_rate_) > ros::Time::now())
00124       {
00125         NODELET_DEBUG("throttle last update at %f skipping", last_update_.toSec());
00126         return;
00127       }
00128     }
00129     else
00130       NODELET_DEBUG("update_rate unset continuing");
00131     last_update_ = ros::Time::now();
00132     rgb_cloud_pub_.publish(rgb_cloud);
00133     rgb_image_pub_.publish(rgb_image);
00134     rgb_caminfo_pub_.publish(rgb_caminfo);
00135     depth_image_pub_.publish(depth_image);
00136     cloud_pub_.publish(cloud);
00137   }*/
00138   
00139   void callback(const tPointCloud::ConstPtr& rgb_cloud,
00140                 const tImage::ConstPtr& rgb_image,
00141                 const tCameraInfo::ConstPtr& rgb_caminfo
00142                 )
00143   {
00144     
00145     if (max_update_rate_ > 0.0)
00146     {
00147       NODELET_DEBUG("update set to %f", max_update_rate_);
00148       //std::cout << "update rate: " << max_update_rate_ << std::endl;
00149       if ( last_update_ + ros::Duration(1.0/max_update_rate_) > ros::Time::now())
00150       {
00151         //std::cout << "last_update[s] " << last_update_.toSec() << std::endl;
00152         NODELET_DEBUG("throttle last update at %f skipping", last_update_.toSec());
00153         return;
00154       }
00155     }
00156     else
00157     {
00158       NODELET_DEBUG("update_rate unset continuing");
00159     }
00160     //std::cout << "publishing" << std::endl;
00161     last_update_ = ros::Time::now();
00162     rgb_cloud_pub_.publish(rgb_cloud);
00163     rgb_image_pub_.publish(rgb_image);
00164     rgb_caminfo_pub_.publish(rgb_caminfo);
00165   }
00166 
00167   void connectCB(const ros::SingleSubscriberPublisher& pub)
00168   {
00169     sub_counter_++;
00170     //std::cout << "connectCB: "<<  sub_counter_ << std::endl;
00171     if(sub_counter_ == 1)
00172     {
00173       ROS_DEBUG("connecting");
00174       rgb_cloud_sub_.subscribe(nh_, "rgb_cloud_in", 1);
00175       rgb_image_sub_.subscribe(nh_, "rgb_image_in", 1);
00176       rgb_caminfo_sub_.subscribe(nh_, "rgb_caminfo_in", 1);
00177 //      depth_image_sub_.subscribe(nh_, "depth_image_in", 1);
00178 //      cloud_sub_.subscribe(nh_, "cloud_in", 1);
00179     }
00180   }
00181 
00182   void disconnectCB(const ros::SingleSubscriberPublisher& pub)
00183   {
00184     sub_counter_--;
00185     if(sub_counter_ == 0)
00186     {
00187       ROS_DEBUG("disconnecting");
00188       rgb_cloud_sub_.unsubscribe();
00189       rgb_image_sub_.unsubscribe();
00190       rgb_caminfo_sub_.unsubscribe();
00191 //      depth_image_sub_.unsubscribe();
00192 //      cloud_sub_.unsubscribe();
00193     }
00194   }
00195 
00196   ros::Time last_update_;
00197   double max_update_rate_;
00198   unsigned int sub_counter_;
00199 
00200   ros::NodeHandle nh_;
00201   ros::Subscriber sub_;
00202   ros::Subscriber sub2_;
00203   ros::Publisher rgb_cloud_pub_, rgb_image_pub_, rgb_caminfo_pub_, depth_image_pub_, cloud_pub_;
00204 
00205   message_filters::Subscriber<tPointCloud> rgb_cloud_sub_;
00206   message_filters::Subscriber<tImage> rgb_image_sub_;
00207   message_filters::Subscriber<tCameraInfo> rgb_caminfo_sub_;
00208   message_filters::Subscriber<tImage> depth_image_sub_;
00209   message_filters::Subscriber<tPointCloud> cloud_sub_;
00210   message_filters::Synchronizer<tSyncPolicy> sync_;
00211 
00212 };
00213 
00214 
00215 PLUGINLIB_DECLARE_CLASS(cob_cam3d_throttle, Cam3DThrottle, cob_cam3d_throttle::Cam3DThrottle, nodelet::Nodelet);
00216 }
00217 


cob_cam3d_throttle
Author(s): Georg Arbeiter
autogenerated on Thu Aug 27 2015 12:46:36