Skip to content

Commit 80de336

Browse files
committed
AR Demo
1 parent 689230f commit 80de336

File tree

9 files changed

+147
-18
lines changed

9 files changed

+147
-18
lines changed

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CMakeLists.txt.user
2+
Examples/Monocular/mono_euroc
3+
Examples/Monocular/mono_kitti
4+
Examples/Monocular/mono_tum
5+
Examples/RGB-D/rgbd_tum
6+
Examples/ROS/ORB_SLAM2/CMakeLists.txt.user
7+
Examples/ROS/ORB_SLAM2/Mono
8+
Examples/ROS/ORB_SLAM2/MonoAR
9+
Examples/ROS/ORB_SLAM2/RGBD
10+
Examples/ROS/ORB_SLAM2/Stereo
11+
Examples/ROS/ORB_SLAM2/build/
12+
Examples/ROS/ORB_SLAM2/src/AR/
13+
Examples/Stereo/stereo_euroc
14+
Examples/Stereo/stereo_kitti
15+
KeyFrameTrajectory.txt
16+
Thirdparty/DBoW2/build/
17+
Thirdparty/DBoW2/lib/
18+
Thirdparty/g2o/build/
19+
Thirdparty/g2o/config.h
20+
Thirdparty/g2o/lib/
21+
Vocabulary/ORBvoc.txt
22+
build/
23+
build_ros.sh
24+
*~
25+
lib/
26+

Examples/ROS/ORB_SLAM2/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ target_link_libraries(Mono
5959
${LIBS}
6060
)
6161

62+
# Node for monocular camera (Augmented Reality Demo)
63+
rosbuild_add_executable(MonoAR
64+
src/AR/ros_mono_ar.cc
65+
src/AR/ViewerAR.h
66+
src/AR/ViewerAR.cc
67+
)
68+
69+
target_link_libraries(MonoAR
70+
${LIBS}
71+
)
72+
6273
# Node for stereo camera
6374
rosbuild_add_executable(Stereo
6475
src/ros_stereo.cc

include/Map.h

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Map
4545
void EraseMapPoint(MapPoint* pMP);
4646
void EraseKeyFrame(KeyFrame* pKF);
4747
void SetReferenceMapPoints(const std::vector<MapPoint*> &vpMPs);
48+
void InformNewBigChange();
49+
int GetLastBigChangeIdx();
4850

4951
std::vector<KeyFrame*> GetAllKeyFrames();
5052
std::vector<MapPoint*> GetAllMapPoints();
@@ -72,6 +74,9 @@ class Map
7274

7375
long unsigned int mnMaxKFid;
7476

77+
// Index related to a big change in the map (loop closure, global BA)
78+
int mnBigChangeIdx;
79+
7580
std::mutex mMutexMap;
7681
};
7782

include/System.h

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class System
8282
// This resumes local mapping thread and performs SLAM again.
8383
void DeactivateLocalizationMode();
8484

85+
// Returns true if there have been a big map change (loop closure, global BA)
86+
// since last call to this function
87+
bool MapChanged();
88+
8589
// Reset the system (clear map)
8690
void Reset();
8791

@@ -112,6 +116,12 @@ class System
112116
// SaveMap(const string &filename);
113117
// LoadMap(const string &filename);
114118

119+
// Information from most recent processed frame
120+
// You can call this right after TrackMonocular (or stereo or RGBD)
121+
int GetTrackingState();
122+
std::vector<MapPoint*> GetTrackedMapPoints();
123+
std::vector<cv::KeyPoint> GetTrackedKeyPointsUn();
124+
115125
private:
116126

117127
// Input sensor
@@ -158,6 +168,12 @@ class System
158168
std::mutex mMutexMode;
159169
bool mbActivateLocalizationMode;
160170
bool mbDeactivateLocalizationMode;
171+
172+
// Tracking state
173+
int mTrackingState;
174+
std::vector<MapPoint*> mTrackedMapPoints;
175+
std::vector<cv::KeyPoint> mTrackedKeyPointsUn;
176+
std::mutex mMutexState;
161177
};
162178

163179
}// namespace ORB_SLAM

src/LoopClosing.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ void LoopClosing::CorrectLoop()
566566
// Optimize graph
567567
Optimizer::OptimizeEssentialGraph(mpMap, mpMatchedKF, mpCurrentKF, NonCorrectedSim3, CorrectedSim3, LoopConnections, mbFixScale);
568568

569+
mpMap->InformNewBigChange();
570+
569571
// Add loop edge
570572
mpMatchedKF->AddLoopEdge(mpCurrentKF);
571573
mpCurrentKF->AddLoopEdge(mpMatchedKF);
@@ -579,8 +581,6 @@ void LoopClosing::CorrectLoop()
579581
// Loop closed. Release Local Mapping.
580582
mpLocalMapper->Release();
581583

582-
cout << "Loop Closed!" << endl;
583-
584584
mLastLoopKFid = mpCurrentKF->mnId;
585585
}
586586

