stereo_visual_odometry_alg_node.cpp
Go to the documentation of this file.
00001 #include "stereo_visual_odometry_alg_node.h"
00002 
00003 using namespace std;
00004 
00005 StereoVisualOdometryAlgNode::StereoVisualOdometryAlgNode(void) :
00006   algorithm_base::IriBaseAlgorithm<StereoVisualOdometryAlgorithm>()
00007 {
00008   //init class attributes if necessary
00009   this->loop_rate_ = 100;//in [Hz]
00010 
00011   // [init publishers]
00012   this->pose_with_covariance_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PoseWithCovarianceStamped>("pose_with_covariance", 100);
00013   
00014   // [init subscribers]
00015   this->right_image_subscriber_ = this->public_node_handle_.subscribe("right_image", 100, &StereoVisualOdometryAlgNode::right_image_callback, this);
00016   this->left_image_subscriber_ = this->public_node_handle_.subscribe("left_image", 100, &StereoVisualOdometryAlgNode::left_image_callback, this);
00017   
00018   // [init services]
00019   
00020   // [init clients]
00021   
00022   // [init action servers]
00023   
00024   // [init action clients]
00025   
00026   count_left_images=0;
00027   count_right_images=0;
00028   H=cv::Mat::eye(4,4,CV_64F);
00029   Hcov=cv::Mat::zeros(6,6,CV_64F);
00030   point=cv::Mat::zeros(4,1,CV_64F);
00031   point.at<double>(3,0)=1.0;
00032 }
00033 
00034 StereoVisualOdometryAlgNode::~StereoVisualOdometryAlgNode(void)
00035 {
00036   // [free dynamic memory]
00037 }
00038 
00039 void StereoVisualOdometryAlgNode::mainNodeThread(void)
00040 {
00041   
00042   if ((image1.rows!=0) & (image2.rows!=0) & (image3.rows!=0) & (image4.rows!=0))
00043   { 
00044     //ros::Time begin = ros::Time::now();
00045     //ROS_INFO("Temps calcul: %f %u",begin.toSec(),begin.toNSec());
00046     
00047     alg_.compute_H_Hcov(image1,image2,image3,image4,H,Hcov,point);
00048     
00049     //ros::Time end = ros::Time::now();
00050     //double total_s = end.toSec()-begin.toSec();
00051     
00052     //ROS_INFO("Temps calcul: %f %u",end.toSec(),end.toNSec());
00053     //ROS_INFO("Temps calcul: %f",total_s);
00054     
00055     image1=image3;
00056     image2=image4;
00057     
00058     image3=cv::Mat::zeros(0,0,CV_64F);
00059     image4=cv::Mat::zeros(0,0,CV_64F);
00060     
00061     double y;
00062     double p;
00063     double r;
00064     
00065     alg_.extract_ypr(H,y,p,r);
00066     
00067     // [fill msg structures]
00068     this->PoseWithCovarianceStamped_msg_.header.stamp = ros::Time::now();
00069     this->PoseWithCovarianceStamped_msg_.header.frame_id = "/teo/stereo/left_camera";
00070     this->PoseWithCovarianceStamped_msg_.pose.pose.position.x=point.at<double>(2,0)/1000.0;
00071     this->PoseWithCovarianceStamped_msg_.pose.pose.position.y=-point.at<double>(0,0)/1000.0;
00072     this->PoseWithCovarianceStamped_msg_.pose.pose.position.z=point.at<double>(1,0)/1000.0;
00073     this->PoseWithCovarianceStamped_msg_.pose.pose.orientation = tf::createQuaternionMsgFromRollPitchYaw(r,y,-p);
00074     //(r,p,y)
00075     
00076     double Hcov_array[36]={Hcov.at<double>(0,0), Hcov.at<double>(0,1), Hcov.at<double>(0,2), Hcov.at<double>(0,3), Hcov.at<double>(0,4), Hcov.at<double>(0,5), 
00077                            Hcov.at<double>(1,0), Hcov.at<double>(1,1), Hcov.at<double>(1,2), Hcov.at<double>(1,3), Hcov.at<double>(1,4), Hcov.at<double>(1,5),
00078                            Hcov.at<double>(2,0), Hcov.at<double>(2,1), Hcov.at<double>(2,2), Hcov.at<double>(2,3), Hcov.at<double>(2,4), Hcov.at<double>(2,5), 
00079                            Hcov.at<double>(3,0), Hcov.at<double>(3,1), Hcov.at<double>(3,2), Hcov.at<double>(3,3), Hcov.at<double>(3,4), Hcov.at<double>(3,5),
00080                            Hcov.at<double>(4,0), Hcov.at<double>(4,1), Hcov.at<double>(4,2), Hcov.at<double>(4,3), Hcov.at<double>(4,4), Hcov.at<double>(4,5), 
00081                            Hcov.at<double>(5,0), Hcov.at<double>(5,1), Hcov.at<double>(5,2), Hcov.at<double>(5,3), Hcov.at<double>(5,4), Hcov.at<double>(5,5)};  
00082     for (int i=0;i<36;i++)
00083     {
00084       this->PoseWithCovarianceStamped_msg_.pose.covariance[i]=Hcov_array[i];
00085     }
00086     
00087     // [publish messages]
00088     this->pose_with_covariance_publisher_.publish(this->PoseWithCovarianceStamped_msg_);
00089   }
00090   
00091   
00092   // [fill srv structure and make request to the server]
00093   
00094   // [fill action structure and make request to the action server]
00095   
00096 }
00097 
00098 /*  [subscriber callbacks] */
00099 void StereoVisualOdometryAlgNode::right_image_callback(const sensor_msgs::Image::ConstPtr& msg) 
00100 { 
00101   ROS_INFO("StereoVisualOdometryAlgNode::right_image_callback: New Message Received"); 
00102   count_right_images=count_right_images+1;
00103   cout<<"number of right images received: "<<count_right_images<<endl;
00104   //use appropiate mutex to shared variables if necessary 
00105   //this->alg_.lock(); 
00106   //this->right_image_mutex_.enter();
00107   
00108   cv_bridge::CvImagePtr img;
00109   try
00110   {
00111     img = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00112   }
00113   catch (cv_bridge::Exception& e)
00114   {
00115     ROS_ERROR("cv_bridge exception: %s", e.what());
00116     return;
00117   }
00118   
00119   if (count_right_images==1)
00120   {
00121     image2=img->image;
00122   }
00123   
00124   else
00125   {
00126     image4=img->image;
00127   }
00128     
00129   //alg_.show_image_alg_test(image2);
00130   
00131   //ROS_INFO("imatge data-size %d",msg->data.size());
00132   
00133   //cout << msg->data <<endl; 
00134 
00135   //unlock previously blocked shared variables 
00136   //this->alg_.unlock(); 
00137   //this->right_image_mutex_.exit(); 
00138 }
00139 
00140 void StereoVisualOdometryAlgNode::left_image_callback(const sensor_msgs::Image::ConstPtr& msg) 
00141 { 
00142   ROS_INFO("StereoVisualOdometryAlgNode::left_image_callback: New Message Received");
00143   count_left_images=count_left_images+1;
00144   cout<<"number of left images received: "<<count_left_images<<endl;
00145 
00146   //use appropiate mutex to shared variables if necessary 
00147   //this->alg_.lock(); 
00148   //this->left_image_mutex_.enter();
00149   
00150   cv_bridge::CvImagePtr img;
00151   try
00152   {
00153     img = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00154   }
00155   catch (cv_bridge::Exception& e)
00156   {
00157     ROS_ERROR("cv_bridge exception: %s", e.what());
00158     return;
00159   }
00160   
00161 
00162   if (count_left_images==1)
00163   {
00164     image1=img->image;
00165   }
00166   
00167   else
00168   {
00169     image3=img->image;
00170   }
00171   
00172   //alg_.show_image_alg_test(image1);
00173   //imshow("left image 2", image2);
00174   //sensor_msgs::Image image2;
00175   //image2 = *msg;
00176 
00177   //std::cout << msg->data << std::endl; 
00178 
00179   //unlock previously blocked shared variables 
00180   //this->alg_.unlock(); 
00181   //this->left_image_mutex_.exit(); 
00182 }
00183 
00184 /*  [service callbacks] */
00185 
00186 /*  [action callbacks] */
00187 
00188 /*  [action requests] */
00189 
00190 void StereoVisualOdometryAlgNode::node_config_update(Config &config, uint32_t level)
00191 {
00192   this->alg_.lock();
00193 
00194   this->alg_.unlock();
00195 }
00196 
00197 void StereoVisualOdometryAlgNode::addNodeDiagnostics(void)
00198 {
00199 }
00200 
00201 /* main function */
00202 int main(int argc,char *argv[])
00203 {
00204   return algorithm_base::main<StereoVisualOdometryAlgNode>(argc, argv, "stereo_visual_odometry_alg_node");
00205 }


iri_stereo_visual_odometry
Author(s): gmoreno
autogenerated on Fri Dec 6 2013 23:59:48