-
Notifications
You must be signed in to change notification settings - Fork 374
/
system.cc
588 lines (485 loc) · 20.7 KB
/
system.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include "stella_vslam/system.h"
#include "stella_vslam/config.h"
#include "stella_vslam/tracking_module.h"
#include "stella_vslam/mapping_module.h"
#include "stella_vslam/global_optimization_module.h"
#include "stella_vslam/camera/camera_factory.h"
#include "stella_vslam/data/camera_database.h"
#include "stella_vslam/data/common.h"
#include "stella_vslam/data/frame_observation.h"
#include "stella_vslam/data/orb_params_database.h"
#include "stella_vslam/data/map_database.h"
#include "stella_vslam/data/bow_database.h"
#include "stella_vslam/data/bow_vocabulary.h"
#include "stella_vslam/data/marker2d.h"
#include "stella_vslam/marker_detector/aruco.h"
#include "stella_vslam/match/stereo.h"
#include "stella_vslam/feature/orb_extractor.h"
#include "stella_vslam/io/trajectory_io.h"
#include "stella_vslam/io/map_database_io_factory.h"
#include "stella_vslam/publish/map_publisher.h"
#include "stella_vslam/publish/frame_publisher.h"
#include "stella_vslam/util/converter.h"
#include "stella_vslam/util/image_converter.h"
#include "stella_vslam/util/yaml.h"
#include <thread>
#include <spdlog/spdlog.h>
namespace stella_vslam {
system::system(const std::shared_ptr<config>& cfg, const std::string& vocab_file_path)
: cfg_(cfg) {
spdlog::debug("CONSTRUCT: system");
print_info();
// load ORB vocabulary
spdlog::info("loading ORB vocabulary: {}", vocab_file_path);
bow_vocab_ = data::bow_vocabulary_util::load(vocab_file_path);
const auto system_params = util::yaml_optional_ref(cfg->yaml_node_, "System");
camera_ = camera::camera_factory::create(util::yaml_optional_ref(cfg->yaml_node_, "Camera"));
orb_params_ = new feature::orb_params(util::yaml_optional_ref(cfg->yaml_node_, "Feature"));
spdlog::info("load orb_params \"{}\"", orb_params_->name_);
// database
cam_db_ = new data::camera_database();
cam_db_->add_camera(camera_);
map_db_ = new data::map_database(system_params["min_num_shared_lms"].as<unsigned int>(15));
bow_db_ = new data::bow_database(bow_vocab_);
orb_params_db_ = new data::orb_params_database();
orb_params_db_->add_orb_params(orb_params_);
// frame and map publisher
frame_publisher_ = std::shared_ptr<publish::frame_publisher>(new publish::frame_publisher(cfg_, map_db_));
map_publisher_ = std::shared_ptr<publish::map_publisher>(new publish::map_publisher(cfg_, map_db_));
// map I/O
auto map_format = system_params["map_format"].as<std::string>("msgpack");
map_database_io_ = io::map_database_io_factory::create(map_format);
// tracking module
tracker_ = new tracking_module(cfg_, camera_, map_db_, bow_vocab_, bow_db_);
// mapping module
mapper_ = new mapping_module(cfg_->yaml_node_["Mapping"], map_db_, bow_db_, bow_vocab_);
// global optimization module
global_optimizer_ = new global_optimization_module(map_db_, bow_db_, bow_vocab_, cfg_->yaml_node_, camera_->setup_type_ != camera::setup_type_t::Monocular);
// preprocessing modules
const auto preprocessing_params = util::yaml_optional_ref(cfg->yaml_node_, "Preprocessing");
if (camera_->setup_type_ == camera::setup_type_t::RGBD) {
depthmap_factor_ = preprocessing_params["depthmap_factor"].as<double>(depthmap_factor_);
if (depthmap_factor_ < 0.) {
throw std::runtime_error("depthmap_factor must be greater than 0");
}
}
auto mask_rectangles = util::get_rectangles(preprocessing_params["mask_rectangles"]);
const auto min_size = preprocessing_params["min_size"].as<unsigned int>(800);
extractor_left_ = new feature::orb_extractor(orb_params_, min_size, mask_rectangles);
if (camera_->setup_type_ == camera::setup_type_t::Stereo) {
extractor_right_ = new feature::orb_extractor(orb_params_, min_size, mask_rectangles);
}
if (cfg->marker_model_) {
if (marker_detector::aruco::is_valid()) {
spdlog::debug("marker detection: enabled");
marker_detector_ = new marker_detector::aruco(camera_, cfg->marker_model_);
}
else {
spdlog::warn("Valid marker_detector is not installed");
}
}
// connect modules each other
tracker_->set_mapping_module(mapper_);
tracker_->set_global_optimization_module(global_optimizer_);
mapper_->set_tracking_module(tracker_);
mapper_->set_global_optimization_module(global_optimizer_);
global_optimizer_->set_tracking_module(tracker_);
global_optimizer_->set_mapping_module(mapper_);
}
system::~system() {
global_optimization_thread_.reset(nullptr);
delete global_optimizer_;
global_optimizer_ = nullptr;
mapping_thread_.reset(nullptr);
delete mapper_;
mapper_ = nullptr;
delete tracker_;
tracker_ = nullptr;
delete bow_db_;
bow_db_ = nullptr;
delete map_db_;
map_db_ = nullptr;
delete cam_db_;
cam_db_ = nullptr;
delete bow_vocab_;
bow_vocab_ = nullptr;
delete extractor_left_;
extractor_left_ = nullptr;
delete extractor_right_;
extractor_right_ = nullptr;
delete marker_detector_;
marker_detector_ = nullptr;
delete orb_params_db_;
orb_params_db_ = nullptr;
spdlog::debug("DESTRUCT: system");
}
void system::print_info() {
std::ostringstream message_stream;
message_stream << std::endl;
message_stream << "original version of OpenVSLAM," << std::endl;
message_stream << "Copyright (C) 2019," << std::endl;
message_stream << "National Institute of Advanced Industrial Science and Technology (AIST)" << std::endl;
message_stream << "All rights reserved." << std::endl;
message_stream << "stella_vslam (the changes after forking from OpenVSLAM)," << std::endl;
message_stream << "Copyright (C) 2022, stella-cv, All rights reserved." << std::endl;
message_stream << std::endl;
message_stream << "This is free software," << std::endl;
message_stream << "and you are welcome to redistribute it under certain conditions." << std::endl;
message_stream << "See the LICENSE file." << std::endl;
message_stream << std::endl;
// show configuration
message_stream << *cfg_ << std::endl;
spdlog::info(message_stream.str());
}
void system::startup(const bool need_initialize) {
spdlog::info("startup SLAM system");
system_is_running_ = true;
if (!need_initialize) {
tracker_->tracking_state_ = tracker_state_t::Lost;
}
mapping_thread_ = std::unique_ptr<std::thread>(new std::thread(&stella_vslam::mapping_module::run, mapper_));
global_optimization_thread_ = std::unique_ptr<std::thread>(new std::thread(&stella_vslam::global_optimization_module::run, global_optimizer_));
}
void system::shutdown() {
// terminate the other threads
auto future_mapper_terminate = mapper_->async_terminate();
auto future_global_optimizer_terminate = global_optimizer_->async_terminate();
future_mapper_terminate.get();
future_global_optimizer_terminate.get();
// wait until the threads stop
mapping_thread_->join();
global_optimization_thread_->join();
spdlog::info("shutdown SLAM system");
system_is_running_ = false;
}
void system::save_frame_trajectory(const std::string& path, const std::string& format) const {
pause_other_threads();
io::trajectory_io trajectory_io(map_db_);
trajectory_io.save_frame_trajectory(path, format);
resume_other_threads();
}
void system::save_keyframe_trajectory(const std::string& path, const std::string& format) const {
pause_other_threads();
io::trajectory_io trajectory_io(map_db_);
trajectory_io.save_keyframe_trajectory(path, format);
resume_other_threads();
}
bool system::load_map_database(const std::string& path) const {
pause_other_threads();
spdlog::debug("load_map_database: {}", path);
bool ok = map_database_io_->load(path, cam_db_, orb_params_db_, map_db_, bow_db_, bow_vocab_);
resume_other_threads();
return ok;
}
bool system::save_map_database(const std::string& path) const {
pause_other_threads();
spdlog::debug("save_map_database: {}", path);
bool ok = map_database_io_->save(path, cam_db_, orb_params_db_, map_db_);
resume_other_threads();
return ok;
}
const std::shared_ptr<publish::map_publisher> system::get_map_publisher() const {
return map_publisher_;
}
const std::shared_ptr<publish::frame_publisher> system::get_frame_publisher() const {
return frame_publisher_;
}
void system::enable_mapping_module() {
std::lock_guard<std::mutex> lock(mtx_mapping_);
if (!system_is_running_) {
spdlog::critical("please call system::enable_mapping_module() after system::startup()");
}
// resume the mapping module
mapper_->resume();
}
void system::disable_mapping_module() {
std::lock_guard<std::mutex> lock(mtx_mapping_);
if (!system_is_running_) {
spdlog::critical("please call system::disable_mapping_module() after system::startup()");
}
// pause the mapping module
auto future_pause = mapper_->async_pause();
// wait until it stops
future_pause.get();
}
bool system::mapping_module_is_enabled() const {
return !mapper_->is_paused();
}
void system::enable_loop_detector() {
std::lock_guard<std::mutex> lock(mtx_loop_detector_);
global_optimizer_->enable_loop_detector();
}
void system::disable_loop_detector() {
std::lock_guard<std::mutex> lock(mtx_loop_detector_);
global_optimizer_->disable_loop_detector();
}
bool system::loop_detector_is_enabled() const {
return global_optimizer_->loop_detector_is_enabled();
}
bool system::request_loop_closure(int keyfrm1_id, int keyfrm2_id) {
return global_optimizer_->request_loop_closure(keyfrm1_id, keyfrm2_id);
}
bool system::loop_BA_is_running() const {
return global_optimizer_->loop_BA_is_running();
}
void system::abort_loop_BA() {
global_optimizer_->abort_loop_BA();
}
void system::enable_temporal_mapping() {
map_db_->set_fixed_keyframe_id_threshold();
}
data::frame system::create_monocular_frame(const cv::Mat& img, const double timestamp, const cv::Mat& mask) {
// color conversion
if (!camera_->is_valid_shape(img)) {
spdlog::warn("preprocess: Input image size is invalid");
}
cv::Mat img_gray = img;
util::convert_to_grayscale(img_gray, camera_->color_order_);
data::frame_observation frm_obs;
// Extract ORB feature
keypts_.clear();
extractor_left_->extract(img_gray, mask, keypts_, frm_obs.descriptors_);
frm_obs.num_keypts_ = keypts_.size();
if (keypts_.empty()) {
spdlog::warn("preprocess: cannot extract any keypoints");
}
// Undistort keypoints
camera_->undistort_keypoints(keypts_, frm_obs.undist_keypts_);
// Convert to bearing vector
camera_->convert_keypoints_to_bearings(frm_obs.undist_keypts_, frm_obs.bearings_);
// Assign all the keypoints into grid
data::assign_keypoints_to_grid(camera_, frm_obs.undist_keypts_, frm_obs.keypt_indices_in_cells_);
// Detect marker
std::unordered_map<unsigned int, data::marker2d> markers_2d;
if (marker_detector_) {
marker_detector_->detect(img_gray, markers_2d);
}
return data::frame(timestamp, camera_, orb_params_, frm_obs, std::move(markers_2d));
}
data::frame system::create_stereo_frame(const cv::Mat& left_img, const cv::Mat& right_img, const double timestamp, const cv::Mat& mask) {
// color conversion
if (!camera_->is_valid_shape(left_img)) {
spdlog::warn("preprocess: Input image size is invalid");
}
if (!camera_->is_valid_shape(right_img)) {
spdlog::warn("preprocess: Input image size is invalid");
}
cv::Mat img_gray = left_img;
cv::Mat right_img_gray = right_img;
util::convert_to_grayscale(img_gray, camera_->color_order_);
util::convert_to_grayscale(right_img_gray, camera_->color_order_);
data::frame_observation frm_obs;
//! keypoints of stereo right image
std::vector<cv::KeyPoint> keypts_right;
//! ORB descriptors of stereo right image
cv::Mat descriptors_right;
// Extract ORB feature
keypts_.clear();
std::thread thread_left([this, &frm_obs, &img_gray, &mask]() {
extractor_left_->extract(img_gray, mask, keypts_, frm_obs.descriptors_);
});
std::thread thread_right([this, &frm_obs, &right_img_gray, &mask, &keypts_right, &descriptors_right]() {
extractor_right_->extract(right_img_gray, mask, keypts_right, descriptors_right);
});
thread_left.join();
thread_right.join();
frm_obs.num_keypts_ = keypts_.size();
if (keypts_.empty()) {
spdlog::warn("preprocess: cannot extract any keypoints");
}
// Undistort keypoints
camera_->undistort_keypoints(keypts_, frm_obs.undist_keypts_);
// Estimate depth with stereo match
match::stereo stereo_matcher(extractor_left_->image_pyramid_, extractor_right_->image_pyramid_,
keypts_, keypts_right, frm_obs.descriptors_, descriptors_right,
orb_params_->scale_factors_, orb_params_->inv_scale_factors_,
camera_->focal_x_baseline_, camera_->true_baseline_);
stereo_matcher.compute(frm_obs.stereo_x_right_, frm_obs.depths_);
// Convert to bearing vector
camera_->convert_keypoints_to_bearings(frm_obs.undist_keypts_, frm_obs.bearings_);
// Assign all the keypoints into grid
data::assign_keypoints_to_grid(camera_, frm_obs.undist_keypts_, frm_obs.keypt_indices_in_cells_);
// Detect marker
std::unordered_map<unsigned int, data::marker2d> markers_2d;
if (marker_detector_) {
marker_detector_->detect(img_gray, markers_2d);
}
return data::frame(timestamp, camera_, orb_params_, frm_obs, std::move(markers_2d));
}
data::frame system::create_RGBD_frame(const cv::Mat& rgb_img, const cv::Mat& depthmap, const double timestamp, const cv::Mat& mask) {
// color and depth scale conversion
if (!camera_->is_valid_shape(rgb_img)) {
spdlog::warn("preprocess: Input image size is invalid");
}
if (!camera_->is_valid_shape(depthmap)) {
spdlog::warn("preprocess: Input image size is invalid");
}
cv::Mat img_gray = rgb_img;
cv::Mat img_depth = depthmap;
util::convert_to_grayscale(img_gray, camera_->color_order_);
util::convert_to_true_depth(img_depth, depthmap_factor_);
data::frame_observation frm_obs;
// Extract ORB feature
keypts_.clear();
extractor_left_->extract(img_gray, mask, keypts_, frm_obs.descriptors_);
frm_obs.num_keypts_ = keypts_.size();
if (keypts_.empty()) {
spdlog::warn("preprocess: cannot extract any keypoints");
}
// Undistort keypoints
camera_->undistort_keypoints(keypts_, frm_obs.undist_keypts_);
// Calculate disparity from depth
// Initialize with invalid value
frm_obs.stereo_x_right_ = std::vector<float>(frm_obs.num_keypts_, -1);
frm_obs.depths_ = std::vector<float>(frm_obs.num_keypts_, -1);
for (unsigned int idx = 0; idx < frm_obs.num_keypts_; idx++) {
const auto& keypt = keypts_.at(idx);
const auto& undist_keypt = frm_obs.undist_keypts_.at(idx);
const float x = keypt.pt.x;
const float y = keypt.pt.y;
const float depth = img_depth.at<float>(y, x);
if (depth <= 0) {
continue;
}
frm_obs.depths_.at(idx) = depth;
frm_obs.stereo_x_right_.at(idx) = undist_keypt.pt.x - camera_->focal_x_baseline_ / depth;
}
// Convert to bearing vector
camera_->convert_keypoints_to_bearings(frm_obs.undist_keypts_, frm_obs.bearings_);
// Assign all the keypoints into grid
data::assign_keypoints_to_grid(camera_, frm_obs.undist_keypts_, frm_obs.keypt_indices_in_cells_);
// Detect marker
std::unordered_map<unsigned int, data::marker2d> markers_2d;
if (marker_detector_) {
marker_detector_->detect(img_gray, markers_2d);
}
return data::frame(timestamp, camera_, orb_params_, frm_obs, std::move(markers_2d));
}
std::shared_ptr<Mat44_t> system::feed_monocular_frame(const cv::Mat& img, const double timestamp, const cv::Mat& mask) {
assert(camera_->setup_type_ == camera::setup_type_t::Monocular);
if (img.empty()) {
spdlog::warn("preprocess: empty image");
return nullptr;
}
return feed_frame(create_monocular_frame(img, timestamp, mask), img);
}
std::shared_ptr<Mat44_t> system::feed_stereo_frame(const cv::Mat& left_img, const cv::Mat& right_img, const double timestamp, const cv::Mat& mask) {
assert(camera_->setup_type_ == camera::setup_type_t::Stereo);
if (left_img.empty() || right_img.empty()) {
spdlog::warn("preprocess: empty image");
return nullptr;
}
return feed_frame(create_stereo_frame(left_img, right_img, timestamp, mask), left_img);
}
std::shared_ptr<Mat44_t> system::feed_RGBD_frame(const cv::Mat& rgb_img, const cv::Mat& depthmap, const double timestamp, const cv::Mat& mask) {
assert(camera_->setup_type_ == camera::setup_type_t::RGBD);
if (rgb_img.empty() || depthmap.empty()) {
spdlog::warn("preprocess: empty image");
return nullptr;
}
return feed_frame(create_RGBD_frame(rgb_img, depthmap, timestamp, mask), rgb_img);
}
std::shared_ptr<Mat44_t> system::feed_frame(const data::frame& frm, const cv::Mat& img) {
check_reset_request();
const auto start = std::chrono::system_clock::now();
const auto cam_pose_wc = tracker_->feed_frame(frm);
const auto end = std::chrono::system_clock::now();
double elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
frame_publisher_->update(tracker_->curr_frm_.get_landmarks(),
!mapper_->is_paused(),
tracker_->tracking_state_,
keypts_,
img,
elapsed_ms);
if (tracker_->tracking_state_ == tracker_state_t::Tracking && cam_pose_wc) {
map_publisher_->set_current_cam_pose(util::converter::inverse_pose(*cam_pose_wc));
}
return cam_pose_wc;
}
bool system::relocalize_by_pose(const Mat44_t& cam_pose_wc) {
const Mat44_t cam_pose_cw = util::converter::inverse_pose(cam_pose_wc);
bool status = tracker_->request_relocalize_by_pose(cam_pose_cw);
if (status) {
// Even if state will be lost, still update the pose in map_publisher_
// to clearly show new camera position
map_publisher_->set_current_cam_pose(cam_pose_cw);
}
return status;
}
bool system::relocalize_by_pose_2d(const Mat44_t& cam_pose_wc, const Vec3_t& normal_vector) {
const Mat44_t cam_pose_cw = util::converter::inverse_pose(cam_pose_wc);
bool status = tracker_->request_relocalize_by_pose_2d(cam_pose_cw, normal_vector);
if (status) {
// Even if state will be lost, still update the pose in map_publisher_
// to clearly show new camera position
map_publisher_->set_current_cam_pose(cam_pose_cw);
}
return status;
}
void system::pause_tracker() {
auto future_pause = tracker_->async_pause();
future_pause.get();
}
bool system::tracker_is_paused() const {
return tracker_->is_paused();
}
void system::resume_tracker() {
tracker_->resume();
}
void system::request_reset() {
std::lock_guard<std::mutex> lock(mtx_reset_);
reset_is_requested_ = true;
}
bool system::reset_is_requested() const {
std::lock_guard<std::mutex> lock(mtx_reset_);
return reset_is_requested_;
}
void system::request_terminate() {
std::lock_guard<std::mutex> lock(mtx_terminate_);
terminate_is_requested_ = true;
}
bool system::terminate_is_requested() const {
std::lock_guard<std::mutex> lock(mtx_terminate_);
return terminate_is_requested_;
}
camera::base* system::get_camera() const {
return camera_;
}
void system::check_reset_request() {
std::lock_guard<std::mutex> lock(mtx_reset_);
if (reset_is_requested_) {
tracker_->reset();
reset_is_requested_ = false;
}
}
void system::pause_other_threads() const {
// pause the mapping module
if (mapper_ && !mapper_->is_terminated()) {
auto future_pause = mapper_->async_pause();
while (future_pause.wait_for(std::chrono::milliseconds(5)) == std::future_status::timeout) {
if (mapper_->is_terminated()) {
break;
}
}
}
// pause the global optimization module
if (global_optimizer_ && !global_optimizer_->is_terminated()) {
auto future_pause = global_optimizer_->async_pause();
while (future_pause.wait_for(std::chrono::milliseconds(5)) == std::future_status::timeout) {
if (global_optimizer_->is_terminated()) {
break;
}
}
}
}
void system::resume_other_threads() const {
// resume the global optimization module
if (global_optimizer_) {
global_optimizer_->resume();
}
// resume the mapping module
if (mapper_) {
mapper_->resume();
}
}
} // namespace stella_vslam