@@ -734,7 +734,9 @@ void LoopClosing::RunGlobalBundleAdjustment(unsigned long nLoopKF)
734734

735735
pMP->SetWorldPos(Rwc*Xc+twc);
736736
}
737-
}
737+
}
738+
739+
mpMap->InformNewBigChange();
738740

739741
mpLocalMapper->Release();
740742

src/Map.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace ORB_SLAM2
2626
{
2727

28-
Map::Map():mnMaxKFid(0)
28+
Map::Map():mnMaxKFid(0),mnBigChangeIdx(0)
2929
{
3030
}
3131

@@ -67,6 +67,18 @@ void Map::SetReferenceMapPoints(const vector<MapPoint *> &vpMPs)
6767
mvpReferenceMapPoints = vpMPs;
6868
}
6969

70+
void Map::InformNewBigChange()
71+
{
72+
unique_lock<mutex> lock(mMutexMap);
73+
mnBigChangeIdx++;
74+
}
75+
76+
int Map::GetLastBigChangeIdx()
77+
{
78+
unique_lock<mutex> lock(mMutexMap);
79+
return mnBigChangeIdx;
80+
}
81+
7082
vector<KeyFrame*> Map::GetAllKeyFrames()
7183
{
7284
unique_lock<mutex> lock(mMutexMap);

src/System.cc

+60-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ORB_SLAM2
3030
{
3131

3232
System::System(const string &strVocFile, const string &strSettingsFile, const eSensor sensor,
33-
const bool bUseViewer):mSensor(sensor),mbReset(false),mbActivateLocalizationMode(false),
33+
const bool bUseViewer):mSensor(sensor), mpViewer(static_cast<Viewer*>(NULL)), mbReset(false),mbActivateLocalizationMode(false),
3434
mbDeactivateLocalizationMode(false)
3535
{
3636
// Output welcome message
@@ -95,11 +95,12 @@ System::System(const string &strVocFile, const string &strSettingsFile, const eS
9595
mptLoopClosing = new thread(&ORB_SLAM2::LoopClosing::Run, mpLoopCloser);
9696

9797
//Initialize the Viewer thread and launch
98-
mpViewer = new Viewer(this, mpFrameDrawer,mpMapDrawer,mpTracker,strSettingsFile);
9998
if(bUseViewer)
99+
{
100+
mpViewer = new Viewer(this, mpFrameDrawer,mpMapDrawer,mpTracker,strSettingsFile);
100101
mptViewer = new thread(&Viewer::Run, mpViewer);
101-
102-
mpTracker->SetViewer(mpViewer);
102+
mpTracker->SetViewer(mpViewer);
103+
}
103104

104105
//Set pointers between threads
105106
mpTracker->SetLocalMapper(mpLocalMapper);
@@ -154,7 +155,13 @@ cv::Mat System::TrackStereo(const cv::Mat &imLeft, const cv::Mat &imRight, const
154155
}
155156
}
156157

157-
return mpTracker->GrabImageStereo(imLeft,imRight,timestamp);
158+
cv::Mat Tcw = mpTracker->GrabImageStereo(imLeft,imRight,timestamp);
159+
160+
unique_lock<mutex> lock2(mMutexState);
161+
mTrackingState = mpTracker->mState;
162+
mTrackedMapPoints = mpTracker->mCurrentFrame.mvpMapPoints;
163+
mTrackedKeyPointsUn = mpTracker->mCurrentFrame.mvKeysUn;
164+
return Tcw;
158165
}
159166

160167
cv::Mat System::TrackRGBD(const cv::Mat &im, const cv::Mat &depthmap, const double &timestamp)
@@ -199,7 +206,13 @@ cv::Mat System::TrackRGBD(const cv::Mat &im, const cv::Mat &depthmap, const doub
199206
}
200207
}
201208

202-
return mpTracker->GrabImageRGBD(im,depthmap,timestamp);
209+
cv::Mat Tcw = mpTracker->GrabImageRGBD(im,depthmap,timestamp);
210+
211+
unique_lock<mutex> lock2(mMutexState);
212+
mTrackingState = mpTracker->mState;
213+
mTrackedMapPoints = mpTracker->mCurrentFrame.mvpMapPoints;
214+
mTrackedKeyPointsUn = mpTracker->mCurrentFrame.mvKeysUn;
215+
return Tcw;
203216
}
204217

205218
cv::Mat System::TrackMonocular(const cv::Mat &im, const double &timestamp)
@@ -244,7 +257,14 @@ cv::Mat System::TrackMonocular(const cv::Mat &im, const double &timestamp)
244257
}
245258
}
246259

247-
return mpTracker->GrabImageMonocular(im,timestamp);
260+
cv::Mat Tcw = mpTracker->GrabImageMonocular(im,timestamp);
261+
262+
unique_lock<mutex> lock2(mMutexState);
263+
mTrackingState = mpTracker->mState;
264+
mTrackedMapPoints = mpTracker->mCurrentFrame.mvpMapPoints;
265+
mTrackedKeyPointsUn = mpTracker->mCurrentFrame.mvKeysUn;
266+
267+
return Tcw;
248268
}
249269

