Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <jsk_topic_tools/lightweight_throttle_nodelet.h>
00036
00037 namespace jsk_topic_tools
00038 {
00039 void LightweightThrottle::onInit()
00040 {
00041 pnh_ = this->getPrivateNodeHandle();
00042 latest_stamp_ = ros::Time::now();
00043 advertised_ = false;
00044 subscribing_ = false;
00045
00046 srv_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(pnh_);
00047 dynamic_reconfigure::Server<Config>::CallbackType f =
00048 boost::bind(&LightweightThrottle::configCallback, this, _1, _2);
00049 srv_->setCallback(f);
00050
00051
00052
00053
00054 sub_.reset(new ros::Subscriber(
00055 pnh_.subscribe<topic_tools::ShapeShifter>("input", 1,
00056 &LightweightThrottle::inCallback,
00057 this,
00058 th_)));
00059 }
00060
00061 void LightweightThrottle::configCallback(Config& config, uint32_t level)
00062 {
00063 boost::mutex::scoped_lock lock(mutex_);
00064 update_rate_ = config.update_rate;
00065 }
00066
00067 void LightweightThrottle::connectionCallback(
00068 const ros::SingleSubscriberPublisher& pub)
00069 {
00070 if (pub_.getNumSubscribers() > 0) {
00071 if (!subscribing_) {
00072 sub_.reset(new ros::Subscriber(
00073 pnh_.subscribe<topic_tools::ShapeShifter>(
00074 "input", 1,
00075 &LightweightThrottle::inCallback,
00076 this,
00077 th_)));
00078 subscribing_ = true;
00079 }
00080 }
00081 else {
00082 if (subscribing_) {
00083 sub_->shutdown();
00084 subscribing_ = false;
00085 }
00086 }
00087 }
00088
00089 void LightweightThrottle::inCallback(
00090 const boost::shared_ptr<topic_tools::ShapeShifter const>& msg)
00091 {
00092 boost::mutex::scoped_lock lock(mutex_);
00093
00094 if (!advertised_) {
00095
00096 sub_->shutdown();
00097 ros::SubscriberStatusCallback connect_cb
00098 = boost::bind(&LightweightThrottle::connectionCallback, this, _1);
00099 ros::AdvertiseOptions opts("output", 1,
00100 msg->getMD5Sum(),
00101 msg->getDataType(),
00102 msg->getMessageDefinition(),
00103 connect_cb,
00104 connect_cb);
00105 advertised_ = true;
00106 pub_ = pnh_.advertise(opts);
00107 }
00108
00109 ros::Time now = ros::Time::now();
00110
00111 if (latest_stamp_ > now) {
00112 ROS_WARN("Detected jump back in time. latest_stamp_ is overwritten.");
00113 latest_stamp_ = now;
00114 }
00115
00116 if (update_rate_ > 0.0 && (now - latest_stamp_).toSec() > 1.0 / update_rate_) {
00117 pub_.publish(msg);
00118 latest_stamp_ = now;
00119 }
00120 }
00121 }
00122
00123 #include <pluginlib/class_list_macros.h>
00124 typedef jsk_topic_tools::LightweightThrottle LightweightThrottle;
00125 PLUGINLIB_EXPORT_CLASS(LightweightThrottle, nodelet::Nodelet)