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
00079 typedef message_filters::sync_policies::ApproximateTime<tPointCloud, tImage, tCameraInfo> tSyncPolicy;
00080
00081 class Cam3DThrottle : public nodelet::Nodelet
00082 {
00083 public:
00084
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
00108
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
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
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
00149 if ( last_update_ + ros::Duration(1.0/max_update_rate_) > ros::Time::now())
00150 {
00151
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
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
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
00178
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
00192
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