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
00009 this->loop_rate_ = 100;
00010
00011
00012 this->pose_with_covariance_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PoseWithCovarianceStamped>("pose_with_covariance", 100);
00013
00014
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
00019
00020
00021
00022
00023
00024
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
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
00045
00046
00047 alg_.compute_H_Hcov(image1,image2,image3,image4,H,Hcov,point);
00048
00049
00050
00051
00052
00053
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
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
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
00088 this->pose_with_covariance_publisher_.publish(this->PoseWithCovarianceStamped_msg_);
00089 }
00090
00091
00092
00093
00094
00095
00096 }
00097
00098
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
00105
00106
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
00130
00131
00132
00133
00134
00135
00136
00137
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
00147
00148
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
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 }
00183
00184
00185
00186
00187
00188
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
00202 int main(int argc,char *argv[])
00203 {
00204 return algorithm_base::main<StereoVisualOdometryAlgNode>(argc, argv, "stereo_visual_odometry_alg_node");
00205 }