250270
void System::ActivateLocalizationMode()
@@ -259,6 +279,19 @@ void System::DeactivateLocalizationMode()
259279
mbDeactivateLocalizationMode = true;
260280
}
261281

282+
bool System::MapChanged()
283+
{
284+
static int n=0;
285+
int curn = mpMap->GetLastBigChangeIdx();
286+
if(n<curn)
287+
{
288+
n=curn;
289+
return true;
290+
}
291+
else
292+
return false;
293+
}
294+
262295
void System::Reset()
263296
{
264297
unique_lock<mutex> lock(mMutexReset);
@@ -269,7 +302,8 @@ void System::Shutdown()
269302
{
270303
mpLocalMapper->RequestFinish();
271304
mpLoopCloser->RequestFinish();
272-
mpViewer->RequestFinish();
305+
if(mpViewer)
306+
mpViewer->RequestFinish();
273307

274308
// Wait until all thread have effectively stopped
275309
while(!mpLocalMapper->isFinished() || !mpLoopCloser->isFinished() ||
@@ -433,4 +467,22 @@ void System::SaveTrajectoryKITTI(const string &filename)
433467
cout << endl << "trajectory saved!" << endl;
434468
}
435469

470+
int System::GetTrackingState()
471+
{
472+
unique_lock<mutex> lock(mMutexState);
473+
return mTrackingState;
474+
}
475+
476+
vector<MapPoint*> System::GetTrackedMapPoints()
477+
{
478+
unique_lock<mutex> lock(mMutexState);
479+
return mTrackedMapPoints;
480+
}
481+
482+
vector<cv::KeyPoint> System::GetTrackedKeyPointsUn()
483+
{
484+
unique_lock<mutex> lock(mMutexState);
485+
return mTrackedKeyPointsUn;
486+
}
487+
436488
} //namespace ORB_SLAM

src/Tracking.cc

+9-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace ORB_SLAM2
4545

4646
Tracking::Tracking(System *pSys, ORBVocabulary* pVoc, FrameDrawer *pFrameDrawer, MapDrawer *pMapDrawer, Map *pMap, KeyFrameDatabase* pKFDB, const string &strSettingPath, const int sensor):
4747
mState(NO_IMAGES_YET), mSensor(sensor), mbOnlyTracking(false), mbVO(false), mpORBVocabulary(pVoc),
48-
mpKeyFrameDB(pKFDB), mpInitializer(static_cast<Initializer*>(NULL)), mpSystem(pSys),
48+
mpKeyFrameDB(pKFDB), mpInitializer(static_cast<Initializer*>(NULL)), mpSystem(pSys), mpViewer(NULL),
4949
mpFrameDrawer(pFrameDrawer), mpMapDrawer(pMapDrawer), mpMap(pMap), mnLastRelocFrameId(0)
5050
{
5151
// Load camera parameters from settings file
@@ -1503,11 +1503,14 @@ bool Tracking::Relocalization()
15031503

15041504
void Tracking::Reset()
15051505
{
1506-
mpViewer->RequestStop();
15071506

15081507
cout << "System Reseting" << endl;
1509-
while(!mpViewer->isStopped())
1510-
usleep(3000);
1508+
if(mpViewer)
1509+
{
1510+
mpViewer->RequestStop();
1511+
while(!mpViewer->isStopped())
1512+
usleep(3000);
1513+
}
15111514

15121515
// Reset Local Mapping
15131516
cout << "Reseting Local Mapper...";
@@ -1542,7 +1545,8 @@ void Tracking::Reset()
15421545
mlFrameTimes.clear();
15431546
mlbLost.clear();
15441547

1545-
mpViewer->Release();
1548+
if(mpViewer)
1549+
mpViewer->Release();
15461550
}
15471551

15481552
void Tracking::ChangeCalibration(const string &strSettingPath)

src/Viewer.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace ORB_SLAM2
2828

2929
Viewer::Viewer(System* pSystem, FrameDrawer *pFrameDrawer, MapDrawer *pMapDrawer, Tracking *pTracking, const string &strSettingPath):
3030
mpSystem(pSystem), mpFrameDrawer(pFrameDrawer),mpMapDrawer(pMapDrawer), mpTracker(pTracking),
31-
mbFinishRequested(false), mbFinished(true), mbStopped(false), mbStopRequested(false)
31+
mbFinishRequested(false), mbFinished(true), mbStopped(true), mbStopRequested(false)
3232
{
3333
cv::FileStorage fSettings(strSettingPath, cv::FileStorage::READ);
3434

@@ -54,6 +54,7 @@ Viewer::Viewer(System* pSystem, FrameDrawer *pFrameDrawer, MapDrawer *pMapDrawer
5454
void Viewer::Run()
5555
{
5656
mbFinished = false;
57+
mbStopped = false;
5758

5859
pangolin::CreateWindowAndBind("ORB-SLAM2: Map Viewer",1024,768);
5960

0 commit comments

Comments
 (0)