ros_rgbd.cc
Go to the documentation of this file.
1 
22 #include<iostream>
23 #include<algorithm>
24 #include<fstream>
25 #include<chrono>
26 
27 #include <ros/ros.h>
28 #include <cv_bridge/cv_bridge.h>
32 #include <sensor_msgs/PointCloud2.h>
33 
34 #include <ros/spinner.h>
35 #include <sensor_msgs/CameraInfo.h>
36 #include <sensor_msgs/Image.h>
38 
39 
40 #include"System.h"
41 #include "geometry_msgs/PoseStamped.h"
42 #include "geometry_msgs/PoseArray.h"
43 #include "sensor_msgs/PointCloud2.h"
44 #include <Converter.h>
45 #include <image_transport/image_transport.h>
46 #include <image_transport/subscriber_filter.h>
49 #include <pcl/point_cloud.h>
50 #include <pcl/point_types.h>
51 #include <pcl/io/pcd_io.h>
52 #include <pcl_conversions/pcl_conversions.h>
53 #include<pcl/filters/voxel_grid.h>
54 #include<pcl/filters/passthrough.h>
55 
56 #include<opencv2/core/core.hpp>
57 #include "MapPoint.h"
58 #include "pointCloudPublisher.h"
59 #include <stdio.h>
60 #include <unistd.h>
61 
62 
63 using namespace std;
64 
65 
67 public:
69  SLAM(_SLAM){}
70 
71  void GrabImage(const sensor_msgs::ImageConstPtr& msgRGB,const sensor_msgs::ImageConstPtr& msgD);
72 
74 };
75 
76 
77 int main(int argc, char **argv)
78 {
79  ros::init(argc, argv, "RGBD");
80  ros::start();
81 
82  if(argc != 3)
83  {
84  cerr << endl << "Usage: rosrun ORB_SLAM2 RGBD path_to_vocabulary path_to_settings" << endl;
85  ros::shutdown();
86  return 1;
87  }
88 
89  //Check settings file
90  cout << "配置文件路径:"<<argv[2] << endl;
91  cv::FileStorage fsSettings(argv[2], cv::FileStorage::READ);
92  if (!fsSettings.isOpened())
93  {
94  cerr << "Failed to open settings file at: " << argv[2] << endl;
95  exit(-1);
96  }
97 
98  // for point cloud resolution
99  string rgbTopic = fsSettings["Image.RGB"];
100  string depthTopic = fsSettings["Image.Depth"];
101 
102  //获取地图保存路径
103  string mapsavepath = fsSettings["Map.SavePath"];
104 
105 
106  // Create SLAM system. It initializes all system threads and gets ready to process frames.
107  ORB_SLAM2::System SLAM(argv[1],argv[2],ORB_SLAM2::System::RGBD,true);
108 
109 
110 
111  if(access(mapsavepath.c_str(),0) == 0)
112  {
113  SLAM.LoadMap(mapsavepath);
114  }
115  ros::NodeHandle nh;
116 
117  PointCloudPublisher publisher(&nh,&SLAM);
118  ImageGrabber igb(SLAM);
119 
120  message_filters::Subscriber<sensor_msgs::Image> rgb_sub(nh, rgbTopic, 1);
121  message_filters::Subscriber<sensor_msgs::Image> depth_sub(nh, depthTopic, 1);
123  message_filters::Synchronizer<sync_pol> sync(sync_pol(10), rgb_sub,depth_sub);
124  sync.registerCallback(boost::bind(&ImageGrabber::GrabImage,&igb,_1,_2));
125  ros::spin();
126 
127 
128  // Stop all threads
129  SLAM.Shutdown();
130 
131  // Save camera trajectory
132  cout<<"开始进入保存地图"<<endl;
133  SLAM.SaveMap(mapsavepath);
134  SLAM.SaveKeyFrameTrajectoryTUM("/home/inspur/KeyFrameTrajectory.txt");
135 
136  ros::shutdown();
137 
138  return 0;
139 }
140 
141 void ImageGrabber::GrabImage(const sensor_msgs::ImageConstPtr& msgRGB,const sensor_msgs::ImageConstPtr& msgD)
142 {
143  // Copy the ros image message to cv::Mat.
144  cv_bridge::CvImageConstPtr cv_ptrRGB;
145  try
146  {
147  cv_ptrRGB = cv_bridge::toCvShare(msgRGB);
148  }
149  catch (cv_bridge::Exception& e)
150  {
151  ROS_ERROR("cv_bridge exception: %s", e.what());
152  return;
153  }
154 
155  cv_bridge::CvImageConstPtr cv_ptrD;
156  try
157  {
158  cv_ptrD = cv_bridge::toCvShare(msgD);
159  }
160  catch (cv_bridge::Exception& e)
161  {
162  ROS_ERROR("cv_bridge exception: %s", e.what());
163  return;
164  }
165 
166  SLAM.TrackRGBD(cv_ptrRGB->image,cv_ptrD->image,cv_ptrRGB->header.stamp.toSec());
167 
168 }
ROSCPP_DECL void start()
ImageGrabber(ORB_SLAM2::System &_SLAM)
Definition: ros_rgbd.cc:68
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL void spin(Spinner &spinner)
ORB_SLAM2::System & SLAM
Definition: ros_rgbd.cc:73
Connection registerCallback(C &callback)
int main(int argc, char **argv)
Definition: ros_rgbd.cc:77
void GrabImage(const sensor_msgs::ImageConstPtr &msgRGB, const sensor_msgs::ImageConstPtr &msgD)
Definition: ros_rgbd.cc:141
ROSCPP_DECL void shutdown()
void SaveKeyFrameTrajectoryTUM(const string &filename)
#define ROS_ERROR(...)


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47