forked from raulmur/ORB_SLAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
190 lines (148 loc) · 6 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* This file is part of ORB-SLAM.
*
* Copyright (C) 2014 Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
* For more information see <http://webdiis.unizar.es/~raulmur/orbslam/>
*
* ORB-SLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ORB-SLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ORB-SLAM. If not, see <http://www.gnu.org/licenses/>.
*/
#include<iostream>
#include<fstream>
#include<ros/ros.h>
#include<ros/package.h>
#include<boost/thread.hpp>
#include<opencv2/core/core.hpp>
#include "Tracking.h"
#include "FramePublisher.h"
#include "Map.h"
#include "MapPublisher.h"
#include "LocalMapping.h"
#include "LoopClosing.h"
#include "KeyFrameDatabase.h"
#include "ORBVocabulary.h"
#include "Converter.h"
using namespace std;
int main(int argc, char **argv)
{
ros::init(argc, argv, "ORB_SLAM");
ros::start();
cout << endl << "ORB-SLAM Copyright (C) 2014 Raul Mur-Artal" << endl <<
"This program comes with ABSOLUTELY NO WARRANTY;" << endl <<
"This is free software, and you are welcome to redistribute it" << endl <<
"under certain conditions. See LICENSE.txt." << endl;
if(argc != 3)
{
cerr << endl << "Usage: rosrun ORB_SLAM ORB_SLAM path_to_vocabulary path_to_settings (absolute or relative to package directory)" << endl;
ros::shutdown();
return 1;
}
// Load Settings and Check
string strSettingsFile = ros::package::getPath("ORB_SLAM")+"/"+argv[2];
cv::FileStorage fsSettings(strSettingsFile.c_str(), cv::FileStorage::READ);
if(!fsSettings.isOpened())
{
ROS_ERROR("Wrong path to settings. Path must be absolut or relative to ORB_SLAM package directory.");
ros::shutdown();
return 1;
}
//Create Frame Publisher for image_view
ORB_SLAM::FramePublisher FramePub;
//Load ORB Vocabulary
/* Old version to load vocabulary using cv::FileStorage
string strVocFile = ros::package::getPath("ORB_SLAM")+"/"+argv[1];
cout << endl << "Loading ORB Vocabulary. This could take a while." << endl;
cv::FileStorage fsVoc(strVocFile.c_str(), cv::FileStorage::READ);
if(!fsVoc.isOpened())
{
cerr << endl << "Wrong path to vocabulary. Path must be absolut or relative to ORB_SLAM package directory." << endl;
ros::shutdown();
return 1;
}
ORB_SLAM::ORBVocabulary Vocabulary;
Vocabulary.load(fsVoc);
*/
// New version to load vocabulary from text file "Data/ORBvoc.txt".
// If you have an own .yml vocabulary, use the function
// saveToTextFile in Thirdparty/DBoW2/DBoW2/TemplatedVocabulary.h
string strVocFile = ros::package::getPath("ORB_SLAM")+"/"+argv[1];
cout << endl << "Loading ORB Vocabulary. This could take a while." << endl;
ORB_SLAM::ORBVocabulary Vocabulary;
bool bVocLoad = Vocabulary.loadFromTextFile(strVocFile);
if(!bVocLoad)
{
cerr << "Wrong path to vocabulary. Path must be absolut or relative to ORB_SLAM package directory." << endl;
cerr << "Falied to open at: " << strVocFile << endl;
ros::shutdown();
return 1;
}
cout << "Vocabulary loaded!" << endl << endl;
//Create KeyFrame Database
ORB_SLAM::KeyFrameDatabase Database(Vocabulary);
//Create the map
ORB_SLAM::Map World;
FramePub.SetMap(&World);
//Create Map Publisher for Rviz
ORB_SLAM::MapPublisher MapPub(&World);
//Initialize the Tracking Thread and launch
ORB_SLAM::Tracking Tracker(&Vocabulary, &FramePub, &MapPub, &World, strSettingsFile);
boost::thread trackingThread(&ORB_SLAM::Tracking::Run,&Tracker);
Tracker.SetKeyFrameDatabase(&Database);
//Initialize the Local Mapping Thread and launch
ORB_SLAM::LocalMapping LocalMapper(&World);
boost::thread localMappingThread(&ORB_SLAM::LocalMapping::Run,&LocalMapper);
//Initialize the Loop Closing Thread and launch
ORB_SLAM::LoopClosing LoopCloser(&World, &Database, &Vocabulary);
boost::thread loopClosingThread(&ORB_SLAM::LoopClosing::Run, &LoopCloser);
//Set pointers between threads
Tracker.SetLocalMapper(&LocalMapper);
Tracker.SetLoopClosing(&LoopCloser);
LocalMapper.SetTracker(&Tracker);
LocalMapper.SetLoopCloser(&LoopCloser);
LoopCloser.SetTracker(&Tracker);
LoopCloser.SetLocalMapper(&LocalMapper);
//This "main" thread will show the current processed frame and publish the map
float fps = fsSettings["Camera.fps"];
if(fps==0)
fps=30;
ros::Rate r(fps);
while (ros::ok())
{
FramePub.Refresh();
MapPub.Refresh();
Tracker.CheckResetByPublishers();
r.sleep();
}
// Save keyframe poses at the end of the execution
ofstream f;
vector<ORB_SLAM::KeyFrame*> vpKFs = World.GetAllKeyFrames();
sort(vpKFs.begin(),vpKFs.end(),ORB_SLAM::KeyFrame::lId);
cout << endl << "Saving Keyframe Trajectory to KeyFrameTrajectory.txt" << endl;
string strFile = ros::package::getPath("ORB_SLAM")+"/"+"KeyFrameTrajectory.txt";
f.open(strFile.c_str());
f << fixed;
for(size_t i=0; i<vpKFs.size(); i++)
{
ORB_SLAM::KeyFrame* pKF = vpKFs[i];
if(pKF->isBad())
continue;
cv::Mat R = pKF->GetRotation().t();
vector<float> q = ORB_SLAM::Converter::toQuaternion(R);
cv::Mat t = pKF->GetCameraCenter();
f << setprecision(6) << pKF->mTimeStamp << setprecision(7) << " " << t.at<float>(0) << " " << t.at<float>(1) << " " << t.at<float>(2)
<< " " << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << endl;
}
f.close();
ros::shutdown();
return 0;
}