forked from AaltoML/SLAM-module
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathslam_implementation.cpp
More file actions
230 lines (196 loc) · 6.76 KB
/
slam_implementation.cpp
File metadata and controls
230 lines (196 loc) · 6.76 KB
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
#include <cmath>
#include <deque>
#include <mutex>
#include <thread>
#include <Eigen/Geometry>
#include <opencv2/core.hpp>
#include "slam_implementation.hpp"
#include "mapper.hpp"
#include "viewer_data_publisher.hpp"
#include "keyframe.hpp"
#include "../commandline/command_queue.hpp"
#include "../odometry/parameters.hpp"
#include "../util/logging.hpp"
#include "../util/string_utils.hpp"
#include "../tracker/image.hpp"
namespace slam {
namespace {
class Worker {
public:
using Result = Slam::Result;
struct Task {
struct Mapper {
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
std::shared_ptr<const MapperInput> mapperInput;
// using a pointer here is an optimization that primarily reduces
// reallocation of PointCloud objects
std::promise<Result> *result = nullptr;
};
struct End {
std::promise<bool> result;
};
std::unique_ptr<Mapper> mapperInput;
std::unique_ptr<End> end;
static std::unique_ptr<Task> wrap(std::unique_ptr<Mapper> mapperInput) {
auto task = std::make_unique<Task>();
task->mapperInput = std::move(mapperInput);
return task;
}
static std::unique_ptr<Task> wrap(std::unique_ptr<End> mapperInput) {
auto task = std::make_unique<Task>();
task->end = std::move(mapperInput);
return task;
}
};
Worker(const odometry::Parameters ¶meters) :
mapper(Mapper::create(parameters)),
resultStorage(100), // MAX_QUEUED_RESULTS = 100
shouldQuit(false),
mutex(),
// Make sure thread is initialized last, because it can immediately call work() before this constructor finishes
thread(parameters.slam.slamThread ?
std::make_unique<std::thread>(&Worker::work, this) :
nullptr)
{}
~Worker() {
if (thread) {
log_debug("signaling the SLAM thead to quit");
{
std::unique_lock<std::mutex> lock(mutex);
shouldQuit = true;
}
log_debug("waiting for the SLAM thead to quit");
thread->join();
}
log_debug("~Worker end");
}
std::future<Result> enqueueMapperInput(std::unique_ptr<Task::Mapper> task) {
std::promise<Result> *result;
auto wrapped = Task::wrap(std::move(task));
if (!thread) {
result = &nextResult();
wrapped->mapperInput->result = result;
process(*wrapped);
} else {
std::unique_lock<std::mutex> lock(mutex);
workQueue.push_back(std::move(wrapped));
result = &nextResult();
workQueue.back()->mapperInput->result = result;
}
return result->get_future();
}
std::future<bool> end() {
std::unique_ptr<Worker::Task::End> endTask(
new Worker::Task::End {
.result = std::promise<bool>(),
});
std::promise<bool> &result = endTask->result;
auto wrapped = Task::wrap(std::move(endTask));
if (!thread) {
process(*wrapped);
} else {
std::unique_lock<std::mutex> lock(mutex);
workQueue.push_back(std::move(wrapped));
}
return result.get_future();
}
void connectDebugAPI(DebugAPI &debug) {
// NOTE: not thread-safe, but called in practice before anything
// is going on in the worker thread
mapper->connectDebugAPI(debug);
mapSavePath = debug.mapSavePath;
}
private:
std::promise<Result> &nextResult() {
auto &result = resultStorage.at(resultCounter);
result = {};
resultCounter = (resultCounter + 1) % resultStorage.size();
return result;
}
std::unique_ptr<Mapper> mapper;
// TODO: not sure if this is really necessary
std::vector<std::promise<Result>> resultStorage;
int resultCounter = 0;
bool shouldQuit;
std::mutex mutex;
std::deque<std::unique_ptr<Task>> workQueue;
std::unique_ptr<std::thread> thread; // Can call work() before constructor finishes, at the very least keep it last in constructor.
std::string mapSavePath;
void work() {
log_debug("starting SLAM thread");
while (true) {
{
std::unique_lock<std::mutex> lock(mutex);
if (shouldQuit) return;
}
std::unique_ptr<Task> task;
{
std::unique_lock<std::mutex> lock(mutex);
if (!workQueue.empty()) {
task = std::move(workQueue.front());
workQueue.pop_front();
}
}
if (task) {
process(*task);
}
else {
// TODO: bad, should properly wait for new elements
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
void process(Task &typedTask) {
if (typedTask.mapperInput) {
Result result;
Eigen::Matrix4d resultPoseMat;
auto &task = *typedTask.mapperInput;
mapper->advance(task.mapperInput, resultPoseMat, result.pointCloud);
result.poseMat = resultPoseMat;
task.result->set_value(result);
}
if (typedTask.end) {
const bool success = mapper->end(mapSavePath);
typedTask.end->result.set_value(success);
log_debug("end processed");
}
}
};
class SlamImplementation : public Slam {
private:
std::unique_ptr<Worker> worker;
public:
SlamImplementation(const odometry::Parameters ¶meters) :
worker(new Worker(parameters))
{}
void connectDebugAPI(DebugAPI &debug) final {
worker->connectDebugAPI(debug);
}
std::future<Slam::Result> addFrame(
std::shared_ptr<tracker::Image> frame,
const std::vector<slam::Pose> &poseTrail,
const std::vector<Feature> &features,
const cv::Mat &colorFrame
) final {
auto mapperInput = std::unique_ptr<MapperInput>(new MapperInput);
mapperInput->frame = frame;
mapperInput->trackerFeatures = features;
mapperInput->poseTrail = poseTrail;
mapperInput->t = poseTrail[0].t;
mapperInput->colorFrame = colorFrame;
return worker->enqueueMapperInput(std::unique_ptr<Worker::Task::Mapper>(
new Worker::Task::Mapper {
.mapperInput = std::move(mapperInput)
}
));
}
std::future<bool> end() final {
log_debug("SlamImplementation::end");
return worker->end();
}
};
}
std::unique_ptr<Slam> Slam::build(const odometry::Parameters ¶meters) {
return std::make_unique<SlamImplementation>(parameters);
}
} // namespace slam