sync.cpp
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 #include <avt_vimba_camera/sync.h>
00034 #include <stdlib.h>
00035 
00036 namespace avt_vimba_camera {
00037 
00038 Sync::Sync(ros::NodeHandle nh, ros::NodeHandle nhp): nh_(nh), nhp_(nhp), init_(false), it_(nh)
00039 {
00040   // Read params
00041   nhp_.param("camera", camera_, string("/stereo_down"));
00042   nhp_.param("timer_period", timer_period_, 10.0);
00043   nhp_.param("max_unsync_time", max_unsync_time_, 5.0);
00044 }
00045 
00046 void Sync::run()
00047 {
00048   // Create the approximate sync subscriber
00049   image_transport::SubscriberFilter left_sub, right_sub;
00050   message_filters::Subscriber<sensor_msgs::CameraInfo> left_info_sub, right_info_sub;
00051 
00052   left_sub      .subscribe(it_, camera_+"/left/image_raw", 5);
00053   right_sub     .subscribe(it_, camera_+"/right/image_raw", 5);
00054   left_info_sub .subscribe(nh_, camera_+"/left/camera_info",  5);
00055   right_info_sub.subscribe(nh_, camera_+"/right/camera_info", 5);
00056 
00057   boost::shared_ptr<SyncType> sync_var;
00058   sync_var.reset(new SyncType(SyncPolicy(5), left_sub, right_sub, left_info_sub, right_info_sub) );
00059   sync_var->registerCallback(bind(&Sync::msgsCallback, this, _1, _2, _3, _4));
00060 
00061   // Sync timer
00062   sync_timer_ = nh_.createTimer(ros::Duration(timer_period_), &Sync::syncCallback, this);
00063 
00064   // Publish info
00065   pub_info_ = nhp_.advertise<std_msgs::String>("info", 1, true);
00066 
00067   ros::spin();
00068 }
00069 
00070 void Sync::msgsCallback(const sensor_msgs::ImageConstPtr& l_img_msg,
00071                         const sensor_msgs::ImageConstPtr& r_img_msg,
00072                         const sensor_msgs::CameraInfoConstPtr& l_info_msg,
00073                         const sensor_msgs::CameraInfoConstPtr& r_info_msg)
00074 {
00075   if (!init_)
00076     ROS_INFO("[SyncNode]: Initialized.");
00077   init_ = true;
00078 
00079   last_ros_sync_ = ros::Time::now().toSec();
00080 }
00081 
00082 void Sync::syncCallback(const ros::TimerEvent&)
00083 {
00084   if (!init_) return;
00085 
00086   double now = ros::Time::now().toSec();
00087 
00088   // Check desired frequency
00089   if (now - last_ros_sync_ > max_unsync_time_)
00090   {
00091     // No sync!
00092     ROS_WARN_STREAM("[SyncNode]: No sync during " << now - last_ros_sync_ << " sec.");
00093 
00094     // Publish info
00095     std_msgs::String msg;
00096     msg.data = "Reseting camera driver at ROSTIME: " +
00097                boost::lexical_cast<string>(now) + "s.";
00098     pub_info_.publish(msg);
00099   }
00100 }
00101 
00102 
00103 };


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Thu Jun 6 2019 18:23:39