My Project 1.0
Holistic Fusion: A Generic Framework for Multimodal Sensor Fusion
Loading...
Searching...
No Matches
TimeGraphKeyBuffer.cpp
Go to the documentation of this file.
1/*
2Copyright 2023 by Julian Nubert, Robotic Systems Lab, ETH Zurich.
3All rights reserved.
4This file is released under the "BSD-3-Clause License".
5Please see the LICENSE file that has been included as part of this package.
6 */
7
8// Workspace
11
12namespace holistic_fusion {
13
14void TimeGraphKeyBuffer::addToBuffer(const double ts, const gtsam::Key& key) {
15 if (verboseLevel_ >= 5) {
16 std::cout << YELLOW_START << "GMsf-TimeKeyBuffer" << COLOR_END << " Adding key " << key << " to timeToKeyBuffer for time "
17 << std::setprecision(14) << ts << std::endl;
18 }
19
20 // Mutex block
21 {
22 // Writing to IMU buffer --> acquire mutex
23 const std::lock_guard<std::mutex> writeInBufferLock(writeInBufferMutex_);
24 timeToKeyBuffer_[ts] = key;
25 keyToTimeBuffer_[key] = ts;
26 if (ts > tLatestInBuffer_) {
28 }
29 }
30
31 // If Key buffer is too large, remove first element
32 if (timeToKeyBuffer_.size() > bufferLength_) {
35 }
36}
37
38bool TimeGraphKeyBuffer::getClosestKeyAndTimestamp(double& tInGraph, gtsam::Key& key, const std::string& callingName,
39 const double maxSearchDeviation, const double tK) {
40 std::_Rb_tree_iterator<std::pair<const double, gtsam::Key>> upperIterator;
41 {
42 // Read from IMU buffer --> acquire mutex
43 const std::lock_guard<std::mutex> writeInBufferLock(writeInBufferMutex_);
44 upperIterator = timeToKeyBuffer_.upper_bound(tK);
45 }
46
47 // Empty buffer
48 if (timeToKeyBuffer_.empty()) {
49 std::cerr << YELLOW_START << "GMsf-TimeKeyBuffer " << RED_START << "called from " << callingName << ": Buffer is empty!" << COLOR_END
50 << std::endl;
51 return false;
52 }
53
54 auto lowerIterator = upperIterator;
55 --lowerIterator;
56
57 // Keep key which is closer to tLidar
58 tInGraph = std::abs(tK - lowerIterator->first) < std::abs(upperIterator->first - tK) ? lowerIterator->first : upperIterator->first;
59 key = std::abs(tK - lowerIterator->first) < std::abs(upperIterator->first - tK) ? lowerIterator->second : upperIterator->second;
60 double timeDeviation = tInGraph - tK;
61
62 if (verboseLevel_ >= 2) {
63 std::cout << YELLOW_START << "GMsf-TimeKeyBuffer" << COLOR_END << " " << callingName << std::setprecision(14)
64 << " searched time step: " << tK << std::endl;
65 std::cout << YELLOW_START << "GMsf-TimeKeyBuffer" << COLOR_END << " " << callingName << std::setprecision(14)
66 << " found time step: " << tInGraph << " at key " << key << std::endl;
67 std::cout << YELLOW_START << "GMsf-TimeKeyBuffer" << COLOR_END << " Time Deviation (t_graph-t_request): " << 1000 * timeDeviation
68 << " ms" << std::endl;
69 std::cout << YELLOW_START << "GMsf-TimeKeyBuffer" << COLOR_END << " Latest IMU timestamp: " << tLatestInBuffer_
70 << ", hence absolut delay of measurement is " << 1000 * (tLatestInBuffer_ - tK) << "ms." << std::endl;
71 }
72
73 // Check for error and warn user
74 if (std::abs(timeDeviation) > maxSearchDeviation) {
75 return false;
76 }
77
78 return true;
79}
80
81} // namespace holistic_fusion
#define COLOR_END
#define RED_START
#define YELLOW_START
void addToBuffer(const double ts, const gtsam::Key &key)
bool getClosestKeyAndTimestamp(double &tInGraph, gtsam::Key &key, const std::string &callingName, const double maxSearchDeviation, const double tK)