OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadedKFVio.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2  * OKVIS - Open Keyframe-based Visual-Inertial SLAM
3  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of Autonomous Systems Lab / ETH Zurich nor the names of
14  * its contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Created on: Aug 21, 2014
30  * Author: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
31  * Modified: Andreas Forster (an.forster@gmail.com)
32  *********************************************************************************/
33 
41 #ifndef INCLUDE_OKVIS_THREADEDKFVIO_HPP_
42 #define INCLUDE_OKVIS_THREADEDKFVIO_HPP_
43 
44 #include <thread>
45 #include <mutex>
46 #include <condition_variable>
47 #include <atomic>
48 
50 #include <okvis/Measurements.hpp>
51 #include <okvis/Frontend.hpp>
52 #include <okvis/MultiFrame.hpp>
53 #include <okvis/Parameters.hpp>
54 #include <okvis/assert_macros.hpp>
55 
58 #include <okvis/VioVisualizer.hpp>
59 #include <okvis/timing/Timer.hpp>
61 
62 #ifdef USE_MOCK
63 #include <../test/MockVioFrontendInterface.hpp>
64 #include <../test/MockVioBackendInterface.hpp>
65 #else
66 #include <okvis/Estimator.hpp>
68 #endif
69 
71 namespace okvis {
72 
88 class ThreadedKFVio : public VioInterface {
89  public:
90 
91  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
92  OKVIS_DEFINE_EXCEPTION(Exception, std::runtime_error)
93 
94 #ifdef DEACTIVATE_TIMERS
96 #else
98 #endif
99 
100 #ifdef USE_MOCK
101 
105 
106 #else
107 
111  ThreadedKFVio(okvis::VioParameters& parameters);
112 #endif
113 
115  virtual ~ThreadedKFVio();
116 
119 
130  virtual bool addImage(const okvis::Time & stamp, size_t cameraIndex,
131  const cv::Mat & image,
132  const std::vector<cv::KeyPoint> * keypoints = 0,
133  bool* asKeyframe = 0);
134 
146  virtual bool addKeypoints(const okvis::Time & stamp, size_t cameraIndex,
147  const std::vector<cv::KeyPoint> & keypoints,
148  const std::vector<uint64_t> & landmarkIds,
149  const cv::Mat& descriptors = cv::Mat(),
150  bool* asKeyframe = 0);
151 
159  virtual bool addImuMeasurement(const okvis::Time & stamp,
160  const Eigen::Vector3d & alpha,
161  const Eigen::Vector3d & omega);
162 
171  virtual void addPositionMeasurement(
172  const okvis::Time & stamp, const Eigen::Vector3d & position,
173  const Eigen::Vector3d & positionOffset,
174  const Eigen::Matrix3d & positionCovariance);
175 
186  virtual void addGpsMeasurement(const okvis::Time & stamp,
187  double lat_wgs84_deg, double lon_wgs84_deg,
188  double alt_wgs84_deg,
189  const Eigen::Vector3d & positionOffset,
190  const Eigen::Matrix3d & positionCovarianceENU);
191 
199  virtual void addMagnetometerMeasurement(
200  const okvis::Time & stamp, const Eigen::Vector3d & fluxDensityMeas,
201  double stdev);
202 
210  virtual void addBarometerMeasurement(const okvis::Time & stamp,
211  double staticPressure, double stdev);
212 
220  virtual void addDifferentialPressureMeasurement(const okvis::Time & stamp,
221  double differentialPressure,
222  double stdev);
223 
227 
231  virtual void setBlocking(bool blocking);
232 
234 
236  void display();
237 
238  private:
240  virtual void startThreads();
242  void init();
243 
244  private:
245 
247  void frameConsumerLoop(size_t cameraIndex);
249  void matchingLoop();
251  void imuConsumerLoop();
254  void positionConsumerLoop();
257  void gpsConsumerLoop();
264 
266  void visualizationLoop();
268  void optimizationLoop();
270  void publisherLoop();
271 
280  okvis::Time& end);
281 
287  int deleteImuMeasurements(const okvis::Time& eraseUntil);
288 
289  private:
290 
295  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
299  Eigen::Matrix<double, 3, 1> omega_S;
302  Eigen::aligned_allocator<okvis::kinematics::Transformation> > vector_of_T_SCi;
306  };
307 
310 
316  std::shared_ptr<okvis::MapPointVector> map_;
317 
318  // lock lastState_mutex_ when accessing these
330  std::atomic_bool repropagationNeeded_;
331 
333 
338 
341 
342 
345 
347  std::vector<std::shared_ptr<
351 
354 
358 
375 
379 
383  std::mutex estimator_mutex_;
384  std::condition_variable optimizationNotification_;
387  std::atomic_bool optimizationDone_;
388  std::mutex lastState_mutex_;
389 
393 
395  std::vector<std::thread> frameConsumerThreads_;
396  std::vector<std::thread> keypointConsumerThreads_;
397  std::vector<std::thread> matchesConsumerThreads_;
398  std::thread imuConsumerThread_;
400  std::thread gpsConsumerThread_;
403 
407 
408  std::thread visualizationThread_;
409  std::thread optimizationThread_;
410  std::thread publisherThread_;
411 
415 
416 #ifdef USE_MOCK
419 #else
422 #endif
423 
425 
426  size_t numCameras_;
428 
430 
434  const size_t maxImuInputQueueSize_;
435 
437  const size_t maxPositionInputQueueSize_ = 10;
438 
439 };
440 
441 } // namespace okvis
442 
443 #endif /* INCLUDE_OKVIS_THREADEDKFVIO_HPP_ */
Eigen::Matrix< double, 3, 1 > omega_S
Definition: ThreadedKFVio.hpp:299
std::mutex lastState_mutex_
Lock when accessing any of the 'lastOptimized*' variables.
Definition: ThreadedKFVio.hpp:388
okvis::kinematics::Transformation T_WS_propagated_
The pose propagated by the IMU measurements.
Definition: ThreadedKFVio.hpp:315
The estimator class.
Definition: Estimator.hpp:77
Header file for the NCameraSystem class.
Definition: MockVioFrontendInterface.hpp:43
virtual bool addImuMeasurement(const okvis::Time &stamp, const Eigen::Vector3d &alpha, const Eigen::Vector3d &omega)
Add an IMU measurement.
Definition: ThreadedKFVio.cpp:244
okvis::threadsafe::ThreadSafeQueue< std::vector< cv::Mat > > displayImages_
The queue containing the actual display images.
Definition: ThreadedKFVio.hpp:374
std::deque< PositionMeasurement, Eigen::aligned_allocator< PositionMeasurement > > PositionMeasurementDeque
Definition: Measurements.hpp:188
This class is to safely notify different threads whether IMU measurements up to a timestamp (e...
Definition: ImuFrameSynchronizer.hpp:59
std::thread visualizationThread_
Thread running visualizationLoop().
Definition: ThreadedKFVio.hpp:408
int deleteImuMeasurements(const okvis::Time &eraseUntil)
Remove IMU measurements from the internal buffer.
Definition: ThreadedKFVio.cpp:700
ImuFrameSynchronizer imuFrameSynchronizer_
Definition: ThreadedKFVio.hpp:334
std::shared_ptr< okvis::MapPointVector > map_
The map. Unused.
Definition: ThreadedKFVio.hpp:316
okvis::Estimator estimator_
The backend estimator.
Definition: ThreadedKFVio.hpp:420
const size_t maxImuInputQueueSize_
Definition: ThreadedKFVio.hpp:434
void visualizationLoop()
Loop that visualizes completed frames.
Definition: ThreadedKFVio.cpp:631
std::deque< ImuMeasurement, Eigen::aligned_allocator< ImuMeasurement > > ImuMeasurementDeque
Definition: Measurements.hpp:163
void differentialConsumerLoop()
Loop to process differential pressure measurements.
Definition: ThreadedKFVio.cpp:627
virtual void addGpsMeasurement(const okvis::Time &stamp, double lat_wgs84_deg, double lon_wgs84_deg, double alt_wgs84_deg, const Eigen::Vector3d &positionOffset, const Eigen::Matrix3d &positionCovarianceENU)
Add a GPS measurement.
Definition: ThreadedKFVio.cpp:285
okvis::SpeedAndBias speedAndBiases
The speeds and biases.
Definition: ThreadedKFVio.hpp:298
okvis::SpeedAndBias lastOptimizedSpeedAndBiases_
Resulting speeds and IMU biases after last optimization.
Definition: ThreadedKFVio.hpp:324
std::thread magnetometerConsumerThread_
Thread running magnetometerConsumerLoop().
Definition: ThreadedKFVio.hpp:401
ThreadedKFVio(okvis::VioParameters &parameters)
Constructor.
Definition: ThreadedKFVio.cpp:73
A frontend using BRISK features.
Definition: Frontend.hpp:57
Header file for the MultiFrame class.
void imuConsumerLoop()
Loop to process IMU measurements.
Definition: ThreadedKFVio.cpp:542
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef okvis::timing::Timer TimerSwitchable
Definition: ThreadedKFVio.hpp:97
okvis::Time lastOptimizedStateTimestamp_
Timestamp of newest frame used in the last optimization.
Definition: ThreadedKFVio.hpp:327
std::atomic_bool repropagationNeeded_
Definition: ThreadedKFVio.hpp:330
Header file for the VioVisualizer class.
Eigen::Matrix< double, 9, 1 > SpeedAndBias
Definition: FrameTypedefs.hpp:234
Definition: Timer.hpp:104
size_t numCameraPairs_
Number of camera pairs in the system.
Definition: ThreadedKFVio.hpp:427
void frameConsumerLoop(size_t cameraIndex)
Loop to process frames from camera with index cameraIndex.
Definition: ThreadedKFVio.cpp:322
Definition: MockVioBackendInterface.hpp:44
This file contains the templated measurement structs, structs encapsulating Sensor data and related t...
size_t numCameras_
Number of cameras in the system.
Definition: ThreadedKFVio.hpp:426
A class that does homogeneous transformations. This relates a frame A and B: T_AB; it consists of tra...
Definition: Transformation.hpp:76
const size_t maxPositionInputQueueSize_
Max position measurements before dropping.
Definition: ThreadedKFVio.hpp:437
std::condition_variable optimizationNotification_
Definition: ThreadedKFVio.hpp:385
virtual ~ThreadedKFVio()
Destructor. This calls Shutdown() for all threadsafe queues and joins all threads.
Definition: ThreadedKFVio.cpp:152
Definition: Timer.hpp:90
okvis::MapPointVector transferredLandmarks
Vector of the landmarks that have been marginalized out.
Definition: ThreadedKFVio.hpp:304
std::vector< MapPoint, Eigen::aligned_allocator< MapPoint > > MapPointVector
Definition: FrameTypedefs.hpp:172
virtual void setBlocking(bool blocking)
Set the blocking variable that indicates whether the addMeasurement() functions should return immedia...
Definition: ThreadedKFVio.cpp:312
Header file for the ImuFrameSynchronizer class.
okvis::threadsafe::ThreadSafeQueue< okvis::PositionMeasurement > positionMeasurementsReceived_
Position measurement input queue.
Definition: ThreadedKFVio.hpp:353
std::mutex frameSynchronizer_mutex_
Lock when accessing the frameSynchronizer_.
Definition: ThreadedKFVio.hpp:382
okvis::PositionMeasurementDeque positionMeasurements_
The Position measurements.
Definition: ThreadedKFVio.hpp:368
okvis::FrameSynchronizer frameSynchronizer_
The frame synchronizer responsible for merging frames into multiframes.
Definition: ThreadedKFVio.hpp:337
okvis::ImuMeasurementDeque imuMeasurements_
The IMU measurements.
Definition: ThreadedKFVio.hpp:365
#define OKVIS_DEFINE_EXCEPTION(exceptionName, exceptionParent)
Macro for defining an exception with a given parent.
Definition: assert_macros.hpp:52
bool onlyPublishLandmarks
Boolean to signalise the publisherLoop() that only the landmarks should be published.
Definition: ThreadedKFVio.hpp:305
This file contains some useful assert macros.
This struct contains the results of the optimization for ease of publication. It is also used for pub...
Definition: ThreadedKFVio.hpp:294
void display()
Trigger display (needed because OSX won't allow threaded display).
Definition: ThreadedKFVio.cpp:647
std::mutex positionMeasurements_mutex_
Lock when accessing imuMeasurements_.
Definition: ThreadedKFVio.hpp:381
virtual bool addKeypoints(const okvis::Time &stamp, size_t cameraIndex, const std::vector< cv::KeyPoint > &keypoints, const std::vector< uint64_t > &landmarkIds, const cv::Mat &descriptors=cv::Mat(), bool *asKeyframe=0)
Add an abstracted image observation.
Definition: ThreadedKFVio.cpp:231
void matchingLoop()
Loop that matches frames with existing frames.
Definition: ThreadedKFVio.cpp:456
void publisherLoop()
Loop that publishes the newest state and landmarks.
Definition: ThreadedKFVio.cpp:857
okvis::MapPointVector landmarksVector
Vector containing the current landmarks.
Definition: ThreadedKFVio.hpp:303
Struct to combine all parameters and settings.
Definition: Parameters.hpp:280
Header file for the ThreadsafeQueue class.
virtual void addDifferentialPressureMeasurement(const okvis::Time &stamp, double differentialPressure, double stdev)
Add a differential pressure measurement.
Definition: ThreadedKFVio.cpp:304
std::thread gpsConsumerThread_
Thread running gpsConsumerLoop().
Definition: ThreadedKFVio.hpp:400
Class that implements a threadsafe FIFO queue.
Definition: ThreadsafeQueue.hpp:74
okvis::Time lastAddedImageTimestamp_
Timestamp of the newest image added to the image input queue.
Definition: ThreadedKFVio.hpp:340
void init()
Initialises settings and calls startThreads().
Definition: ThreadedKFVio.cpp:92
virtual void addPositionMeasurement(const okvis::Time &stamp, const Eigen::Vector3d &position, const Eigen::Vector3d &positionOffset, const Eigen::Matrix3d &positionCovariance)
Add a position measurement.
Definition: ThreadedKFVio.cpp:264
std::vector< std::thread > matchesConsumerThreads_
Unused.
Definition: ThreadedKFVio.hpp:397
std::vector< std::shared_ptr< okvis::threadsafe::ThreadSafeQueue< std::shared_ptr< okvis::CameraMeasurement > > > > cameraMeasurementsReceived_
Camera measurement input queues. For each camera in the configuration one.
Definition: ThreadedKFVio.hpp:348
std::thread imuConsumerThread_
Thread running imuConsumerLoop().
Definition: ThreadedKFVio.hpp:398
EIGEN_MAKE_ALIGNED_OPERATOR_NEW okvis::Time stamp
Timestamp of the optimized/propagated pose.
Definition: ThreadedKFVio.hpp:296
virtual void startThreads()
Start all threads.
Definition: ThreadedKFVio.cpp:127
void gpsConsumerLoop()
Loop to process GPS measurements.
Definition: ThreadedKFVio.cpp:619
okvis::threadsafe::ThreadSafeQueue< okvis::ImuMeasurement > imuMeasurementsReceived_
IMU measurement input queue.
Definition: ThreadedKFVio.hpp:350
virtual void addBarometerMeasurement(const okvis::Time &stamp, double staticPressure, double stdev)
Add a static pressure measurement.
Definition: ThreadedKFVio.cpp:298
std::thread publisherThread_
Thread running publisherLoop().
Definition: ThreadedKFVio.hpp:410
virtual void addMagnetometerMeasurement(const okvis::Time &stamp, const Eigen::Vector3d &fluxDensityMeas, double stdev)
Add a magnetometer measurement.
Definition: ThreadedKFVio.cpp:292
okvis::ImuMeasurementDeque getImuMeasurments(okvis::Time &start, okvis::Time &end)
Get a subset of the recorded IMU measurements.
Definition: ThreadedKFVio.cpp:663
okvis::threadsafe::ThreadSafeQueue< std::shared_ptr< okvis::MultiFrame > > keypointMeasurements_
The queue containing multiframes with completely detected frames.
Definition: ThreadedKFVio.hpp:360
okvis::kinematics::Transformation lastOptimized_T_WS_
Resulting pose of the last optimization.
Definition: ThreadedKFVio.hpp:321
An abstract base class for interfaces between Front- and Backend.
Definition: VioInterface.hpp:66
Header file for the FrameSynchronizer class.
Header file for the Estimator class. This does all the backend work.
IMU parameters.
Definition: Parameters.hpp:105
This class combines multiple frames with the same or similar timestamp into one multiframe.
Definition: FrameSynchronizer.hpp:60
Header file for the Frontend class.
std::vector< std::thread > frameConsumerThreads_
Threads running the frameConsumerLoop(). One per camera.
Definition: ThreadedKFVio.hpp:395
std::thread differentialConsumerThread_
Thread running differentialConsumerLoop().
Definition: ThreadedKFVio.hpp:402
std::vector< std::thread > keypointConsumerThreads_
Threads running matchingLoop().
Definition: ThreadedKFVio.hpp:396
okvis::threadsafe::ThreadSafeQueue< std::shared_ptr< okvis::MultiFrame > > matchedFrames_
The queue containing multiframes with completely matched frames. These are already part of the estima...
Definition: ThreadedKFVio.hpp:362
Time representation. May either represent wall clock time or ROS clock time.
Definition: Time.hpp:187
void optimizationLoop()
Loop that performs the optimization and marginalisation.
Definition: ThreadedKFVio.cpp:720
virtual bool addImage(const okvis::Time &stamp, size_t cameraIndex, const cv::Mat &image, const std::vector< cv::KeyPoint > *keypoints=0, bool *asKeyframe=0)
Add a new image.
Definition: ThreadedKFVio.cpp:192
This file contains struct definitions that encapsulate parameters and settings.
Header file for the VioFrontendInterface class.
std::atomic_bool optimizationDone_
Boolean flag for whether optimization is done for the last state that has been added to the estimator...
Definition: ThreadedKFVio.hpp:387
This class manages the complete data flow in and out of the algorithm, as well as between the process...
Definition: ThreadedKFVio.hpp:88
std::vector< okvis::kinematics::Transformation, Eigen::aligned_allocator< okvis::kinematics::Transformation > > vector_of_T_SCi
The relative transformation of the cameras to the sensor (IMU) frame.
Definition: ThreadedKFVio.hpp:302
okvis::SpeedAndBias speedAndBiases_propagated_
Definition: ThreadedKFVio.hpp:311
std::thread positionConsumerThread_
Thread running positionConsumerLoop().
Definition: ThreadedKFVio.hpp:399
okvis::Frontend frontend_
The frontend.
Definition: ThreadedKFVio.hpp:421
okvis::threadsafe::ThreadSafeQueue< OptimizationResults > optimizationResults_
The queue containing the results of the optimization or IMU propagation ready for publishing...
Definition: ThreadedKFVio.hpp:370
okvis::kinematics::Transformation T_WS
The pose.
Definition: ThreadedKFVio.hpp:297
okvis::ImuParameters imu_params_
The IMU parameters.
Definition: ThreadedKFVio.hpp:314
void positionConsumerLoop()
Loop to process position measurements.
Definition: ThreadedKFVio.cpp:604
std::thread optimizationThread_
Thread running optimizationLoop().
Definition: ThreadedKFVio.hpp:409
okvis::threadsafe::ThreadSafeQueue< VioVisualizer::VisualizationData::Ptr > visualizationData_
The queue containing visualization data that is ready to be displayed.
Definition: ThreadedKFVio.hpp:372
okvis::VioParameters parameters_
The parameters and settings.
Definition: ThreadedKFVio.hpp:429
std::mutex imuMeasurements_mutex_
Lock when accessing imuMeasurements_.
Definition: ThreadedKFVio.hpp:380
okvis::Time lastAddedStateTimestamp_
Timestamp of the newest state in the Estimator.
Definition: ThreadedKFVio.hpp:339
void magnetometerConsumerLoop()
Loop to process magnetometer measurements.
Definition: ThreadedKFVio.cpp:623
std::mutex estimator_mutex_
Condition variable to signalise that optimization is done.
Definition: ThreadedKFVio.hpp:383