00001 /* 00002 * synchronizedcamerathrottler.cc 00003 * Mac Mason <mmason@willowgarage.com> 00004 * 00005 * Like topic_tools/throttler, but uses a camera subscriber to smartly 00006 * throttle pairs of messages. 00007 */ 00008 00009 #include <boost/thread.hpp> 00010 #include "ros/ros.h" 00011 #include "image_transport/image_transport.h" 00012 00013 // The fancy subscriber handles synchronization for us. 00014 image_transport::CameraSubscriber camera_sub; 00015 image_transport::CameraPublisher camera_pub; 00016 00017 // (Not) off to the races. 00018 boost::mutex mutex; 00019 00020 sensor_msgs::Image current_image; 00021 sensor_msgs::CameraInfo current_info; 00022 00023 void camera_cb(const sensor_msgs::ImageConstPtr& img, 00024 const sensor_msgs::CameraInfoConstPtr& info) 00025 { 00026 boost::mutex::scoped_lock lock(mutex); 00027 current_image = *img; 00028 current_info = *info; 00029 } 00030 00031 void timer_cb(const ros::TimerEvent& e) 00032 { 00033 boost::mutex::scoped_lock lock(mutex); 00034 camera_pub.publish(current_image, current_info); 00035 } 00036 00037 int main(int argc, char** argv) 00038 { 00039 ros::init(argc, argv, "synchronizedcamerathrottler"); 00040 if (argc != 2) 00041 { 00042 ROS_FATAL("Need to provide a frequency!"); 00043 return 1; 00044 } 00045 double frequency = atof(argv[1]); 00046 00047 ros::NodeHandle node; 00048 image_transport::ImageTransport transport(node); 00049 camera_sub = transport.subscribeCamera("cam_input", 100, camera_cb); 00050 camera_pub = transport.advertiseCamera("cam_output", 100); 00051 00052 ros::Timer timer = 00053 node.createTimer(ros::Duration(1.0 / frequency), timer_cb); 00054 00055 ros::spin(); 00056 return 0; 00057 }