Camera.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
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  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "rtabmap/core/Camera.h"
29 
32 #include <rtabmap/utilite/UStl.h>
34 #include <rtabmap/utilite/UFile.h>
36 #include <rtabmap/utilite/UTimer.h>
37 
38 #include <opencv2/imgproc/imgproc.hpp>
39 
40 #include <iostream>
41 #include <cmath>
42 
43 namespace rtabmap
44 {
45 
46 Camera::Camera(float imageRate, const Transform & localTransform) :
47  _imageRate(imageRate),
48  _localTransform(localTransform),
49  _targetImageSize(0,0),
50  _frameRateTimer(new UTimer()),
51  _seq(0)
52 {
53 }
54 
56 {
57  UDEBUG("");
58  delete _frameRateTimer;
59  UDEBUG("");
60 }
61 
63 {
65 }
66 
67 bool Camera::initFromFile(const std::string & calibrationPath)
68 {
69  return init(UDirectory::getDir(calibrationPath), uSplit(UFile::getName(calibrationPath), '.').front());
70 }
71 
73 {
74  bool warnFrameRateTooHigh = false;
75  float actualFrameRate = 0;
76  float imageRate = _imageRate;
77  if(imageRate>0)
78  {
79  int sleepTime = (1000.0f/imageRate - 1000.0f*_frameRateTimer->getElapsedTime());
80  if(sleepTime > 2)
81  {
82  uSleep(sleepTime-2);
83  }
84  else if(sleepTime < 0)
85  {
86  warnFrameRateTooHigh = true;
87  actualFrameRate = 1.0/(_frameRateTimer->getElapsedTime());
88  }
89 
90  // Add precision at the cost of a small overhead
91  while(_frameRateTimer->getElapsedTime() < 1.0/double(imageRate)-0.000001)
92  {
93  //
94  }
95 
96  double slept = _frameRateTimer->getElapsedTime();
98  UDEBUG("slept=%fs vs target=%fs", slept, 1.0/double(imageRate));
99  }
100 
101  UTimer timer;
102  SensorData data = this->captureImage(info);
103  double captureTime = timer.ticks();
104  if(warnFrameRateTooHigh)
105  {
106  UWARN("Camera: Cannot reach target image rate %f Hz, current rate is %f Hz and capture time = %f s.",
107  imageRate, actualFrameRate, captureTime);
108  }
109  else
110  {
111  UDEBUG("Time capturing image = %fs", captureTime);
112  }
113  if(info)
114  {
115  info->id = data.id();
116  info->stamp = data.stamp();
117  info->timeCapture = captureTime;
118  }
119  return data;
120 }
121 
122 } // namespace rtabmap
Definition: UTimer.h:46
std::string getName()
Definition: UFile.h:135
static std::string getDir(const std::string &filePath)
Definition: UDirectory.cpp:273
void resetTimer()
Definition: Camera.cpp:62
Some conversion functions.
virtual ~Camera()
Definition: Camera.cpp:55
bool initFromFile(const std::string &calibrationPath)
Definition: Camera.cpp:67
Camera(float imageRate=0, const Transform &localTransform=CameraModel::opticalRotation())
Definition: Camera.cpp:46
double getElapsedTime()
Definition: UTimer.cpp:97
virtual SensorData captureImage(CameraInfo *info=0)=0
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:566
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")=0
SensorData takeImage(CameraInfo *info=0)
Definition: Camera.cpp:72
Wrappers of STL for convenient functions.
void start()
Definition: UTimer.cpp:87
int id() const
Definition: SensorData.h:155
double stamp() const
Definition: SensorData.h:157
UTimer * _frameRateTimer
Definition: Camera.h:90
void uSleep(unsigned int ms)
float _imageRate
Definition: Camera.h:87
#define UDEBUG(...)
#define UWARN(...)
double ticks()
Definition: UTimer.cpp:117


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:58